blob: 8d472665226bbf34202a651fa9f2b882bb8541d1 [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,
Olli Etuaho55bde912017-10-25 13:41:13 +03001033 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001034{
Olli Etuaho3739d232015-04-08 12:23:44 +03001035 ASSERT(type != nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03001036 if (type->getQualifier() == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001037 {
1038 // Make the qualifier make sense.
Olli Etuaho55bde912017-10-25 13:41:13 +03001039 type->setQualifier(EvqTemporary);
Olli Etuaho3739d232015-04-08 12:23:44 +03001040
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 Etuaho55bde912017-10-25 13:41:13 +03001056 // This will make the type sized if it isn't sized yet.
1057 checkIsNotUnsizedArray(line, "implicitly sized arrays need to be initialized",
1058 identifier.c_str(), type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001059}
1060
Olli Etuaho2935c582015-04-08 14:32:06 +03001061// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001062// and update the symbol table.
1063//
Olli Etuaho2935c582015-04-08 14:32:06 +03001064// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001065//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001066bool TParseContext::declareVariable(const TSourceLoc &line,
1067 const TString &identifier,
1068 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001069 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070{
Olli Etuaho2935c582015-04-08 14:32:06 +03001071 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001072
Olli Etuaho43364892017-02-13 16:00:12 +00001073 checkBindingIsValid(line, type);
1074
Olli Etuaho856c4972016-08-08 11:38:39 +03001075 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001076
Olli Etuaho2935c582015-04-08 14:32:06 +03001077 // gl_LastFragData may be redeclared with a new precision qualifier
1078 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1079 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001080 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1081 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001082 if (type.isArrayOfArrays())
1083 {
1084 error(line, "redeclaration of gl_LastFragData as an array of arrays",
1085 identifier.c_str());
1086 return false;
1087 }
1088 else if (static_cast<int>(type.getOutermostArraySize()) ==
1089 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001090 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001091 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001092 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001093 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001094 }
1095 }
1096 else
1097 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001098 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1099 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001100 return false;
1101 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001102 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001103
Olli Etuaho8a176262016-08-16 14:23:01 +03001104 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001105 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001106
Olli Etuaho0f684632017-07-13 12:42:15 +03001107 (*variable) = symbolTable.declareVariable(&identifier, type);
1108 if (!(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001109 {
1110 error(line, "redefinition", identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001111 return false;
1112 }
1113
Olli Etuaho8a176262016-08-16 14:23:01 +03001114 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001115 return false;
1116
1117 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001118}
1119
Martin Radev70866b82016-07-22 15:27:42 +03001120void TParseContext::checkIsParameterQualifierValid(
1121 const TSourceLoc &line,
1122 const TTypeQualifierBuilder &typeQualifierBuilder,
1123 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301124{
Olli Etuahocce89652017-06-19 16:04:09 +03001125 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001126 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001127
1128 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301129 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001130 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1131 }
1132
1133 if (!IsImage(type->getBasicType()))
1134 {
Olli Etuaho43364892017-02-13 16:00:12 +00001135 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001136 }
1137 else
1138 {
1139 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001140 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001141
Martin Radev70866b82016-07-22 15:27:42 +03001142 type->setQualifier(typeQualifier.qualifier);
1143
1144 if (typeQualifier.precision != EbpUndefined)
1145 {
1146 type->setPrecision(typeQualifier.precision);
1147 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001148}
1149
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001150bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001151{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001152 ASSERT(extension != TExtension::UNDEFINED);
Jamie Madillb98c3a82015-07-23 14:26:04 -04001153 const TExtensionBehavior &extBehavior = extensionBehavior();
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001154 TExtensionBehavior::const_iterator iter = extBehavior.find(extension);
Arun Patole7e7e68d2015-05-22 12:02:25 +05301155 if (iter == extBehavior.end())
1156 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001157 error(line, "extension is not supported", GetExtensionNameString(extension));
Olli Etuaho8a176262016-08-16 14:23:01 +03001158 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001159 }
zmo@google.comf5450912011-09-09 01:37:19 +00001160 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +05301161 if (iter->second == EBhDisable || iter->second == EBhUndefined)
1162 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001163 error(line, "extension is disabled", GetExtensionNameString(extension));
Olli Etuaho8a176262016-08-16 14:23:01 +03001164 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001165 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301166 if (iter->second == EBhWarn)
1167 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001168 warning(line, "extension is being used", GetExtensionNameString(extension));
Olli Etuaho8a176262016-08-16 14:23:01 +03001169 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001170 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001171
Olli Etuaho8a176262016-08-16 14:23:01 +03001172 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001173}
1174
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001175// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1176// compile-time or link-time errors are the same whether or not the declaration is empty".
1177// This function implements all the checks that are done on qualifiers regardless of if the
1178// declaration is empty.
1179void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1180 const sh::TLayoutQualifier &layoutQualifier,
1181 const TSourceLoc &location)
1182{
1183 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1184 {
1185 error(location, "Shared memory declarations cannot have layout specified", "layout");
1186 }
1187
1188 if (layoutQualifier.matrixPacking != EmpUnspecified)
1189 {
1190 error(location, "layout qualifier only valid for interface blocks",
1191 getMatrixPackingString(layoutQualifier.matrixPacking));
1192 return;
1193 }
1194
1195 if (layoutQualifier.blockStorage != EbsUnspecified)
1196 {
1197 error(location, "layout qualifier only valid for interface blocks",
1198 getBlockStorageString(layoutQualifier.blockStorage));
1199 return;
1200 }
1201
1202 if (qualifier == EvqFragmentOut)
1203 {
1204 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1205 {
1206 error(location, "invalid layout qualifier combination", "yuv");
1207 return;
1208 }
1209 }
1210 else
1211 {
1212 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1213 }
1214
Olli Etuaho95468d12017-05-04 11:14:34 +03001215 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1216 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001217 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1218 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001219 {
1220 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1221 }
1222
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001223 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001224 if (mShaderVersion >= 310)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001225 {
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001226 canHaveLocation = canHaveLocation || qualifier == EvqUniform || IsVarying(qualifier);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001227 // We're not checking whether the uniform location is in range here since that depends on
1228 // the type of the variable.
1229 // The type can only be fully determined for non-empty declarations.
1230 }
1231 if (!canHaveLocation)
1232 {
1233 checkLocationIsNotSpecified(location, layoutQualifier);
1234 }
1235}
1236
jchen104cdac9e2017-05-08 11:01:20 +08001237void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1238 const TSourceLoc &location)
1239{
1240 if (publicType.precision != EbpHigh)
1241 {
1242 error(location, "Can only be highp", "atomic counter");
1243 }
1244 // dEQP enforces compile error if location is specified. See uniform_location.test.
1245 if (publicType.layoutQualifier.location != -1)
1246 {
1247 error(location, "location must not be set for atomic_uint", "layout");
1248 }
1249 if (publicType.layoutQualifier.binding == -1)
1250 {
1251 error(location, "no binding specified", "atomic counter");
1252 }
1253}
1254
Olli Etuaho55bde912017-10-25 13:41:13 +03001255void TParseContext::emptyDeclarationErrorCheck(const TType &type, const TSourceLoc &location)
Martin Radevb8b01222016-11-20 23:25:53 +02001256{
Olli Etuaho55bde912017-10-25 13:41:13 +03001257 if (type.isUnsizedArray())
Martin Radevb8b01222016-11-20 23:25:53 +02001258 {
1259 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1260 // error. It is assumed that this applies to empty declarations as well.
1261 error(location, "empty array declaration needs to specify a size", "");
1262 }
Martin Radevb8b01222016-11-20 23:25:53 +02001263}
1264
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001265// These checks are done for all declarations that are non-empty. They're done for non-empty
1266// declarations starting a declarator list, and declarators that follow an empty declaration.
1267void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1268 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001269{
Olli Etuahofa33d582015-04-09 14:33:12 +03001270 switch (publicType.qualifier)
1271 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001272 case EvqVaryingIn:
1273 case EvqVaryingOut:
1274 case EvqAttribute:
1275 case EvqVertexIn:
1276 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001277 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001278 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001279 {
1280 error(identifierLocation, "cannot be used with a structure",
1281 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001282 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001283 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001284 break;
1285 case EvqBuffer:
1286 if (publicType.getBasicType() != EbtInterfaceBlock)
1287 {
1288 error(identifierLocation,
1289 "cannot declare buffer variables at global scope(outside a block)",
1290 getQualifierString(publicType.qualifier));
1291 return;
1292 }
1293 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001294 default:
1295 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001296 }
jchen10cc2a10e2017-05-03 14:05:12 +08001297 std::string reason(getBasicString(publicType.getBasicType()));
1298 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001299 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001300 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001301 {
1302 return;
1303 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001304
Andrei Volykhina5527072017-03-22 16:46:30 +03001305 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1306 publicType.qualifier != EvqConst) &&
1307 publicType.getBasicType() == EbtYuvCscStandardEXT)
1308 {
1309 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1310 getQualifierString(publicType.qualifier));
1311 return;
1312 }
1313
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001314 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1315 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001316 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1317 // But invalid shaders may still reach here with an unsized array declaration.
Olli Etuaho55bde912017-10-25 13:41:13 +03001318 TType type(publicType);
1319 if (!type.isUnsizedArray())
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001320 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001321 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1322 publicType.layoutQualifier);
1323 }
1324 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001325
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001326 // check for layout qualifier issues
1327 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001328
Martin Radev2cc85b32016-08-05 16:22:53 +03001329 if (IsImage(publicType.getBasicType()))
1330 {
1331
1332 switch (layoutQualifier.imageInternalFormat)
1333 {
1334 case EiifRGBA32F:
1335 case EiifRGBA16F:
1336 case EiifR32F:
1337 case EiifRGBA8:
1338 case EiifRGBA8_SNORM:
1339 if (!IsFloatImage(publicType.getBasicType()))
1340 {
1341 error(identifierLocation,
1342 "internal image format requires a floating image type",
1343 getBasicString(publicType.getBasicType()));
1344 return;
1345 }
1346 break;
1347 case EiifRGBA32I:
1348 case EiifRGBA16I:
1349 case EiifRGBA8I:
1350 case EiifR32I:
1351 if (!IsIntegerImage(publicType.getBasicType()))
1352 {
1353 error(identifierLocation,
1354 "internal image format requires an integer image type",
1355 getBasicString(publicType.getBasicType()));
1356 return;
1357 }
1358 break;
1359 case EiifRGBA32UI:
1360 case EiifRGBA16UI:
1361 case EiifRGBA8UI:
1362 case EiifR32UI:
1363 if (!IsUnsignedImage(publicType.getBasicType()))
1364 {
1365 error(identifierLocation,
1366 "internal image format requires an unsigned image type",
1367 getBasicString(publicType.getBasicType()));
1368 return;
1369 }
1370 break;
1371 case EiifUnspecified:
1372 error(identifierLocation, "layout qualifier", "No image internal format specified");
1373 return;
1374 default:
1375 error(identifierLocation, "layout qualifier", "unrecognized token");
1376 return;
1377 }
1378
1379 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1380 switch (layoutQualifier.imageInternalFormat)
1381 {
1382 case EiifR32F:
1383 case EiifR32I:
1384 case EiifR32UI:
1385 break;
1386 default:
1387 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1388 {
1389 error(identifierLocation, "layout qualifier",
1390 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1391 "image variables must be qualified readonly and/or writeonly");
1392 return;
1393 }
1394 break;
1395 }
1396 }
1397 else
1398 {
Olli Etuaho43364892017-02-13 16:00:12 +00001399 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001400 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1401 }
jchen104cdac9e2017-05-08 11:01:20 +08001402
1403 if (IsAtomicCounter(publicType.getBasicType()))
1404 {
1405 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1406 }
1407 else
1408 {
1409 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1410 }
Olli Etuaho43364892017-02-13 16:00:12 +00001411}
Martin Radev2cc85b32016-08-05 16:22:53 +03001412
Olli Etuaho43364892017-02-13 16:00:12 +00001413void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1414{
1415 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001416 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1417 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1418 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1419 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1420 // when it comes to which shaders are accepted by the compiler.
1421 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001422 if (IsImage(type.getBasicType()))
1423 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001424 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1425 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001426 }
1427 else if (IsSampler(type.getBasicType()))
1428 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001429 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1430 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001431 }
jchen104cdac9e2017-05-08 11:01:20 +08001432 else if (IsAtomicCounter(type.getBasicType()))
1433 {
1434 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1435 }
Olli Etuaho43364892017-02-13 16:00:12 +00001436 else
1437 {
1438 ASSERT(!IsOpaqueType(type.getBasicType()));
1439 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001440 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001441}
1442
Olli Etuaho856c4972016-08-08 11:38:39 +03001443void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1444 const TString &layoutQualifierName,
1445 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001446{
1447
1448 if (mShaderVersion < versionRequired)
1449 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001450 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001451 }
1452}
1453
Olli Etuaho856c4972016-08-08 11:38:39 +03001454bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1455 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001456{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001457 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001458 for (size_t i = 0u; i < localSize.size(); ++i)
1459 {
1460 if (localSize[i] != -1)
1461 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001462 error(location,
1463 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1464 "global layout declaration",
1465 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001466 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001467 }
1468 }
1469
Olli Etuaho8a176262016-08-16 14:23:01 +03001470 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001471}
1472
Olli Etuaho43364892017-02-13 16:00:12 +00001473void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001474 TLayoutImageInternalFormat internalFormat)
1475{
1476 if (internalFormat != EiifUnspecified)
1477 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001478 error(location, "invalid layout qualifier: only valid when used with images",
1479 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001480 }
Olli Etuaho43364892017-02-13 16:00:12 +00001481}
1482
1483void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1484{
1485 if (binding != -1)
1486 {
1487 error(location,
1488 "invalid layout qualifier: only valid when used with opaque types or blocks",
1489 "binding");
1490 }
1491}
1492
jchen104cdac9e2017-05-08 11:01:20 +08001493void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1494{
1495 if (offset != -1)
1496 {
1497 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1498 "offset");
1499 }
1500}
1501
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001502void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1503 int binding,
1504 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001505{
1506 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001507 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001508 {
1509 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1510 }
1511}
1512
1513void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1514 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001515 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001516{
1517 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001518 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001519 {
1520 error(location, "sampler binding greater than maximum texture units", "binding");
1521 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001522}
1523
Jiajia Qinbc585152017-06-23 15:42:17 +08001524void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1525 const TQualifier &qualifier,
1526 int binding,
1527 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001528{
1529 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001530 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001531 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001532 if (binding + size > mMaxUniformBufferBindings)
1533 {
1534 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1535 "binding");
1536 }
1537 }
1538 else if (qualifier == EvqBuffer)
1539 {
1540 if (binding + size > mMaxShaderStorageBufferBindings)
1541 {
1542 error(location,
1543 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1544 "binding");
1545 }
jchen10af713a22017-04-19 09:10:56 +08001546 }
1547}
jchen104cdac9e2017-05-08 11:01:20 +08001548void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1549{
1550 if (binding >= mMaxAtomicCounterBindings)
1551 {
1552 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1553 "binding");
1554 }
1555}
jchen10af713a22017-04-19 09:10:56 +08001556
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001557void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1558 int objectLocationCount,
1559 const TLayoutQualifier &layoutQualifier)
1560{
1561 int loc = layoutQualifier.location;
1562 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1563 {
1564 error(location, "Uniform location out of range", "location");
1565 }
1566}
1567
Andrei Volykhina5527072017-03-22 16:46:30 +03001568void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1569{
1570 if (yuv != false)
1571 {
1572 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1573 }
1574}
1575
Jiajia Qinbc585152017-06-23 15:42:17 +08001576void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1577 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001578{
1579 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1580 {
1581 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001582 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1583 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1584 qual == EvqInOut || qual == EvqConstReadOnly))
1585 {
1586 if (argument->getMemoryQualifier().writeonly)
1587 {
1588 error(argument->getLine(),
1589 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
1590 fnCall->getFunctionSymbolInfo()->getName().c_str());
1591 return;
1592 }
1593 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001594 if (qual == EvqOut || qual == EvqInOut)
1595 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001596 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001597 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001598 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001599 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuahoec9232b2017-03-27 17:01:37 +03001600 fnCall->getFunctionSymbolInfo()->getName().c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001601 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001602 }
1603 }
1604 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001605}
1606
Martin Radev70866b82016-07-22 15:27:42 +03001607void TParseContext::checkInvariantVariableQualifier(bool invariant,
1608 const TQualifier qualifier,
1609 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001610{
Martin Radev70866b82016-07-22 15:27:42 +03001611 if (!invariant)
1612 return;
1613
1614 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001615 {
Martin Radev70866b82016-07-22 15:27:42 +03001616 // input variables in the fragment shader can be also qualified as invariant
1617 if (!sh::CanBeInvariantESSL1(qualifier))
1618 {
1619 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1620 }
1621 }
1622 else
1623 {
1624 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1625 {
1626 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1627 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001628 }
1629}
1630
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001631bool TParseContext::supportsExtension(TExtension extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001632{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001633 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001634 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1635 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001636}
1637
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001638bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001639{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001640 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001641}
1642
Jamie Madillb98c3a82015-07-23 14:26:04 -04001643void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1644 const char *extName,
1645 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001646{
1647 pp::SourceLocation srcLoc;
1648 srcLoc.file = loc.first_file;
1649 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001650 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001651}
1652
Jamie Madillb98c3a82015-07-23 14:26:04 -04001653void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1654 const char *name,
1655 const char *value,
1656 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001657{
1658 pp::SourceLocation srcLoc;
1659 srcLoc.file = loc.first_file;
1660 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001661 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001662}
1663
Martin Radev4c4c8e72016-08-04 12:25:34 +03001664sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001665{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001666 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001667 for (size_t i = 0u; i < result.size(); ++i)
1668 {
1669 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1670 {
1671 result[i] = 1;
1672 }
1673 else
1674 {
1675 result[i] = mComputeShaderLocalSize[i];
1676 }
1677 }
1678 return result;
1679}
1680
Olli Etuaho56229f12017-07-10 14:16:33 +03001681TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1682 const TSourceLoc &line)
1683{
1684 TIntermConstantUnion *node = new TIntermConstantUnion(
1685 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1686 node->setLine(line);
1687 return node;
1688}
1689
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001690/////////////////////////////////////////////////////////////////////////////////
1691//
1692// Non-Errors.
1693//
1694/////////////////////////////////////////////////////////////////////////////////
1695
Jamie Madill5c097022014-08-20 16:38:32 -04001696const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1697 const TString *name,
1698 const TSymbol *symbol)
1699{
Jamie Madill5c097022014-08-20 16:38:32 -04001700 if (!symbol)
1701 {
1702 error(location, "undeclared identifier", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001703 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001704 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001705
1706 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001707 {
1708 error(location, "variable expected", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001709 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001710 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001711
1712 const TVariable *variable = static_cast<const TVariable *>(symbol);
1713
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001714 if (variable->getExtension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001715 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001716 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001717 }
1718
Olli Etuaho0f684632017-07-13 12:42:15 +03001719 // Reject shaders using both gl_FragData and gl_FragColor
1720 TQualifier qualifier = variable->getType().getQualifier();
1721 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001722 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001723 mUsesFragData = true;
1724 }
1725 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1726 {
1727 mUsesFragColor = true;
1728 }
1729 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1730 {
1731 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001732 }
1733
Olli Etuaho0f684632017-07-13 12:42:15 +03001734 // This validation is not quite correct - it's only an error to write to
1735 // both FragData and FragColor. For simplicity, and because users shouldn't
1736 // be rewarded for reading from undefined varaibles, return an error
1737 // if they are both referenced, rather than assigned.
1738 if (mUsesFragData && mUsesFragColor)
1739 {
1740 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1741 if (mUsesSecondaryOutputs)
1742 {
1743 errorMessage =
1744 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1745 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1746 }
1747 error(location, errorMessage, name->c_str());
1748 }
1749
1750 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1751 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1752 qualifier == EvqWorkGroupSize)
1753 {
1754 error(location,
1755 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1756 "gl_WorkGroupSize");
1757 }
Jamie Madill5c097022014-08-20 16:38:32 -04001758 return variable;
1759}
1760
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001761TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1762 const TString *name,
1763 const TSymbol *symbol)
1764{
1765 const TVariable *variable = getNamedVariable(location, name, symbol);
1766
Olli Etuaho0f684632017-07-13 12:42:15 +03001767 if (!variable)
1768 {
1769 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1770 node->setLine(location);
1771 return node;
1772 }
1773
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001774 const TType &variableType = variable->getType();
Olli Etuaho56229f12017-07-10 14:16:33 +03001775 TIntermTyped *node = nullptr;
1776
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001777 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001778 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001779 const TConstantUnion *constArray = variable->getConstPointer();
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001780 node = new TIntermConstantUnion(constArray, variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001781 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001782 else if (variableType.getQualifier() == EvqWorkGroupSize && mComputeShaderLocalSizeDeclared)
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001783 {
1784 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1785 // needs to be added to the AST as a constant and not as a symbol.
1786 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1787 TConstantUnion *constArray = new TConstantUnion[3];
1788 for (size_t i = 0; i < 3; ++i)
1789 {
1790 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1791 }
1792
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001793 ASSERT(variableType.getBasicType() == EbtUInt);
1794 ASSERT(variableType.getObjectSize() == 3);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001795
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001796 TType type(variableType);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001797 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001798 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001799 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001800 else if ((mGeometryShaderInputPrimitiveType != EptUndefined) &&
1801 (variableType.getQualifier() == EvqPerVertexIn))
Jiawei Shaod8105a02017-08-08 09:54:36 +08001802 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001803 ASSERT(mGeometryShaderInputArraySize > 0u);
1804
1805 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), variableType);
Olli Etuaho55bde912017-10-25 13:41:13 +03001806 node->getTypePointer()->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
Jiawei Shaod8105a02017-08-08 09:54:36 +08001807 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001808 else
1809 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001810 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001811 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001812 ASSERT(node != nullptr);
1813 node->setLine(location);
1814 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001815}
1816
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001817// Initializers show up in several places in the grammar. Have one set of
1818// code to handle them here.
1819//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001820// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001821bool TParseContext::executeInitializer(const TSourceLoc &line,
1822 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001823 TType type,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001824 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001825 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001826{
Olli Etuaho13389b62016-10-16 11:48:18 +01001827 ASSERT(initNode != nullptr);
1828 ASSERT(*initNode == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001829
Olli Etuaho2935c582015-04-08 14:32:06 +03001830 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001831 if (type.isUnsizedArray())
1832 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001833 // In case initializer is not an array or type has more dimensions than initializer, this
1834 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1835 // actually is an array or not. Having a non-array initializer for an unsized array will
1836 // result in an error later, so we don't generate an error message here.
1837 type.sizeUnsizedArrays(initializer->getType().getArraySizes());
Olli Etuaho376f1b52015-04-13 13:23:41 +03001838 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001839 if (!declareVariable(line, identifier, type, &variable))
1840 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001841 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001842 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001843
Olli Etuahob0c645e2015-05-12 14:25:36 +03001844 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001845 if (symbolTable.atGlobalLevel() &&
1846 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001847 {
1848 // Error message does not completely match behavior with ESSL 1.00, but
1849 // we want to steer developers towards only using constant expressions.
1850 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001851 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001852 }
1853 if (globalInitWarning)
1854 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001855 warning(
1856 line,
1857 "global variable initializers should be constant expressions "
1858 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1859 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001860 }
1861
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001862 //
1863 // identifier must be of type constant, a global, or a temporary
1864 //
1865 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301866 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1867 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001868 error(line, " cannot initialize this type of qualifier ",
1869 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001870 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001871 }
1872 //
1873 // test for and propagate constant
1874 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001875
Arun Patole7e7e68d2015-05-22 12:02:25 +05301876 if (qualifier == EvqConst)
1877 {
1878 if (qualifier != initializer->getType().getQualifier())
1879 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001880 std::stringstream reasonStream;
1881 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1882 << "'";
1883 std::string reason = reasonStream.str();
1884 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001885 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001886 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001887 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301888 if (type != initializer->getType())
1889 {
1890 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001891 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001892 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001893 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001894 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001895
1896 // Save the constant folded value to the variable if possible. For example array
1897 // initializers are not folded, since that way copying the array literal to multiple places
1898 // in the shader is avoided.
1899 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1900 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301901 if (initializer->getAsConstantUnion())
1902 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001903 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001904 ASSERT(*initNode == nullptr);
1905 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301906 }
1907 else if (initializer->getAsSymbolNode())
1908 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001909 const TSymbol *symbol =
1910 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1911 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001912
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001913 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001914 if (constArray)
1915 {
1916 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001917 ASSERT(*initNode == nullptr);
1918 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001919 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001920 }
1921 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001922
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001923 TIntermSymbol *intermSymbol =
1924 new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
1925 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001926 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1927 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001928 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001929 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001930 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001931 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001932
Olli Etuaho914b79a2017-06-19 16:03:19 +03001933 return true;
1934}
1935
1936TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
1937 const TString &identifier,
1938 TIntermTyped *initializer,
1939 const TSourceLoc &loc)
1940{
1941 checkIsScalarBool(loc, pType);
1942 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03001943 TType type(pType);
1944 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03001945 {
1946 // The initializer is valid. The init condition needs to have a node - either the
1947 // initializer node, or a constant node in case the initialized variable is const and won't
1948 // be recorded in the AST.
1949 if (initNode == nullptr)
1950 {
1951 return initializer;
1952 }
1953 else
1954 {
1955 TIntermDeclaration *declaration = new TIntermDeclaration();
1956 declaration->appendDeclarator(initNode);
1957 return declaration;
1958 }
1959 }
1960 return nullptr;
1961}
1962
1963TIntermNode *TParseContext::addLoop(TLoopType type,
1964 TIntermNode *init,
1965 TIntermNode *cond,
1966 TIntermTyped *expr,
1967 TIntermNode *body,
1968 const TSourceLoc &line)
1969{
1970 TIntermNode *node = nullptr;
1971 TIntermTyped *typedCond = nullptr;
1972 if (cond)
1973 {
1974 typedCond = cond->getAsTyped();
1975 }
1976 if (cond == nullptr || typedCond)
1977 {
Olli Etuahocce89652017-06-19 16:04:09 +03001978 if (type == ELoopDoWhile)
1979 {
1980 checkIsScalarBool(line, typedCond);
1981 }
1982 // In the case of other loops, it was checked before that the condition is a scalar boolean.
1983 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
1984 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
1985 !typedCond->isVector()));
1986
Olli Etuaho3ec75682017-07-05 17:02:55 +03001987 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03001988 node->setLine(line);
1989 return node;
1990 }
1991
Olli Etuahocce89652017-06-19 16:04:09 +03001992 ASSERT(type != ELoopDoWhile);
1993
Olli Etuaho914b79a2017-06-19 16:03:19 +03001994 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
1995 ASSERT(declaration);
1996 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
1997 ASSERT(declarator->getLeft()->getAsSymbolNode());
1998
1999 // The condition is a declaration. In the AST representation we don't support declarations as
2000 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2001 // the loop.
2002 TIntermBlock *block = new TIntermBlock();
2003
2004 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2005 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2006 block->appendStatement(declareCondition);
2007
2008 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2009 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002010 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002011 block->appendStatement(loop);
2012 loop->setLine(line);
2013 block->setLine(line);
2014 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002015}
2016
Olli Etuahocce89652017-06-19 16:04:09 +03002017TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2018 TIntermNodePair code,
2019 const TSourceLoc &loc)
2020{
Olli Etuaho56229f12017-07-10 14:16:33 +03002021 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002022
2023 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002024 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002025 {
2026 if (cond->getAsConstantUnion()->getBConst(0) == true)
2027 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002028 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002029 }
2030 else
2031 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002032 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002033 }
2034 }
2035
Olli Etuaho3ec75682017-07-05 17:02:55 +03002036 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002037 node->setLine(loc);
2038
2039 return node;
2040}
2041
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002042void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2043{
2044 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2045 typeSpecifier->getBasicType());
2046
2047 if (mShaderVersion < 300 && typeSpecifier->array)
2048 {
2049 error(typeSpecifier->getLine(), "not supported", "first-class array");
2050 typeSpecifier->clearArrayness();
2051 }
2052}
2053
Martin Radev70866b82016-07-22 15:27:42 +03002054TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302055 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002056{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002057 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002058
Martin Radev70866b82016-07-22 15:27:42 +03002059 TPublicType returnType = typeSpecifier;
2060 returnType.qualifier = typeQualifier.qualifier;
2061 returnType.invariant = typeQualifier.invariant;
2062 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002063 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002064 returnType.precision = typeSpecifier.precision;
2065
2066 if (typeQualifier.precision != EbpUndefined)
2067 {
2068 returnType.precision = typeQualifier.precision;
2069 }
2070
Martin Radev4a9cd802016-09-01 16:51:51 +03002071 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2072 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002073
Martin Radev4a9cd802016-09-01 16:51:51 +03002074 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2075 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002076
Martin Radev4a9cd802016-09-01 16:51:51 +03002077 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002078
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002079 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002080 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002081 if (typeSpecifier.array)
2082 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002083 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002084 returnType.clearArrayness();
2085 }
2086
Martin Radev70866b82016-07-22 15:27:42 +03002087 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002088 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002089 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002090 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002091 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002092 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002093
Martin Radev70866b82016-07-22 15:27:42 +03002094 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002095 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002096 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002097 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002098 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002099 }
2100 }
2101 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002102 {
Martin Radev70866b82016-07-22 15:27:42 +03002103 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002104 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002105 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002106 }
Martin Radev70866b82016-07-22 15:27:42 +03002107 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2108 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002109 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002110 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2111 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002112 }
Martin Radev70866b82016-07-22 15:27:42 +03002113 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002114 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002115 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002116 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002117 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002118 }
2119
2120 return returnType;
2121}
2122
Olli Etuaho856c4972016-08-08 11:38:39 +03002123void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2124 const TPublicType &type,
2125 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002126{
2127 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002128 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002129 {
2130 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002131 }
2132
2133 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2134 switch (qualifier)
2135 {
2136 case EvqVertexIn:
2137 // ESSL 3.00 section 4.3.4
2138 if (type.array)
2139 {
2140 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002141 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002142 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002143 return;
2144 case EvqFragmentOut:
2145 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002146 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002147 {
2148 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002149 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002150 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002151 return;
2152 default:
2153 break;
2154 }
2155
2156 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2157 // restrictions.
2158 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002159 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2160 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002161 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2162 {
2163 error(qualifierLocation, "must use 'flat' interpolation here",
2164 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002165 }
2166
Martin Radev4a9cd802016-09-01 16:51:51 +03002167 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002168 {
2169 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2170 // These restrictions are only implied by the ESSL 3.00 spec, but
2171 // the ESSL 3.10 spec lists these restrictions explicitly.
2172 if (type.array)
2173 {
2174 error(qualifierLocation, "cannot be an array of structures",
2175 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002176 }
2177 if (type.isStructureContainingArrays())
2178 {
2179 error(qualifierLocation, "cannot be a structure containing an array",
2180 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002181 }
2182 if (type.isStructureContainingType(EbtStruct))
2183 {
2184 error(qualifierLocation, "cannot be a structure containing a structure",
2185 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002186 }
2187 if (type.isStructureContainingType(EbtBool))
2188 {
2189 error(qualifierLocation, "cannot be a structure containing a bool",
2190 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002191 }
2192 }
2193}
2194
Martin Radev2cc85b32016-08-05 16:22:53 +03002195void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2196{
2197 if (qualifier.getType() == QtStorage)
2198 {
2199 const TStorageQualifierWrapper &storageQualifier =
2200 static_cast<const TStorageQualifierWrapper &>(qualifier);
2201 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2202 !symbolTable.atGlobalLevel())
2203 {
2204 error(storageQualifier.getLine(),
2205 "Local variables can only use the const storage qualifier.",
2206 storageQualifier.getQualifierString().c_str());
2207 }
2208 }
2209}
2210
Olli Etuaho43364892017-02-13 16:00:12 +00002211void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002212 const TSourceLoc &location)
2213{
Jiajia Qinbc585152017-06-23 15:42:17 +08002214 const std::string reason(
2215 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2216 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002217 if (memoryQualifier.readonly)
2218 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002219 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002220 }
2221 if (memoryQualifier.writeonly)
2222 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002223 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002224 }
Martin Radev049edfa2016-11-11 14:35:37 +02002225 if (memoryQualifier.coherent)
2226 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002227 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002228 }
2229 if (memoryQualifier.restrictQualifier)
2230 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002231 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002232 }
2233 if (memoryQualifier.volatileQualifier)
2234 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002235 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002236 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002237}
2238
jchen104cdac9e2017-05-08 11:01:20 +08002239// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2240// intermediate tree.
2241void TParseContext::checkAtomicCounterOffsetIsNotOverlapped(TPublicType &publicType,
2242 size_t size,
2243 bool forceAppend,
2244 const TSourceLoc &loc,
2245 TType &type)
2246{
2247 auto &bindingState = mAtomicCounterBindingStates[publicType.layoutQualifier.binding];
2248 int offset;
2249 if (publicType.layoutQualifier.offset == -1 || forceAppend)
2250 {
2251 offset = bindingState.appendSpan(size);
2252 }
2253 else
2254 {
2255 offset = bindingState.insertSpan(publicType.layoutQualifier.offset, size);
2256 }
2257 if (offset == -1)
2258 {
2259 error(loc, "Offset overlapping", "atomic counter");
2260 return;
2261 }
2262 TLayoutQualifier qualifier = type.getLayoutQualifier();
2263 qualifier.offset = offset;
2264 type.setLayoutQualifier(qualifier);
2265}
2266
Olli Etuaho13389b62016-10-16 11:48:18 +01002267TIntermDeclaration *TParseContext::parseSingleDeclaration(
2268 TPublicType &publicType,
2269 const TSourceLoc &identifierOrTypeLocation,
2270 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002271{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002272 TType type(publicType);
2273 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2274 mDirectiveHandler.pragma().stdgl.invariantAll)
2275 {
2276 TQualifier qualifier = type.getQualifier();
2277
2278 // The directive handler has already taken care of rejecting invalid uses of this pragma
2279 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2280 // affected variable declarations:
2281 //
2282 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2283 // elsewhere, in TranslatorGLSL.)
2284 //
2285 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2286 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2287 // the way this is currently implemented we have to enable this compiler option before
2288 // parsing the shader and determining the shading language version it uses. If this were
2289 // implemented as a post-pass, the workaround could be more targeted.
2290 //
2291 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2292 // the specification, but there are desktop OpenGL drivers that expect that this is the
2293 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2294 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2295 {
2296 type.setInvariant(true);
2297 }
2298 }
2299
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002300 if (IsGeometryShaderInput(mShaderType, type.getQualifier()))
2301 {
2302 error(identifierOrTypeLocation,
2303 "Geometry shader input varying variable must be declared as an array",
2304 identifier.c_str());
2305 }
2306
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002307 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2308 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002309
Olli Etuahobab4c082015-04-24 16:38:49 +03002310 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002311 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002312
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002313 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002314 if (emptyDeclaration)
2315 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002316 emptyDeclarationErrorCheck(type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002317 // In most cases we don't need to create a symbol node for an empty declaration.
2318 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2319 if (type.getBasicType() == EbtStruct)
2320 {
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03002321 symbol = new TIntermSymbol(symbolTable.getEmptySymbolId(), "", type);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002322 }
jchen104cdac9e2017-05-08 11:01:20 +08002323 else if (IsAtomicCounter(publicType.getBasicType()))
2324 {
2325 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2326 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002327 }
2328 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002329 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002330 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002331
Olli Etuaho55bde912017-10-25 13:41:13 +03002332 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002333
jchen104cdac9e2017-05-08 11:01:20 +08002334 if (IsAtomicCounter(publicType.getBasicType()))
2335 {
2336
2337 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, false,
2338 identifierOrTypeLocation, type);
2339 }
2340
Olli Etuaho2935c582015-04-08 14:32:06 +03002341 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002342 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002343
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002344 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002345 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002346 symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
Olli Etuaho13389b62016-10-16 11:48:18 +01002347 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002348 }
2349
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002350 TIntermDeclaration *declaration = new TIntermDeclaration();
2351 declaration->setLine(identifierOrTypeLocation);
2352 if (symbol)
2353 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002354 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002355 declaration->appendDeclarator(symbol);
2356 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002357 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002358}
2359
Olli Etuaho55bde912017-10-25 13:41:13 +03002360TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002361 const TSourceLoc &identifierLocation,
2362 const TString &identifier,
2363 const TSourceLoc &indexLocation,
Olli Etuaho55bde912017-10-25 13:41:13 +03002364 unsigned int arraySize)
Jamie Madill60ed9812013-06-06 11:56:46 -04002365{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002366 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002367
Olli Etuaho55bde912017-10-25 13:41:13 +03002368 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002369 identifierLocation);
2370
Olli Etuaho55bde912017-10-25 13:41:13 +03002371 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002372
Olli Etuaho55bde912017-10-25 13:41:13 +03002373 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002374
Olli Etuaho55bde912017-10-25 13:41:13 +03002375 TType arrayType(elementType);
2376 arrayType.makeArray(arraySize);
Jamie Madill60ed9812013-06-06 11:56:46 -04002377
Olli Etuaho55bde912017-10-25 13:41:13 +03002378 if (IsGeometryShaderInput(mShaderType, elementType.qualifier))
jchen104cdac9e2017-05-08 11:01:20 +08002379 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002380 if (arrayType.isUnsizedArray())
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002381 {
2382 // Set size for the unsized geometry shader inputs if they are declared after a valid
2383 // input primitive declaration.
2384 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2385 {
2386 ASSERT(mGeometryShaderInputArraySize > 0u);
Olli Etuaho55bde912017-10-25 13:41:13 +03002387 arrayType.sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002388 }
2389 else
2390 {
2391 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2392 // An input can be declared without an array size if there is a previous layout
2393 // which specifies the size.
2394 error(indexLocation,
2395 "Missing a valid input primitive declaration before declaring an unsized "
2396 "array input",
2397 "");
2398 }
2399 }
2400 else
2401 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002402 setGeometryShaderInputArraySize(arrayType.getOutermostArraySize(), indexLocation);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002403 }
2404 }
Olli Etuaho55bde912017-10-25 13:41:13 +03002405
2406 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2407
2408 if (IsAtomicCounter(elementType.getBasicType()))
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002409 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002410 checkAtomicCounterOffsetIsNotOverlapped(
2411 elementType, kAtomicCounterArrayStride * arrayType.getArraySizeProduct(), false,
2412 identifierLocation, arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002413 }
2414
Olli Etuaho2935c582015-04-08 14:32:06 +03002415 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002416 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002417
Olli Etuaho13389b62016-10-16 11:48:18 +01002418 TIntermDeclaration *declaration = new TIntermDeclaration();
2419 declaration->setLine(identifierLocation);
2420
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002421 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002422 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002423 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2424 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002425 declaration->appendDeclarator(symbol);
2426 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002427
Olli Etuaho13389b62016-10-16 11:48:18 +01002428 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002429}
2430
Olli Etuaho13389b62016-10-16 11:48:18 +01002431TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2432 const TSourceLoc &identifierLocation,
2433 const TString &identifier,
2434 const TSourceLoc &initLocation,
2435 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002436{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002437 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002438
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002439 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2440 identifierLocation);
2441
2442 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002443
Olli Etuaho13389b62016-10-16 11:48:18 +01002444 TIntermDeclaration *declaration = new TIntermDeclaration();
2445 declaration->setLine(identifierLocation);
2446
2447 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002448 TType type(publicType);
2449 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002450 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002451 if (initNode)
2452 {
2453 declaration->appendDeclarator(initNode);
2454 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002455 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002456 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002457}
2458
Olli Etuaho13389b62016-10-16 11:48:18 +01002459TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002460 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002461 const TSourceLoc &identifierLocation,
2462 const TString &identifier,
2463 const TSourceLoc &indexLocation,
Olli Etuaho55bde912017-10-25 13:41:13 +03002464 unsigned int arraySize,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002465 const TSourceLoc &initLocation,
2466 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002467{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002468 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002469
Olli Etuaho55bde912017-10-25 13:41:13 +03002470 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002471 identifierLocation);
2472
Olli Etuaho55bde912017-10-25 13:41:13 +03002473 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002474
Olli Etuaho55bde912017-10-25 13:41:13 +03002475 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002476
Olli Etuaho55bde912017-10-25 13:41:13 +03002477 TType arrayType(elementType);
2478 arrayType.makeArray(arraySize);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002479
Olli Etuaho13389b62016-10-16 11:48:18 +01002480 TIntermDeclaration *declaration = new TIntermDeclaration();
2481 declaration->setLine(identifierLocation);
2482
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002483 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002484 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002485 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002486 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002487 if (initNode)
2488 {
2489 declaration->appendDeclarator(initNode);
2490 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002491 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002492
2493 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002494}
2495
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002496TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002497 const TTypeQualifierBuilder &typeQualifierBuilder,
2498 const TSourceLoc &identifierLoc,
2499 const TString *identifier,
2500 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002501{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002502 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002503
Martin Radev70866b82016-07-22 15:27:42 +03002504 if (!typeQualifier.invariant)
2505 {
2506 error(identifierLoc, "Expected invariant", identifier->c_str());
2507 return nullptr;
2508 }
2509 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2510 {
2511 return nullptr;
2512 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002513 if (!symbol)
2514 {
2515 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002516 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002517 }
Martin Radev70866b82016-07-22 15:27:42 +03002518 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002519 {
Martin Radev70866b82016-07-22 15:27:42 +03002520 error(identifierLoc, "invariant declaration specifies qualifier",
2521 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002522 }
Martin Radev70866b82016-07-22 15:27:42 +03002523 if (typeQualifier.precision != EbpUndefined)
2524 {
2525 error(identifierLoc, "invariant declaration specifies precision",
2526 getPrecisionString(typeQualifier.precision));
2527 }
2528 if (!typeQualifier.layoutQualifier.isEmpty())
2529 {
2530 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2531 }
2532
2533 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002534 if (!variable)
2535 {
2536 return nullptr;
2537 }
Martin Radev70866b82016-07-22 15:27:42 +03002538 const TType &type = variable->getType();
2539
2540 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2541 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002542 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002543
2544 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2545
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002546 TIntermSymbol *intermSymbol = new TIntermSymbol(variable->getUniqueId(), *identifier, type);
2547 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002548
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002549 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002550}
2551
Olli Etuaho13389b62016-10-16 11:48:18 +01002552void TParseContext::parseDeclarator(TPublicType &publicType,
2553 const TSourceLoc &identifierLocation,
2554 const TString &identifier,
2555 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002556{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002557 // If the declaration starting this declarator list was empty (example: int,), some checks were
2558 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002559 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002560 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002561 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2562 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002563 }
2564
Olli Etuaho856c4972016-08-08 11:38:39 +03002565 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002566
Olli Etuaho2935c582015-04-08 14:32:06 +03002567 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002568 TType type(publicType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002569 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &type);
2570
jchen104cdac9e2017-05-08 11:01:20 +08002571 if (IsAtomicCounter(publicType.getBasicType()))
2572 {
2573 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, true,
2574 identifierLocation, type);
2575 }
Olli Etuaho43364892017-02-13 16:00:12 +00002576 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002577
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002578 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002579 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002580 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
2581 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002582 declarationOut->appendDeclarator(symbol);
2583 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002584}
2585
Olli Etuaho55bde912017-10-25 13:41:13 +03002586void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002587 const TSourceLoc &identifierLocation,
2588 const TString &identifier,
2589 const TSourceLoc &arrayLocation,
Olli Etuaho55bde912017-10-25 13:41:13 +03002590 unsigned int arraySize,
Olli Etuaho13389b62016-10-16 11:48:18 +01002591 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002592{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002593 // If the declaration starting this declarator list was empty (example: int,), some checks were
2594 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002595 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002596 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002597 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002598 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002599 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002600
Olli Etuaho55bde912017-10-25 13:41:13 +03002601 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002602
Olli Etuaho55bde912017-10-25 13:41:13 +03002603 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002604 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002605 TType arrayType(elementType);
2606 arrayType.makeArray(arraySize);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002607
Olli Etuaho55bde912017-10-25 13:41:13 +03002608 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2609
2610 if (IsAtomicCounter(elementType.getBasicType()))
jchen104cdac9e2017-05-08 11:01:20 +08002611 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002612 checkAtomicCounterOffsetIsNotOverlapped(
2613 elementType, kAtomicCounterArrayStride * arrayType.getArraySizeProduct(), true,
2614 identifierLocation, arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002615 }
2616
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002617 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002618 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002619
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002620 if (variable)
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002621 {
2622 TIntermSymbol *symbol =
2623 new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2624 symbol->setLine(identifierLocation);
2625 declarationOut->appendDeclarator(symbol);
2626 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002627 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002628}
2629
Olli Etuaho13389b62016-10-16 11:48:18 +01002630void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2631 const TSourceLoc &identifierLocation,
2632 const TString &identifier,
2633 const TSourceLoc &initLocation,
2634 TIntermTyped *initializer,
2635 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002636{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002637 // If the declaration starting this declarator list was empty (example: int,), some checks were
2638 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002639 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002640 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002641 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2642 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002643 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002644
Olli Etuaho856c4972016-08-08 11:38:39 +03002645 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002646
Olli Etuaho13389b62016-10-16 11:48:18 +01002647 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002648 TType type(publicType);
2649 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002650 {
2651 //
2652 // build the intermediate representation
2653 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002654 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002655 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002656 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002657 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002658 }
2659}
2660
Olli Etuaho55bde912017-10-25 13:41:13 +03002661void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002662 const TSourceLoc &identifierLocation,
2663 const TString &identifier,
2664 const TSourceLoc &indexLocation,
Olli Etuaho55bde912017-10-25 13:41:13 +03002665 unsigned int arraySize,
Olli Etuaho13389b62016-10-16 11:48:18 +01002666 const TSourceLoc &initLocation,
2667 TIntermTyped *initializer,
2668 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002669{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002670 // If the declaration starting this declarator list was empty (example: int,), some checks were
2671 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002672 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002673 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002674 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002675 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002676 }
2677
Olli Etuaho55bde912017-10-25 13:41:13 +03002678 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002679
Olli Etuaho55bde912017-10-25 13:41:13 +03002680 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002681
Olli Etuaho55bde912017-10-25 13:41:13 +03002682 TType arrayType(elementType);
2683 arrayType.makeArray(arraySize);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002684
2685 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002686 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002687 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002688 {
2689 if (initNode)
2690 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002691 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002692 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002693 }
2694}
2695
jchen104cdac9e2017-05-08 11:01:20 +08002696void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2697 const TSourceLoc &location)
2698{
2699 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2700 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2701 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2702 {
2703 error(location, "Requires both binding and offset", "layout");
2704 return;
2705 }
2706 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2707}
2708
Olli Etuahocce89652017-06-19 16:04:09 +03002709void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2710 const TPublicType &type,
2711 const TSourceLoc &loc)
2712{
2713 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2714 !getFragmentPrecisionHigh())
2715 {
2716 error(loc, "precision is not supported in fragment shader", "highp");
2717 }
2718
2719 if (!CanSetDefaultPrecisionOnType(type))
2720 {
2721 error(loc, "illegal type argument for default precision qualifier",
2722 getBasicString(type.getBasicType()));
2723 return;
2724 }
2725 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2726}
2727
Shaob5cc1192017-07-06 10:47:20 +08002728bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2729{
2730 switch (typeQualifier.layoutQualifier.primitiveType)
2731 {
2732 case EptLines:
2733 case EptLinesAdjacency:
2734 case EptTriangles:
2735 case EptTrianglesAdjacency:
2736 return typeQualifier.qualifier == EvqGeometryIn;
2737
2738 case EptLineStrip:
2739 case EptTriangleStrip:
2740 return typeQualifier.qualifier == EvqGeometryOut;
2741
2742 case EptPoints:
2743 return true;
2744
2745 default:
2746 UNREACHABLE();
2747 return false;
2748 }
2749}
2750
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002751void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2752 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002753{
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002754 if (mGeometryShaderInputArraySize == 0u)
2755 {
2756 mGeometryShaderInputArraySize = inputArraySize;
2757 }
2758 else if (mGeometryShaderInputArraySize != inputArraySize)
2759 {
2760 error(line,
2761 "Array size or input primitive declaration doesn't match the size of earlier sized "
2762 "array inputs.",
2763 "layout");
2764 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002765}
2766
Shaob5cc1192017-07-06 10:47:20 +08002767bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2768{
2769 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2770
2771 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2772
2773 if (layoutQualifier.maxVertices != -1)
2774 {
2775 error(typeQualifier.line,
2776 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2777 return false;
2778 }
2779
2780 // Set mGeometryInputPrimitiveType if exists
2781 if (layoutQualifier.primitiveType != EptUndefined)
2782 {
2783 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2784 {
2785 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2786 return false;
2787 }
2788
2789 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2790 {
2791 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002792 setGeometryShaderInputArraySize(
2793 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2794 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002795 }
2796 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2797 {
2798 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2799 "layout");
2800 return false;
2801 }
2802 }
2803
2804 // Set mGeometryInvocations if exists
2805 if (layoutQualifier.invocations > 0)
2806 {
2807 if (mGeometryShaderInvocations == 0)
2808 {
2809 mGeometryShaderInvocations = layoutQualifier.invocations;
2810 }
2811 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2812 {
2813 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2814 "layout");
2815 return false;
2816 }
2817 }
2818
2819 return true;
2820}
2821
2822bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2823{
2824 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2825
2826 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2827
2828 if (layoutQualifier.invocations > 0)
2829 {
2830 error(typeQualifier.line,
2831 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2832 return false;
2833 }
2834
2835 // Set mGeometryOutputPrimitiveType if exists
2836 if (layoutQualifier.primitiveType != EptUndefined)
2837 {
2838 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2839 {
2840 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2841 return false;
2842 }
2843
2844 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2845 {
2846 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2847 }
2848 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2849 {
2850 error(typeQualifier.line,
2851 "primitive doesn't match earlier output primitive declaration", "layout");
2852 return false;
2853 }
2854 }
2855
2856 // Set mGeometryMaxVertices if exists
2857 if (layoutQualifier.maxVertices > -1)
2858 {
2859 if (mGeometryShaderMaxVertices == -1)
2860 {
2861 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2862 }
2863 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2864 {
2865 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2866 "layout");
2867 return false;
2868 }
2869 }
2870
2871 return true;
2872}
2873
Martin Radev70866b82016-07-22 15:27:42 +03002874void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002875{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002876 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002877 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002878
Martin Radev70866b82016-07-22 15:27:42 +03002879 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2880 typeQualifier.line);
2881
Jamie Madillc2128ff2016-07-04 10:26:17 -04002882 // It should never be the case, but some strange parser errors can send us here.
2883 if (layoutQualifier.isEmpty())
2884 {
2885 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002886 return;
2887 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002888
Martin Radev802abe02016-08-04 17:48:32 +03002889 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002890 {
Olli Etuaho43364892017-02-13 16:00:12 +00002891 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002892 return;
2893 }
2894
Olli Etuaho43364892017-02-13 16:00:12 +00002895 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2896
2897 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002898
2899 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2900
Andrei Volykhina5527072017-03-22 16:46:30 +03002901 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2902
jchen104cdac9e2017-05-08 11:01:20 +08002903 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2904
Martin Radev802abe02016-08-04 17:48:32 +03002905 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002906 {
Martin Radev802abe02016-08-04 17:48:32 +03002907 if (mComputeShaderLocalSizeDeclared &&
2908 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2909 {
2910 error(typeQualifier.line, "Work group size does not match the previous declaration",
2911 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002912 return;
2913 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002914
Martin Radev802abe02016-08-04 17:48:32 +03002915 if (mShaderVersion < 310)
2916 {
2917 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002918 return;
2919 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002920
Martin Radev4c4c8e72016-08-04 12:25:34 +03002921 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002922 {
2923 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002924 return;
2925 }
2926
2927 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2928 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2929
2930 const TConstantUnion *maxComputeWorkGroupSizeData =
2931 maxComputeWorkGroupSize->getConstPointer();
2932
2933 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2934 {
2935 if (layoutQualifier.localSize[i] != -1)
2936 {
2937 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2938 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2939 if (mComputeShaderLocalSize[i] < 1 ||
2940 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2941 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002942 std::stringstream reasonStream;
2943 reasonStream << "invalid value: Value must be at least 1 and no greater than "
2944 << maxComputeWorkGroupSizeValue;
2945 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03002946
Olli Etuaho4de340a2016-12-16 09:32:03 +00002947 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03002948 return;
2949 }
2950 }
2951 }
2952
2953 mComputeShaderLocalSizeDeclared = true;
2954 }
Shaob5cc1192017-07-06 10:47:20 +08002955 else if (typeQualifier.qualifier == EvqGeometryIn)
2956 {
2957 if (mShaderVersion < 310)
2958 {
2959 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
2960 return;
2961 }
2962
2963 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
2964 {
2965 return;
2966 }
2967 }
2968 else if (typeQualifier.qualifier == EvqGeometryOut)
2969 {
2970 if (mShaderVersion < 310)
2971 {
2972 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
2973 "layout");
2974 return;
2975 }
2976
2977 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
2978 {
2979 return;
2980 }
2981 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03002982 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
2983 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00002984 {
2985 // This error is only specified in WebGL, but tightens unspecified behavior in the native
2986 // specification.
2987 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
2988 {
2989 error(typeQualifier.line, "Number of views does not match the previous declaration",
2990 "layout");
2991 return;
2992 }
2993
2994 if (layoutQualifier.numViews == -1)
2995 {
2996 error(typeQualifier.line, "No num_views specified", "layout");
2997 return;
2998 }
2999
3000 if (layoutQualifier.numViews > mMaxNumViews)
3001 {
3002 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3003 "layout");
3004 return;
3005 }
3006
3007 mNumViews = layoutQualifier.numViews;
3008 }
Martin Radev802abe02016-08-04 17:48:32 +03003009 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003010 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003011 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003012 {
Martin Radev802abe02016-08-04 17:48:32 +03003013 return;
3014 }
3015
Jiajia Qinbc585152017-06-23 15:42:17 +08003016 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003017 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003018 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003019 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003020 return;
3021 }
3022
3023 if (mShaderVersion < 300)
3024 {
3025 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3026 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003027 return;
3028 }
3029
Olli Etuaho09b04a22016-12-15 13:30:26 +00003030 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003031
3032 if (layoutQualifier.matrixPacking != EmpUnspecified)
3033 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003034 if (typeQualifier.qualifier == EvqUniform)
3035 {
3036 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3037 }
3038 else if (typeQualifier.qualifier == EvqBuffer)
3039 {
3040 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3041 }
Martin Radev802abe02016-08-04 17:48:32 +03003042 }
3043
3044 if (layoutQualifier.blockStorage != EbsUnspecified)
3045 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003046 if (typeQualifier.qualifier == EvqUniform)
3047 {
3048 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3049 }
3050 else if (typeQualifier.qualifier == EvqBuffer)
3051 {
3052 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3053 }
Martin Radev802abe02016-08-04 17:48:32 +03003054 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003055 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003056}
3057
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003058TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3059 const TFunction &function,
3060 const TSourceLoc &location,
3061 bool insertParametersToSymbolTable)
3062{
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003063 checkIsNotReserved(location, function.getName());
3064
Olli Etuahofe486322017-03-21 09:30:54 +00003065 TIntermFunctionPrototype *prototype =
3066 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003067 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
3068 // point to the data that already exists in the symbol table.
3069 prototype->getFunctionSymbolInfo()->setFromFunction(function);
3070 prototype->setLine(location);
3071
3072 for (size_t i = 0; i < function.getParamCount(); i++)
3073 {
3074 const TConstParameter &param = function.getParam(i);
3075
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003076 TIntermSymbol *symbol = nullptr;
3077
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003078 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3079 // be used for unused args).
3080 if (param.name != nullptr)
3081 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003082 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003083 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003084 {
Olli Etuaho0f684632017-07-13 12:42:15 +03003085 TVariable *variable = symbolTable.declareVariable(param.name, *param.type);
3086 if (variable)
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003087 {
3088 symbol = new TIntermSymbol(variable->getUniqueId(), variable->getName(),
3089 variable->getType());
3090 }
3091 else
3092 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003093 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003094 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003095 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003096 // Unsized type of a named parameter should have already been checked and sanitized.
3097 ASSERT(!param.type->isUnsizedArray());
3098 }
3099 else
3100 {
3101 if (param.type->isUnsizedArray())
3102 {
3103 error(location, "function parameter array must be sized at compile time", "[]");
3104 // We don't need to size the arrays since the parameter is unnamed and hence
3105 // inaccessible.
3106 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003107 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003108 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003109 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003110 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3111 // symbol.
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003112 symbol = new TIntermSymbol(symbolTable.getEmptySymbolId(), "", *param.type);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003113 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003114 symbol->setLine(location);
3115 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003116 }
3117 return prototype;
3118}
3119
Olli Etuaho16c745a2017-01-16 17:02:27 +00003120TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3121 const TFunction &parsedFunction,
3122 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003123{
Olli Etuaho476197f2016-10-11 13:59:08 +01003124 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3125 // first declaration. Either way the instance in the symbol table is used to track whether the
3126 // function is declared multiple times.
3127 TFunction *function = static_cast<TFunction *>(
3128 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3129 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003130 {
3131 // ESSL 1.00.17 section 4.2.7.
3132 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3133 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003134 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003135 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003136
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003137 TIntermFunctionPrototype *prototype =
3138 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003139
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003140 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003141
3142 if (!symbolTable.atGlobalLevel())
3143 {
3144 // ESSL 3.00.4 section 4.2.4.
3145 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003146 }
3147
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003148 return prototype;
3149}
3150
Olli Etuaho336b1472016-10-05 16:37:55 +01003151TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003152 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003153 TIntermBlock *functionBody,
3154 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003155{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003156 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003157 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3158 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003159 error(location, "function does not return a value:",
3160 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003161 }
3162
Olli Etuahof51fdd22016-10-03 10:03:40 +01003163 if (functionBody == nullptr)
3164 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003165 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003166 functionBody->setLine(location);
3167 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003168 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003169 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003170 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003171
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003172 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003173 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003174}
3175
Olli Etuaho476197f2016-10-11 13:59:08 +01003176void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3177 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003178 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003179{
Olli Etuaho476197f2016-10-11 13:59:08 +01003180 ASSERT(function);
3181 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003182 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003183 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003184
3185 if (builtIn)
3186 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003187 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003188 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003189 else
Jamie Madill185fb402015-06-12 15:48:48 -04003190 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003191 TFunction *prevDec = static_cast<TFunction *>(
3192 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3193
3194 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3195 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3196 // occurance.
3197 if (*function != prevDec)
3198 {
3199 // Swap the parameters of the previous declaration to the parameters of the function
3200 // definition (parameter names may differ).
3201 prevDec->swapParameters(**function);
3202
3203 // The function definition will share the same symbol as any previous declaration.
3204 *function = prevDec;
3205 }
3206
3207 if ((*function)->isDefined())
3208 {
3209 error(location, "function already has a body", (*function)->getName().c_str());
3210 }
3211
3212 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003213 }
Jamie Madill185fb402015-06-12 15:48:48 -04003214
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003215 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003216 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003217 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003218
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003219 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003220 setLoopNestingLevel(0);
3221}
3222
Jamie Madillb98c3a82015-07-23 14:26:04 -04003223TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003224{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003225 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003226 // We don't know at this point whether this is a function definition or a prototype.
3227 // The definition production code will check for redefinitions.
3228 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003229 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003230 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3231 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003232 //
3233 TFunction *prevDec =
3234 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303235
Martin Radevda6254b2016-12-14 17:00:36 +02003236 if (getShaderVersion() >= 300 &&
3237 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
3238 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303239 {
Martin Radevda6254b2016-12-14 17:00:36 +02003240 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303241 // Therefore overloading or redefining builtin functions is an error.
3242 error(location, "Name of a built-in function cannot be redeclared as function",
3243 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303244 }
3245 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003246 {
3247 if (prevDec->getReturnType() != function->getReturnType())
3248 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003249 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003250 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003251 }
3252 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3253 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003254 if (prevDec->getParam(i).type->getQualifier() !=
3255 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003256 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003257 error(location,
3258 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003259 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003260 }
3261 }
3262 }
3263
3264 //
3265 // Check for previously declared variables using the same name.
3266 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003267 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003268 if (prevSym)
3269 {
3270 if (!prevSym->isFunction())
3271 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003272 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003273 }
3274 }
3275 else
3276 {
3277 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003278 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003279 }
3280
3281 // We're at the inner scope level of the function's arguments and body statement.
3282 // Add the function prototype to the surrounding scope instead.
3283 symbolTable.getOuterLevel()->insert(function);
3284
Olli Etuaho78d13742017-01-18 13:06:10 +00003285 // Raise error message if main function takes any parameters or return anything other than void
3286 if (function->getName() == "main")
3287 {
3288 if (function->getParamCount() > 0)
3289 {
3290 error(location, "function cannot take any parameter(s)", "main");
3291 }
3292 if (function->getReturnType().getBasicType() != EbtVoid)
3293 {
3294 error(location, "main function cannot return a value",
3295 function->getReturnType().getBasicString());
3296 }
3297 }
3298
Jamie Madill185fb402015-06-12 15:48:48 -04003299 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003300 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3301 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003302 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3303 //
3304 return function;
3305}
3306
Olli Etuaho9de84a52016-06-14 17:36:01 +03003307TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3308 const TString *name,
3309 const TSourceLoc &location)
3310{
3311 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3312 {
3313 error(location, "no qualifiers allowed for function return",
3314 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003315 }
3316 if (!type.layoutQualifier.isEmpty())
3317 {
3318 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003319 }
jchen10cc2a10e2017-05-03 14:05:12 +08003320 // make sure an opaque type is not involved as well...
3321 std::string reason(getBasicString(type.getBasicType()));
3322 reason += "s can't be function return values";
3323 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003324 if (mShaderVersion < 300)
3325 {
3326 // Array return values are forbidden, but there's also no valid syntax for declaring array
3327 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00003328 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003329
3330 if (type.isStructureContainingArrays())
3331 {
3332 // ESSL 1.00.17 section 6.1 Function Definitions
3333 error(location, "structures containing arrays can't be function return values",
3334 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003335 }
3336 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003337
3338 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003339 return new TFunction(&symbolTable, name, new TType(type));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003340}
3341
Olli Etuahocce89652017-06-19 16:04:09 +03003342TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3343{
Olli Etuahocce89652017-06-19 16:04:09 +03003344 const TType *returnType = TCache::getType(EbtVoid, EbpUndefined);
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003345 return new TFunction(&symbolTable, name, returnType);
Olli Etuahocce89652017-06-19 16:04:09 +03003346}
3347
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003348TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003349{
Olli Etuahocce89652017-06-19 16:04:09 +03003350 if (mShaderVersion < 300 && publicType.array)
3351 {
3352 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3353 "[]");
3354 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003355 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003356 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003357 error(publicType.getLine(), "constructor can't be a structure definition",
3358 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003359 }
3360
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003361 TType *type = new TType(publicType);
3362 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003363 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003364 error(publicType.getLine(), "cannot construct this type",
3365 getBasicString(publicType.getBasicType()));
3366 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003367 }
3368
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003369 return new TFunction(&symbolTable, nullptr, type, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003370}
3371
Olli Etuaho55bde912017-10-25 13:41:13 +03003372void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3373 const char *errorMessage,
3374 const char *token,
3375 TType *arrayType)
3376{
3377 if (arrayType->isUnsizedArray())
3378 {
3379 error(line, errorMessage, token);
3380 arrayType->sizeUnsizedArrays(TVector<unsigned int>());
3381 }
3382}
3383
3384TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahocce89652017-06-19 16:04:09 +03003385 const TString *name,
3386 const TSourceLoc &nameLoc)
3387{
Olli Etuaho55bde912017-10-25 13:41:13 +03003388 ASSERT(type);
3389 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name->c_str(),
3390 type);
3391 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003392 {
3393 error(nameLoc, "illegal use of type 'void'", name->c_str());
3394 }
3395 checkIsNotReserved(nameLoc, *name);
Olli Etuahocce89652017-06-19 16:04:09 +03003396 TParameter param = {name, type};
3397 return param;
3398}
3399
Olli Etuaho55bde912017-10-25 13:41:13 +03003400TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3401 const TString *name,
3402 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003403{
Olli Etuaho55bde912017-10-25 13:41:13 +03003404 TType *type = new TType(publicType);
3405 return parseParameterDeclarator(type, name, nameLoc);
3406}
3407
3408TParameter TParseContext::parseParameterArrayDeclarator(const TString *name,
3409 const TSourceLoc &nameLoc,
3410 unsigned int arraySize,
3411 const TSourceLoc &arrayLoc,
3412 TPublicType *elementType)
3413{
3414 checkArrayElementIsNotArray(arrayLoc, *elementType);
3415 TType *arrayType = new TType(*elementType);
3416 arrayType->makeArray(arraySize);
3417 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003418}
3419
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003420bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(TIntermSequence *arguments,
3421 TType type,
3422 const TSourceLoc &line)
3423{
3424 if (arguments->empty())
3425 {
3426 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3427 return false;
3428 }
3429 for (TIntermNode *arg : *arguments)
3430 {
3431 TIntermTyped *element = arg->getAsTyped();
3432 ASSERT(element);
3433 size_t dimensionalityFromElement = element->getType().getArraySizes().size() + 1u;
3434 if (dimensionalityFromElement > type.getArraySizes().size())
3435 {
3436 error(line, "constructing from a non-dereferenced array", "constructor");
3437 return false;
3438 }
3439 else if (dimensionalityFromElement < type.getArraySizes().size())
3440 {
3441 if (dimensionalityFromElement == 1u)
3442 {
3443 error(line, "implicitly sized array of arrays constructor argument is not an array",
3444 "constructor");
3445 }
3446 else
3447 {
3448 error(line,
3449 "implicitly sized array of arrays constructor argument dimensionality is too "
3450 "low",
3451 "constructor");
3452 }
3453 return false;
3454 }
3455 }
3456 return true;
3457}
3458
Jamie Madillb98c3a82015-07-23 14:26:04 -04003459// This function is used to test for the correctness of the parameters passed to various constructor
3460// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003461//
Olli Etuaho856c4972016-08-08 11:38:39 +03003462// 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 +00003463//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003464TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003465 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303466 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003467{
Olli Etuaho856c4972016-08-08 11:38:39 +03003468 if (type.isUnsizedArray())
3469 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003470 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003471 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003472 type.sizeUnsizedArrays(TVector<unsigned int>());
Olli Etuaho3ec75682017-07-05 17:02:55 +03003473 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003474 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003475 TIntermTyped *firstElement = arguments->at(0)->getAsTyped();
3476 ASSERT(firstElement);
3477 type.setArraySize(type.getArraySizes().size() - 1u,
3478 static_cast<unsigned int>(arguments->size()));
3479 for (size_t i = 0; i < firstElement->getType().getArraySizes().size(); ++i)
3480 {
3481 if (type.getArraySizes()[i] == 0u)
3482 {
3483 type.setArraySize(i, firstElement->getType().getArraySizes().at(i));
3484 }
3485 }
3486 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003487 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003488
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003489 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003490 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003491 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003492 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003493
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003494 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003495 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003496
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003497 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3498 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003499 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003500 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003501 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003502 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003503}
3504
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003505//
3506// Interface/uniform blocks
Jiawei Shaod8105a02017-08-08 09:54:36 +08003507// TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003508//
Olli Etuaho13389b62016-10-16 11:48:18 +01003509TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003510 const TTypeQualifierBuilder &typeQualifierBuilder,
3511 const TSourceLoc &nameLine,
3512 const TString &blockName,
3513 TFieldList *fieldList,
3514 const TString *instanceName,
3515 const TSourceLoc &instanceLine,
3516 TIntermTyped *arrayIndex,
3517 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003518{
Olli Etuaho856c4972016-08-08 11:38:39 +03003519 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003520
Olli Etuaho77ba4082016-12-16 12:01:18 +00003521 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003522
Jiajia Qinbc585152017-06-23 15:42:17 +08003523 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003524 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003525 error(typeQualifier.line,
3526 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3527 "3.10",
3528 getQualifierString(typeQualifier.qualifier));
3529 }
3530 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3531 {
3532 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003533 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003534 }
3535
Martin Radev70866b82016-07-22 15:27:42 +03003536 if (typeQualifier.invariant)
3537 {
3538 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3539 }
3540
Jiajia Qinbc585152017-06-23 15:42:17 +08003541 if (typeQualifier.qualifier != EvqBuffer)
3542 {
3543 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3544 }
Olli Etuaho43364892017-02-13 16:00:12 +00003545
jchen10af713a22017-04-19 09:10:56 +08003546 // add array index
3547 unsigned int arraySize = 0;
3548 if (arrayIndex != nullptr)
3549 {
3550 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3551 }
3552
3553 if (mShaderVersion < 310)
3554 {
3555 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3556 }
3557 else
3558 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003559 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3560 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003561 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003562
Andrei Volykhina5527072017-03-22 16:46:30 +03003563 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3564
Jamie Madill099c0f32013-06-20 11:55:52 -04003565 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003566 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003567
Jamie Madill099c0f32013-06-20 11:55:52 -04003568 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3569 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003570 if (typeQualifier.qualifier == EvqUniform)
3571 {
3572 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3573 }
3574 else if (typeQualifier.qualifier == EvqBuffer)
3575 {
3576 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3577 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003578 }
3579
Jamie Madill1566ef72013-06-20 11:55:54 -04003580 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3581 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003582 if (typeQualifier.qualifier == EvqUniform)
3583 {
3584 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3585 }
3586 else if (typeQualifier.qualifier == EvqBuffer)
3587 {
3588 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3589 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003590 }
3591
Olli Etuaho856c4972016-08-08 11:38:39 +03003592 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003593
Martin Radev2cc85b32016-08-05 16:22:53 +03003594 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3595
Olli Etuaho0f684632017-07-13 12:42:15 +03003596 if (!symbolTable.declareInterfaceBlockName(&blockName))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303597 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003598 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003599 }
3600
Jamie Madill98493dd2013-07-08 14:39:03 -04003601 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303602 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3603 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003604 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303605 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003606 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303607 {
jchen10cc2a10e2017-05-03 14:05:12 +08003608 std::string reason("unsupported type - ");
3609 reason += fieldType->getBasicString();
3610 reason += " types are not allowed in interface blocks";
3611 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003612 }
3613
Jamie Madill98493dd2013-07-08 14:39:03 -04003614 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003615 switch (qualifier)
3616 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003617 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003618 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003619 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003620 if (typeQualifier.qualifier == EvqBuffer)
3621 {
3622 error(field->line(), "invalid qualifier on shader storage block member",
3623 getQualifierString(qualifier));
3624 }
3625 break;
3626 case EvqBuffer:
3627 if (typeQualifier.qualifier == EvqUniform)
3628 {
3629 error(field->line(), "invalid qualifier on uniform block member",
3630 getQualifierString(qualifier));
3631 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003632 break;
3633 default:
3634 error(field->line(), "invalid qualifier on interface block member",
3635 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003636 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003637 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003638
Martin Radev70866b82016-07-22 15:27:42 +03003639 if (fieldType->isInvariant())
3640 {
3641 error(field->line(), "invalid qualifier on interface block member", "invariant");
3642 }
3643
Jamie Madilla5efff92013-06-06 11:56:47 -04003644 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003645 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003646 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003647 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003648
Jamie Madill98493dd2013-07-08 14:39:03 -04003649 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003650 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003651 error(field->line(), "invalid layout qualifier: cannot be used here",
3652 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003653 }
3654
Jamie Madill98493dd2013-07-08 14:39:03 -04003655 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003656 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003657 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003658 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003659 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003660 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003661 warning(field->line(),
3662 "extraneous layout qualifier: only has an effect on matrix types",
3663 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003664 }
3665
Jamie Madill98493dd2013-07-08 14:39:03 -04003666 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003667
3668 if (typeQualifier.qualifier == EvqBuffer)
3669 {
3670 // set memory qualifiers
3671 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3672 // qualified with a memory qualifier, it is as if all of its members were declared with
3673 // the same memory qualifier.
3674 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3675 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3676 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3677 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3678 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3679 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3680 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3681 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3682 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3683 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3684 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003685 }
3686
Jamie Madillb98c3a82015-07-23 14:26:04 -04003687 TInterfaceBlock *interfaceBlock =
Shaob18c33e2017-08-16 12:37:51 +08003688 new TInterfaceBlock(&blockName, fieldList, instanceName, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003689 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
3690 if (arrayIndex != nullptr)
3691 {
3692 interfaceBlockType.makeArray(arraySize);
3693 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003694
3695 TString symbolName = "";
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003696 const TSymbolUniqueId *symbolId = nullptr;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003697
Jamie Madill98493dd2013-07-08 14:39:03 -04003698 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003699 {
3700 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003701 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3702 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003703 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303704 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003705
3706 // set parent pointer of the field variable
3707 fieldType->setInterfaceBlock(interfaceBlock);
3708
Olli Etuaho0f684632017-07-13 12:42:15 +03003709 TVariable *fieldVariable = symbolTable.declareVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003710
Olli Etuaho0f684632017-07-13 12:42:15 +03003711 if (fieldVariable)
3712 {
3713 fieldVariable->setQualifier(typeQualifier.qualifier);
3714 }
3715 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303716 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003717 error(field->line(), "redefinition of an interface block member name",
3718 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003719 }
3720 }
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003721 symbolId = &symbolTable.getEmptySymbolId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003722 }
3723 else
3724 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003725 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003726
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003727 // add a symbol for this interface block
Olli Etuaho0f684632017-07-13 12:42:15 +03003728 TVariable *instanceTypeDef = symbolTable.declareVariable(instanceName, interfaceBlockType);
3729 if (instanceTypeDef)
3730 {
3731 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003732 symbolId = &instanceTypeDef->getUniqueId();
Olli Etuaho0f684632017-07-13 12:42:15 +03003733 }
3734 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303735 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003736 error(instanceLine, "redefinition of an interface block instance name",
3737 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003738 }
Olli Etuaho0f684632017-07-13 12:42:15 +03003739 symbolName = *instanceName;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003740 }
3741
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003742 TIntermDeclaration *declaration = nullptr;
3743
3744 if (symbolId)
3745 {
3746 TIntermSymbol *blockSymbol = new TIntermSymbol(*symbolId, symbolName, interfaceBlockType);
3747 blockSymbol->setLine(typeQualifier.line);
3748 declaration = new TIntermDeclaration();
3749 declaration->appendDeclarator(blockSymbol);
3750 declaration->setLine(nameLine);
3751 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003752
3753 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003754 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003755}
3756
Olli Etuaho383b7912016-08-05 11:22:59 +03003757void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003758{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003759 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003760
3761 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003762 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303763 if (mStructNestingLevel > 1)
3764 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003765 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003766 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003767}
3768
3769void TParseContext::exitStructDeclaration()
3770{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003771 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003772}
3773
Olli Etuaho8a176262016-08-16 14:23:01 +03003774void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003775{
Jamie Madillacb4b812016-11-07 13:50:29 -05003776 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303777 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003778 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003779 }
3780
Arun Patole7e7e68d2015-05-22 12:02:25 +05303781 if (field.type()->getBasicType() != EbtStruct)
3782 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003783 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003784 }
3785
3786 // We're already inside a structure definition at this point, so add
3787 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303788 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3789 {
Jamie Madill41a49272014-03-18 16:10:13 -04003790 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003791 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3792 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003793 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003794 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003795 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003796 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003797}
3798
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003799//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003800// Parse an array index expression
3801//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003802TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3803 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303804 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003805{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003806 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3807 {
3808 if (baseExpression->getAsSymbolNode())
3809 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303810 error(location, " left of '[' is not of type array, matrix, or vector ",
3811 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003812 }
3813 else
3814 {
3815 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3816 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003817
Olli Etuaho3ec75682017-07-05 17:02:55 +03003818 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003819 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003820
Jiawei Shaod8105a02017-08-08 09:54:36 +08003821 if (baseExpression->getQualifier() == EvqPerVertexIn)
3822 {
3823 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
3824 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3825 {
3826 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3827 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3828 }
3829 }
3830
Jamie Madill21c1e452014-12-29 11:33:41 -05003831 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3832
Olli Etuaho36b05142015-11-12 13:10:42 +02003833 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3834 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3835 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3836 // index is a constant expression.
3837 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3838 {
3839 if (baseExpression->isInterfaceBlock())
3840 {
Jiawei Shaod8105a02017-08-08 09:54:36 +08003841 // TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
3842 switch (baseExpression->getQualifier())
3843 {
3844 case EvqPerVertexIn:
3845 break;
3846 case EvqUniform:
3847 case EvqBuffer:
3848 error(location,
3849 "array indexes for uniform block arrays and shader storage block arrays "
3850 "must be constant integral expressions",
3851 "[");
3852 break;
3853 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003854 // We can reach here only in error cases.
3855 ASSERT(mDiagnostics->numErrors() > 0);
3856 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003857 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003858 }
3859 else if (baseExpression->getQualifier() == EvqFragmentOut)
3860 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003861 error(location,
3862 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003863 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003864 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3865 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003866 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003867 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003868 }
3869
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003870 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003871 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003872 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3873 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3874 // constant fold expressions that are not constant expressions). The most compatible way to
3875 // handle this case is to report a warning instead of an error and force the index to be in
3876 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003877 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003878 int index = 0;
3879 if (indexConstantUnion->getBasicType() == EbtInt)
3880 {
3881 index = indexConstantUnion->getIConst(0);
3882 }
3883 else if (indexConstantUnion->getBasicType() == EbtUInt)
3884 {
3885 index = static_cast<int>(indexConstantUnion->getUConst(0));
3886 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003887
3888 int safeIndex = -1;
3889
3890 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003891 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003892 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003893 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003894 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003895 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003896 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003897 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003898 "GL_EXT_draw_buffers is disabled",
3899 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003900 safeIndex = 0;
3901 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003902 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003903 // Only do generic out-of-range check if similar error hasn't already been reported.
3904 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003905 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003906 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003907 baseExpression->getOutermostArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003908 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003909 }
3910 }
3911 else if (baseExpression->isMatrix())
3912 {
3913 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003914 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003915 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003916 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003917 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003918 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003919 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3920 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003921 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003922 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003923
3924 ASSERT(safeIndex >= 0);
3925 // Data of constant unions can't be changed, because it may be shared with other
3926 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3927 // sanitized object.
Olli Etuaho56229f12017-07-10 14:16:33 +03003928 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003929 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003930 TConstantUnion *safeConstantUnion = new TConstantUnion();
3931 safeConstantUnion->setIConst(safeIndex);
3932 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
Olli Etuaho56229f12017-07-10 14:16:33 +03003933 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003934 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003935
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003936 TIntermBinary *node = new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
3937 node->setLine(location);
3938 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003939 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003940 else
3941 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003942 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
3943 node->setLine(location);
3944 // Indirect indexing can never be constant folded.
3945 return node;
Jamie Madill7164cf42013-07-08 13:30:59 -04003946 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003947}
3948
Olli Etuaho90892fb2016-07-14 14:44:51 +03003949int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3950 const TSourceLoc &location,
3951 int index,
3952 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003953 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003954{
3955 if (index >= arraySize || index < 0)
3956 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003957 std::stringstream reasonStream;
3958 reasonStream << reason << " '" << index << "'";
3959 std::string token = reasonStream.str();
3960 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003961 if (index < 0)
3962 {
3963 return 0;
3964 }
3965 else
3966 {
3967 return arraySize - 1;
3968 }
3969 }
3970 return index;
3971}
3972
Jamie Madillb98c3a82015-07-23 14:26:04 -04003973TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3974 const TSourceLoc &dotLocation,
3975 const TString &fieldString,
3976 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003977{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003978 if (baseExpression->isArray())
3979 {
3980 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003981 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003982 }
3983
3984 if (baseExpression->isVector())
3985 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003986 TVector<int> fieldOffsets;
3987 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
3988 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003989 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003990 fieldOffsets.resize(1);
3991 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003992 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003993 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
3994 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003995
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003996 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003997 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003998 else if (baseExpression->getBasicType() == EbtStruct)
3999 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304000 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004001 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004002 {
4003 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004004 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004005 }
4006 else
4007 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004008 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004009 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004010 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004011 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004012 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004013 {
4014 fieldFound = true;
4015 break;
4016 }
4017 }
4018 if (fieldFound)
4019 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004020 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004021 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004022 TIntermBinary *node =
4023 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4024 node->setLine(dotLocation);
4025 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004026 }
4027 else
4028 {
4029 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004030 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004031 }
4032 }
4033 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004034 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004035 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304036 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004037 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004038 {
4039 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004040 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004041 }
4042 else
4043 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004044 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004045 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004046 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004047 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004048 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004049 {
4050 fieldFound = true;
4051 break;
4052 }
4053 }
4054 if (fieldFound)
4055 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004056 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004057 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004058 TIntermBinary *node =
4059 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4060 node->setLine(dotLocation);
4061 // Indexing interface blocks can never be constant folded.
4062 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004063 }
4064 else
4065 {
4066 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004067 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004068 }
4069 }
4070 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004071 else
4072 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004073 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004074 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004075 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304076 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004077 }
4078 else
4079 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304080 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004081 " field selection requires structure, vector, or interface block on left hand "
4082 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304083 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004084 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004085 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004086 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004087}
4088
Jamie Madillb98c3a82015-07-23 14:26:04 -04004089TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4090 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004091{
Martin Radev802abe02016-08-04 17:48:32 +03004092 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004093
4094 if (qualifierType == "shared")
4095 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004096 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004097 {
4098 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4099 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004100 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004101 }
4102 else if (qualifierType == "packed")
4103 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004104 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004105 {
4106 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4107 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004108 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004109 }
4110 else if (qualifierType == "std140")
4111 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004112 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004113 }
4114 else if (qualifierType == "row_major")
4115 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004116 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004117 }
4118 else if (qualifierType == "column_major")
4119 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004120 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004121 }
4122 else if (qualifierType == "location")
4123 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004124 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4125 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004126 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004127 else if (qualifierType == "yuv" && isExtensionEnabled(TExtension::EXT_YUV_target) &&
Andrei Volykhina5527072017-03-22 16:46:30 +03004128 mShaderType == GL_FRAGMENT_SHADER)
4129 {
4130 qualifier.yuv = true;
4131 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004132 else if (qualifierType == "rgba32f")
4133 {
4134 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4135 qualifier.imageInternalFormat = EiifRGBA32F;
4136 }
4137 else if (qualifierType == "rgba16f")
4138 {
4139 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4140 qualifier.imageInternalFormat = EiifRGBA16F;
4141 }
4142 else if (qualifierType == "r32f")
4143 {
4144 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4145 qualifier.imageInternalFormat = EiifR32F;
4146 }
4147 else if (qualifierType == "rgba8")
4148 {
4149 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4150 qualifier.imageInternalFormat = EiifRGBA8;
4151 }
4152 else if (qualifierType == "rgba8_snorm")
4153 {
4154 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4155 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4156 }
4157 else if (qualifierType == "rgba32i")
4158 {
4159 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4160 qualifier.imageInternalFormat = EiifRGBA32I;
4161 }
4162 else if (qualifierType == "rgba16i")
4163 {
4164 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4165 qualifier.imageInternalFormat = EiifRGBA16I;
4166 }
4167 else if (qualifierType == "rgba8i")
4168 {
4169 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4170 qualifier.imageInternalFormat = EiifRGBA8I;
4171 }
4172 else if (qualifierType == "r32i")
4173 {
4174 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4175 qualifier.imageInternalFormat = EiifR32I;
4176 }
4177 else if (qualifierType == "rgba32ui")
4178 {
4179 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4180 qualifier.imageInternalFormat = EiifRGBA32UI;
4181 }
4182 else if (qualifierType == "rgba16ui")
4183 {
4184 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4185 qualifier.imageInternalFormat = EiifRGBA16UI;
4186 }
4187 else if (qualifierType == "rgba8ui")
4188 {
4189 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4190 qualifier.imageInternalFormat = EiifRGBA8UI;
4191 }
4192 else if (qualifierType == "r32ui")
4193 {
4194 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4195 qualifier.imageInternalFormat = EiifR32UI;
4196 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004197 else if (qualifierType == "points" && isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004198 mShaderType == GL_GEOMETRY_SHADER_OES)
4199 {
4200 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4201 qualifier.primitiveType = EptPoints;
4202 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004203 else if (qualifierType == "lines" && isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004204 mShaderType == GL_GEOMETRY_SHADER_OES)
4205 {
4206 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4207 qualifier.primitiveType = EptLines;
4208 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004209 else if (qualifierType == "lines_adjacency" &&
4210 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004211 mShaderType == GL_GEOMETRY_SHADER_OES)
4212 {
4213 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4214 qualifier.primitiveType = EptLinesAdjacency;
4215 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004216 else if (qualifierType == "triangles" && isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004217 mShaderType == GL_GEOMETRY_SHADER_OES)
4218 {
4219 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4220 qualifier.primitiveType = EptTriangles;
4221 }
4222 else if (qualifierType == "triangles_adjacency" &&
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004223 isExtensionEnabled(TExtension::OES_geometry_shader) &&
4224 mShaderType == GL_GEOMETRY_SHADER_OES)
Shaob5cc1192017-07-06 10:47:20 +08004225 {
4226 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4227 qualifier.primitiveType = EptTrianglesAdjacency;
4228 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004229 else if (qualifierType == "line_strip" && isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004230 mShaderType == GL_GEOMETRY_SHADER_OES)
4231 {
4232 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4233 qualifier.primitiveType = EptLineStrip;
4234 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004235 else if (qualifierType == "triangle_strip" &&
4236 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004237 mShaderType == GL_GEOMETRY_SHADER_OES)
4238 {
4239 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4240 qualifier.primitiveType = EptTriangleStrip;
4241 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004242
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004243 else
4244 {
4245 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004246 }
4247
Jamie Madilla5efff92013-06-06 11:56:47 -04004248 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004249}
4250
Martin Radev802abe02016-08-04 17:48:32 +03004251void TParseContext::parseLocalSize(const TString &qualifierType,
4252 const TSourceLoc &qualifierTypeLine,
4253 int intValue,
4254 const TSourceLoc &intValueLine,
4255 const std::string &intValueString,
4256 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004257 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004258{
Olli Etuaho856c4972016-08-08 11:38:39 +03004259 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004260 if (intValue < 1)
4261 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004262 std::stringstream reasonStream;
4263 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4264 std::string reason = reasonStream.str();
4265 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004266 }
4267 (*localSize)[index] = intValue;
4268}
4269
Olli Etuaho09b04a22016-12-15 13:30:26 +00004270void TParseContext::parseNumViews(int intValue,
4271 const TSourceLoc &intValueLine,
4272 const std::string &intValueString,
4273 int *numViews)
4274{
4275 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4276 // specification.
4277 if (intValue < 1)
4278 {
4279 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4280 }
4281 *numViews = intValue;
4282}
4283
Shaob5cc1192017-07-06 10:47:20 +08004284void TParseContext::parseInvocations(int intValue,
4285 const TSourceLoc &intValueLine,
4286 const std::string &intValueString,
4287 int *numInvocations)
4288{
4289 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4290 // it doesn't make sense to accept invocations <= 0.
4291 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4292 {
4293 error(intValueLine,
4294 "out of range: invocations must be in the range of [1, "
4295 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4296 intValueString.c_str());
4297 }
4298 else
4299 {
4300 *numInvocations = intValue;
4301 }
4302}
4303
4304void TParseContext::parseMaxVertices(int intValue,
4305 const TSourceLoc &intValueLine,
4306 const std::string &intValueString,
4307 int *maxVertices)
4308{
4309 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4310 // it doesn't make sense to accept max_vertices < 0.
4311 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4312 {
4313 error(
4314 intValueLine,
4315 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4316 intValueString.c_str());
4317 }
4318 else
4319 {
4320 *maxVertices = intValue;
4321 }
4322}
4323
Jamie Madillb98c3a82015-07-23 14:26:04 -04004324TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4325 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004326 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304327 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004328{
Martin Radev802abe02016-08-04 17:48:32 +03004329 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004330
Martin Radev802abe02016-08-04 17:48:32 +03004331 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004332
Martin Radev802abe02016-08-04 17:48:32 +03004333 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004334 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004335 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004336 if (intValue < 0)
4337 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004338 error(intValueLine, "out of range: location must be non-negative",
4339 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004340 }
4341 else
4342 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004343 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004344 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004345 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004346 }
Olli Etuaho43364892017-02-13 16:00:12 +00004347 else if (qualifierType == "binding")
4348 {
4349 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4350 if (intValue < 0)
4351 {
4352 error(intValueLine, "out of range: binding must be non-negative",
4353 intValueString.c_str());
4354 }
4355 else
4356 {
4357 qualifier.binding = intValue;
4358 }
4359 }
jchen104cdac9e2017-05-08 11:01:20 +08004360 else if (qualifierType == "offset")
4361 {
4362 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4363 if (intValue < 0)
4364 {
4365 error(intValueLine, "out of range: offset must be non-negative",
4366 intValueString.c_str());
4367 }
4368 else
4369 {
4370 qualifier.offset = intValue;
4371 }
4372 }
Martin Radev802abe02016-08-04 17:48:32 +03004373 else if (qualifierType == "local_size_x")
4374 {
4375 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4376 &qualifier.localSize);
4377 }
4378 else if (qualifierType == "local_size_y")
4379 {
4380 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4381 &qualifier.localSize);
4382 }
4383 else if (qualifierType == "local_size_z")
4384 {
4385 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4386 &qualifier.localSize);
4387 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004388 else if (qualifierType == "num_views" && isExtensionEnabled(TExtension::OVR_multiview) &&
Olli Etuaho09b04a22016-12-15 13:30:26 +00004389 mShaderType == GL_VERTEX_SHADER)
4390 {
4391 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4392 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004393 else if (qualifierType == "invocations" &&
4394 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004395 mShaderType == GL_GEOMETRY_SHADER_OES)
4396 {
4397 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4398 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004399 else if (qualifierType == "max_vertices" &&
4400 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004401 mShaderType == GL_GEOMETRY_SHADER_OES)
4402 {
4403 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4404 }
4405
Martin Radev802abe02016-08-04 17:48:32 +03004406 else
4407 {
4408 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004409 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004410
Jamie Madilla5efff92013-06-06 11:56:47 -04004411 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004412}
4413
Olli Etuaho613b9592016-09-05 12:05:53 +03004414TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4415{
4416 return new TTypeQualifierBuilder(
4417 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4418 mShaderVersion);
4419}
4420
Olli Etuahocce89652017-06-19 16:04:09 +03004421TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4422 const TSourceLoc &loc)
4423{
4424 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4425 return new TStorageQualifierWrapper(qualifier, loc);
4426}
4427
4428TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4429{
4430 if (getShaderType() == GL_VERTEX_SHADER)
4431 {
4432 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4433 }
4434 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4435}
4436
4437TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4438{
4439 if (declaringFunction())
4440 {
4441 return new TStorageQualifierWrapper(EvqIn, loc);
4442 }
Shaob5cc1192017-07-06 10:47:20 +08004443
4444 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004445 {
Shaob5cc1192017-07-06 10:47:20 +08004446 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004447 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004448 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004449 {
4450 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4451 }
4452 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004453 }
Shaob5cc1192017-07-06 10:47:20 +08004454 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004455 {
Shaob5cc1192017-07-06 10:47:20 +08004456 if (mShaderVersion < 300)
4457 {
4458 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4459 }
4460 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004461 }
Shaob5cc1192017-07-06 10:47:20 +08004462 case GL_COMPUTE_SHADER:
4463 {
4464 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4465 }
4466 case GL_GEOMETRY_SHADER_OES:
4467 {
4468 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4469 }
4470 default:
4471 {
4472 UNREACHABLE();
4473 return new TStorageQualifierWrapper(EvqLast, loc);
4474 }
Olli Etuahocce89652017-06-19 16:04:09 +03004475 }
Olli Etuahocce89652017-06-19 16:04:09 +03004476}
4477
4478TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4479{
4480 if (declaringFunction())
4481 {
4482 return new TStorageQualifierWrapper(EvqOut, loc);
4483 }
Shaob5cc1192017-07-06 10:47:20 +08004484 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004485 {
Shaob5cc1192017-07-06 10:47:20 +08004486 case GL_VERTEX_SHADER:
4487 {
4488 if (mShaderVersion < 300)
4489 {
4490 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4491 }
4492 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4493 }
4494 case GL_FRAGMENT_SHADER:
4495 {
4496 if (mShaderVersion < 300)
4497 {
4498 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4499 }
4500 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4501 }
4502 case GL_COMPUTE_SHADER:
4503 {
4504 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4505 return new TStorageQualifierWrapper(EvqLast, loc);
4506 }
4507 case GL_GEOMETRY_SHADER_OES:
4508 {
4509 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4510 }
4511 default:
4512 {
4513 UNREACHABLE();
4514 return new TStorageQualifierWrapper(EvqLast, loc);
4515 }
Olli Etuahocce89652017-06-19 16:04:09 +03004516 }
Olli Etuahocce89652017-06-19 16:04:09 +03004517}
4518
4519TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4520{
4521 if (!declaringFunction())
4522 {
4523 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4524 }
4525 return new TStorageQualifierWrapper(EvqInOut, loc);
4526}
4527
Jamie Madillb98c3a82015-07-23 14:26:04 -04004528TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004529 TLayoutQualifier rightQualifier,
4530 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004531{
Martin Radevc28888b2016-07-22 15:27:42 +03004532 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004533 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004534}
4535
Olli Etuahocce89652017-06-19 16:04:09 +03004536TField *TParseContext::parseStructDeclarator(TString *identifier, const TSourceLoc &loc)
4537{
4538 checkIsNotReserved(loc, *identifier);
4539 TType *type = new TType(EbtVoid, EbpUndefined);
4540 return new TField(type, identifier, loc);
4541}
4542
4543TField *TParseContext::parseStructArrayDeclarator(TString *identifier,
4544 const TSourceLoc &loc,
4545 TIntermTyped *arraySize,
4546 const TSourceLoc &arraySizeLoc)
4547{
4548 checkIsNotReserved(loc, *identifier);
4549
4550 TType *type = new TType(EbtVoid, EbpUndefined);
4551 unsigned int size = checkIsValidArraySize(arraySizeLoc, arraySize);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004552 type->makeArray(size);
Olli Etuahocce89652017-06-19 16:04:09 +03004553
4554 return new TField(type, identifier, loc);
4555}
4556
Olli Etuaho4de340a2016-12-16 09:32:03 +00004557TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4558 const TFieldList *newlyAddedFields,
4559 const TSourceLoc &location)
4560{
4561 for (TField *field : *newlyAddedFields)
4562 {
4563 for (TField *oldField : *processedFields)
4564 {
4565 if (oldField->name() == field->name())
4566 {
4567 error(location, "duplicate field name in structure", field->name().c_str());
4568 }
4569 }
4570 processedFields->push_back(field);
4571 }
4572 return processedFields;
4573}
4574
Martin Radev70866b82016-07-22 15:27:42 +03004575TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4576 const TTypeQualifierBuilder &typeQualifierBuilder,
4577 TPublicType *typeSpecifier,
4578 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004579{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004580 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004581
Martin Radev70866b82016-07-22 15:27:42 +03004582 typeSpecifier->qualifier = typeQualifier.qualifier;
4583 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004584 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004585 typeSpecifier->invariant = typeQualifier.invariant;
4586 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304587 {
Martin Radev70866b82016-07-22 15:27:42 +03004588 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004589 }
Martin Radev70866b82016-07-22 15:27:42 +03004590 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004591}
4592
Jamie Madillb98c3a82015-07-23 14:26:04 -04004593TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004594 TFieldList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004595{
Martin Radev4a9cd802016-09-01 16:51:51 +03004596 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4597 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004598
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004599 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
4600 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004601
Martin Radev4a9cd802016-09-01 16:51:51 +03004602 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004603
Olli Etuaho55bde912017-10-25 13:41:13 +03004604 for (TField *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304605 {
Olli Etuaho55bde912017-10-25 13:41:13 +03004606 auto declaratorArraySizes = declarator->type()->getArraySizes();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004607 // don't allow arrays of arrays
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004608 if (!declaratorArraySizes.empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304609 {
Olli Etuahoe0803872017-08-23 15:30:23 +03004610 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004611 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004612
Olli Etuaho55bde912017-10-25 13:41:13 +03004613 TType *type = declarator->type();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004614 *type = TType(typeSpecifier);
4615 for (unsigned int arraySize : declaratorArraySizes)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304616 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004617 type->makeArray(arraySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004618 }
Olli Etuaho55bde912017-10-25 13:41:13 +03004619 checkIsNotUnsizedArray(typeSpecifier.getLine(),
4620 "array members of structs must specify a size",
4621 declarator->name().c_str(), type);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004622
Olli Etuaho55bde912017-10-25 13:41:13 +03004623 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *declarator);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004624 }
4625
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004626 return declaratorList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004627}
4628
Martin Radev4a9cd802016-09-01 16:51:51 +03004629TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4630 const TSourceLoc &nameLine,
4631 const TString *structName,
4632 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004633{
Olli Etuahoa5e693a2017-07-13 16:07:26 +03004634 TStructure *structure = new TStructure(&symbolTable, structName, fieldList);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004635
Jamie Madill9b820842015-02-12 10:40:10 -05004636 // Store a bool in the struct if we're at global scope, to allow us to
4637 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004638 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004639
Jamie Madill98493dd2013-07-08 14:39:03 -04004640 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004641 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004642 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004643 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304644 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004645 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004646 }
4647 }
4648
4649 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004650 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004651 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004652 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004653 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004654 switch (qualifier)
4655 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004656 case EvqGlobal:
4657 case EvqTemporary:
4658 break;
4659 default:
4660 error(field.line(), "invalid qualifier on struct member",
4661 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004662 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004663 }
Martin Radev70866b82016-07-22 15:27:42 +03004664 if (field.type()->isInvariant())
4665 {
4666 error(field.line(), "invalid qualifier on struct member", "invariant");
4667 }
jchen104cdac9e2017-05-08 11:01:20 +08004668 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4669 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004670 {
4671 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4672 }
4673
Olli Etuaho43364892017-02-13 16:00:12 +00004674 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4675
4676 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004677
4678 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004679 }
4680
Martin Radev4a9cd802016-09-01 16:51:51 +03004681 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004682 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004683 exitStructDeclaration();
4684
Martin Radev4a9cd802016-09-01 16:51:51 +03004685 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004686}
4687
Jamie Madillb98c3a82015-07-23 14:26:04 -04004688TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004689 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004690 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004691{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004692 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004693 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004694 init->isVector())
4695 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004696 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4697 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004698 return nullptr;
4699 }
4700
Olli Etuaho923ecef2017-10-11 12:01:38 +03004701 ASSERT(statementList);
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004702 if (!ValidateSwitchStatementList(switchType, mShaderVersion, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004703 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004704 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004705 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004706 }
4707
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004708 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4709 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004710 return node;
4711}
4712
4713TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4714{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004715 if (mSwitchNestingLevel == 0)
4716 {
4717 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004718 return nullptr;
4719 }
4720 if (condition == nullptr)
4721 {
4722 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004723 return nullptr;
4724 }
4725 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004726 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004727 {
4728 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004729 }
4730 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004731 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4732 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4733 // fold in case labels.
4734 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004735 {
4736 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004737 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004738 TIntermCase *node = new TIntermCase(condition);
4739 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004740 return node;
4741}
4742
4743TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4744{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004745 if (mSwitchNestingLevel == 0)
4746 {
4747 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004748 return nullptr;
4749 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004750 TIntermCase *node = new TIntermCase(nullptr);
4751 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004752 return node;
4753}
4754
Jamie Madillb98c3a82015-07-23 14:26:04 -04004755TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4756 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004757 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004758{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004759 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004760
4761 switch (op)
4762 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004763 case EOpLogicalNot:
4764 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4765 child->isVector())
4766 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004767 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004768 return nullptr;
4769 }
4770 break;
4771 case EOpBitwiseNot:
4772 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4773 child->isMatrix() || child->isArray())
4774 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004775 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004776 return nullptr;
4777 }
4778 break;
4779 case EOpPostIncrement:
4780 case EOpPreIncrement:
4781 case EOpPostDecrement:
4782 case EOpPreDecrement:
4783 case EOpNegative:
4784 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004785 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4786 child->getBasicType() == EbtBool || child->isArray() ||
4787 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004788 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004789 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004790 return nullptr;
4791 }
4792 // Operators for built-ins are already type checked against their prototype.
4793 default:
4794 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004795 }
4796
Jiajia Qinbc585152017-06-23 15:42:17 +08004797 if (child->getMemoryQualifier().writeonly)
4798 {
4799 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4800 return nullptr;
4801 }
4802
Olli Etuahof119a262016-08-19 15:54:22 +03004803 TIntermUnary *node = new TIntermUnary(op, child);
4804 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004805
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004806 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004807}
4808
Olli Etuaho09b22472015-02-11 11:47:26 +02004809TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4810{
Olli Etuahocce89652017-06-19 16:04:09 +03004811 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004812 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004813 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004814 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004815 return child;
4816 }
4817 return node;
4818}
4819
Jamie Madillb98c3a82015-07-23 14:26:04 -04004820TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4821 TIntermTyped *child,
4822 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004823{
Olli Etuaho856c4972016-08-08 11:38:39 +03004824 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004825 return addUnaryMath(op, child, loc);
4826}
4827
Jamie Madillb98c3a82015-07-23 14:26:04 -04004828bool TParseContext::binaryOpCommonCheck(TOperator op,
4829 TIntermTyped *left,
4830 TIntermTyped *right,
4831 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004832{
jchen10b4cf5652017-05-05 18:51:17 +08004833 // Check opaque types are not allowed to be operands in expressions other than array indexing
4834 // and structure member selection.
4835 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4836 {
4837 switch (op)
4838 {
4839 case EOpIndexDirect:
4840 case EOpIndexIndirect:
4841 break;
4842 case EOpIndexDirectStruct:
4843 UNREACHABLE();
4844
4845 default:
4846 error(loc, "Invalid operation for variables with an opaque type",
4847 GetOperatorString(op));
4848 return false;
4849 }
4850 }
jchen10cc2a10e2017-05-03 14:05:12 +08004851
Jiajia Qinbc585152017-06-23 15:42:17 +08004852 if (right->getMemoryQualifier().writeonly)
4853 {
4854 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4855 return false;
4856 }
4857
4858 if (left->getMemoryQualifier().writeonly)
4859 {
4860 switch (op)
4861 {
4862 case EOpAssign:
4863 case EOpInitialize:
4864 case EOpIndexDirect:
4865 case EOpIndexIndirect:
4866 case EOpIndexDirectStruct:
4867 case EOpIndexDirectInterfaceBlock:
4868 break;
4869 default:
4870 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4871 return false;
4872 }
4873 }
4874
Olli Etuaho244be012016-08-18 15:26:02 +03004875 if (left->getType().getStruct() || right->getType().getStruct())
4876 {
4877 switch (op)
4878 {
4879 case EOpIndexDirectStruct:
4880 ASSERT(left->getType().getStruct());
4881 break;
4882 case EOpEqual:
4883 case EOpNotEqual:
4884 case EOpAssign:
4885 case EOpInitialize:
4886 if (left->getType() != right->getType())
4887 {
4888 return false;
4889 }
4890 break;
4891 default:
4892 error(loc, "Invalid operation for structs", GetOperatorString(op));
4893 return false;
4894 }
4895 }
4896
Olli Etuaho94050052017-05-08 14:17:44 +03004897 if (left->isInterfaceBlock() || right->isInterfaceBlock())
4898 {
4899 switch (op)
4900 {
4901 case EOpIndexDirectInterfaceBlock:
4902 ASSERT(left->getType().getInterfaceBlock());
4903 break;
4904 default:
4905 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
4906 return false;
4907 }
4908 }
4909
Olli Etuahod6b14282015-03-17 14:31:35 +02004910 if (left->isArray() || right->isArray())
4911 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004912 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02004913 {
4914 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4915 return false;
4916 }
4917
4918 if (left->isArray() != right->isArray())
4919 {
4920 error(loc, "array / non-array mismatch", GetOperatorString(op));
4921 return false;
4922 }
4923
4924 switch (op)
4925 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004926 case EOpEqual:
4927 case EOpNotEqual:
4928 case EOpAssign:
4929 case EOpInitialize:
4930 break;
4931 default:
4932 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4933 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02004934 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03004935 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004936 if (left->getType().getArraySizes() != right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02004937 {
4938 error(loc, "array size mismatch", GetOperatorString(op));
4939 return false;
4940 }
Olli Etuahod6b14282015-03-17 14:31:35 +02004941 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004942
4943 // Check ops which require integer / ivec parameters
4944 bool isBitShift = false;
4945 switch (op)
4946 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004947 case EOpBitShiftLeft:
4948 case EOpBitShiftRight:
4949 case EOpBitShiftLeftAssign:
4950 case EOpBitShiftRightAssign:
4951 // Unsigned can be bit-shifted by signed and vice versa, but we need to
4952 // check that the basic type is an integer type.
4953 isBitShift = true;
4954 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
4955 {
4956 return false;
4957 }
4958 break;
4959 case EOpBitwiseAnd:
4960 case EOpBitwiseXor:
4961 case EOpBitwiseOr:
4962 case EOpBitwiseAndAssign:
4963 case EOpBitwiseXorAssign:
4964 case EOpBitwiseOrAssign:
4965 // It is enough to check the type of only one operand, since later it
4966 // is checked that the operand types match.
4967 if (!IsInteger(left->getBasicType()))
4968 {
4969 return false;
4970 }
4971 break;
4972 default:
4973 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004974 }
4975
4976 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
4977 // So the basic type should usually match.
4978 if (!isBitShift && left->getBasicType() != right->getBasicType())
4979 {
4980 return false;
4981 }
4982
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004983 // Check that:
4984 // 1. Type sizes match exactly on ops that require that.
4985 // 2. Restrictions for structs that contain arrays or samplers are respected.
4986 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04004987 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004988 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004989 case EOpAssign:
4990 case EOpInitialize:
4991 case EOpEqual:
4992 case EOpNotEqual:
4993 // ESSL 1.00 sections 5.7, 5.8, 5.9
4994 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
4995 {
4996 error(loc, "undefined operation for structs containing arrays",
4997 GetOperatorString(op));
4998 return false;
4999 }
5000 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5001 // we interpret the spec so that this extends to structs containing samplers,
5002 // similarly to ESSL 1.00 spec.
5003 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5004 left->getType().isStructureContainingSamplers())
5005 {
5006 error(loc, "undefined operation for structs containing samplers",
5007 GetOperatorString(op));
5008 return false;
5009 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005010
Olli Etuahoe1805592017-01-02 16:41:20 +00005011 if ((left->getNominalSize() != right->getNominalSize()) ||
5012 (left->getSecondarySize() != right->getSecondarySize()))
5013 {
5014 error(loc, "dimension mismatch", GetOperatorString(op));
5015 return false;
5016 }
5017 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005018 case EOpLessThan:
5019 case EOpGreaterThan:
5020 case EOpLessThanEqual:
5021 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005022 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005023 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005024 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005025 return false;
5026 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005027 break;
5028 case EOpAdd:
5029 case EOpSub:
5030 case EOpDiv:
5031 case EOpIMod:
5032 case EOpBitShiftLeft:
5033 case EOpBitShiftRight:
5034 case EOpBitwiseAnd:
5035 case EOpBitwiseXor:
5036 case EOpBitwiseOr:
5037 case EOpAddAssign:
5038 case EOpSubAssign:
5039 case EOpDivAssign:
5040 case EOpIModAssign:
5041 case EOpBitShiftLeftAssign:
5042 case EOpBitShiftRightAssign:
5043 case EOpBitwiseAndAssign:
5044 case EOpBitwiseXorAssign:
5045 case EOpBitwiseOrAssign:
5046 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5047 {
5048 return false;
5049 }
5050
5051 // Are the sizes compatible?
5052 if (left->getNominalSize() != right->getNominalSize() ||
5053 left->getSecondarySize() != right->getSecondarySize())
5054 {
5055 // If the nominal sizes of operands do not match:
5056 // One of them must be a scalar.
5057 if (!left->isScalar() && !right->isScalar())
5058 return false;
5059
5060 // In the case of compound assignment other than multiply-assign,
5061 // the right side needs to be a scalar. Otherwise a vector/matrix
5062 // would be assigned to a scalar. A scalar can't be shifted by a
5063 // vector either.
5064 if (!right->isScalar() &&
5065 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5066 return false;
5067 }
5068 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005069 default:
5070 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005071 }
5072
Olli Etuahod6b14282015-03-17 14:31:35 +02005073 return true;
5074}
5075
Olli Etuaho1dded802016-08-18 18:13:13 +03005076bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5077 const TType &left,
5078 const TType &right)
5079{
5080 switch (op)
5081 {
5082 case EOpMul:
5083 case EOpMulAssign:
5084 return left.getNominalSize() == right.getNominalSize() &&
5085 left.getSecondarySize() == right.getSecondarySize();
5086 case EOpVectorTimesScalar:
5087 return true;
5088 case EOpVectorTimesScalarAssign:
5089 ASSERT(!left.isMatrix() && !right.isMatrix());
5090 return left.isVector() && !right.isVector();
5091 case EOpVectorTimesMatrix:
5092 return left.getNominalSize() == right.getRows();
5093 case EOpVectorTimesMatrixAssign:
5094 ASSERT(!left.isMatrix() && right.isMatrix());
5095 return left.isVector() && left.getNominalSize() == right.getRows() &&
5096 left.getNominalSize() == right.getCols();
5097 case EOpMatrixTimesVector:
5098 return left.getCols() == right.getNominalSize();
5099 case EOpMatrixTimesScalar:
5100 return true;
5101 case EOpMatrixTimesScalarAssign:
5102 ASSERT(left.isMatrix() && !right.isMatrix());
5103 return !right.isVector();
5104 case EOpMatrixTimesMatrix:
5105 return left.getCols() == right.getRows();
5106 case EOpMatrixTimesMatrixAssign:
5107 ASSERT(left.isMatrix() && right.isMatrix());
5108 // We need to check two things:
5109 // 1. The matrix multiplication step is valid.
5110 // 2. The result will have the same number of columns as the lvalue.
5111 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5112
5113 default:
5114 UNREACHABLE();
5115 return false;
5116 }
5117}
5118
Jamie Madillb98c3a82015-07-23 14:26:04 -04005119TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5120 TIntermTyped *left,
5121 TIntermTyped *right,
5122 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005123{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005124 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005125 return nullptr;
5126
Olli Etuahofc1806e2015-03-17 13:03:11 +02005127 switch (op)
5128 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005129 case EOpEqual:
5130 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005131 case EOpLessThan:
5132 case EOpGreaterThan:
5133 case EOpLessThanEqual:
5134 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005135 break;
5136 case EOpLogicalOr:
5137 case EOpLogicalXor:
5138 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005139 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5140 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005141 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005142 {
5143 return nullptr;
5144 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005145 // Basic types matching should have been already checked.
5146 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005147 break;
5148 case EOpAdd:
5149 case EOpSub:
5150 case EOpDiv:
5151 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005152 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5153 !right->getType().getStruct());
5154 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005155 {
5156 return nullptr;
5157 }
5158 break;
5159 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005160 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5161 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005162 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005163 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005164 {
5165 return nullptr;
5166 }
5167 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005168 default:
5169 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005170 }
5171
Olli Etuaho1dded802016-08-18 18:13:13 +03005172 if (op == EOpMul)
5173 {
5174 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5175 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5176 {
5177 return nullptr;
5178 }
5179 }
5180
Olli Etuaho3fdec912016-08-18 15:08:06 +03005181 TIntermBinary *node = new TIntermBinary(op, left, right);
5182 node->setLine(loc);
5183
Olli Etuaho3fdec912016-08-18 15:08:06 +03005184 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005185 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005186}
5187
Jamie Madillb98c3a82015-07-23 14:26:04 -04005188TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5189 TIntermTyped *left,
5190 TIntermTyped *right,
5191 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005192{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005193 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005194 if (node == 0)
5195 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005196 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5197 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005198 return left;
5199 }
5200 return node;
5201}
5202
Jamie Madillb98c3a82015-07-23 14:26:04 -04005203TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5204 TIntermTyped *left,
5205 TIntermTyped *right,
5206 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005207{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005208 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005209 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005210 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005211 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5212 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005213 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005214 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005215 }
5216 return node;
5217}
5218
Olli Etuaho13389b62016-10-16 11:48:18 +01005219TIntermBinary *TParseContext::createAssign(TOperator op,
5220 TIntermTyped *left,
5221 TIntermTyped *right,
5222 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005223{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005224 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005225 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005226 if (op == EOpMulAssign)
5227 {
5228 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5229 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5230 {
5231 return nullptr;
5232 }
5233 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005234 TIntermBinary *node = new TIntermBinary(op, left, right);
5235 node->setLine(loc);
5236
Olli Etuaho3fdec912016-08-18 15:08:06 +03005237 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005238 }
5239 return nullptr;
5240}
5241
Jamie Madillb98c3a82015-07-23 14:26:04 -04005242TIntermTyped *TParseContext::addAssign(TOperator op,
5243 TIntermTyped *left,
5244 TIntermTyped *right,
5245 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005246{
Olli Etuahocce89652017-06-19 16:04:09 +03005247 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005248 TIntermTyped *node = createAssign(op, left, right, loc);
5249 if (node == nullptr)
5250 {
5251 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005252 return left;
5253 }
5254 return node;
5255}
5256
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005257TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5258 TIntermTyped *right,
5259 const TSourceLoc &loc)
5260{
Corentin Wallez0d959252016-07-12 17:26:32 -04005261 // WebGL2 section 5.26, the following results in an error:
5262 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005263 if (mShaderSpec == SH_WEBGL2_SPEC &&
5264 (left->isArray() || left->getBasicType() == EbtVoid ||
5265 left->getType().isStructureContainingArrays() || right->isArray() ||
5266 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005267 {
5268 error(loc,
5269 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5270 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005271 }
5272
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005273 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5274 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5275 commaNode->getTypePointer()->setQualifier(resultQualifier);
5276 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005277}
5278
Olli Etuaho49300862015-02-20 14:54:49 +02005279TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5280{
5281 switch (op)
5282 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005283 case EOpContinue:
5284 if (mLoopNestingLevel <= 0)
5285 {
5286 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005287 }
5288 break;
5289 case EOpBreak:
5290 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5291 {
5292 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005293 }
5294 break;
5295 case EOpReturn:
5296 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5297 {
5298 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005299 }
5300 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005301 case EOpKill:
5302 if (mShaderType != GL_FRAGMENT_SHADER)
5303 {
5304 error(loc, "discard supported in fragment shaders only", "discard");
5305 }
5306 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005307 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005308 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005309 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005310 }
Olli Etuahocce89652017-06-19 16:04:09 +03005311 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005312}
5313
Jamie Madillb98c3a82015-07-23 14:26:04 -04005314TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005315 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005316 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005317{
Olli Etuahocce89652017-06-19 16:04:09 +03005318 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005319 {
Olli Etuahocce89652017-06-19 16:04:09 +03005320 ASSERT(op == EOpReturn);
5321 mFunctionReturnsValue = true;
5322 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5323 {
5324 error(loc, "void function cannot return a value", "return");
5325 }
5326 else if (*mCurrentFunctionType != expression->getType())
5327 {
5328 error(loc, "function return is not matching type:", "return");
5329 }
Olli Etuaho49300862015-02-20 14:54:49 +02005330 }
Olli Etuahocce89652017-06-19 16:04:09 +03005331 TIntermBranch *node = new TIntermBranch(op, expression);
5332 node->setLine(loc);
5333 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005334}
5335
Martin Radev84aa2dc2017-09-11 15:51:02 +03005336void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5337{
5338 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
5339 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5340 bool isTextureGather = (name == "textureGather");
5341 bool isTextureGatherOffset = (name == "textureGatherOffset");
5342 if (isTextureGather || isTextureGatherOffset)
5343 {
5344 TIntermNode *componentNode = nullptr;
5345 TIntermSequence *arguments = functionCall->getSequence();
5346 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5347 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5348 ASSERT(sampler != nullptr);
5349 switch (sampler->getBasicType())
5350 {
5351 case EbtSampler2D:
5352 case EbtISampler2D:
5353 case EbtUSampler2D:
5354 case EbtSampler2DArray:
5355 case EbtISampler2DArray:
5356 case EbtUSampler2DArray:
5357 if ((isTextureGather && arguments->size() == 3u) ||
5358 (isTextureGatherOffset && arguments->size() == 4u))
5359 {
5360 componentNode = arguments->back();
5361 }
5362 break;
5363 case EbtSamplerCube:
5364 case EbtISamplerCube:
5365 case EbtUSamplerCube:
5366 ASSERT(!isTextureGatherOffset);
5367 if (arguments->size() == 3u)
5368 {
5369 componentNode = arguments->back();
5370 }
5371 break;
5372 case EbtSampler2DShadow:
5373 case EbtSampler2DArrayShadow:
5374 case EbtSamplerCubeShadow:
5375 break;
5376 default:
5377 UNREACHABLE();
5378 break;
5379 }
5380 if (componentNode)
5381 {
5382 const TIntermConstantUnion *componentConstantUnion =
5383 componentNode->getAsConstantUnion();
5384 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5385 {
5386 error(functionCall->getLine(), "Texture component must be a constant expression",
5387 name.c_str());
5388 }
5389 else
5390 {
5391 int component = componentConstantUnion->getIConst(0);
5392 if (component < 0 || component > 3)
5393 {
5394 error(functionCall->getLine(), "Component must be in the range [0;3]",
5395 name.c_str());
5396 }
5397 }
5398 }
5399 }
5400}
5401
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005402void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5403{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005404 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01005405 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005406 TIntermNode *offset = nullptr;
5407 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005408 bool useTextureGatherOffsetConstraints = false;
Olli Etuahoec9232b2017-03-27 17:01:37 +03005409 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5410 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5411 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005412 {
5413 offset = arguments->back();
5414 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005415 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005416 {
5417 // A bias parameter might follow the offset parameter.
5418 ASSERT(arguments->size() >= 3);
5419 offset = (*arguments)[2];
5420 }
Martin Radev84aa2dc2017-09-11 15:51:02 +03005421 else if (name == "textureGatherOffset")
5422 {
5423 ASSERT(arguments->size() >= 3u);
5424 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5425 ASSERT(sampler != nullptr);
5426 switch (sampler->getBasicType())
5427 {
5428 case EbtSampler2D:
5429 case EbtISampler2D:
5430 case EbtUSampler2D:
5431 case EbtSampler2DArray:
5432 case EbtISampler2DArray:
5433 case EbtUSampler2DArray:
5434 offset = (*arguments)[2];
5435 break;
5436 case EbtSampler2DShadow:
5437 case EbtSampler2DArrayShadow:
5438 offset = (*arguments)[3];
5439 break;
5440 default:
5441 UNREACHABLE();
5442 break;
5443 }
5444 useTextureGatherOffsetConstraints = true;
5445 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005446 if (offset != nullptr)
5447 {
5448 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5449 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5450 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005451 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005452 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005453 }
5454 else
5455 {
5456 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5457 size_t size = offsetConstantUnion->getType().getObjectSize();
5458 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005459 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5460 : mMinProgramTexelOffset;
5461 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5462 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005463 for (size_t i = 0u; i < size; ++i)
5464 {
5465 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005466 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005467 {
5468 std::stringstream tokenStream;
5469 tokenStream << offsetValue;
5470 std::string token = tokenStream.str();
5471 error(offset->getLine(), "Texture offset value out of valid range",
5472 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005473 }
5474 }
5475 }
5476 }
5477}
5478
Martin Radev2cc85b32016-08-05 16:22:53 +03005479// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5480void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5481{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005482 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005483 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5484
5485 if (name.compare(0, 5, "image") == 0)
5486 {
5487 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005488 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005489
Olli Etuaho485eefd2017-02-14 17:40:06 +00005490 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005491
5492 if (name.compare(5, 5, "Store") == 0)
5493 {
5494 if (memoryQualifier.readonly)
5495 {
5496 error(imageNode->getLine(),
5497 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005498 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005499 }
5500 }
5501 else if (name.compare(5, 4, "Load") == 0)
5502 {
5503 if (memoryQualifier.writeonly)
5504 {
5505 error(imageNode->getLine(),
5506 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005507 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005508 }
5509 }
5510 }
5511}
5512
5513// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5514void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5515 const TFunction *functionDefinition,
5516 const TIntermAggregate *functionCall)
5517{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005518 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005519
5520 const TIntermSequence &arguments = *functionCall->getSequence();
5521
5522 ASSERT(functionDefinition->getParamCount() == arguments.size());
5523
5524 for (size_t i = 0; i < arguments.size(); ++i)
5525 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005526 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5527 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005528 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5529 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5530
5531 if (IsImage(functionArgumentType.getBasicType()))
5532 {
5533 const TMemoryQualifier &functionArgumentMemoryQualifier =
5534 functionArgumentType.getMemoryQualifier();
5535 const TMemoryQualifier &functionParameterMemoryQualifier =
5536 functionParameterType.getMemoryQualifier();
5537 if (functionArgumentMemoryQualifier.readonly &&
5538 !functionParameterMemoryQualifier.readonly)
5539 {
5540 error(functionCall->getLine(),
5541 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005542 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005543 }
5544
5545 if (functionArgumentMemoryQualifier.writeonly &&
5546 !functionParameterMemoryQualifier.writeonly)
5547 {
5548 error(functionCall->getLine(),
5549 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005550 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005551 }
Martin Radev049edfa2016-11-11 14:35:37 +02005552
5553 if (functionArgumentMemoryQualifier.coherent &&
5554 !functionParameterMemoryQualifier.coherent)
5555 {
5556 error(functionCall->getLine(),
5557 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005558 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005559 }
5560
5561 if (functionArgumentMemoryQualifier.volatileQualifier &&
5562 !functionParameterMemoryQualifier.volatileQualifier)
5563 {
5564 error(functionCall->getLine(),
5565 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005566 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005567 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005568 }
5569 }
5570}
5571
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005572TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005573{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005574 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005575}
5576
5577TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005578 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005579 TIntermNode *thisNode,
5580 const TSourceLoc &loc)
5581{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005582 if (thisNode != nullptr)
5583 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005584 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005585 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005586
5587 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005588 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005589 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005590 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005591 }
5592 else
5593 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005594 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005595 return addNonConstructorFunctionCall(fnCall, arguments, loc);
5596 }
5597}
5598
5599TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
5600 TIntermSequence *arguments,
5601 TIntermNode *thisNode,
5602 const TSourceLoc &loc)
5603{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005604 TIntermTyped *typedThis = thisNode->getAsTyped();
5605 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5606 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5607 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
5608 // So accessing fnCall->getName() below is safe.
5609 if (fnCall->getName() != "length")
5610 {
5611 error(loc, "invalid method", fnCall->getName().c_str());
5612 }
5613 else if (!arguments->empty())
5614 {
5615 error(loc, "method takes no parameters", "length");
5616 }
5617 else if (typedThis == nullptr || !typedThis->isArray())
5618 {
5619 error(loc, "length can only be called on arrays", "length");
5620 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005621 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5622 mGeometryShaderInputPrimitiveType == EptUndefined)
5623 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08005624 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005625 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5626 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005627 else
5628 {
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005629 TIntermUnary *node = new TIntermUnary(EOpArrayLength, typedThis);
5630 node->setLine(loc);
5631 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005632 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005633 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005634}
5635
5636TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
5637 TIntermSequence *arguments,
5638 const TSourceLoc &loc)
5639{
5640 // First find by unmangled name to check whether the function name has been
5641 // hidden by a variable name or struct typename.
5642 // If a function is found, check for one with a matching argument list.
5643 bool builtIn;
5644 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
5645 if (symbol != nullptr && !symbol->isFunction())
5646 {
5647 error(loc, "function name expected", fnCall->getName().c_str());
5648 }
5649 else
5650 {
5651 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
5652 mShaderVersion, &builtIn);
5653 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005654 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005655 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
5656 }
5657 else
5658 {
5659 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005660 //
5661 // A declared function.
5662 //
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03005663 if (builtIn && fnCandidate->getExtension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005664 {
Olli Etuaho856c4972016-08-08 11:38:39 +03005665 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005666 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005667 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005668 if (builtIn && op != EOpNull)
5669 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005670 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005671 if (fnCandidate->getParamCount() == 1)
5672 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005673 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005674 TIntermNode *unaryParamNode = arguments->front();
5675 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005676 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005677 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005678 }
5679 else
5680 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005681 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005682 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005683 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005684
5685 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005686 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305687
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005688 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305689 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005690 // See if we can constant fold a built-in. Note that this may be possible
5691 // even if it is not const-qualified.
5692 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305693 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005694 else
5695 {
5696 return callNode;
5697 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005698 }
5699 }
5700 else
5701 {
5702 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005703 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005704
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005705 // If builtIn == false, the function is user defined - could be an overloaded
5706 // built-in as well.
5707 // if builtIn == true, it's a builtIn function with no op associated with it.
5708 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005709 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005710 {
Olli Etuahofe486322017-03-21 09:30:54 +00005711 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005712 checkTextureOffsetConst(callNode);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005713 checkTextureGather(callNode);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005714 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005715 }
5716 else
5717 {
Olli Etuahofe486322017-03-21 09:30:54 +00005718 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005719 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005720 }
5721
Jiajia Qinbc585152017-06-23 15:42:17 +08005722 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005723
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005724 callNode->setLine(loc);
5725
5726 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005727 }
5728 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005729 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005730
5731 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005732 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005733}
5734
Jamie Madillb98c3a82015-07-23 14:26:04 -04005735TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005736 TIntermTyped *trueExpression,
5737 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005738 const TSourceLoc &loc)
5739{
Olli Etuaho56229f12017-07-10 14:16:33 +03005740 if (!checkIsScalarBool(loc, cond))
5741 {
5742 return falseExpression;
5743 }
Olli Etuaho52901742015-04-15 13:42:45 +03005744
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005745 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005746 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005747 std::stringstream reasonStream;
5748 reasonStream << "mismatching ternary operator operand types '"
5749 << trueExpression->getCompleteString() << " and '"
5750 << falseExpression->getCompleteString() << "'";
5751 std::string reason = reasonStream.str();
5752 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005753 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005754 }
Olli Etuahode318b22016-10-25 16:18:25 +01005755 if (IsOpaqueType(trueExpression->getBasicType()))
5756 {
5757 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005758 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005759 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5760 // Note that structs containing opaque types don't need to be checked as structs are
5761 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005762 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005763 return falseExpression;
5764 }
5765
Jiajia Qinbc585152017-06-23 15:42:17 +08005766 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5767 falseExpression->getMemoryQualifier().writeonly)
5768 {
5769 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5770 return falseExpression;
5771 }
5772
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005773 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005774 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005775 // ESSL 3.00.6 section 5.7:
5776 // Ternary operator support is optional for arrays. No certainty that it works across all
5777 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5778 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005779 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005780 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005781 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005782 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005783 }
Olli Etuaho94050052017-05-08 14:17:44 +03005784 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5785 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005786 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005787 return falseExpression;
5788 }
5789
Corentin Wallez0d959252016-07-12 17:26:32 -04005790 // WebGL2 section 5.26, the following results in an error:
5791 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005792 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005793 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005794 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005795 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005796 }
5797
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005798 // Note that the node resulting from here can be a constant union without being qualified as
5799 // constant.
5800 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5801 node->setLine(loc);
5802
5803 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005804}
Olli Etuaho49300862015-02-20 14:54:49 +02005805
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005806//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005807// Parse an array of strings using yyparse.
5808//
5809// Returns 0 for success.
5810//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005811int PaParseStrings(size_t count,
5812 const char *const string[],
5813 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305814 TParseContext *context)
5815{
Yunchao He4f285442017-04-21 12:15:49 +08005816 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005817 return 1;
5818
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005819 if (glslang_initialize(context))
5820 return 1;
5821
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005822 int error = glslang_scan(count, string, length, context);
5823 if (!error)
5824 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005825
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005826 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005827
alokp@chromium.org6b495712012-06-29 00:06:58 +00005828 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005829}
Jamie Madill45bcc782016-11-07 13:58:48 -05005830
5831} // namespace sh