blob: adec61905fa555cb85e2da177b9598d77b999679 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Jamie Madill6b9cb252013-10-17 10:45:47 -04007#include "compiler/translator/ParseContext.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00008
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009#include <stdarg.h>
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000010#include <stdio.h>
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011
jchen104cdac9e2017-05-08 11:01:20 +080012#include "common/mathutil.h"
daniel@transgaming.comb401a922012-10-26 18:58:24 +000013#include "compiler/preprocessor/SourceLocation.h"
Dmitry Skiba01971112015-07-10 14:54:00 -040014#include "compiler/translator/Cache.h"
Olli Etuaho3ec75682017-07-05 17:02:55 +030015#include "compiler/translator/IntermNode_util.h"
Olli Etuahob0c645e2015-05-12 14:25:36 +030016#include "compiler/translator/ValidateGlobalInitializer.h"
jchen104cdac9e2017-05-08 11:01:20 +080017#include "compiler/translator/ValidateSwitch.h"
18#include "compiler/translator/glslang.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030019#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000020
Jamie Madill45bcc782016-11-07 13:58:48 -050021namespace sh
22{
23
alokp@chromium.org8b851c62012-06-15 16:25:11 +000024///////////////////////////////////////////////////////////////////////
25//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000026// Sub- vector and matrix fields
27//
28////////////////////////////////////////////////////////////////////////
29
Martin Radev2cc85b32016-08-05 16:22:53 +030030namespace
31{
32
33const int kWebGLMaxStructNesting = 4;
34
Olli Etuaho0f684632017-07-13 12:42:15 +030035bool ContainsSampler(const TStructure *structType);
36
Martin Radev2cc85b32016-08-05 16:22:53 +030037bool ContainsSampler(const TType &type)
38{
39 if (IsSampler(type.getBasicType()))
Olli Etuaho0f684632017-07-13 12:42:15 +030040 {
Martin Radev2cc85b32016-08-05 16:22:53 +030041 return true;
Olli Etuaho0f684632017-07-13 12:42:15 +030042 }
jchen10cc2a10e2017-05-03 14:05:12 +080043 if (type.getBasicType() == EbtStruct)
Martin Radev2cc85b32016-08-05 16:22:53 +030044 {
Olli Etuaho0f684632017-07-13 12:42:15 +030045 return ContainsSampler(type.getStruct());
Martin Radev2cc85b32016-08-05 16:22:53 +030046 }
47
48 return false;
49}
50
Olli Etuaho0f684632017-07-13 12:42:15 +030051bool ContainsSampler(const TStructure *structType)
52{
53 for (const auto &field : structType->fields())
54 {
55 if (ContainsSampler(*field->type()))
56 return true;
57 }
58 return false;
59}
60
Olli Etuaho485eefd2017-02-14 17:40:06 +000061// Get a token from an image argument to use as an error message token.
62const char *GetImageArgumentToken(TIntermTyped *imageNode)
63{
64 ASSERT(IsImage(imageNode->getBasicType()));
65 while (imageNode->getAsBinaryNode() &&
66 (imageNode->getAsBinaryNode()->getOp() == EOpIndexIndirect ||
67 imageNode->getAsBinaryNode()->getOp() == EOpIndexDirect))
68 {
69 imageNode = imageNode->getAsBinaryNode()->getLeft();
70 }
71 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
72 if (imageSymbol)
73 {
74 return imageSymbol->getSymbol().c_str();
75 }
76 return "image";
77}
78
Olli Etuahocce89652017-06-19 16:04:09 +030079bool CanSetDefaultPrecisionOnType(const TPublicType &type)
80{
81 if (!SupportsPrecision(type.getBasicType()))
82 {
83 return false;
84 }
85 if (type.getBasicType() == EbtUInt)
86 {
87 // ESSL 3.00.4 section 4.5.4
88 return false;
89 }
90 if (type.isAggregate())
91 {
92 // Not allowed to set for aggregate types
93 return false;
94 }
95 return true;
96}
97
Jiawei Shaod8105a02017-08-08 09:54:36 +080098// Map input primitive types to input array sizes in a geometry shader.
99GLuint GetGeometryShaderInputArraySize(TLayoutPrimitiveType primitiveType)
100{
101 switch (primitiveType)
102 {
103 case EptPoints:
104 return 1u;
105 case EptLines:
106 return 2u;
107 case EptTriangles:
108 return 3u;
109 case EptLinesAdjacency:
110 return 4u;
111 case EptTrianglesAdjacency:
112 return 6u;
113 default:
114 UNREACHABLE();
115 return 0u;
116 }
117}
118
Martin Radev2cc85b32016-08-05 16:22:53 +0300119} // namespace
120
jchen104cdac9e2017-05-08 11:01:20 +0800121// This tracks each binding point's current default offset for inheritance of subsequent
122// variables using the same binding, and keeps offsets unique and non overlapping.
123// See GLSL ES 3.1, section 4.4.6.
124class TParseContext::AtomicCounterBindingState
125{
126 public:
127 AtomicCounterBindingState() : mDefaultOffset(0) {}
128 // Inserts a new span and returns -1 if overlapping, else returns the starting offset of
129 // newly inserted span.
130 int insertSpan(int start, size_t length)
131 {
132 gl::RangeI newSpan(start, start + static_cast<int>(length));
133 for (const auto &span : mSpans)
134 {
135 if (newSpan.intersects(span))
136 {
137 return -1;
138 }
139 }
140 mSpans.push_back(newSpan);
141 mDefaultOffset = newSpan.high();
142 return start;
143 }
144 // Inserts a new span starting from the default offset.
145 int appendSpan(size_t length) { return insertSpan(mDefaultOffset, length); }
146 void setDefaultOffset(int offset) { mDefaultOffset = offset; }
147
148 private:
149 int mDefaultOffset;
150 std::vector<gl::RangeI> mSpans;
151};
152
Jamie Madillacb4b812016-11-07 13:50:29 -0500153TParseContext::TParseContext(TSymbolTable &symt,
154 TExtensionBehavior &ext,
155 sh::GLenum type,
156 ShShaderSpec spec,
157 ShCompileOptions options,
158 bool checksPrecErrors,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000159 TDiagnostics *diagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500160 const ShBuiltInResources &resources)
Olli Etuaho56229f12017-07-10 14:16:33 +0300161 : symbolTable(symt),
Olli Etuahobb7e5a72017-04-24 10:16:44 +0300162 mDeferredNonEmptyDeclarationErrorCheck(false),
Jamie Madillacb4b812016-11-07 13:50:29 -0500163 mShaderType(type),
164 mShaderSpec(spec),
165 mCompileOptions(options),
166 mShaderVersion(100),
167 mTreeRoot(nullptr),
168 mLoopNestingLevel(0),
169 mStructNestingLevel(0),
170 mSwitchNestingLevel(0),
171 mCurrentFunctionType(nullptr),
172 mFunctionReturnsValue(false),
173 mChecksPrecisionErrors(checksPrecErrors),
174 mFragmentPrecisionHighOnESSL1(false),
Jiajia Qinbc585152017-06-23 15:42:17 +0800175 mDefaultUniformMatrixPacking(EmpColumnMajor),
176 mDefaultUniformBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
177 mDefaultBufferMatrixPacking(EmpColumnMajor),
178 mDefaultBufferBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000179 mDiagnostics(diagnostics),
Jamie Madillacb4b812016-11-07 13:50:29 -0500180 mDirectiveHandler(ext,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000181 *mDiagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500182 mShaderVersion,
183 mShaderType,
184 resources.WEBGL_debug_shader_precision == 1),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000185 mPreprocessor(mDiagnostics, &mDirectiveHandler, pp::PreprocessorSettings()),
Jamie Madillacb4b812016-11-07 13:50:29 -0500186 mScanner(nullptr),
187 mUsesFragData(false),
188 mUsesFragColor(false),
189 mUsesSecondaryOutputs(false),
190 mMinProgramTexelOffset(resources.MinProgramTexelOffset),
191 mMaxProgramTexelOffset(resources.MaxProgramTexelOffset),
Martin Radev84aa2dc2017-09-11 15:51:02 +0300192 mMinProgramTextureGatherOffset(resources.MinProgramTextureGatherOffset),
193 mMaxProgramTextureGatherOffset(resources.MaxProgramTextureGatherOffset),
Jamie Madillacb4b812016-11-07 13:50:29 -0500194 mComputeShaderLocalSizeDeclared(false),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000195 mNumViews(-1),
196 mMaxNumViews(resources.MaxViewsOVR),
Olli Etuaho43364892017-02-13 16:00:12 +0000197 mMaxImageUnits(resources.MaxImageUnits),
198 mMaxCombinedTextureImageUnits(resources.MaxCombinedTextureImageUnits),
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000199 mMaxUniformLocations(resources.MaxUniformLocations),
jchen10af713a22017-04-19 09:10:56 +0800200 mMaxUniformBufferBindings(resources.MaxUniformBufferBindings),
jchen104cdac9e2017-05-08 11:01:20 +0800201 mMaxAtomicCounterBindings(resources.MaxAtomicCounterBindings),
Jiajia Qinbc585152017-06-23 15:42:17 +0800202 mMaxShaderStorageBufferBindings(resources.MaxShaderStorageBufferBindings),
Shaob5cc1192017-07-06 10:47:20 +0800203 mDeclaringFunction(false),
204 mGeometryShaderInputPrimitiveType(EptUndefined),
205 mGeometryShaderOutputPrimitiveType(EptUndefined),
206 mGeometryShaderInvocations(0),
207 mGeometryShaderMaxVertices(-1),
208 mMaxGeometryShaderInvocations(resources.MaxGeometryShaderInvocations),
Jiawei Shaod8105a02017-08-08 09:54:36 +0800209 mMaxGeometryShaderMaxVertices(resources.MaxGeometryOutputVertices),
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800210 mGeometryShaderInputArraySize(0u)
Jamie Madillacb4b812016-11-07 13:50:29 -0500211{
212 mComputeShaderLocalSize.fill(-1);
213}
214
jchen104cdac9e2017-05-08 11:01:20 +0800215TParseContext::~TParseContext()
216{
217}
218
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300219bool TParseContext::parseVectorFields(const TSourceLoc &line,
220 const TString &compString,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400221 int vecSize,
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300222 TVector<int> *fieldOffsets)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000223{
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300224 ASSERT(fieldOffsets);
225 size_t fieldCount = compString.size();
226 if (fieldCount > 4u)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530227 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000228 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000229 return false;
230 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300231 fieldOffsets->resize(fieldCount);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000232
Jamie Madillb98c3a82015-07-23 14:26:04 -0400233 enum
234 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000235 exyzw,
236 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +0000237 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000238 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000239
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300240 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530241 {
242 switch (compString[i])
243 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400244 case 'x':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300245 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400246 fieldSet[i] = exyzw;
247 break;
248 case 'r':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300249 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400250 fieldSet[i] = ergba;
251 break;
252 case 's':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300253 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400254 fieldSet[i] = estpq;
255 break;
256 case 'y':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300257 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400258 fieldSet[i] = exyzw;
259 break;
260 case 'g':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300261 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400262 fieldSet[i] = ergba;
263 break;
264 case 't':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300265 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400266 fieldSet[i] = estpq;
267 break;
268 case 'z':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300269 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400270 fieldSet[i] = exyzw;
271 break;
272 case 'b':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300273 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400274 fieldSet[i] = ergba;
275 break;
276 case 'p':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300277 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400278 fieldSet[i] = estpq;
279 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530280
Jamie Madillb98c3a82015-07-23 14:26:04 -0400281 case 'w':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300282 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400283 fieldSet[i] = exyzw;
284 break;
285 case 'a':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300286 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400287 fieldSet[i] = ergba;
288 break;
289 case 'q':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300290 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400291 fieldSet[i] = estpq;
292 break;
293 default:
294 error(line, "illegal vector field selection", compString.c_str());
295 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000296 }
297 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000298
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300299 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530300 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300301 if ((*fieldOffsets)[i] >= vecSize)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530302 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400303 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000304 return false;
305 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000306
Arun Patole7e7e68d2015-05-22 12:02:25 +0530307 if (i > 0)
308 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400309 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530310 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400311 error(line, "illegal - vector component fields not from the same set",
312 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000313 return false;
314 }
315 }
316 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000317
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000318 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000319}
320
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000321///////////////////////////////////////////////////////////////////////
322//
323// Errors
324//
325////////////////////////////////////////////////////////////////////////
326
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000327//
328// Used by flex/bison to output all syntax and parsing errors.
329//
Olli Etuaho4de340a2016-12-16 09:32:03 +0000330void TParseContext::error(const TSourceLoc &loc, const char *reason, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000331{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000332 mDiagnostics->error(loc, reason, token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000333}
334
Olli Etuaho4de340a2016-12-16 09:32:03 +0000335void TParseContext::warning(const TSourceLoc &loc, const char *reason, const char *token)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530336{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000337 mDiagnostics->warning(loc, reason, token);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000338}
339
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200340void TParseContext::outOfRangeError(bool isError,
341 const TSourceLoc &loc,
342 const char *reason,
Olli Etuaho4de340a2016-12-16 09:32:03 +0000343 const char *token)
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200344{
345 if (isError)
346 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000347 error(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200348 }
349 else
350 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000351 warning(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200352 }
353}
354
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000355//
356// Same error message for all places assignments don't work.
357//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530358void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000359{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000360 std::stringstream reasonStream;
361 reasonStream << "cannot convert from '" << right << "' to '" << left << "'";
362 std::string reason = reasonStream.str();
363 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000364}
365
366//
367// Same error message for all places unary operations don't work.
368//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530369void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000370{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000371 std::stringstream reasonStream;
372 reasonStream << "wrong operand type - no operation '" << op
373 << "' exists that takes an operand of type " << operand
374 << " (or there is no acceptable conversion)";
375 std::string reason = reasonStream.str();
376 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000377}
378
379//
380// Same error message for all binary operations don't work.
381//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400382void TParseContext::binaryOpError(const TSourceLoc &line,
383 const char *op,
384 TString left,
385 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000386{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000387 std::stringstream reasonStream;
388 reasonStream << "wrong operand types - no operation '" << op
389 << "' exists that takes a left-hand operand of type '" << left
390 << "' and a right operand of type '" << right
391 << "' (or there is no acceptable conversion)";
392 std::string reason = reasonStream.str();
393 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000394}
395
Olli Etuaho856c4972016-08-08 11:38:39 +0300396void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
397 TPrecision precision,
398 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530399{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400400 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300401 return;
Martin Radev70866b82016-07-22 15:27:42 +0300402
403 if (precision != EbpUndefined && !SupportsPrecision(type))
404 {
405 error(line, "illegal type for precision qualifier", getBasicString(type));
406 }
407
Olli Etuaho183d7e22015-11-20 15:59:09 +0200408 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530409 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200410 switch (type)
411 {
412 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400413 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300414 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200415 case EbtInt:
416 case EbtUInt:
417 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400418 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300419 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200420 default:
jchen10cc2a10e2017-05-03 14:05:12 +0800421 if (IsOpaqueType(type))
Olli Etuaho183d7e22015-11-20 15:59:09 +0200422 {
jchen10cc2a10e2017-05-03 14:05:12 +0800423 error(line, "No precision specified", getBasicString(type));
Martin Radev2cc85b32016-08-05 16:22:53 +0300424 return;
425 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200426 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000427 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000428}
429
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000430// Both test and if necessary, spit out an error, to see if the node is really
431// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300432bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000433{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500434 TIntermSymbol *symNode = node->getAsSymbolNode();
435 TIntermBinary *binaryNode = node->getAsBinaryNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100436 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
437
438 if (swizzleNode)
439 {
440 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
441 if (ok && swizzleNode->hasDuplicateOffsets())
442 {
443 error(line, " l-value of swizzle cannot have duplicate components", op);
444 return false;
445 }
446 return ok;
447 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000448
Arun Patole7e7e68d2015-05-22 12:02:25 +0530449 if (binaryNode)
450 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400451 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530452 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400453 case EOpIndexDirect:
454 case EOpIndexIndirect:
455 case EOpIndexDirectStruct:
456 case EOpIndexDirectInterfaceBlock:
Olli Etuaho856c4972016-08-08 11:38:39 +0300457 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400458 default:
459 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000460 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000461 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300462 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000463 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000464
jchen10cc2a10e2017-05-03 14:05:12 +0800465 std::string message;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530466 switch (node->getQualifier())
467 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400468 case EvqConst:
469 message = "can't modify a const";
470 break;
471 case EvqConstReadOnly:
472 message = "can't modify a const";
473 break;
474 case EvqAttribute:
475 message = "can't modify an attribute";
476 break;
477 case EvqFragmentIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400478 case EvqVertexIn:
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800479 case EvqGeometryIn:
Jiawei Shaoe8ef2bc2017-08-29 13:38:57 +0800480 case EvqFlatIn:
481 case EvqSmoothIn:
482 case EvqCentroidIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400483 message = "can't modify an input";
484 break;
485 case EvqUniform:
486 message = "can't modify a uniform";
487 break;
488 case EvqVaryingIn:
489 message = "can't modify a varying";
490 break;
491 case EvqFragCoord:
492 message = "can't modify gl_FragCoord";
493 break;
494 case EvqFrontFacing:
495 message = "can't modify gl_FrontFacing";
496 break;
497 case EvqPointCoord:
498 message = "can't modify gl_PointCoord";
499 break;
Martin Radevb0883602016-08-04 17:48:58 +0300500 case EvqNumWorkGroups:
501 message = "can't modify gl_NumWorkGroups";
502 break;
503 case EvqWorkGroupSize:
504 message = "can't modify gl_WorkGroupSize";
505 break;
506 case EvqWorkGroupID:
507 message = "can't modify gl_WorkGroupID";
508 break;
509 case EvqLocalInvocationID:
510 message = "can't modify gl_LocalInvocationID";
511 break;
512 case EvqGlobalInvocationID:
513 message = "can't modify gl_GlobalInvocationID";
514 break;
515 case EvqLocalInvocationIndex:
516 message = "can't modify gl_LocalInvocationIndex";
517 break;
Olli Etuaho7142f6c2017-05-05 17:07:26 +0300518 case EvqViewIDOVR:
519 message = "can't modify gl_ViewID_OVR";
520 break;
Martin Radev802abe02016-08-04 17:48:32 +0300521 case EvqComputeIn:
522 message = "can't modify work group size variable";
523 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +0800524 case EvqPerVertexIn:
525 message = "can't modify any member in gl_in";
526 break;
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800527 case EvqPrimitiveIDIn:
528 message = "can't modify gl_PrimitiveIDIn";
529 break;
530 case EvqInvocationID:
531 message = "can't modify gl_InvocationID";
532 break;
533 case EvqPrimitiveID:
534 if (mShaderType == GL_FRAGMENT_SHADER)
535 {
536 message = "can't modify gl_PrimitiveID in a fragment shader";
537 }
538 break;
539 case EvqLayer:
540 if (mShaderType == GL_FRAGMENT_SHADER)
541 {
542 message = "can't modify gl_Layer in a fragment shader";
543 }
544 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400545 default:
546 //
547 // Type that can't be written to?
548 //
549 if (node->getBasicType() == EbtVoid)
550 {
551 message = "can't modify void";
552 }
jchen10cc2a10e2017-05-03 14:05:12 +0800553 if (IsOpaqueType(node->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -0400554 {
jchen10cc2a10e2017-05-03 14:05:12 +0800555 message = "can't modify a variable with type ";
556 message += getBasicString(node->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300557 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800558 else if (node->getMemoryQualifier().readonly)
559 {
560 message = "can't modify a readonly variable";
561 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000562 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000563
jchen10cc2a10e2017-05-03 14:05:12 +0800564 if (message.empty() && binaryNode == 0 && symNode == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530565 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000566 error(line, "l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000567
Olli Etuaho8a176262016-08-16 14:23:01 +0300568 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000569 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000570
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000571 //
572 // Everything else is okay, no error.
573 //
jchen10cc2a10e2017-05-03 14:05:12 +0800574 if (message.empty())
Olli Etuaho8a176262016-08-16 14:23:01 +0300575 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000576
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000577 //
578 // If we get here, we have an error and a message.
579 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530580 if (symNode)
581 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000582 const char *symbol = symNode->getSymbol().c_str();
583 std::stringstream reasonStream;
584 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
585 std::string reason = reasonStream.str();
586 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000587 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530588 else
589 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000590 std::stringstream reasonStream;
591 reasonStream << "l-value required (" << message << ")";
592 std::string reason = reasonStream.str();
593 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000594 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000595
Olli Etuaho8a176262016-08-16 14:23:01 +0300596 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000597}
598
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000599// Both test, and if necessary spit out an error, to see if the node is really
600// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300601void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000602{
Olli Etuaho383b7912016-08-05 11:22:59 +0300603 if (node->getQualifier() != EvqConst)
604 {
605 error(node->getLine(), "constant expression required", "");
606 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000607}
608
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000609// Both test, and if necessary spit out an error, to see if the node is really
610// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300611void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000612{
Olli Etuaho383b7912016-08-05 11:22:59 +0300613 if (!node->isScalarInt())
614 {
615 error(node->getLine(), "integer expression required", token);
616 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000617}
618
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000619// Both test, and if necessary spit out an error, to see if we are currently
620// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800621bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000622{
Olli Etuaho856c4972016-08-08 11:38:39 +0300623 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300624 {
625 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800626 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300627 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800628 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000629}
630
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300631// ESSL 3.00.5 sections 3.8 and 3.9.
632// If it starts "gl_" or contains two consecutive underscores, it's reserved.
633// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300634bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000635{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530636 static const char *reservedErrMsg = "reserved built-in name";
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300637 if (identifier.compare(0, 3, "gl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530638 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300639 error(line, reservedErrMsg, "gl_");
640 return false;
641 }
642 if (sh::IsWebGLBasedSpec(mShaderSpec))
643 {
644 if (identifier.compare(0, 6, "webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530645 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300646 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300647 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000648 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300649 if (identifier.compare(0, 7, "_webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530650 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300651 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300652 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000653 }
654 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300655 if (identifier.find("__") != TString::npos)
656 {
657 error(line,
658 "identifiers containing two consecutive underscores (__) are reserved as "
659 "possible future keywords",
660 identifier.c_str());
661 return false;
662 }
Olli Etuaho8a176262016-08-16 14:23:01 +0300663 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000664}
665
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300666// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300667bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800668 const TIntermSequence *arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300669 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000670{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800671 if (arguments->empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530672 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200673 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300674 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000675 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200676
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300677 for (TIntermNode *arg : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530678 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300679 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200680 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300681 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200682 {
jchen10cc2a10e2017-05-03 14:05:12 +0800683 std::string reason("cannot convert a variable with type ");
684 reason += getBasicString(argTyped->getBasicType());
685 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300686 return false;
687 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800688 else if (argTyped->getMemoryQualifier().writeonly)
689 {
690 error(line, "cannot convert a variable with writeonly", "constructor");
691 return false;
692 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200693 if (argTyped->getBasicType() == EbtVoid)
694 {
695 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300696 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200697 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000698 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000699
Olli Etuaho856c4972016-08-08 11:38:39 +0300700 if (type.isArray())
701 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300702 // The size of an unsized constructor should already have been determined.
703 ASSERT(!type.isUnsizedArray());
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300704 if (static_cast<size_t>(type.getOutermostArraySize()) != arguments->size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300705 {
706 error(line, "array constructor needs one argument per array element", "constructor");
707 return false;
708 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300709 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
710 // the array.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800711 for (TIntermNode *const &argNode : *arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300712 {
713 const TType &argType = argNode->getAsTyped()->getType();
Jamie Madill34bf2d92017-02-06 13:40:59 -0500714 if (argType.isArray())
715 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300716 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500717 return false;
718 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300719 if (!argType.isElementTypeOf(type))
Olli Etuaho856c4972016-08-08 11:38:39 +0300720 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000721 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300722 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300723 }
724 }
725 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300726 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300727 {
728 const TFieldList &fields = type.getStruct()->fields();
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300729 if (fields.size() != arguments->size())
730 {
731 error(line,
732 "Number of constructor parameters does not match the number of structure fields",
733 "constructor");
734 return false;
735 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300736
737 for (size_t i = 0; i < fields.size(); i++)
738 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800739 if (i >= arguments->size() ||
740 (*arguments)[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300741 {
742 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000743 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300744 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300745 }
746 }
747 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300748 else
749 {
750 // We're constructing a scalar, vector, or matrix.
751
752 // Note: It's okay to have too many components available, but not okay to have unused
753 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
754 // there is an extra argument, so 'overFull' will become true.
755
756 size_t size = 0;
757 bool full = false;
758 bool overFull = false;
759 bool matrixArg = false;
760 for (TIntermNode *arg : *arguments)
761 {
762 const TIntermTyped *argTyped = arg->getAsTyped();
763 ASSERT(argTyped != nullptr);
764
Olli Etuaho487b63a2017-05-23 15:55:09 +0300765 if (argTyped->getBasicType() == EbtStruct)
766 {
767 error(line, "a struct cannot be used as a constructor argument for this type",
768 "constructor");
769 return false;
770 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300771 if (argTyped->getType().isArray())
772 {
773 error(line, "constructing from a non-dereferenced array", "constructor");
774 return false;
775 }
776 if (argTyped->getType().isMatrix())
777 {
778 matrixArg = true;
779 }
780
781 size += argTyped->getType().getObjectSize();
782 if (full)
783 {
784 overFull = true;
785 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300786 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300787 {
788 full = true;
789 }
790 }
791
792 if (type.isMatrix() && matrixArg)
793 {
794 if (arguments->size() != 1)
795 {
796 error(line, "constructing matrix from matrix can only take one argument",
797 "constructor");
798 return false;
799 }
800 }
801 else
802 {
803 if (size != 1 && size < type.getObjectSize())
804 {
805 error(line, "not enough data provided for construction", "constructor");
806 return false;
807 }
808 if (overFull)
809 {
810 error(line, "too many arguments", "constructor");
811 return false;
812 }
813 }
814 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300815
Olli Etuaho8a176262016-08-16 14:23:01 +0300816 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000817}
818
Jamie Madillb98c3a82015-07-23 14:26:04 -0400819// This function checks to see if a void variable has been declared and raise an error message for
820// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000821//
822// returns true in case of an error
823//
Olli Etuaho856c4972016-08-08 11:38:39 +0300824bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400825 const TString &identifier,
826 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000827{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300828 if (type == EbtVoid)
829 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000830 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300831 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300832 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000833
Olli Etuaho8a176262016-08-16 14:23:01 +0300834 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000835}
836
Jamie Madillb98c3a82015-07-23 14:26:04 -0400837// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300838// or not.
Olli Etuaho56229f12017-07-10 14:16:33 +0300839bool TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000840{
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300841 if (type->getBasicType() != EbtBool || !type->isScalar())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530842 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000843 error(line, "boolean expression expected", "");
Olli Etuaho56229f12017-07-10 14:16:33 +0300844 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530845 }
Olli Etuaho56229f12017-07-10 14:16:33 +0300846 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000847}
848
Jamie Madillb98c3a82015-07-23 14:26:04 -0400849// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300850// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300851void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000852{
Martin Radev4a9cd802016-09-01 16:51:51 +0300853 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530854 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000855 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530856 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857}
858
jchen10cc2a10e2017-05-03 14:05:12 +0800859bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
860 const TTypeSpecifierNonArray &pType,
861 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530863 if (pType.type == EbtStruct)
864 {
Olli Etuaho0f684632017-07-13 12:42:15 +0300865 if (ContainsSampler(pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530866 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000867 std::stringstream reasonStream;
868 reasonStream << reason << " (structure contains a sampler)";
869 std::string reasonStr = reasonStream.str();
870 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300871 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000872 }
jchen10cc2a10e2017-05-03 14:05:12 +0800873 // only samplers need to be checked from structs, since other opaque types can't be struct
874 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300875 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530876 }
jchen10cc2a10e2017-05-03 14:05:12 +0800877 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530878 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000879 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300880 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000881 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000882
Olli Etuaho8a176262016-08-16 14:23:01 +0300883 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000884}
885
Olli Etuaho856c4972016-08-08 11:38:39 +0300886void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
887 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400888{
889 if (pType.layoutQualifier.location != -1)
890 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400891 error(line, "location must only be specified for a single input or output variable",
892 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400893 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400894}
895
Olli Etuaho856c4972016-08-08 11:38:39 +0300896void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
897 const TLayoutQualifier &layoutQualifier)
898{
899 if (layoutQualifier.location != -1)
900 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000901 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
902 if (mShaderVersion >= 310)
903 {
904 errorMsg =
Jiawei Shao4cc89e22017-08-31 14:25:54 +0800905 "invalid layout qualifier: only valid on shader inputs, outputs, and uniforms";
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000906 }
907 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300908 }
909}
910
Qin Jiajiaca68d982017-09-18 16:41:56 +0800911void TParseContext::checkStd430IsForShaderStorageBlock(const TSourceLoc &location,
912 const TLayoutBlockStorage &blockStorage,
913 const TQualifier &qualifier)
914{
915 if (blockStorage == EbsStd430 && qualifier != EvqBuffer)
916 {
917 error(location, "The std430 layout is supported only for shader storage blocks.", "std430");
918 }
919}
920
Martin Radev2cc85b32016-08-05 16:22:53 +0300921void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
922 TQualifier qualifier,
923 const TType &type)
924{
Martin Radev2cc85b32016-08-05 16:22:53 +0300925 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800926 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530927 {
jchen10cc2a10e2017-05-03 14:05:12 +0800928 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000929 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000930}
931
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000932// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300933unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000934{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530935 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000936
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200937 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
938 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
939 // fold as array size.
940 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000941 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000942 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300943 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000944 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000945
Olli Etuaho856c4972016-08-08 11:38:39 +0300946 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400947
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000948 if (constant->getBasicType() == EbtUInt)
949 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300950 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000951 }
952 else
953 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300954 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000955
Olli Etuaho856c4972016-08-08 11:38:39 +0300956 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000957 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400958 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300959 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000960 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400961
Olli Etuaho856c4972016-08-08 11:38:39 +0300962 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400963 }
964
Olli Etuaho856c4972016-08-08 11:38:39 +0300965 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400966 {
967 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300968 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400969 }
970
971 // The size of arrays is restricted here to prevent issues further down the
972 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
973 // 4096 registers so this should be reasonable even for aggressively optimizable code.
974 const unsigned int sizeLimit = 65536;
975
Olli Etuaho856c4972016-08-08 11:38:39 +0300976 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400977 {
978 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300979 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000980 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300981
982 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000983}
984
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000985// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300986bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
987 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000988{
Olli Etuaho8a176262016-08-16 14:23:01 +0300989 if ((elementQualifier.qualifier == EvqAttribute) ||
990 (elementQualifier.qualifier == EvqVertexIn) ||
991 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300992 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400993 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300994 TType(elementQualifier).getQualifierString());
995 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000996 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000997
Olli Etuaho8a176262016-08-16 14:23:01 +0300998 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000999}
1000
Olli Etuaho8a176262016-08-16 14:23:01 +03001001// See if this element type can be formed into an array.
Olli Etuahoe0803872017-08-23 15:30:23 +03001002bool TParseContext::checkArrayElementIsNotArray(const TSourceLoc &line,
1003 const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001004{
Olli Etuaho8a176262016-08-16 14:23:01 +03001005 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -04001006 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001007 error(line, "cannot declare arrays of arrays",
1008 TType(elementType).getCompleteString().c_str());
1009 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001010 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001011 return true;
1012}
1013
1014// Check if this qualified element type can be formed into an array. This is only called when array
1015// brackets are associated with an identifier in a declaration, like this:
1016// float a[2];
1017// Similar checks are done in addFullySpecifiedType for array declarations where the array brackets
1018// are associated with the type, like this:
1019// float[2] a;
1020bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
1021 const TPublicType &elementType)
1022{
1023 if (!checkArrayElementIsNotArray(indexLocation, elementType))
1024 {
1025 return false;
1026 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001027 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
1028 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
1029 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +03001030 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +03001031 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +03001032 {
Olli Etuahoe0803872017-08-23 15:30:23 +03001033 error(indexLocation, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001034 TType(elementType).getCompleteString().c_str());
1035 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +03001036 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001037 return checkIsValidQualifierForArray(indexLocation, elementType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001038}
1039
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001040// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001041void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
1042 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001043 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001044{
Olli Etuaho3739d232015-04-08 12:23:44 +03001045 ASSERT(type != nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03001046 if (type->getQualifier() == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001047 {
1048 // Make the qualifier make sense.
Olli Etuaho55bde912017-10-25 13:41:13 +03001049 type->setQualifier(EvqTemporary);
Olli Etuaho3739d232015-04-08 12:23:44 +03001050
1051 // Generate informative error messages for ESSL1.
1052 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001053 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001054 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301055 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001056 "structures containing arrays may not be declared constant since they cannot be "
1057 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +05301058 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001059 }
1060 else
1061 {
1062 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
1063 }
Olli Etuaho383b7912016-08-05 11:22:59 +03001064 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001065 }
Olli Etuaho55bde912017-10-25 13:41:13 +03001066 // This will make the type sized if it isn't sized yet.
1067 checkIsNotUnsizedArray(line, "implicitly sized arrays need to be initialized",
1068 identifier.c_str(), type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001069}
1070
Olli Etuaho2935c582015-04-08 14:32:06 +03001071// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001072// and update the symbol table.
1073//
Olli Etuaho2935c582015-04-08 14:32:06 +03001074// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001075//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001076bool TParseContext::declareVariable(const TSourceLoc &line,
1077 const TString &identifier,
1078 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001079 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001080{
Olli Etuaho2935c582015-04-08 14:32:06 +03001081 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001082
Olli Etuaho43364892017-02-13 16:00:12 +00001083 checkBindingIsValid(line, type);
1084
Olli Etuaho856c4972016-08-08 11:38:39 +03001085 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001086
Olli Etuaho2935c582015-04-08 14:32:06 +03001087 // gl_LastFragData may be redeclared with a new precision qualifier
1088 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1089 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001090 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1091 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001092 if (type.isArrayOfArrays())
1093 {
1094 error(line, "redeclaration of gl_LastFragData as an array of arrays",
1095 identifier.c_str());
1096 return false;
1097 }
1098 else if (static_cast<int>(type.getOutermostArraySize()) ==
1099 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001100 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001101 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001102 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001103 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001104 }
1105 }
1106 else
1107 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001108 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1109 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001110 return false;
1111 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001112 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001113
Olli Etuaho8a176262016-08-16 14:23:01 +03001114 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001115 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001116
Olli Etuaho0f684632017-07-13 12:42:15 +03001117 (*variable) = symbolTable.declareVariable(&identifier, type);
1118 if (!(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001119 {
1120 error(line, "redefinition", identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001121 return false;
1122 }
1123
Olli Etuaho8a176262016-08-16 14:23:01 +03001124 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001125 return false;
1126
1127 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001128}
1129
Martin Radev70866b82016-07-22 15:27:42 +03001130void TParseContext::checkIsParameterQualifierValid(
1131 const TSourceLoc &line,
1132 const TTypeQualifierBuilder &typeQualifierBuilder,
1133 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301134{
Olli Etuahocce89652017-06-19 16:04:09 +03001135 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001136 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001137
1138 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301139 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001140 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1141 }
1142
1143 if (!IsImage(type->getBasicType()))
1144 {
Olli Etuaho43364892017-02-13 16:00:12 +00001145 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001146 }
1147 else
1148 {
1149 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001150 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001151
Martin Radev70866b82016-07-22 15:27:42 +03001152 type->setQualifier(typeQualifier.qualifier);
1153
1154 if (typeQualifier.precision != EbpUndefined)
1155 {
1156 type->setPrecision(typeQualifier.precision);
1157 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001158}
1159
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001160bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001161{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001162 ASSERT(extension != TExtension::UNDEFINED);
Jamie Madillb98c3a82015-07-23 14:26:04 -04001163 const TExtensionBehavior &extBehavior = extensionBehavior();
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001164 TExtensionBehavior::const_iterator iter = extBehavior.find(extension);
Arun Patole7e7e68d2015-05-22 12:02:25 +05301165 if (iter == extBehavior.end())
1166 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001167 error(line, "extension is not supported", GetExtensionNameString(extension));
Olli Etuaho8a176262016-08-16 14:23:01 +03001168 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001169 }
zmo@google.comf5450912011-09-09 01:37:19 +00001170 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +05301171 if (iter->second == EBhDisable || iter->second == EBhUndefined)
1172 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001173 error(line, "extension is disabled", GetExtensionNameString(extension));
Olli Etuaho8a176262016-08-16 14:23:01 +03001174 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001175 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301176 if (iter->second == EBhWarn)
1177 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001178 warning(line, "extension is being used", GetExtensionNameString(extension));
Olli Etuaho8a176262016-08-16 14:23:01 +03001179 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001180 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001181
Olli Etuaho8a176262016-08-16 14:23:01 +03001182 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001183}
1184
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001185// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1186// compile-time or link-time errors are the same whether or not the declaration is empty".
1187// This function implements all the checks that are done on qualifiers regardless of if the
1188// declaration is empty.
1189void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1190 const sh::TLayoutQualifier &layoutQualifier,
1191 const TSourceLoc &location)
1192{
1193 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1194 {
1195 error(location, "Shared memory declarations cannot have layout specified", "layout");
1196 }
1197
1198 if (layoutQualifier.matrixPacking != EmpUnspecified)
1199 {
1200 error(location, "layout qualifier only valid for interface blocks",
1201 getMatrixPackingString(layoutQualifier.matrixPacking));
1202 return;
1203 }
1204
1205 if (layoutQualifier.blockStorage != EbsUnspecified)
1206 {
1207 error(location, "layout qualifier only valid for interface blocks",
1208 getBlockStorageString(layoutQualifier.blockStorage));
1209 return;
1210 }
1211
1212 if (qualifier == EvqFragmentOut)
1213 {
1214 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1215 {
1216 error(location, "invalid layout qualifier combination", "yuv");
1217 return;
1218 }
1219 }
1220 else
1221 {
1222 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1223 }
1224
Olli Etuaho95468d12017-05-04 11:14:34 +03001225 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1226 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001227 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1228 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001229 {
1230 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1231 }
1232
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001233 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001234 if (mShaderVersion >= 310)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001235 {
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001236 canHaveLocation = canHaveLocation || qualifier == EvqUniform || IsVarying(qualifier);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001237 // We're not checking whether the uniform location is in range here since that depends on
1238 // the type of the variable.
1239 // The type can only be fully determined for non-empty declarations.
1240 }
1241 if (!canHaveLocation)
1242 {
1243 checkLocationIsNotSpecified(location, layoutQualifier);
1244 }
1245}
1246
jchen104cdac9e2017-05-08 11:01:20 +08001247void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1248 const TSourceLoc &location)
1249{
1250 if (publicType.precision != EbpHigh)
1251 {
1252 error(location, "Can only be highp", "atomic counter");
1253 }
1254 // dEQP enforces compile error if location is specified. See uniform_location.test.
1255 if (publicType.layoutQualifier.location != -1)
1256 {
1257 error(location, "location must not be set for atomic_uint", "layout");
1258 }
1259 if (publicType.layoutQualifier.binding == -1)
1260 {
1261 error(location, "no binding specified", "atomic counter");
1262 }
1263}
1264
Olli Etuaho55bde912017-10-25 13:41:13 +03001265void TParseContext::emptyDeclarationErrorCheck(const TType &type, const TSourceLoc &location)
Martin Radevb8b01222016-11-20 23:25:53 +02001266{
Olli Etuaho55bde912017-10-25 13:41:13 +03001267 if (type.isUnsizedArray())
Martin Radevb8b01222016-11-20 23:25:53 +02001268 {
1269 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1270 // error. It is assumed that this applies to empty declarations as well.
1271 error(location, "empty array declaration needs to specify a size", "");
1272 }
Martin Radevb8b01222016-11-20 23:25:53 +02001273}
1274
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001275// These checks are done for all declarations that are non-empty. They're done for non-empty
1276// declarations starting a declarator list, and declarators that follow an empty declaration.
1277void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1278 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001279{
Olli Etuahofa33d582015-04-09 14:33:12 +03001280 switch (publicType.qualifier)
1281 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001282 case EvqVaryingIn:
1283 case EvqVaryingOut:
1284 case EvqAttribute:
1285 case EvqVertexIn:
1286 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001287 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001288 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001289 {
1290 error(identifierLocation, "cannot be used with a structure",
1291 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001292 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001293 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001294 break;
1295 case EvqBuffer:
1296 if (publicType.getBasicType() != EbtInterfaceBlock)
1297 {
1298 error(identifierLocation,
1299 "cannot declare buffer variables at global scope(outside a block)",
1300 getQualifierString(publicType.qualifier));
1301 return;
1302 }
1303 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001304 default:
1305 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001306 }
jchen10cc2a10e2017-05-03 14:05:12 +08001307 std::string reason(getBasicString(publicType.getBasicType()));
1308 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001309 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001310 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001311 {
1312 return;
1313 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001314
Andrei Volykhina5527072017-03-22 16:46:30 +03001315 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1316 publicType.qualifier != EvqConst) &&
1317 publicType.getBasicType() == EbtYuvCscStandardEXT)
1318 {
1319 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1320 getQualifierString(publicType.qualifier));
1321 return;
1322 }
1323
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001324 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1325 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001326 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1327 // But invalid shaders may still reach here with an unsized array declaration.
Olli Etuaho55bde912017-10-25 13:41:13 +03001328 TType type(publicType);
1329 if (!type.isUnsizedArray())
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001330 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001331 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1332 publicType.layoutQualifier);
1333 }
1334 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001335
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001336 // check for layout qualifier issues
1337 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001338
Martin Radev2cc85b32016-08-05 16:22:53 +03001339 if (IsImage(publicType.getBasicType()))
1340 {
1341
1342 switch (layoutQualifier.imageInternalFormat)
1343 {
1344 case EiifRGBA32F:
1345 case EiifRGBA16F:
1346 case EiifR32F:
1347 case EiifRGBA8:
1348 case EiifRGBA8_SNORM:
1349 if (!IsFloatImage(publicType.getBasicType()))
1350 {
1351 error(identifierLocation,
1352 "internal image format requires a floating image type",
1353 getBasicString(publicType.getBasicType()));
1354 return;
1355 }
1356 break;
1357 case EiifRGBA32I:
1358 case EiifRGBA16I:
1359 case EiifRGBA8I:
1360 case EiifR32I:
1361 if (!IsIntegerImage(publicType.getBasicType()))
1362 {
1363 error(identifierLocation,
1364 "internal image format requires an integer image type",
1365 getBasicString(publicType.getBasicType()));
1366 return;
1367 }
1368 break;
1369 case EiifRGBA32UI:
1370 case EiifRGBA16UI:
1371 case EiifRGBA8UI:
1372 case EiifR32UI:
1373 if (!IsUnsignedImage(publicType.getBasicType()))
1374 {
1375 error(identifierLocation,
1376 "internal image format requires an unsigned image type",
1377 getBasicString(publicType.getBasicType()));
1378 return;
1379 }
1380 break;
1381 case EiifUnspecified:
1382 error(identifierLocation, "layout qualifier", "No image internal format specified");
1383 return;
1384 default:
1385 error(identifierLocation, "layout qualifier", "unrecognized token");
1386 return;
1387 }
1388
1389 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1390 switch (layoutQualifier.imageInternalFormat)
1391 {
1392 case EiifR32F:
1393 case EiifR32I:
1394 case EiifR32UI:
1395 break;
1396 default:
1397 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1398 {
1399 error(identifierLocation, "layout qualifier",
1400 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1401 "image variables must be qualified readonly and/or writeonly");
1402 return;
1403 }
1404 break;
1405 }
1406 }
1407 else
1408 {
Olli Etuaho43364892017-02-13 16:00:12 +00001409 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001410 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1411 }
jchen104cdac9e2017-05-08 11:01:20 +08001412
1413 if (IsAtomicCounter(publicType.getBasicType()))
1414 {
1415 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1416 }
1417 else
1418 {
1419 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1420 }
Olli Etuaho43364892017-02-13 16:00:12 +00001421}
Martin Radev2cc85b32016-08-05 16:22:53 +03001422
Olli Etuaho43364892017-02-13 16:00:12 +00001423void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1424{
1425 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001426 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1427 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1428 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1429 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1430 // when it comes to which shaders are accepted by the compiler.
1431 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001432 if (IsImage(type.getBasicType()))
1433 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001434 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1435 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001436 }
1437 else if (IsSampler(type.getBasicType()))
1438 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001439 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1440 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001441 }
jchen104cdac9e2017-05-08 11:01:20 +08001442 else if (IsAtomicCounter(type.getBasicType()))
1443 {
1444 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1445 }
Olli Etuaho43364892017-02-13 16:00:12 +00001446 else
1447 {
1448 ASSERT(!IsOpaqueType(type.getBasicType()));
1449 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001450 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001451}
1452
Olli Etuaho856c4972016-08-08 11:38:39 +03001453void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1454 const TString &layoutQualifierName,
1455 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001456{
1457
1458 if (mShaderVersion < versionRequired)
1459 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001460 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001461 }
1462}
1463
Olli Etuaho856c4972016-08-08 11:38:39 +03001464bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1465 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001466{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001467 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001468 for (size_t i = 0u; i < localSize.size(); ++i)
1469 {
1470 if (localSize[i] != -1)
1471 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001472 error(location,
1473 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1474 "global layout declaration",
1475 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001476 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001477 }
1478 }
1479
Olli Etuaho8a176262016-08-16 14:23:01 +03001480 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001481}
1482
Olli Etuaho43364892017-02-13 16:00:12 +00001483void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001484 TLayoutImageInternalFormat internalFormat)
1485{
1486 if (internalFormat != EiifUnspecified)
1487 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001488 error(location, "invalid layout qualifier: only valid when used with images",
1489 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001490 }
Olli Etuaho43364892017-02-13 16:00:12 +00001491}
1492
1493void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1494{
1495 if (binding != -1)
1496 {
1497 error(location,
1498 "invalid layout qualifier: only valid when used with opaque types or blocks",
1499 "binding");
1500 }
1501}
1502
jchen104cdac9e2017-05-08 11:01:20 +08001503void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1504{
1505 if (offset != -1)
1506 {
1507 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1508 "offset");
1509 }
1510}
1511
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001512void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1513 int binding,
1514 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001515{
1516 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001517 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001518 {
1519 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1520 }
1521}
1522
1523void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1524 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001525 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001526{
1527 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001528 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001529 {
1530 error(location, "sampler binding greater than maximum texture units", "binding");
1531 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001532}
1533
Jiajia Qinbc585152017-06-23 15:42:17 +08001534void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1535 const TQualifier &qualifier,
1536 int binding,
1537 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001538{
1539 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001540 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001541 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001542 if (binding + size > mMaxUniformBufferBindings)
1543 {
1544 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1545 "binding");
1546 }
1547 }
1548 else if (qualifier == EvqBuffer)
1549 {
1550 if (binding + size > mMaxShaderStorageBufferBindings)
1551 {
1552 error(location,
1553 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1554 "binding");
1555 }
jchen10af713a22017-04-19 09:10:56 +08001556 }
1557}
jchen104cdac9e2017-05-08 11:01:20 +08001558void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1559{
1560 if (binding >= mMaxAtomicCounterBindings)
1561 {
1562 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1563 "binding");
1564 }
1565}
jchen10af713a22017-04-19 09:10:56 +08001566
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001567void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1568 int objectLocationCount,
1569 const TLayoutQualifier &layoutQualifier)
1570{
1571 int loc = layoutQualifier.location;
1572 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1573 {
1574 error(location, "Uniform location out of range", "location");
1575 }
1576}
1577
Andrei Volykhina5527072017-03-22 16:46:30 +03001578void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1579{
1580 if (yuv != false)
1581 {
1582 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1583 }
1584}
1585
Jiajia Qinbc585152017-06-23 15:42:17 +08001586void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1587 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001588{
1589 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1590 {
1591 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001592 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1593 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1594 qual == EvqInOut || qual == EvqConstReadOnly))
1595 {
1596 if (argument->getMemoryQualifier().writeonly)
1597 {
1598 error(argument->getLine(),
1599 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
1600 fnCall->getFunctionSymbolInfo()->getName().c_str());
1601 return;
1602 }
1603 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001604 if (qual == EvqOut || qual == EvqInOut)
1605 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001606 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001607 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001608 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001609 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuahoec9232b2017-03-27 17:01:37 +03001610 fnCall->getFunctionSymbolInfo()->getName().c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001611 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001612 }
1613 }
1614 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001615}
1616
Martin Radev70866b82016-07-22 15:27:42 +03001617void TParseContext::checkInvariantVariableQualifier(bool invariant,
1618 const TQualifier qualifier,
1619 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001620{
Martin Radev70866b82016-07-22 15:27:42 +03001621 if (!invariant)
1622 return;
1623
1624 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001625 {
Martin Radev70866b82016-07-22 15:27:42 +03001626 // input variables in the fragment shader can be also qualified as invariant
1627 if (!sh::CanBeInvariantESSL1(qualifier))
1628 {
1629 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1630 }
1631 }
1632 else
1633 {
1634 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1635 {
1636 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1637 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001638 }
1639}
1640
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001641bool TParseContext::supportsExtension(TExtension extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001642{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001643 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001644 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1645 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001646}
1647
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001648bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001649{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001650 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001651}
1652
Jamie Madillb98c3a82015-07-23 14:26:04 -04001653void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1654 const char *extName,
1655 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001656{
1657 pp::SourceLocation srcLoc;
1658 srcLoc.file = loc.first_file;
1659 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001660 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001661}
1662
Jamie Madillb98c3a82015-07-23 14:26:04 -04001663void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1664 const char *name,
1665 const char *value,
1666 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001667{
1668 pp::SourceLocation srcLoc;
1669 srcLoc.file = loc.first_file;
1670 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001671 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001672}
1673
Martin Radev4c4c8e72016-08-04 12:25:34 +03001674sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001675{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001676 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001677 for (size_t i = 0u; i < result.size(); ++i)
1678 {
1679 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1680 {
1681 result[i] = 1;
1682 }
1683 else
1684 {
1685 result[i] = mComputeShaderLocalSize[i];
1686 }
1687 }
1688 return result;
1689}
1690
Olli Etuaho56229f12017-07-10 14:16:33 +03001691TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1692 const TSourceLoc &line)
1693{
1694 TIntermConstantUnion *node = new TIntermConstantUnion(
1695 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1696 node->setLine(line);
1697 return node;
1698}
1699
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001700/////////////////////////////////////////////////////////////////////////////////
1701//
1702// Non-Errors.
1703//
1704/////////////////////////////////////////////////////////////////////////////////
1705
Jamie Madill5c097022014-08-20 16:38:32 -04001706const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1707 const TString *name,
1708 const TSymbol *symbol)
1709{
Jamie Madill5c097022014-08-20 16:38:32 -04001710 if (!symbol)
1711 {
1712 error(location, "undeclared identifier", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001713 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001714 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001715
1716 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001717 {
1718 error(location, "variable expected", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001719 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001720 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001721
1722 const TVariable *variable = static_cast<const TVariable *>(symbol);
1723
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001724 if (variable->getExtension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001725 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001726 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001727 }
1728
Olli Etuaho0f684632017-07-13 12:42:15 +03001729 // Reject shaders using both gl_FragData and gl_FragColor
1730 TQualifier qualifier = variable->getType().getQualifier();
1731 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001732 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001733 mUsesFragData = true;
1734 }
1735 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1736 {
1737 mUsesFragColor = true;
1738 }
1739 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1740 {
1741 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001742 }
1743
Olli Etuaho0f684632017-07-13 12:42:15 +03001744 // This validation is not quite correct - it's only an error to write to
1745 // both FragData and FragColor. For simplicity, and because users shouldn't
1746 // be rewarded for reading from undefined varaibles, return an error
1747 // if they are both referenced, rather than assigned.
1748 if (mUsesFragData && mUsesFragColor)
1749 {
1750 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1751 if (mUsesSecondaryOutputs)
1752 {
1753 errorMessage =
1754 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1755 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1756 }
1757 error(location, errorMessage, name->c_str());
1758 }
1759
1760 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1761 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1762 qualifier == EvqWorkGroupSize)
1763 {
1764 error(location,
1765 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1766 "gl_WorkGroupSize");
1767 }
Jamie Madill5c097022014-08-20 16:38:32 -04001768 return variable;
1769}
1770
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001771TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1772 const TString *name,
1773 const TSymbol *symbol)
1774{
1775 const TVariable *variable = getNamedVariable(location, name, symbol);
1776
Olli Etuaho0f684632017-07-13 12:42:15 +03001777 if (!variable)
1778 {
1779 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1780 node->setLine(location);
1781 return node;
1782 }
1783
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001784 const TType &variableType = variable->getType();
Olli Etuaho56229f12017-07-10 14:16:33 +03001785 TIntermTyped *node = nullptr;
1786
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001787 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001788 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001789 const TConstantUnion *constArray = variable->getConstPointer();
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001790 node = new TIntermConstantUnion(constArray, variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001791 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001792 else if (variableType.getQualifier() == EvqWorkGroupSize && mComputeShaderLocalSizeDeclared)
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001793 {
1794 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1795 // needs to be added to the AST as a constant and not as a symbol.
1796 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1797 TConstantUnion *constArray = new TConstantUnion[3];
1798 for (size_t i = 0; i < 3; ++i)
1799 {
1800 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1801 }
1802
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001803 ASSERT(variableType.getBasicType() == EbtUInt);
1804 ASSERT(variableType.getObjectSize() == 3);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001805
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001806 TType type(variableType);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001807 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001808 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001809 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001810 else if ((mGeometryShaderInputPrimitiveType != EptUndefined) &&
1811 (variableType.getQualifier() == EvqPerVertexIn))
Jiawei Shaod8105a02017-08-08 09:54:36 +08001812 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001813 ASSERT(mGeometryShaderInputArraySize > 0u);
1814
1815 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), variableType);
Olli Etuaho55bde912017-10-25 13:41:13 +03001816 node->getTypePointer()->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
Jiawei Shaod8105a02017-08-08 09:54:36 +08001817 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001818 else
1819 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001820 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001821 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001822 ASSERT(node != nullptr);
1823 node->setLine(location);
1824 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001825}
1826
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001827// Initializers show up in several places in the grammar. Have one set of
1828// code to handle them here.
1829//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001830// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001831bool TParseContext::executeInitializer(const TSourceLoc &line,
1832 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001833 TType type,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001834 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001835 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001836{
Olli Etuaho13389b62016-10-16 11:48:18 +01001837 ASSERT(initNode != nullptr);
1838 ASSERT(*initNode == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001839
Olli Etuaho2935c582015-04-08 14:32:06 +03001840 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001841 if (type.isUnsizedArray())
1842 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001843 // In case initializer is not an array or type has more dimensions than initializer, this
1844 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1845 // actually is an array or not. Having a non-array initializer for an unsized array will
1846 // result in an error later, so we don't generate an error message here.
1847 type.sizeUnsizedArrays(initializer->getType().getArraySizes());
Olli Etuaho376f1b52015-04-13 13:23:41 +03001848 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001849 if (!declareVariable(line, identifier, type, &variable))
1850 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001851 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001852 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001853
Olli Etuahob0c645e2015-05-12 14:25:36 +03001854 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001855 if (symbolTable.atGlobalLevel() &&
1856 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001857 {
1858 // Error message does not completely match behavior with ESSL 1.00, but
1859 // we want to steer developers towards only using constant expressions.
1860 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001861 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001862 }
1863 if (globalInitWarning)
1864 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001865 warning(
1866 line,
1867 "global variable initializers should be constant expressions "
1868 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1869 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001870 }
1871
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001872 //
1873 // identifier must be of type constant, a global, or a temporary
1874 //
1875 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301876 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1877 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001878 error(line, " cannot initialize this type of qualifier ",
1879 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001880 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001881 }
1882 //
1883 // test for and propagate constant
1884 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001885
Arun Patole7e7e68d2015-05-22 12:02:25 +05301886 if (qualifier == EvqConst)
1887 {
1888 if (qualifier != initializer->getType().getQualifier())
1889 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001890 std::stringstream reasonStream;
1891 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1892 << "'";
1893 std::string reason = reasonStream.str();
1894 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001895 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001896 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001897 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301898 if (type != initializer->getType())
1899 {
1900 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001901 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001902 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001903 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001904 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001905
1906 // Save the constant folded value to the variable if possible. For example array
1907 // initializers are not folded, since that way copying the array literal to multiple places
1908 // in the shader is avoided.
1909 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1910 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301911 if (initializer->getAsConstantUnion())
1912 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001913 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001914 ASSERT(*initNode == nullptr);
1915 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301916 }
1917 else if (initializer->getAsSymbolNode())
1918 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001919 const TSymbol *symbol =
1920 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1921 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001922
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001923 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001924 if (constArray)
1925 {
1926 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001927 ASSERT(*initNode == nullptr);
1928 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001929 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001930 }
1931 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001932
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001933 TIntermSymbol *intermSymbol =
1934 new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
1935 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001936 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1937 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001938 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001939 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001940 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001941 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001942
Olli Etuaho914b79a2017-06-19 16:03:19 +03001943 return true;
1944}
1945
1946TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
1947 const TString &identifier,
1948 TIntermTyped *initializer,
1949 const TSourceLoc &loc)
1950{
1951 checkIsScalarBool(loc, pType);
1952 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03001953 TType type(pType);
1954 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03001955 {
1956 // The initializer is valid. The init condition needs to have a node - either the
1957 // initializer node, or a constant node in case the initialized variable is const and won't
1958 // be recorded in the AST.
1959 if (initNode == nullptr)
1960 {
1961 return initializer;
1962 }
1963 else
1964 {
1965 TIntermDeclaration *declaration = new TIntermDeclaration();
1966 declaration->appendDeclarator(initNode);
1967 return declaration;
1968 }
1969 }
1970 return nullptr;
1971}
1972
1973TIntermNode *TParseContext::addLoop(TLoopType type,
1974 TIntermNode *init,
1975 TIntermNode *cond,
1976 TIntermTyped *expr,
1977 TIntermNode *body,
1978 const TSourceLoc &line)
1979{
1980 TIntermNode *node = nullptr;
1981 TIntermTyped *typedCond = nullptr;
1982 if (cond)
1983 {
1984 typedCond = cond->getAsTyped();
1985 }
1986 if (cond == nullptr || typedCond)
1987 {
Olli Etuahocce89652017-06-19 16:04:09 +03001988 if (type == ELoopDoWhile)
1989 {
1990 checkIsScalarBool(line, typedCond);
1991 }
1992 // In the case of other loops, it was checked before that the condition is a scalar boolean.
1993 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
1994 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
1995 !typedCond->isVector()));
1996
Olli Etuaho3ec75682017-07-05 17:02:55 +03001997 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03001998 node->setLine(line);
1999 return node;
2000 }
2001
Olli Etuahocce89652017-06-19 16:04:09 +03002002 ASSERT(type != ELoopDoWhile);
2003
Olli Etuaho914b79a2017-06-19 16:03:19 +03002004 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2005 ASSERT(declaration);
2006 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2007 ASSERT(declarator->getLeft()->getAsSymbolNode());
2008
2009 // The condition is a declaration. In the AST representation we don't support declarations as
2010 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2011 // the loop.
2012 TIntermBlock *block = new TIntermBlock();
2013
2014 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2015 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2016 block->appendStatement(declareCondition);
2017
2018 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2019 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002020 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002021 block->appendStatement(loop);
2022 loop->setLine(line);
2023 block->setLine(line);
2024 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002025}
2026
Olli Etuahocce89652017-06-19 16:04:09 +03002027TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2028 TIntermNodePair code,
2029 const TSourceLoc &loc)
2030{
Olli Etuaho56229f12017-07-10 14:16:33 +03002031 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002032
2033 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002034 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002035 {
2036 if (cond->getAsConstantUnion()->getBConst(0) == true)
2037 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002038 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002039 }
2040 else
2041 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002042 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002043 }
2044 }
2045
Olli Etuaho3ec75682017-07-05 17:02:55 +03002046 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002047 node->setLine(loc);
2048
2049 return node;
2050}
2051
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002052void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2053{
2054 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2055 typeSpecifier->getBasicType());
2056
2057 if (mShaderVersion < 300 && typeSpecifier->array)
2058 {
2059 error(typeSpecifier->getLine(), "not supported", "first-class array");
2060 typeSpecifier->clearArrayness();
2061 }
2062}
2063
Martin Radev70866b82016-07-22 15:27:42 +03002064TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302065 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002066{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002067 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002068
Martin Radev70866b82016-07-22 15:27:42 +03002069 TPublicType returnType = typeSpecifier;
2070 returnType.qualifier = typeQualifier.qualifier;
2071 returnType.invariant = typeQualifier.invariant;
2072 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002073 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002074 returnType.precision = typeSpecifier.precision;
2075
2076 if (typeQualifier.precision != EbpUndefined)
2077 {
2078 returnType.precision = typeQualifier.precision;
2079 }
2080
Martin Radev4a9cd802016-09-01 16:51:51 +03002081 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2082 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002083
Martin Radev4a9cd802016-09-01 16:51:51 +03002084 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2085 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002086
Martin Radev4a9cd802016-09-01 16:51:51 +03002087 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002088
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002089 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002090 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002091 if (typeSpecifier.array)
2092 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002093 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002094 returnType.clearArrayness();
2095 }
2096
Martin Radev70866b82016-07-22 15:27:42 +03002097 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002098 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002099 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002100 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002101 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002102 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002103
Martin Radev70866b82016-07-22 15:27:42 +03002104 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002105 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002106 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002107 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002108 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002109 }
2110 }
2111 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002112 {
Martin Radev70866b82016-07-22 15:27:42 +03002113 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002114 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002115 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002116 }
Martin Radev70866b82016-07-22 15:27:42 +03002117 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2118 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002119 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002120 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2121 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002122 }
Martin Radev70866b82016-07-22 15:27:42 +03002123 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002124 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002125 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002126 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002127 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002128 }
2129
2130 return returnType;
2131}
2132
Olli Etuaho856c4972016-08-08 11:38:39 +03002133void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2134 const TPublicType &type,
2135 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002136{
2137 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002138 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002139 {
2140 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002141 }
2142
2143 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2144 switch (qualifier)
2145 {
2146 case EvqVertexIn:
2147 // ESSL 3.00 section 4.3.4
2148 if (type.array)
2149 {
2150 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002151 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002152 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002153 return;
2154 case EvqFragmentOut:
2155 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002156 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002157 {
2158 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002159 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002160 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002161 return;
2162 default:
2163 break;
2164 }
2165
2166 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2167 // restrictions.
2168 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002169 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2170 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002171 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2172 {
2173 error(qualifierLocation, "must use 'flat' interpolation here",
2174 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002175 }
2176
Martin Radev4a9cd802016-09-01 16:51:51 +03002177 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002178 {
2179 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2180 // These restrictions are only implied by the ESSL 3.00 spec, but
2181 // the ESSL 3.10 spec lists these restrictions explicitly.
2182 if (type.array)
2183 {
2184 error(qualifierLocation, "cannot be an array of structures",
2185 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002186 }
2187 if (type.isStructureContainingArrays())
2188 {
2189 error(qualifierLocation, "cannot be a structure containing an array",
2190 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002191 }
2192 if (type.isStructureContainingType(EbtStruct))
2193 {
2194 error(qualifierLocation, "cannot be a structure containing a structure",
2195 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002196 }
2197 if (type.isStructureContainingType(EbtBool))
2198 {
2199 error(qualifierLocation, "cannot be a structure containing a bool",
2200 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002201 }
2202 }
2203}
2204
Martin Radev2cc85b32016-08-05 16:22:53 +03002205void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2206{
2207 if (qualifier.getType() == QtStorage)
2208 {
2209 const TStorageQualifierWrapper &storageQualifier =
2210 static_cast<const TStorageQualifierWrapper &>(qualifier);
2211 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2212 !symbolTable.atGlobalLevel())
2213 {
2214 error(storageQualifier.getLine(),
2215 "Local variables can only use the const storage qualifier.",
2216 storageQualifier.getQualifierString().c_str());
2217 }
2218 }
2219}
2220
Olli Etuaho43364892017-02-13 16:00:12 +00002221void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002222 const TSourceLoc &location)
2223{
Jiajia Qinbc585152017-06-23 15:42:17 +08002224 const std::string reason(
2225 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2226 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002227 if (memoryQualifier.readonly)
2228 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002229 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002230 }
2231 if (memoryQualifier.writeonly)
2232 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002233 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002234 }
Martin Radev049edfa2016-11-11 14:35:37 +02002235 if (memoryQualifier.coherent)
2236 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002237 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002238 }
2239 if (memoryQualifier.restrictQualifier)
2240 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002241 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002242 }
2243 if (memoryQualifier.volatileQualifier)
2244 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002245 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002246 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002247}
2248
jchen104cdac9e2017-05-08 11:01:20 +08002249// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2250// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002251void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2252 const TSourceLoc &loc,
2253 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002254{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002255 if (!IsAtomicCounter(type->getBasicType()))
2256 {
2257 return;
2258 }
2259
2260 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2261 : kAtomicCounterSize;
2262 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2263 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002264 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002265 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002266 {
2267 offset = bindingState.appendSpan(size);
2268 }
2269 else
2270 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002271 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002272 }
2273 if (offset == -1)
2274 {
2275 error(loc, "Offset overlapping", "atomic counter");
2276 return;
2277 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002278 layoutQualifier.offset = offset;
2279 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002280}
2281
Olli Etuaho454c34c2017-10-25 16:35:56 +03002282void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
2283 const char *token,
2284 TType *type)
2285{
2286 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2287 {
2288 if (type->isArray() && type->getOutermostArraySize() == 0u)
2289 {
2290 // Set size for the unsized geometry shader inputs if they are declared after a valid
2291 // input primitive declaration.
2292 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2293 {
2294 ASSERT(mGeometryShaderInputArraySize > 0u);
2295 type->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
2296 }
2297 else
2298 {
2299 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2300 // An input can be declared without an array size if there is a previous layout
2301 // which specifies the size.
2302 error(location,
2303 "Missing a valid input primitive declaration before declaring an unsized "
2304 "array input",
2305 token);
2306 }
2307 }
2308 else if (type->isArray())
2309 {
2310 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2311 }
2312 else
2313 {
2314 error(location, "Geometry shader input variable must be declared as an array", token);
2315 }
2316 }
2317}
2318
Olli Etuaho13389b62016-10-16 11:48:18 +01002319TIntermDeclaration *TParseContext::parseSingleDeclaration(
2320 TPublicType &publicType,
2321 const TSourceLoc &identifierOrTypeLocation,
2322 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002323{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002324 TType type(publicType);
2325 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2326 mDirectiveHandler.pragma().stdgl.invariantAll)
2327 {
2328 TQualifier qualifier = type.getQualifier();
2329
2330 // The directive handler has already taken care of rejecting invalid uses of this pragma
2331 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2332 // affected variable declarations:
2333 //
2334 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2335 // elsewhere, in TranslatorGLSL.)
2336 //
2337 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2338 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2339 // the way this is currently implemented we have to enable this compiler option before
2340 // parsing the shader and determining the shading language version it uses. If this were
2341 // implemented as a post-pass, the workaround could be more targeted.
2342 //
2343 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2344 // the specification, but there are desktop OpenGL drivers that expect that this is the
2345 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2346 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2347 {
2348 type.setInvariant(true);
2349 }
2350 }
2351
Olli Etuaho454c34c2017-10-25 16:35:56 +03002352 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier.c_str(), &type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002353
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002354 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2355 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002356
Olli Etuahobab4c082015-04-24 16:38:49 +03002357 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002358 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002359
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002360 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002361 if (emptyDeclaration)
2362 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002363 emptyDeclarationErrorCheck(type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002364 // In most cases we don't need to create a symbol node for an empty declaration.
2365 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2366 if (type.getBasicType() == EbtStruct)
2367 {
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03002368 symbol = new TIntermSymbol(symbolTable.getEmptySymbolId(), "", type);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002369 }
jchen104cdac9e2017-05-08 11:01:20 +08002370 else if (IsAtomicCounter(publicType.getBasicType()))
2371 {
2372 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2373 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002374 }
2375 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002376 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002377 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002378
Olli Etuaho55bde912017-10-25 13:41:13 +03002379 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002380
Olli Etuaho55bc9052017-10-25 17:33:06 +03002381 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, &type);
jchen104cdac9e2017-05-08 11:01:20 +08002382
Olli Etuaho2935c582015-04-08 14:32:06 +03002383 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002384 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002385
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002386 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002387 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002388 symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
Olli Etuaho13389b62016-10-16 11:48:18 +01002389 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002390 }
2391
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002392 TIntermDeclaration *declaration = new TIntermDeclaration();
2393 declaration->setLine(identifierOrTypeLocation);
2394 if (symbol)
2395 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002396 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002397 declaration->appendDeclarator(symbol);
2398 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002399 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002400}
2401
Olli Etuaho55bde912017-10-25 13:41:13 +03002402TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002403 const TSourceLoc &identifierLocation,
2404 const TString &identifier,
2405 const TSourceLoc &indexLocation,
Olli Etuaho55bde912017-10-25 13:41:13 +03002406 unsigned int arraySize)
Jamie Madill60ed9812013-06-06 11:56:46 -04002407{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002408 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002409
Olli Etuaho55bde912017-10-25 13:41:13 +03002410 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002411 identifierLocation);
2412
Olli Etuaho55bde912017-10-25 13:41:13 +03002413 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002414
Olli Etuaho55bde912017-10-25 13:41:13 +03002415 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002416
Olli Etuaho55bde912017-10-25 13:41:13 +03002417 TType arrayType(elementType);
2418 arrayType.makeArray(arraySize);
Jamie Madill60ed9812013-06-06 11:56:46 -04002419
Olli Etuaho454c34c2017-10-25 16:35:56 +03002420 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier.c_str(), &arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002421
2422 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2423
Olli Etuaho55bc9052017-10-25 17:33:06 +03002424 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002425
Olli Etuaho2935c582015-04-08 14:32:06 +03002426 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002427 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002428
Olli Etuaho13389b62016-10-16 11:48:18 +01002429 TIntermDeclaration *declaration = new TIntermDeclaration();
2430 declaration->setLine(identifierLocation);
2431
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002432 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002433 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002434 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2435 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002436 declaration->appendDeclarator(symbol);
2437 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002438
Olli Etuaho13389b62016-10-16 11:48:18 +01002439 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002440}
2441
Olli Etuaho13389b62016-10-16 11:48:18 +01002442TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2443 const TSourceLoc &identifierLocation,
2444 const TString &identifier,
2445 const TSourceLoc &initLocation,
2446 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002447{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002448 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002449
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002450 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2451 identifierLocation);
2452
2453 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002454
Olli Etuaho13389b62016-10-16 11:48:18 +01002455 TIntermDeclaration *declaration = new TIntermDeclaration();
2456 declaration->setLine(identifierLocation);
2457
2458 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002459 TType type(publicType);
2460 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002461 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002462 if (initNode)
2463 {
2464 declaration->appendDeclarator(initNode);
2465 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002466 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002467 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002468}
2469
Olli Etuaho13389b62016-10-16 11:48:18 +01002470TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002471 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002472 const TSourceLoc &identifierLocation,
2473 const TString &identifier,
2474 const TSourceLoc &indexLocation,
Olli Etuaho55bde912017-10-25 13:41:13 +03002475 unsigned int arraySize,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002476 const TSourceLoc &initLocation,
2477 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002478{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002479 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002480
Olli Etuaho55bde912017-10-25 13:41:13 +03002481 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002482 identifierLocation);
2483
Olli Etuaho55bde912017-10-25 13:41:13 +03002484 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002485
Olli Etuaho55bde912017-10-25 13:41:13 +03002486 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002487
Olli Etuaho55bde912017-10-25 13:41:13 +03002488 TType arrayType(elementType);
2489 arrayType.makeArray(arraySize);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002490
Olli Etuaho13389b62016-10-16 11:48:18 +01002491 TIntermDeclaration *declaration = new TIntermDeclaration();
2492 declaration->setLine(identifierLocation);
2493
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002494 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002495 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002496 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002497 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002498 if (initNode)
2499 {
2500 declaration->appendDeclarator(initNode);
2501 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002502 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002503
2504 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002505}
2506
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002507TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002508 const TTypeQualifierBuilder &typeQualifierBuilder,
2509 const TSourceLoc &identifierLoc,
2510 const TString *identifier,
2511 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002512{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002513 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002514
Martin Radev70866b82016-07-22 15:27:42 +03002515 if (!typeQualifier.invariant)
2516 {
2517 error(identifierLoc, "Expected invariant", identifier->c_str());
2518 return nullptr;
2519 }
2520 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2521 {
2522 return nullptr;
2523 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002524 if (!symbol)
2525 {
2526 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002527 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002528 }
Martin Radev70866b82016-07-22 15:27:42 +03002529 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002530 {
Martin Radev70866b82016-07-22 15:27:42 +03002531 error(identifierLoc, "invariant declaration specifies qualifier",
2532 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002533 }
Martin Radev70866b82016-07-22 15:27:42 +03002534 if (typeQualifier.precision != EbpUndefined)
2535 {
2536 error(identifierLoc, "invariant declaration specifies precision",
2537 getPrecisionString(typeQualifier.precision));
2538 }
2539 if (!typeQualifier.layoutQualifier.isEmpty())
2540 {
2541 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2542 }
2543
2544 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002545 if (!variable)
2546 {
2547 return nullptr;
2548 }
Martin Radev70866b82016-07-22 15:27:42 +03002549 const TType &type = variable->getType();
2550
2551 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2552 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002553 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002554
2555 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2556
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002557 TIntermSymbol *intermSymbol = new TIntermSymbol(variable->getUniqueId(), *identifier, type);
2558 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002559
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002560 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002561}
2562
Olli Etuaho13389b62016-10-16 11:48:18 +01002563void TParseContext::parseDeclarator(TPublicType &publicType,
2564 const TSourceLoc &identifierLocation,
2565 const TString &identifier,
2566 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002567{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002568 // If the declaration starting this declarator list was empty (example: int,), some checks were
2569 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002570 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002571 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002572 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2573 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002574 }
2575
Olli Etuaho856c4972016-08-08 11:38:39 +03002576 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002577
Olli Etuaho2935c582015-04-08 14:32:06 +03002578 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002579 TType type(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002580
2581 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &type);
2582
Olli Etuaho55bde912017-10-25 13:41:13 +03002583 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &type);
2584
Olli Etuaho55bc9052017-10-25 17:33:06 +03002585 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &type);
2586
Olli Etuaho43364892017-02-13 16:00:12 +00002587 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002588
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002589 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002590 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002591 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
2592 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002593 declarationOut->appendDeclarator(symbol);
2594 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002595}
2596
Olli Etuaho55bde912017-10-25 13:41:13 +03002597void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002598 const TSourceLoc &identifierLocation,
2599 const TString &identifier,
2600 const TSourceLoc &arrayLocation,
Olli Etuaho55bde912017-10-25 13:41:13 +03002601 unsigned int arraySize,
Olli Etuaho13389b62016-10-16 11:48:18 +01002602 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002603{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002604 // If the declaration starting this declarator list was empty (example: int,), some checks were
2605 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002606 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002607 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002608 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002609 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002610 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002611
Olli Etuaho55bde912017-10-25 13:41:13 +03002612 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002613
Olli Etuaho55bde912017-10-25 13:41:13 +03002614 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002615 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002616 TType arrayType(elementType);
2617 arrayType.makeArray(arraySize);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002618
Olli Etuaho454c34c2017-10-25 16:35:56 +03002619 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &arrayType);
2620
Olli Etuaho55bde912017-10-25 13:41:13 +03002621 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2622
Olli Etuaho55bc9052017-10-25 17:33:06 +03002623 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002624
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002625 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002626 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002627
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002628 if (variable)
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002629 {
2630 TIntermSymbol *symbol =
2631 new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2632 symbol->setLine(identifierLocation);
2633 declarationOut->appendDeclarator(symbol);
2634 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002635 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002636}
2637
Olli Etuaho13389b62016-10-16 11:48:18 +01002638void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2639 const TSourceLoc &identifierLocation,
2640 const TString &identifier,
2641 const TSourceLoc &initLocation,
2642 TIntermTyped *initializer,
2643 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002644{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002645 // If the declaration starting this declarator list was empty (example: int,), some checks were
2646 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002647 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002648 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002649 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2650 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002651 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002652
Olli Etuaho856c4972016-08-08 11:38:39 +03002653 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002654
Olli Etuaho13389b62016-10-16 11:48:18 +01002655 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002656 TType type(publicType);
2657 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002658 {
2659 //
2660 // build the intermediate representation
2661 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002662 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002663 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002664 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002665 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002666 }
2667}
2668
Olli Etuaho55bde912017-10-25 13:41:13 +03002669void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002670 const TSourceLoc &identifierLocation,
2671 const TString &identifier,
2672 const TSourceLoc &indexLocation,
Olli Etuaho55bde912017-10-25 13:41:13 +03002673 unsigned int arraySize,
Olli Etuaho13389b62016-10-16 11:48:18 +01002674 const TSourceLoc &initLocation,
2675 TIntermTyped *initializer,
2676 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002677{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002678 // If the declaration starting this declarator list was empty (example: int,), some checks were
2679 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002680 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002681 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002682 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002683 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002684 }
2685
Olli Etuaho55bde912017-10-25 13:41:13 +03002686 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002687
Olli Etuaho55bde912017-10-25 13:41:13 +03002688 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002689
Olli Etuaho55bde912017-10-25 13:41:13 +03002690 TType arrayType(elementType);
2691 arrayType.makeArray(arraySize);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002692
2693 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002694 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002695 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002696 {
2697 if (initNode)
2698 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002699 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002700 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002701 }
2702}
2703
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002704TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2705{
2706 // It's simpler to parse an empty statement as a constant expression rather than having a
2707 // different type of node just for empty statements, that will be pruned from the AST anyway.
2708 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2709 node->setLine(location);
2710 return node;
2711}
2712
jchen104cdac9e2017-05-08 11:01:20 +08002713void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2714 const TSourceLoc &location)
2715{
2716 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2717 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2718 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2719 {
2720 error(location, "Requires both binding and offset", "layout");
2721 return;
2722 }
2723 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2724}
2725
Olli Etuahocce89652017-06-19 16:04:09 +03002726void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2727 const TPublicType &type,
2728 const TSourceLoc &loc)
2729{
2730 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2731 !getFragmentPrecisionHigh())
2732 {
2733 error(loc, "precision is not supported in fragment shader", "highp");
2734 }
2735
2736 if (!CanSetDefaultPrecisionOnType(type))
2737 {
2738 error(loc, "illegal type argument for default precision qualifier",
2739 getBasicString(type.getBasicType()));
2740 return;
2741 }
2742 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2743}
2744
Shaob5cc1192017-07-06 10:47:20 +08002745bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2746{
2747 switch (typeQualifier.layoutQualifier.primitiveType)
2748 {
2749 case EptLines:
2750 case EptLinesAdjacency:
2751 case EptTriangles:
2752 case EptTrianglesAdjacency:
2753 return typeQualifier.qualifier == EvqGeometryIn;
2754
2755 case EptLineStrip:
2756 case EptTriangleStrip:
2757 return typeQualifier.qualifier == EvqGeometryOut;
2758
2759 case EptPoints:
2760 return true;
2761
2762 default:
2763 UNREACHABLE();
2764 return false;
2765 }
2766}
2767
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002768void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2769 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002770{
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002771 if (mGeometryShaderInputArraySize == 0u)
2772 {
2773 mGeometryShaderInputArraySize = inputArraySize;
2774 }
2775 else if (mGeometryShaderInputArraySize != inputArraySize)
2776 {
2777 error(line,
2778 "Array size or input primitive declaration doesn't match the size of earlier sized "
2779 "array inputs.",
2780 "layout");
2781 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002782}
2783
Shaob5cc1192017-07-06 10:47:20 +08002784bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2785{
2786 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2787
2788 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2789
2790 if (layoutQualifier.maxVertices != -1)
2791 {
2792 error(typeQualifier.line,
2793 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2794 return false;
2795 }
2796
2797 // Set mGeometryInputPrimitiveType if exists
2798 if (layoutQualifier.primitiveType != EptUndefined)
2799 {
2800 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2801 {
2802 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2803 return false;
2804 }
2805
2806 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2807 {
2808 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002809 setGeometryShaderInputArraySize(
2810 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2811 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002812 }
2813 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2814 {
2815 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2816 "layout");
2817 return false;
2818 }
2819 }
2820
2821 // Set mGeometryInvocations if exists
2822 if (layoutQualifier.invocations > 0)
2823 {
2824 if (mGeometryShaderInvocations == 0)
2825 {
2826 mGeometryShaderInvocations = layoutQualifier.invocations;
2827 }
2828 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2829 {
2830 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2831 "layout");
2832 return false;
2833 }
2834 }
2835
2836 return true;
2837}
2838
2839bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2840{
2841 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2842
2843 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2844
2845 if (layoutQualifier.invocations > 0)
2846 {
2847 error(typeQualifier.line,
2848 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2849 return false;
2850 }
2851
2852 // Set mGeometryOutputPrimitiveType if exists
2853 if (layoutQualifier.primitiveType != EptUndefined)
2854 {
2855 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2856 {
2857 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2858 return false;
2859 }
2860
2861 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2862 {
2863 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2864 }
2865 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2866 {
2867 error(typeQualifier.line,
2868 "primitive doesn't match earlier output primitive declaration", "layout");
2869 return false;
2870 }
2871 }
2872
2873 // Set mGeometryMaxVertices if exists
2874 if (layoutQualifier.maxVertices > -1)
2875 {
2876 if (mGeometryShaderMaxVertices == -1)
2877 {
2878 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2879 }
2880 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2881 {
2882 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2883 "layout");
2884 return false;
2885 }
2886 }
2887
2888 return true;
2889}
2890
Martin Radev70866b82016-07-22 15:27:42 +03002891void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002892{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002893 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002894 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002895
Martin Radev70866b82016-07-22 15:27:42 +03002896 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2897 typeQualifier.line);
2898
Jamie Madillc2128ff2016-07-04 10:26:17 -04002899 // It should never be the case, but some strange parser errors can send us here.
2900 if (layoutQualifier.isEmpty())
2901 {
2902 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002903 return;
2904 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002905
Martin Radev802abe02016-08-04 17:48:32 +03002906 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002907 {
Olli Etuaho43364892017-02-13 16:00:12 +00002908 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002909 return;
2910 }
2911
Olli Etuaho43364892017-02-13 16:00:12 +00002912 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2913
2914 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002915
2916 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2917
Andrei Volykhina5527072017-03-22 16:46:30 +03002918 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2919
jchen104cdac9e2017-05-08 11:01:20 +08002920 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2921
Qin Jiajiaca68d982017-09-18 16:41:56 +08002922 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
2923 typeQualifier.qualifier);
2924
Martin Radev802abe02016-08-04 17:48:32 +03002925 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002926 {
Martin Radev802abe02016-08-04 17:48:32 +03002927 if (mComputeShaderLocalSizeDeclared &&
2928 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2929 {
2930 error(typeQualifier.line, "Work group size does not match the previous declaration",
2931 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002932 return;
2933 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002934
Martin Radev802abe02016-08-04 17:48:32 +03002935 if (mShaderVersion < 310)
2936 {
2937 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002938 return;
2939 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002940
Martin Radev4c4c8e72016-08-04 12:25:34 +03002941 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002942 {
2943 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002944 return;
2945 }
2946
2947 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2948 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2949
2950 const TConstantUnion *maxComputeWorkGroupSizeData =
2951 maxComputeWorkGroupSize->getConstPointer();
2952
2953 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2954 {
2955 if (layoutQualifier.localSize[i] != -1)
2956 {
2957 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2958 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2959 if (mComputeShaderLocalSize[i] < 1 ||
2960 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2961 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002962 std::stringstream reasonStream;
2963 reasonStream << "invalid value: Value must be at least 1 and no greater than "
2964 << maxComputeWorkGroupSizeValue;
2965 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03002966
Olli Etuaho4de340a2016-12-16 09:32:03 +00002967 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03002968 return;
2969 }
2970 }
2971 }
2972
2973 mComputeShaderLocalSizeDeclared = true;
2974 }
Shaob5cc1192017-07-06 10:47:20 +08002975 else if (typeQualifier.qualifier == EvqGeometryIn)
2976 {
2977 if (mShaderVersion < 310)
2978 {
2979 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
2980 return;
2981 }
2982
2983 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
2984 {
2985 return;
2986 }
2987 }
2988 else if (typeQualifier.qualifier == EvqGeometryOut)
2989 {
2990 if (mShaderVersion < 310)
2991 {
2992 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
2993 "layout");
2994 return;
2995 }
2996
2997 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
2998 {
2999 return;
3000 }
3001 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003002 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3003 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003004 {
3005 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3006 // specification.
3007 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3008 {
3009 error(typeQualifier.line, "Number of views does not match the previous declaration",
3010 "layout");
3011 return;
3012 }
3013
3014 if (layoutQualifier.numViews == -1)
3015 {
3016 error(typeQualifier.line, "No num_views specified", "layout");
3017 return;
3018 }
3019
3020 if (layoutQualifier.numViews > mMaxNumViews)
3021 {
3022 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3023 "layout");
3024 return;
3025 }
3026
3027 mNumViews = layoutQualifier.numViews;
3028 }
Martin Radev802abe02016-08-04 17:48:32 +03003029 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003030 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003031 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003032 {
Martin Radev802abe02016-08-04 17:48:32 +03003033 return;
3034 }
3035
Jiajia Qinbc585152017-06-23 15:42:17 +08003036 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003037 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003038 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003039 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003040 return;
3041 }
3042
3043 if (mShaderVersion < 300)
3044 {
3045 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3046 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003047 return;
3048 }
3049
Olli Etuaho09b04a22016-12-15 13:30:26 +00003050 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003051
3052 if (layoutQualifier.matrixPacking != EmpUnspecified)
3053 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003054 if (typeQualifier.qualifier == EvqUniform)
3055 {
3056 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3057 }
3058 else if (typeQualifier.qualifier == EvqBuffer)
3059 {
3060 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3061 }
Martin Radev802abe02016-08-04 17:48:32 +03003062 }
3063
3064 if (layoutQualifier.blockStorage != EbsUnspecified)
3065 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003066 if (typeQualifier.qualifier == EvqUniform)
3067 {
3068 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3069 }
3070 else if (typeQualifier.qualifier == EvqBuffer)
3071 {
3072 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3073 }
Martin Radev802abe02016-08-04 17:48:32 +03003074 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003075 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003076}
3077
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003078TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3079 const TFunction &function,
3080 const TSourceLoc &location,
3081 bool insertParametersToSymbolTable)
3082{
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003083 checkIsNotReserved(location, function.getName());
3084
Olli Etuahofe486322017-03-21 09:30:54 +00003085 TIntermFunctionPrototype *prototype =
3086 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003087 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
3088 // point to the data that already exists in the symbol table.
3089 prototype->getFunctionSymbolInfo()->setFromFunction(function);
3090 prototype->setLine(location);
3091
3092 for (size_t i = 0; i < function.getParamCount(); i++)
3093 {
3094 const TConstParameter &param = function.getParam(i);
3095
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003096 TIntermSymbol *symbol = nullptr;
3097
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003098 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3099 // be used for unused args).
3100 if (param.name != nullptr)
3101 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003102 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003103 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003104 {
Olli Etuaho0f684632017-07-13 12:42:15 +03003105 TVariable *variable = symbolTable.declareVariable(param.name, *param.type);
3106 if (variable)
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003107 {
3108 symbol = new TIntermSymbol(variable->getUniqueId(), variable->getName(),
3109 variable->getType());
3110 }
3111 else
3112 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003113 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003114 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003115 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003116 // Unsized type of a named parameter should have already been checked and sanitized.
3117 ASSERT(!param.type->isUnsizedArray());
3118 }
3119 else
3120 {
3121 if (param.type->isUnsizedArray())
3122 {
3123 error(location, "function parameter array must be sized at compile time", "[]");
3124 // We don't need to size the arrays since the parameter is unnamed and hence
3125 // inaccessible.
3126 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003127 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003128 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003129 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003130 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3131 // symbol.
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003132 symbol = new TIntermSymbol(symbolTable.getEmptySymbolId(), "", *param.type);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003133 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003134 symbol->setLine(location);
3135 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003136 }
3137 return prototype;
3138}
3139
Olli Etuaho16c745a2017-01-16 17:02:27 +00003140TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3141 const TFunction &parsedFunction,
3142 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003143{
Olli Etuaho476197f2016-10-11 13:59:08 +01003144 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3145 // first declaration. Either way the instance in the symbol table is used to track whether the
3146 // function is declared multiple times.
3147 TFunction *function = static_cast<TFunction *>(
3148 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3149 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003150 {
3151 // ESSL 1.00.17 section 4.2.7.
3152 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3153 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003154 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003155 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003156
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003157 TIntermFunctionPrototype *prototype =
3158 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003159
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003160 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003161
3162 if (!symbolTable.atGlobalLevel())
3163 {
3164 // ESSL 3.00.4 section 4.2.4.
3165 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003166 }
3167
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003168 return prototype;
3169}
3170
Olli Etuaho336b1472016-10-05 16:37:55 +01003171TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003172 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003173 TIntermBlock *functionBody,
3174 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003175{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003176 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003177 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3178 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003179 error(location, "function does not return a value:",
3180 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003181 }
3182
Olli Etuahof51fdd22016-10-03 10:03:40 +01003183 if (functionBody == nullptr)
3184 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003185 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003186 functionBody->setLine(location);
3187 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003188 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003189 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003190 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003191
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003192 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003193 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003194}
3195
Olli Etuaho476197f2016-10-11 13:59:08 +01003196void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3197 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003198 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003199{
Olli Etuaho476197f2016-10-11 13:59:08 +01003200 ASSERT(function);
3201 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003202 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003203 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003204
3205 if (builtIn)
3206 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003207 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003208 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003209 else
Jamie Madill185fb402015-06-12 15:48:48 -04003210 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003211 TFunction *prevDec = static_cast<TFunction *>(
3212 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3213
3214 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3215 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3216 // occurance.
3217 if (*function != prevDec)
3218 {
3219 // Swap the parameters of the previous declaration to the parameters of the function
3220 // definition (parameter names may differ).
3221 prevDec->swapParameters(**function);
3222
3223 // The function definition will share the same symbol as any previous declaration.
3224 *function = prevDec;
3225 }
3226
3227 if ((*function)->isDefined())
3228 {
3229 error(location, "function already has a body", (*function)->getName().c_str());
3230 }
3231
3232 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003233 }
Jamie Madill185fb402015-06-12 15:48:48 -04003234
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003235 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003236 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003237 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003238
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003239 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003240 setLoopNestingLevel(0);
3241}
3242
Jamie Madillb98c3a82015-07-23 14:26:04 -04003243TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003244{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003245 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003246 // We don't know at this point whether this is a function definition or a prototype.
3247 // The definition production code will check for redefinitions.
3248 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003249 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003250 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3251 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003252 //
3253 TFunction *prevDec =
3254 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303255
Martin Radevda6254b2016-12-14 17:00:36 +02003256 if (getShaderVersion() >= 300 &&
3257 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
3258 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303259 {
Martin Radevda6254b2016-12-14 17:00:36 +02003260 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303261 // Therefore overloading or redefining builtin functions is an error.
3262 error(location, "Name of a built-in function cannot be redeclared as function",
3263 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303264 }
3265 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003266 {
3267 if (prevDec->getReturnType() != function->getReturnType())
3268 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003269 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003270 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003271 }
3272 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3273 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003274 if (prevDec->getParam(i).type->getQualifier() !=
3275 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003276 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003277 error(location,
3278 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003279 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003280 }
3281 }
3282 }
3283
3284 //
3285 // Check for previously declared variables using the same name.
3286 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003287 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003288 if (prevSym)
3289 {
3290 if (!prevSym->isFunction())
3291 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003292 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003293 }
3294 }
3295 else
3296 {
3297 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003298 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003299 }
3300
3301 // We're at the inner scope level of the function's arguments and body statement.
3302 // Add the function prototype to the surrounding scope instead.
3303 symbolTable.getOuterLevel()->insert(function);
3304
Olli Etuaho78d13742017-01-18 13:06:10 +00003305 // Raise error message if main function takes any parameters or return anything other than void
3306 if (function->getName() == "main")
3307 {
3308 if (function->getParamCount() > 0)
3309 {
3310 error(location, "function cannot take any parameter(s)", "main");
3311 }
3312 if (function->getReturnType().getBasicType() != EbtVoid)
3313 {
3314 error(location, "main function cannot return a value",
3315 function->getReturnType().getBasicString());
3316 }
3317 }
3318
Jamie Madill185fb402015-06-12 15:48:48 -04003319 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003320 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3321 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003322 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3323 //
3324 return function;
3325}
3326
Olli Etuaho9de84a52016-06-14 17:36:01 +03003327TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3328 const TString *name,
3329 const TSourceLoc &location)
3330{
3331 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3332 {
3333 error(location, "no qualifiers allowed for function return",
3334 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003335 }
3336 if (!type.layoutQualifier.isEmpty())
3337 {
3338 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003339 }
jchen10cc2a10e2017-05-03 14:05:12 +08003340 // make sure an opaque type is not involved as well...
3341 std::string reason(getBasicString(type.getBasicType()));
3342 reason += "s can't be function return values";
3343 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003344 if (mShaderVersion < 300)
3345 {
3346 // Array return values are forbidden, but there's also no valid syntax for declaring array
3347 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00003348 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003349
3350 if (type.isStructureContainingArrays())
3351 {
3352 // ESSL 1.00.17 section 6.1 Function Definitions
3353 error(location, "structures containing arrays can't be function return values",
3354 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003355 }
3356 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003357
3358 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003359 return new TFunction(&symbolTable, name, new TType(type));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003360}
3361
Olli Etuahocce89652017-06-19 16:04:09 +03003362TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3363{
Olli Etuahocce89652017-06-19 16:04:09 +03003364 const TType *returnType = TCache::getType(EbtVoid, EbpUndefined);
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003365 return new TFunction(&symbolTable, name, returnType);
Olli Etuahocce89652017-06-19 16:04:09 +03003366}
3367
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003368TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003369{
Olli Etuahocce89652017-06-19 16:04:09 +03003370 if (mShaderVersion < 300 && publicType.array)
3371 {
3372 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3373 "[]");
3374 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003375 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003376 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003377 error(publicType.getLine(), "constructor can't be a structure definition",
3378 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003379 }
3380
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003381 TType *type = new TType(publicType);
3382 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003383 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003384 error(publicType.getLine(), "cannot construct this type",
3385 getBasicString(publicType.getBasicType()));
3386 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003387 }
3388
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003389 return new TFunction(&symbolTable, nullptr, type, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003390}
3391
Olli Etuaho55bde912017-10-25 13:41:13 +03003392void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3393 const char *errorMessage,
3394 const char *token,
3395 TType *arrayType)
3396{
3397 if (arrayType->isUnsizedArray())
3398 {
3399 error(line, errorMessage, token);
3400 arrayType->sizeUnsizedArrays(TVector<unsigned int>());
3401 }
3402}
3403
3404TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahocce89652017-06-19 16:04:09 +03003405 const TString *name,
3406 const TSourceLoc &nameLoc)
3407{
Olli Etuaho55bde912017-10-25 13:41:13 +03003408 ASSERT(type);
3409 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name->c_str(),
3410 type);
3411 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003412 {
3413 error(nameLoc, "illegal use of type 'void'", name->c_str());
3414 }
3415 checkIsNotReserved(nameLoc, *name);
Olli Etuahocce89652017-06-19 16:04:09 +03003416 TParameter param = {name, type};
3417 return param;
3418}
3419
Olli Etuaho55bde912017-10-25 13:41:13 +03003420TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3421 const TString *name,
3422 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003423{
Olli Etuaho55bde912017-10-25 13:41:13 +03003424 TType *type = new TType(publicType);
3425 return parseParameterDeclarator(type, name, nameLoc);
3426}
3427
3428TParameter TParseContext::parseParameterArrayDeclarator(const TString *name,
3429 const TSourceLoc &nameLoc,
3430 unsigned int arraySize,
3431 const TSourceLoc &arrayLoc,
3432 TPublicType *elementType)
3433{
3434 checkArrayElementIsNotArray(arrayLoc, *elementType);
3435 TType *arrayType = new TType(*elementType);
3436 arrayType->makeArray(arraySize);
3437 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003438}
3439
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003440bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(TIntermSequence *arguments,
3441 TType type,
3442 const TSourceLoc &line)
3443{
3444 if (arguments->empty())
3445 {
3446 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3447 return false;
3448 }
3449 for (TIntermNode *arg : *arguments)
3450 {
3451 TIntermTyped *element = arg->getAsTyped();
3452 ASSERT(element);
3453 size_t dimensionalityFromElement = element->getType().getArraySizes().size() + 1u;
3454 if (dimensionalityFromElement > type.getArraySizes().size())
3455 {
3456 error(line, "constructing from a non-dereferenced array", "constructor");
3457 return false;
3458 }
3459 else if (dimensionalityFromElement < type.getArraySizes().size())
3460 {
3461 if (dimensionalityFromElement == 1u)
3462 {
3463 error(line, "implicitly sized array of arrays constructor argument is not an array",
3464 "constructor");
3465 }
3466 else
3467 {
3468 error(line,
3469 "implicitly sized array of arrays constructor argument dimensionality is too "
3470 "low",
3471 "constructor");
3472 }
3473 return false;
3474 }
3475 }
3476 return true;
3477}
3478
Jamie Madillb98c3a82015-07-23 14:26:04 -04003479// This function is used to test for the correctness of the parameters passed to various constructor
3480// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003481//
Olli Etuaho856c4972016-08-08 11:38:39 +03003482// 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 +00003483//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003484TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003485 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303486 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003487{
Olli Etuaho856c4972016-08-08 11:38:39 +03003488 if (type.isUnsizedArray())
3489 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003490 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003491 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003492 type.sizeUnsizedArrays(TVector<unsigned int>());
Olli Etuaho3ec75682017-07-05 17:02:55 +03003493 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003494 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003495 TIntermTyped *firstElement = arguments->at(0)->getAsTyped();
3496 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003497 if (type.getOutermostArraySize() == 0u)
3498 {
3499 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments->size()));
3500 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003501 for (size_t i = 0; i < firstElement->getType().getArraySizes().size(); ++i)
3502 {
3503 if (type.getArraySizes()[i] == 0u)
3504 {
3505 type.setArraySize(i, firstElement->getType().getArraySizes().at(i));
3506 }
3507 }
3508 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003509 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003510
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003511 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003512 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003513 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003514 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003515
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003516 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003517 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003518
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003519 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3520 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003521 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003522 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003523 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003524 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003525}
3526
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003527//
3528// Interface/uniform blocks
Jiawei Shaod8105a02017-08-08 09:54:36 +08003529// TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003530//
Olli Etuaho13389b62016-10-16 11:48:18 +01003531TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003532 const TTypeQualifierBuilder &typeQualifierBuilder,
3533 const TSourceLoc &nameLine,
3534 const TString &blockName,
3535 TFieldList *fieldList,
3536 const TString *instanceName,
3537 const TSourceLoc &instanceLine,
3538 TIntermTyped *arrayIndex,
3539 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003540{
Olli Etuaho856c4972016-08-08 11:38:39 +03003541 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003542
Olli Etuaho77ba4082016-12-16 12:01:18 +00003543 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003544
Jiajia Qinbc585152017-06-23 15:42:17 +08003545 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003546 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003547 error(typeQualifier.line,
3548 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3549 "3.10",
3550 getQualifierString(typeQualifier.qualifier));
3551 }
3552 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3553 {
3554 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003555 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003556 }
3557
Martin Radev70866b82016-07-22 15:27:42 +03003558 if (typeQualifier.invariant)
3559 {
3560 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3561 }
3562
Jiajia Qinbc585152017-06-23 15:42:17 +08003563 if (typeQualifier.qualifier != EvqBuffer)
3564 {
3565 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3566 }
Olli Etuaho43364892017-02-13 16:00:12 +00003567
jchen10af713a22017-04-19 09:10:56 +08003568 // add array index
3569 unsigned int arraySize = 0;
3570 if (arrayIndex != nullptr)
3571 {
3572 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3573 }
3574
3575 if (mShaderVersion < 310)
3576 {
3577 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3578 }
3579 else
3580 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003581 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3582 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003583 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003584
Andrei Volykhina5527072017-03-22 16:46:30 +03003585 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3586
Jamie Madill099c0f32013-06-20 11:55:52 -04003587 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003588 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003589 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3590 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003591
Jamie Madill099c0f32013-06-20 11:55:52 -04003592 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3593 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003594 if (typeQualifier.qualifier == EvqUniform)
3595 {
3596 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3597 }
3598 else if (typeQualifier.qualifier == EvqBuffer)
3599 {
3600 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3601 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003602 }
3603
Jamie Madill1566ef72013-06-20 11:55:54 -04003604 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3605 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003606 if (typeQualifier.qualifier == EvqUniform)
3607 {
3608 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3609 }
3610 else if (typeQualifier.qualifier == EvqBuffer)
3611 {
3612 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3613 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003614 }
3615
Olli Etuaho856c4972016-08-08 11:38:39 +03003616 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003617
Martin Radev2cc85b32016-08-05 16:22:53 +03003618 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3619
Olli Etuaho0f684632017-07-13 12:42:15 +03003620 if (!symbolTable.declareInterfaceBlockName(&blockName))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303621 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003622 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003623 }
3624
Jamie Madill98493dd2013-07-08 14:39:03 -04003625 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303626 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3627 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003628 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303629 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003630 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303631 {
jchen10cc2a10e2017-05-03 14:05:12 +08003632 std::string reason("unsupported type - ");
3633 reason += fieldType->getBasicString();
3634 reason += " types are not allowed in interface blocks";
3635 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003636 }
3637
Jamie Madill98493dd2013-07-08 14:39:03 -04003638 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003639 switch (qualifier)
3640 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003641 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003642 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003643 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003644 if (typeQualifier.qualifier == EvqBuffer)
3645 {
3646 error(field->line(), "invalid qualifier on shader storage block member",
3647 getQualifierString(qualifier));
3648 }
3649 break;
3650 case EvqBuffer:
3651 if (typeQualifier.qualifier == EvqUniform)
3652 {
3653 error(field->line(), "invalid qualifier on uniform block member",
3654 getQualifierString(qualifier));
3655 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003656 break;
3657 default:
3658 error(field->line(), "invalid qualifier on interface block member",
3659 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003660 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003661 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003662
Martin Radev70866b82016-07-22 15:27:42 +03003663 if (fieldType->isInvariant())
3664 {
3665 error(field->line(), "invalid qualifier on interface block member", "invariant");
3666 }
3667
Jamie Madilla5efff92013-06-06 11:56:47 -04003668 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003669 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003670 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003671 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003672
Jamie Madill98493dd2013-07-08 14:39:03 -04003673 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003674 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003675 error(field->line(), "invalid layout qualifier: cannot be used here",
3676 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003677 }
3678
Jamie Madill98493dd2013-07-08 14:39:03 -04003679 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003680 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003681 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003682 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003683 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003684 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003685 warning(field->line(),
3686 "extraneous layout qualifier: only has an effect on matrix types",
3687 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003688 }
3689
Jamie Madill98493dd2013-07-08 14:39:03 -04003690 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003691
3692 if (typeQualifier.qualifier == EvqBuffer)
3693 {
3694 // set memory qualifiers
3695 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3696 // qualified with a memory qualifier, it is as if all of its members were declared with
3697 // the same memory qualifier.
3698 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3699 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3700 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3701 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3702 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3703 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3704 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3705 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3706 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3707 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3708 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003709 }
3710
Jamie Madillb98c3a82015-07-23 14:26:04 -04003711 TInterfaceBlock *interfaceBlock =
Shaob18c33e2017-08-16 12:37:51 +08003712 new TInterfaceBlock(&blockName, fieldList, instanceName, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003713 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
3714 if (arrayIndex != nullptr)
3715 {
3716 interfaceBlockType.makeArray(arraySize);
3717 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003718
3719 TString symbolName = "";
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003720 const TSymbolUniqueId *symbolId = nullptr;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003721
Jamie Madill98493dd2013-07-08 14:39:03 -04003722 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003723 {
3724 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003725 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3726 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003727 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303728 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003729
3730 // set parent pointer of the field variable
3731 fieldType->setInterfaceBlock(interfaceBlock);
3732
Olli Etuaho0f684632017-07-13 12:42:15 +03003733 TVariable *fieldVariable = symbolTable.declareVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003734
Olli Etuaho0f684632017-07-13 12:42:15 +03003735 if (fieldVariable)
3736 {
3737 fieldVariable->setQualifier(typeQualifier.qualifier);
3738 }
3739 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303740 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003741 error(field->line(), "redefinition of an interface block member name",
3742 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003743 }
3744 }
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003745 symbolId = &symbolTable.getEmptySymbolId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003746 }
3747 else
3748 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003749 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003750
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003751 // add a symbol for this interface block
Olli Etuaho0f684632017-07-13 12:42:15 +03003752 TVariable *instanceTypeDef = symbolTable.declareVariable(instanceName, interfaceBlockType);
3753 if (instanceTypeDef)
3754 {
3755 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003756 symbolId = &instanceTypeDef->getUniqueId();
Olli Etuaho0f684632017-07-13 12:42:15 +03003757 }
3758 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303759 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003760 error(instanceLine, "redefinition of an interface block instance name",
3761 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003762 }
Olli Etuaho0f684632017-07-13 12:42:15 +03003763 symbolName = *instanceName;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003764 }
3765
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003766 TIntermDeclaration *declaration = nullptr;
3767
3768 if (symbolId)
3769 {
3770 TIntermSymbol *blockSymbol = new TIntermSymbol(*symbolId, symbolName, interfaceBlockType);
3771 blockSymbol->setLine(typeQualifier.line);
3772 declaration = new TIntermDeclaration();
3773 declaration->appendDeclarator(blockSymbol);
3774 declaration->setLine(nameLine);
3775 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003776
3777 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003778 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003779}
3780
Olli Etuaho383b7912016-08-05 11:22:59 +03003781void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003782{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003783 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003784
3785 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003786 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303787 if (mStructNestingLevel > 1)
3788 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003789 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003790 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003791}
3792
3793void TParseContext::exitStructDeclaration()
3794{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003795 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003796}
3797
Olli Etuaho8a176262016-08-16 14:23:01 +03003798void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003799{
Jamie Madillacb4b812016-11-07 13:50:29 -05003800 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303801 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003802 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003803 }
3804
Arun Patole7e7e68d2015-05-22 12:02:25 +05303805 if (field.type()->getBasicType() != EbtStruct)
3806 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003807 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003808 }
3809
3810 // We're already inside a structure definition at this point, so add
3811 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303812 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3813 {
Jamie Madill41a49272014-03-18 16:10:13 -04003814 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003815 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3816 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003817 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003818 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003819 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003820 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003821}
3822
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003823//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003824// Parse an array index expression
3825//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003826TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3827 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303828 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003829{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003830 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3831 {
3832 if (baseExpression->getAsSymbolNode())
3833 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303834 error(location, " left of '[' is not of type array, matrix, or vector ",
3835 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003836 }
3837 else
3838 {
3839 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3840 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003841
Olli Etuaho3ec75682017-07-05 17:02:55 +03003842 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003843 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003844
Jiawei Shaod8105a02017-08-08 09:54:36 +08003845 if (baseExpression->getQualifier() == EvqPerVertexIn)
3846 {
3847 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
3848 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3849 {
3850 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3851 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3852 }
3853 }
3854
Jamie Madill21c1e452014-12-29 11:33:41 -05003855 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3856
Olli Etuaho36b05142015-11-12 13:10:42 +02003857 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3858 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3859 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3860 // index is a constant expression.
3861 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3862 {
3863 if (baseExpression->isInterfaceBlock())
3864 {
Jiawei Shaod8105a02017-08-08 09:54:36 +08003865 // TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
3866 switch (baseExpression->getQualifier())
3867 {
3868 case EvqPerVertexIn:
3869 break;
3870 case EvqUniform:
3871 case EvqBuffer:
3872 error(location,
3873 "array indexes for uniform block arrays and shader storage block arrays "
3874 "must be constant integral expressions",
3875 "[");
3876 break;
3877 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003878 // We can reach here only in error cases.
3879 ASSERT(mDiagnostics->numErrors() > 0);
3880 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003881 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003882 }
3883 else if (baseExpression->getQualifier() == EvqFragmentOut)
3884 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003885 error(location,
3886 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003887 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003888 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3889 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003890 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003891 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003892 }
3893
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003894 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003895 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003896 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3897 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3898 // constant fold expressions that are not constant expressions). The most compatible way to
3899 // handle this case is to report a warning instead of an error and force the index to be in
3900 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003901 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003902 int index = 0;
3903 if (indexConstantUnion->getBasicType() == EbtInt)
3904 {
3905 index = indexConstantUnion->getIConst(0);
3906 }
3907 else if (indexConstantUnion->getBasicType() == EbtUInt)
3908 {
3909 index = static_cast<int>(indexConstantUnion->getUConst(0));
3910 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003911
3912 int safeIndex = -1;
3913
3914 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003915 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003916 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003917 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003918 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003919 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003920 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003921 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003922 "GL_EXT_draw_buffers is disabled",
3923 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003924 safeIndex = 0;
3925 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003926 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003927 // Only do generic out-of-range check if similar error hasn't already been reported.
3928 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003929 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003930 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003931 baseExpression->getOutermostArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003932 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003933 }
3934 }
3935 else if (baseExpression->isMatrix())
3936 {
3937 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003938 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003939 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003940 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003941 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003942 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003943 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3944 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003945 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003946 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003947
3948 ASSERT(safeIndex >= 0);
3949 // Data of constant unions can't be changed, because it may be shared with other
3950 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3951 // sanitized object.
Olli Etuaho56229f12017-07-10 14:16:33 +03003952 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003953 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003954 TConstantUnion *safeConstantUnion = new TConstantUnion();
3955 safeConstantUnion->setIConst(safeIndex);
3956 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
Olli Etuaho56229f12017-07-10 14:16:33 +03003957 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003958 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003959
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003960 TIntermBinary *node = new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
3961 node->setLine(location);
3962 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003963 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003964 else
3965 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003966 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
3967 node->setLine(location);
3968 // Indirect indexing can never be constant folded.
3969 return node;
Jamie Madill7164cf42013-07-08 13:30:59 -04003970 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003971}
3972
Olli Etuaho90892fb2016-07-14 14:44:51 +03003973int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3974 const TSourceLoc &location,
3975 int index,
3976 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003977 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003978{
3979 if (index >= arraySize || index < 0)
3980 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003981 std::stringstream reasonStream;
3982 reasonStream << reason << " '" << index << "'";
3983 std::string token = reasonStream.str();
3984 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003985 if (index < 0)
3986 {
3987 return 0;
3988 }
3989 else
3990 {
3991 return arraySize - 1;
3992 }
3993 }
3994 return index;
3995}
3996
Jamie Madillb98c3a82015-07-23 14:26:04 -04003997TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3998 const TSourceLoc &dotLocation,
3999 const TString &fieldString,
4000 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004001{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004002 if (baseExpression->isArray())
4003 {
4004 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004005 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004006 }
4007
4008 if (baseExpression->isVector())
4009 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004010 TVector<int> fieldOffsets;
4011 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4012 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004013 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004014 fieldOffsets.resize(1);
4015 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004016 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004017 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4018 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004019
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004020 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004021 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004022 else if (baseExpression->getBasicType() == EbtStruct)
4023 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304024 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004025 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004026 {
4027 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004028 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004029 }
4030 else
4031 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004032 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004033 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004034 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004035 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004036 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004037 {
4038 fieldFound = true;
4039 break;
4040 }
4041 }
4042 if (fieldFound)
4043 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004044 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004045 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004046 TIntermBinary *node =
4047 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4048 node->setLine(dotLocation);
4049 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004050 }
4051 else
4052 {
4053 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004054 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004055 }
4056 }
4057 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004058 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004059 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304060 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004061 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004062 {
4063 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004064 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004065 }
4066 else
4067 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004068 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004069 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004070 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004071 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004072 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004073 {
4074 fieldFound = true;
4075 break;
4076 }
4077 }
4078 if (fieldFound)
4079 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004080 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004081 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004082 TIntermBinary *node =
4083 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4084 node->setLine(dotLocation);
4085 // Indexing interface blocks can never be constant folded.
4086 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004087 }
4088 else
4089 {
4090 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004091 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004092 }
4093 }
4094 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004095 else
4096 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004097 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004098 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004099 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304100 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004101 }
4102 else
4103 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304104 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004105 " field selection requires structure, vector, or interface block on left hand "
4106 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304107 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004108 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004109 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004110 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004111}
4112
Jamie Madillb98c3a82015-07-23 14:26:04 -04004113TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4114 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004115{
Martin Radev802abe02016-08-04 17:48:32 +03004116 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004117
4118 if (qualifierType == "shared")
4119 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004120 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004121 {
4122 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4123 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004124 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004125 }
4126 else if (qualifierType == "packed")
4127 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004128 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004129 {
4130 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4131 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004132 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004133 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004134 else if (qualifierType == "std430")
4135 {
4136 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4137 qualifier.blockStorage = EbsStd430;
4138 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004139 else if (qualifierType == "std140")
4140 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004141 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004142 }
4143 else if (qualifierType == "row_major")
4144 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004145 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004146 }
4147 else if (qualifierType == "column_major")
4148 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004149 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004150 }
4151 else if (qualifierType == "location")
4152 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004153 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4154 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004155 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004156 else if (qualifierType == "yuv" && isExtensionEnabled(TExtension::EXT_YUV_target) &&
Andrei Volykhina5527072017-03-22 16:46:30 +03004157 mShaderType == GL_FRAGMENT_SHADER)
4158 {
4159 qualifier.yuv = true;
4160 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004161 else if (qualifierType == "rgba32f")
4162 {
4163 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4164 qualifier.imageInternalFormat = EiifRGBA32F;
4165 }
4166 else if (qualifierType == "rgba16f")
4167 {
4168 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4169 qualifier.imageInternalFormat = EiifRGBA16F;
4170 }
4171 else if (qualifierType == "r32f")
4172 {
4173 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4174 qualifier.imageInternalFormat = EiifR32F;
4175 }
4176 else if (qualifierType == "rgba8")
4177 {
4178 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4179 qualifier.imageInternalFormat = EiifRGBA8;
4180 }
4181 else if (qualifierType == "rgba8_snorm")
4182 {
4183 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4184 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4185 }
4186 else if (qualifierType == "rgba32i")
4187 {
4188 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4189 qualifier.imageInternalFormat = EiifRGBA32I;
4190 }
4191 else if (qualifierType == "rgba16i")
4192 {
4193 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4194 qualifier.imageInternalFormat = EiifRGBA16I;
4195 }
4196 else if (qualifierType == "rgba8i")
4197 {
4198 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4199 qualifier.imageInternalFormat = EiifRGBA8I;
4200 }
4201 else if (qualifierType == "r32i")
4202 {
4203 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4204 qualifier.imageInternalFormat = EiifR32I;
4205 }
4206 else if (qualifierType == "rgba32ui")
4207 {
4208 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4209 qualifier.imageInternalFormat = EiifRGBA32UI;
4210 }
4211 else if (qualifierType == "rgba16ui")
4212 {
4213 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4214 qualifier.imageInternalFormat = EiifRGBA16UI;
4215 }
4216 else if (qualifierType == "rgba8ui")
4217 {
4218 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4219 qualifier.imageInternalFormat = EiifRGBA8UI;
4220 }
4221 else if (qualifierType == "r32ui")
4222 {
4223 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4224 qualifier.imageInternalFormat = EiifR32UI;
4225 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004226 else if (qualifierType == "points" && isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004227 mShaderType == GL_GEOMETRY_SHADER_OES)
4228 {
4229 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4230 qualifier.primitiveType = EptPoints;
4231 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004232 else if (qualifierType == "lines" && isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004233 mShaderType == GL_GEOMETRY_SHADER_OES)
4234 {
4235 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4236 qualifier.primitiveType = EptLines;
4237 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004238 else if (qualifierType == "lines_adjacency" &&
4239 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004240 mShaderType == GL_GEOMETRY_SHADER_OES)
4241 {
4242 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4243 qualifier.primitiveType = EptLinesAdjacency;
4244 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004245 else if (qualifierType == "triangles" && isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004246 mShaderType == GL_GEOMETRY_SHADER_OES)
4247 {
4248 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4249 qualifier.primitiveType = EptTriangles;
4250 }
4251 else if (qualifierType == "triangles_adjacency" &&
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004252 isExtensionEnabled(TExtension::OES_geometry_shader) &&
4253 mShaderType == GL_GEOMETRY_SHADER_OES)
Shaob5cc1192017-07-06 10:47:20 +08004254 {
4255 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4256 qualifier.primitiveType = EptTrianglesAdjacency;
4257 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004258 else if (qualifierType == "line_strip" && isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004259 mShaderType == GL_GEOMETRY_SHADER_OES)
4260 {
4261 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4262 qualifier.primitiveType = EptLineStrip;
4263 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004264 else if (qualifierType == "triangle_strip" &&
4265 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004266 mShaderType == GL_GEOMETRY_SHADER_OES)
4267 {
4268 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4269 qualifier.primitiveType = EptTriangleStrip;
4270 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004271
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004272 else
4273 {
4274 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004275 }
4276
Jamie Madilla5efff92013-06-06 11:56:47 -04004277 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004278}
4279
Martin Radev802abe02016-08-04 17:48:32 +03004280void TParseContext::parseLocalSize(const TString &qualifierType,
4281 const TSourceLoc &qualifierTypeLine,
4282 int intValue,
4283 const TSourceLoc &intValueLine,
4284 const std::string &intValueString,
4285 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004286 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004287{
Olli Etuaho856c4972016-08-08 11:38:39 +03004288 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004289 if (intValue < 1)
4290 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004291 std::stringstream reasonStream;
4292 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4293 std::string reason = reasonStream.str();
4294 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004295 }
4296 (*localSize)[index] = intValue;
4297}
4298
Olli Etuaho09b04a22016-12-15 13:30:26 +00004299void TParseContext::parseNumViews(int intValue,
4300 const TSourceLoc &intValueLine,
4301 const std::string &intValueString,
4302 int *numViews)
4303{
4304 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4305 // specification.
4306 if (intValue < 1)
4307 {
4308 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4309 }
4310 *numViews = intValue;
4311}
4312
Shaob5cc1192017-07-06 10:47:20 +08004313void TParseContext::parseInvocations(int intValue,
4314 const TSourceLoc &intValueLine,
4315 const std::string &intValueString,
4316 int *numInvocations)
4317{
4318 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4319 // it doesn't make sense to accept invocations <= 0.
4320 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4321 {
4322 error(intValueLine,
4323 "out of range: invocations must be in the range of [1, "
4324 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4325 intValueString.c_str());
4326 }
4327 else
4328 {
4329 *numInvocations = intValue;
4330 }
4331}
4332
4333void TParseContext::parseMaxVertices(int intValue,
4334 const TSourceLoc &intValueLine,
4335 const std::string &intValueString,
4336 int *maxVertices)
4337{
4338 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4339 // it doesn't make sense to accept max_vertices < 0.
4340 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4341 {
4342 error(
4343 intValueLine,
4344 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4345 intValueString.c_str());
4346 }
4347 else
4348 {
4349 *maxVertices = intValue;
4350 }
4351}
4352
Jamie Madillb98c3a82015-07-23 14:26:04 -04004353TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4354 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004355 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304356 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004357{
Martin Radev802abe02016-08-04 17:48:32 +03004358 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004359
Martin Radev802abe02016-08-04 17:48:32 +03004360 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004361
Martin Radev802abe02016-08-04 17:48:32 +03004362 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004363 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004364 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004365 if (intValue < 0)
4366 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004367 error(intValueLine, "out of range: location must be non-negative",
4368 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004369 }
4370 else
4371 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004372 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004373 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004374 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004375 }
Olli Etuaho43364892017-02-13 16:00:12 +00004376 else if (qualifierType == "binding")
4377 {
4378 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4379 if (intValue < 0)
4380 {
4381 error(intValueLine, "out of range: binding must be non-negative",
4382 intValueString.c_str());
4383 }
4384 else
4385 {
4386 qualifier.binding = intValue;
4387 }
4388 }
jchen104cdac9e2017-05-08 11:01:20 +08004389 else if (qualifierType == "offset")
4390 {
4391 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4392 if (intValue < 0)
4393 {
4394 error(intValueLine, "out of range: offset must be non-negative",
4395 intValueString.c_str());
4396 }
4397 else
4398 {
4399 qualifier.offset = intValue;
4400 }
4401 }
Martin Radev802abe02016-08-04 17:48:32 +03004402 else if (qualifierType == "local_size_x")
4403 {
4404 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4405 &qualifier.localSize);
4406 }
4407 else if (qualifierType == "local_size_y")
4408 {
4409 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4410 &qualifier.localSize);
4411 }
4412 else if (qualifierType == "local_size_z")
4413 {
4414 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4415 &qualifier.localSize);
4416 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004417 else if (qualifierType == "num_views" && isExtensionEnabled(TExtension::OVR_multiview) &&
Olli Etuaho09b04a22016-12-15 13:30:26 +00004418 mShaderType == GL_VERTEX_SHADER)
4419 {
4420 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4421 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004422 else if (qualifierType == "invocations" &&
4423 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004424 mShaderType == GL_GEOMETRY_SHADER_OES)
4425 {
4426 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4427 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004428 else if (qualifierType == "max_vertices" &&
4429 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004430 mShaderType == GL_GEOMETRY_SHADER_OES)
4431 {
4432 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4433 }
4434
Martin Radev802abe02016-08-04 17:48:32 +03004435 else
4436 {
4437 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004438 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004439
Jamie Madilla5efff92013-06-06 11:56:47 -04004440 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004441}
4442
Olli Etuaho613b9592016-09-05 12:05:53 +03004443TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4444{
4445 return new TTypeQualifierBuilder(
4446 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4447 mShaderVersion);
4448}
4449
Olli Etuahocce89652017-06-19 16:04:09 +03004450TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4451 const TSourceLoc &loc)
4452{
4453 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4454 return new TStorageQualifierWrapper(qualifier, loc);
4455}
4456
4457TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4458{
4459 if (getShaderType() == GL_VERTEX_SHADER)
4460 {
4461 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4462 }
4463 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4464}
4465
4466TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4467{
4468 if (declaringFunction())
4469 {
4470 return new TStorageQualifierWrapper(EvqIn, loc);
4471 }
Shaob5cc1192017-07-06 10:47:20 +08004472
4473 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004474 {
Shaob5cc1192017-07-06 10:47:20 +08004475 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004476 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004477 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004478 {
4479 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4480 }
4481 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004482 }
Shaob5cc1192017-07-06 10:47:20 +08004483 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004484 {
Shaob5cc1192017-07-06 10:47:20 +08004485 if (mShaderVersion < 300)
4486 {
4487 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4488 }
4489 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004490 }
Shaob5cc1192017-07-06 10:47:20 +08004491 case GL_COMPUTE_SHADER:
4492 {
4493 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4494 }
4495 case GL_GEOMETRY_SHADER_OES:
4496 {
4497 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4498 }
4499 default:
4500 {
4501 UNREACHABLE();
4502 return new TStorageQualifierWrapper(EvqLast, loc);
4503 }
Olli Etuahocce89652017-06-19 16:04:09 +03004504 }
Olli Etuahocce89652017-06-19 16:04:09 +03004505}
4506
4507TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4508{
4509 if (declaringFunction())
4510 {
4511 return new TStorageQualifierWrapper(EvqOut, loc);
4512 }
Shaob5cc1192017-07-06 10:47:20 +08004513 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004514 {
Shaob5cc1192017-07-06 10:47:20 +08004515 case GL_VERTEX_SHADER:
4516 {
4517 if (mShaderVersion < 300)
4518 {
4519 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4520 }
4521 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4522 }
4523 case GL_FRAGMENT_SHADER:
4524 {
4525 if (mShaderVersion < 300)
4526 {
4527 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4528 }
4529 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4530 }
4531 case GL_COMPUTE_SHADER:
4532 {
4533 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4534 return new TStorageQualifierWrapper(EvqLast, loc);
4535 }
4536 case GL_GEOMETRY_SHADER_OES:
4537 {
4538 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4539 }
4540 default:
4541 {
4542 UNREACHABLE();
4543 return new TStorageQualifierWrapper(EvqLast, loc);
4544 }
Olli Etuahocce89652017-06-19 16:04:09 +03004545 }
Olli Etuahocce89652017-06-19 16:04:09 +03004546}
4547
4548TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4549{
4550 if (!declaringFunction())
4551 {
4552 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4553 }
4554 return new TStorageQualifierWrapper(EvqInOut, loc);
4555}
4556
Jamie Madillb98c3a82015-07-23 14:26:04 -04004557TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004558 TLayoutQualifier rightQualifier,
4559 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004560{
Martin Radevc28888b2016-07-22 15:27:42 +03004561 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004562 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004563}
4564
Olli Etuahocce89652017-06-19 16:04:09 +03004565TField *TParseContext::parseStructDeclarator(TString *identifier, const TSourceLoc &loc)
4566{
4567 checkIsNotReserved(loc, *identifier);
4568 TType *type = new TType(EbtVoid, EbpUndefined);
4569 return new TField(type, identifier, loc);
4570}
4571
4572TField *TParseContext::parseStructArrayDeclarator(TString *identifier,
4573 const TSourceLoc &loc,
Olli Etuaho4ddae352017-10-26 16:20:18 +03004574 unsigned int arraySize,
Olli Etuahocce89652017-06-19 16:04:09 +03004575 const TSourceLoc &arraySizeLoc)
4576{
4577 checkIsNotReserved(loc, *identifier);
4578
4579 TType *type = new TType(EbtVoid, EbpUndefined);
Olli Etuaho4ddae352017-10-26 16:20:18 +03004580 type->makeArray(arraySize);
Olli Etuahocce89652017-06-19 16:04:09 +03004581
4582 return new TField(type, identifier, loc);
4583}
4584
Olli Etuaho722bfb52017-10-26 17:00:11 +03004585void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4586 const TFieldList::const_iterator end,
4587 const TString &name,
4588 const TSourceLoc &location)
4589{
4590 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4591 {
4592 if ((*fieldIter)->name() == name)
4593 {
4594 error(location, "duplicate field name in structure", name.c_str());
4595 }
4596 }
4597}
4598
4599TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4600{
4601 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4602 ++fieldIter)
4603 {
4604 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4605 location);
4606 }
4607 return fields;
4608}
4609
Olli Etuaho4de340a2016-12-16 09:32:03 +00004610TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4611 const TFieldList *newlyAddedFields,
4612 const TSourceLoc &location)
4613{
4614 for (TField *field : *newlyAddedFields)
4615 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004616 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4617 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004618 processedFields->push_back(field);
4619 }
4620 return processedFields;
4621}
4622
Martin Radev70866b82016-07-22 15:27:42 +03004623TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4624 const TTypeQualifierBuilder &typeQualifierBuilder,
4625 TPublicType *typeSpecifier,
4626 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004627{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004628 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004629
Martin Radev70866b82016-07-22 15:27:42 +03004630 typeSpecifier->qualifier = typeQualifier.qualifier;
4631 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004632 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004633 typeSpecifier->invariant = typeQualifier.invariant;
4634 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304635 {
Martin Radev70866b82016-07-22 15:27:42 +03004636 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004637 }
Martin Radev70866b82016-07-22 15:27:42 +03004638 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004639}
4640
Jamie Madillb98c3a82015-07-23 14:26:04 -04004641TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004642 TFieldList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004643{
Martin Radev4a9cd802016-09-01 16:51:51 +03004644 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4645 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004646
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004647 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
4648 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004649
Martin Radev4a9cd802016-09-01 16:51:51 +03004650 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004651
Olli Etuaho55bde912017-10-25 13:41:13 +03004652 for (TField *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304653 {
Olli Etuaho55bde912017-10-25 13:41:13 +03004654 auto declaratorArraySizes = declarator->type()->getArraySizes();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004655 // don't allow arrays of arrays
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004656 if (!declaratorArraySizes.empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304657 {
Olli Etuahoe0803872017-08-23 15:30:23 +03004658 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004659 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004660
Olli Etuaho55bde912017-10-25 13:41:13 +03004661 TType *type = declarator->type();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004662 *type = TType(typeSpecifier);
4663 for (unsigned int arraySize : declaratorArraySizes)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304664 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004665 type->makeArray(arraySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004666 }
Olli Etuaho55bde912017-10-25 13:41:13 +03004667 checkIsNotUnsizedArray(typeSpecifier.getLine(),
4668 "array members of structs must specify a size",
4669 declarator->name().c_str(), type);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004670
Olli Etuaho55bde912017-10-25 13:41:13 +03004671 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *declarator);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004672 }
4673
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004674 return declaratorList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004675}
4676
Martin Radev4a9cd802016-09-01 16:51:51 +03004677TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4678 const TSourceLoc &nameLine,
4679 const TString *structName,
4680 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004681{
Olli Etuahoa5e693a2017-07-13 16:07:26 +03004682 TStructure *structure = new TStructure(&symbolTable, structName, fieldList);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004683
Jamie Madill9b820842015-02-12 10:40:10 -05004684 // Store a bool in the struct if we're at global scope, to allow us to
4685 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004686 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004687
Jamie Madill98493dd2013-07-08 14:39:03 -04004688 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004689 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004690 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004691 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304692 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004693 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004694 }
4695 }
4696
4697 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004698 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004699 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004700 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004701 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004702 switch (qualifier)
4703 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004704 case EvqGlobal:
4705 case EvqTemporary:
4706 break;
4707 default:
4708 error(field.line(), "invalid qualifier on struct member",
4709 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004710 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004711 }
Martin Radev70866b82016-07-22 15:27:42 +03004712 if (field.type()->isInvariant())
4713 {
4714 error(field.line(), "invalid qualifier on struct member", "invariant");
4715 }
jchen104cdac9e2017-05-08 11:01:20 +08004716 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4717 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004718 {
4719 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4720 }
4721
Olli Etuaho43364892017-02-13 16:00:12 +00004722 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4723
4724 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004725
4726 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004727 }
4728
Martin Radev4a9cd802016-09-01 16:51:51 +03004729 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004730 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004731 exitStructDeclaration();
4732
Martin Radev4a9cd802016-09-01 16:51:51 +03004733 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004734}
4735
Jamie Madillb98c3a82015-07-23 14:26:04 -04004736TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004737 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004738 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004739{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004740 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004741 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004742 init->isVector())
4743 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004744 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4745 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004746 return nullptr;
4747 }
4748
Olli Etuaho923ecef2017-10-11 12:01:38 +03004749 ASSERT(statementList);
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004750 if (!ValidateSwitchStatementList(switchType, mShaderVersion, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004751 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004752 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004753 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004754 }
4755
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004756 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4757 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004758 return node;
4759}
4760
4761TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4762{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004763 if (mSwitchNestingLevel == 0)
4764 {
4765 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004766 return nullptr;
4767 }
4768 if (condition == nullptr)
4769 {
4770 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004771 return nullptr;
4772 }
4773 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004774 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004775 {
4776 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004777 }
4778 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004779 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4780 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4781 // fold in case labels.
4782 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004783 {
4784 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004785 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004786 TIntermCase *node = new TIntermCase(condition);
4787 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004788 return node;
4789}
4790
4791TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4792{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004793 if (mSwitchNestingLevel == 0)
4794 {
4795 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004796 return nullptr;
4797 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004798 TIntermCase *node = new TIntermCase(nullptr);
4799 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004800 return node;
4801}
4802
Jamie Madillb98c3a82015-07-23 14:26:04 -04004803TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4804 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004805 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004806{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004807 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004808
4809 switch (op)
4810 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004811 case EOpLogicalNot:
4812 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4813 child->isVector())
4814 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004815 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004816 return nullptr;
4817 }
4818 break;
4819 case EOpBitwiseNot:
4820 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4821 child->isMatrix() || child->isArray())
4822 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004823 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004824 return nullptr;
4825 }
4826 break;
4827 case EOpPostIncrement:
4828 case EOpPreIncrement:
4829 case EOpPostDecrement:
4830 case EOpPreDecrement:
4831 case EOpNegative:
4832 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004833 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4834 child->getBasicType() == EbtBool || child->isArray() ||
4835 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004836 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004837 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004838 return nullptr;
4839 }
4840 // Operators for built-ins are already type checked against their prototype.
4841 default:
4842 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004843 }
4844
Jiajia Qinbc585152017-06-23 15:42:17 +08004845 if (child->getMemoryQualifier().writeonly)
4846 {
4847 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4848 return nullptr;
4849 }
4850
Olli Etuahof119a262016-08-19 15:54:22 +03004851 TIntermUnary *node = new TIntermUnary(op, child);
4852 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004853
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004854 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004855}
4856
Olli Etuaho09b22472015-02-11 11:47:26 +02004857TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4858{
Olli Etuahocce89652017-06-19 16:04:09 +03004859 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004860 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004861 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004862 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004863 return child;
4864 }
4865 return node;
4866}
4867
Jamie Madillb98c3a82015-07-23 14:26:04 -04004868TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4869 TIntermTyped *child,
4870 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004871{
Olli Etuaho856c4972016-08-08 11:38:39 +03004872 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004873 return addUnaryMath(op, child, loc);
4874}
4875
Jamie Madillb98c3a82015-07-23 14:26:04 -04004876bool TParseContext::binaryOpCommonCheck(TOperator op,
4877 TIntermTyped *left,
4878 TIntermTyped *right,
4879 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004880{
jchen10b4cf5652017-05-05 18:51:17 +08004881 // Check opaque types are not allowed to be operands in expressions other than array indexing
4882 // and structure member selection.
4883 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4884 {
4885 switch (op)
4886 {
4887 case EOpIndexDirect:
4888 case EOpIndexIndirect:
4889 break;
4890 case EOpIndexDirectStruct:
4891 UNREACHABLE();
4892
4893 default:
4894 error(loc, "Invalid operation for variables with an opaque type",
4895 GetOperatorString(op));
4896 return false;
4897 }
4898 }
jchen10cc2a10e2017-05-03 14:05:12 +08004899
Jiajia Qinbc585152017-06-23 15:42:17 +08004900 if (right->getMemoryQualifier().writeonly)
4901 {
4902 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4903 return false;
4904 }
4905
4906 if (left->getMemoryQualifier().writeonly)
4907 {
4908 switch (op)
4909 {
4910 case EOpAssign:
4911 case EOpInitialize:
4912 case EOpIndexDirect:
4913 case EOpIndexIndirect:
4914 case EOpIndexDirectStruct:
4915 case EOpIndexDirectInterfaceBlock:
4916 break;
4917 default:
4918 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4919 return false;
4920 }
4921 }
4922
Olli Etuaho244be012016-08-18 15:26:02 +03004923 if (left->getType().getStruct() || right->getType().getStruct())
4924 {
4925 switch (op)
4926 {
4927 case EOpIndexDirectStruct:
4928 ASSERT(left->getType().getStruct());
4929 break;
4930 case EOpEqual:
4931 case EOpNotEqual:
4932 case EOpAssign:
4933 case EOpInitialize:
4934 if (left->getType() != right->getType())
4935 {
4936 return false;
4937 }
4938 break;
4939 default:
4940 error(loc, "Invalid operation for structs", GetOperatorString(op));
4941 return false;
4942 }
4943 }
4944
Olli Etuaho94050052017-05-08 14:17:44 +03004945 if (left->isInterfaceBlock() || right->isInterfaceBlock())
4946 {
4947 switch (op)
4948 {
4949 case EOpIndexDirectInterfaceBlock:
4950 ASSERT(left->getType().getInterfaceBlock());
4951 break;
4952 default:
4953 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
4954 return false;
4955 }
4956 }
4957
Olli Etuahod6b14282015-03-17 14:31:35 +02004958 if (left->isArray() || right->isArray())
4959 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004960 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02004961 {
4962 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4963 return false;
4964 }
4965
4966 if (left->isArray() != right->isArray())
4967 {
4968 error(loc, "array / non-array mismatch", GetOperatorString(op));
4969 return false;
4970 }
4971
4972 switch (op)
4973 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004974 case EOpEqual:
4975 case EOpNotEqual:
4976 case EOpAssign:
4977 case EOpInitialize:
4978 break;
4979 default:
4980 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4981 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02004982 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03004983 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004984 if (left->getType().getArraySizes() != right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02004985 {
4986 error(loc, "array size mismatch", GetOperatorString(op));
4987 return false;
4988 }
Olli Etuahod6b14282015-03-17 14:31:35 +02004989 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004990
4991 // Check ops which require integer / ivec parameters
4992 bool isBitShift = false;
4993 switch (op)
4994 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004995 case EOpBitShiftLeft:
4996 case EOpBitShiftRight:
4997 case EOpBitShiftLeftAssign:
4998 case EOpBitShiftRightAssign:
4999 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5000 // check that the basic type is an integer type.
5001 isBitShift = true;
5002 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5003 {
5004 return false;
5005 }
5006 break;
5007 case EOpBitwiseAnd:
5008 case EOpBitwiseXor:
5009 case EOpBitwiseOr:
5010 case EOpBitwiseAndAssign:
5011 case EOpBitwiseXorAssign:
5012 case EOpBitwiseOrAssign:
5013 // It is enough to check the type of only one operand, since later it
5014 // is checked that the operand types match.
5015 if (!IsInteger(left->getBasicType()))
5016 {
5017 return false;
5018 }
5019 break;
5020 default:
5021 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005022 }
5023
5024 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5025 // So the basic type should usually match.
5026 if (!isBitShift && left->getBasicType() != right->getBasicType())
5027 {
5028 return false;
5029 }
5030
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005031 // Check that:
5032 // 1. Type sizes match exactly on ops that require that.
5033 // 2. Restrictions for structs that contain arrays or samplers are respected.
5034 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005035 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005036 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005037 case EOpAssign:
5038 case EOpInitialize:
5039 case EOpEqual:
5040 case EOpNotEqual:
5041 // ESSL 1.00 sections 5.7, 5.8, 5.9
5042 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5043 {
5044 error(loc, "undefined operation for structs containing arrays",
5045 GetOperatorString(op));
5046 return false;
5047 }
5048 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5049 // we interpret the spec so that this extends to structs containing samplers,
5050 // similarly to ESSL 1.00 spec.
5051 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5052 left->getType().isStructureContainingSamplers())
5053 {
5054 error(loc, "undefined operation for structs containing samplers",
5055 GetOperatorString(op));
5056 return false;
5057 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005058
Olli Etuahoe1805592017-01-02 16:41:20 +00005059 if ((left->getNominalSize() != right->getNominalSize()) ||
5060 (left->getSecondarySize() != right->getSecondarySize()))
5061 {
5062 error(loc, "dimension mismatch", GetOperatorString(op));
5063 return false;
5064 }
5065 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005066 case EOpLessThan:
5067 case EOpGreaterThan:
5068 case EOpLessThanEqual:
5069 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005070 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005071 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005072 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005073 return false;
5074 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005075 break;
5076 case EOpAdd:
5077 case EOpSub:
5078 case EOpDiv:
5079 case EOpIMod:
5080 case EOpBitShiftLeft:
5081 case EOpBitShiftRight:
5082 case EOpBitwiseAnd:
5083 case EOpBitwiseXor:
5084 case EOpBitwiseOr:
5085 case EOpAddAssign:
5086 case EOpSubAssign:
5087 case EOpDivAssign:
5088 case EOpIModAssign:
5089 case EOpBitShiftLeftAssign:
5090 case EOpBitShiftRightAssign:
5091 case EOpBitwiseAndAssign:
5092 case EOpBitwiseXorAssign:
5093 case EOpBitwiseOrAssign:
5094 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5095 {
5096 return false;
5097 }
5098
5099 // Are the sizes compatible?
5100 if (left->getNominalSize() != right->getNominalSize() ||
5101 left->getSecondarySize() != right->getSecondarySize())
5102 {
5103 // If the nominal sizes of operands do not match:
5104 // One of them must be a scalar.
5105 if (!left->isScalar() && !right->isScalar())
5106 return false;
5107
5108 // In the case of compound assignment other than multiply-assign,
5109 // the right side needs to be a scalar. Otherwise a vector/matrix
5110 // would be assigned to a scalar. A scalar can't be shifted by a
5111 // vector either.
5112 if (!right->isScalar() &&
5113 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5114 return false;
5115 }
5116 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005117 default:
5118 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005119 }
5120
Olli Etuahod6b14282015-03-17 14:31:35 +02005121 return true;
5122}
5123
Olli Etuaho1dded802016-08-18 18:13:13 +03005124bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5125 const TType &left,
5126 const TType &right)
5127{
5128 switch (op)
5129 {
5130 case EOpMul:
5131 case EOpMulAssign:
5132 return left.getNominalSize() == right.getNominalSize() &&
5133 left.getSecondarySize() == right.getSecondarySize();
5134 case EOpVectorTimesScalar:
5135 return true;
5136 case EOpVectorTimesScalarAssign:
5137 ASSERT(!left.isMatrix() && !right.isMatrix());
5138 return left.isVector() && !right.isVector();
5139 case EOpVectorTimesMatrix:
5140 return left.getNominalSize() == right.getRows();
5141 case EOpVectorTimesMatrixAssign:
5142 ASSERT(!left.isMatrix() && right.isMatrix());
5143 return left.isVector() && left.getNominalSize() == right.getRows() &&
5144 left.getNominalSize() == right.getCols();
5145 case EOpMatrixTimesVector:
5146 return left.getCols() == right.getNominalSize();
5147 case EOpMatrixTimesScalar:
5148 return true;
5149 case EOpMatrixTimesScalarAssign:
5150 ASSERT(left.isMatrix() && !right.isMatrix());
5151 return !right.isVector();
5152 case EOpMatrixTimesMatrix:
5153 return left.getCols() == right.getRows();
5154 case EOpMatrixTimesMatrixAssign:
5155 ASSERT(left.isMatrix() && right.isMatrix());
5156 // We need to check two things:
5157 // 1. The matrix multiplication step is valid.
5158 // 2. The result will have the same number of columns as the lvalue.
5159 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5160
5161 default:
5162 UNREACHABLE();
5163 return false;
5164 }
5165}
5166
Jamie Madillb98c3a82015-07-23 14:26:04 -04005167TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5168 TIntermTyped *left,
5169 TIntermTyped *right,
5170 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005171{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005172 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005173 return nullptr;
5174
Olli Etuahofc1806e2015-03-17 13:03:11 +02005175 switch (op)
5176 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005177 case EOpEqual:
5178 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005179 case EOpLessThan:
5180 case EOpGreaterThan:
5181 case EOpLessThanEqual:
5182 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005183 break;
5184 case EOpLogicalOr:
5185 case EOpLogicalXor:
5186 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005187 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5188 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005189 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005190 {
5191 return nullptr;
5192 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005193 // Basic types matching should have been already checked.
5194 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005195 break;
5196 case EOpAdd:
5197 case EOpSub:
5198 case EOpDiv:
5199 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005200 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5201 !right->getType().getStruct());
5202 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005203 {
5204 return nullptr;
5205 }
5206 break;
5207 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005208 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5209 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005210 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005211 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005212 {
5213 return nullptr;
5214 }
5215 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005216 default:
5217 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005218 }
5219
Olli Etuaho1dded802016-08-18 18:13:13 +03005220 if (op == EOpMul)
5221 {
5222 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5223 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5224 {
5225 return nullptr;
5226 }
5227 }
5228
Olli Etuaho3fdec912016-08-18 15:08:06 +03005229 TIntermBinary *node = new TIntermBinary(op, left, right);
5230 node->setLine(loc);
5231
Olli Etuaho3fdec912016-08-18 15:08:06 +03005232 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005233 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005234}
5235
Jamie Madillb98c3a82015-07-23 14:26:04 -04005236TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5237 TIntermTyped *left,
5238 TIntermTyped *right,
5239 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005240{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005241 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005242 if (node == 0)
5243 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005244 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5245 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005246 return left;
5247 }
5248 return node;
5249}
5250
Jamie Madillb98c3a82015-07-23 14:26:04 -04005251TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5252 TIntermTyped *left,
5253 TIntermTyped *right,
5254 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005255{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005256 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005257 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005258 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005259 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5260 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005261 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005262 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005263 }
5264 return node;
5265}
5266
Olli Etuaho13389b62016-10-16 11:48:18 +01005267TIntermBinary *TParseContext::createAssign(TOperator op,
5268 TIntermTyped *left,
5269 TIntermTyped *right,
5270 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005271{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005272 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005273 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005274 if (op == EOpMulAssign)
5275 {
5276 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5277 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5278 {
5279 return nullptr;
5280 }
5281 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005282 TIntermBinary *node = new TIntermBinary(op, left, right);
5283 node->setLine(loc);
5284
Olli Etuaho3fdec912016-08-18 15:08:06 +03005285 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005286 }
5287 return nullptr;
5288}
5289
Jamie Madillb98c3a82015-07-23 14:26:04 -04005290TIntermTyped *TParseContext::addAssign(TOperator op,
5291 TIntermTyped *left,
5292 TIntermTyped *right,
5293 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005294{
Olli Etuahocce89652017-06-19 16:04:09 +03005295 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005296 TIntermTyped *node = createAssign(op, left, right, loc);
5297 if (node == nullptr)
5298 {
5299 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005300 return left;
5301 }
5302 return node;
5303}
5304
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005305TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5306 TIntermTyped *right,
5307 const TSourceLoc &loc)
5308{
Corentin Wallez0d959252016-07-12 17:26:32 -04005309 // WebGL2 section 5.26, the following results in an error:
5310 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005311 if (mShaderSpec == SH_WEBGL2_SPEC &&
5312 (left->isArray() || left->getBasicType() == EbtVoid ||
5313 left->getType().isStructureContainingArrays() || right->isArray() ||
5314 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005315 {
5316 error(loc,
5317 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5318 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005319 }
5320
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005321 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5322 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5323 commaNode->getTypePointer()->setQualifier(resultQualifier);
5324 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005325}
5326
Olli Etuaho49300862015-02-20 14:54:49 +02005327TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5328{
5329 switch (op)
5330 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005331 case EOpContinue:
5332 if (mLoopNestingLevel <= 0)
5333 {
5334 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005335 }
5336 break;
5337 case EOpBreak:
5338 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5339 {
5340 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005341 }
5342 break;
5343 case EOpReturn:
5344 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5345 {
5346 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005347 }
5348 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005349 case EOpKill:
5350 if (mShaderType != GL_FRAGMENT_SHADER)
5351 {
5352 error(loc, "discard supported in fragment shaders only", "discard");
5353 }
5354 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005355 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005356 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005357 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005358 }
Olli Etuahocce89652017-06-19 16:04:09 +03005359 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005360}
5361
Jamie Madillb98c3a82015-07-23 14:26:04 -04005362TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005363 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005364 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005365{
Olli Etuahocce89652017-06-19 16:04:09 +03005366 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005367 {
Olli Etuahocce89652017-06-19 16:04:09 +03005368 ASSERT(op == EOpReturn);
5369 mFunctionReturnsValue = true;
5370 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5371 {
5372 error(loc, "void function cannot return a value", "return");
5373 }
5374 else if (*mCurrentFunctionType != expression->getType())
5375 {
5376 error(loc, "function return is not matching type:", "return");
5377 }
Olli Etuaho49300862015-02-20 14:54:49 +02005378 }
Olli Etuahocce89652017-06-19 16:04:09 +03005379 TIntermBranch *node = new TIntermBranch(op, expression);
5380 node->setLine(loc);
5381 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005382}
5383
Martin Radev84aa2dc2017-09-11 15:51:02 +03005384void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5385{
5386 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
5387 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5388 bool isTextureGather = (name == "textureGather");
5389 bool isTextureGatherOffset = (name == "textureGatherOffset");
5390 if (isTextureGather || isTextureGatherOffset)
5391 {
5392 TIntermNode *componentNode = nullptr;
5393 TIntermSequence *arguments = functionCall->getSequence();
5394 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5395 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5396 ASSERT(sampler != nullptr);
5397 switch (sampler->getBasicType())
5398 {
5399 case EbtSampler2D:
5400 case EbtISampler2D:
5401 case EbtUSampler2D:
5402 case EbtSampler2DArray:
5403 case EbtISampler2DArray:
5404 case EbtUSampler2DArray:
5405 if ((isTextureGather && arguments->size() == 3u) ||
5406 (isTextureGatherOffset && arguments->size() == 4u))
5407 {
5408 componentNode = arguments->back();
5409 }
5410 break;
5411 case EbtSamplerCube:
5412 case EbtISamplerCube:
5413 case EbtUSamplerCube:
5414 ASSERT(!isTextureGatherOffset);
5415 if (arguments->size() == 3u)
5416 {
5417 componentNode = arguments->back();
5418 }
5419 break;
5420 case EbtSampler2DShadow:
5421 case EbtSampler2DArrayShadow:
5422 case EbtSamplerCubeShadow:
5423 break;
5424 default:
5425 UNREACHABLE();
5426 break;
5427 }
5428 if (componentNode)
5429 {
5430 const TIntermConstantUnion *componentConstantUnion =
5431 componentNode->getAsConstantUnion();
5432 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5433 {
5434 error(functionCall->getLine(), "Texture component must be a constant expression",
5435 name.c_str());
5436 }
5437 else
5438 {
5439 int component = componentConstantUnion->getIConst(0);
5440 if (component < 0 || component > 3)
5441 {
5442 error(functionCall->getLine(), "Component must be in the range [0;3]",
5443 name.c_str());
5444 }
5445 }
5446 }
5447 }
5448}
5449
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005450void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5451{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005452 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01005453 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005454 TIntermNode *offset = nullptr;
5455 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005456 bool useTextureGatherOffsetConstraints = false;
Olli Etuahoec9232b2017-03-27 17:01:37 +03005457 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5458 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5459 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005460 {
5461 offset = arguments->back();
5462 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005463 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005464 {
5465 // A bias parameter might follow the offset parameter.
5466 ASSERT(arguments->size() >= 3);
5467 offset = (*arguments)[2];
5468 }
Martin Radev84aa2dc2017-09-11 15:51:02 +03005469 else if (name == "textureGatherOffset")
5470 {
5471 ASSERT(arguments->size() >= 3u);
5472 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5473 ASSERT(sampler != nullptr);
5474 switch (sampler->getBasicType())
5475 {
5476 case EbtSampler2D:
5477 case EbtISampler2D:
5478 case EbtUSampler2D:
5479 case EbtSampler2DArray:
5480 case EbtISampler2DArray:
5481 case EbtUSampler2DArray:
5482 offset = (*arguments)[2];
5483 break;
5484 case EbtSampler2DShadow:
5485 case EbtSampler2DArrayShadow:
5486 offset = (*arguments)[3];
5487 break;
5488 default:
5489 UNREACHABLE();
5490 break;
5491 }
5492 useTextureGatherOffsetConstraints = true;
5493 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005494 if (offset != nullptr)
5495 {
5496 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5497 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5498 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005499 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005500 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005501 }
5502 else
5503 {
5504 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5505 size_t size = offsetConstantUnion->getType().getObjectSize();
5506 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005507 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5508 : mMinProgramTexelOffset;
5509 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5510 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005511 for (size_t i = 0u; i < size; ++i)
5512 {
5513 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005514 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005515 {
5516 std::stringstream tokenStream;
5517 tokenStream << offsetValue;
5518 std::string token = tokenStream.str();
5519 error(offset->getLine(), "Texture offset value out of valid range",
5520 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005521 }
5522 }
5523 }
5524 }
5525}
5526
Martin Radev2cc85b32016-08-05 16:22:53 +03005527// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5528void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5529{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005530 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005531 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5532
5533 if (name.compare(0, 5, "image") == 0)
5534 {
5535 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005536 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005537
Olli Etuaho485eefd2017-02-14 17:40:06 +00005538 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005539
5540 if (name.compare(5, 5, "Store") == 0)
5541 {
5542 if (memoryQualifier.readonly)
5543 {
5544 error(imageNode->getLine(),
5545 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005546 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005547 }
5548 }
5549 else if (name.compare(5, 4, "Load") == 0)
5550 {
5551 if (memoryQualifier.writeonly)
5552 {
5553 error(imageNode->getLine(),
5554 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005555 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005556 }
5557 }
5558 }
5559}
5560
5561// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5562void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5563 const TFunction *functionDefinition,
5564 const TIntermAggregate *functionCall)
5565{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005566 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005567
5568 const TIntermSequence &arguments = *functionCall->getSequence();
5569
5570 ASSERT(functionDefinition->getParamCount() == arguments.size());
5571
5572 for (size_t i = 0; i < arguments.size(); ++i)
5573 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005574 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5575 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005576 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5577 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5578
5579 if (IsImage(functionArgumentType.getBasicType()))
5580 {
5581 const TMemoryQualifier &functionArgumentMemoryQualifier =
5582 functionArgumentType.getMemoryQualifier();
5583 const TMemoryQualifier &functionParameterMemoryQualifier =
5584 functionParameterType.getMemoryQualifier();
5585 if (functionArgumentMemoryQualifier.readonly &&
5586 !functionParameterMemoryQualifier.readonly)
5587 {
5588 error(functionCall->getLine(),
5589 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005590 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005591 }
5592
5593 if (functionArgumentMemoryQualifier.writeonly &&
5594 !functionParameterMemoryQualifier.writeonly)
5595 {
5596 error(functionCall->getLine(),
5597 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005598 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005599 }
Martin Radev049edfa2016-11-11 14:35:37 +02005600
5601 if (functionArgumentMemoryQualifier.coherent &&
5602 !functionParameterMemoryQualifier.coherent)
5603 {
5604 error(functionCall->getLine(),
5605 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005606 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005607 }
5608
5609 if (functionArgumentMemoryQualifier.volatileQualifier &&
5610 !functionParameterMemoryQualifier.volatileQualifier)
5611 {
5612 error(functionCall->getLine(),
5613 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005614 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005615 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005616 }
5617 }
5618}
5619
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005620TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005621{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005622 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005623}
5624
5625TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005626 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005627 TIntermNode *thisNode,
5628 const TSourceLoc &loc)
5629{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005630 if (thisNode != nullptr)
5631 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005632 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005633 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005634
5635 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005636 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005637 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005638 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005639 }
5640 else
5641 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005642 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005643 return addNonConstructorFunctionCall(fnCall, arguments, loc);
5644 }
5645}
5646
5647TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
5648 TIntermSequence *arguments,
5649 TIntermNode *thisNode,
5650 const TSourceLoc &loc)
5651{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005652 TIntermTyped *typedThis = thisNode->getAsTyped();
5653 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5654 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5655 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
5656 // So accessing fnCall->getName() below is safe.
5657 if (fnCall->getName() != "length")
5658 {
5659 error(loc, "invalid method", fnCall->getName().c_str());
5660 }
5661 else if (!arguments->empty())
5662 {
5663 error(loc, "method takes no parameters", "length");
5664 }
5665 else if (typedThis == nullptr || !typedThis->isArray())
5666 {
5667 error(loc, "length can only be called on arrays", "length");
5668 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005669 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5670 mGeometryShaderInputPrimitiveType == EptUndefined)
5671 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08005672 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005673 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5674 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005675 else
5676 {
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005677 TIntermUnary *node = new TIntermUnary(EOpArrayLength, typedThis);
5678 node->setLine(loc);
5679 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005680 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005681 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005682}
5683
5684TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
5685 TIntermSequence *arguments,
5686 const TSourceLoc &loc)
5687{
5688 // First find by unmangled name to check whether the function name has been
5689 // hidden by a variable name or struct typename.
5690 // If a function is found, check for one with a matching argument list.
5691 bool builtIn;
5692 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
5693 if (symbol != nullptr && !symbol->isFunction())
5694 {
5695 error(loc, "function name expected", fnCall->getName().c_str());
5696 }
5697 else
5698 {
5699 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
5700 mShaderVersion, &builtIn);
5701 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005702 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005703 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
5704 }
5705 else
5706 {
5707 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005708 //
5709 // A declared function.
5710 //
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03005711 if (builtIn && fnCandidate->getExtension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005712 {
Olli Etuaho856c4972016-08-08 11:38:39 +03005713 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005714 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005715 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005716 if (builtIn && op != EOpNull)
5717 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005718 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005719 if (fnCandidate->getParamCount() == 1)
5720 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005721 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005722 TIntermNode *unaryParamNode = arguments->front();
5723 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005724 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005725 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005726 }
5727 else
5728 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005729 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005730 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005731 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005732
5733 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005734 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305735
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005736 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305737 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005738 // See if we can constant fold a built-in. Note that this may be possible
5739 // even if it is not const-qualified.
5740 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305741 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005742 else
5743 {
5744 return callNode;
5745 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005746 }
5747 }
5748 else
5749 {
5750 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005751 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005752
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005753 // If builtIn == false, the function is user defined - could be an overloaded
5754 // built-in as well.
5755 // if builtIn == true, it's a builtIn function with no op associated with it.
5756 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005757 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005758 {
Olli Etuahofe486322017-03-21 09:30:54 +00005759 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005760 checkTextureOffsetConst(callNode);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005761 checkTextureGather(callNode);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005762 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005763 }
5764 else
5765 {
Olli Etuahofe486322017-03-21 09:30:54 +00005766 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005767 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005768 }
5769
Jiajia Qinbc585152017-06-23 15:42:17 +08005770 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005771
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005772 callNode->setLine(loc);
5773
5774 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005775 }
5776 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005777 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005778
5779 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005780 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005781}
5782
Jamie Madillb98c3a82015-07-23 14:26:04 -04005783TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005784 TIntermTyped *trueExpression,
5785 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005786 const TSourceLoc &loc)
5787{
Olli Etuaho56229f12017-07-10 14:16:33 +03005788 if (!checkIsScalarBool(loc, cond))
5789 {
5790 return falseExpression;
5791 }
Olli Etuaho52901742015-04-15 13:42:45 +03005792
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005793 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005794 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005795 std::stringstream reasonStream;
5796 reasonStream << "mismatching ternary operator operand types '"
5797 << trueExpression->getCompleteString() << " and '"
5798 << falseExpression->getCompleteString() << "'";
5799 std::string reason = reasonStream.str();
5800 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005801 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005802 }
Olli Etuahode318b22016-10-25 16:18:25 +01005803 if (IsOpaqueType(trueExpression->getBasicType()))
5804 {
5805 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005806 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005807 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5808 // Note that structs containing opaque types don't need to be checked as structs are
5809 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005810 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005811 return falseExpression;
5812 }
5813
Jiajia Qinbc585152017-06-23 15:42:17 +08005814 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5815 falseExpression->getMemoryQualifier().writeonly)
5816 {
5817 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5818 return falseExpression;
5819 }
5820
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005821 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005822 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005823 // ESSL 3.00.6 section 5.7:
5824 // Ternary operator support is optional for arrays. No certainty that it works across all
5825 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5826 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005827 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005828 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005829 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005830 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005831 }
Olli Etuaho94050052017-05-08 14:17:44 +03005832 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5833 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005834 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005835 return falseExpression;
5836 }
5837
Corentin Wallez0d959252016-07-12 17:26:32 -04005838 // WebGL2 section 5.26, the following results in an error:
5839 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005840 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005841 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005842 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005843 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005844 }
5845
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005846 // Note that the node resulting from here can be a constant union without being qualified as
5847 // constant.
5848 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5849 node->setLine(loc);
5850
5851 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005852}
Olli Etuaho49300862015-02-20 14:54:49 +02005853
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005854//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005855// Parse an array of strings using yyparse.
5856//
5857// Returns 0 for success.
5858//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005859int PaParseStrings(size_t count,
5860 const char *const string[],
5861 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305862 TParseContext *context)
5863{
Yunchao He4f285442017-04-21 12:15:49 +08005864 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005865 return 1;
5866
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005867 if (glslang_initialize(context))
5868 return 1;
5869
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005870 int error = glslang_scan(count, string, length, context);
5871 if (!error)
5872 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005873
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005874 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005875
alokp@chromium.org6b495712012-06-29 00:06:58 +00005876 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005877}
Jamie Madill45bcc782016-11-07 13:58:48 -05005878
5879} // namespace sh