blob: 390439df8393ca478a02ca8a9f78613e9680f5ce [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
Martin Radev2cc85b32016-08-05 16:22:53 +0300911void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
912 TQualifier qualifier,
913 const TType &type)
914{
Martin Radev2cc85b32016-08-05 16:22:53 +0300915 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800916 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530917 {
jchen10cc2a10e2017-05-03 14:05:12 +0800918 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000919 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000920}
921
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000922// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300923unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000924{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530925 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000926
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200927 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
928 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
929 // fold as array size.
930 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000931 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000932 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300933 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000934 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000935
Olli Etuaho856c4972016-08-08 11:38:39 +0300936 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400937
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000938 if (constant->getBasicType() == EbtUInt)
939 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300940 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000941 }
942 else
943 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300944 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000945
Olli Etuaho856c4972016-08-08 11:38:39 +0300946 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000947 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400948 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300949 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000950 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400951
Olli Etuaho856c4972016-08-08 11:38:39 +0300952 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400953 }
954
Olli Etuaho856c4972016-08-08 11:38:39 +0300955 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400956 {
957 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300958 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400959 }
960
961 // The size of arrays is restricted here to prevent issues further down the
962 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
963 // 4096 registers so this should be reasonable even for aggressively optimizable code.
964 const unsigned int sizeLimit = 65536;
965
Olli Etuaho856c4972016-08-08 11:38:39 +0300966 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400967 {
968 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300969 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000970 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300971
972 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000973}
974
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000975// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300976bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
977 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000978{
Olli Etuaho8a176262016-08-16 14:23:01 +0300979 if ((elementQualifier.qualifier == EvqAttribute) ||
980 (elementQualifier.qualifier == EvqVertexIn) ||
981 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300982 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400983 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300984 TType(elementQualifier).getQualifierString());
985 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000986 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000987
Olli Etuaho8a176262016-08-16 14:23:01 +0300988 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000989}
990
Olli Etuaho8a176262016-08-16 14:23:01 +0300991// See if this element type can be formed into an array.
Olli Etuahoe0803872017-08-23 15:30:23 +0300992bool TParseContext::checkArrayElementIsNotArray(const TSourceLoc &line,
993 const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000994{
Olli Etuaho8a176262016-08-16 14:23:01 +0300995 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -0400996 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300997 error(line, "cannot declare arrays of arrays",
998 TType(elementType).getCompleteString().c_str());
999 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001000 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001001 return true;
1002}
1003
1004// Check if this qualified element type can be formed into an array. This is only called when array
1005// brackets are associated with an identifier in a declaration, like this:
1006// float a[2];
1007// Similar checks are done in addFullySpecifiedType for array declarations where the array brackets
1008// are associated with the type, like this:
1009// float[2] a;
1010bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
1011 const TPublicType &elementType)
1012{
1013 if (!checkArrayElementIsNotArray(indexLocation, elementType))
1014 {
1015 return false;
1016 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001017 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
1018 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
1019 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +03001020 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +03001021 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +03001022 {
Olli Etuahoe0803872017-08-23 15:30:23 +03001023 error(indexLocation, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001024 TType(elementType).getCompleteString().c_str());
1025 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +03001026 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001027 return checkIsValidQualifierForArray(indexLocation, elementType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001028}
1029
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001030// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001031void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
1032 const TString &identifier,
1033 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001034{
Olli Etuaho3739d232015-04-08 12:23:44 +03001035 ASSERT(type != nullptr);
1036 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001037 {
1038 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +03001039 type->qualifier = EvqTemporary;
1040
1041 // Generate informative error messages for ESSL1.
1042 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001043 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001044 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301045 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001046 "structures containing arrays may not be declared constant since they cannot be "
1047 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +05301048 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001049 }
1050 else
1051 {
1052 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
1053 }
Olli Etuaho383b7912016-08-05 11:22:59 +03001054 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001055 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001056 if (type->isUnsizedArray())
1057 {
1058 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +03001059 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001060}
1061
Olli Etuaho2935c582015-04-08 14:32:06 +03001062// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001063// and update the symbol table.
1064//
Olli Etuaho2935c582015-04-08 14:32:06 +03001065// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001066//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001067bool TParseContext::declareVariable(const TSourceLoc &line,
1068 const TString &identifier,
1069 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001070 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001071{
Olli Etuaho2935c582015-04-08 14:32:06 +03001072 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001073
Olli Etuaho43364892017-02-13 16:00:12 +00001074 checkBindingIsValid(line, type);
1075
Olli Etuaho856c4972016-08-08 11:38:39 +03001076 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001077
Olli Etuaho2935c582015-04-08 14:32:06 +03001078 // gl_LastFragData may be redeclared with a new precision qualifier
1079 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1080 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001081 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1082 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001083 if (type.isArrayOfArrays())
1084 {
1085 error(line, "redeclaration of gl_LastFragData as an array of arrays",
1086 identifier.c_str());
1087 return false;
1088 }
1089 else if (static_cast<int>(type.getOutermostArraySize()) ==
1090 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001091 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001092 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001093 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001094 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001095 }
1096 }
1097 else
1098 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001099 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1100 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001101 return false;
1102 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001103 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001104
Olli Etuaho8a176262016-08-16 14:23:01 +03001105 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001106 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001107
Olli Etuaho0f684632017-07-13 12:42:15 +03001108 (*variable) = symbolTable.declareVariable(&identifier, type);
1109 if (!(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001110 {
1111 error(line, "redefinition", identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001112 return false;
1113 }
1114
Olli Etuaho8a176262016-08-16 14:23:01 +03001115 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001116 return false;
1117
1118 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001119}
1120
Martin Radev70866b82016-07-22 15:27:42 +03001121void TParseContext::checkIsParameterQualifierValid(
1122 const TSourceLoc &line,
1123 const TTypeQualifierBuilder &typeQualifierBuilder,
1124 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301125{
Olli Etuahocce89652017-06-19 16:04:09 +03001126 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001127 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001128
1129 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301130 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001131 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1132 }
1133
1134 if (!IsImage(type->getBasicType()))
1135 {
Olli Etuaho43364892017-02-13 16:00:12 +00001136 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001137 }
1138 else
1139 {
1140 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001141 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001142
Martin Radev70866b82016-07-22 15:27:42 +03001143 type->setQualifier(typeQualifier.qualifier);
1144
1145 if (typeQualifier.precision != EbpUndefined)
1146 {
1147 type->setPrecision(typeQualifier.precision);
1148 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001149}
1150
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001151bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001152{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001153 ASSERT(extension != TExtension::UNDEFINED);
Jamie Madillb98c3a82015-07-23 14:26:04 -04001154 const TExtensionBehavior &extBehavior = extensionBehavior();
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001155 TExtensionBehavior::const_iterator iter = extBehavior.find(extension);
Arun Patole7e7e68d2015-05-22 12:02:25 +05301156 if (iter == extBehavior.end())
1157 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001158 error(line, "extension is not supported", GetExtensionNameString(extension));
Olli Etuaho8a176262016-08-16 14:23:01 +03001159 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001160 }
zmo@google.comf5450912011-09-09 01:37:19 +00001161 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +05301162 if (iter->second == EBhDisable || iter->second == EBhUndefined)
1163 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001164 error(line, "extension is disabled", GetExtensionNameString(extension));
Olli Etuaho8a176262016-08-16 14:23:01 +03001165 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001166 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301167 if (iter->second == EBhWarn)
1168 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001169 warning(line, "extension is being used", GetExtensionNameString(extension));
Olli Etuaho8a176262016-08-16 14:23:01 +03001170 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001171 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001172
Olli Etuaho8a176262016-08-16 14:23:01 +03001173 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001174}
1175
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001176// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1177// compile-time or link-time errors are the same whether or not the declaration is empty".
1178// This function implements all the checks that are done on qualifiers regardless of if the
1179// declaration is empty.
1180void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1181 const sh::TLayoutQualifier &layoutQualifier,
1182 const TSourceLoc &location)
1183{
1184 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1185 {
1186 error(location, "Shared memory declarations cannot have layout specified", "layout");
1187 }
1188
1189 if (layoutQualifier.matrixPacking != EmpUnspecified)
1190 {
1191 error(location, "layout qualifier only valid for interface blocks",
1192 getMatrixPackingString(layoutQualifier.matrixPacking));
1193 return;
1194 }
1195
1196 if (layoutQualifier.blockStorage != EbsUnspecified)
1197 {
1198 error(location, "layout qualifier only valid for interface blocks",
1199 getBlockStorageString(layoutQualifier.blockStorage));
1200 return;
1201 }
1202
1203 if (qualifier == EvqFragmentOut)
1204 {
1205 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1206 {
1207 error(location, "invalid layout qualifier combination", "yuv");
1208 return;
1209 }
1210 }
1211 else
1212 {
1213 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1214 }
1215
Olli Etuaho95468d12017-05-04 11:14:34 +03001216 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1217 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001218 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1219 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001220 {
1221 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1222 }
1223
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001224 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001225 if (mShaderVersion >= 310)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001226 {
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001227 canHaveLocation = canHaveLocation || qualifier == EvqUniform || IsVarying(qualifier);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001228 // We're not checking whether the uniform location is in range here since that depends on
1229 // the type of the variable.
1230 // The type can only be fully determined for non-empty declarations.
1231 }
1232 if (!canHaveLocation)
1233 {
1234 checkLocationIsNotSpecified(location, layoutQualifier);
1235 }
1236}
1237
jchen104cdac9e2017-05-08 11:01:20 +08001238void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1239 const TSourceLoc &location)
1240{
1241 if (publicType.precision != EbpHigh)
1242 {
1243 error(location, "Can only be highp", "atomic counter");
1244 }
1245 // dEQP enforces compile error if location is specified. See uniform_location.test.
1246 if (publicType.layoutQualifier.location != -1)
1247 {
1248 error(location, "location must not be set for atomic_uint", "layout");
1249 }
1250 if (publicType.layoutQualifier.binding == -1)
1251 {
1252 error(location, "no binding specified", "atomic counter");
1253 }
1254}
1255
Martin Radevb8b01222016-11-20 23:25:53 +02001256void TParseContext::emptyDeclarationErrorCheck(const TPublicType &publicType,
1257 const TSourceLoc &location)
1258{
1259 if (publicType.isUnsizedArray())
1260 {
1261 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1262 // error. It is assumed that this applies to empty declarations as well.
1263 error(location, "empty array declaration needs to specify a size", "");
1264 }
Martin Radevb8b01222016-11-20 23:25:53 +02001265}
1266
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001267// These checks are done for all declarations that are non-empty. They're done for non-empty
1268// declarations starting a declarator list, and declarators that follow an empty declaration.
1269void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1270 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001271{
Olli Etuahofa33d582015-04-09 14:33:12 +03001272 switch (publicType.qualifier)
1273 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001274 case EvqVaryingIn:
1275 case EvqVaryingOut:
1276 case EvqAttribute:
1277 case EvqVertexIn:
1278 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001279 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001280 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001281 {
1282 error(identifierLocation, "cannot be used with a structure",
1283 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001284 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001285 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001286 break;
1287 case EvqBuffer:
1288 if (publicType.getBasicType() != EbtInterfaceBlock)
1289 {
1290 error(identifierLocation,
1291 "cannot declare buffer variables at global scope(outside a block)",
1292 getQualifierString(publicType.qualifier));
1293 return;
1294 }
1295 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001296 default:
1297 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001298 }
jchen10cc2a10e2017-05-03 14:05:12 +08001299 std::string reason(getBasicString(publicType.getBasicType()));
1300 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001301 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001302 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001303 {
1304 return;
1305 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001306
Andrei Volykhina5527072017-03-22 16:46:30 +03001307 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1308 publicType.qualifier != EvqConst) &&
1309 publicType.getBasicType() == EbtYuvCscStandardEXT)
1310 {
1311 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1312 getQualifierString(publicType.qualifier));
1313 return;
1314 }
1315
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001316 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1317 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001318 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1319 // But invalid shaders may still reach here with an unsized array declaration.
1320 if (!publicType.isUnsizedArray())
1321 {
1322 TType type(publicType);
1323 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1324 publicType.layoutQualifier);
1325 }
1326 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001327
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001328 // check for layout qualifier issues
1329 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001330
Martin Radev2cc85b32016-08-05 16:22:53 +03001331 if (IsImage(publicType.getBasicType()))
1332 {
1333
1334 switch (layoutQualifier.imageInternalFormat)
1335 {
1336 case EiifRGBA32F:
1337 case EiifRGBA16F:
1338 case EiifR32F:
1339 case EiifRGBA8:
1340 case EiifRGBA8_SNORM:
1341 if (!IsFloatImage(publicType.getBasicType()))
1342 {
1343 error(identifierLocation,
1344 "internal image format requires a floating image type",
1345 getBasicString(publicType.getBasicType()));
1346 return;
1347 }
1348 break;
1349 case EiifRGBA32I:
1350 case EiifRGBA16I:
1351 case EiifRGBA8I:
1352 case EiifR32I:
1353 if (!IsIntegerImage(publicType.getBasicType()))
1354 {
1355 error(identifierLocation,
1356 "internal image format requires an integer image type",
1357 getBasicString(publicType.getBasicType()));
1358 return;
1359 }
1360 break;
1361 case EiifRGBA32UI:
1362 case EiifRGBA16UI:
1363 case EiifRGBA8UI:
1364 case EiifR32UI:
1365 if (!IsUnsignedImage(publicType.getBasicType()))
1366 {
1367 error(identifierLocation,
1368 "internal image format requires an unsigned image type",
1369 getBasicString(publicType.getBasicType()));
1370 return;
1371 }
1372 break;
1373 case EiifUnspecified:
1374 error(identifierLocation, "layout qualifier", "No image internal format specified");
1375 return;
1376 default:
1377 error(identifierLocation, "layout qualifier", "unrecognized token");
1378 return;
1379 }
1380
1381 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1382 switch (layoutQualifier.imageInternalFormat)
1383 {
1384 case EiifR32F:
1385 case EiifR32I:
1386 case EiifR32UI:
1387 break;
1388 default:
1389 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1390 {
1391 error(identifierLocation, "layout qualifier",
1392 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1393 "image variables must be qualified readonly and/or writeonly");
1394 return;
1395 }
1396 break;
1397 }
1398 }
1399 else
1400 {
Olli Etuaho43364892017-02-13 16:00:12 +00001401 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001402 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1403 }
jchen104cdac9e2017-05-08 11:01:20 +08001404
1405 if (IsAtomicCounter(publicType.getBasicType()))
1406 {
1407 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1408 }
1409 else
1410 {
1411 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1412 }
Olli Etuaho43364892017-02-13 16:00:12 +00001413}
Martin Radev2cc85b32016-08-05 16:22:53 +03001414
Olli Etuaho43364892017-02-13 16:00:12 +00001415void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1416{
1417 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001418 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1419 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1420 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1421 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1422 // when it comes to which shaders are accepted by the compiler.
1423 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001424 if (IsImage(type.getBasicType()))
1425 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001426 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1427 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001428 }
1429 else if (IsSampler(type.getBasicType()))
1430 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001431 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1432 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001433 }
jchen104cdac9e2017-05-08 11:01:20 +08001434 else if (IsAtomicCounter(type.getBasicType()))
1435 {
1436 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1437 }
Olli Etuaho43364892017-02-13 16:00:12 +00001438 else
1439 {
1440 ASSERT(!IsOpaqueType(type.getBasicType()));
1441 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001442 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001443}
1444
Olli Etuaho856c4972016-08-08 11:38:39 +03001445void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1446 const TString &layoutQualifierName,
1447 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001448{
1449
1450 if (mShaderVersion < versionRequired)
1451 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001452 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001453 }
1454}
1455
Olli Etuaho856c4972016-08-08 11:38:39 +03001456bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1457 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001458{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001459 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001460 for (size_t i = 0u; i < localSize.size(); ++i)
1461 {
1462 if (localSize[i] != -1)
1463 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001464 error(location,
1465 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1466 "global layout declaration",
1467 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001468 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001469 }
1470 }
1471
Olli Etuaho8a176262016-08-16 14:23:01 +03001472 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001473}
1474
Olli Etuaho43364892017-02-13 16:00:12 +00001475void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001476 TLayoutImageInternalFormat internalFormat)
1477{
1478 if (internalFormat != EiifUnspecified)
1479 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001480 error(location, "invalid layout qualifier: only valid when used with images",
1481 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001482 }
Olli Etuaho43364892017-02-13 16:00:12 +00001483}
1484
1485void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1486{
1487 if (binding != -1)
1488 {
1489 error(location,
1490 "invalid layout qualifier: only valid when used with opaque types or blocks",
1491 "binding");
1492 }
1493}
1494
jchen104cdac9e2017-05-08 11:01:20 +08001495void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1496{
1497 if (offset != -1)
1498 {
1499 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1500 "offset");
1501 }
1502}
1503
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001504void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1505 int binding,
1506 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001507{
1508 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001509 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001510 {
1511 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1512 }
1513}
1514
1515void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1516 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001517 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001518{
1519 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001520 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001521 {
1522 error(location, "sampler binding greater than maximum texture units", "binding");
1523 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001524}
1525
Jiajia Qinbc585152017-06-23 15:42:17 +08001526void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1527 const TQualifier &qualifier,
1528 int binding,
1529 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001530{
1531 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001532 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001533 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001534 if (binding + size > mMaxUniformBufferBindings)
1535 {
1536 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1537 "binding");
1538 }
1539 }
1540 else if (qualifier == EvqBuffer)
1541 {
1542 if (binding + size > mMaxShaderStorageBufferBindings)
1543 {
1544 error(location,
1545 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1546 "binding");
1547 }
jchen10af713a22017-04-19 09:10:56 +08001548 }
1549}
jchen104cdac9e2017-05-08 11:01:20 +08001550void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1551{
1552 if (binding >= mMaxAtomicCounterBindings)
1553 {
1554 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1555 "binding");
1556 }
1557}
jchen10af713a22017-04-19 09:10:56 +08001558
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001559void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1560 int objectLocationCount,
1561 const TLayoutQualifier &layoutQualifier)
1562{
1563 int loc = layoutQualifier.location;
1564 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1565 {
1566 error(location, "Uniform location out of range", "location");
1567 }
1568}
1569
Andrei Volykhina5527072017-03-22 16:46:30 +03001570void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1571{
1572 if (yuv != false)
1573 {
1574 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1575 }
1576}
1577
Jiajia Qinbc585152017-06-23 15:42:17 +08001578void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1579 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001580{
1581 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1582 {
1583 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001584 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1585 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1586 qual == EvqInOut || qual == EvqConstReadOnly))
1587 {
1588 if (argument->getMemoryQualifier().writeonly)
1589 {
1590 error(argument->getLine(),
1591 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
1592 fnCall->getFunctionSymbolInfo()->getName().c_str());
1593 return;
1594 }
1595 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001596 if (qual == EvqOut || qual == EvqInOut)
1597 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001598 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001599 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001600 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001601 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuahoec9232b2017-03-27 17:01:37 +03001602 fnCall->getFunctionSymbolInfo()->getName().c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001603 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001604 }
1605 }
1606 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001607}
1608
Martin Radev70866b82016-07-22 15:27:42 +03001609void TParseContext::checkInvariantVariableQualifier(bool invariant,
1610 const TQualifier qualifier,
1611 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001612{
Martin Radev70866b82016-07-22 15:27:42 +03001613 if (!invariant)
1614 return;
1615
1616 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001617 {
Martin Radev70866b82016-07-22 15:27:42 +03001618 // input variables in the fragment shader can be also qualified as invariant
1619 if (!sh::CanBeInvariantESSL1(qualifier))
1620 {
1621 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1622 }
1623 }
1624 else
1625 {
1626 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1627 {
1628 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1629 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001630 }
1631}
1632
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001633bool TParseContext::supportsExtension(TExtension extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001634{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001635 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001636 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1637 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001638}
1639
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001640bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001641{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001642 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001643}
1644
Jamie Madillb98c3a82015-07-23 14:26:04 -04001645void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1646 const char *extName,
1647 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001648{
1649 pp::SourceLocation srcLoc;
1650 srcLoc.file = loc.first_file;
1651 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001652 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001653}
1654
Jamie Madillb98c3a82015-07-23 14:26:04 -04001655void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1656 const char *name,
1657 const char *value,
1658 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001659{
1660 pp::SourceLocation srcLoc;
1661 srcLoc.file = loc.first_file;
1662 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001663 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001664}
1665
Martin Radev4c4c8e72016-08-04 12:25:34 +03001666sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001667{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001668 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001669 for (size_t i = 0u; i < result.size(); ++i)
1670 {
1671 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1672 {
1673 result[i] = 1;
1674 }
1675 else
1676 {
1677 result[i] = mComputeShaderLocalSize[i];
1678 }
1679 }
1680 return result;
1681}
1682
Olli Etuaho56229f12017-07-10 14:16:33 +03001683TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1684 const TSourceLoc &line)
1685{
1686 TIntermConstantUnion *node = new TIntermConstantUnion(
1687 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1688 node->setLine(line);
1689 return node;
1690}
1691
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001692/////////////////////////////////////////////////////////////////////////////////
1693//
1694// Non-Errors.
1695//
1696/////////////////////////////////////////////////////////////////////////////////
1697
Jamie Madill5c097022014-08-20 16:38:32 -04001698const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1699 const TString *name,
1700 const TSymbol *symbol)
1701{
Jamie Madill5c097022014-08-20 16:38:32 -04001702 if (!symbol)
1703 {
1704 error(location, "undeclared identifier", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001705 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001706 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001707
1708 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001709 {
1710 error(location, "variable expected", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001711 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001712 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001713
1714 const TVariable *variable = static_cast<const TVariable *>(symbol);
1715
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001716 if (variable->getExtension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001717 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001718 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001719 }
1720
Olli Etuaho0f684632017-07-13 12:42:15 +03001721 // Reject shaders using both gl_FragData and gl_FragColor
1722 TQualifier qualifier = variable->getType().getQualifier();
1723 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001724 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001725 mUsesFragData = true;
1726 }
1727 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1728 {
1729 mUsesFragColor = true;
1730 }
1731 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1732 {
1733 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001734 }
1735
Olli Etuaho0f684632017-07-13 12:42:15 +03001736 // This validation is not quite correct - it's only an error to write to
1737 // both FragData and FragColor. For simplicity, and because users shouldn't
1738 // be rewarded for reading from undefined varaibles, return an error
1739 // if they are both referenced, rather than assigned.
1740 if (mUsesFragData && mUsesFragColor)
1741 {
1742 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1743 if (mUsesSecondaryOutputs)
1744 {
1745 errorMessage =
1746 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1747 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1748 }
1749 error(location, errorMessage, name->c_str());
1750 }
1751
1752 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1753 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1754 qualifier == EvqWorkGroupSize)
1755 {
1756 error(location,
1757 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1758 "gl_WorkGroupSize");
1759 }
Jamie Madill5c097022014-08-20 16:38:32 -04001760 return variable;
1761}
1762
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001763TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1764 const TString *name,
1765 const TSymbol *symbol)
1766{
1767 const TVariable *variable = getNamedVariable(location, name, symbol);
1768
Olli Etuaho0f684632017-07-13 12:42:15 +03001769 if (!variable)
1770 {
1771 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1772 node->setLine(location);
1773 return node;
1774 }
1775
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001776 const TType &variableType = variable->getType();
Olli Etuaho56229f12017-07-10 14:16:33 +03001777 TIntermTyped *node = nullptr;
1778
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001779 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001780 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001781 const TConstantUnion *constArray = variable->getConstPointer();
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001782 node = new TIntermConstantUnion(constArray, variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001783 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001784 else if (variableType.getQualifier() == EvqWorkGroupSize && mComputeShaderLocalSizeDeclared)
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001785 {
1786 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1787 // needs to be added to the AST as a constant and not as a symbol.
1788 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1789 TConstantUnion *constArray = new TConstantUnion[3];
1790 for (size_t i = 0; i < 3; ++i)
1791 {
1792 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1793 }
1794
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001795 ASSERT(variableType.getBasicType() == EbtUInt);
1796 ASSERT(variableType.getObjectSize() == 3);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001797
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001798 TType type(variableType);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001799 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001800 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001801 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001802 else if ((mGeometryShaderInputPrimitiveType != EptUndefined) &&
1803 (variableType.getQualifier() == EvqPerVertexIn))
Jiawei Shaod8105a02017-08-08 09:54:36 +08001804 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001805 ASSERT(mGeometryShaderInputArraySize > 0u);
1806
1807 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), variableType);
1808 node->getTypePointer()->setArraySize(0, mGeometryShaderInputArraySize);
Jiawei Shaod8105a02017-08-08 09:54:36 +08001809 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001810 else
1811 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001812 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001813 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001814 ASSERT(node != nullptr);
1815 node->setLine(location);
1816 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001817}
1818
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001819// Initializers show up in several places in the grammar. Have one set of
1820// code to handle them here.
1821//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001822// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001823bool TParseContext::executeInitializer(const TSourceLoc &line,
1824 const TString &identifier,
1825 const TPublicType &pType,
1826 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001827 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001828{
Olli Etuaho13389b62016-10-16 11:48:18 +01001829 ASSERT(initNode != nullptr);
1830 ASSERT(*initNode == nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001831 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001832
Olli Etuaho2935c582015-04-08 14:32:06 +03001833 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001834 if (type.isUnsizedArray())
1835 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001836 // In case initializer is not an array or type has more dimensions than initializer, this
1837 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1838 // actually is an array or not. Having a non-array initializer for an unsized array will
1839 // result in an error later, so we don't generate an error message here.
1840 type.sizeUnsizedArrays(initializer->getType().getArraySizes());
Olli Etuaho376f1b52015-04-13 13:23:41 +03001841 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001842 if (!declareVariable(line, identifier, type, &variable))
1843 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001844 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001845 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001846
Olli Etuahob0c645e2015-05-12 14:25:36 +03001847 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001848 if (symbolTable.atGlobalLevel() &&
1849 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001850 {
1851 // Error message does not completely match behavior with ESSL 1.00, but
1852 // we want to steer developers towards only using constant expressions.
1853 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001854 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001855 }
1856 if (globalInitWarning)
1857 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001858 warning(
1859 line,
1860 "global variable initializers should be constant expressions "
1861 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1862 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001863 }
1864
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001865 //
1866 // identifier must be of type constant, a global, or a temporary
1867 //
1868 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301869 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1870 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001871 error(line, " cannot initialize this type of qualifier ",
1872 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001873 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001874 }
1875 //
1876 // test for and propagate constant
1877 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001878
Arun Patole7e7e68d2015-05-22 12:02:25 +05301879 if (qualifier == EvqConst)
1880 {
1881 if (qualifier != initializer->getType().getQualifier())
1882 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001883 std::stringstream reasonStream;
1884 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1885 << "'";
1886 std::string reason = reasonStream.str();
1887 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001888 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001889 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001890 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301891 if (type != initializer->getType())
1892 {
1893 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001894 variable->getType().getQualifierString());
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 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001898
1899 // Save the constant folded value to the variable if possible. For example array
1900 // initializers are not folded, since that way copying the array literal to multiple places
1901 // in the shader is avoided.
1902 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1903 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301904 if (initializer->getAsConstantUnion())
1905 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001906 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001907 ASSERT(*initNode == nullptr);
1908 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301909 }
1910 else if (initializer->getAsSymbolNode())
1911 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001912 const TSymbol *symbol =
1913 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1914 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001915
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001916 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001917 if (constArray)
1918 {
1919 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001920 ASSERT(*initNode == nullptr);
1921 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001922 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001923 }
1924 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001925
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001926 TIntermSymbol *intermSymbol =
1927 new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
1928 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001929 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1930 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001931 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001932 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001933 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001934 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001935
Olli Etuaho914b79a2017-06-19 16:03:19 +03001936 return true;
1937}
1938
1939TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
1940 const TString &identifier,
1941 TIntermTyped *initializer,
1942 const TSourceLoc &loc)
1943{
1944 checkIsScalarBool(loc, pType);
1945 TIntermBinary *initNode = nullptr;
1946 if (executeInitializer(loc, identifier, pType, initializer, &initNode))
1947 {
1948 // The initializer is valid. The init condition needs to have a node - either the
1949 // initializer node, or a constant node in case the initialized variable is const and won't
1950 // be recorded in the AST.
1951 if (initNode == nullptr)
1952 {
1953 return initializer;
1954 }
1955 else
1956 {
1957 TIntermDeclaration *declaration = new TIntermDeclaration();
1958 declaration->appendDeclarator(initNode);
1959 return declaration;
1960 }
1961 }
1962 return nullptr;
1963}
1964
1965TIntermNode *TParseContext::addLoop(TLoopType type,
1966 TIntermNode *init,
1967 TIntermNode *cond,
1968 TIntermTyped *expr,
1969 TIntermNode *body,
1970 const TSourceLoc &line)
1971{
1972 TIntermNode *node = nullptr;
1973 TIntermTyped *typedCond = nullptr;
1974 if (cond)
1975 {
1976 typedCond = cond->getAsTyped();
1977 }
1978 if (cond == nullptr || typedCond)
1979 {
Olli Etuahocce89652017-06-19 16:04:09 +03001980 if (type == ELoopDoWhile)
1981 {
1982 checkIsScalarBool(line, typedCond);
1983 }
1984 // In the case of other loops, it was checked before that the condition is a scalar boolean.
1985 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
1986 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
1987 !typedCond->isVector()));
1988
Olli Etuaho3ec75682017-07-05 17:02:55 +03001989 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03001990 node->setLine(line);
1991 return node;
1992 }
1993
Olli Etuahocce89652017-06-19 16:04:09 +03001994 ASSERT(type != ELoopDoWhile);
1995
Olli Etuaho914b79a2017-06-19 16:03:19 +03001996 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
1997 ASSERT(declaration);
1998 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
1999 ASSERT(declarator->getLeft()->getAsSymbolNode());
2000
2001 // The condition is a declaration. In the AST representation we don't support declarations as
2002 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2003 // the loop.
2004 TIntermBlock *block = new TIntermBlock();
2005
2006 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2007 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2008 block->appendStatement(declareCondition);
2009
2010 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2011 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002012 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002013 block->appendStatement(loop);
2014 loop->setLine(line);
2015 block->setLine(line);
2016 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002017}
2018
Olli Etuahocce89652017-06-19 16:04:09 +03002019TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2020 TIntermNodePair code,
2021 const TSourceLoc &loc)
2022{
Olli Etuaho56229f12017-07-10 14:16:33 +03002023 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002024
2025 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002026 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002027 {
2028 if (cond->getAsConstantUnion()->getBConst(0) == true)
2029 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002030 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002031 }
2032 else
2033 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002034 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002035 }
2036 }
2037
Olli Etuaho3ec75682017-07-05 17:02:55 +03002038 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002039 node->setLine(loc);
2040
2041 return node;
2042}
2043
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002044void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2045{
2046 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2047 typeSpecifier->getBasicType());
2048
2049 if (mShaderVersion < 300 && typeSpecifier->array)
2050 {
2051 error(typeSpecifier->getLine(), "not supported", "first-class array");
2052 typeSpecifier->clearArrayness();
2053 }
2054}
2055
Martin Radev70866b82016-07-22 15:27:42 +03002056TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302057 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002058{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002059 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002060
Martin Radev70866b82016-07-22 15:27:42 +03002061 TPublicType returnType = typeSpecifier;
2062 returnType.qualifier = typeQualifier.qualifier;
2063 returnType.invariant = typeQualifier.invariant;
2064 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002065 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002066 returnType.precision = typeSpecifier.precision;
2067
2068 if (typeQualifier.precision != EbpUndefined)
2069 {
2070 returnType.precision = typeQualifier.precision;
2071 }
2072
Martin Radev4a9cd802016-09-01 16:51:51 +03002073 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2074 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002075
Martin Radev4a9cd802016-09-01 16:51:51 +03002076 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2077 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002078
Martin Radev4a9cd802016-09-01 16:51:51 +03002079 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002080
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002081 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002082 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002083 if (typeSpecifier.array)
2084 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002085 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002086 returnType.clearArrayness();
2087 }
2088
Martin Radev70866b82016-07-22 15:27:42 +03002089 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002090 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002091 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002092 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002093 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002094 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002095
Martin Radev70866b82016-07-22 15:27:42 +03002096 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002097 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002098 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002099 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002100 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002101 }
2102 }
2103 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002104 {
Martin Radev70866b82016-07-22 15:27:42 +03002105 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002106 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002107 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002108 }
Martin Radev70866b82016-07-22 15:27:42 +03002109 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2110 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002111 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002112 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2113 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002114 }
Martin Radev70866b82016-07-22 15:27:42 +03002115 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002116 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002117 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002118 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002119 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002120 }
2121
2122 return returnType;
2123}
2124
Olli Etuaho856c4972016-08-08 11:38:39 +03002125void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2126 const TPublicType &type,
2127 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002128{
2129 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002130 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002131 {
2132 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002133 }
2134
2135 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2136 switch (qualifier)
2137 {
2138 case EvqVertexIn:
2139 // ESSL 3.00 section 4.3.4
2140 if (type.array)
2141 {
2142 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002143 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002144 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002145 return;
2146 case EvqFragmentOut:
2147 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002148 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002149 {
2150 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002151 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002152 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002153 return;
2154 default:
2155 break;
2156 }
2157
2158 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2159 // restrictions.
2160 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002161 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2162 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002163 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2164 {
2165 error(qualifierLocation, "must use 'flat' interpolation here",
2166 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002167 }
2168
Martin Radev4a9cd802016-09-01 16:51:51 +03002169 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002170 {
2171 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2172 // These restrictions are only implied by the ESSL 3.00 spec, but
2173 // the ESSL 3.10 spec lists these restrictions explicitly.
2174 if (type.array)
2175 {
2176 error(qualifierLocation, "cannot be an array of structures",
2177 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002178 }
2179 if (type.isStructureContainingArrays())
2180 {
2181 error(qualifierLocation, "cannot be a structure containing an array",
2182 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002183 }
2184 if (type.isStructureContainingType(EbtStruct))
2185 {
2186 error(qualifierLocation, "cannot be a structure containing a structure",
2187 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002188 }
2189 if (type.isStructureContainingType(EbtBool))
2190 {
2191 error(qualifierLocation, "cannot be a structure containing a bool",
2192 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002193 }
2194 }
2195}
2196
Martin Radev2cc85b32016-08-05 16:22:53 +03002197void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2198{
2199 if (qualifier.getType() == QtStorage)
2200 {
2201 const TStorageQualifierWrapper &storageQualifier =
2202 static_cast<const TStorageQualifierWrapper &>(qualifier);
2203 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2204 !symbolTable.atGlobalLevel())
2205 {
2206 error(storageQualifier.getLine(),
2207 "Local variables can only use the const storage qualifier.",
2208 storageQualifier.getQualifierString().c_str());
2209 }
2210 }
2211}
2212
Olli Etuaho43364892017-02-13 16:00:12 +00002213void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002214 const TSourceLoc &location)
2215{
Jiajia Qinbc585152017-06-23 15:42:17 +08002216 const std::string reason(
2217 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2218 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002219 if (memoryQualifier.readonly)
2220 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002221 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002222 }
2223 if (memoryQualifier.writeonly)
2224 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002225 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002226 }
Martin Radev049edfa2016-11-11 14:35:37 +02002227 if (memoryQualifier.coherent)
2228 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002229 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002230 }
2231 if (memoryQualifier.restrictQualifier)
2232 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002233 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002234 }
2235 if (memoryQualifier.volatileQualifier)
2236 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002237 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002238 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002239}
2240
jchen104cdac9e2017-05-08 11:01:20 +08002241// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2242// intermediate tree.
2243void TParseContext::checkAtomicCounterOffsetIsNotOverlapped(TPublicType &publicType,
2244 size_t size,
2245 bool forceAppend,
2246 const TSourceLoc &loc,
2247 TType &type)
2248{
2249 auto &bindingState = mAtomicCounterBindingStates[publicType.layoutQualifier.binding];
2250 int offset;
2251 if (publicType.layoutQualifier.offset == -1 || forceAppend)
2252 {
2253 offset = bindingState.appendSpan(size);
2254 }
2255 else
2256 {
2257 offset = bindingState.insertSpan(publicType.layoutQualifier.offset, size);
2258 }
2259 if (offset == -1)
2260 {
2261 error(loc, "Offset overlapping", "atomic counter");
2262 return;
2263 }
2264 TLayoutQualifier qualifier = type.getLayoutQualifier();
2265 qualifier.offset = offset;
2266 type.setLayoutQualifier(qualifier);
2267}
2268
Olli Etuaho13389b62016-10-16 11:48:18 +01002269TIntermDeclaration *TParseContext::parseSingleDeclaration(
2270 TPublicType &publicType,
2271 const TSourceLoc &identifierOrTypeLocation,
2272 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002273{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002274 TType type(publicType);
2275 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2276 mDirectiveHandler.pragma().stdgl.invariantAll)
2277 {
2278 TQualifier qualifier = type.getQualifier();
2279
2280 // The directive handler has already taken care of rejecting invalid uses of this pragma
2281 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2282 // affected variable declarations:
2283 //
2284 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2285 // elsewhere, in TranslatorGLSL.)
2286 //
2287 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2288 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2289 // the way this is currently implemented we have to enable this compiler option before
2290 // parsing the shader and determining the shading language version it uses. If this were
2291 // implemented as a post-pass, the workaround could be more targeted.
2292 //
2293 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2294 // the specification, but there are desktop OpenGL drivers that expect that this is the
2295 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2296 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2297 {
2298 type.setInvariant(true);
2299 }
2300 }
2301
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002302 if (IsGeometryShaderInput(mShaderType, type.getQualifier()))
2303 {
2304 error(identifierOrTypeLocation,
2305 "Geometry shader input varying variable must be declared as an array",
2306 identifier.c_str());
2307 }
2308
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002309 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2310 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002311
Olli Etuahobab4c082015-04-24 16:38:49 +03002312 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002313 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002314
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002315 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002316 if (emptyDeclaration)
2317 {
Martin Radevb8b01222016-11-20 23:25:53 +02002318 emptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002319 // In most cases we don't need to create a symbol node for an empty declaration.
2320 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2321 if (type.getBasicType() == EbtStruct)
2322 {
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03002323 symbol = new TIntermSymbol(symbolTable.getEmptySymbolId(), "", type);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002324 }
jchen104cdac9e2017-05-08 11:01:20 +08002325 else if (IsAtomicCounter(publicType.getBasicType()))
2326 {
2327 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2328 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002329 }
2330 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002331 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002332 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002333
Olli Etuaho856c4972016-08-08 11:38:39 +03002334 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002335
jchen104cdac9e2017-05-08 11:01:20 +08002336 if (IsAtomicCounter(publicType.getBasicType()))
2337 {
2338
2339 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, false,
2340 identifierOrTypeLocation, type);
2341 }
2342
Olli Etuaho2935c582015-04-08 14:32:06 +03002343 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002344 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002345
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002346 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002347 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002348 symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
Olli Etuaho13389b62016-10-16 11:48:18 +01002349 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002350 }
2351
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002352 TIntermDeclaration *declaration = new TIntermDeclaration();
2353 declaration->setLine(identifierOrTypeLocation);
2354 if (symbol)
2355 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002356 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002357 declaration->appendDeclarator(symbol);
2358 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002359 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002360}
2361
Olli Etuaho13389b62016-10-16 11:48:18 +01002362TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
2363 const TSourceLoc &identifierLocation,
2364 const TString &identifier,
2365 const TSourceLoc &indexLocation,
2366 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04002367{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002368 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002369
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002370 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2371 identifierLocation);
2372
2373 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002374
Olli Etuaho856c4972016-08-08 11:38:39 +03002375 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002376
Olli Etuaho8a176262016-08-16 14:23:01 +03002377 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002378
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002379 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002380
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002381 if (indexExpression == nullptr)
jchen104cdac9e2017-05-08 11:01:20 +08002382 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002383 if (IsGeometryShaderInput(mShaderType, publicType.qualifier))
2384 {
2385 // Set size for the unsized geometry shader inputs if they are declared after a valid
2386 // input primitive declaration.
2387 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2388 {
2389 ASSERT(mGeometryShaderInputArraySize > 0u);
2390 arrayType.makeArray(mGeometryShaderInputArraySize);
2391 }
2392 else
2393 {
2394 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2395 // An input can be declared without an array size if there is a previous layout
2396 // which specifies the size.
2397 error(indexLocation,
2398 "Missing a valid input primitive declaration before declaring an unsized "
2399 "array input",
2400 "");
2401 }
2402 }
2403 else
2404 {
2405 // Unsized array declarations are only allowed in declaring geometry shader inputs.
2406 error(indexLocation, "Invalid unsized array declaration", "");
2407 }
2408 }
2409 else
2410 {
2411 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
2412 if (IsGeometryShaderInput(mShaderType, publicType.qualifier))
2413 {
2414 setGeometryShaderInputArraySize(size, indexLocation);
2415 }
2416
2417 // Make the type an array even if size check failed.
2418 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2419 arrayType.makeArray(size);
2420
2421 if (IsAtomicCounter(publicType.getBasicType()))
2422 {
2423 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size,
2424 false, identifierLocation, arrayType);
2425 }
jchen104cdac9e2017-05-08 11:01:20 +08002426 }
2427
Olli Etuaho2935c582015-04-08 14:32:06 +03002428 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002429 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002430
Olli Etuaho13389b62016-10-16 11:48:18 +01002431 TIntermDeclaration *declaration = new TIntermDeclaration();
2432 declaration->setLine(identifierLocation);
2433
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002434 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002435 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002436 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2437 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002438 declaration->appendDeclarator(symbol);
2439 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002440
Olli Etuaho13389b62016-10-16 11:48:18 +01002441 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002442}
2443
Olli Etuaho13389b62016-10-16 11:48:18 +01002444TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2445 const TSourceLoc &identifierLocation,
2446 const TString &identifier,
2447 const TSourceLoc &initLocation,
2448 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002449{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002450 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002451
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002452 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2453 identifierLocation);
2454
2455 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002456
Olli Etuaho13389b62016-10-16 11:48:18 +01002457 TIntermDeclaration *declaration = new TIntermDeclaration();
2458 declaration->setLine(identifierLocation);
2459
2460 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002461 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002462 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002463 if (initNode)
2464 {
2465 declaration->appendDeclarator(initNode);
2466 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002467 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002468 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002469}
2470
Olli Etuaho13389b62016-10-16 11:48:18 +01002471TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04002472 TPublicType &publicType,
2473 const TSourceLoc &identifierLocation,
2474 const TString &identifier,
2475 const TSourceLoc &indexLocation,
2476 TIntermTyped *indexExpression,
2477 const TSourceLoc &initLocation,
2478 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002479{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002480 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002481
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002482 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2483 identifierLocation);
2484
2485 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002486
Olli Etuaho8a176262016-08-16 14:23:01 +03002487 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002488
2489 TPublicType arrayType(publicType);
2490
Olli Etuaho856c4972016-08-08 11:38:39 +03002491 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002492 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2493 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002494 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002495 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002496 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002497 }
2498 // Make the type an array even if size check failed.
2499 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2500 arrayType.setArraySize(size);
2501
Olli Etuaho13389b62016-10-16 11:48:18 +01002502 TIntermDeclaration *declaration = new TIntermDeclaration();
2503 declaration->setLine(identifierLocation);
2504
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002505 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002506 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002507 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002508 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002509 if (initNode)
2510 {
2511 declaration->appendDeclarator(initNode);
2512 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002513 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002514
2515 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002516}
2517
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002518TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002519 const TTypeQualifierBuilder &typeQualifierBuilder,
2520 const TSourceLoc &identifierLoc,
2521 const TString *identifier,
2522 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002523{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002524 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002525
Martin Radev70866b82016-07-22 15:27:42 +03002526 if (!typeQualifier.invariant)
2527 {
2528 error(identifierLoc, "Expected invariant", identifier->c_str());
2529 return nullptr;
2530 }
2531 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2532 {
2533 return nullptr;
2534 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002535 if (!symbol)
2536 {
2537 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002538 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002539 }
Martin Radev70866b82016-07-22 15:27:42 +03002540 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002541 {
Martin Radev70866b82016-07-22 15:27:42 +03002542 error(identifierLoc, "invariant declaration specifies qualifier",
2543 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002544 }
Martin Radev70866b82016-07-22 15:27:42 +03002545 if (typeQualifier.precision != EbpUndefined)
2546 {
2547 error(identifierLoc, "invariant declaration specifies precision",
2548 getPrecisionString(typeQualifier.precision));
2549 }
2550 if (!typeQualifier.layoutQualifier.isEmpty())
2551 {
2552 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2553 }
2554
2555 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002556 if (!variable)
2557 {
2558 return nullptr;
2559 }
Martin Radev70866b82016-07-22 15:27:42 +03002560 const TType &type = variable->getType();
2561
2562 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2563 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002564 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002565
2566 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2567
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002568 TIntermSymbol *intermSymbol = new TIntermSymbol(variable->getUniqueId(), *identifier, type);
2569 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002570
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002571 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002572}
2573
Olli Etuaho13389b62016-10-16 11:48:18 +01002574void TParseContext::parseDeclarator(TPublicType &publicType,
2575 const TSourceLoc &identifierLocation,
2576 const TString &identifier,
2577 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002578{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002579 // If the declaration starting this declarator list was empty (example: int,), some checks were
2580 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002581 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002582 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002583 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2584 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002585 }
2586
Olli Etuaho856c4972016-08-08 11:38:39 +03002587 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002588
Olli Etuaho856c4972016-08-08 11:38:39 +03002589 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002590
Olli Etuaho2935c582015-04-08 14:32:06 +03002591 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002592 TType type(publicType);
jchen104cdac9e2017-05-08 11:01:20 +08002593 if (IsAtomicCounter(publicType.getBasicType()))
2594 {
2595 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, true,
2596 identifierLocation, type);
2597 }
Olli Etuaho43364892017-02-13 16:00:12 +00002598 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002599
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002600 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002601 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002602 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
2603 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002604 declarationOut->appendDeclarator(symbol);
2605 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002606}
2607
Olli Etuaho13389b62016-10-16 11:48:18 +01002608void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2609 const TSourceLoc &identifierLocation,
2610 const TString &identifier,
2611 const TSourceLoc &arrayLocation,
2612 TIntermTyped *indexExpression,
2613 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002614{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002615 // If the declaration starting this declarator list was empty (example: int,), some checks were
2616 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002617 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002618 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002619 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2620 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002621 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002622
Olli Etuaho856c4972016-08-08 11:38:39 +03002623 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002624
Olli Etuaho856c4972016-08-08 11:38:39 +03002625 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002626
Olli Etuaho8a176262016-08-16 14:23:01 +03002627 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002628 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002629 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002630 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002631 arrayType.makeArray(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002632
jchen104cdac9e2017-05-08 11:01:20 +08002633 if (IsAtomicCounter(publicType.getBasicType()))
2634 {
2635 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size,
2636 true, identifierLocation, arrayType);
2637 }
2638
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002639 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002640 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002641
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002642 if (variable)
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002643 {
2644 TIntermSymbol *symbol =
2645 new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2646 symbol->setLine(identifierLocation);
2647 declarationOut->appendDeclarator(symbol);
2648 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002649 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002650}
2651
Olli Etuaho13389b62016-10-16 11:48:18 +01002652void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2653 const TSourceLoc &identifierLocation,
2654 const TString &identifier,
2655 const TSourceLoc &initLocation,
2656 TIntermTyped *initializer,
2657 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002658{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002659 // If the declaration starting this declarator list was empty (example: int,), some checks were
2660 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002661 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002662 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002663 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2664 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002665 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002666
Olli Etuaho856c4972016-08-08 11:38:39 +03002667 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002668
Olli Etuaho13389b62016-10-16 11:48:18 +01002669 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002670 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002671 {
2672 //
2673 // build the intermediate representation
2674 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002675 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002676 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002677 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002678 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002679 }
2680}
2681
Olli Etuaho13389b62016-10-16 11:48:18 +01002682void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2683 const TSourceLoc &identifierLocation,
2684 const TString &identifier,
2685 const TSourceLoc &indexLocation,
2686 TIntermTyped *indexExpression,
2687 const TSourceLoc &initLocation,
2688 TIntermTyped *initializer,
2689 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002690{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002691 // If the declaration starting this declarator list was empty (example: int,), some checks were
2692 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002693 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002694 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002695 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2696 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002697 }
2698
Olli Etuaho856c4972016-08-08 11:38:39 +03002699 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002700
Olli Etuaho8a176262016-08-16 14:23:01 +03002701 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002702
2703 TPublicType arrayType(publicType);
2704
Olli Etuaho856c4972016-08-08 11:38:39 +03002705 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002706 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2707 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002708 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002709 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002710 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002711 }
2712 // Make the type an array even if size check failed.
2713 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2714 arrayType.setArraySize(size);
2715
2716 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002717 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002718 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002719 {
2720 if (initNode)
2721 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002722 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002723 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002724 }
2725}
2726
jchen104cdac9e2017-05-08 11:01:20 +08002727void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2728 const TSourceLoc &location)
2729{
2730 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2731 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2732 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2733 {
2734 error(location, "Requires both binding and offset", "layout");
2735 return;
2736 }
2737 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2738}
2739
Olli Etuahocce89652017-06-19 16:04:09 +03002740void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2741 const TPublicType &type,
2742 const TSourceLoc &loc)
2743{
2744 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2745 !getFragmentPrecisionHigh())
2746 {
2747 error(loc, "precision is not supported in fragment shader", "highp");
2748 }
2749
2750 if (!CanSetDefaultPrecisionOnType(type))
2751 {
2752 error(loc, "illegal type argument for default precision qualifier",
2753 getBasicString(type.getBasicType()));
2754 return;
2755 }
2756 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2757}
2758
Shaob5cc1192017-07-06 10:47:20 +08002759bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2760{
2761 switch (typeQualifier.layoutQualifier.primitiveType)
2762 {
2763 case EptLines:
2764 case EptLinesAdjacency:
2765 case EptTriangles:
2766 case EptTrianglesAdjacency:
2767 return typeQualifier.qualifier == EvqGeometryIn;
2768
2769 case EptLineStrip:
2770 case EptTriangleStrip:
2771 return typeQualifier.qualifier == EvqGeometryOut;
2772
2773 case EptPoints:
2774 return true;
2775
2776 default:
2777 UNREACHABLE();
2778 return false;
2779 }
2780}
2781
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002782void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2783 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002784{
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002785 if (mGeometryShaderInputArraySize == 0u)
2786 {
2787 mGeometryShaderInputArraySize = inputArraySize;
2788 }
2789 else if (mGeometryShaderInputArraySize != inputArraySize)
2790 {
2791 error(line,
2792 "Array size or input primitive declaration doesn't match the size of earlier sized "
2793 "array inputs.",
2794 "layout");
2795 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002796}
2797
Shaob5cc1192017-07-06 10:47:20 +08002798bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2799{
2800 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2801
2802 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2803
2804 if (layoutQualifier.maxVertices != -1)
2805 {
2806 error(typeQualifier.line,
2807 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2808 return false;
2809 }
2810
2811 // Set mGeometryInputPrimitiveType if exists
2812 if (layoutQualifier.primitiveType != EptUndefined)
2813 {
2814 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2815 {
2816 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2817 return false;
2818 }
2819
2820 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2821 {
2822 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002823 setGeometryShaderInputArraySize(
2824 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2825 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002826 }
2827 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2828 {
2829 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2830 "layout");
2831 return false;
2832 }
2833 }
2834
2835 // Set mGeometryInvocations if exists
2836 if (layoutQualifier.invocations > 0)
2837 {
2838 if (mGeometryShaderInvocations == 0)
2839 {
2840 mGeometryShaderInvocations = layoutQualifier.invocations;
2841 }
2842 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2843 {
2844 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2845 "layout");
2846 return false;
2847 }
2848 }
2849
2850 return true;
2851}
2852
2853bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2854{
2855 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2856
2857 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2858
2859 if (layoutQualifier.invocations > 0)
2860 {
2861 error(typeQualifier.line,
2862 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2863 return false;
2864 }
2865
2866 // Set mGeometryOutputPrimitiveType if exists
2867 if (layoutQualifier.primitiveType != EptUndefined)
2868 {
2869 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2870 {
2871 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2872 return false;
2873 }
2874
2875 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2876 {
2877 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2878 }
2879 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2880 {
2881 error(typeQualifier.line,
2882 "primitive doesn't match earlier output primitive declaration", "layout");
2883 return false;
2884 }
2885 }
2886
2887 // Set mGeometryMaxVertices if exists
2888 if (layoutQualifier.maxVertices > -1)
2889 {
2890 if (mGeometryShaderMaxVertices == -1)
2891 {
2892 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2893 }
2894 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2895 {
2896 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2897 "layout");
2898 return false;
2899 }
2900 }
2901
2902 return true;
2903}
2904
Martin Radev70866b82016-07-22 15:27:42 +03002905void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002906{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002907 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002908 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002909
Martin Radev70866b82016-07-22 15:27:42 +03002910 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2911 typeQualifier.line);
2912
Jamie Madillc2128ff2016-07-04 10:26:17 -04002913 // It should never be the case, but some strange parser errors can send us here.
2914 if (layoutQualifier.isEmpty())
2915 {
2916 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002917 return;
2918 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002919
Martin Radev802abe02016-08-04 17:48:32 +03002920 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002921 {
Olli Etuaho43364892017-02-13 16:00:12 +00002922 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002923 return;
2924 }
2925
Olli Etuaho43364892017-02-13 16:00:12 +00002926 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2927
2928 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002929
2930 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2931
Andrei Volykhina5527072017-03-22 16:46:30 +03002932 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2933
jchen104cdac9e2017-05-08 11:01:20 +08002934 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2935
Martin Radev802abe02016-08-04 17:48:32 +03002936 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002937 {
Martin Radev802abe02016-08-04 17:48:32 +03002938 if (mComputeShaderLocalSizeDeclared &&
2939 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2940 {
2941 error(typeQualifier.line, "Work group size does not match the previous declaration",
2942 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002943 return;
2944 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002945
Martin Radev802abe02016-08-04 17:48:32 +03002946 if (mShaderVersion < 310)
2947 {
2948 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002949 return;
2950 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002951
Martin Radev4c4c8e72016-08-04 12:25:34 +03002952 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002953 {
2954 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002955 return;
2956 }
2957
2958 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2959 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2960
2961 const TConstantUnion *maxComputeWorkGroupSizeData =
2962 maxComputeWorkGroupSize->getConstPointer();
2963
2964 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2965 {
2966 if (layoutQualifier.localSize[i] != -1)
2967 {
2968 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2969 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2970 if (mComputeShaderLocalSize[i] < 1 ||
2971 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2972 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002973 std::stringstream reasonStream;
2974 reasonStream << "invalid value: Value must be at least 1 and no greater than "
2975 << maxComputeWorkGroupSizeValue;
2976 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03002977
Olli Etuaho4de340a2016-12-16 09:32:03 +00002978 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03002979 return;
2980 }
2981 }
2982 }
2983
2984 mComputeShaderLocalSizeDeclared = true;
2985 }
Shaob5cc1192017-07-06 10:47:20 +08002986 else if (typeQualifier.qualifier == EvqGeometryIn)
2987 {
2988 if (mShaderVersion < 310)
2989 {
2990 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
2991 return;
2992 }
2993
2994 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
2995 {
2996 return;
2997 }
2998 }
2999 else if (typeQualifier.qualifier == EvqGeometryOut)
3000 {
3001 if (mShaderVersion < 310)
3002 {
3003 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3004 "layout");
3005 return;
3006 }
3007
3008 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3009 {
3010 return;
3011 }
3012 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003013 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3014 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003015 {
3016 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3017 // specification.
3018 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3019 {
3020 error(typeQualifier.line, "Number of views does not match the previous declaration",
3021 "layout");
3022 return;
3023 }
3024
3025 if (layoutQualifier.numViews == -1)
3026 {
3027 error(typeQualifier.line, "No num_views specified", "layout");
3028 return;
3029 }
3030
3031 if (layoutQualifier.numViews > mMaxNumViews)
3032 {
3033 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3034 "layout");
3035 return;
3036 }
3037
3038 mNumViews = layoutQualifier.numViews;
3039 }
Martin Radev802abe02016-08-04 17:48:32 +03003040 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003041 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003042 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003043 {
Martin Radev802abe02016-08-04 17:48:32 +03003044 return;
3045 }
3046
Jiajia Qinbc585152017-06-23 15:42:17 +08003047 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003048 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003049 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003050 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003051 return;
3052 }
3053
3054 if (mShaderVersion < 300)
3055 {
3056 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3057 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003058 return;
3059 }
3060
Olli Etuaho09b04a22016-12-15 13:30:26 +00003061 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003062
3063 if (layoutQualifier.matrixPacking != EmpUnspecified)
3064 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003065 if (typeQualifier.qualifier == EvqUniform)
3066 {
3067 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3068 }
3069 else if (typeQualifier.qualifier == EvqBuffer)
3070 {
3071 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3072 }
Martin Radev802abe02016-08-04 17:48:32 +03003073 }
3074
3075 if (layoutQualifier.blockStorage != EbsUnspecified)
3076 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003077 if (typeQualifier.qualifier == EvqUniform)
3078 {
3079 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3080 }
3081 else if (typeQualifier.qualifier == EvqBuffer)
3082 {
3083 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3084 }
Martin Radev802abe02016-08-04 17:48:32 +03003085 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003086 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003087}
3088
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003089TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3090 const TFunction &function,
3091 const TSourceLoc &location,
3092 bool insertParametersToSymbolTable)
3093{
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003094 checkIsNotReserved(location, function.getName());
3095
Olli Etuahofe486322017-03-21 09:30:54 +00003096 TIntermFunctionPrototype *prototype =
3097 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003098 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
3099 // point to the data that already exists in the symbol table.
3100 prototype->getFunctionSymbolInfo()->setFromFunction(function);
3101 prototype->setLine(location);
3102
3103 for (size_t i = 0; i < function.getParamCount(); i++)
3104 {
3105 const TConstParameter &param = function.getParam(i);
3106
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003107 TIntermSymbol *symbol = nullptr;
3108
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003109 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3110 // be used for unused args).
3111 if (param.name != nullptr)
3112 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003113 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003114 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003115 {
Olli Etuaho0f684632017-07-13 12:42:15 +03003116 TVariable *variable = symbolTable.declareVariable(param.name, *param.type);
3117 if (variable)
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003118 {
3119 symbol = new TIntermSymbol(variable->getUniqueId(), variable->getName(),
3120 variable->getType());
3121 }
3122 else
3123 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003124 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003125 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003126 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003127 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003128 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003129 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003130 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3131 // symbol.
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003132 symbol = new TIntermSymbol(symbolTable.getEmptySymbolId(), "", *param.type);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003133 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003134 symbol->setLine(location);
3135 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003136 }
3137 return prototype;
3138}
3139
Olli Etuaho16c745a2017-01-16 17:02:27 +00003140TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3141 const TFunction &parsedFunction,
3142 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003143{
Olli Etuaho476197f2016-10-11 13:59:08 +01003144 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3145 // first declaration. Either way the instance in the symbol table is used to track whether the
3146 // function is declared multiple times.
3147 TFunction *function = static_cast<TFunction *>(
3148 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3149 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003150 {
3151 // ESSL 1.00.17 section 4.2.7.
3152 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3153 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003154 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003155 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003156
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003157 TIntermFunctionPrototype *prototype =
3158 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003159
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003160 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003161
3162 if (!symbolTable.atGlobalLevel())
3163 {
3164 // ESSL 3.00.4 section 4.2.4.
3165 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003166 }
3167
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003168 return prototype;
3169}
3170
Olli Etuaho336b1472016-10-05 16:37:55 +01003171TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003172 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003173 TIntermBlock *functionBody,
3174 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003175{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003176 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003177 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3178 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003179 error(location, "function does not return a value:",
3180 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003181 }
3182
Olli Etuahof51fdd22016-10-03 10:03:40 +01003183 if (functionBody == nullptr)
3184 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003185 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003186 functionBody->setLine(location);
3187 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003188 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003189 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003190 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003191
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003192 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003193 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003194}
3195
Olli Etuaho476197f2016-10-11 13:59:08 +01003196void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3197 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003198 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003199{
Olli Etuaho476197f2016-10-11 13:59:08 +01003200 ASSERT(function);
3201 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003202 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003203 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003204
3205 if (builtIn)
3206 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003207 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003208 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003209 else
Jamie Madill185fb402015-06-12 15:48:48 -04003210 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003211 TFunction *prevDec = static_cast<TFunction *>(
3212 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3213
3214 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3215 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3216 // occurance.
3217 if (*function != prevDec)
3218 {
3219 // Swap the parameters of the previous declaration to the parameters of the function
3220 // definition (parameter names may differ).
3221 prevDec->swapParameters(**function);
3222
3223 // The function definition will share the same symbol as any previous declaration.
3224 *function = prevDec;
3225 }
3226
3227 if ((*function)->isDefined())
3228 {
3229 error(location, "function already has a body", (*function)->getName().c_str());
3230 }
3231
3232 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003233 }
Jamie Madill185fb402015-06-12 15:48:48 -04003234
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003235 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003236 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003237 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003238
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003239 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003240 setLoopNestingLevel(0);
3241}
3242
Jamie Madillb98c3a82015-07-23 14:26:04 -04003243TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003244{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003245 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003246 // We don't know at this point whether this is a function definition or a prototype.
3247 // The definition production code will check for redefinitions.
3248 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003249 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003250 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3251 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003252 //
3253 TFunction *prevDec =
3254 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303255
Martin Radevda6254b2016-12-14 17:00:36 +02003256 if (getShaderVersion() >= 300 &&
3257 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
3258 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303259 {
Martin Radevda6254b2016-12-14 17:00:36 +02003260 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303261 // Therefore overloading or redefining builtin functions is an error.
3262 error(location, "Name of a built-in function cannot be redeclared as function",
3263 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303264 }
3265 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003266 {
3267 if (prevDec->getReturnType() != function->getReturnType())
3268 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003269 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003270 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003271 }
3272 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3273 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003274 if (prevDec->getParam(i).type->getQualifier() !=
3275 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003276 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003277 error(location,
3278 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003279 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003280 }
3281 }
3282 }
3283
3284 //
3285 // Check for previously declared variables using the same name.
3286 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003287 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003288 if (prevSym)
3289 {
3290 if (!prevSym->isFunction())
3291 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003292 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003293 }
3294 }
3295 else
3296 {
3297 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003298 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003299 }
3300
3301 // We're at the inner scope level of the function's arguments and body statement.
3302 // Add the function prototype to the surrounding scope instead.
3303 symbolTable.getOuterLevel()->insert(function);
3304
Olli Etuaho78d13742017-01-18 13:06:10 +00003305 // Raise error message if main function takes any parameters or return anything other than void
3306 if (function->getName() == "main")
3307 {
3308 if (function->getParamCount() > 0)
3309 {
3310 error(location, "function cannot take any parameter(s)", "main");
3311 }
3312 if (function->getReturnType().getBasicType() != EbtVoid)
3313 {
3314 error(location, "main function cannot return a value",
3315 function->getReturnType().getBasicString());
3316 }
3317 }
3318
Jamie Madill185fb402015-06-12 15:48:48 -04003319 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003320 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3321 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003322 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3323 //
3324 return function;
3325}
3326
Olli Etuaho9de84a52016-06-14 17:36:01 +03003327TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3328 const TString *name,
3329 const TSourceLoc &location)
3330{
3331 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3332 {
3333 error(location, "no qualifiers allowed for function return",
3334 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003335 }
3336 if (!type.layoutQualifier.isEmpty())
3337 {
3338 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003339 }
jchen10cc2a10e2017-05-03 14:05:12 +08003340 // make sure an opaque type is not involved as well...
3341 std::string reason(getBasicString(type.getBasicType()));
3342 reason += "s can't be function return values";
3343 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003344 if (mShaderVersion < 300)
3345 {
3346 // Array return values are forbidden, but there's also no valid syntax for declaring array
3347 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00003348 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003349
3350 if (type.isStructureContainingArrays())
3351 {
3352 // ESSL 1.00.17 section 6.1 Function Definitions
3353 error(location, "structures containing arrays can't be function return values",
3354 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003355 }
3356 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003357
3358 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003359 return new TFunction(&symbolTable, name, new TType(type));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003360}
3361
Olli Etuahocce89652017-06-19 16:04:09 +03003362TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3363{
Olli Etuahocce89652017-06-19 16:04:09 +03003364 const TType *returnType = TCache::getType(EbtVoid, EbpUndefined);
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003365 return new TFunction(&symbolTable, name, returnType);
Olli Etuahocce89652017-06-19 16:04:09 +03003366}
3367
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003368TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003369{
Olli Etuahocce89652017-06-19 16:04:09 +03003370 if (mShaderVersion < 300 && publicType.array)
3371 {
3372 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3373 "[]");
3374 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003375 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003376 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003377 error(publicType.getLine(), "constructor can't be a structure definition",
3378 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003379 }
3380
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003381 TType *type = new TType(publicType);
3382 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003383 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003384 error(publicType.getLine(), "cannot construct this type",
3385 getBasicString(publicType.getBasicType()));
3386 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003387 }
3388
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003389 return new TFunction(&symbolTable, nullptr, type, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003390}
3391
Olli Etuahocce89652017-06-19 16:04:09 +03003392TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3393 const TString *name,
3394 const TSourceLoc &nameLoc)
3395{
3396 if (publicType.getBasicType() == EbtVoid)
3397 {
3398 error(nameLoc, "illegal use of type 'void'", name->c_str());
3399 }
3400 checkIsNotReserved(nameLoc, *name);
3401 TType *type = new TType(publicType);
3402 TParameter param = {name, type};
3403 return param;
3404}
3405
3406TParameter TParseContext::parseParameterArrayDeclarator(const TString *identifier,
3407 const TSourceLoc &identifierLoc,
3408 TIntermTyped *arraySize,
3409 const TSourceLoc &arrayLoc,
3410 TPublicType *type)
3411{
Olli Etuahoe0803872017-08-23 15:30:23 +03003412 checkArrayElementIsNotArray(arrayLoc, *type);
Olli Etuahocce89652017-06-19 16:04:09 +03003413 unsigned int size = checkIsValidArraySize(arrayLoc, arraySize);
3414 type->setArraySize(size);
3415 return parseParameterDeclarator(*type, identifier, identifierLoc);
3416}
3417
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003418bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(TIntermSequence *arguments,
3419 TType type,
3420 const TSourceLoc &line)
3421{
3422 if (arguments->empty())
3423 {
3424 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3425 return false;
3426 }
3427 for (TIntermNode *arg : *arguments)
3428 {
3429 TIntermTyped *element = arg->getAsTyped();
3430 ASSERT(element);
3431 size_t dimensionalityFromElement = element->getType().getArraySizes().size() + 1u;
3432 if (dimensionalityFromElement > type.getArraySizes().size())
3433 {
3434 error(line, "constructing from a non-dereferenced array", "constructor");
3435 return false;
3436 }
3437 else if (dimensionalityFromElement < type.getArraySizes().size())
3438 {
3439 if (dimensionalityFromElement == 1u)
3440 {
3441 error(line, "implicitly sized array of arrays constructor argument is not an array",
3442 "constructor");
3443 }
3444 else
3445 {
3446 error(line,
3447 "implicitly sized array of arrays constructor argument dimensionality is too "
3448 "low",
3449 "constructor");
3450 }
3451 return false;
3452 }
3453 }
3454 return true;
3455}
3456
Jamie Madillb98c3a82015-07-23 14:26:04 -04003457// This function is used to test for the correctness of the parameters passed to various constructor
3458// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003459//
Olli Etuaho856c4972016-08-08 11:38:39 +03003460// 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 +00003461//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003462TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003463 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303464 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003465{
Olli Etuaho856c4972016-08-08 11:38:39 +03003466 if (type.isUnsizedArray())
3467 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003468 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003469 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003470 type.sizeUnsizedArrays(TVector<unsigned int>());
Olli Etuaho3ec75682017-07-05 17:02:55 +03003471 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003472 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003473 TIntermTyped *firstElement = arguments->at(0)->getAsTyped();
3474 ASSERT(firstElement);
3475 type.setArraySize(type.getArraySizes().size() - 1u,
3476 static_cast<unsigned int>(arguments->size()));
3477 for (size_t i = 0; i < firstElement->getType().getArraySizes().size(); ++i)
3478 {
3479 if (type.getArraySizes()[i] == 0u)
3480 {
3481 type.setArraySize(i, firstElement->getType().getArraySizes().at(i));
3482 }
3483 }
3484 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003485 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003486
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003487 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003488 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003489 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003490 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003491
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003492 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003493 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003494
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003495 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3496 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003497 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003498 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003499 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003500 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003501}
3502
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003503//
3504// Interface/uniform blocks
Jiawei Shaod8105a02017-08-08 09:54:36 +08003505// TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003506//
Olli Etuaho13389b62016-10-16 11:48:18 +01003507TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003508 const TTypeQualifierBuilder &typeQualifierBuilder,
3509 const TSourceLoc &nameLine,
3510 const TString &blockName,
3511 TFieldList *fieldList,
3512 const TString *instanceName,
3513 const TSourceLoc &instanceLine,
3514 TIntermTyped *arrayIndex,
3515 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003516{
Olli Etuaho856c4972016-08-08 11:38:39 +03003517 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003518
Olli Etuaho77ba4082016-12-16 12:01:18 +00003519 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003520
Jiajia Qinbc585152017-06-23 15:42:17 +08003521 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003522 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003523 error(typeQualifier.line,
3524 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3525 "3.10",
3526 getQualifierString(typeQualifier.qualifier));
3527 }
3528 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3529 {
3530 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003531 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003532 }
3533
Martin Radev70866b82016-07-22 15:27:42 +03003534 if (typeQualifier.invariant)
3535 {
3536 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3537 }
3538
Jiajia Qinbc585152017-06-23 15:42:17 +08003539 if (typeQualifier.qualifier != EvqBuffer)
3540 {
3541 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3542 }
Olli Etuaho43364892017-02-13 16:00:12 +00003543
jchen10af713a22017-04-19 09:10:56 +08003544 // add array index
3545 unsigned int arraySize = 0;
3546 if (arrayIndex != nullptr)
3547 {
3548 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3549 }
3550
3551 if (mShaderVersion < 310)
3552 {
3553 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3554 }
3555 else
3556 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003557 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3558 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003559 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003560
Andrei Volykhina5527072017-03-22 16:46:30 +03003561 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3562
Jamie Madill099c0f32013-06-20 11:55:52 -04003563 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003564 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003565
Jamie Madill099c0f32013-06-20 11:55:52 -04003566 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3567 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003568 if (typeQualifier.qualifier == EvqUniform)
3569 {
3570 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3571 }
3572 else if (typeQualifier.qualifier == EvqBuffer)
3573 {
3574 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3575 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003576 }
3577
Jamie Madill1566ef72013-06-20 11:55:54 -04003578 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3579 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003580 if (typeQualifier.qualifier == EvqUniform)
3581 {
3582 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3583 }
3584 else if (typeQualifier.qualifier == EvqBuffer)
3585 {
3586 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3587 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003588 }
3589
Olli Etuaho856c4972016-08-08 11:38:39 +03003590 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003591
Martin Radev2cc85b32016-08-05 16:22:53 +03003592 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3593
Olli Etuaho0f684632017-07-13 12:42:15 +03003594 if (!symbolTable.declareInterfaceBlockName(&blockName))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303595 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003596 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003597 }
3598
Jamie Madill98493dd2013-07-08 14:39:03 -04003599 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303600 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3601 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003602 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303603 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003604 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303605 {
jchen10cc2a10e2017-05-03 14:05:12 +08003606 std::string reason("unsupported type - ");
3607 reason += fieldType->getBasicString();
3608 reason += " types are not allowed in interface blocks";
3609 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003610 }
3611
Jamie Madill98493dd2013-07-08 14:39:03 -04003612 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003613 switch (qualifier)
3614 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003615 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003616 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003617 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003618 if (typeQualifier.qualifier == EvqBuffer)
3619 {
3620 error(field->line(), "invalid qualifier on shader storage block member",
3621 getQualifierString(qualifier));
3622 }
3623 break;
3624 case EvqBuffer:
3625 if (typeQualifier.qualifier == EvqUniform)
3626 {
3627 error(field->line(), "invalid qualifier on uniform block member",
3628 getQualifierString(qualifier));
3629 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003630 break;
3631 default:
3632 error(field->line(), "invalid qualifier on interface block member",
3633 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003634 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003635 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003636
Martin Radev70866b82016-07-22 15:27:42 +03003637 if (fieldType->isInvariant())
3638 {
3639 error(field->line(), "invalid qualifier on interface block member", "invariant");
3640 }
3641
Jamie Madilla5efff92013-06-06 11:56:47 -04003642 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003643 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003644 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003645 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003646
Jamie Madill98493dd2013-07-08 14:39:03 -04003647 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003648 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003649 error(field->line(), "invalid layout qualifier: cannot be used here",
3650 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003651 }
3652
Jamie Madill98493dd2013-07-08 14:39:03 -04003653 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003654 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003655 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003656 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003657 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003658 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003659 warning(field->line(),
3660 "extraneous layout qualifier: only has an effect on matrix types",
3661 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003662 }
3663
Jamie Madill98493dd2013-07-08 14:39:03 -04003664 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003665
3666 if (typeQualifier.qualifier == EvqBuffer)
3667 {
3668 // set memory qualifiers
3669 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3670 // qualified with a memory qualifier, it is as if all of its members were declared with
3671 // the same memory qualifier.
3672 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3673 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3674 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3675 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3676 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3677 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3678 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3679 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3680 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3681 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3682 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003683 }
3684
Jamie Madillb98c3a82015-07-23 14:26:04 -04003685 TInterfaceBlock *interfaceBlock =
Shaob18c33e2017-08-16 12:37:51 +08003686 new TInterfaceBlock(&blockName, fieldList, instanceName, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003687 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
3688 if (arrayIndex != nullptr)
3689 {
3690 interfaceBlockType.makeArray(arraySize);
3691 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003692
3693 TString symbolName = "";
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003694 const TSymbolUniqueId *symbolId = nullptr;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003695
Jamie Madill98493dd2013-07-08 14:39:03 -04003696 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003697 {
3698 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003699 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3700 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003701 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303702 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003703
3704 // set parent pointer of the field variable
3705 fieldType->setInterfaceBlock(interfaceBlock);
3706
Olli Etuaho0f684632017-07-13 12:42:15 +03003707 TVariable *fieldVariable = symbolTable.declareVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003708
Olli Etuaho0f684632017-07-13 12:42:15 +03003709 if (fieldVariable)
3710 {
3711 fieldVariable->setQualifier(typeQualifier.qualifier);
3712 }
3713 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303714 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003715 error(field->line(), "redefinition of an interface block member name",
3716 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003717 }
3718 }
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003719 symbolId = &symbolTable.getEmptySymbolId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003720 }
3721 else
3722 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003723 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003724
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003725 // add a symbol for this interface block
Olli Etuaho0f684632017-07-13 12:42:15 +03003726 TVariable *instanceTypeDef = symbolTable.declareVariable(instanceName, interfaceBlockType);
3727 if (instanceTypeDef)
3728 {
3729 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003730 symbolId = &instanceTypeDef->getUniqueId();
Olli Etuaho0f684632017-07-13 12:42:15 +03003731 }
3732 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303733 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003734 error(instanceLine, "redefinition of an interface block instance name",
3735 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003736 }
Olli Etuaho0f684632017-07-13 12:42:15 +03003737 symbolName = *instanceName;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003738 }
3739
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003740 TIntermDeclaration *declaration = nullptr;
3741
3742 if (symbolId)
3743 {
3744 TIntermSymbol *blockSymbol = new TIntermSymbol(*symbolId, symbolName, interfaceBlockType);
3745 blockSymbol->setLine(typeQualifier.line);
3746 declaration = new TIntermDeclaration();
3747 declaration->appendDeclarator(blockSymbol);
3748 declaration->setLine(nameLine);
3749 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003750
3751 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003752 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003753}
3754
Olli Etuaho383b7912016-08-05 11:22:59 +03003755void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003756{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003757 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003758
3759 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003760 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303761 if (mStructNestingLevel > 1)
3762 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003763 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003764 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003765}
3766
3767void TParseContext::exitStructDeclaration()
3768{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003769 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003770}
3771
Olli Etuaho8a176262016-08-16 14:23:01 +03003772void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003773{
Jamie Madillacb4b812016-11-07 13:50:29 -05003774 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303775 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003776 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003777 }
3778
Arun Patole7e7e68d2015-05-22 12:02:25 +05303779 if (field.type()->getBasicType() != EbtStruct)
3780 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003781 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003782 }
3783
3784 // We're already inside a structure definition at this point, so add
3785 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303786 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3787 {
Jamie Madill41a49272014-03-18 16:10:13 -04003788 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003789 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3790 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003791 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003792 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003793 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003794 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003795}
3796
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003797//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003798// Parse an array index expression
3799//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003800TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3801 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303802 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003803{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003804 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3805 {
3806 if (baseExpression->getAsSymbolNode())
3807 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303808 error(location, " left of '[' is not of type array, matrix, or vector ",
3809 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003810 }
3811 else
3812 {
3813 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3814 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003815
Olli Etuaho3ec75682017-07-05 17:02:55 +03003816 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003817 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003818
Jiawei Shaod8105a02017-08-08 09:54:36 +08003819 if (baseExpression->getQualifier() == EvqPerVertexIn)
3820 {
3821 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
3822 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3823 {
3824 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3825 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3826 }
3827 }
3828
Jamie Madill21c1e452014-12-29 11:33:41 -05003829 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3830
Olli Etuaho36b05142015-11-12 13:10:42 +02003831 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3832 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3833 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3834 // index is a constant expression.
3835 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3836 {
3837 if (baseExpression->isInterfaceBlock())
3838 {
Jiawei Shaod8105a02017-08-08 09:54:36 +08003839 // TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
3840 switch (baseExpression->getQualifier())
3841 {
3842 case EvqPerVertexIn:
3843 break;
3844 case EvqUniform:
3845 case EvqBuffer:
3846 error(location,
3847 "array indexes for uniform block arrays and shader storage block arrays "
3848 "must be constant integral expressions",
3849 "[");
3850 break;
3851 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003852 // We can reach here only in error cases.
3853 ASSERT(mDiagnostics->numErrors() > 0);
3854 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003855 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003856 }
3857 else if (baseExpression->getQualifier() == EvqFragmentOut)
3858 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003859 error(location,
3860 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003861 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003862 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3863 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003864 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003865 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003866 }
3867
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003868 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003869 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003870 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3871 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3872 // constant fold expressions that are not constant expressions). The most compatible way to
3873 // handle this case is to report a warning instead of an error and force the index to be in
3874 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003875 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003876 int index = 0;
3877 if (indexConstantUnion->getBasicType() == EbtInt)
3878 {
3879 index = indexConstantUnion->getIConst(0);
3880 }
3881 else if (indexConstantUnion->getBasicType() == EbtUInt)
3882 {
3883 index = static_cast<int>(indexConstantUnion->getUConst(0));
3884 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003885
3886 int safeIndex = -1;
3887
3888 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003889 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003890 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003891 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003892 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003893 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003894 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003895 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003896 "GL_EXT_draw_buffers is disabled",
3897 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003898 safeIndex = 0;
3899 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003900 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003901 // Only do generic out-of-range check if similar error hasn't already been reported.
3902 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003903 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003904 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003905 baseExpression->getOutermostArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003906 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003907 }
3908 }
3909 else if (baseExpression->isMatrix())
3910 {
3911 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003912 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003913 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003914 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003915 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003916 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003917 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3918 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003919 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003920 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003921
3922 ASSERT(safeIndex >= 0);
3923 // Data of constant unions can't be changed, because it may be shared with other
3924 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3925 // sanitized object.
Olli Etuaho56229f12017-07-10 14:16:33 +03003926 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003927 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003928 TConstantUnion *safeConstantUnion = new TConstantUnion();
3929 safeConstantUnion->setIConst(safeIndex);
3930 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
Olli Etuaho56229f12017-07-10 14:16:33 +03003931 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003932 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003933
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003934 TIntermBinary *node = new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
3935 node->setLine(location);
3936 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003937 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003938 else
3939 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003940 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
3941 node->setLine(location);
3942 // Indirect indexing can never be constant folded.
3943 return node;
Jamie Madill7164cf42013-07-08 13:30:59 -04003944 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003945}
3946
Olli Etuaho90892fb2016-07-14 14:44:51 +03003947int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3948 const TSourceLoc &location,
3949 int index,
3950 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003951 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003952{
3953 if (index >= arraySize || index < 0)
3954 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003955 std::stringstream reasonStream;
3956 reasonStream << reason << " '" << index << "'";
3957 std::string token = reasonStream.str();
3958 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003959 if (index < 0)
3960 {
3961 return 0;
3962 }
3963 else
3964 {
3965 return arraySize - 1;
3966 }
3967 }
3968 return index;
3969}
3970
Jamie Madillb98c3a82015-07-23 14:26:04 -04003971TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3972 const TSourceLoc &dotLocation,
3973 const TString &fieldString,
3974 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003975{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003976 if (baseExpression->isArray())
3977 {
3978 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003979 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003980 }
3981
3982 if (baseExpression->isVector())
3983 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003984 TVector<int> fieldOffsets;
3985 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
3986 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003987 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003988 fieldOffsets.resize(1);
3989 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003990 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003991 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
3992 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003993
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003994 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003995 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003996 else if (baseExpression->getBasicType() == EbtStruct)
3997 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303998 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003999 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004000 {
4001 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004002 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004003 }
4004 else
4005 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004006 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004007 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004008 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004009 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004010 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004011 {
4012 fieldFound = true;
4013 break;
4014 }
4015 }
4016 if (fieldFound)
4017 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004018 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004019 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004020 TIntermBinary *node =
4021 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4022 node->setLine(dotLocation);
4023 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004024 }
4025 else
4026 {
4027 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004028 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004029 }
4030 }
4031 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004032 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004033 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304034 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004035 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004036 {
4037 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004038 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004039 }
4040 else
4041 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004042 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004043 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004044 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004045 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004046 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004047 {
4048 fieldFound = true;
4049 break;
4050 }
4051 }
4052 if (fieldFound)
4053 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004054 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004055 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004056 TIntermBinary *node =
4057 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4058 node->setLine(dotLocation);
4059 // Indexing interface blocks can never be constant folded.
4060 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004061 }
4062 else
4063 {
4064 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004065 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004066 }
4067 }
4068 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004069 else
4070 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004071 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004072 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004073 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304074 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004075 }
4076 else
4077 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304078 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004079 " field selection requires structure, vector, or interface block on left hand "
4080 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304081 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004082 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004083 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004084 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004085}
4086
Jamie Madillb98c3a82015-07-23 14:26:04 -04004087TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4088 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004089{
Martin Radev802abe02016-08-04 17:48:32 +03004090 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004091
4092 if (qualifierType == "shared")
4093 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004094 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004095 {
4096 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4097 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004098 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004099 }
4100 else if (qualifierType == "packed")
4101 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004102 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004103 {
4104 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4105 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004106 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004107 }
4108 else if (qualifierType == "std140")
4109 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004110 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004111 }
4112 else if (qualifierType == "row_major")
4113 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004114 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004115 }
4116 else if (qualifierType == "column_major")
4117 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004118 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004119 }
4120 else if (qualifierType == "location")
4121 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004122 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4123 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004124 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004125 else if (qualifierType == "yuv" && isExtensionEnabled(TExtension::EXT_YUV_target) &&
Andrei Volykhina5527072017-03-22 16:46:30 +03004126 mShaderType == GL_FRAGMENT_SHADER)
4127 {
4128 qualifier.yuv = true;
4129 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004130 else if (qualifierType == "rgba32f")
4131 {
4132 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4133 qualifier.imageInternalFormat = EiifRGBA32F;
4134 }
4135 else if (qualifierType == "rgba16f")
4136 {
4137 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4138 qualifier.imageInternalFormat = EiifRGBA16F;
4139 }
4140 else if (qualifierType == "r32f")
4141 {
4142 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4143 qualifier.imageInternalFormat = EiifR32F;
4144 }
4145 else if (qualifierType == "rgba8")
4146 {
4147 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4148 qualifier.imageInternalFormat = EiifRGBA8;
4149 }
4150 else if (qualifierType == "rgba8_snorm")
4151 {
4152 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4153 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4154 }
4155 else if (qualifierType == "rgba32i")
4156 {
4157 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4158 qualifier.imageInternalFormat = EiifRGBA32I;
4159 }
4160 else if (qualifierType == "rgba16i")
4161 {
4162 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4163 qualifier.imageInternalFormat = EiifRGBA16I;
4164 }
4165 else if (qualifierType == "rgba8i")
4166 {
4167 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4168 qualifier.imageInternalFormat = EiifRGBA8I;
4169 }
4170 else if (qualifierType == "r32i")
4171 {
4172 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4173 qualifier.imageInternalFormat = EiifR32I;
4174 }
4175 else if (qualifierType == "rgba32ui")
4176 {
4177 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4178 qualifier.imageInternalFormat = EiifRGBA32UI;
4179 }
4180 else if (qualifierType == "rgba16ui")
4181 {
4182 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4183 qualifier.imageInternalFormat = EiifRGBA16UI;
4184 }
4185 else if (qualifierType == "rgba8ui")
4186 {
4187 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4188 qualifier.imageInternalFormat = EiifRGBA8UI;
4189 }
4190 else if (qualifierType == "r32ui")
4191 {
4192 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4193 qualifier.imageInternalFormat = EiifR32UI;
4194 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004195 else if (qualifierType == "points" && isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004196 mShaderType == GL_GEOMETRY_SHADER_OES)
4197 {
4198 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4199 qualifier.primitiveType = EptPoints;
4200 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004201 else if (qualifierType == "lines" && isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004202 mShaderType == GL_GEOMETRY_SHADER_OES)
4203 {
4204 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4205 qualifier.primitiveType = EptLines;
4206 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004207 else if (qualifierType == "lines_adjacency" &&
4208 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004209 mShaderType == GL_GEOMETRY_SHADER_OES)
4210 {
4211 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4212 qualifier.primitiveType = EptLinesAdjacency;
4213 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004214 else if (qualifierType == "triangles" && isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004215 mShaderType == GL_GEOMETRY_SHADER_OES)
4216 {
4217 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4218 qualifier.primitiveType = EptTriangles;
4219 }
4220 else if (qualifierType == "triangles_adjacency" &&
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004221 isExtensionEnabled(TExtension::OES_geometry_shader) &&
4222 mShaderType == GL_GEOMETRY_SHADER_OES)
Shaob5cc1192017-07-06 10:47:20 +08004223 {
4224 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4225 qualifier.primitiveType = EptTrianglesAdjacency;
4226 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004227 else if (qualifierType == "line_strip" && isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004228 mShaderType == GL_GEOMETRY_SHADER_OES)
4229 {
4230 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4231 qualifier.primitiveType = EptLineStrip;
4232 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004233 else if (qualifierType == "triangle_strip" &&
4234 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004235 mShaderType == GL_GEOMETRY_SHADER_OES)
4236 {
4237 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4238 qualifier.primitiveType = EptTriangleStrip;
4239 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004240
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004241 else
4242 {
4243 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004244 }
4245
Jamie Madilla5efff92013-06-06 11:56:47 -04004246 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004247}
4248
Martin Radev802abe02016-08-04 17:48:32 +03004249void TParseContext::parseLocalSize(const TString &qualifierType,
4250 const TSourceLoc &qualifierTypeLine,
4251 int intValue,
4252 const TSourceLoc &intValueLine,
4253 const std::string &intValueString,
4254 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004255 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004256{
Olli Etuaho856c4972016-08-08 11:38:39 +03004257 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004258 if (intValue < 1)
4259 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004260 std::stringstream reasonStream;
4261 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4262 std::string reason = reasonStream.str();
4263 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004264 }
4265 (*localSize)[index] = intValue;
4266}
4267
Olli Etuaho09b04a22016-12-15 13:30:26 +00004268void TParseContext::parseNumViews(int intValue,
4269 const TSourceLoc &intValueLine,
4270 const std::string &intValueString,
4271 int *numViews)
4272{
4273 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4274 // specification.
4275 if (intValue < 1)
4276 {
4277 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4278 }
4279 *numViews = intValue;
4280}
4281
Shaob5cc1192017-07-06 10:47:20 +08004282void TParseContext::parseInvocations(int intValue,
4283 const TSourceLoc &intValueLine,
4284 const std::string &intValueString,
4285 int *numInvocations)
4286{
4287 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4288 // it doesn't make sense to accept invocations <= 0.
4289 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4290 {
4291 error(intValueLine,
4292 "out of range: invocations must be in the range of [1, "
4293 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4294 intValueString.c_str());
4295 }
4296 else
4297 {
4298 *numInvocations = intValue;
4299 }
4300}
4301
4302void TParseContext::parseMaxVertices(int intValue,
4303 const TSourceLoc &intValueLine,
4304 const std::string &intValueString,
4305 int *maxVertices)
4306{
4307 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4308 // it doesn't make sense to accept max_vertices < 0.
4309 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4310 {
4311 error(
4312 intValueLine,
4313 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4314 intValueString.c_str());
4315 }
4316 else
4317 {
4318 *maxVertices = intValue;
4319 }
4320}
4321
Jamie Madillb98c3a82015-07-23 14:26:04 -04004322TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4323 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004324 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304325 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004326{
Martin Radev802abe02016-08-04 17:48:32 +03004327 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004328
Martin Radev802abe02016-08-04 17:48:32 +03004329 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004330
Martin Radev802abe02016-08-04 17:48:32 +03004331 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004332 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004333 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004334 if (intValue < 0)
4335 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004336 error(intValueLine, "out of range: location must be non-negative",
4337 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004338 }
4339 else
4340 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004341 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004342 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004343 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004344 }
Olli Etuaho43364892017-02-13 16:00:12 +00004345 else if (qualifierType == "binding")
4346 {
4347 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4348 if (intValue < 0)
4349 {
4350 error(intValueLine, "out of range: binding must be non-negative",
4351 intValueString.c_str());
4352 }
4353 else
4354 {
4355 qualifier.binding = intValue;
4356 }
4357 }
jchen104cdac9e2017-05-08 11:01:20 +08004358 else if (qualifierType == "offset")
4359 {
4360 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4361 if (intValue < 0)
4362 {
4363 error(intValueLine, "out of range: offset must be non-negative",
4364 intValueString.c_str());
4365 }
4366 else
4367 {
4368 qualifier.offset = intValue;
4369 }
4370 }
Martin Radev802abe02016-08-04 17:48:32 +03004371 else if (qualifierType == "local_size_x")
4372 {
4373 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4374 &qualifier.localSize);
4375 }
4376 else if (qualifierType == "local_size_y")
4377 {
4378 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4379 &qualifier.localSize);
4380 }
4381 else if (qualifierType == "local_size_z")
4382 {
4383 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4384 &qualifier.localSize);
4385 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004386 else if (qualifierType == "num_views" && isExtensionEnabled(TExtension::OVR_multiview) &&
Olli Etuaho09b04a22016-12-15 13:30:26 +00004387 mShaderType == GL_VERTEX_SHADER)
4388 {
4389 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4390 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004391 else if (qualifierType == "invocations" &&
4392 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004393 mShaderType == GL_GEOMETRY_SHADER_OES)
4394 {
4395 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4396 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004397 else if (qualifierType == "max_vertices" &&
4398 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004399 mShaderType == GL_GEOMETRY_SHADER_OES)
4400 {
4401 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4402 }
4403
Martin Radev802abe02016-08-04 17:48:32 +03004404 else
4405 {
4406 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004407 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004408
Jamie Madilla5efff92013-06-06 11:56:47 -04004409 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004410}
4411
Olli Etuaho613b9592016-09-05 12:05:53 +03004412TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4413{
4414 return new TTypeQualifierBuilder(
4415 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4416 mShaderVersion);
4417}
4418
Olli Etuahocce89652017-06-19 16:04:09 +03004419TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4420 const TSourceLoc &loc)
4421{
4422 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4423 return new TStorageQualifierWrapper(qualifier, loc);
4424}
4425
4426TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4427{
4428 if (getShaderType() == GL_VERTEX_SHADER)
4429 {
4430 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4431 }
4432 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4433}
4434
4435TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4436{
4437 if (declaringFunction())
4438 {
4439 return new TStorageQualifierWrapper(EvqIn, loc);
4440 }
Shaob5cc1192017-07-06 10:47:20 +08004441
4442 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004443 {
Shaob5cc1192017-07-06 10:47:20 +08004444 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004445 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004446 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004447 {
4448 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4449 }
4450 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004451 }
Shaob5cc1192017-07-06 10:47:20 +08004452 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004453 {
Shaob5cc1192017-07-06 10:47:20 +08004454 if (mShaderVersion < 300)
4455 {
4456 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4457 }
4458 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004459 }
Shaob5cc1192017-07-06 10:47:20 +08004460 case GL_COMPUTE_SHADER:
4461 {
4462 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4463 }
4464 case GL_GEOMETRY_SHADER_OES:
4465 {
4466 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4467 }
4468 default:
4469 {
4470 UNREACHABLE();
4471 return new TStorageQualifierWrapper(EvqLast, loc);
4472 }
Olli Etuahocce89652017-06-19 16:04:09 +03004473 }
Olli Etuahocce89652017-06-19 16:04:09 +03004474}
4475
4476TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4477{
4478 if (declaringFunction())
4479 {
4480 return new TStorageQualifierWrapper(EvqOut, loc);
4481 }
Shaob5cc1192017-07-06 10:47:20 +08004482 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004483 {
Shaob5cc1192017-07-06 10:47:20 +08004484 case GL_VERTEX_SHADER:
4485 {
4486 if (mShaderVersion < 300)
4487 {
4488 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4489 }
4490 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4491 }
4492 case GL_FRAGMENT_SHADER:
4493 {
4494 if (mShaderVersion < 300)
4495 {
4496 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4497 }
4498 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4499 }
4500 case GL_COMPUTE_SHADER:
4501 {
4502 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4503 return new TStorageQualifierWrapper(EvqLast, loc);
4504 }
4505 case GL_GEOMETRY_SHADER_OES:
4506 {
4507 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4508 }
4509 default:
4510 {
4511 UNREACHABLE();
4512 return new TStorageQualifierWrapper(EvqLast, loc);
4513 }
Olli Etuahocce89652017-06-19 16:04:09 +03004514 }
Olli Etuahocce89652017-06-19 16:04:09 +03004515}
4516
4517TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4518{
4519 if (!declaringFunction())
4520 {
4521 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4522 }
4523 return new TStorageQualifierWrapper(EvqInOut, loc);
4524}
4525
Jamie Madillb98c3a82015-07-23 14:26:04 -04004526TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004527 TLayoutQualifier rightQualifier,
4528 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004529{
Martin Radevc28888b2016-07-22 15:27:42 +03004530 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004531 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004532}
4533
Olli Etuahocce89652017-06-19 16:04:09 +03004534TField *TParseContext::parseStructDeclarator(TString *identifier, const TSourceLoc &loc)
4535{
4536 checkIsNotReserved(loc, *identifier);
4537 TType *type = new TType(EbtVoid, EbpUndefined);
4538 return new TField(type, identifier, loc);
4539}
4540
4541TField *TParseContext::parseStructArrayDeclarator(TString *identifier,
4542 const TSourceLoc &loc,
4543 TIntermTyped *arraySize,
4544 const TSourceLoc &arraySizeLoc)
4545{
4546 checkIsNotReserved(loc, *identifier);
4547
4548 TType *type = new TType(EbtVoid, EbpUndefined);
4549 unsigned int size = checkIsValidArraySize(arraySizeLoc, arraySize);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004550 type->makeArray(size);
Olli Etuahocce89652017-06-19 16:04:09 +03004551
4552 return new TField(type, identifier, loc);
4553}
4554
Olli Etuaho4de340a2016-12-16 09:32:03 +00004555TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4556 const TFieldList *newlyAddedFields,
4557 const TSourceLoc &location)
4558{
4559 for (TField *field : *newlyAddedFields)
4560 {
4561 for (TField *oldField : *processedFields)
4562 {
4563 if (oldField->name() == field->name())
4564 {
4565 error(location, "duplicate field name in structure", field->name().c_str());
4566 }
4567 }
4568 processedFields->push_back(field);
4569 }
4570 return processedFields;
4571}
4572
Martin Radev70866b82016-07-22 15:27:42 +03004573TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4574 const TTypeQualifierBuilder &typeQualifierBuilder,
4575 TPublicType *typeSpecifier,
4576 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004577{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004578 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004579
Martin Radev70866b82016-07-22 15:27:42 +03004580 typeSpecifier->qualifier = typeQualifier.qualifier;
4581 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004582 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004583 typeSpecifier->invariant = typeQualifier.invariant;
4584 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304585 {
Martin Radev70866b82016-07-22 15:27:42 +03004586 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004587 }
Martin Radev70866b82016-07-22 15:27:42 +03004588 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004589}
4590
Jamie Madillb98c3a82015-07-23 14:26:04 -04004591TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004592 TFieldList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004593{
Martin Radev4a9cd802016-09-01 16:51:51 +03004594 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4595 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004596
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004597 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
4598 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004599
Martin Radev4a9cd802016-09-01 16:51:51 +03004600 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004601
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004602 for (unsigned int i = 0; i < declaratorList->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304603 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004604 auto declaratorArraySizes = (*declaratorList)[i]->type()->getArraySizes();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004605 // don't allow arrays of arrays
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004606 if (!declaratorArraySizes.empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304607 {
Olli Etuahoe0803872017-08-23 15:30:23 +03004608 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004609 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004610
4611 TType *type = (*declaratorList)[i]->type();
4612 *type = TType(typeSpecifier);
4613 for (unsigned int arraySize : declaratorArraySizes)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304614 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004615 type->makeArray(arraySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004616 }
4617
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004618 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*declaratorList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004619 }
4620
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004621 return declaratorList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004622}
4623
Martin Radev4a9cd802016-09-01 16:51:51 +03004624TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4625 const TSourceLoc &nameLine,
4626 const TString *structName,
4627 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004628{
Olli Etuahoa5e693a2017-07-13 16:07:26 +03004629 TStructure *structure = new TStructure(&symbolTable, structName, fieldList);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004630
Jamie Madill9b820842015-02-12 10:40:10 -05004631 // Store a bool in the struct if we're at global scope, to allow us to
4632 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004633 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004634
Jamie Madill98493dd2013-07-08 14:39:03 -04004635 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004636 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004637 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004638 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304639 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004640 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004641 }
4642 }
4643
4644 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004645 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004646 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004647 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004648 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004649 switch (qualifier)
4650 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004651 case EvqGlobal:
4652 case EvqTemporary:
4653 break;
4654 default:
4655 error(field.line(), "invalid qualifier on struct member",
4656 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004657 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004658 }
Martin Radev70866b82016-07-22 15:27:42 +03004659 if (field.type()->isInvariant())
4660 {
4661 error(field.line(), "invalid qualifier on struct member", "invariant");
4662 }
jchen104cdac9e2017-05-08 11:01:20 +08004663 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4664 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004665 {
4666 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4667 }
4668
Olli Etuaho43364892017-02-13 16:00:12 +00004669 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4670
4671 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004672
4673 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004674 }
4675
Martin Radev4a9cd802016-09-01 16:51:51 +03004676 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004677 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004678 exitStructDeclaration();
4679
Martin Radev4a9cd802016-09-01 16:51:51 +03004680 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004681}
4682
Jamie Madillb98c3a82015-07-23 14:26:04 -04004683TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004684 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004685 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004686{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004687 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004688 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004689 init->isVector())
4690 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004691 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4692 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004693 return nullptr;
4694 }
4695
Olli Etuaho923ecef2017-10-11 12:01:38 +03004696 ASSERT(statementList);
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004697 if (!ValidateSwitchStatementList(switchType, mShaderVersion, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004698 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004699 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004700 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004701 }
4702
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004703 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4704 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004705 return node;
4706}
4707
4708TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4709{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004710 if (mSwitchNestingLevel == 0)
4711 {
4712 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004713 return nullptr;
4714 }
4715 if (condition == nullptr)
4716 {
4717 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004718 return nullptr;
4719 }
4720 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004721 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004722 {
4723 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004724 }
4725 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004726 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4727 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4728 // fold in case labels.
4729 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004730 {
4731 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004732 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004733 TIntermCase *node = new TIntermCase(condition);
4734 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004735 return node;
4736}
4737
4738TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4739{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004740 if (mSwitchNestingLevel == 0)
4741 {
4742 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004743 return nullptr;
4744 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004745 TIntermCase *node = new TIntermCase(nullptr);
4746 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004747 return node;
4748}
4749
Jamie Madillb98c3a82015-07-23 14:26:04 -04004750TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4751 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004752 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004753{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004754 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004755
4756 switch (op)
4757 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004758 case EOpLogicalNot:
4759 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4760 child->isVector())
4761 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004762 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004763 return nullptr;
4764 }
4765 break;
4766 case EOpBitwiseNot:
4767 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4768 child->isMatrix() || child->isArray())
4769 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004770 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004771 return nullptr;
4772 }
4773 break;
4774 case EOpPostIncrement:
4775 case EOpPreIncrement:
4776 case EOpPostDecrement:
4777 case EOpPreDecrement:
4778 case EOpNegative:
4779 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004780 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4781 child->getBasicType() == EbtBool || child->isArray() ||
4782 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004783 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004784 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004785 return nullptr;
4786 }
4787 // Operators for built-ins are already type checked against their prototype.
4788 default:
4789 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004790 }
4791
Jiajia Qinbc585152017-06-23 15:42:17 +08004792 if (child->getMemoryQualifier().writeonly)
4793 {
4794 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4795 return nullptr;
4796 }
4797
Olli Etuahof119a262016-08-19 15:54:22 +03004798 TIntermUnary *node = new TIntermUnary(op, child);
4799 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004800
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004801 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004802}
4803
Olli Etuaho09b22472015-02-11 11:47:26 +02004804TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4805{
Olli Etuahocce89652017-06-19 16:04:09 +03004806 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004807 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004808 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004809 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004810 return child;
4811 }
4812 return node;
4813}
4814
Jamie Madillb98c3a82015-07-23 14:26:04 -04004815TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4816 TIntermTyped *child,
4817 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004818{
Olli Etuaho856c4972016-08-08 11:38:39 +03004819 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004820 return addUnaryMath(op, child, loc);
4821}
4822
Jamie Madillb98c3a82015-07-23 14:26:04 -04004823bool TParseContext::binaryOpCommonCheck(TOperator op,
4824 TIntermTyped *left,
4825 TIntermTyped *right,
4826 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004827{
jchen10b4cf5652017-05-05 18:51:17 +08004828 // Check opaque types are not allowed to be operands in expressions other than array indexing
4829 // and structure member selection.
4830 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4831 {
4832 switch (op)
4833 {
4834 case EOpIndexDirect:
4835 case EOpIndexIndirect:
4836 break;
4837 case EOpIndexDirectStruct:
4838 UNREACHABLE();
4839
4840 default:
4841 error(loc, "Invalid operation for variables with an opaque type",
4842 GetOperatorString(op));
4843 return false;
4844 }
4845 }
jchen10cc2a10e2017-05-03 14:05:12 +08004846
Jiajia Qinbc585152017-06-23 15:42:17 +08004847 if (right->getMemoryQualifier().writeonly)
4848 {
4849 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4850 return false;
4851 }
4852
4853 if (left->getMemoryQualifier().writeonly)
4854 {
4855 switch (op)
4856 {
4857 case EOpAssign:
4858 case EOpInitialize:
4859 case EOpIndexDirect:
4860 case EOpIndexIndirect:
4861 case EOpIndexDirectStruct:
4862 case EOpIndexDirectInterfaceBlock:
4863 break;
4864 default:
4865 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4866 return false;
4867 }
4868 }
4869
Olli Etuaho244be012016-08-18 15:26:02 +03004870 if (left->getType().getStruct() || right->getType().getStruct())
4871 {
4872 switch (op)
4873 {
4874 case EOpIndexDirectStruct:
4875 ASSERT(left->getType().getStruct());
4876 break;
4877 case EOpEqual:
4878 case EOpNotEqual:
4879 case EOpAssign:
4880 case EOpInitialize:
4881 if (left->getType() != right->getType())
4882 {
4883 return false;
4884 }
4885 break;
4886 default:
4887 error(loc, "Invalid operation for structs", GetOperatorString(op));
4888 return false;
4889 }
4890 }
4891
Olli Etuaho94050052017-05-08 14:17:44 +03004892 if (left->isInterfaceBlock() || right->isInterfaceBlock())
4893 {
4894 switch (op)
4895 {
4896 case EOpIndexDirectInterfaceBlock:
4897 ASSERT(left->getType().getInterfaceBlock());
4898 break;
4899 default:
4900 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
4901 return false;
4902 }
4903 }
4904
Olli Etuahod6b14282015-03-17 14:31:35 +02004905 if (left->isArray() || right->isArray())
4906 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004907 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02004908 {
4909 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4910 return false;
4911 }
4912
4913 if (left->isArray() != right->isArray())
4914 {
4915 error(loc, "array / non-array mismatch", GetOperatorString(op));
4916 return false;
4917 }
4918
4919 switch (op)
4920 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004921 case EOpEqual:
4922 case EOpNotEqual:
4923 case EOpAssign:
4924 case EOpInitialize:
4925 break;
4926 default:
4927 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4928 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02004929 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03004930 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004931 if (left->getType().getArraySizes() != right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02004932 {
4933 error(loc, "array size mismatch", GetOperatorString(op));
4934 return false;
4935 }
Olli Etuahod6b14282015-03-17 14:31:35 +02004936 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004937
4938 // Check ops which require integer / ivec parameters
4939 bool isBitShift = false;
4940 switch (op)
4941 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004942 case EOpBitShiftLeft:
4943 case EOpBitShiftRight:
4944 case EOpBitShiftLeftAssign:
4945 case EOpBitShiftRightAssign:
4946 // Unsigned can be bit-shifted by signed and vice versa, but we need to
4947 // check that the basic type is an integer type.
4948 isBitShift = true;
4949 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
4950 {
4951 return false;
4952 }
4953 break;
4954 case EOpBitwiseAnd:
4955 case EOpBitwiseXor:
4956 case EOpBitwiseOr:
4957 case EOpBitwiseAndAssign:
4958 case EOpBitwiseXorAssign:
4959 case EOpBitwiseOrAssign:
4960 // It is enough to check the type of only one operand, since later it
4961 // is checked that the operand types match.
4962 if (!IsInteger(left->getBasicType()))
4963 {
4964 return false;
4965 }
4966 break;
4967 default:
4968 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004969 }
4970
4971 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
4972 // So the basic type should usually match.
4973 if (!isBitShift && left->getBasicType() != right->getBasicType())
4974 {
4975 return false;
4976 }
4977
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004978 // Check that:
4979 // 1. Type sizes match exactly on ops that require that.
4980 // 2. Restrictions for structs that contain arrays or samplers are respected.
4981 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04004982 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004983 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004984 case EOpAssign:
4985 case EOpInitialize:
4986 case EOpEqual:
4987 case EOpNotEqual:
4988 // ESSL 1.00 sections 5.7, 5.8, 5.9
4989 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
4990 {
4991 error(loc, "undefined operation for structs containing arrays",
4992 GetOperatorString(op));
4993 return false;
4994 }
4995 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
4996 // we interpret the spec so that this extends to structs containing samplers,
4997 // similarly to ESSL 1.00 spec.
4998 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
4999 left->getType().isStructureContainingSamplers())
5000 {
5001 error(loc, "undefined operation for structs containing samplers",
5002 GetOperatorString(op));
5003 return false;
5004 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005005
Olli Etuahoe1805592017-01-02 16:41:20 +00005006 if ((left->getNominalSize() != right->getNominalSize()) ||
5007 (left->getSecondarySize() != right->getSecondarySize()))
5008 {
5009 error(loc, "dimension mismatch", GetOperatorString(op));
5010 return false;
5011 }
5012 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005013 case EOpLessThan:
5014 case EOpGreaterThan:
5015 case EOpLessThanEqual:
5016 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005017 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005018 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005019 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005020 return false;
5021 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005022 break;
5023 case EOpAdd:
5024 case EOpSub:
5025 case EOpDiv:
5026 case EOpIMod:
5027 case EOpBitShiftLeft:
5028 case EOpBitShiftRight:
5029 case EOpBitwiseAnd:
5030 case EOpBitwiseXor:
5031 case EOpBitwiseOr:
5032 case EOpAddAssign:
5033 case EOpSubAssign:
5034 case EOpDivAssign:
5035 case EOpIModAssign:
5036 case EOpBitShiftLeftAssign:
5037 case EOpBitShiftRightAssign:
5038 case EOpBitwiseAndAssign:
5039 case EOpBitwiseXorAssign:
5040 case EOpBitwiseOrAssign:
5041 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5042 {
5043 return false;
5044 }
5045
5046 // Are the sizes compatible?
5047 if (left->getNominalSize() != right->getNominalSize() ||
5048 left->getSecondarySize() != right->getSecondarySize())
5049 {
5050 // If the nominal sizes of operands do not match:
5051 // One of them must be a scalar.
5052 if (!left->isScalar() && !right->isScalar())
5053 return false;
5054
5055 // In the case of compound assignment other than multiply-assign,
5056 // the right side needs to be a scalar. Otherwise a vector/matrix
5057 // would be assigned to a scalar. A scalar can't be shifted by a
5058 // vector either.
5059 if (!right->isScalar() &&
5060 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5061 return false;
5062 }
5063 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005064 default:
5065 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005066 }
5067
Olli Etuahod6b14282015-03-17 14:31:35 +02005068 return true;
5069}
5070
Olli Etuaho1dded802016-08-18 18:13:13 +03005071bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5072 const TType &left,
5073 const TType &right)
5074{
5075 switch (op)
5076 {
5077 case EOpMul:
5078 case EOpMulAssign:
5079 return left.getNominalSize() == right.getNominalSize() &&
5080 left.getSecondarySize() == right.getSecondarySize();
5081 case EOpVectorTimesScalar:
5082 return true;
5083 case EOpVectorTimesScalarAssign:
5084 ASSERT(!left.isMatrix() && !right.isMatrix());
5085 return left.isVector() && !right.isVector();
5086 case EOpVectorTimesMatrix:
5087 return left.getNominalSize() == right.getRows();
5088 case EOpVectorTimesMatrixAssign:
5089 ASSERT(!left.isMatrix() && right.isMatrix());
5090 return left.isVector() && left.getNominalSize() == right.getRows() &&
5091 left.getNominalSize() == right.getCols();
5092 case EOpMatrixTimesVector:
5093 return left.getCols() == right.getNominalSize();
5094 case EOpMatrixTimesScalar:
5095 return true;
5096 case EOpMatrixTimesScalarAssign:
5097 ASSERT(left.isMatrix() && !right.isMatrix());
5098 return !right.isVector();
5099 case EOpMatrixTimesMatrix:
5100 return left.getCols() == right.getRows();
5101 case EOpMatrixTimesMatrixAssign:
5102 ASSERT(left.isMatrix() && right.isMatrix());
5103 // We need to check two things:
5104 // 1. The matrix multiplication step is valid.
5105 // 2. The result will have the same number of columns as the lvalue.
5106 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5107
5108 default:
5109 UNREACHABLE();
5110 return false;
5111 }
5112}
5113
Jamie Madillb98c3a82015-07-23 14:26:04 -04005114TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5115 TIntermTyped *left,
5116 TIntermTyped *right,
5117 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005118{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005119 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005120 return nullptr;
5121
Olli Etuahofc1806e2015-03-17 13:03:11 +02005122 switch (op)
5123 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005124 case EOpEqual:
5125 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005126 case EOpLessThan:
5127 case EOpGreaterThan:
5128 case EOpLessThanEqual:
5129 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005130 break;
5131 case EOpLogicalOr:
5132 case EOpLogicalXor:
5133 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005134 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5135 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005136 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005137 {
5138 return nullptr;
5139 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005140 // Basic types matching should have been already checked.
5141 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005142 break;
5143 case EOpAdd:
5144 case EOpSub:
5145 case EOpDiv:
5146 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005147 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5148 !right->getType().getStruct());
5149 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005150 {
5151 return nullptr;
5152 }
5153 break;
5154 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005155 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5156 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005157 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005158 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005159 {
5160 return nullptr;
5161 }
5162 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005163 default:
5164 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005165 }
5166
Olli Etuaho1dded802016-08-18 18:13:13 +03005167 if (op == EOpMul)
5168 {
5169 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5170 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5171 {
5172 return nullptr;
5173 }
5174 }
5175
Olli Etuaho3fdec912016-08-18 15:08:06 +03005176 TIntermBinary *node = new TIntermBinary(op, left, right);
5177 node->setLine(loc);
5178
Olli Etuaho3fdec912016-08-18 15:08:06 +03005179 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005180 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005181}
5182
Jamie Madillb98c3a82015-07-23 14:26:04 -04005183TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5184 TIntermTyped *left,
5185 TIntermTyped *right,
5186 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005187{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005188 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005189 if (node == 0)
5190 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005191 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5192 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005193 return left;
5194 }
5195 return node;
5196}
5197
Jamie Madillb98c3a82015-07-23 14:26:04 -04005198TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5199 TIntermTyped *left,
5200 TIntermTyped *right,
5201 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005202{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005203 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005204 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005205 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005206 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5207 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005208 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005209 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005210 }
5211 return node;
5212}
5213
Olli Etuaho13389b62016-10-16 11:48:18 +01005214TIntermBinary *TParseContext::createAssign(TOperator op,
5215 TIntermTyped *left,
5216 TIntermTyped *right,
5217 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005218{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005219 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005220 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005221 if (op == EOpMulAssign)
5222 {
5223 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5224 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5225 {
5226 return nullptr;
5227 }
5228 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005229 TIntermBinary *node = new TIntermBinary(op, left, right);
5230 node->setLine(loc);
5231
Olli Etuaho3fdec912016-08-18 15:08:06 +03005232 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005233 }
5234 return nullptr;
5235}
5236
Jamie Madillb98c3a82015-07-23 14:26:04 -04005237TIntermTyped *TParseContext::addAssign(TOperator op,
5238 TIntermTyped *left,
5239 TIntermTyped *right,
5240 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005241{
Olli Etuahocce89652017-06-19 16:04:09 +03005242 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005243 TIntermTyped *node = createAssign(op, left, right, loc);
5244 if (node == nullptr)
5245 {
5246 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005247 return left;
5248 }
5249 return node;
5250}
5251
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005252TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5253 TIntermTyped *right,
5254 const TSourceLoc &loc)
5255{
Corentin Wallez0d959252016-07-12 17:26:32 -04005256 // WebGL2 section 5.26, the following results in an error:
5257 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005258 if (mShaderSpec == SH_WEBGL2_SPEC &&
5259 (left->isArray() || left->getBasicType() == EbtVoid ||
5260 left->getType().isStructureContainingArrays() || right->isArray() ||
5261 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005262 {
5263 error(loc,
5264 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5265 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005266 }
5267
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005268 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5269 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5270 commaNode->getTypePointer()->setQualifier(resultQualifier);
5271 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005272}
5273
Olli Etuaho49300862015-02-20 14:54:49 +02005274TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5275{
5276 switch (op)
5277 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005278 case EOpContinue:
5279 if (mLoopNestingLevel <= 0)
5280 {
5281 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005282 }
5283 break;
5284 case EOpBreak:
5285 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5286 {
5287 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005288 }
5289 break;
5290 case EOpReturn:
5291 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5292 {
5293 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005294 }
5295 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005296 case EOpKill:
5297 if (mShaderType != GL_FRAGMENT_SHADER)
5298 {
5299 error(loc, "discard supported in fragment shaders only", "discard");
5300 }
5301 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005302 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005303 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005304 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005305 }
Olli Etuahocce89652017-06-19 16:04:09 +03005306 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005307}
5308
Jamie Madillb98c3a82015-07-23 14:26:04 -04005309TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005310 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005311 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005312{
Olli Etuahocce89652017-06-19 16:04:09 +03005313 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005314 {
Olli Etuahocce89652017-06-19 16:04:09 +03005315 ASSERT(op == EOpReturn);
5316 mFunctionReturnsValue = true;
5317 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5318 {
5319 error(loc, "void function cannot return a value", "return");
5320 }
5321 else if (*mCurrentFunctionType != expression->getType())
5322 {
5323 error(loc, "function return is not matching type:", "return");
5324 }
Olli Etuaho49300862015-02-20 14:54:49 +02005325 }
Olli Etuahocce89652017-06-19 16:04:09 +03005326 TIntermBranch *node = new TIntermBranch(op, expression);
5327 node->setLine(loc);
5328 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005329}
5330
Martin Radev84aa2dc2017-09-11 15:51:02 +03005331void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5332{
5333 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
5334 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5335 bool isTextureGather = (name == "textureGather");
5336 bool isTextureGatherOffset = (name == "textureGatherOffset");
5337 if (isTextureGather || isTextureGatherOffset)
5338 {
5339 TIntermNode *componentNode = nullptr;
5340 TIntermSequence *arguments = functionCall->getSequence();
5341 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5342 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5343 ASSERT(sampler != nullptr);
5344 switch (sampler->getBasicType())
5345 {
5346 case EbtSampler2D:
5347 case EbtISampler2D:
5348 case EbtUSampler2D:
5349 case EbtSampler2DArray:
5350 case EbtISampler2DArray:
5351 case EbtUSampler2DArray:
5352 if ((isTextureGather && arguments->size() == 3u) ||
5353 (isTextureGatherOffset && arguments->size() == 4u))
5354 {
5355 componentNode = arguments->back();
5356 }
5357 break;
5358 case EbtSamplerCube:
5359 case EbtISamplerCube:
5360 case EbtUSamplerCube:
5361 ASSERT(!isTextureGatherOffset);
5362 if (arguments->size() == 3u)
5363 {
5364 componentNode = arguments->back();
5365 }
5366 break;
5367 case EbtSampler2DShadow:
5368 case EbtSampler2DArrayShadow:
5369 case EbtSamplerCubeShadow:
5370 break;
5371 default:
5372 UNREACHABLE();
5373 break;
5374 }
5375 if (componentNode)
5376 {
5377 const TIntermConstantUnion *componentConstantUnion =
5378 componentNode->getAsConstantUnion();
5379 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5380 {
5381 error(functionCall->getLine(), "Texture component must be a constant expression",
5382 name.c_str());
5383 }
5384 else
5385 {
5386 int component = componentConstantUnion->getIConst(0);
5387 if (component < 0 || component > 3)
5388 {
5389 error(functionCall->getLine(), "Component must be in the range [0;3]",
5390 name.c_str());
5391 }
5392 }
5393 }
5394 }
5395}
5396
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005397void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5398{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005399 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01005400 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005401 TIntermNode *offset = nullptr;
5402 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005403 bool useTextureGatherOffsetConstraints = false;
Olli Etuahoec9232b2017-03-27 17:01:37 +03005404 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5405 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5406 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005407 {
5408 offset = arguments->back();
5409 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005410 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005411 {
5412 // A bias parameter might follow the offset parameter.
5413 ASSERT(arguments->size() >= 3);
5414 offset = (*arguments)[2];
5415 }
Martin Radev84aa2dc2017-09-11 15:51:02 +03005416 else if (name == "textureGatherOffset")
5417 {
5418 ASSERT(arguments->size() >= 3u);
5419 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5420 ASSERT(sampler != nullptr);
5421 switch (sampler->getBasicType())
5422 {
5423 case EbtSampler2D:
5424 case EbtISampler2D:
5425 case EbtUSampler2D:
5426 case EbtSampler2DArray:
5427 case EbtISampler2DArray:
5428 case EbtUSampler2DArray:
5429 offset = (*arguments)[2];
5430 break;
5431 case EbtSampler2DShadow:
5432 case EbtSampler2DArrayShadow:
5433 offset = (*arguments)[3];
5434 break;
5435 default:
5436 UNREACHABLE();
5437 break;
5438 }
5439 useTextureGatherOffsetConstraints = true;
5440 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005441 if (offset != nullptr)
5442 {
5443 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5444 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5445 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005446 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005447 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005448 }
5449 else
5450 {
5451 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5452 size_t size = offsetConstantUnion->getType().getObjectSize();
5453 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005454 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5455 : mMinProgramTexelOffset;
5456 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5457 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005458 for (size_t i = 0u; i < size; ++i)
5459 {
5460 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005461 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005462 {
5463 std::stringstream tokenStream;
5464 tokenStream << offsetValue;
5465 std::string token = tokenStream.str();
5466 error(offset->getLine(), "Texture offset value out of valid range",
5467 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005468 }
5469 }
5470 }
5471 }
5472}
5473
Martin Radev2cc85b32016-08-05 16:22:53 +03005474// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5475void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5476{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005477 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005478 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5479
5480 if (name.compare(0, 5, "image") == 0)
5481 {
5482 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005483 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005484
Olli Etuaho485eefd2017-02-14 17:40:06 +00005485 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005486
5487 if (name.compare(5, 5, "Store") == 0)
5488 {
5489 if (memoryQualifier.readonly)
5490 {
5491 error(imageNode->getLine(),
5492 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005493 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005494 }
5495 }
5496 else if (name.compare(5, 4, "Load") == 0)
5497 {
5498 if (memoryQualifier.writeonly)
5499 {
5500 error(imageNode->getLine(),
5501 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005502 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005503 }
5504 }
5505 }
5506}
5507
5508// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5509void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5510 const TFunction *functionDefinition,
5511 const TIntermAggregate *functionCall)
5512{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005513 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005514
5515 const TIntermSequence &arguments = *functionCall->getSequence();
5516
5517 ASSERT(functionDefinition->getParamCount() == arguments.size());
5518
5519 for (size_t i = 0; i < arguments.size(); ++i)
5520 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005521 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5522 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005523 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5524 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5525
5526 if (IsImage(functionArgumentType.getBasicType()))
5527 {
5528 const TMemoryQualifier &functionArgumentMemoryQualifier =
5529 functionArgumentType.getMemoryQualifier();
5530 const TMemoryQualifier &functionParameterMemoryQualifier =
5531 functionParameterType.getMemoryQualifier();
5532 if (functionArgumentMemoryQualifier.readonly &&
5533 !functionParameterMemoryQualifier.readonly)
5534 {
5535 error(functionCall->getLine(),
5536 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005537 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005538 }
5539
5540 if (functionArgumentMemoryQualifier.writeonly &&
5541 !functionParameterMemoryQualifier.writeonly)
5542 {
5543 error(functionCall->getLine(),
5544 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005545 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005546 }
Martin Radev049edfa2016-11-11 14:35:37 +02005547
5548 if (functionArgumentMemoryQualifier.coherent &&
5549 !functionParameterMemoryQualifier.coherent)
5550 {
5551 error(functionCall->getLine(),
5552 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005553 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005554 }
5555
5556 if (functionArgumentMemoryQualifier.volatileQualifier &&
5557 !functionParameterMemoryQualifier.volatileQualifier)
5558 {
5559 error(functionCall->getLine(),
5560 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005561 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005562 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005563 }
5564 }
5565}
5566
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005567TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005568{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005569 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005570}
5571
5572TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005573 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005574 TIntermNode *thisNode,
5575 const TSourceLoc &loc)
5576{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005577 if (thisNode != nullptr)
5578 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005579 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005580 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005581
5582 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005583 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005584 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005585 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005586 }
5587 else
5588 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005589 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005590 return addNonConstructorFunctionCall(fnCall, arguments, loc);
5591 }
5592}
5593
5594TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
5595 TIntermSequence *arguments,
5596 TIntermNode *thisNode,
5597 const TSourceLoc &loc)
5598{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005599 TIntermTyped *typedThis = thisNode->getAsTyped();
5600 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5601 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5602 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
5603 // So accessing fnCall->getName() below is safe.
5604 if (fnCall->getName() != "length")
5605 {
5606 error(loc, "invalid method", fnCall->getName().c_str());
5607 }
5608 else if (!arguments->empty())
5609 {
5610 error(loc, "method takes no parameters", "length");
5611 }
5612 else if (typedThis == nullptr || !typedThis->isArray())
5613 {
5614 error(loc, "length can only be called on arrays", "length");
5615 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005616 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5617 mGeometryShaderInputPrimitiveType == EptUndefined)
5618 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08005619 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005620 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5621 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005622 else
5623 {
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005624 TIntermUnary *node = new TIntermUnary(EOpArrayLength, typedThis);
5625 node->setLine(loc);
5626 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005627 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005628 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005629}
5630
5631TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
5632 TIntermSequence *arguments,
5633 const TSourceLoc &loc)
5634{
5635 // First find by unmangled name to check whether the function name has been
5636 // hidden by a variable name or struct typename.
5637 // If a function is found, check for one with a matching argument list.
5638 bool builtIn;
5639 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
5640 if (symbol != nullptr && !symbol->isFunction())
5641 {
5642 error(loc, "function name expected", fnCall->getName().c_str());
5643 }
5644 else
5645 {
5646 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
5647 mShaderVersion, &builtIn);
5648 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005649 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005650 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
5651 }
5652 else
5653 {
5654 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005655 //
5656 // A declared function.
5657 //
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03005658 if (builtIn && fnCandidate->getExtension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005659 {
Olli Etuaho856c4972016-08-08 11:38:39 +03005660 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005661 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005662 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005663 if (builtIn && op != EOpNull)
5664 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005665 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005666 if (fnCandidate->getParamCount() == 1)
5667 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005668 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005669 TIntermNode *unaryParamNode = arguments->front();
5670 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005671 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005672 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005673 }
5674 else
5675 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005676 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005677 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005678 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005679
5680 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005681 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305682
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005683 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305684 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005685 // See if we can constant fold a built-in. Note that this may be possible
5686 // even if it is not const-qualified.
5687 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305688 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005689 else
5690 {
5691 return callNode;
5692 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005693 }
5694 }
5695 else
5696 {
5697 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005698 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005699
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005700 // If builtIn == false, the function is user defined - could be an overloaded
5701 // built-in as well.
5702 // if builtIn == true, it's a builtIn function with no op associated with it.
5703 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005704 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005705 {
Olli Etuahofe486322017-03-21 09:30:54 +00005706 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005707 checkTextureOffsetConst(callNode);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005708 checkTextureGather(callNode);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005709 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005710 }
5711 else
5712 {
Olli Etuahofe486322017-03-21 09:30:54 +00005713 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005714 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005715 }
5716
Jiajia Qinbc585152017-06-23 15:42:17 +08005717 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005718
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005719 callNode->setLine(loc);
5720
5721 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005722 }
5723 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005724 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005725
5726 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005727 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005728}
5729
Jamie Madillb98c3a82015-07-23 14:26:04 -04005730TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005731 TIntermTyped *trueExpression,
5732 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005733 const TSourceLoc &loc)
5734{
Olli Etuaho56229f12017-07-10 14:16:33 +03005735 if (!checkIsScalarBool(loc, cond))
5736 {
5737 return falseExpression;
5738 }
Olli Etuaho52901742015-04-15 13:42:45 +03005739
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005740 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005741 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005742 std::stringstream reasonStream;
5743 reasonStream << "mismatching ternary operator operand types '"
5744 << trueExpression->getCompleteString() << " and '"
5745 << falseExpression->getCompleteString() << "'";
5746 std::string reason = reasonStream.str();
5747 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005748 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005749 }
Olli Etuahode318b22016-10-25 16:18:25 +01005750 if (IsOpaqueType(trueExpression->getBasicType()))
5751 {
5752 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005753 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005754 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5755 // Note that structs containing opaque types don't need to be checked as structs are
5756 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005757 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005758 return falseExpression;
5759 }
5760
Jiajia Qinbc585152017-06-23 15:42:17 +08005761 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5762 falseExpression->getMemoryQualifier().writeonly)
5763 {
5764 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5765 return falseExpression;
5766 }
5767
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005768 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005769 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005770 // ESSL 3.00.6 section 5.7:
5771 // Ternary operator support is optional for arrays. No certainty that it works across all
5772 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5773 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005774 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005775 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005776 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005777 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005778 }
Olli Etuaho94050052017-05-08 14:17:44 +03005779 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5780 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005781 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005782 return falseExpression;
5783 }
5784
Corentin Wallez0d959252016-07-12 17:26:32 -04005785 // WebGL2 section 5.26, the following results in an error:
5786 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005787 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005788 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005789 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005790 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005791 }
5792
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005793 // Note that the node resulting from here can be a constant union without being qualified as
5794 // constant.
5795 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5796 node->setLine(loc);
5797
5798 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005799}
Olli Etuaho49300862015-02-20 14:54:49 +02005800
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005801//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005802// Parse an array of strings using yyparse.
5803//
5804// Returns 0 for success.
5805//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005806int PaParseStrings(size_t count,
5807 const char *const string[],
5808 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305809 TParseContext *context)
5810{
Yunchao He4f285442017-04-21 12:15:49 +08005811 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005812 return 1;
5813
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005814 if (glslang_initialize(context))
5815 return 1;
5816
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005817 int error = glslang_scan(count, string, length, context);
5818 if (!error)
5819 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005820
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005821 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005822
alokp@chromium.org6b495712012-06-29 00:06:58 +00005823 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005824}
Jamie Madill45bcc782016-11-07 13:58:48 -05005825
5826} // namespace sh