blob: 15a9cd6257ea17057a910725f532d259c150b6f7 [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
jchen104cdac9e2017-05-08 11:01:20 +08002704void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2705 const TSourceLoc &location)
2706{
2707 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2708 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2709 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2710 {
2711 error(location, "Requires both binding and offset", "layout");
2712 return;
2713 }
2714 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2715}
2716
Olli Etuahocce89652017-06-19 16:04:09 +03002717void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2718 const TPublicType &type,
2719 const TSourceLoc &loc)
2720{
2721 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2722 !getFragmentPrecisionHigh())
2723 {
2724 error(loc, "precision is not supported in fragment shader", "highp");
2725 }
2726
2727 if (!CanSetDefaultPrecisionOnType(type))
2728 {
2729 error(loc, "illegal type argument for default precision qualifier",
2730 getBasicString(type.getBasicType()));
2731 return;
2732 }
2733 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2734}
2735
Shaob5cc1192017-07-06 10:47:20 +08002736bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2737{
2738 switch (typeQualifier.layoutQualifier.primitiveType)
2739 {
2740 case EptLines:
2741 case EptLinesAdjacency:
2742 case EptTriangles:
2743 case EptTrianglesAdjacency:
2744 return typeQualifier.qualifier == EvqGeometryIn;
2745
2746 case EptLineStrip:
2747 case EptTriangleStrip:
2748 return typeQualifier.qualifier == EvqGeometryOut;
2749
2750 case EptPoints:
2751 return true;
2752
2753 default:
2754 UNREACHABLE();
2755 return false;
2756 }
2757}
2758
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002759void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2760 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002761{
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002762 if (mGeometryShaderInputArraySize == 0u)
2763 {
2764 mGeometryShaderInputArraySize = inputArraySize;
2765 }
2766 else if (mGeometryShaderInputArraySize != inputArraySize)
2767 {
2768 error(line,
2769 "Array size or input primitive declaration doesn't match the size of earlier sized "
2770 "array inputs.",
2771 "layout");
2772 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002773}
2774
Shaob5cc1192017-07-06 10:47:20 +08002775bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2776{
2777 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2778
2779 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2780
2781 if (layoutQualifier.maxVertices != -1)
2782 {
2783 error(typeQualifier.line,
2784 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2785 return false;
2786 }
2787
2788 // Set mGeometryInputPrimitiveType if exists
2789 if (layoutQualifier.primitiveType != EptUndefined)
2790 {
2791 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2792 {
2793 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2794 return false;
2795 }
2796
2797 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2798 {
2799 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002800 setGeometryShaderInputArraySize(
2801 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2802 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002803 }
2804 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2805 {
2806 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2807 "layout");
2808 return false;
2809 }
2810 }
2811
2812 // Set mGeometryInvocations if exists
2813 if (layoutQualifier.invocations > 0)
2814 {
2815 if (mGeometryShaderInvocations == 0)
2816 {
2817 mGeometryShaderInvocations = layoutQualifier.invocations;
2818 }
2819 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2820 {
2821 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2822 "layout");
2823 return false;
2824 }
2825 }
2826
2827 return true;
2828}
2829
2830bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2831{
2832 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2833
2834 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2835
2836 if (layoutQualifier.invocations > 0)
2837 {
2838 error(typeQualifier.line,
2839 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2840 return false;
2841 }
2842
2843 // Set mGeometryOutputPrimitiveType if exists
2844 if (layoutQualifier.primitiveType != EptUndefined)
2845 {
2846 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2847 {
2848 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2849 return false;
2850 }
2851
2852 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2853 {
2854 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2855 }
2856 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2857 {
2858 error(typeQualifier.line,
2859 "primitive doesn't match earlier output primitive declaration", "layout");
2860 return false;
2861 }
2862 }
2863
2864 // Set mGeometryMaxVertices if exists
2865 if (layoutQualifier.maxVertices > -1)
2866 {
2867 if (mGeometryShaderMaxVertices == -1)
2868 {
2869 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2870 }
2871 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2872 {
2873 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2874 "layout");
2875 return false;
2876 }
2877 }
2878
2879 return true;
2880}
2881
Martin Radev70866b82016-07-22 15:27:42 +03002882void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002883{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002884 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002885 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002886
Martin Radev70866b82016-07-22 15:27:42 +03002887 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2888 typeQualifier.line);
2889
Jamie Madillc2128ff2016-07-04 10:26:17 -04002890 // It should never be the case, but some strange parser errors can send us here.
2891 if (layoutQualifier.isEmpty())
2892 {
2893 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002894 return;
2895 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002896
Martin Radev802abe02016-08-04 17:48:32 +03002897 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002898 {
Olli Etuaho43364892017-02-13 16:00:12 +00002899 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002900 return;
2901 }
2902
Olli Etuaho43364892017-02-13 16:00:12 +00002903 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2904
2905 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002906
2907 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2908
Andrei Volykhina5527072017-03-22 16:46:30 +03002909 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2910
jchen104cdac9e2017-05-08 11:01:20 +08002911 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2912
Qin Jiajiaca68d982017-09-18 16:41:56 +08002913 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
2914 typeQualifier.qualifier);
2915
Martin Radev802abe02016-08-04 17:48:32 +03002916 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002917 {
Martin Radev802abe02016-08-04 17:48:32 +03002918 if (mComputeShaderLocalSizeDeclared &&
2919 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2920 {
2921 error(typeQualifier.line, "Work group size does not match the previous declaration",
2922 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002923 return;
2924 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002925
Martin Radev802abe02016-08-04 17:48:32 +03002926 if (mShaderVersion < 310)
2927 {
2928 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002929 return;
2930 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002931
Martin Radev4c4c8e72016-08-04 12:25:34 +03002932 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002933 {
2934 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002935 return;
2936 }
2937
2938 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2939 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2940
2941 const TConstantUnion *maxComputeWorkGroupSizeData =
2942 maxComputeWorkGroupSize->getConstPointer();
2943
2944 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2945 {
2946 if (layoutQualifier.localSize[i] != -1)
2947 {
2948 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2949 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2950 if (mComputeShaderLocalSize[i] < 1 ||
2951 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2952 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002953 std::stringstream reasonStream;
2954 reasonStream << "invalid value: Value must be at least 1 and no greater than "
2955 << maxComputeWorkGroupSizeValue;
2956 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03002957
Olli Etuaho4de340a2016-12-16 09:32:03 +00002958 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03002959 return;
2960 }
2961 }
2962 }
2963
2964 mComputeShaderLocalSizeDeclared = true;
2965 }
Shaob5cc1192017-07-06 10:47:20 +08002966 else if (typeQualifier.qualifier == EvqGeometryIn)
2967 {
2968 if (mShaderVersion < 310)
2969 {
2970 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
2971 return;
2972 }
2973
2974 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
2975 {
2976 return;
2977 }
2978 }
2979 else if (typeQualifier.qualifier == EvqGeometryOut)
2980 {
2981 if (mShaderVersion < 310)
2982 {
2983 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
2984 "layout");
2985 return;
2986 }
2987
2988 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
2989 {
2990 return;
2991 }
2992 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03002993 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
2994 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00002995 {
2996 // This error is only specified in WebGL, but tightens unspecified behavior in the native
2997 // specification.
2998 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
2999 {
3000 error(typeQualifier.line, "Number of views does not match the previous declaration",
3001 "layout");
3002 return;
3003 }
3004
3005 if (layoutQualifier.numViews == -1)
3006 {
3007 error(typeQualifier.line, "No num_views specified", "layout");
3008 return;
3009 }
3010
3011 if (layoutQualifier.numViews > mMaxNumViews)
3012 {
3013 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3014 "layout");
3015 return;
3016 }
3017
3018 mNumViews = layoutQualifier.numViews;
3019 }
Martin Radev802abe02016-08-04 17:48:32 +03003020 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003021 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003022 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003023 {
Martin Radev802abe02016-08-04 17:48:32 +03003024 return;
3025 }
3026
Jiajia Qinbc585152017-06-23 15:42:17 +08003027 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003028 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003029 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003030 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003031 return;
3032 }
3033
3034 if (mShaderVersion < 300)
3035 {
3036 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3037 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003038 return;
3039 }
3040
Olli Etuaho09b04a22016-12-15 13:30:26 +00003041 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003042
3043 if (layoutQualifier.matrixPacking != EmpUnspecified)
3044 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003045 if (typeQualifier.qualifier == EvqUniform)
3046 {
3047 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3048 }
3049 else if (typeQualifier.qualifier == EvqBuffer)
3050 {
3051 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3052 }
Martin Radev802abe02016-08-04 17:48:32 +03003053 }
3054
3055 if (layoutQualifier.blockStorage != EbsUnspecified)
3056 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003057 if (typeQualifier.qualifier == EvqUniform)
3058 {
3059 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3060 }
3061 else if (typeQualifier.qualifier == EvqBuffer)
3062 {
3063 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3064 }
Martin Radev802abe02016-08-04 17:48:32 +03003065 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003066 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003067}
3068
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003069TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3070 const TFunction &function,
3071 const TSourceLoc &location,
3072 bool insertParametersToSymbolTable)
3073{
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003074 checkIsNotReserved(location, function.getName());
3075
Olli Etuahofe486322017-03-21 09:30:54 +00003076 TIntermFunctionPrototype *prototype =
3077 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003078 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
3079 // point to the data that already exists in the symbol table.
3080 prototype->getFunctionSymbolInfo()->setFromFunction(function);
3081 prototype->setLine(location);
3082
3083 for (size_t i = 0; i < function.getParamCount(); i++)
3084 {
3085 const TConstParameter &param = function.getParam(i);
3086
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003087 TIntermSymbol *symbol = nullptr;
3088
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003089 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3090 // be used for unused args).
3091 if (param.name != nullptr)
3092 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003093 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003094 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003095 {
Olli Etuaho0f684632017-07-13 12:42:15 +03003096 TVariable *variable = symbolTable.declareVariable(param.name, *param.type);
3097 if (variable)
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003098 {
3099 symbol = new TIntermSymbol(variable->getUniqueId(), variable->getName(),
3100 variable->getType());
3101 }
3102 else
3103 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003104 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003105 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003106 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003107 // Unsized type of a named parameter should have already been checked and sanitized.
3108 ASSERT(!param.type->isUnsizedArray());
3109 }
3110 else
3111 {
3112 if (param.type->isUnsizedArray())
3113 {
3114 error(location, "function parameter array must be sized at compile time", "[]");
3115 // We don't need to size the arrays since the parameter is unnamed and hence
3116 // inaccessible.
3117 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003118 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003119 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003120 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003121 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3122 // symbol.
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003123 symbol = new TIntermSymbol(symbolTable.getEmptySymbolId(), "", *param.type);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003124 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003125 symbol->setLine(location);
3126 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003127 }
3128 return prototype;
3129}
3130
Olli Etuaho16c745a2017-01-16 17:02:27 +00003131TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3132 const TFunction &parsedFunction,
3133 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003134{
Olli Etuaho476197f2016-10-11 13:59:08 +01003135 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3136 // first declaration. Either way the instance in the symbol table is used to track whether the
3137 // function is declared multiple times.
3138 TFunction *function = static_cast<TFunction *>(
3139 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3140 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003141 {
3142 // ESSL 1.00.17 section 4.2.7.
3143 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3144 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003145 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003146 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003147
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003148 TIntermFunctionPrototype *prototype =
3149 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003150
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003151 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003152
3153 if (!symbolTable.atGlobalLevel())
3154 {
3155 // ESSL 3.00.4 section 4.2.4.
3156 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003157 }
3158
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003159 return prototype;
3160}
3161
Olli Etuaho336b1472016-10-05 16:37:55 +01003162TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003163 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003164 TIntermBlock *functionBody,
3165 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003166{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003167 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003168 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3169 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003170 error(location, "function does not return a value:",
3171 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003172 }
3173
Olli Etuahof51fdd22016-10-03 10:03:40 +01003174 if (functionBody == nullptr)
3175 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003176 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003177 functionBody->setLine(location);
3178 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003179 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003180 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003181 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003182
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003183 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003184 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003185}
3186
Olli Etuaho476197f2016-10-11 13:59:08 +01003187void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3188 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003189 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003190{
Olli Etuaho476197f2016-10-11 13:59:08 +01003191 ASSERT(function);
3192 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003193 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003194 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003195
3196 if (builtIn)
3197 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003198 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003199 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003200 else
Jamie Madill185fb402015-06-12 15:48:48 -04003201 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003202 TFunction *prevDec = static_cast<TFunction *>(
3203 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3204
3205 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3206 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3207 // occurance.
3208 if (*function != prevDec)
3209 {
3210 // Swap the parameters of the previous declaration to the parameters of the function
3211 // definition (parameter names may differ).
3212 prevDec->swapParameters(**function);
3213
3214 // The function definition will share the same symbol as any previous declaration.
3215 *function = prevDec;
3216 }
3217
3218 if ((*function)->isDefined())
3219 {
3220 error(location, "function already has a body", (*function)->getName().c_str());
3221 }
3222
3223 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003224 }
Jamie Madill185fb402015-06-12 15:48:48 -04003225
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003226 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003227 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003228 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003229
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003230 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003231 setLoopNestingLevel(0);
3232}
3233
Jamie Madillb98c3a82015-07-23 14:26:04 -04003234TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003235{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003236 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003237 // We don't know at this point whether this is a function definition or a prototype.
3238 // The definition production code will check for redefinitions.
3239 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003240 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003241 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3242 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003243 //
3244 TFunction *prevDec =
3245 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303246
Martin Radevda6254b2016-12-14 17:00:36 +02003247 if (getShaderVersion() >= 300 &&
3248 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
3249 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303250 {
Martin Radevda6254b2016-12-14 17:00:36 +02003251 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303252 // Therefore overloading or redefining builtin functions is an error.
3253 error(location, "Name of a built-in function cannot be redeclared as function",
3254 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303255 }
3256 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003257 {
3258 if (prevDec->getReturnType() != function->getReturnType())
3259 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003260 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003261 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003262 }
3263 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3264 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003265 if (prevDec->getParam(i).type->getQualifier() !=
3266 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003267 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003268 error(location,
3269 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003270 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003271 }
3272 }
3273 }
3274
3275 //
3276 // Check for previously declared variables using the same name.
3277 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003278 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003279 if (prevSym)
3280 {
3281 if (!prevSym->isFunction())
3282 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003283 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003284 }
3285 }
3286 else
3287 {
3288 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003289 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003290 }
3291
3292 // We're at the inner scope level of the function's arguments and body statement.
3293 // Add the function prototype to the surrounding scope instead.
3294 symbolTable.getOuterLevel()->insert(function);
3295
Olli Etuaho78d13742017-01-18 13:06:10 +00003296 // Raise error message if main function takes any parameters or return anything other than void
3297 if (function->getName() == "main")
3298 {
3299 if (function->getParamCount() > 0)
3300 {
3301 error(location, "function cannot take any parameter(s)", "main");
3302 }
3303 if (function->getReturnType().getBasicType() != EbtVoid)
3304 {
3305 error(location, "main function cannot return a value",
3306 function->getReturnType().getBasicString());
3307 }
3308 }
3309
Jamie Madill185fb402015-06-12 15:48:48 -04003310 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003311 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3312 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003313 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3314 //
3315 return function;
3316}
3317
Olli Etuaho9de84a52016-06-14 17:36:01 +03003318TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3319 const TString *name,
3320 const TSourceLoc &location)
3321{
3322 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3323 {
3324 error(location, "no qualifiers allowed for function return",
3325 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003326 }
3327 if (!type.layoutQualifier.isEmpty())
3328 {
3329 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003330 }
jchen10cc2a10e2017-05-03 14:05:12 +08003331 // make sure an opaque type is not involved as well...
3332 std::string reason(getBasicString(type.getBasicType()));
3333 reason += "s can't be function return values";
3334 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003335 if (mShaderVersion < 300)
3336 {
3337 // Array return values are forbidden, but there's also no valid syntax for declaring array
3338 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00003339 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003340
3341 if (type.isStructureContainingArrays())
3342 {
3343 // ESSL 1.00.17 section 6.1 Function Definitions
3344 error(location, "structures containing arrays can't be function return values",
3345 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003346 }
3347 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003348
3349 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003350 return new TFunction(&symbolTable, name, new TType(type));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003351}
3352
Olli Etuahocce89652017-06-19 16:04:09 +03003353TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3354{
Olli Etuahocce89652017-06-19 16:04:09 +03003355 const TType *returnType = TCache::getType(EbtVoid, EbpUndefined);
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003356 return new TFunction(&symbolTable, name, returnType);
Olli Etuahocce89652017-06-19 16:04:09 +03003357}
3358
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003359TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003360{
Olli Etuahocce89652017-06-19 16:04:09 +03003361 if (mShaderVersion < 300 && publicType.array)
3362 {
3363 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3364 "[]");
3365 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003366 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003367 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003368 error(publicType.getLine(), "constructor can't be a structure definition",
3369 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003370 }
3371
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003372 TType *type = new TType(publicType);
3373 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003374 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003375 error(publicType.getLine(), "cannot construct this type",
3376 getBasicString(publicType.getBasicType()));
3377 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003378 }
3379
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003380 return new TFunction(&symbolTable, nullptr, type, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003381}
3382
Olli Etuaho55bde912017-10-25 13:41:13 +03003383void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3384 const char *errorMessage,
3385 const char *token,
3386 TType *arrayType)
3387{
3388 if (arrayType->isUnsizedArray())
3389 {
3390 error(line, errorMessage, token);
3391 arrayType->sizeUnsizedArrays(TVector<unsigned int>());
3392 }
3393}
3394
3395TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahocce89652017-06-19 16:04:09 +03003396 const TString *name,
3397 const TSourceLoc &nameLoc)
3398{
Olli Etuaho55bde912017-10-25 13:41:13 +03003399 ASSERT(type);
3400 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name->c_str(),
3401 type);
3402 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003403 {
3404 error(nameLoc, "illegal use of type 'void'", name->c_str());
3405 }
3406 checkIsNotReserved(nameLoc, *name);
Olli Etuahocce89652017-06-19 16:04:09 +03003407 TParameter param = {name, type};
3408 return param;
3409}
3410
Olli Etuaho55bde912017-10-25 13:41:13 +03003411TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3412 const TString *name,
3413 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003414{
Olli Etuaho55bde912017-10-25 13:41:13 +03003415 TType *type = new TType(publicType);
3416 return parseParameterDeclarator(type, name, nameLoc);
3417}
3418
3419TParameter TParseContext::parseParameterArrayDeclarator(const TString *name,
3420 const TSourceLoc &nameLoc,
3421 unsigned int arraySize,
3422 const TSourceLoc &arrayLoc,
3423 TPublicType *elementType)
3424{
3425 checkArrayElementIsNotArray(arrayLoc, *elementType);
3426 TType *arrayType = new TType(*elementType);
3427 arrayType->makeArray(arraySize);
3428 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003429}
3430
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003431bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(TIntermSequence *arguments,
3432 TType type,
3433 const TSourceLoc &line)
3434{
3435 if (arguments->empty())
3436 {
3437 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3438 return false;
3439 }
3440 for (TIntermNode *arg : *arguments)
3441 {
3442 TIntermTyped *element = arg->getAsTyped();
3443 ASSERT(element);
3444 size_t dimensionalityFromElement = element->getType().getArraySizes().size() + 1u;
3445 if (dimensionalityFromElement > type.getArraySizes().size())
3446 {
3447 error(line, "constructing from a non-dereferenced array", "constructor");
3448 return false;
3449 }
3450 else if (dimensionalityFromElement < type.getArraySizes().size())
3451 {
3452 if (dimensionalityFromElement == 1u)
3453 {
3454 error(line, "implicitly sized array of arrays constructor argument is not an array",
3455 "constructor");
3456 }
3457 else
3458 {
3459 error(line,
3460 "implicitly sized array of arrays constructor argument dimensionality is too "
3461 "low",
3462 "constructor");
3463 }
3464 return false;
3465 }
3466 }
3467 return true;
3468}
3469
Jamie Madillb98c3a82015-07-23 14:26:04 -04003470// This function is used to test for the correctness of the parameters passed to various constructor
3471// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003472//
Olli Etuaho856c4972016-08-08 11:38:39 +03003473// 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 +00003474//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003475TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003476 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303477 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003478{
Olli Etuaho856c4972016-08-08 11:38:39 +03003479 if (type.isUnsizedArray())
3480 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003481 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003482 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003483 type.sizeUnsizedArrays(TVector<unsigned int>());
Olli Etuaho3ec75682017-07-05 17:02:55 +03003484 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003485 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003486 TIntermTyped *firstElement = arguments->at(0)->getAsTyped();
3487 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003488 if (type.getOutermostArraySize() == 0u)
3489 {
3490 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments->size()));
3491 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003492 for (size_t i = 0; i < firstElement->getType().getArraySizes().size(); ++i)
3493 {
3494 if (type.getArraySizes()[i] == 0u)
3495 {
3496 type.setArraySize(i, firstElement->getType().getArraySizes().at(i));
3497 }
3498 }
3499 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003500 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003501
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003502 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003503 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003504 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003505 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003506
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003507 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003508 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003509
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003510 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3511 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003512 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003513 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003514 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003515 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003516}
3517
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003518//
3519// Interface/uniform blocks
Jiawei Shaod8105a02017-08-08 09:54:36 +08003520// TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003521//
Olli Etuaho13389b62016-10-16 11:48:18 +01003522TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003523 const TTypeQualifierBuilder &typeQualifierBuilder,
3524 const TSourceLoc &nameLine,
3525 const TString &blockName,
3526 TFieldList *fieldList,
3527 const TString *instanceName,
3528 const TSourceLoc &instanceLine,
3529 TIntermTyped *arrayIndex,
3530 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003531{
Olli Etuaho856c4972016-08-08 11:38:39 +03003532 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003533
Olli Etuaho77ba4082016-12-16 12:01:18 +00003534 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003535
Jiajia Qinbc585152017-06-23 15:42:17 +08003536 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003537 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003538 error(typeQualifier.line,
3539 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3540 "3.10",
3541 getQualifierString(typeQualifier.qualifier));
3542 }
3543 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3544 {
3545 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003546 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003547 }
3548
Martin Radev70866b82016-07-22 15:27:42 +03003549 if (typeQualifier.invariant)
3550 {
3551 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3552 }
3553
Jiajia Qinbc585152017-06-23 15:42:17 +08003554 if (typeQualifier.qualifier != EvqBuffer)
3555 {
3556 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3557 }
Olli Etuaho43364892017-02-13 16:00:12 +00003558
jchen10af713a22017-04-19 09:10:56 +08003559 // add array index
3560 unsigned int arraySize = 0;
3561 if (arrayIndex != nullptr)
3562 {
3563 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3564 }
3565
3566 if (mShaderVersion < 310)
3567 {
3568 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3569 }
3570 else
3571 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003572 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3573 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003574 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003575
Andrei Volykhina5527072017-03-22 16:46:30 +03003576 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3577
Jamie Madill099c0f32013-06-20 11:55:52 -04003578 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003579 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003580 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3581 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003582
Jamie Madill099c0f32013-06-20 11:55:52 -04003583 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3584 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003585 if (typeQualifier.qualifier == EvqUniform)
3586 {
3587 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3588 }
3589 else if (typeQualifier.qualifier == EvqBuffer)
3590 {
3591 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3592 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003593 }
3594
Jamie Madill1566ef72013-06-20 11:55:54 -04003595 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3596 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003597 if (typeQualifier.qualifier == EvqUniform)
3598 {
3599 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3600 }
3601 else if (typeQualifier.qualifier == EvqBuffer)
3602 {
3603 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3604 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003605 }
3606
Olli Etuaho856c4972016-08-08 11:38:39 +03003607 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003608
Martin Radev2cc85b32016-08-05 16:22:53 +03003609 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3610
Olli Etuaho0f684632017-07-13 12:42:15 +03003611 if (!symbolTable.declareInterfaceBlockName(&blockName))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303612 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003613 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003614 }
3615
Jamie Madill98493dd2013-07-08 14:39:03 -04003616 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303617 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3618 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003619 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303620 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003621 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303622 {
jchen10cc2a10e2017-05-03 14:05:12 +08003623 std::string reason("unsupported type - ");
3624 reason += fieldType->getBasicString();
3625 reason += " types are not allowed in interface blocks";
3626 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003627 }
3628
Jamie Madill98493dd2013-07-08 14:39:03 -04003629 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003630 switch (qualifier)
3631 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003632 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003633 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003634 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003635 if (typeQualifier.qualifier == EvqBuffer)
3636 {
3637 error(field->line(), "invalid qualifier on shader storage block member",
3638 getQualifierString(qualifier));
3639 }
3640 break;
3641 case EvqBuffer:
3642 if (typeQualifier.qualifier == EvqUniform)
3643 {
3644 error(field->line(), "invalid qualifier on uniform block member",
3645 getQualifierString(qualifier));
3646 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003647 break;
3648 default:
3649 error(field->line(), "invalid qualifier on interface block member",
3650 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003651 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003652 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003653
Martin Radev70866b82016-07-22 15:27:42 +03003654 if (fieldType->isInvariant())
3655 {
3656 error(field->line(), "invalid qualifier on interface block member", "invariant");
3657 }
3658
Jamie Madilla5efff92013-06-06 11:56:47 -04003659 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003660 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003661 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003662 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003663
Jamie Madill98493dd2013-07-08 14:39:03 -04003664 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003665 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003666 error(field->line(), "invalid layout qualifier: cannot be used here",
3667 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003668 }
3669
Jamie Madill98493dd2013-07-08 14:39:03 -04003670 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003671 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003672 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003673 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003674 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003675 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003676 warning(field->line(),
3677 "extraneous layout qualifier: only has an effect on matrix types",
3678 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003679 }
3680
Jamie Madill98493dd2013-07-08 14:39:03 -04003681 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003682
3683 if (typeQualifier.qualifier == EvqBuffer)
3684 {
3685 // set memory qualifiers
3686 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3687 // qualified with a memory qualifier, it is as if all of its members were declared with
3688 // the same memory qualifier.
3689 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3690 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3691 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3692 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3693 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3694 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3695 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3696 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3697 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3698 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3699 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003700 }
3701
Jamie Madillb98c3a82015-07-23 14:26:04 -04003702 TInterfaceBlock *interfaceBlock =
Shaob18c33e2017-08-16 12:37:51 +08003703 new TInterfaceBlock(&blockName, fieldList, instanceName, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003704 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
3705 if (arrayIndex != nullptr)
3706 {
3707 interfaceBlockType.makeArray(arraySize);
3708 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003709
3710 TString symbolName = "";
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003711 const TSymbolUniqueId *symbolId = nullptr;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003712
Jamie Madill98493dd2013-07-08 14:39:03 -04003713 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003714 {
3715 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003716 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3717 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003718 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303719 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003720
3721 // set parent pointer of the field variable
3722 fieldType->setInterfaceBlock(interfaceBlock);
3723
Olli Etuaho0f684632017-07-13 12:42:15 +03003724 TVariable *fieldVariable = symbolTable.declareVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003725
Olli Etuaho0f684632017-07-13 12:42:15 +03003726 if (fieldVariable)
3727 {
3728 fieldVariable->setQualifier(typeQualifier.qualifier);
3729 }
3730 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303731 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003732 error(field->line(), "redefinition of an interface block member name",
3733 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003734 }
3735 }
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003736 symbolId = &symbolTable.getEmptySymbolId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003737 }
3738 else
3739 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003740 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003741
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003742 // add a symbol for this interface block
Olli Etuaho0f684632017-07-13 12:42:15 +03003743 TVariable *instanceTypeDef = symbolTable.declareVariable(instanceName, interfaceBlockType);
3744 if (instanceTypeDef)
3745 {
3746 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003747 symbolId = &instanceTypeDef->getUniqueId();
Olli Etuaho0f684632017-07-13 12:42:15 +03003748 }
3749 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303750 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003751 error(instanceLine, "redefinition of an interface block instance name",
3752 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003753 }
Olli Etuaho0f684632017-07-13 12:42:15 +03003754 symbolName = *instanceName;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003755 }
3756
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003757 TIntermDeclaration *declaration = nullptr;
3758
3759 if (symbolId)
3760 {
3761 TIntermSymbol *blockSymbol = new TIntermSymbol(*symbolId, symbolName, interfaceBlockType);
3762 blockSymbol->setLine(typeQualifier.line);
3763 declaration = new TIntermDeclaration();
3764 declaration->appendDeclarator(blockSymbol);
3765 declaration->setLine(nameLine);
3766 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003767
3768 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003769 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003770}
3771
Olli Etuaho383b7912016-08-05 11:22:59 +03003772void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003773{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003774 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003775
3776 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003777 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303778 if (mStructNestingLevel > 1)
3779 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003780 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003781 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003782}
3783
3784void TParseContext::exitStructDeclaration()
3785{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003786 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003787}
3788
Olli Etuaho8a176262016-08-16 14:23:01 +03003789void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003790{
Jamie Madillacb4b812016-11-07 13:50:29 -05003791 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303792 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003793 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003794 }
3795
Arun Patole7e7e68d2015-05-22 12:02:25 +05303796 if (field.type()->getBasicType() != EbtStruct)
3797 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003798 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003799 }
3800
3801 // We're already inside a structure definition at this point, so add
3802 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303803 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3804 {
Jamie Madill41a49272014-03-18 16:10:13 -04003805 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003806 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3807 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003808 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003809 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003810 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003811 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003812}
3813
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003814//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003815// Parse an array index expression
3816//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003817TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3818 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303819 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003820{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003821 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3822 {
3823 if (baseExpression->getAsSymbolNode())
3824 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303825 error(location, " left of '[' is not of type array, matrix, or vector ",
3826 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003827 }
3828 else
3829 {
3830 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3831 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003832
Olli Etuaho3ec75682017-07-05 17:02:55 +03003833 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003834 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003835
Jiawei Shaod8105a02017-08-08 09:54:36 +08003836 if (baseExpression->getQualifier() == EvqPerVertexIn)
3837 {
3838 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
3839 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3840 {
3841 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3842 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3843 }
3844 }
3845
Jamie Madill21c1e452014-12-29 11:33:41 -05003846 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3847
Olli Etuaho36b05142015-11-12 13:10:42 +02003848 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3849 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3850 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3851 // index is a constant expression.
3852 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3853 {
3854 if (baseExpression->isInterfaceBlock())
3855 {
Jiawei Shaod8105a02017-08-08 09:54:36 +08003856 // TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
3857 switch (baseExpression->getQualifier())
3858 {
3859 case EvqPerVertexIn:
3860 break;
3861 case EvqUniform:
3862 case EvqBuffer:
3863 error(location,
3864 "array indexes for uniform block arrays and shader storage block arrays "
3865 "must be constant integral expressions",
3866 "[");
3867 break;
3868 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003869 // We can reach here only in error cases.
3870 ASSERT(mDiagnostics->numErrors() > 0);
3871 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003872 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003873 }
3874 else if (baseExpression->getQualifier() == EvqFragmentOut)
3875 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003876 error(location,
3877 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003878 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003879 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3880 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003881 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003882 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003883 }
3884
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003885 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003886 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003887 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3888 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3889 // constant fold expressions that are not constant expressions). The most compatible way to
3890 // handle this case is to report a warning instead of an error and force the index to be in
3891 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003892 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003893 int index = 0;
3894 if (indexConstantUnion->getBasicType() == EbtInt)
3895 {
3896 index = indexConstantUnion->getIConst(0);
3897 }
3898 else if (indexConstantUnion->getBasicType() == EbtUInt)
3899 {
3900 index = static_cast<int>(indexConstantUnion->getUConst(0));
3901 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003902
3903 int safeIndex = -1;
3904
3905 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003906 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003907 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003908 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003909 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003910 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003911 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003912 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003913 "GL_EXT_draw_buffers is disabled",
3914 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003915 safeIndex = 0;
3916 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003917 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003918 // Only do generic out-of-range check if similar error hasn't already been reported.
3919 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003920 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003921 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003922 baseExpression->getOutermostArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003923 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003924 }
3925 }
3926 else if (baseExpression->isMatrix())
3927 {
3928 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003929 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003930 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003931 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003932 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003933 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003934 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3935 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003936 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003937 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003938
3939 ASSERT(safeIndex >= 0);
3940 // Data of constant unions can't be changed, because it may be shared with other
3941 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3942 // sanitized object.
Olli Etuaho56229f12017-07-10 14:16:33 +03003943 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003944 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003945 TConstantUnion *safeConstantUnion = new TConstantUnion();
3946 safeConstantUnion->setIConst(safeIndex);
3947 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
Olli Etuaho56229f12017-07-10 14:16:33 +03003948 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003949 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003950
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003951 TIntermBinary *node = new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
3952 node->setLine(location);
3953 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003954 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003955 else
3956 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003957 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
3958 node->setLine(location);
3959 // Indirect indexing can never be constant folded.
3960 return node;
Jamie Madill7164cf42013-07-08 13:30:59 -04003961 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003962}
3963
Olli Etuaho90892fb2016-07-14 14:44:51 +03003964int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3965 const TSourceLoc &location,
3966 int index,
3967 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003968 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003969{
3970 if (index >= arraySize || index < 0)
3971 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003972 std::stringstream reasonStream;
3973 reasonStream << reason << " '" << index << "'";
3974 std::string token = reasonStream.str();
3975 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003976 if (index < 0)
3977 {
3978 return 0;
3979 }
3980 else
3981 {
3982 return arraySize - 1;
3983 }
3984 }
3985 return index;
3986}
3987
Jamie Madillb98c3a82015-07-23 14:26:04 -04003988TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3989 const TSourceLoc &dotLocation,
3990 const TString &fieldString,
3991 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003992{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003993 if (baseExpression->isArray())
3994 {
3995 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003996 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003997 }
3998
3999 if (baseExpression->isVector())
4000 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004001 TVector<int> fieldOffsets;
4002 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4003 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004004 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004005 fieldOffsets.resize(1);
4006 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004007 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004008 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4009 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004010
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004011 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004012 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004013 else if (baseExpression->getBasicType() == EbtStruct)
4014 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304015 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004016 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004017 {
4018 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004019 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004020 }
4021 else
4022 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004023 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004024 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004025 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004026 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004027 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004028 {
4029 fieldFound = true;
4030 break;
4031 }
4032 }
4033 if (fieldFound)
4034 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004035 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004036 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004037 TIntermBinary *node =
4038 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4039 node->setLine(dotLocation);
4040 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004041 }
4042 else
4043 {
4044 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004045 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004046 }
4047 }
4048 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004049 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004050 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304051 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004052 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004053 {
4054 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004055 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004056 }
4057 else
4058 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004059 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004060 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004061 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004062 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004063 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004064 {
4065 fieldFound = true;
4066 break;
4067 }
4068 }
4069 if (fieldFound)
4070 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004071 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004072 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004073 TIntermBinary *node =
4074 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4075 node->setLine(dotLocation);
4076 // Indexing interface blocks can never be constant folded.
4077 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004078 }
4079 else
4080 {
4081 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004082 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004083 }
4084 }
4085 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004086 else
4087 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004088 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004089 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004090 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304091 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004092 }
4093 else
4094 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304095 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004096 " field selection requires structure, vector, or interface block on left hand "
4097 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304098 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004099 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004100 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004101 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004102}
4103
Jamie Madillb98c3a82015-07-23 14:26:04 -04004104TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4105 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004106{
Martin Radev802abe02016-08-04 17:48:32 +03004107 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004108
4109 if (qualifierType == "shared")
4110 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004111 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004112 {
4113 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4114 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004115 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004116 }
4117 else if (qualifierType == "packed")
4118 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004119 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004120 {
4121 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4122 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004123 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004124 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004125 else if (qualifierType == "std430")
4126 {
4127 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4128 qualifier.blockStorage = EbsStd430;
4129 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004130 else if (qualifierType == "std140")
4131 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004132 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004133 }
4134 else if (qualifierType == "row_major")
4135 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004136 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004137 }
4138 else if (qualifierType == "column_major")
4139 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004140 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004141 }
4142 else if (qualifierType == "location")
4143 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004144 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4145 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004146 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004147 else if (qualifierType == "yuv" && isExtensionEnabled(TExtension::EXT_YUV_target) &&
Andrei Volykhina5527072017-03-22 16:46:30 +03004148 mShaderType == GL_FRAGMENT_SHADER)
4149 {
4150 qualifier.yuv = true;
4151 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004152 else if (qualifierType == "rgba32f")
4153 {
4154 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4155 qualifier.imageInternalFormat = EiifRGBA32F;
4156 }
4157 else if (qualifierType == "rgba16f")
4158 {
4159 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4160 qualifier.imageInternalFormat = EiifRGBA16F;
4161 }
4162 else if (qualifierType == "r32f")
4163 {
4164 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4165 qualifier.imageInternalFormat = EiifR32F;
4166 }
4167 else if (qualifierType == "rgba8")
4168 {
4169 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4170 qualifier.imageInternalFormat = EiifRGBA8;
4171 }
4172 else if (qualifierType == "rgba8_snorm")
4173 {
4174 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4175 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4176 }
4177 else if (qualifierType == "rgba32i")
4178 {
4179 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4180 qualifier.imageInternalFormat = EiifRGBA32I;
4181 }
4182 else if (qualifierType == "rgba16i")
4183 {
4184 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4185 qualifier.imageInternalFormat = EiifRGBA16I;
4186 }
4187 else if (qualifierType == "rgba8i")
4188 {
4189 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4190 qualifier.imageInternalFormat = EiifRGBA8I;
4191 }
4192 else if (qualifierType == "r32i")
4193 {
4194 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4195 qualifier.imageInternalFormat = EiifR32I;
4196 }
4197 else if (qualifierType == "rgba32ui")
4198 {
4199 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4200 qualifier.imageInternalFormat = EiifRGBA32UI;
4201 }
4202 else if (qualifierType == "rgba16ui")
4203 {
4204 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4205 qualifier.imageInternalFormat = EiifRGBA16UI;
4206 }
4207 else if (qualifierType == "rgba8ui")
4208 {
4209 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4210 qualifier.imageInternalFormat = EiifRGBA8UI;
4211 }
4212 else if (qualifierType == "r32ui")
4213 {
4214 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4215 qualifier.imageInternalFormat = EiifR32UI;
4216 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004217 else if (qualifierType == "points" && isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004218 mShaderType == GL_GEOMETRY_SHADER_OES)
4219 {
4220 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4221 qualifier.primitiveType = EptPoints;
4222 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004223 else if (qualifierType == "lines" && isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004224 mShaderType == GL_GEOMETRY_SHADER_OES)
4225 {
4226 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4227 qualifier.primitiveType = EptLines;
4228 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004229 else if (qualifierType == "lines_adjacency" &&
4230 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004231 mShaderType == GL_GEOMETRY_SHADER_OES)
4232 {
4233 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4234 qualifier.primitiveType = EptLinesAdjacency;
4235 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004236 else if (qualifierType == "triangles" && isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004237 mShaderType == GL_GEOMETRY_SHADER_OES)
4238 {
4239 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4240 qualifier.primitiveType = EptTriangles;
4241 }
4242 else if (qualifierType == "triangles_adjacency" &&
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004243 isExtensionEnabled(TExtension::OES_geometry_shader) &&
4244 mShaderType == GL_GEOMETRY_SHADER_OES)
Shaob5cc1192017-07-06 10:47:20 +08004245 {
4246 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4247 qualifier.primitiveType = EptTrianglesAdjacency;
4248 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004249 else if (qualifierType == "line_strip" && isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004250 mShaderType == GL_GEOMETRY_SHADER_OES)
4251 {
4252 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4253 qualifier.primitiveType = EptLineStrip;
4254 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004255 else if (qualifierType == "triangle_strip" &&
4256 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004257 mShaderType == GL_GEOMETRY_SHADER_OES)
4258 {
4259 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4260 qualifier.primitiveType = EptTriangleStrip;
4261 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004262
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004263 else
4264 {
4265 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004266 }
4267
Jamie Madilla5efff92013-06-06 11:56:47 -04004268 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004269}
4270
Martin Radev802abe02016-08-04 17:48:32 +03004271void TParseContext::parseLocalSize(const TString &qualifierType,
4272 const TSourceLoc &qualifierTypeLine,
4273 int intValue,
4274 const TSourceLoc &intValueLine,
4275 const std::string &intValueString,
4276 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004277 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004278{
Olli Etuaho856c4972016-08-08 11:38:39 +03004279 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004280 if (intValue < 1)
4281 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004282 std::stringstream reasonStream;
4283 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4284 std::string reason = reasonStream.str();
4285 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004286 }
4287 (*localSize)[index] = intValue;
4288}
4289
Olli Etuaho09b04a22016-12-15 13:30:26 +00004290void TParseContext::parseNumViews(int intValue,
4291 const TSourceLoc &intValueLine,
4292 const std::string &intValueString,
4293 int *numViews)
4294{
4295 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4296 // specification.
4297 if (intValue < 1)
4298 {
4299 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4300 }
4301 *numViews = intValue;
4302}
4303
Shaob5cc1192017-07-06 10:47:20 +08004304void TParseContext::parseInvocations(int intValue,
4305 const TSourceLoc &intValueLine,
4306 const std::string &intValueString,
4307 int *numInvocations)
4308{
4309 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4310 // it doesn't make sense to accept invocations <= 0.
4311 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4312 {
4313 error(intValueLine,
4314 "out of range: invocations must be in the range of [1, "
4315 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4316 intValueString.c_str());
4317 }
4318 else
4319 {
4320 *numInvocations = intValue;
4321 }
4322}
4323
4324void TParseContext::parseMaxVertices(int intValue,
4325 const TSourceLoc &intValueLine,
4326 const std::string &intValueString,
4327 int *maxVertices)
4328{
4329 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4330 // it doesn't make sense to accept max_vertices < 0.
4331 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4332 {
4333 error(
4334 intValueLine,
4335 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4336 intValueString.c_str());
4337 }
4338 else
4339 {
4340 *maxVertices = intValue;
4341 }
4342}
4343
Jamie Madillb98c3a82015-07-23 14:26:04 -04004344TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4345 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004346 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304347 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004348{
Martin Radev802abe02016-08-04 17:48:32 +03004349 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004350
Martin Radev802abe02016-08-04 17:48:32 +03004351 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004352
Martin Radev802abe02016-08-04 17:48:32 +03004353 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004354 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004355 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004356 if (intValue < 0)
4357 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004358 error(intValueLine, "out of range: location must be non-negative",
4359 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004360 }
4361 else
4362 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004363 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004364 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004365 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004366 }
Olli Etuaho43364892017-02-13 16:00:12 +00004367 else if (qualifierType == "binding")
4368 {
4369 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4370 if (intValue < 0)
4371 {
4372 error(intValueLine, "out of range: binding must be non-negative",
4373 intValueString.c_str());
4374 }
4375 else
4376 {
4377 qualifier.binding = intValue;
4378 }
4379 }
jchen104cdac9e2017-05-08 11:01:20 +08004380 else if (qualifierType == "offset")
4381 {
4382 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4383 if (intValue < 0)
4384 {
4385 error(intValueLine, "out of range: offset must be non-negative",
4386 intValueString.c_str());
4387 }
4388 else
4389 {
4390 qualifier.offset = intValue;
4391 }
4392 }
Martin Radev802abe02016-08-04 17:48:32 +03004393 else if (qualifierType == "local_size_x")
4394 {
4395 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4396 &qualifier.localSize);
4397 }
4398 else if (qualifierType == "local_size_y")
4399 {
4400 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4401 &qualifier.localSize);
4402 }
4403 else if (qualifierType == "local_size_z")
4404 {
4405 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4406 &qualifier.localSize);
4407 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004408 else if (qualifierType == "num_views" && isExtensionEnabled(TExtension::OVR_multiview) &&
Olli Etuaho09b04a22016-12-15 13:30:26 +00004409 mShaderType == GL_VERTEX_SHADER)
4410 {
4411 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4412 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004413 else if (qualifierType == "invocations" &&
4414 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004415 mShaderType == GL_GEOMETRY_SHADER_OES)
4416 {
4417 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4418 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004419 else if (qualifierType == "max_vertices" &&
4420 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004421 mShaderType == GL_GEOMETRY_SHADER_OES)
4422 {
4423 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4424 }
4425
Martin Radev802abe02016-08-04 17:48:32 +03004426 else
4427 {
4428 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004429 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004430
Jamie Madilla5efff92013-06-06 11:56:47 -04004431 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004432}
4433
Olli Etuaho613b9592016-09-05 12:05:53 +03004434TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4435{
4436 return new TTypeQualifierBuilder(
4437 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4438 mShaderVersion);
4439}
4440
Olli Etuahocce89652017-06-19 16:04:09 +03004441TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4442 const TSourceLoc &loc)
4443{
4444 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4445 return new TStorageQualifierWrapper(qualifier, loc);
4446}
4447
4448TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4449{
4450 if (getShaderType() == GL_VERTEX_SHADER)
4451 {
4452 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4453 }
4454 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4455}
4456
4457TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4458{
4459 if (declaringFunction())
4460 {
4461 return new TStorageQualifierWrapper(EvqIn, loc);
4462 }
Shaob5cc1192017-07-06 10:47:20 +08004463
4464 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004465 {
Shaob5cc1192017-07-06 10:47:20 +08004466 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004467 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004468 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004469 {
4470 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4471 }
4472 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004473 }
Shaob5cc1192017-07-06 10:47:20 +08004474 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004475 {
Shaob5cc1192017-07-06 10:47:20 +08004476 if (mShaderVersion < 300)
4477 {
4478 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4479 }
4480 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004481 }
Shaob5cc1192017-07-06 10:47:20 +08004482 case GL_COMPUTE_SHADER:
4483 {
4484 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4485 }
4486 case GL_GEOMETRY_SHADER_OES:
4487 {
4488 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4489 }
4490 default:
4491 {
4492 UNREACHABLE();
4493 return new TStorageQualifierWrapper(EvqLast, loc);
4494 }
Olli Etuahocce89652017-06-19 16:04:09 +03004495 }
Olli Etuahocce89652017-06-19 16:04:09 +03004496}
4497
4498TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4499{
4500 if (declaringFunction())
4501 {
4502 return new TStorageQualifierWrapper(EvqOut, loc);
4503 }
Shaob5cc1192017-07-06 10:47:20 +08004504 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004505 {
Shaob5cc1192017-07-06 10:47:20 +08004506 case GL_VERTEX_SHADER:
4507 {
4508 if (mShaderVersion < 300)
4509 {
4510 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4511 }
4512 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4513 }
4514 case GL_FRAGMENT_SHADER:
4515 {
4516 if (mShaderVersion < 300)
4517 {
4518 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4519 }
4520 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4521 }
4522 case GL_COMPUTE_SHADER:
4523 {
4524 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4525 return new TStorageQualifierWrapper(EvqLast, loc);
4526 }
4527 case GL_GEOMETRY_SHADER_OES:
4528 {
4529 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4530 }
4531 default:
4532 {
4533 UNREACHABLE();
4534 return new TStorageQualifierWrapper(EvqLast, loc);
4535 }
Olli Etuahocce89652017-06-19 16:04:09 +03004536 }
Olli Etuahocce89652017-06-19 16:04:09 +03004537}
4538
4539TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4540{
4541 if (!declaringFunction())
4542 {
4543 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4544 }
4545 return new TStorageQualifierWrapper(EvqInOut, loc);
4546}
4547
Jamie Madillb98c3a82015-07-23 14:26:04 -04004548TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004549 TLayoutQualifier rightQualifier,
4550 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004551{
Martin Radevc28888b2016-07-22 15:27:42 +03004552 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004553 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004554}
4555
Olli Etuahocce89652017-06-19 16:04:09 +03004556TField *TParseContext::parseStructDeclarator(TString *identifier, const TSourceLoc &loc)
4557{
4558 checkIsNotReserved(loc, *identifier);
4559 TType *type = new TType(EbtVoid, EbpUndefined);
4560 return new TField(type, identifier, loc);
4561}
4562
4563TField *TParseContext::parseStructArrayDeclarator(TString *identifier,
4564 const TSourceLoc &loc,
Olli Etuaho4ddae352017-10-26 16:20:18 +03004565 unsigned int arraySize,
Olli Etuahocce89652017-06-19 16:04:09 +03004566 const TSourceLoc &arraySizeLoc)
4567{
4568 checkIsNotReserved(loc, *identifier);
4569
4570 TType *type = new TType(EbtVoid, EbpUndefined);
Olli Etuaho4ddae352017-10-26 16:20:18 +03004571 type->makeArray(arraySize);
Olli Etuahocce89652017-06-19 16:04:09 +03004572
4573 return new TField(type, identifier, loc);
4574}
4575
Olli Etuaho722bfb52017-10-26 17:00:11 +03004576void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4577 const TFieldList::const_iterator end,
4578 const TString &name,
4579 const TSourceLoc &location)
4580{
4581 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4582 {
4583 if ((*fieldIter)->name() == name)
4584 {
4585 error(location, "duplicate field name in structure", name.c_str());
4586 }
4587 }
4588}
4589
4590TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4591{
4592 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4593 ++fieldIter)
4594 {
4595 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4596 location);
4597 }
4598 return fields;
4599}
4600
Olli Etuaho4de340a2016-12-16 09:32:03 +00004601TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4602 const TFieldList *newlyAddedFields,
4603 const TSourceLoc &location)
4604{
4605 for (TField *field : *newlyAddedFields)
4606 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004607 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4608 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004609 processedFields->push_back(field);
4610 }
4611 return processedFields;
4612}
4613
Martin Radev70866b82016-07-22 15:27:42 +03004614TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4615 const TTypeQualifierBuilder &typeQualifierBuilder,
4616 TPublicType *typeSpecifier,
4617 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004618{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004619 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004620
Martin Radev70866b82016-07-22 15:27:42 +03004621 typeSpecifier->qualifier = typeQualifier.qualifier;
4622 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004623 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004624 typeSpecifier->invariant = typeQualifier.invariant;
4625 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304626 {
Martin Radev70866b82016-07-22 15:27:42 +03004627 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004628 }
Martin Radev70866b82016-07-22 15:27:42 +03004629 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004630}
4631
Jamie Madillb98c3a82015-07-23 14:26:04 -04004632TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004633 TFieldList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004634{
Martin Radev4a9cd802016-09-01 16:51:51 +03004635 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4636 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004637
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004638 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
4639 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004640
Martin Radev4a9cd802016-09-01 16:51:51 +03004641 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004642
Olli Etuaho55bde912017-10-25 13:41:13 +03004643 for (TField *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304644 {
Olli Etuaho55bde912017-10-25 13:41:13 +03004645 auto declaratorArraySizes = declarator->type()->getArraySizes();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004646 // don't allow arrays of arrays
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004647 if (!declaratorArraySizes.empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304648 {
Olli Etuahoe0803872017-08-23 15:30:23 +03004649 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004650 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004651
Olli Etuaho55bde912017-10-25 13:41:13 +03004652 TType *type = declarator->type();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004653 *type = TType(typeSpecifier);
4654 for (unsigned int arraySize : declaratorArraySizes)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304655 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004656 type->makeArray(arraySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004657 }
Olli Etuaho55bde912017-10-25 13:41:13 +03004658 checkIsNotUnsizedArray(typeSpecifier.getLine(),
4659 "array members of structs must specify a size",
4660 declarator->name().c_str(), type);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004661
Olli Etuaho55bde912017-10-25 13:41:13 +03004662 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *declarator);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004663 }
4664
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004665 return declaratorList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004666}
4667
Martin Radev4a9cd802016-09-01 16:51:51 +03004668TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4669 const TSourceLoc &nameLine,
4670 const TString *structName,
4671 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004672{
Olli Etuahoa5e693a2017-07-13 16:07:26 +03004673 TStructure *structure = new TStructure(&symbolTable, structName, fieldList);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004674
Jamie Madill9b820842015-02-12 10:40:10 -05004675 // Store a bool in the struct if we're at global scope, to allow us to
4676 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004677 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004678
Jamie Madill98493dd2013-07-08 14:39:03 -04004679 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004680 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004681 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004682 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304683 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004684 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004685 }
4686 }
4687
4688 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004689 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004690 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004691 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004692 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004693 switch (qualifier)
4694 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004695 case EvqGlobal:
4696 case EvqTemporary:
4697 break;
4698 default:
4699 error(field.line(), "invalid qualifier on struct member",
4700 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004701 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004702 }
Martin Radev70866b82016-07-22 15:27:42 +03004703 if (field.type()->isInvariant())
4704 {
4705 error(field.line(), "invalid qualifier on struct member", "invariant");
4706 }
jchen104cdac9e2017-05-08 11:01:20 +08004707 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4708 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004709 {
4710 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4711 }
4712
Olli Etuaho43364892017-02-13 16:00:12 +00004713 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4714
4715 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004716
4717 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004718 }
4719
Martin Radev4a9cd802016-09-01 16:51:51 +03004720 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004721 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004722 exitStructDeclaration();
4723
Martin Radev4a9cd802016-09-01 16:51:51 +03004724 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004725}
4726
Jamie Madillb98c3a82015-07-23 14:26:04 -04004727TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004728 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004729 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004730{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004731 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004732 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004733 init->isVector())
4734 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004735 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4736 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004737 return nullptr;
4738 }
4739
Olli Etuaho923ecef2017-10-11 12:01:38 +03004740 ASSERT(statementList);
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004741 if (!ValidateSwitchStatementList(switchType, mShaderVersion, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004742 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004743 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004744 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004745 }
4746
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004747 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4748 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004749 return node;
4750}
4751
4752TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4753{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004754 if (mSwitchNestingLevel == 0)
4755 {
4756 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004757 return nullptr;
4758 }
4759 if (condition == nullptr)
4760 {
4761 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004762 return nullptr;
4763 }
4764 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004765 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004766 {
4767 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004768 }
4769 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004770 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4771 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4772 // fold in case labels.
4773 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004774 {
4775 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004776 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004777 TIntermCase *node = new TIntermCase(condition);
4778 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004779 return node;
4780}
4781
4782TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4783{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004784 if (mSwitchNestingLevel == 0)
4785 {
4786 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004787 return nullptr;
4788 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004789 TIntermCase *node = new TIntermCase(nullptr);
4790 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004791 return node;
4792}
4793
Jamie Madillb98c3a82015-07-23 14:26:04 -04004794TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4795 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004796 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004797{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004798 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004799
4800 switch (op)
4801 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004802 case EOpLogicalNot:
4803 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4804 child->isVector())
4805 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004806 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004807 return nullptr;
4808 }
4809 break;
4810 case EOpBitwiseNot:
4811 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4812 child->isMatrix() || child->isArray())
4813 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004814 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004815 return nullptr;
4816 }
4817 break;
4818 case EOpPostIncrement:
4819 case EOpPreIncrement:
4820 case EOpPostDecrement:
4821 case EOpPreDecrement:
4822 case EOpNegative:
4823 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004824 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4825 child->getBasicType() == EbtBool || child->isArray() ||
4826 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004827 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004828 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004829 return nullptr;
4830 }
4831 // Operators for built-ins are already type checked against their prototype.
4832 default:
4833 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004834 }
4835
Jiajia Qinbc585152017-06-23 15:42:17 +08004836 if (child->getMemoryQualifier().writeonly)
4837 {
4838 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4839 return nullptr;
4840 }
4841
Olli Etuahof119a262016-08-19 15:54:22 +03004842 TIntermUnary *node = new TIntermUnary(op, child);
4843 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004844
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004845 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004846}
4847
Olli Etuaho09b22472015-02-11 11:47:26 +02004848TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4849{
Olli Etuahocce89652017-06-19 16:04:09 +03004850 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004851 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004852 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004853 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004854 return child;
4855 }
4856 return node;
4857}
4858
Jamie Madillb98c3a82015-07-23 14:26:04 -04004859TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4860 TIntermTyped *child,
4861 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004862{
Olli Etuaho856c4972016-08-08 11:38:39 +03004863 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004864 return addUnaryMath(op, child, loc);
4865}
4866
Jamie Madillb98c3a82015-07-23 14:26:04 -04004867bool TParseContext::binaryOpCommonCheck(TOperator op,
4868 TIntermTyped *left,
4869 TIntermTyped *right,
4870 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004871{
jchen10b4cf5652017-05-05 18:51:17 +08004872 // Check opaque types are not allowed to be operands in expressions other than array indexing
4873 // and structure member selection.
4874 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4875 {
4876 switch (op)
4877 {
4878 case EOpIndexDirect:
4879 case EOpIndexIndirect:
4880 break;
4881 case EOpIndexDirectStruct:
4882 UNREACHABLE();
4883
4884 default:
4885 error(loc, "Invalid operation for variables with an opaque type",
4886 GetOperatorString(op));
4887 return false;
4888 }
4889 }
jchen10cc2a10e2017-05-03 14:05:12 +08004890
Jiajia Qinbc585152017-06-23 15:42:17 +08004891 if (right->getMemoryQualifier().writeonly)
4892 {
4893 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4894 return false;
4895 }
4896
4897 if (left->getMemoryQualifier().writeonly)
4898 {
4899 switch (op)
4900 {
4901 case EOpAssign:
4902 case EOpInitialize:
4903 case EOpIndexDirect:
4904 case EOpIndexIndirect:
4905 case EOpIndexDirectStruct:
4906 case EOpIndexDirectInterfaceBlock:
4907 break;
4908 default:
4909 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4910 return false;
4911 }
4912 }
4913
Olli Etuaho244be012016-08-18 15:26:02 +03004914 if (left->getType().getStruct() || right->getType().getStruct())
4915 {
4916 switch (op)
4917 {
4918 case EOpIndexDirectStruct:
4919 ASSERT(left->getType().getStruct());
4920 break;
4921 case EOpEqual:
4922 case EOpNotEqual:
4923 case EOpAssign:
4924 case EOpInitialize:
4925 if (left->getType() != right->getType())
4926 {
4927 return false;
4928 }
4929 break;
4930 default:
4931 error(loc, "Invalid operation for structs", GetOperatorString(op));
4932 return false;
4933 }
4934 }
4935
Olli Etuaho94050052017-05-08 14:17:44 +03004936 if (left->isInterfaceBlock() || right->isInterfaceBlock())
4937 {
4938 switch (op)
4939 {
4940 case EOpIndexDirectInterfaceBlock:
4941 ASSERT(left->getType().getInterfaceBlock());
4942 break;
4943 default:
4944 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
4945 return false;
4946 }
4947 }
4948
Olli Etuahod6b14282015-03-17 14:31:35 +02004949 if (left->isArray() || right->isArray())
4950 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004951 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02004952 {
4953 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4954 return false;
4955 }
4956
4957 if (left->isArray() != right->isArray())
4958 {
4959 error(loc, "array / non-array mismatch", GetOperatorString(op));
4960 return false;
4961 }
4962
4963 switch (op)
4964 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004965 case EOpEqual:
4966 case EOpNotEqual:
4967 case EOpAssign:
4968 case EOpInitialize:
4969 break;
4970 default:
4971 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4972 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02004973 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03004974 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004975 if (left->getType().getArraySizes() != right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02004976 {
4977 error(loc, "array size mismatch", GetOperatorString(op));
4978 return false;
4979 }
Olli Etuahod6b14282015-03-17 14:31:35 +02004980 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004981
4982 // Check ops which require integer / ivec parameters
4983 bool isBitShift = false;
4984 switch (op)
4985 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004986 case EOpBitShiftLeft:
4987 case EOpBitShiftRight:
4988 case EOpBitShiftLeftAssign:
4989 case EOpBitShiftRightAssign:
4990 // Unsigned can be bit-shifted by signed and vice versa, but we need to
4991 // check that the basic type is an integer type.
4992 isBitShift = true;
4993 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
4994 {
4995 return false;
4996 }
4997 break;
4998 case EOpBitwiseAnd:
4999 case EOpBitwiseXor:
5000 case EOpBitwiseOr:
5001 case EOpBitwiseAndAssign:
5002 case EOpBitwiseXorAssign:
5003 case EOpBitwiseOrAssign:
5004 // It is enough to check the type of only one operand, since later it
5005 // is checked that the operand types match.
5006 if (!IsInteger(left->getBasicType()))
5007 {
5008 return false;
5009 }
5010 break;
5011 default:
5012 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005013 }
5014
5015 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5016 // So the basic type should usually match.
5017 if (!isBitShift && left->getBasicType() != right->getBasicType())
5018 {
5019 return false;
5020 }
5021
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005022 // Check that:
5023 // 1. Type sizes match exactly on ops that require that.
5024 // 2. Restrictions for structs that contain arrays or samplers are respected.
5025 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005026 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005027 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005028 case EOpAssign:
5029 case EOpInitialize:
5030 case EOpEqual:
5031 case EOpNotEqual:
5032 // ESSL 1.00 sections 5.7, 5.8, 5.9
5033 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5034 {
5035 error(loc, "undefined operation for structs containing arrays",
5036 GetOperatorString(op));
5037 return false;
5038 }
5039 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5040 // we interpret the spec so that this extends to structs containing samplers,
5041 // similarly to ESSL 1.00 spec.
5042 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5043 left->getType().isStructureContainingSamplers())
5044 {
5045 error(loc, "undefined operation for structs containing samplers",
5046 GetOperatorString(op));
5047 return false;
5048 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005049
Olli Etuahoe1805592017-01-02 16:41:20 +00005050 if ((left->getNominalSize() != right->getNominalSize()) ||
5051 (left->getSecondarySize() != right->getSecondarySize()))
5052 {
5053 error(loc, "dimension mismatch", GetOperatorString(op));
5054 return false;
5055 }
5056 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005057 case EOpLessThan:
5058 case EOpGreaterThan:
5059 case EOpLessThanEqual:
5060 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005061 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005062 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005063 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005064 return false;
5065 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005066 break;
5067 case EOpAdd:
5068 case EOpSub:
5069 case EOpDiv:
5070 case EOpIMod:
5071 case EOpBitShiftLeft:
5072 case EOpBitShiftRight:
5073 case EOpBitwiseAnd:
5074 case EOpBitwiseXor:
5075 case EOpBitwiseOr:
5076 case EOpAddAssign:
5077 case EOpSubAssign:
5078 case EOpDivAssign:
5079 case EOpIModAssign:
5080 case EOpBitShiftLeftAssign:
5081 case EOpBitShiftRightAssign:
5082 case EOpBitwiseAndAssign:
5083 case EOpBitwiseXorAssign:
5084 case EOpBitwiseOrAssign:
5085 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5086 {
5087 return false;
5088 }
5089
5090 // Are the sizes compatible?
5091 if (left->getNominalSize() != right->getNominalSize() ||
5092 left->getSecondarySize() != right->getSecondarySize())
5093 {
5094 // If the nominal sizes of operands do not match:
5095 // One of them must be a scalar.
5096 if (!left->isScalar() && !right->isScalar())
5097 return false;
5098
5099 // In the case of compound assignment other than multiply-assign,
5100 // the right side needs to be a scalar. Otherwise a vector/matrix
5101 // would be assigned to a scalar. A scalar can't be shifted by a
5102 // vector either.
5103 if (!right->isScalar() &&
5104 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5105 return false;
5106 }
5107 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005108 default:
5109 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005110 }
5111
Olli Etuahod6b14282015-03-17 14:31:35 +02005112 return true;
5113}
5114
Olli Etuaho1dded802016-08-18 18:13:13 +03005115bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5116 const TType &left,
5117 const TType &right)
5118{
5119 switch (op)
5120 {
5121 case EOpMul:
5122 case EOpMulAssign:
5123 return left.getNominalSize() == right.getNominalSize() &&
5124 left.getSecondarySize() == right.getSecondarySize();
5125 case EOpVectorTimesScalar:
5126 return true;
5127 case EOpVectorTimesScalarAssign:
5128 ASSERT(!left.isMatrix() && !right.isMatrix());
5129 return left.isVector() && !right.isVector();
5130 case EOpVectorTimesMatrix:
5131 return left.getNominalSize() == right.getRows();
5132 case EOpVectorTimesMatrixAssign:
5133 ASSERT(!left.isMatrix() && right.isMatrix());
5134 return left.isVector() && left.getNominalSize() == right.getRows() &&
5135 left.getNominalSize() == right.getCols();
5136 case EOpMatrixTimesVector:
5137 return left.getCols() == right.getNominalSize();
5138 case EOpMatrixTimesScalar:
5139 return true;
5140 case EOpMatrixTimesScalarAssign:
5141 ASSERT(left.isMatrix() && !right.isMatrix());
5142 return !right.isVector();
5143 case EOpMatrixTimesMatrix:
5144 return left.getCols() == right.getRows();
5145 case EOpMatrixTimesMatrixAssign:
5146 ASSERT(left.isMatrix() && right.isMatrix());
5147 // We need to check two things:
5148 // 1. The matrix multiplication step is valid.
5149 // 2. The result will have the same number of columns as the lvalue.
5150 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5151
5152 default:
5153 UNREACHABLE();
5154 return false;
5155 }
5156}
5157
Jamie Madillb98c3a82015-07-23 14:26:04 -04005158TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5159 TIntermTyped *left,
5160 TIntermTyped *right,
5161 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005162{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005163 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005164 return nullptr;
5165
Olli Etuahofc1806e2015-03-17 13:03:11 +02005166 switch (op)
5167 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005168 case EOpEqual:
5169 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005170 case EOpLessThan:
5171 case EOpGreaterThan:
5172 case EOpLessThanEqual:
5173 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005174 break;
5175 case EOpLogicalOr:
5176 case EOpLogicalXor:
5177 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005178 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5179 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005180 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005181 {
5182 return nullptr;
5183 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005184 // Basic types matching should have been already checked.
5185 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005186 break;
5187 case EOpAdd:
5188 case EOpSub:
5189 case EOpDiv:
5190 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005191 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5192 !right->getType().getStruct());
5193 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005194 {
5195 return nullptr;
5196 }
5197 break;
5198 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005199 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5200 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005201 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005202 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005203 {
5204 return nullptr;
5205 }
5206 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005207 default:
5208 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005209 }
5210
Olli Etuaho1dded802016-08-18 18:13:13 +03005211 if (op == EOpMul)
5212 {
5213 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5214 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5215 {
5216 return nullptr;
5217 }
5218 }
5219
Olli Etuaho3fdec912016-08-18 15:08:06 +03005220 TIntermBinary *node = new TIntermBinary(op, left, right);
5221 node->setLine(loc);
5222
Olli Etuaho3fdec912016-08-18 15:08:06 +03005223 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005224 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005225}
5226
Jamie Madillb98c3a82015-07-23 14:26:04 -04005227TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5228 TIntermTyped *left,
5229 TIntermTyped *right,
5230 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005231{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005232 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005233 if (node == 0)
5234 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005235 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5236 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005237 return left;
5238 }
5239 return node;
5240}
5241
Jamie Madillb98c3a82015-07-23 14:26:04 -04005242TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5243 TIntermTyped *left,
5244 TIntermTyped *right,
5245 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005246{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005247 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005248 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005249 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005250 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5251 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005252 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005253 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005254 }
5255 return node;
5256}
5257
Olli Etuaho13389b62016-10-16 11:48:18 +01005258TIntermBinary *TParseContext::createAssign(TOperator op,
5259 TIntermTyped *left,
5260 TIntermTyped *right,
5261 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005262{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005263 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005264 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005265 if (op == EOpMulAssign)
5266 {
5267 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5268 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5269 {
5270 return nullptr;
5271 }
5272 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005273 TIntermBinary *node = new TIntermBinary(op, left, right);
5274 node->setLine(loc);
5275
Olli Etuaho3fdec912016-08-18 15:08:06 +03005276 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005277 }
5278 return nullptr;
5279}
5280
Jamie Madillb98c3a82015-07-23 14:26:04 -04005281TIntermTyped *TParseContext::addAssign(TOperator op,
5282 TIntermTyped *left,
5283 TIntermTyped *right,
5284 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005285{
Olli Etuahocce89652017-06-19 16:04:09 +03005286 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005287 TIntermTyped *node = createAssign(op, left, right, loc);
5288 if (node == nullptr)
5289 {
5290 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005291 return left;
5292 }
5293 return node;
5294}
5295
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005296TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5297 TIntermTyped *right,
5298 const TSourceLoc &loc)
5299{
Corentin Wallez0d959252016-07-12 17:26:32 -04005300 // WebGL2 section 5.26, the following results in an error:
5301 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005302 if (mShaderSpec == SH_WEBGL2_SPEC &&
5303 (left->isArray() || left->getBasicType() == EbtVoid ||
5304 left->getType().isStructureContainingArrays() || right->isArray() ||
5305 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005306 {
5307 error(loc,
5308 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5309 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005310 }
5311
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005312 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5313 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5314 commaNode->getTypePointer()->setQualifier(resultQualifier);
5315 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005316}
5317
Olli Etuaho49300862015-02-20 14:54:49 +02005318TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5319{
5320 switch (op)
5321 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005322 case EOpContinue:
5323 if (mLoopNestingLevel <= 0)
5324 {
5325 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005326 }
5327 break;
5328 case EOpBreak:
5329 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5330 {
5331 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005332 }
5333 break;
5334 case EOpReturn:
5335 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5336 {
5337 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005338 }
5339 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005340 case EOpKill:
5341 if (mShaderType != GL_FRAGMENT_SHADER)
5342 {
5343 error(loc, "discard supported in fragment shaders only", "discard");
5344 }
5345 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005346 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005347 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005348 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005349 }
Olli Etuahocce89652017-06-19 16:04:09 +03005350 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005351}
5352
Jamie Madillb98c3a82015-07-23 14:26:04 -04005353TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005354 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005355 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005356{
Olli Etuahocce89652017-06-19 16:04:09 +03005357 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005358 {
Olli Etuahocce89652017-06-19 16:04:09 +03005359 ASSERT(op == EOpReturn);
5360 mFunctionReturnsValue = true;
5361 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5362 {
5363 error(loc, "void function cannot return a value", "return");
5364 }
5365 else if (*mCurrentFunctionType != expression->getType())
5366 {
5367 error(loc, "function return is not matching type:", "return");
5368 }
Olli Etuaho49300862015-02-20 14:54:49 +02005369 }
Olli Etuahocce89652017-06-19 16:04:09 +03005370 TIntermBranch *node = new TIntermBranch(op, expression);
5371 node->setLine(loc);
5372 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005373}
5374
Martin Radev84aa2dc2017-09-11 15:51:02 +03005375void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5376{
5377 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
5378 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5379 bool isTextureGather = (name == "textureGather");
5380 bool isTextureGatherOffset = (name == "textureGatherOffset");
5381 if (isTextureGather || isTextureGatherOffset)
5382 {
5383 TIntermNode *componentNode = nullptr;
5384 TIntermSequence *arguments = functionCall->getSequence();
5385 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5386 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5387 ASSERT(sampler != nullptr);
5388 switch (sampler->getBasicType())
5389 {
5390 case EbtSampler2D:
5391 case EbtISampler2D:
5392 case EbtUSampler2D:
5393 case EbtSampler2DArray:
5394 case EbtISampler2DArray:
5395 case EbtUSampler2DArray:
5396 if ((isTextureGather && arguments->size() == 3u) ||
5397 (isTextureGatherOffset && arguments->size() == 4u))
5398 {
5399 componentNode = arguments->back();
5400 }
5401 break;
5402 case EbtSamplerCube:
5403 case EbtISamplerCube:
5404 case EbtUSamplerCube:
5405 ASSERT(!isTextureGatherOffset);
5406 if (arguments->size() == 3u)
5407 {
5408 componentNode = arguments->back();
5409 }
5410 break;
5411 case EbtSampler2DShadow:
5412 case EbtSampler2DArrayShadow:
5413 case EbtSamplerCubeShadow:
5414 break;
5415 default:
5416 UNREACHABLE();
5417 break;
5418 }
5419 if (componentNode)
5420 {
5421 const TIntermConstantUnion *componentConstantUnion =
5422 componentNode->getAsConstantUnion();
5423 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5424 {
5425 error(functionCall->getLine(), "Texture component must be a constant expression",
5426 name.c_str());
5427 }
5428 else
5429 {
5430 int component = componentConstantUnion->getIConst(0);
5431 if (component < 0 || component > 3)
5432 {
5433 error(functionCall->getLine(), "Component must be in the range [0;3]",
5434 name.c_str());
5435 }
5436 }
5437 }
5438 }
5439}
5440
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005441void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5442{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005443 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01005444 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005445 TIntermNode *offset = nullptr;
5446 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005447 bool useTextureGatherOffsetConstraints = false;
Olli Etuahoec9232b2017-03-27 17:01:37 +03005448 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5449 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5450 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005451 {
5452 offset = arguments->back();
5453 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005454 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005455 {
5456 // A bias parameter might follow the offset parameter.
5457 ASSERT(arguments->size() >= 3);
5458 offset = (*arguments)[2];
5459 }
Martin Radev84aa2dc2017-09-11 15:51:02 +03005460 else if (name == "textureGatherOffset")
5461 {
5462 ASSERT(arguments->size() >= 3u);
5463 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5464 ASSERT(sampler != nullptr);
5465 switch (sampler->getBasicType())
5466 {
5467 case EbtSampler2D:
5468 case EbtISampler2D:
5469 case EbtUSampler2D:
5470 case EbtSampler2DArray:
5471 case EbtISampler2DArray:
5472 case EbtUSampler2DArray:
5473 offset = (*arguments)[2];
5474 break;
5475 case EbtSampler2DShadow:
5476 case EbtSampler2DArrayShadow:
5477 offset = (*arguments)[3];
5478 break;
5479 default:
5480 UNREACHABLE();
5481 break;
5482 }
5483 useTextureGatherOffsetConstraints = true;
5484 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005485 if (offset != nullptr)
5486 {
5487 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5488 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5489 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005490 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005491 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005492 }
5493 else
5494 {
5495 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5496 size_t size = offsetConstantUnion->getType().getObjectSize();
5497 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005498 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5499 : mMinProgramTexelOffset;
5500 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5501 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005502 for (size_t i = 0u; i < size; ++i)
5503 {
5504 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005505 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005506 {
5507 std::stringstream tokenStream;
5508 tokenStream << offsetValue;
5509 std::string token = tokenStream.str();
5510 error(offset->getLine(), "Texture offset value out of valid range",
5511 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005512 }
5513 }
5514 }
5515 }
5516}
5517
Martin Radev2cc85b32016-08-05 16:22:53 +03005518// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5519void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5520{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005521 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005522 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5523
5524 if (name.compare(0, 5, "image") == 0)
5525 {
5526 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005527 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005528
Olli Etuaho485eefd2017-02-14 17:40:06 +00005529 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005530
5531 if (name.compare(5, 5, "Store") == 0)
5532 {
5533 if (memoryQualifier.readonly)
5534 {
5535 error(imageNode->getLine(),
5536 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005537 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005538 }
5539 }
5540 else if (name.compare(5, 4, "Load") == 0)
5541 {
5542 if (memoryQualifier.writeonly)
5543 {
5544 error(imageNode->getLine(),
5545 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005546 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005547 }
5548 }
5549 }
5550}
5551
5552// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5553void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5554 const TFunction *functionDefinition,
5555 const TIntermAggregate *functionCall)
5556{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005557 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005558
5559 const TIntermSequence &arguments = *functionCall->getSequence();
5560
5561 ASSERT(functionDefinition->getParamCount() == arguments.size());
5562
5563 for (size_t i = 0; i < arguments.size(); ++i)
5564 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005565 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5566 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005567 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5568 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5569
5570 if (IsImage(functionArgumentType.getBasicType()))
5571 {
5572 const TMemoryQualifier &functionArgumentMemoryQualifier =
5573 functionArgumentType.getMemoryQualifier();
5574 const TMemoryQualifier &functionParameterMemoryQualifier =
5575 functionParameterType.getMemoryQualifier();
5576 if (functionArgumentMemoryQualifier.readonly &&
5577 !functionParameterMemoryQualifier.readonly)
5578 {
5579 error(functionCall->getLine(),
5580 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005581 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005582 }
5583
5584 if (functionArgumentMemoryQualifier.writeonly &&
5585 !functionParameterMemoryQualifier.writeonly)
5586 {
5587 error(functionCall->getLine(),
5588 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005589 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005590 }
Martin Radev049edfa2016-11-11 14:35:37 +02005591
5592 if (functionArgumentMemoryQualifier.coherent &&
5593 !functionParameterMemoryQualifier.coherent)
5594 {
5595 error(functionCall->getLine(),
5596 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005597 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005598 }
5599
5600 if (functionArgumentMemoryQualifier.volatileQualifier &&
5601 !functionParameterMemoryQualifier.volatileQualifier)
5602 {
5603 error(functionCall->getLine(),
5604 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005605 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005606 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005607 }
5608 }
5609}
5610
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005611TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005612{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005613 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005614}
5615
5616TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005617 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005618 TIntermNode *thisNode,
5619 const TSourceLoc &loc)
5620{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005621 if (thisNode != nullptr)
5622 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005623 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005624 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005625
5626 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005627 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005628 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005629 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005630 }
5631 else
5632 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005633 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005634 return addNonConstructorFunctionCall(fnCall, arguments, loc);
5635 }
5636}
5637
5638TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
5639 TIntermSequence *arguments,
5640 TIntermNode *thisNode,
5641 const TSourceLoc &loc)
5642{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005643 TIntermTyped *typedThis = thisNode->getAsTyped();
5644 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5645 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5646 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
5647 // So accessing fnCall->getName() below is safe.
5648 if (fnCall->getName() != "length")
5649 {
5650 error(loc, "invalid method", fnCall->getName().c_str());
5651 }
5652 else if (!arguments->empty())
5653 {
5654 error(loc, "method takes no parameters", "length");
5655 }
5656 else if (typedThis == nullptr || !typedThis->isArray())
5657 {
5658 error(loc, "length can only be called on arrays", "length");
5659 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005660 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5661 mGeometryShaderInputPrimitiveType == EptUndefined)
5662 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08005663 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005664 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5665 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005666 else
5667 {
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005668 TIntermUnary *node = new TIntermUnary(EOpArrayLength, typedThis);
5669 node->setLine(loc);
5670 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005671 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005672 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005673}
5674
5675TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
5676 TIntermSequence *arguments,
5677 const TSourceLoc &loc)
5678{
5679 // First find by unmangled name to check whether the function name has been
5680 // hidden by a variable name or struct typename.
5681 // If a function is found, check for one with a matching argument list.
5682 bool builtIn;
5683 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
5684 if (symbol != nullptr && !symbol->isFunction())
5685 {
5686 error(loc, "function name expected", fnCall->getName().c_str());
5687 }
5688 else
5689 {
5690 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
5691 mShaderVersion, &builtIn);
5692 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005693 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005694 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
5695 }
5696 else
5697 {
5698 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005699 //
5700 // A declared function.
5701 //
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03005702 if (builtIn && fnCandidate->getExtension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005703 {
Olli Etuaho856c4972016-08-08 11:38:39 +03005704 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005705 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005706 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005707 if (builtIn && op != EOpNull)
5708 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005709 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005710 if (fnCandidate->getParamCount() == 1)
5711 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005712 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005713 TIntermNode *unaryParamNode = arguments->front();
5714 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005715 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005716 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005717 }
5718 else
5719 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005720 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005721 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005722 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005723
5724 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005725 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305726
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005727 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305728 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005729 // See if we can constant fold a built-in. Note that this may be possible
5730 // even if it is not const-qualified.
5731 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305732 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005733 else
5734 {
5735 return callNode;
5736 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005737 }
5738 }
5739 else
5740 {
5741 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005742 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005743
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005744 // If builtIn == false, the function is user defined - could be an overloaded
5745 // built-in as well.
5746 // if builtIn == true, it's a builtIn function with no op associated with it.
5747 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005748 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005749 {
Olli Etuahofe486322017-03-21 09:30:54 +00005750 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005751 checkTextureOffsetConst(callNode);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005752 checkTextureGather(callNode);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005753 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005754 }
5755 else
5756 {
Olli Etuahofe486322017-03-21 09:30:54 +00005757 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005758 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005759 }
5760
Jiajia Qinbc585152017-06-23 15:42:17 +08005761 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005762
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005763 callNode->setLine(loc);
5764
5765 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005766 }
5767 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005768 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005769
5770 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005771 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005772}
5773
Jamie Madillb98c3a82015-07-23 14:26:04 -04005774TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005775 TIntermTyped *trueExpression,
5776 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005777 const TSourceLoc &loc)
5778{
Olli Etuaho56229f12017-07-10 14:16:33 +03005779 if (!checkIsScalarBool(loc, cond))
5780 {
5781 return falseExpression;
5782 }
Olli Etuaho52901742015-04-15 13:42:45 +03005783
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005784 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005785 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005786 std::stringstream reasonStream;
5787 reasonStream << "mismatching ternary operator operand types '"
5788 << trueExpression->getCompleteString() << " and '"
5789 << falseExpression->getCompleteString() << "'";
5790 std::string reason = reasonStream.str();
5791 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005792 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005793 }
Olli Etuahode318b22016-10-25 16:18:25 +01005794 if (IsOpaqueType(trueExpression->getBasicType()))
5795 {
5796 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005797 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005798 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5799 // Note that structs containing opaque types don't need to be checked as structs are
5800 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005801 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005802 return falseExpression;
5803 }
5804
Jiajia Qinbc585152017-06-23 15:42:17 +08005805 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5806 falseExpression->getMemoryQualifier().writeonly)
5807 {
5808 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5809 return falseExpression;
5810 }
5811
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005812 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005813 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005814 // ESSL 3.00.6 section 5.7:
5815 // Ternary operator support is optional for arrays. No certainty that it works across all
5816 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5817 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005818 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005819 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005820 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005821 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005822 }
Olli Etuaho94050052017-05-08 14:17:44 +03005823 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5824 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005825 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005826 return falseExpression;
5827 }
5828
Corentin Wallez0d959252016-07-12 17:26:32 -04005829 // WebGL2 section 5.26, the following results in an error:
5830 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005831 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005832 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005833 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005834 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005835 }
5836
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005837 // Note that the node resulting from here can be a constant union without being qualified as
5838 // constant.
5839 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5840 node->setLine(loc);
5841
5842 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005843}
Olli Etuaho49300862015-02-20 14:54:49 +02005844
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005845//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005846// Parse an array of strings using yyparse.
5847//
5848// Returns 0 for success.
5849//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005850int PaParseStrings(size_t count,
5851 const char *const string[],
5852 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305853 TParseContext *context)
5854{
Yunchao He4f285442017-04-21 12:15:49 +08005855 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005856 return 1;
5857
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005858 if (glslang_initialize(context))
5859 return 1;
5860
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005861 int error = glslang_scan(count, string, length, context);
5862 if (!error)
5863 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005864
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005865 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005866
alokp@chromium.org6b495712012-06-29 00:06:58 +00005867 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005868}
Jamie Madill45bcc782016-11-07 13:58:48 -05005869
5870} // namespace sh