blob: 43e0cadd3a177b21d5964cafc333ababa67a79e6 [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 Etuaho454c34c2017-10-25 16:35:56 +03002267void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
2268 const char *token,
2269 TType *type)
2270{
2271 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2272 {
2273 if (type->isArray() && type->getOutermostArraySize() == 0u)
2274 {
2275 // Set size for the unsized geometry shader inputs if they are declared after a valid
2276 // input primitive declaration.
2277 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2278 {
2279 ASSERT(mGeometryShaderInputArraySize > 0u);
2280 type->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
2281 }
2282 else
2283 {
2284 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2285 // An input can be declared without an array size if there is a previous layout
2286 // which specifies the size.
2287 error(location,
2288 "Missing a valid input primitive declaration before declaring an unsized "
2289 "array input",
2290 token);
2291 }
2292 }
2293 else if (type->isArray())
2294 {
2295 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2296 }
2297 else
2298 {
2299 error(location, "Geometry shader input variable must be declared as an array", token);
2300 }
2301 }
2302}
2303
Olli Etuaho13389b62016-10-16 11:48:18 +01002304TIntermDeclaration *TParseContext::parseSingleDeclaration(
2305 TPublicType &publicType,
2306 const TSourceLoc &identifierOrTypeLocation,
2307 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002308{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002309 TType type(publicType);
2310 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2311 mDirectiveHandler.pragma().stdgl.invariantAll)
2312 {
2313 TQualifier qualifier = type.getQualifier();
2314
2315 // The directive handler has already taken care of rejecting invalid uses of this pragma
2316 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2317 // affected variable declarations:
2318 //
2319 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2320 // elsewhere, in TranslatorGLSL.)
2321 //
2322 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2323 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2324 // the way this is currently implemented we have to enable this compiler option before
2325 // parsing the shader and determining the shading language version it uses. If this were
2326 // implemented as a post-pass, the workaround could be more targeted.
2327 //
2328 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2329 // the specification, but there are desktop OpenGL drivers that expect that this is the
2330 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2331 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2332 {
2333 type.setInvariant(true);
2334 }
2335 }
2336
Olli Etuaho454c34c2017-10-25 16:35:56 +03002337 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier.c_str(), &type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002338
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002339 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2340 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002341
Olli Etuahobab4c082015-04-24 16:38:49 +03002342 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002343 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002344
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002345 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002346 if (emptyDeclaration)
2347 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002348 emptyDeclarationErrorCheck(type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002349 // In most cases we don't need to create a symbol node for an empty declaration.
2350 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2351 if (type.getBasicType() == EbtStruct)
2352 {
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03002353 symbol = new TIntermSymbol(symbolTable.getEmptySymbolId(), "", type);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002354 }
jchen104cdac9e2017-05-08 11:01:20 +08002355 else if (IsAtomicCounter(publicType.getBasicType()))
2356 {
2357 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2358 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002359 }
2360 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002361 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002362 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002363
Olli Etuaho55bde912017-10-25 13:41:13 +03002364 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002365
jchen104cdac9e2017-05-08 11:01:20 +08002366 if (IsAtomicCounter(publicType.getBasicType()))
2367 {
2368
2369 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, false,
2370 identifierOrTypeLocation, type);
2371 }
2372
Olli Etuaho2935c582015-04-08 14:32:06 +03002373 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002374 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002375
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002376 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002377 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002378 symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
Olli Etuaho13389b62016-10-16 11:48:18 +01002379 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002380 }
2381
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002382 TIntermDeclaration *declaration = new TIntermDeclaration();
2383 declaration->setLine(identifierOrTypeLocation);
2384 if (symbol)
2385 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002386 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002387 declaration->appendDeclarator(symbol);
2388 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002389 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002390}
2391
Olli Etuaho55bde912017-10-25 13:41:13 +03002392TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002393 const TSourceLoc &identifierLocation,
2394 const TString &identifier,
2395 const TSourceLoc &indexLocation,
Olli Etuaho55bde912017-10-25 13:41:13 +03002396 unsigned int arraySize)
Jamie Madill60ed9812013-06-06 11:56:46 -04002397{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002398 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002399
Olli Etuaho55bde912017-10-25 13:41:13 +03002400 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002401 identifierLocation);
2402
Olli Etuaho55bde912017-10-25 13:41:13 +03002403 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002404
Olli Etuaho55bde912017-10-25 13:41:13 +03002405 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002406
Olli Etuaho55bde912017-10-25 13:41:13 +03002407 TType arrayType(elementType);
2408 arrayType.makeArray(arraySize);
Jamie Madill60ed9812013-06-06 11:56:46 -04002409
Olli Etuaho454c34c2017-10-25 16:35:56 +03002410 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier.c_str(), &arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002411
2412 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2413
2414 if (IsAtomicCounter(elementType.getBasicType()))
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002415 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002416 checkAtomicCounterOffsetIsNotOverlapped(
2417 elementType, kAtomicCounterArrayStride * arrayType.getArraySizeProduct(), false,
2418 identifierLocation, arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002419 }
2420
Olli Etuaho2935c582015-04-08 14:32:06 +03002421 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002422 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002423
Olli Etuaho13389b62016-10-16 11:48:18 +01002424 TIntermDeclaration *declaration = new TIntermDeclaration();
2425 declaration->setLine(identifierLocation);
2426
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002427 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002428 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002429 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2430 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002431 declaration->appendDeclarator(symbol);
2432 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002433
Olli Etuaho13389b62016-10-16 11:48:18 +01002434 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002435}
2436
Olli Etuaho13389b62016-10-16 11:48:18 +01002437TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2438 const TSourceLoc &identifierLocation,
2439 const TString &identifier,
2440 const TSourceLoc &initLocation,
2441 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002442{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002443 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002444
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002445 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2446 identifierLocation);
2447
2448 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002449
Olli Etuaho13389b62016-10-16 11:48:18 +01002450 TIntermDeclaration *declaration = new TIntermDeclaration();
2451 declaration->setLine(identifierLocation);
2452
2453 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002454 TType type(publicType);
2455 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002456 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002457 if (initNode)
2458 {
2459 declaration->appendDeclarator(initNode);
2460 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002461 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002462 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002463}
2464
Olli Etuaho13389b62016-10-16 11:48:18 +01002465TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002466 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002467 const TSourceLoc &identifierLocation,
2468 const TString &identifier,
2469 const TSourceLoc &indexLocation,
Olli Etuaho55bde912017-10-25 13:41:13 +03002470 unsigned int arraySize,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002471 const TSourceLoc &initLocation,
2472 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002473{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002474 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002475
Olli Etuaho55bde912017-10-25 13:41:13 +03002476 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002477 identifierLocation);
2478
Olli Etuaho55bde912017-10-25 13:41:13 +03002479 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002480
Olli Etuaho55bde912017-10-25 13:41:13 +03002481 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002482
Olli Etuaho55bde912017-10-25 13:41:13 +03002483 TType arrayType(elementType);
2484 arrayType.makeArray(arraySize);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002485
Olli Etuaho13389b62016-10-16 11:48:18 +01002486 TIntermDeclaration *declaration = new TIntermDeclaration();
2487 declaration->setLine(identifierLocation);
2488
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002489 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002490 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002491 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002492 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002493 if (initNode)
2494 {
2495 declaration->appendDeclarator(initNode);
2496 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002497 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002498
2499 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002500}
2501
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002502TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002503 const TTypeQualifierBuilder &typeQualifierBuilder,
2504 const TSourceLoc &identifierLoc,
2505 const TString *identifier,
2506 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002507{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002508 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002509
Martin Radev70866b82016-07-22 15:27:42 +03002510 if (!typeQualifier.invariant)
2511 {
2512 error(identifierLoc, "Expected invariant", identifier->c_str());
2513 return nullptr;
2514 }
2515 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2516 {
2517 return nullptr;
2518 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002519 if (!symbol)
2520 {
2521 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002522 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002523 }
Martin Radev70866b82016-07-22 15:27:42 +03002524 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002525 {
Martin Radev70866b82016-07-22 15:27:42 +03002526 error(identifierLoc, "invariant declaration specifies qualifier",
2527 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002528 }
Martin Radev70866b82016-07-22 15:27:42 +03002529 if (typeQualifier.precision != EbpUndefined)
2530 {
2531 error(identifierLoc, "invariant declaration specifies precision",
2532 getPrecisionString(typeQualifier.precision));
2533 }
2534 if (!typeQualifier.layoutQualifier.isEmpty())
2535 {
2536 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2537 }
2538
2539 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002540 if (!variable)
2541 {
2542 return nullptr;
2543 }
Martin Radev70866b82016-07-22 15:27:42 +03002544 const TType &type = variable->getType();
2545
2546 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2547 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002548 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002549
2550 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2551
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002552 TIntermSymbol *intermSymbol = new TIntermSymbol(variable->getUniqueId(), *identifier, type);
2553 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002554
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002555 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002556}
2557
Olli Etuaho13389b62016-10-16 11:48:18 +01002558void TParseContext::parseDeclarator(TPublicType &publicType,
2559 const TSourceLoc &identifierLocation,
2560 const TString &identifier,
2561 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002562{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002563 // If the declaration starting this declarator list was empty (example: int,), some checks were
2564 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002565 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002566 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002567 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2568 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002569 }
2570
Olli Etuaho856c4972016-08-08 11:38:39 +03002571 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002572
Olli Etuaho2935c582015-04-08 14:32:06 +03002573 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002574 TType type(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002575
2576 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &type);
2577
Olli Etuaho55bde912017-10-25 13:41:13 +03002578 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &type);
2579
jchen104cdac9e2017-05-08 11:01:20 +08002580 if (IsAtomicCounter(publicType.getBasicType()))
2581 {
2582 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, true,
2583 identifierLocation, type);
2584 }
Olli Etuaho43364892017-02-13 16:00:12 +00002585 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002586
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002587 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002588 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002589 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
2590 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002591 declarationOut->appendDeclarator(symbol);
2592 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002593}
2594
Olli Etuaho55bde912017-10-25 13:41:13 +03002595void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002596 const TSourceLoc &identifierLocation,
2597 const TString &identifier,
2598 const TSourceLoc &arrayLocation,
Olli Etuaho55bde912017-10-25 13:41:13 +03002599 unsigned int arraySize,
Olli Etuaho13389b62016-10-16 11:48:18 +01002600 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002601{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002602 // If the declaration starting this declarator list was empty (example: int,), some checks were
2603 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002604 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002605 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002606 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002607 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002608 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002609
Olli Etuaho55bde912017-10-25 13:41:13 +03002610 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002611
Olli Etuaho55bde912017-10-25 13:41:13 +03002612 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002613 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002614 TType arrayType(elementType);
2615 arrayType.makeArray(arraySize);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002616
Olli Etuaho454c34c2017-10-25 16:35:56 +03002617 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &arrayType);
2618
Olli Etuaho55bde912017-10-25 13:41:13 +03002619 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2620
2621 if (IsAtomicCounter(elementType.getBasicType()))
jchen104cdac9e2017-05-08 11:01:20 +08002622 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002623 checkAtomicCounterOffsetIsNotOverlapped(
2624 elementType, kAtomicCounterArrayStride * arrayType.getArraySizeProduct(), true,
2625 identifierLocation, arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002626 }
2627
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002628 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002629 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002630
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002631 if (variable)
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002632 {
2633 TIntermSymbol *symbol =
2634 new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2635 symbol->setLine(identifierLocation);
2636 declarationOut->appendDeclarator(symbol);
2637 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002638 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002639}
2640
Olli Etuaho13389b62016-10-16 11:48:18 +01002641void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2642 const TSourceLoc &identifierLocation,
2643 const TString &identifier,
2644 const TSourceLoc &initLocation,
2645 TIntermTyped *initializer,
2646 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002647{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002648 // If the declaration starting this declarator list was empty (example: int,), some checks were
2649 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002650 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002651 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002652 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2653 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002654 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002655
Olli Etuaho856c4972016-08-08 11:38:39 +03002656 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002657
Olli Etuaho13389b62016-10-16 11:48:18 +01002658 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002659 TType type(publicType);
2660 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002661 {
2662 //
2663 // build the intermediate representation
2664 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002665 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002666 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002667 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002668 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002669 }
2670}
2671
Olli Etuaho55bde912017-10-25 13:41:13 +03002672void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002673 const TSourceLoc &identifierLocation,
2674 const TString &identifier,
2675 const TSourceLoc &indexLocation,
Olli Etuaho55bde912017-10-25 13:41:13 +03002676 unsigned int arraySize,
Olli Etuaho13389b62016-10-16 11:48:18 +01002677 const TSourceLoc &initLocation,
2678 TIntermTyped *initializer,
2679 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002680{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002681 // If the declaration starting this declarator list was empty (example: int,), some checks were
2682 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002683 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002684 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002685 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002686 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002687 }
2688
Olli Etuaho55bde912017-10-25 13:41:13 +03002689 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002690
Olli Etuaho55bde912017-10-25 13:41:13 +03002691 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002692
Olli Etuaho55bde912017-10-25 13:41:13 +03002693 TType arrayType(elementType);
2694 arrayType.makeArray(arraySize);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002695
2696 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002697 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002698 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002699 {
2700 if (initNode)
2701 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002702 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002703 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002704 }
2705}
2706
jchen104cdac9e2017-05-08 11:01:20 +08002707void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2708 const TSourceLoc &location)
2709{
2710 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2711 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2712 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2713 {
2714 error(location, "Requires both binding and offset", "layout");
2715 return;
2716 }
2717 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2718}
2719
Olli Etuahocce89652017-06-19 16:04:09 +03002720void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2721 const TPublicType &type,
2722 const TSourceLoc &loc)
2723{
2724 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2725 !getFragmentPrecisionHigh())
2726 {
2727 error(loc, "precision is not supported in fragment shader", "highp");
2728 }
2729
2730 if (!CanSetDefaultPrecisionOnType(type))
2731 {
2732 error(loc, "illegal type argument for default precision qualifier",
2733 getBasicString(type.getBasicType()));
2734 return;
2735 }
2736 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2737}
2738
Shaob5cc1192017-07-06 10:47:20 +08002739bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2740{
2741 switch (typeQualifier.layoutQualifier.primitiveType)
2742 {
2743 case EptLines:
2744 case EptLinesAdjacency:
2745 case EptTriangles:
2746 case EptTrianglesAdjacency:
2747 return typeQualifier.qualifier == EvqGeometryIn;
2748
2749 case EptLineStrip:
2750 case EptTriangleStrip:
2751 return typeQualifier.qualifier == EvqGeometryOut;
2752
2753 case EptPoints:
2754 return true;
2755
2756 default:
2757 UNREACHABLE();
2758 return false;
2759 }
2760}
2761
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002762void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2763 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002764{
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002765 if (mGeometryShaderInputArraySize == 0u)
2766 {
2767 mGeometryShaderInputArraySize = inputArraySize;
2768 }
2769 else if (mGeometryShaderInputArraySize != inputArraySize)
2770 {
2771 error(line,
2772 "Array size or input primitive declaration doesn't match the size of earlier sized "
2773 "array inputs.",
2774 "layout");
2775 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002776}
2777
Shaob5cc1192017-07-06 10:47:20 +08002778bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2779{
2780 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2781
2782 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2783
2784 if (layoutQualifier.maxVertices != -1)
2785 {
2786 error(typeQualifier.line,
2787 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2788 return false;
2789 }
2790
2791 // Set mGeometryInputPrimitiveType if exists
2792 if (layoutQualifier.primitiveType != EptUndefined)
2793 {
2794 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2795 {
2796 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2797 return false;
2798 }
2799
2800 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2801 {
2802 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002803 setGeometryShaderInputArraySize(
2804 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2805 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002806 }
2807 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2808 {
2809 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2810 "layout");
2811 return false;
2812 }
2813 }
2814
2815 // Set mGeometryInvocations if exists
2816 if (layoutQualifier.invocations > 0)
2817 {
2818 if (mGeometryShaderInvocations == 0)
2819 {
2820 mGeometryShaderInvocations = layoutQualifier.invocations;
2821 }
2822 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2823 {
2824 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2825 "layout");
2826 return false;
2827 }
2828 }
2829
2830 return true;
2831}
2832
2833bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2834{
2835 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2836
2837 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2838
2839 if (layoutQualifier.invocations > 0)
2840 {
2841 error(typeQualifier.line,
2842 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2843 return false;
2844 }
2845
2846 // Set mGeometryOutputPrimitiveType if exists
2847 if (layoutQualifier.primitiveType != EptUndefined)
2848 {
2849 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2850 {
2851 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2852 return false;
2853 }
2854
2855 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2856 {
2857 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2858 }
2859 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2860 {
2861 error(typeQualifier.line,
2862 "primitive doesn't match earlier output primitive declaration", "layout");
2863 return false;
2864 }
2865 }
2866
2867 // Set mGeometryMaxVertices if exists
2868 if (layoutQualifier.maxVertices > -1)
2869 {
2870 if (mGeometryShaderMaxVertices == -1)
2871 {
2872 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2873 }
2874 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2875 {
2876 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2877 "layout");
2878 return false;
2879 }
2880 }
2881
2882 return true;
2883}
2884
Martin Radev70866b82016-07-22 15:27:42 +03002885void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002886{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002887 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002888 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002889
Martin Radev70866b82016-07-22 15:27:42 +03002890 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2891 typeQualifier.line);
2892
Jamie Madillc2128ff2016-07-04 10:26:17 -04002893 // It should never be the case, but some strange parser errors can send us here.
2894 if (layoutQualifier.isEmpty())
2895 {
2896 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002897 return;
2898 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002899
Martin Radev802abe02016-08-04 17:48:32 +03002900 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002901 {
Olli Etuaho43364892017-02-13 16:00:12 +00002902 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002903 return;
2904 }
2905
Olli Etuaho43364892017-02-13 16:00:12 +00002906 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2907
2908 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002909
2910 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2911
Andrei Volykhina5527072017-03-22 16:46:30 +03002912 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2913
jchen104cdac9e2017-05-08 11:01:20 +08002914 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2915
Martin Radev802abe02016-08-04 17:48:32 +03002916 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002917 {
Martin Radev802abe02016-08-04 17:48:32 +03002918 if (mComputeShaderLocalSizeDeclared &&
2919 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2920 {
2921 error(typeQualifier.line, "Work group size does not match the previous declaration",
2922 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002923 return;
2924 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002925
Martin Radev802abe02016-08-04 17:48:32 +03002926 if (mShaderVersion < 310)
2927 {
2928 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002929 return;
2930 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002931
Martin Radev4c4c8e72016-08-04 12:25:34 +03002932 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002933 {
2934 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002935 return;
2936 }
2937
2938 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2939 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2940
2941 const TConstantUnion *maxComputeWorkGroupSizeData =
2942 maxComputeWorkGroupSize->getConstPointer();
2943
2944 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2945 {
2946 if (layoutQualifier.localSize[i] != -1)
2947 {
2948 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2949 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2950 if (mComputeShaderLocalSize[i] < 1 ||
2951 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2952 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002953 std::stringstream reasonStream;
2954 reasonStream << "invalid value: Value must be at least 1 and no greater than "
2955 << maxComputeWorkGroupSizeValue;
2956 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03002957
Olli Etuaho4de340a2016-12-16 09:32:03 +00002958 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03002959 return;
2960 }
2961 }
2962 }
2963
2964 mComputeShaderLocalSizeDeclared = true;
2965 }
Shaob5cc1192017-07-06 10:47:20 +08002966 else if (typeQualifier.qualifier == EvqGeometryIn)
2967 {
2968 if (mShaderVersion < 310)
2969 {
2970 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
2971 return;
2972 }
2973
2974 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
2975 {
2976 return;
2977 }
2978 }
2979 else if (typeQualifier.qualifier == EvqGeometryOut)
2980 {
2981 if (mShaderVersion < 310)
2982 {
2983 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
2984 "layout");
2985 return;
2986 }
2987
2988 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
2989 {
2990 return;
2991 }
2992 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03002993 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
2994 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00002995 {
2996 // This error is only specified in WebGL, but tightens unspecified behavior in the native
2997 // specification.
2998 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
2999 {
3000 error(typeQualifier.line, "Number of views does not match the previous declaration",
3001 "layout");
3002 return;
3003 }
3004
3005 if (layoutQualifier.numViews == -1)
3006 {
3007 error(typeQualifier.line, "No num_views specified", "layout");
3008 return;
3009 }
3010
3011 if (layoutQualifier.numViews > mMaxNumViews)
3012 {
3013 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3014 "layout");
3015 return;
3016 }
3017
3018 mNumViews = layoutQualifier.numViews;
3019 }
Martin Radev802abe02016-08-04 17:48:32 +03003020 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003021 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003022 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003023 {
Martin Radev802abe02016-08-04 17:48:32 +03003024 return;
3025 }
3026
Jiajia Qinbc585152017-06-23 15:42:17 +08003027 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003028 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003029 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003030 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003031 return;
3032 }
3033
3034 if (mShaderVersion < 300)
3035 {
3036 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3037 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003038 return;
3039 }
3040
Olli Etuaho09b04a22016-12-15 13:30:26 +00003041 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003042
3043 if (layoutQualifier.matrixPacking != EmpUnspecified)
3044 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003045 if (typeQualifier.qualifier == EvqUniform)
3046 {
3047 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3048 }
3049 else if (typeQualifier.qualifier == EvqBuffer)
3050 {
3051 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3052 }
Martin Radev802abe02016-08-04 17:48:32 +03003053 }
3054
3055 if (layoutQualifier.blockStorage != EbsUnspecified)
3056 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003057 if (typeQualifier.qualifier == EvqUniform)
3058 {
3059 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3060 }
3061 else if (typeQualifier.qualifier == EvqBuffer)
3062 {
3063 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3064 }
Martin Radev802abe02016-08-04 17:48:32 +03003065 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003066 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003067}
3068
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003069TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3070 const TFunction &function,
3071 const TSourceLoc &location,
3072 bool insertParametersToSymbolTable)
3073{
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003074 checkIsNotReserved(location, function.getName());
3075
Olli Etuahofe486322017-03-21 09:30:54 +00003076 TIntermFunctionPrototype *prototype =
3077 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003078 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
3079 // point to the data that already exists in the symbol table.
3080 prototype->getFunctionSymbolInfo()->setFromFunction(function);
3081 prototype->setLine(location);
3082
3083 for (size_t i = 0; i < function.getParamCount(); i++)
3084 {
3085 const TConstParameter &param = function.getParam(i);
3086
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003087 TIntermSymbol *symbol = nullptr;
3088
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003089 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3090 // be used for unused args).
3091 if (param.name != nullptr)
3092 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003093 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003094 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003095 {
Olli Etuaho0f684632017-07-13 12:42:15 +03003096 TVariable *variable = symbolTable.declareVariable(param.name, *param.type);
3097 if (variable)
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003098 {
3099 symbol = new TIntermSymbol(variable->getUniqueId(), variable->getName(),
3100 variable->getType());
3101 }
3102 else
3103 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003104 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003105 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003106 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003107 // Unsized type of a named parameter should have already been checked and sanitized.
3108 ASSERT(!param.type->isUnsizedArray());
3109 }
3110 else
3111 {
3112 if (param.type->isUnsizedArray())
3113 {
3114 error(location, "function parameter array must be sized at compile time", "[]");
3115 // We don't need to size the arrays since the parameter is unnamed and hence
3116 // inaccessible.
3117 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003118 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003119 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003120 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003121 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3122 // symbol.
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003123 symbol = new TIntermSymbol(symbolTable.getEmptySymbolId(), "", *param.type);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003124 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003125 symbol->setLine(location);
3126 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003127 }
3128 return prototype;
3129}
3130
Olli Etuaho16c745a2017-01-16 17:02:27 +00003131TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3132 const TFunction &parsedFunction,
3133 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003134{
Olli Etuaho476197f2016-10-11 13:59:08 +01003135 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3136 // first declaration. Either way the instance in the symbol table is used to track whether the
3137 // function is declared multiple times.
3138 TFunction *function = static_cast<TFunction *>(
3139 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3140 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003141 {
3142 // ESSL 1.00.17 section 4.2.7.
3143 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3144 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003145 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003146 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003147
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003148 TIntermFunctionPrototype *prototype =
3149 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003150
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003151 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003152
3153 if (!symbolTable.atGlobalLevel())
3154 {
3155 // ESSL 3.00.4 section 4.2.4.
3156 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003157 }
3158
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003159 return prototype;
3160}
3161
Olli Etuaho336b1472016-10-05 16:37:55 +01003162TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003163 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003164 TIntermBlock *functionBody,
3165 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003166{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003167 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003168 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3169 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003170 error(location, "function does not return a value:",
3171 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003172 }
3173
Olli Etuahof51fdd22016-10-03 10:03:40 +01003174 if (functionBody == nullptr)
3175 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003176 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003177 functionBody->setLine(location);
3178 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003179 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003180 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003181 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003182
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003183 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003184 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003185}
3186
Olli Etuaho476197f2016-10-11 13:59:08 +01003187void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3188 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003189 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003190{
Olli Etuaho476197f2016-10-11 13:59:08 +01003191 ASSERT(function);
3192 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003193 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003194 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003195
3196 if (builtIn)
3197 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003198 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003199 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003200 else
Jamie Madill185fb402015-06-12 15:48:48 -04003201 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003202 TFunction *prevDec = static_cast<TFunction *>(
3203 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3204
3205 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3206 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3207 // occurance.
3208 if (*function != prevDec)
3209 {
3210 // Swap the parameters of the previous declaration to the parameters of the function
3211 // definition (parameter names may differ).
3212 prevDec->swapParameters(**function);
3213
3214 // The function definition will share the same symbol as any previous declaration.
3215 *function = prevDec;
3216 }
3217
3218 if ((*function)->isDefined())
3219 {
3220 error(location, "function already has a body", (*function)->getName().c_str());
3221 }
3222
3223 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003224 }
Jamie Madill185fb402015-06-12 15:48:48 -04003225
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003226 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003227 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003228 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003229
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003230 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003231 setLoopNestingLevel(0);
3232}
3233
Jamie Madillb98c3a82015-07-23 14:26:04 -04003234TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003235{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003236 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003237 // We don't know at this point whether this is a function definition or a prototype.
3238 // The definition production code will check for redefinitions.
3239 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003240 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003241 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3242 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003243 //
3244 TFunction *prevDec =
3245 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303246
Martin Radevda6254b2016-12-14 17:00:36 +02003247 if (getShaderVersion() >= 300 &&
3248 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
3249 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303250 {
Martin Radevda6254b2016-12-14 17:00:36 +02003251 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303252 // Therefore overloading or redefining builtin functions is an error.
3253 error(location, "Name of a built-in function cannot be redeclared as function",
3254 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303255 }
3256 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003257 {
3258 if (prevDec->getReturnType() != function->getReturnType())
3259 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003260 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003261 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003262 }
3263 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3264 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003265 if (prevDec->getParam(i).type->getQualifier() !=
3266 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003267 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003268 error(location,
3269 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003270 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003271 }
3272 }
3273 }
3274
3275 //
3276 // Check for previously declared variables using the same name.
3277 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003278 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003279 if (prevSym)
3280 {
3281 if (!prevSym->isFunction())
3282 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003283 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003284 }
3285 }
3286 else
3287 {
3288 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003289 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003290 }
3291
3292 // We're at the inner scope level of the function's arguments and body statement.
3293 // Add the function prototype to the surrounding scope instead.
3294 symbolTable.getOuterLevel()->insert(function);
3295
Olli Etuaho78d13742017-01-18 13:06:10 +00003296 // Raise error message if main function takes any parameters or return anything other than void
3297 if (function->getName() == "main")
3298 {
3299 if (function->getParamCount() > 0)
3300 {
3301 error(location, "function cannot take any parameter(s)", "main");
3302 }
3303 if (function->getReturnType().getBasicType() != EbtVoid)
3304 {
3305 error(location, "main function cannot return a value",
3306 function->getReturnType().getBasicString());
3307 }
3308 }
3309
Jamie Madill185fb402015-06-12 15:48:48 -04003310 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003311 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3312 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003313 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3314 //
3315 return function;
3316}
3317
Olli Etuaho9de84a52016-06-14 17:36:01 +03003318TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3319 const TString *name,
3320 const TSourceLoc &location)
3321{
3322 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3323 {
3324 error(location, "no qualifiers allowed for function return",
3325 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003326 }
3327 if (!type.layoutQualifier.isEmpty())
3328 {
3329 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003330 }
jchen10cc2a10e2017-05-03 14:05:12 +08003331 // make sure an opaque type is not involved as well...
3332 std::string reason(getBasicString(type.getBasicType()));
3333 reason += "s can't be function return values";
3334 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003335 if (mShaderVersion < 300)
3336 {
3337 // Array return values are forbidden, but there's also no valid syntax for declaring array
3338 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00003339 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003340
3341 if (type.isStructureContainingArrays())
3342 {
3343 // ESSL 1.00.17 section 6.1 Function Definitions
3344 error(location, "structures containing arrays can't be function return values",
3345 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003346 }
3347 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003348
3349 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003350 return new TFunction(&symbolTable, name, new TType(type));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003351}
3352
Olli Etuahocce89652017-06-19 16:04:09 +03003353TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3354{
Olli Etuahocce89652017-06-19 16:04:09 +03003355 const TType *returnType = TCache::getType(EbtVoid, EbpUndefined);
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003356 return new TFunction(&symbolTable, name, returnType);
Olli Etuahocce89652017-06-19 16:04:09 +03003357}
3358
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003359TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003360{
Olli Etuahocce89652017-06-19 16:04:09 +03003361 if (mShaderVersion < 300 && publicType.array)
3362 {
3363 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3364 "[]");
3365 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003366 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003367 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003368 error(publicType.getLine(), "constructor can't be a structure definition",
3369 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003370 }
3371
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003372 TType *type = new TType(publicType);
3373 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003374 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003375 error(publicType.getLine(), "cannot construct this type",
3376 getBasicString(publicType.getBasicType()));
3377 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003378 }
3379
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003380 return new TFunction(&symbolTable, nullptr, type, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003381}
3382
Olli Etuaho55bde912017-10-25 13:41:13 +03003383void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3384 const char *errorMessage,
3385 const char *token,
3386 TType *arrayType)
3387{
3388 if (arrayType->isUnsizedArray())
3389 {
3390 error(line, errorMessage, token);
3391 arrayType->sizeUnsizedArrays(TVector<unsigned int>());
3392 }
3393}
3394
3395TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahocce89652017-06-19 16:04:09 +03003396 const TString *name,
3397 const TSourceLoc &nameLoc)
3398{
Olli Etuaho55bde912017-10-25 13:41:13 +03003399 ASSERT(type);
3400 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name->c_str(),
3401 type);
3402 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003403 {
3404 error(nameLoc, "illegal use of type 'void'", name->c_str());
3405 }
3406 checkIsNotReserved(nameLoc, *name);
Olli Etuahocce89652017-06-19 16:04:09 +03003407 TParameter param = {name, type};
3408 return param;
3409}
3410
Olli Etuaho55bde912017-10-25 13:41:13 +03003411TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3412 const TString *name,
3413 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003414{
Olli Etuaho55bde912017-10-25 13:41:13 +03003415 TType *type = new TType(publicType);
3416 return parseParameterDeclarator(type, name, nameLoc);
3417}
3418
3419TParameter TParseContext::parseParameterArrayDeclarator(const TString *name,
3420 const TSourceLoc &nameLoc,
3421 unsigned int arraySize,
3422 const TSourceLoc &arrayLoc,
3423 TPublicType *elementType)
3424{
3425 checkArrayElementIsNotArray(arrayLoc, *elementType);
3426 TType *arrayType = new TType(*elementType);
3427 arrayType->makeArray(arraySize);
3428 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003429}
3430
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003431bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(TIntermSequence *arguments,
3432 TType type,
3433 const TSourceLoc &line)
3434{
3435 if (arguments->empty())
3436 {
3437 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3438 return false;
3439 }
3440 for (TIntermNode *arg : *arguments)
3441 {
3442 TIntermTyped *element = arg->getAsTyped();
3443 ASSERT(element);
3444 size_t dimensionalityFromElement = element->getType().getArraySizes().size() + 1u;
3445 if (dimensionalityFromElement > type.getArraySizes().size())
3446 {
3447 error(line, "constructing from a non-dereferenced array", "constructor");
3448 return false;
3449 }
3450 else if (dimensionalityFromElement < type.getArraySizes().size())
3451 {
3452 if (dimensionalityFromElement == 1u)
3453 {
3454 error(line, "implicitly sized array of arrays constructor argument is not an array",
3455 "constructor");
3456 }
3457 else
3458 {
3459 error(line,
3460 "implicitly sized array of arrays constructor argument dimensionality is too "
3461 "low",
3462 "constructor");
3463 }
3464 return false;
3465 }
3466 }
3467 return true;
3468}
3469
Jamie Madillb98c3a82015-07-23 14:26:04 -04003470// This function is used to test for the correctness of the parameters passed to various constructor
3471// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003472//
Olli Etuaho856c4972016-08-08 11:38:39 +03003473// Returns a node to add to the tree regardless of if an error was generated or not.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003474//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003475TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003476 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303477 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003478{
Olli Etuaho856c4972016-08-08 11:38:39 +03003479 if (type.isUnsizedArray())
3480 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003481 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003482 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003483 type.sizeUnsizedArrays(TVector<unsigned int>());
Olli Etuaho3ec75682017-07-05 17:02:55 +03003484 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003485 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003486 TIntermTyped *firstElement = arguments->at(0)->getAsTyped();
3487 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003488 if (type.getOutermostArraySize() == 0u)
3489 {
3490 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments->size()));
3491 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003492 for (size_t i = 0; i < firstElement->getType().getArraySizes().size(); ++i)
3493 {
3494 if (type.getArraySizes()[i] == 0u)
3495 {
3496 type.setArraySize(i, firstElement->getType().getArraySizes().at(i));
3497 }
3498 }
3499 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003500 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003501
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003502 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003503 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003504 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003505 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003506
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003507 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003508 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003509
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003510 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3511 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003512 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003513 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003514 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003515 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003516}
3517
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003518//
3519// Interface/uniform blocks
Jiawei Shaod8105a02017-08-08 09:54:36 +08003520// TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003521//
Olli Etuaho13389b62016-10-16 11:48:18 +01003522TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003523 const TTypeQualifierBuilder &typeQualifierBuilder,
3524 const TSourceLoc &nameLine,
3525 const TString &blockName,
3526 TFieldList *fieldList,
3527 const TString *instanceName,
3528 const TSourceLoc &instanceLine,
3529 TIntermTyped *arrayIndex,
3530 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003531{
Olli Etuaho856c4972016-08-08 11:38:39 +03003532 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003533
Olli Etuaho77ba4082016-12-16 12:01:18 +00003534 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003535
Jiajia Qinbc585152017-06-23 15:42:17 +08003536 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003537 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003538 error(typeQualifier.line,
3539 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3540 "3.10",
3541 getQualifierString(typeQualifier.qualifier));
3542 }
3543 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3544 {
3545 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003546 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003547 }
3548
Martin Radev70866b82016-07-22 15:27:42 +03003549 if (typeQualifier.invariant)
3550 {
3551 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3552 }
3553
Jiajia Qinbc585152017-06-23 15:42:17 +08003554 if (typeQualifier.qualifier != EvqBuffer)
3555 {
3556 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3557 }
Olli Etuaho43364892017-02-13 16:00:12 +00003558
jchen10af713a22017-04-19 09:10:56 +08003559 // add array index
3560 unsigned int arraySize = 0;
3561 if (arrayIndex != nullptr)
3562 {
3563 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3564 }
3565
3566 if (mShaderVersion < 310)
3567 {
3568 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3569 }
3570 else
3571 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003572 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3573 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003574 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003575
Andrei Volykhina5527072017-03-22 16:46:30 +03003576 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3577
Jamie Madill099c0f32013-06-20 11:55:52 -04003578 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003579 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003580
Jamie Madill099c0f32013-06-20 11:55:52 -04003581 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3582 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003583 if (typeQualifier.qualifier == EvqUniform)
3584 {
3585 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3586 }
3587 else if (typeQualifier.qualifier == EvqBuffer)
3588 {
3589 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3590 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003591 }
3592
Jamie Madill1566ef72013-06-20 11:55:54 -04003593 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3594 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003595 if (typeQualifier.qualifier == EvqUniform)
3596 {
3597 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3598 }
3599 else if (typeQualifier.qualifier == EvqBuffer)
3600 {
3601 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3602 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003603 }
3604
Olli Etuaho856c4972016-08-08 11:38:39 +03003605 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003606
Martin Radev2cc85b32016-08-05 16:22:53 +03003607 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3608
Olli Etuaho0f684632017-07-13 12:42:15 +03003609 if (!symbolTable.declareInterfaceBlockName(&blockName))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303610 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003611 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003612 }
3613
Jamie Madill98493dd2013-07-08 14:39:03 -04003614 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303615 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3616 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003617 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303618 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003619 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303620 {
jchen10cc2a10e2017-05-03 14:05:12 +08003621 std::string reason("unsupported type - ");
3622 reason += fieldType->getBasicString();
3623 reason += " types are not allowed in interface blocks";
3624 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003625 }
3626
Jamie Madill98493dd2013-07-08 14:39:03 -04003627 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003628 switch (qualifier)
3629 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003630 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003631 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003632 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003633 if (typeQualifier.qualifier == EvqBuffer)
3634 {
3635 error(field->line(), "invalid qualifier on shader storage block member",
3636 getQualifierString(qualifier));
3637 }
3638 break;
3639 case EvqBuffer:
3640 if (typeQualifier.qualifier == EvqUniform)
3641 {
3642 error(field->line(), "invalid qualifier on uniform block member",
3643 getQualifierString(qualifier));
3644 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003645 break;
3646 default:
3647 error(field->line(), "invalid qualifier on interface block member",
3648 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003649 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003650 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003651
Martin Radev70866b82016-07-22 15:27:42 +03003652 if (fieldType->isInvariant())
3653 {
3654 error(field->line(), "invalid qualifier on interface block member", "invariant");
3655 }
3656
Jamie Madilla5efff92013-06-06 11:56:47 -04003657 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003658 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003659 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003660 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003661
Jamie Madill98493dd2013-07-08 14:39:03 -04003662 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003663 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003664 error(field->line(), "invalid layout qualifier: cannot be used here",
3665 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003666 }
3667
Jamie Madill98493dd2013-07-08 14:39:03 -04003668 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003669 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003670 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003671 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003672 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003673 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003674 warning(field->line(),
3675 "extraneous layout qualifier: only has an effect on matrix types",
3676 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003677 }
3678
Jamie Madill98493dd2013-07-08 14:39:03 -04003679 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003680
3681 if (typeQualifier.qualifier == EvqBuffer)
3682 {
3683 // set memory qualifiers
3684 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3685 // qualified with a memory qualifier, it is as if all of its members were declared with
3686 // the same memory qualifier.
3687 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3688 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3689 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3690 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3691 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3692 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3693 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3694 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3695 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3696 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3697 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003698 }
3699
Jamie Madillb98c3a82015-07-23 14:26:04 -04003700 TInterfaceBlock *interfaceBlock =
Shaob18c33e2017-08-16 12:37:51 +08003701 new TInterfaceBlock(&blockName, fieldList, instanceName, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003702 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
3703 if (arrayIndex != nullptr)
3704 {
3705 interfaceBlockType.makeArray(arraySize);
3706 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003707
3708 TString symbolName = "";
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003709 const TSymbolUniqueId *symbolId = nullptr;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003710
Jamie Madill98493dd2013-07-08 14:39:03 -04003711 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003712 {
3713 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003714 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3715 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003716 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303717 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003718
3719 // set parent pointer of the field variable
3720 fieldType->setInterfaceBlock(interfaceBlock);
3721
Olli Etuaho0f684632017-07-13 12:42:15 +03003722 TVariable *fieldVariable = symbolTable.declareVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003723
Olli Etuaho0f684632017-07-13 12:42:15 +03003724 if (fieldVariable)
3725 {
3726 fieldVariable->setQualifier(typeQualifier.qualifier);
3727 }
3728 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303729 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003730 error(field->line(), "redefinition of an interface block member name",
3731 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003732 }
3733 }
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003734 symbolId = &symbolTable.getEmptySymbolId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003735 }
3736 else
3737 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003738 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003739
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003740 // add a symbol for this interface block
Olli Etuaho0f684632017-07-13 12:42:15 +03003741 TVariable *instanceTypeDef = symbolTable.declareVariable(instanceName, interfaceBlockType);
3742 if (instanceTypeDef)
3743 {
3744 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003745 symbolId = &instanceTypeDef->getUniqueId();
Olli Etuaho0f684632017-07-13 12:42:15 +03003746 }
3747 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303748 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003749 error(instanceLine, "redefinition of an interface block instance name",
3750 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003751 }
Olli Etuaho0f684632017-07-13 12:42:15 +03003752 symbolName = *instanceName;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003753 }
3754
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003755 TIntermDeclaration *declaration = nullptr;
3756
3757 if (symbolId)
3758 {
3759 TIntermSymbol *blockSymbol = new TIntermSymbol(*symbolId, symbolName, interfaceBlockType);
3760 blockSymbol->setLine(typeQualifier.line);
3761 declaration = new TIntermDeclaration();
3762 declaration->appendDeclarator(blockSymbol);
3763 declaration->setLine(nameLine);
3764 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003765
3766 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003767 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003768}
3769
Olli Etuaho383b7912016-08-05 11:22:59 +03003770void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003771{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003772 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003773
3774 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003775 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303776 if (mStructNestingLevel > 1)
3777 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003778 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003779 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003780}
3781
3782void TParseContext::exitStructDeclaration()
3783{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003784 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003785}
3786
Olli Etuaho8a176262016-08-16 14:23:01 +03003787void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003788{
Jamie Madillacb4b812016-11-07 13:50:29 -05003789 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303790 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003791 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003792 }
3793
Arun Patole7e7e68d2015-05-22 12:02:25 +05303794 if (field.type()->getBasicType() != EbtStruct)
3795 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003796 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003797 }
3798
3799 // We're already inside a structure definition at this point, so add
3800 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303801 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3802 {
Jamie Madill41a49272014-03-18 16:10:13 -04003803 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003804 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3805 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003806 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003807 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003808 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003809 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003810}
3811
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003812//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003813// Parse an array index expression
3814//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003815TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3816 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303817 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003818{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003819 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3820 {
3821 if (baseExpression->getAsSymbolNode())
3822 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303823 error(location, " left of '[' is not of type array, matrix, or vector ",
3824 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003825 }
3826 else
3827 {
3828 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3829 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003830
Olli Etuaho3ec75682017-07-05 17:02:55 +03003831 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003832 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003833
Jiawei Shaod8105a02017-08-08 09:54:36 +08003834 if (baseExpression->getQualifier() == EvqPerVertexIn)
3835 {
3836 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
3837 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3838 {
3839 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3840 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3841 }
3842 }
3843
Jamie Madill21c1e452014-12-29 11:33:41 -05003844 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3845
Olli Etuaho36b05142015-11-12 13:10:42 +02003846 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3847 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3848 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3849 // index is a constant expression.
3850 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3851 {
3852 if (baseExpression->isInterfaceBlock())
3853 {
Jiawei Shaod8105a02017-08-08 09:54:36 +08003854 // TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
3855 switch (baseExpression->getQualifier())
3856 {
3857 case EvqPerVertexIn:
3858 break;
3859 case EvqUniform:
3860 case EvqBuffer:
3861 error(location,
3862 "array indexes for uniform block arrays and shader storage block arrays "
3863 "must be constant integral expressions",
3864 "[");
3865 break;
3866 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003867 // We can reach here only in error cases.
3868 ASSERT(mDiagnostics->numErrors() > 0);
3869 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003870 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003871 }
3872 else if (baseExpression->getQualifier() == EvqFragmentOut)
3873 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003874 error(location,
3875 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003876 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003877 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3878 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003879 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003880 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003881 }
3882
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003883 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003884 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003885 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3886 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3887 // constant fold expressions that are not constant expressions). The most compatible way to
3888 // handle this case is to report a warning instead of an error and force the index to be in
3889 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003890 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003891 int index = 0;
3892 if (indexConstantUnion->getBasicType() == EbtInt)
3893 {
3894 index = indexConstantUnion->getIConst(0);
3895 }
3896 else if (indexConstantUnion->getBasicType() == EbtUInt)
3897 {
3898 index = static_cast<int>(indexConstantUnion->getUConst(0));
3899 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003900
3901 int safeIndex = -1;
3902
3903 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003904 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003905 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003906 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003907 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003908 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003909 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003910 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003911 "GL_EXT_draw_buffers is disabled",
3912 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003913 safeIndex = 0;
3914 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003915 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003916 // Only do generic out-of-range check if similar error hasn't already been reported.
3917 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003918 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003919 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003920 baseExpression->getOutermostArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003921 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003922 }
3923 }
3924 else if (baseExpression->isMatrix())
3925 {
3926 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003927 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003928 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003929 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003930 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003931 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003932 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3933 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003934 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003935 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003936
3937 ASSERT(safeIndex >= 0);
3938 // Data of constant unions can't be changed, because it may be shared with other
3939 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3940 // sanitized object.
Olli Etuaho56229f12017-07-10 14:16:33 +03003941 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003942 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003943 TConstantUnion *safeConstantUnion = new TConstantUnion();
3944 safeConstantUnion->setIConst(safeIndex);
3945 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
Olli Etuaho56229f12017-07-10 14:16:33 +03003946 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003947 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003948
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003949 TIntermBinary *node = new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
3950 node->setLine(location);
3951 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003952 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003953 else
3954 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003955 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
3956 node->setLine(location);
3957 // Indirect indexing can never be constant folded.
3958 return node;
Jamie Madill7164cf42013-07-08 13:30:59 -04003959 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003960}
3961
Olli Etuaho90892fb2016-07-14 14:44:51 +03003962int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3963 const TSourceLoc &location,
3964 int index,
3965 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003966 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003967{
3968 if (index >= arraySize || index < 0)
3969 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003970 std::stringstream reasonStream;
3971 reasonStream << reason << " '" << index << "'";
3972 std::string token = reasonStream.str();
3973 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003974 if (index < 0)
3975 {
3976 return 0;
3977 }
3978 else
3979 {
3980 return arraySize - 1;
3981 }
3982 }
3983 return index;
3984}
3985
Jamie Madillb98c3a82015-07-23 14:26:04 -04003986TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3987 const TSourceLoc &dotLocation,
3988 const TString &fieldString,
3989 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003990{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003991 if (baseExpression->isArray())
3992 {
3993 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003994 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003995 }
3996
3997 if (baseExpression->isVector())
3998 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003999 TVector<int> fieldOffsets;
4000 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4001 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004002 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004003 fieldOffsets.resize(1);
4004 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004005 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004006 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4007 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004008
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004009 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004010 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004011 else if (baseExpression->getBasicType() == EbtStruct)
4012 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304013 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004014 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004015 {
4016 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004017 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004018 }
4019 else
4020 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004021 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004022 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004023 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004024 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004025 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004026 {
4027 fieldFound = true;
4028 break;
4029 }
4030 }
4031 if (fieldFound)
4032 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004033 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004034 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004035 TIntermBinary *node =
4036 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4037 node->setLine(dotLocation);
4038 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004039 }
4040 else
4041 {
4042 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004043 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004044 }
4045 }
4046 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004047 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004048 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304049 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004050 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004051 {
4052 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004053 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004054 }
4055 else
4056 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004057 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004058 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004059 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004060 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004061 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004062 {
4063 fieldFound = true;
4064 break;
4065 }
4066 }
4067 if (fieldFound)
4068 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004069 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004070 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004071 TIntermBinary *node =
4072 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4073 node->setLine(dotLocation);
4074 // Indexing interface blocks can never be constant folded.
4075 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004076 }
4077 else
4078 {
4079 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004080 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004081 }
4082 }
4083 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004084 else
4085 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004086 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004087 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004088 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304089 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004090 }
4091 else
4092 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304093 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004094 " field selection requires structure, vector, or interface block on left hand "
4095 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304096 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004097 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004098 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004099 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004100}
4101
Jamie Madillb98c3a82015-07-23 14:26:04 -04004102TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4103 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004104{
Martin Radev802abe02016-08-04 17:48:32 +03004105 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004106
4107 if (qualifierType == "shared")
4108 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004109 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004110 {
4111 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4112 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004113 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004114 }
4115 else if (qualifierType == "packed")
4116 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004117 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004118 {
4119 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4120 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004121 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004122 }
4123 else if (qualifierType == "std140")
4124 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004125 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004126 }
4127 else if (qualifierType == "row_major")
4128 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004129 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004130 }
4131 else if (qualifierType == "column_major")
4132 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004133 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004134 }
4135 else if (qualifierType == "location")
4136 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004137 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4138 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004139 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004140 else if (qualifierType == "yuv" && isExtensionEnabled(TExtension::EXT_YUV_target) &&
Andrei Volykhina5527072017-03-22 16:46:30 +03004141 mShaderType == GL_FRAGMENT_SHADER)
4142 {
4143 qualifier.yuv = true;
4144 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004145 else if (qualifierType == "rgba32f")
4146 {
4147 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4148 qualifier.imageInternalFormat = EiifRGBA32F;
4149 }
4150 else if (qualifierType == "rgba16f")
4151 {
4152 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4153 qualifier.imageInternalFormat = EiifRGBA16F;
4154 }
4155 else if (qualifierType == "r32f")
4156 {
4157 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4158 qualifier.imageInternalFormat = EiifR32F;
4159 }
4160 else if (qualifierType == "rgba8")
4161 {
4162 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4163 qualifier.imageInternalFormat = EiifRGBA8;
4164 }
4165 else if (qualifierType == "rgba8_snorm")
4166 {
4167 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4168 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4169 }
4170 else if (qualifierType == "rgba32i")
4171 {
4172 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4173 qualifier.imageInternalFormat = EiifRGBA32I;
4174 }
4175 else if (qualifierType == "rgba16i")
4176 {
4177 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4178 qualifier.imageInternalFormat = EiifRGBA16I;
4179 }
4180 else if (qualifierType == "rgba8i")
4181 {
4182 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4183 qualifier.imageInternalFormat = EiifRGBA8I;
4184 }
4185 else if (qualifierType == "r32i")
4186 {
4187 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4188 qualifier.imageInternalFormat = EiifR32I;
4189 }
4190 else if (qualifierType == "rgba32ui")
4191 {
4192 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4193 qualifier.imageInternalFormat = EiifRGBA32UI;
4194 }
4195 else if (qualifierType == "rgba16ui")
4196 {
4197 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4198 qualifier.imageInternalFormat = EiifRGBA16UI;
4199 }
4200 else if (qualifierType == "rgba8ui")
4201 {
4202 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4203 qualifier.imageInternalFormat = EiifRGBA8UI;
4204 }
4205 else if (qualifierType == "r32ui")
4206 {
4207 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4208 qualifier.imageInternalFormat = EiifR32UI;
4209 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004210 else if (qualifierType == "points" && 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 = EptPoints;
4215 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004216 else if (qualifierType == "lines" && 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 = EptLines;
4221 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004222 else if (qualifierType == "lines_adjacency" &&
4223 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004224 mShaderType == GL_GEOMETRY_SHADER_OES)
4225 {
4226 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4227 qualifier.primitiveType = EptLinesAdjacency;
4228 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004229 else if (qualifierType == "triangles" && 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 = EptTriangles;
4234 }
4235 else if (qualifierType == "triangles_adjacency" &&
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004236 isExtensionEnabled(TExtension::OES_geometry_shader) &&
4237 mShaderType == GL_GEOMETRY_SHADER_OES)
Shaob5cc1192017-07-06 10:47:20 +08004238 {
4239 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4240 qualifier.primitiveType = EptTrianglesAdjacency;
4241 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004242 else if (qualifierType == "line_strip" && isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004243 mShaderType == GL_GEOMETRY_SHADER_OES)
4244 {
4245 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4246 qualifier.primitiveType = EptLineStrip;
4247 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004248 else if (qualifierType == "triangle_strip" &&
4249 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004250 mShaderType == GL_GEOMETRY_SHADER_OES)
4251 {
4252 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4253 qualifier.primitiveType = EptTriangleStrip;
4254 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004255
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004256 else
4257 {
4258 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004259 }
4260
Jamie Madilla5efff92013-06-06 11:56:47 -04004261 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004262}
4263
Martin Radev802abe02016-08-04 17:48:32 +03004264void TParseContext::parseLocalSize(const TString &qualifierType,
4265 const TSourceLoc &qualifierTypeLine,
4266 int intValue,
4267 const TSourceLoc &intValueLine,
4268 const std::string &intValueString,
4269 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004270 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004271{
Olli Etuaho856c4972016-08-08 11:38:39 +03004272 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004273 if (intValue < 1)
4274 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004275 std::stringstream reasonStream;
4276 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4277 std::string reason = reasonStream.str();
4278 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004279 }
4280 (*localSize)[index] = intValue;
4281}
4282
Olli Etuaho09b04a22016-12-15 13:30:26 +00004283void TParseContext::parseNumViews(int intValue,
4284 const TSourceLoc &intValueLine,
4285 const std::string &intValueString,
4286 int *numViews)
4287{
4288 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4289 // specification.
4290 if (intValue < 1)
4291 {
4292 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4293 }
4294 *numViews = intValue;
4295}
4296
Shaob5cc1192017-07-06 10:47:20 +08004297void TParseContext::parseInvocations(int intValue,
4298 const TSourceLoc &intValueLine,
4299 const std::string &intValueString,
4300 int *numInvocations)
4301{
4302 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4303 // it doesn't make sense to accept invocations <= 0.
4304 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4305 {
4306 error(intValueLine,
4307 "out of range: invocations must be in the range of [1, "
4308 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4309 intValueString.c_str());
4310 }
4311 else
4312 {
4313 *numInvocations = intValue;
4314 }
4315}
4316
4317void TParseContext::parseMaxVertices(int intValue,
4318 const TSourceLoc &intValueLine,
4319 const std::string &intValueString,
4320 int *maxVertices)
4321{
4322 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4323 // it doesn't make sense to accept max_vertices < 0.
4324 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4325 {
4326 error(
4327 intValueLine,
4328 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4329 intValueString.c_str());
4330 }
4331 else
4332 {
4333 *maxVertices = intValue;
4334 }
4335}
4336
Jamie Madillb98c3a82015-07-23 14:26:04 -04004337TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4338 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004339 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304340 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004341{
Martin Radev802abe02016-08-04 17:48:32 +03004342 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004343
Martin Radev802abe02016-08-04 17:48:32 +03004344 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004345
Martin Radev802abe02016-08-04 17:48:32 +03004346 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004347 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004348 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004349 if (intValue < 0)
4350 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004351 error(intValueLine, "out of range: location must be non-negative",
4352 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004353 }
4354 else
4355 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004356 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004357 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004358 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004359 }
Olli Etuaho43364892017-02-13 16:00:12 +00004360 else if (qualifierType == "binding")
4361 {
4362 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4363 if (intValue < 0)
4364 {
4365 error(intValueLine, "out of range: binding must be non-negative",
4366 intValueString.c_str());
4367 }
4368 else
4369 {
4370 qualifier.binding = intValue;
4371 }
4372 }
jchen104cdac9e2017-05-08 11:01:20 +08004373 else if (qualifierType == "offset")
4374 {
4375 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4376 if (intValue < 0)
4377 {
4378 error(intValueLine, "out of range: offset must be non-negative",
4379 intValueString.c_str());
4380 }
4381 else
4382 {
4383 qualifier.offset = intValue;
4384 }
4385 }
Martin Radev802abe02016-08-04 17:48:32 +03004386 else if (qualifierType == "local_size_x")
4387 {
4388 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4389 &qualifier.localSize);
4390 }
4391 else if (qualifierType == "local_size_y")
4392 {
4393 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4394 &qualifier.localSize);
4395 }
4396 else if (qualifierType == "local_size_z")
4397 {
4398 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4399 &qualifier.localSize);
4400 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004401 else if (qualifierType == "num_views" && isExtensionEnabled(TExtension::OVR_multiview) &&
Olli Etuaho09b04a22016-12-15 13:30:26 +00004402 mShaderType == GL_VERTEX_SHADER)
4403 {
4404 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4405 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004406 else if (qualifierType == "invocations" &&
4407 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004408 mShaderType == GL_GEOMETRY_SHADER_OES)
4409 {
4410 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4411 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004412 else if (qualifierType == "max_vertices" &&
4413 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004414 mShaderType == GL_GEOMETRY_SHADER_OES)
4415 {
4416 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4417 }
4418
Martin Radev802abe02016-08-04 17:48:32 +03004419 else
4420 {
4421 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004422 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004423
Jamie Madilla5efff92013-06-06 11:56:47 -04004424 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004425}
4426
Olli Etuaho613b9592016-09-05 12:05:53 +03004427TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4428{
4429 return new TTypeQualifierBuilder(
4430 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4431 mShaderVersion);
4432}
4433
Olli Etuahocce89652017-06-19 16:04:09 +03004434TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4435 const TSourceLoc &loc)
4436{
4437 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4438 return new TStorageQualifierWrapper(qualifier, loc);
4439}
4440
4441TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4442{
4443 if (getShaderType() == GL_VERTEX_SHADER)
4444 {
4445 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4446 }
4447 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4448}
4449
4450TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4451{
4452 if (declaringFunction())
4453 {
4454 return new TStorageQualifierWrapper(EvqIn, loc);
4455 }
Shaob5cc1192017-07-06 10:47:20 +08004456
4457 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004458 {
Shaob5cc1192017-07-06 10:47:20 +08004459 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004460 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004461 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004462 {
4463 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4464 }
4465 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004466 }
Shaob5cc1192017-07-06 10:47:20 +08004467 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004468 {
Shaob5cc1192017-07-06 10:47:20 +08004469 if (mShaderVersion < 300)
4470 {
4471 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4472 }
4473 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004474 }
Shaob5cc1192017-07-06 10:47:20 +08004475 case GL_COMPUTE_SHADER:
4476 {
4477 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4478 }
4479 case GL_GEOMETRY_SHADER_OES:
4480 {
4481 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4482 }
4483 default:
4484 {
4485 UNREACHABLE();
4486 return new TStorageQualifierWrapper(EvqLast, loc);
4487 }
Olli Etuahocce89652017-06-19 16:04:09 +03004488 }
Olli Etuahocce89652017-06-19 16:04:09 +03004489}
4490
4491TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4492{
4493 if (declaringFunction())
4494 {
4495 return new TStorageQualifierWrapper(EvqOut, loc);
4496 }
Shaob5cc1192017-07-06 10:47:20 +08004497 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004498 {
Shaob5cc1192017-07-06 10:47:20 +08004499 case GL_VERTEX_SHADER:
4500 {
4501 if (mShaderVersion < 300)
4502 {
4503 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4504 }
4505 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4506 }
4507 case GL_FRAGMENT_SHADER:
4508 {
4509 if (mShaderVersion < 300)
4510 {
4511 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4512 }
4513 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4514 }
4515 case GL_COMPUTE_SHADER:
4516 {
4517 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4518 return new TStorageQualifierWrapper(EvqLast, loc);
4519 }
4520 case GL_GEOMETRY_SHADER_OES:
4521 {
4522 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4523 }
4524 default:
4525 {
4526 UNREACHABLE();
4527 return new TStorageQualifierWrapper(EvqLast, loc);
4528 }
Olli Etuahocce89652017-06-19 16:04:09 +03004529 }
Olli Etuahocce89652017-06-19 16:04:09 +03004530}
4531
4532TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4533{
4534 if (!declaringFunction())
4535 {
4536 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4537 }
4538 return new TStorageQualifierWrapper(EvqInOut, loc);
4539}
4540
Jamie Madillb98c3a82015-07-23 14:26:04 -04004541TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004542 TLayoutQualifier rightQualifier,
4543 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004544{
Martin Radevc28888b2016-07-22 15:27:42 +03004545 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004546 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004547}
4548
Olli Etuahocce89652017-06-19 16:04:09 +03004549TField *TParseContext::parseStructDeclarator(TString *identifier, const TSourceLoc &loc)
4550{
4551 checkIsNotReserved(loc, *identifier);
4552 TType *type = new TType(EbtVoid, EbpUndefined);
4553 return new TField(type, identifier, loc);
4554}
4555
4556TField *TParseContext::parseStructArrayDeclarator(TString *identifier,
4557 const TSourceLoc &loc,
Olli Etuaho4ddae352017-10-26 16:20:18 +03004558 unsigned int arraySize,
Olli Etuahocce89652017-06-19 16:04:09 +03004559 const TSourceLoc &arraySizeLoc)
4560{
4561 checkIsNotReserved(loc, *identifier);
4562
4563 TType *type = new TType(EbtVoid, EbpUndefined);
Olli Etuaho4ddae352017-10-26 16:20:18 +03004564 type->makeArray(arraySize);
Olli Etuahocce89652017-06-19 16:04:09 +03004565
4566 return new TField(type, identifier, loc);
4567}
4568
Olli Etuaho4de340a2016-12-16 09:32:03 +00004569TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4570 const TFieldList *newlyAddedFields,
4571 const TSourceLoc &location)
4572{
4573 for (TField *field : *newlyAddedFields)
4574 {
4575 for (TField *oldField : *processedFields)
4576 {
4577 if (oldField->name() == field->name())
4578 {
4579 error(location, "duplicate field name in structure", field->name().c_str());
4580 }
4581 }
4582 processedFields->push_back(field);
4583 }
4584 return processedFields;
4585}
4586
Martin Radev70866b82016-07-22 15:27:42 +03004587TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4588 const TTypeQualifierBuilder &typeQualifierBuilder,
4589 TPublicType *typeSpecifier,
4590 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004591{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004592 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004593
Martin Radev70866b82016-07-22 15:27:42 +03004594 typeSpecifier->qualifier = typeQualifier.qualifier;
4595 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004596 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004597 typeSpecifier->invariant = typeQualifier.invariant;
4598 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304599 {
Martin Radev70866b82016-07-22 15:27:42 +03004600 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004601 }
Martin Radev70866b82016-07-22 15:27:42 +03004602 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004603}
4604
Jamie Madillb98c3a82015-07-23 14:26:04 -04004605TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004606 TFieldList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004607{
Martin Radev4a9cd802016-09-01 16:51:51 +03004608 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4609 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004610
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004611 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
4612 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004613
Martin Radev4a9cd802016-09-01 16:51:51 +03004614 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004615
Olli Etuaho55bde912017-10-25 13:41:13 +03004616 for (TField *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304617 {
Olli Etuaho55bde912017-10-25 13:41:13 +03004618 auto declaratorArraySizes = declarator->type()->getArraySizes();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004619 // don't allow arrays of arrays
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004620 if (!declaratorArraySizes.empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304621 {
Olli Etuahoe0803872017-08-23 15:30:23 +03004622 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004623 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004624
Olli Etuaho55bde912017-10-25 13:41:13 +03004625 TType *type = declarator->type();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004626 *type = TType(typeSpecifier);
4627 for (unsigned int arraySize : declaratorArraySizes)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304628 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004629 type->makeArray(arraySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004630 }
Olli Etuaho55bde912017-10-25 13:41:13 +03004631 checkIsNotUnsizedArray(typeSpecifier.getLine(),
4632 "array members of structs must specify a size",
4633 declarator->name().c_str(), type);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004634
Olli Etuaho55bde912017-10-25 13:41:13 +03004635 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *declarator);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004636 }
4637
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004638 return declaratorList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004639}
4640
Martin Radev4a9cd802016-09-01 16:51:51 +03004641TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4642 const TSourceLoc &nameLine,
4643 const TString *structName,
4644 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004645{
Olli Etuahoa5e693a2017-07-13 16:07:26 +03004646 TStructure *structure = new TStructure(&symbolTable, structName, fieldList);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004647
Jamie Madill9b820842015-02-12 10:40:10 -05004648 // Store a bool in the struct if we're at global scope, to allow us to
4649 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004650 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004651
Jamie Madill98493dd2013-07-08 14:39:03 -04004652 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004653 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004654 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004655 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304656 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004657 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004658 }
4659 }
4660
4661 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004662 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004663 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004664 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004665 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004666 switch (qualifier)
4667 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004668 case EvqGlobal:
4669 case EvqTemporary:
4670 break;
4671 default:
4672 error(field.line(), "invalid qualifier on struct member",
4673 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004674 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004675 }
Martin Radev70866b82016-07-22 15:27:42 +03004676 if (field.type()->isInvariant())
4677 {
4678 error(field.line(), "invalid qualifier on struct member", "invariant");
4679 }
jchen104cdac9e2017-05-08 11:01:20 +08004680 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4681 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004682 {
4683 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4684 }
4685
Olli Etuaho43364892017-02-13 16:00:12 +00004686 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4687
4688 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004689
4690 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004691 }
4692
Martin Radev4a9cd802016-09-01 16:51:51 +03004693 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004694 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004695 exitStructDeclaration();
4696
Martin Radev4a9cd802016-09-01 16:51:51 +03004697 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004698}
4699
Jamie Madillb98c3a82015-07-23 14:26:04 -04004700TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004701 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004702 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004703{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004704 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004705 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004706 init->isVector())
4707 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004708 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4709 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004710 return nullptr;
4711 }
4712
Olli Etuaho923ecef2017-10-11 12:01:38 +03004713 ASSERT(statementList);
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004714 if (!ValidateSwitchStatementList(switchType, mShaderVersion, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004715 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004716 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004717 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004718 }
4719
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004720 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4721 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004722 return node;
4723}
4724
4725TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4726{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004727 if (mSwitchNestingLevel == 0)
4728 {
4729 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004730 return nullptr;
4731 }
4732 if (condition == nullptr)
4733 {
4734 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004735 return nullptr;
4736 }
4737 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004738 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004739 {
4740 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004741 }
4742 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004743 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4744 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4745 // fold in case labels.
4746 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004747 {
4748 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004749 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004750 TIntermCase *node = new TIntermCase(condition);
4751 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004752 return node;
4753}
4754
4755TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4756{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004757 if (mSwitchNestingLevel == 0)
4758 {
4759 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004760 return nullptr;
4761 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004762 TIntermCase *node = new TIntermCase(nullptr);
4763 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004764 return node;
4765}
4766
Jamie Madillb98c3a82015-07-23 14:26:04 -04004767TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4768 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004769 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004770{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004771 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004772
4773 switch (op)
4774 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004775 case EOpLogicalNot:
4776 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4777 child->isVector())
4778 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004779 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004780 return nullptr;
4781 }
4782 break;
4783 case EOpBitwiseNot:
4784 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4785 child->isMatrix() || child->isArray())
4786 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004787 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004788 return nullptr;
4789 }
4790 break;
4791 case EOpPostIncrement:
4792 case EOpPreIncrement:
4793 case EOpPostDecrement:
4794 case EOpPreDecrement:
4795 case EOpNegative:
4796 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004797 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4798 child->getBasicType() == EbtBool || child->isArray() ||
4799 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004800 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004801 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004802 return nullptr;
4803 }
4804 // Operators for built-ins are already type checked against their prototype.
4805 default:
4806 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004807 }
4808
Jiajia Qinbc585152017-06-23 15:42:17 +08004809 if (child->getMemoryQualifier().writeonly)
4810 {
4811 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4812 return nullptr;
4813 }
4814
Olli Etuahof119a262016-08-19 15:54:22 +03004815 TIntermUnary *node = new TIntermUnary(op, child);
4816 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004817
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004818 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004819}
4820
Olli Etuaho09b22472015-02-11 11:47:26 +02004821TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4822{
Olli Etuahocce89652017-06-19 16:04:09 +03004823 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004824 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004825 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004826 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004827 return child;
4828 }
4829 return node;
4830}
4831
Jamie Madillb98c3a82015-07-23 14:26:04 -04004832TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4833 TIntermTyped *child,
4834 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004835{
Olli Etuaho856c4972016-08-08 11:38:39 +03004836 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004837 return addUnaryMath(op, child, loc);
4838}
4839
Jamie Madillb98c3a82015-07-23 14:26:04 -04004840bool TParseContext::binaryOpCommonCheck(TOperator op,
4841 TIntermTyped *left,
4842 TIntermTyped *right,
4843 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004844{
jchen10b4cf5652017-05-05 18:51:17 +08004845 // Check opaque types are not allowed to be operands in expressions other than array indexing
4846 // and structure member selection.
4847 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4848 {
4849 switch (op)
4850 {
4851 case EOpIndexDirect:
4852 case EOpIndexIndirect:
4853 break;
4854 case EOpIndexDirectStruct:
4855 UNREACHABLE();
4856
4857 default:
4858 error(loc, "Invalid operation for variables with an opaque type",
4859 GetOperatorString(op));
4860 return false;
4861 }
4862 }
jchen10cc2a10e2017-05-03 14:05:12 +08004863
Jiajia Qinbc585152017-06-23 15:42:17 +08004864 if (right->getMemoryQualifier().writeonly)
4865 {
4866 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4867 return false;
4868 }
4869
4870 if (left->getMemoryQualifier().writeonly)
4871 {
4872 switch (op)
4873 {
4874 case EOpAssign:
4875 case EOpInitialize:
4876 case EOpIndexDirect:
4877 case EOpIndexIndirect:
4878 case EOpIndexDirectStruct:
4879 case EOpIndexDirectInterfaceBlock:
4880 break;
4881 default:
4882 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4883 return false;
4884 }
4885 }
4886
Olli Etuaho244be012016-08-18 15:26:02 +03004887 if (left->getType().getStruct() || right->getType().getStruct())
4888 {
4889 switch (op)
4890 {
4891 case EOpIndexDirectStruct:
4892 ASSERT(left->getType().getStruct());
4893 break;
4894 case EOpEqual:
4895 case EOpNotEqual:
4896 case EOpAssign:
4897 case EOpInitialize:
4898 if (left->getType() != right->getType())
4899 {
4900 return false;
4901 }
4902 break;
4903 default:
4904 error(loc, "Invalid operation for structs", GetOperatorString(op));
4905 return false;
4906 }
4907 }
4908
Olli Etuaho94050052017-05-08 14:17:44 +03004909 if (left->isInterfaceBlock() || right->isInterfaceBlock())
4910 {
4911 switch (op)
4912 {
4913 case EOpIndexDirectInterfaceBlock:
4914 ASSERT(left->getType().getInterfaceBlock());
4915 break;
4916 default:
4917 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
4918 return false;
4919 }
4920 }
4921
Olli Etuahod6b14282015-03-17 14:31:35 +02004922 if (left->isArray() || right->isArray())
4923 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004924 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02004925 {
4926 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4927 return false;
4928 }
4929
4930 if (left->isArray() != right->isArray())
4931 {
4932 error(loc, "array / non-array mismatch", GetOperatorString(op));
4933 return false;
4934 }
4935
4936 switch (op)
4937 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004938 case EOpEqual:
4939 case EOpNotEqual:
4940 case EOpAssign:
4941 case EOpInitialize:
4942 break;
4943 default:
4944 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4945 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02004946 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03004947 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004948 if (left->getType().getArraySizes() != right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02004949 {
4950 error(loc, "array size mismatch", GetOperatorString(op));
4951 return false;
4952 }
Olli Etuahod6b14282015-03-17 14:31:35 +02004953 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004954
4955 // Check ops which require integer / ivec parameters
4956 bool isBitShift = false;
4957 switch (op)
4958 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004959 case EOpBitShiftLeft:
4960 case EOpBitShiftRight:
4961 case EOpBitShiftLeftAssign:
4962 case EOpBitShiftRightAssign:
4963 // Unsigned can be bit-shifted by signed and vice versa, but we need to
4964 // check that the basic type is an integer type.
4965 isBitShift = true;
4966 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
4967 {
4968 return false;
4969 }
4970 break;
4971 case EOpBitwiseAnd:
4972 case EOpBitwiseXor:
4973 case EOpBitwiseOr:
4974 case EOpBitwiseAndAssign:
4975 case EOpBitwiseXorAssign:
4976 case EOpBitwiseOrAssign:
4977 // It is enough to check the type of only one operand, since later it
4978 // is checked that the operand types match.
4979 if (!IsInteger(left->getBasicType()))
4980 {
4981 return false;
4982 }
4983 break;
4984 default:
4985 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004986 }
4987
4988 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
4989 // So the basic type should usually match.
4990 if (!isBitShift && left->getBasicType() != right->getBasicType())
4991 {
4992 return false;
4993 }
4994
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004995 // Check that:
4996 // 1. Type sizes match exactly on ops that require that.
4997 // 2. Restrictions for structs that contain arrays or samplers are respected.
4998 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04004999 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005000 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005001 case EOpAssign:
5002 case EOpInitialize:
5003 case EOpEqual:
5004 case EOpNotEqual:
5005 // ESSL 1.00 sections 5.7, 5.8, 5.9
5006 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5007 {
5008 error(loc, "undefined operation for structs containing arrays",
5009 GetOperatorString(op));
5010 return false;
5011 }
5012 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5013 // we interpret the spec so that this extends to structs containing samplers,
5014 // similarly to ESSL 1.00 spec.
5015 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5016 left->getType().isStructureContainingSamplers())
5017 {
5018 error(loc, "undefined operation for structs containing samplers",
5019 GetOperatorString(op));
5020 return false;
5021 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005022
Olli Etuahoe1805592017-01-02 16:41:20 +00005023 if ((left->getNominalSize() != right->getNominalSize()) ||
5024 (left->getSecondarySize() != right->getSecondarySize()))
5025 {
5026 error(loc, "dimension mismatch", GetOperatorString(op));
5027 return false;
5028 }
5029 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005030 case EOpLessThan:
5031 case EOpGreaterThan:
5032 case EOpLessThanEqual:
5033 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005034 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005035 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005036 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005037 return false;
5038 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005039 break;
5040 case EOpAdd:
5041 case EOpSub:
5042 case EOpDiv:
5043 case EOpIMod:
5044 case EOpBitShiftLeft:
5045 case EOpBitShiftRight:
5046 case EOpBitwiseAnd:
5047 case EOpBitwiseXor:
5048 case EOpBitwiseOr:
5049 case EOpAddAssign:
5050 case EOpSubAssign:
5051 case EOpDivAssign:
5052 case EOpIModAssign:
5053 case EOpBitShiftLeftAssign:
5054 case EOpBitShiftRightAssign:
5055 case EOpBitwiseAndAssign:
5056 case EOpBitwiseXorAssign:
5057 case EOpBitwiseOrAssign:
5058 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5059 {
5060 return false;
5061 }
5062
5063 // Are the sizes compatible?
5064 if (left->getNominalSize() != right->getNominalSize() ||
5065 left->getSecondarySize() != right->getSecondarySize())
5066 {
5067 // If the nominal sizes of operands do not match:
5068 // One of them must be a scalar.
5069 if (!left->isScalar() && !right->isScalar())
5070 return false;
5071
5072 // In the case of compound assignment other than multiply-assign,
5073 // the right side needs to be a scalar. Otherwise a vector/matrix
5074 // would be assigned to a scalar. A scalar can't be shifted by a
5075 // vector either.
5076 if (!right->isScalar() &&
5077 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5078 return false;
5079 }
5080 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005081 default:
5082 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005083 }
5084
Olli Etuahod6b14282015-03-17 14:31:35 +02005085 return true;
5086}
5087
Olli Etuaho1dded802016-08-18 18:13:13 +03005088bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5089 const TType &left,
5090 const TType &right)
5091{
5092 switch (op)
5093 {
5094 case EOpMul:
5095 case EOpMulAssign:
5096 return left.getNominalSize() == right.getNominalSize() &&
5097 left.getSecondarySize() == right.getSecondarySize();
5098 case EOpVectorTimesScalar:
5099 return true;
5100 case EOpVectorTimesScalarAssign:
5101 ASSERT(!left.isMatrix() && !right.isMatrix());
5102 return left.isVector() && !right.isVector();
5103 case EOpVectorTimesMatrix:
5104 return left.getNominalSize() == right.getRows();
5105 case EOpVectorTimesMatrixAssign:
5106 ASSERT(!left.isMatrix() && right.isMatrix());
5107 return left.isVector() && left.getNominalSize() == right.getRows() &&
5108 left.getNominalSize() == right.getCols();
5109 case EOpMatrixTimesVector:
5110 return left.getCols() == right.getNominalSize();
5111 case EOpMatrixTimesScalar:
5112 return true;
5113 case EOpMatrixTimesScalarAssign:
5114 ASSERT(left.isMatrix() && !right.isMatrix());
5115 return !right.isVector();
5116 case EOpMatrixTimesMatrix:
5117 return left.getCols() == right.getRows();
5118 case EOpMatrixTimesMatrixAssign:
5119 ASSERT(left.isMatrix() && right.isMatrix());
5120 // We need to check two things:
5121 // 1. The matrix multiplication step is valid.
5122 // 2. The result will have the same number of columns as the lvalue.
5123 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5124
5125 default:
5126 UNREACHABLE();
5127 return false;
5128 }
5129}
5130
Jamie Madillb98c3a82015-07-23 14:26:04 -04005131TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5132 TIntermTyped *left,
5133 TIntermTyped *right,
5134 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005135{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005136 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005137 return nullptr;
5138
Olli Etuahofc1806e2015-03-17 13:03:11 +02005139 switch (op)
5140 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005141 case EOpEqual:
5142 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005143 case EOpLessThan:
5144 case EOpGreaterThan:
5145 case EOpLessThanEqual:
5146 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005147 break;
5148 case EOpLogicalOr:
5149 case EOpLogicalXor:
5150 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005151 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5152 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005153 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005154 {
5155 return nullptr;
5156 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005157 // Basic types matching should have been already checked.
5158 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005159 break;
5160 case EOpAdd:
5161 case EOpSub:
5162 case EOpDiv:
5163 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005164 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5165 !right->getType().getStruct());
5166 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005167 {
5168 return nullptr;
5169 }
5170 break;
5171 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005172 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5173 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005174 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005175 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005176 {
5177 return nullptr;
5178 }
5179 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005180 default:
5181 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005182 }
5183
Olli Etuaho1dded802016-08-18 18:13:13 +03005184 if (op == EOpMul)
5185 {
5186 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5187 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5188 {
5189 return nullptr;
5190 }
5191 }
5192
Olli Etuaho3fdec912016-08-18 15:08:06 +03005193 TIntermBinary *node = new TIntermBinary(op, left, right);
5194 node->setLine(loc);
5195
Olli Etuaho3fdec912016-08-18 15:08:06 +03005196 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005197 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005198}
5199
Jamie Madillb98c3a82015-07-23 14:26:04 -04005200TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5201 TIntermTyped *left,
5202 TIntermTyped *right,
5203 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005204{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005205 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005206 if (node == 0)
5207 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005208 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5209 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005210 return left;
5211 }
5212 return node;
5213}
5214
Jamie Madillb98c3a82015-07-23 14:26:04 -04005215TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5216 TIntermTyped *left,
5217 TIntermTyped *right,
5218 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005219{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005220 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005221 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005222 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005223 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5224 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005225 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005226 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005227 }
5228 return node;
5229}
5230
Olli Etuaho13389b62016-10-16 11:48:18 +01005231TIntermBinary *TParseContext::createAssign(TOperator op,
5232 TIntermTyped *left,
5233 TIntermTyped *right,
5234 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005235{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005236 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005237 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005238 if (op == EOpMulAssign)
5239 {
5240 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5241 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5242 {
5243 return nullptr;
5244 }
5245 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005246 TIntermBinary *node = new TIntermBinary(op, left, right);
5247 node->setLine(loc);
5248
Olli Etuaho3fdec912016-08-18 15:08:06 +03005249 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005250 }
5251 return nullptr;
5252}
5253
Jamie Madillb98c3a82015-07-23 14:26:04 -04005254TIntermTyped *TParseContext::addAssign(TOperator op,
5255 TIntermTyped *left,
5256 TIntermTyped *right,
5257 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005258{
Olli Etuahocce89652017-06-19 16:04:09 +03005259 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005260 TIntermTyped *node = createAssign(op, left, right, loc);
5261 if (node == nullptr)
5262 {
5263 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005264 return left;
5265 }
5266 return node;
5267}
5268
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005269TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5270 TIntermTyped *right,
5271 const TSourceLoc &loc)
5272{
Corentin Wallez0d959252016-07-12 17:26:32 -04005273 // WebGL2 section 5.26, the following results in an error:
5274 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005275 if (mShaderSpec == SH_WEBGL2_SPEC &&
5276 (left->isArray() || left->getBasicType() == EbtVoid ||
5277 left->getType().isStructureContainingArrays() || right->isArray() ||
5278 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005279 {
5280 error(loc,
5281 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5282 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005283 }
5284
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005285 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5286 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5287 commaNode->getTypePointer()->setQualifier(resultQualifier);
5288 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005289}
5290
Olli Etuaho49300862015-02-20 14:54:49 +02005291TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5292{
5293 switch (op)
5294 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005295 case EOpContinue:
5296 if (mLoopNestingLevel <= 0)
5297 {
5298 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005299 }
5300 break;
5301 case EOpBreak:
5302 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5303 {
5304 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005305 }
5306 break;
5307 case EOpReturn:
5308 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5309 {
5310 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005311 }
5312 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005313 case EOpKill:
5314 if (mShaderType != GL_FRAGMENT_SHADER)
5315 {
5316 error(loc, "discard supported in fragment shaders only", "discard");
5317 }
5318 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005319 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005320 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005321 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005322 }
Olli Etuahocce89652017-06-19 16:04:09 +03005323 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005324}
5325
Jamie Madillb98c3a82015-07-23 14:26:04 -04005326TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005327 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005328 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005329{
Olli Etuahocce89652017-06-19 16:04:09 +03005330 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005331 {
Olli Etuahocce89652017-06-19 16:04:09 +03005332 ASSERT(op == EOpReturn);
5333 mFunctionReturnsValue = true;
5334 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5335 {
5336 error(loc, "void function cannot return a value", "return");
5337 }
5338 else if (*mCurrentFunctionType != expression->getType())
5339 {
5340 error(loc, "function return is not matching type:", "return");
5341 }
Olli Etuaho49300862015-02-20 14:54:49 +02005342 }
Olli Etuahocce89652017-06-19 16:04:09 +03005343 TIntermBranch *node = new TIntermBranch(op, expression);
5344 node->setLine(loc);
5345 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005346}
5347
Martin Radev84aa2dc2017-09-11 15:51:02 +03005348void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5349{
5350 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
5351 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5352 bool isTextureGather = (name == "textureGather");
5353 bool isTextureGatherOffset = (name == "textureGatherOffset");
5354 if (isTextureGather || isTextureGatherOffset)
5355 {
5356 TIntermNode *componentNode = nullptr;
5357 TIntermSequence *arguments = functionCall->getSequence();
5358 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5359 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5360 ASSERT(sampler != nullptr);
5361 switch (sampler->getBasicType())
5362 {
5363 case EbtSampler2D:
5364 case EbtISampler2D:
5365 case EbtUSampler2D:
5366 case EbtSampler2DArray:
5367 case EbtISampler2DArray:
5368 case EbtUSampler2DArray:
5369 if ((isTextureGather && arguments->size() == 3u) ||
5370 (isTextureGatherOffset && arguments->size() == 4u))
5371 {
5372 componentNode = arguments->back();
5373 }
5374 break;
5375 case EbtSamplerCube:
5376 case EbtISamplerCube:
5377 case EbtUSamplerCube:
5378 ASSERT(!isTextureGatherOffset);
5379 if (arguments->size() == 3u)
5380 {
5381 componentNode = arguments->back();
5382 }
5383 break;
5384 case EbtSampler2DShadow:
5385 case EbtSampler2DArrayShadow:
5386 case EbtSamplerCubeShadow:
5387 break;
5388 default:
5389 UNREACHABLE();
5390 break;
5391 }
5392 if (componentNode)
5393 {
5394 const TIntermConstantUnion *componentConstantUnion =
5395 componentNode->getAsConstantUnion();
5396 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5397 {
5398 error(functionCall->getLine(), "Texture component must be a constant expression",
5399 name.c_str());
5400 }
5401 else
5402 {
5403 int component = componentConstantUnion->getIConst(0);
5404 if (component < 0 || component > 3)
5405 {
5406 error(functionCall->getLine(), "Component must be in the range [0;3]",
5407 name.c_str());
5408 }
5409 }
5410 }
5411 }
5412}
5413
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005414void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5415{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005416 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01005417 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005418 TIntermNode *offset = nullptr;
5419 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005420 bool useTextureGatherOffsetConstraints = false;
Olli Etuahoec9232b2017-03-27 17:01:37 +03005421 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5422 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5423 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005424 {
5425 offset = arguments->back();
5426 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005427 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005428 {
5429 // A bias parameter might follow the offset parameter.
5430 ASSERT(arguments->size() >= 3);
5431 offset = (*arguments)[2];
5432 }
Martin Radev84aa2dc2017-09-11 15:51:02 +03005433 else if (name == "textureGatherOffset")
5434 {
5435 ASSERT(arguments->size() >= 3u);
5436 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5437 ASSERT(sampler != nullptr);
5438 switch (sampler->getBasicType())
5439 {
5440 case EbtSampler2D:
5441 case EbtISampler2D:
5442 case EbtUSampler2D:
5443 case EbtSampler2DArray:
5444 case EbtISampler2DArray:
5445 case EbtUSampler2DArray:
5446 offset = (*arguments)[2];
5447 break;
5448 case EbtSampler2DShadow:
5449 case EbtSampler2DArrayShadow:
5450 offset = (*arguments)[3];
5451 break;
5452 default:
5453 UNREACHABLE();
5454 break;
5455 }
5456 useTextureGatherOffsetConstraints = true;
5457 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005458 if (offset != nullptr)
5459 {
5460 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5461 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5462 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005463 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005464 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005465 }
5466 else
5467 {
5468 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5469 size_t size = offsetConstantUnion->getType().getObjectSize();
5470 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005471 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5472 : mMinProgramTexelOffset;
5473 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5474 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005475 for (size_t i = 0u; i < size; ++i)
5476 {
5477 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005478 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005479 {
5480 std::stringstream tokenStream;
5481 tokenStream << offsetValue;
5482 std::string token = tokenStream.str();
5483 error(offset->getLine(), "Texture offset value out of valid range",
5484 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005485 }
5486 }
5487 }
5488 }
5489}
5490
Martin Radev2cc85b32016-08-05 16:22:53 +03005491// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5492void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5493{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005494 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005495 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5496
5497 if (name.compare(0, 5, "image") == 0)
5498 {
5499 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005500 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005501
Olli Etuaho485eefd2017-02-14 17:40:06 +00005502 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005503
5504 if (name.compare(5, 5, "Store") == 0)
5505 {
5506 if (memoryQualifier.readonly)
5507 {
5508 error(imageNode->getLine(),
5509 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005510 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005511 }
5512 }
5513 else if (name.compare(5, 4, "Load") == 0)
5514 {
5515 if (memoryQualifier.writeonly)
5516 {
5517 error(imageNode->getLine(),
5518 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005519 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005520 }
5521 }
5522 }
5523}
5524
5525// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5526void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5527 const TFunction *functionDefinition,
5528 const TIntermAggregate *functionCall)
5529{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005530 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005531
5532 const TIntermSequence &arguments = *functionCall->getSequence();
5533
5534 ASSERT(functionDefinition->getParamCount() == arguments.size());
5535
5536 for (size_t i = 0; i < arguments.size(); ++i)
5537 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005538 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5539 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005540 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5541 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5542
5543 if (IsImage(functionArgumentType.getBasicType()))
5544 {
5545 const TMemoryQualifier &functionArgumentMemoryQualifier =
5546 functionArgumentType.getMemoryQualifier();
5547 const TMemoryQualifier &functionParameterMemoryQualifier =
5548 functionParameterType.getMemoryQualifier();
5549 if (functionArgumentMemoryQualifier.readonly &&
5550 !functionParameterMemoryQualifier.readonly)
5551 {
5552 error(functionCall->getLine(),
5553 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005554 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005555 }
5556
5557 if (functionArgumentMemoryQualifier.writeonly &&
5558 !functionParameterMemoryQualifier.writeonly)
5559 {
5560 error(functionCall->getLine(),
5561 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005562 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005563 }
Martin Radev049edfa2016-11-11 14:35:37 +02005564
5565 if (functionArgumentMemoryQualifier.coherent &&
5566 !functionParameterMemoryQualifier.coherent)
5567 {
5568 error(functionCall->getLine(),
5569 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005570 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005571 }
5572
5573 if (functionArgumentMemoryQualifier.volatileQualifier &&
5574 !functionParameterMemoryQualifier.volatileQualifier)
5575 {
5576 error(functionCall->getLine(),
5577 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005578 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005579 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005580 }
5581 }
5582}
5583
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005584TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005585{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005586 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005587}
5588
5589TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005590 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005591 TIntermNode *thisNode,
5592 const TSourceLoc &loc)
5593{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005594 if (thisNode != nullptr)
5595 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005596 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005597 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005598
5599 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005600 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005601 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005602 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005603 }
5604 else
5605 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005606 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005607 return addNonConstructorFunctionCall(fnCall, arguments, loc);
5608 }
5609}
5610
5611TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
5612 TIntermSequence *arguments,
5613 TIntermNode *thisNode,
5614 const TSourceLoc &loc)
5615{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005616 TIntermTyped *typedThis = thisNode->getAsTyped();
5617 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5618 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5619 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
5620 // So accessing fnCall->getName() below is safe.
5621 if (fnCall->getName() != "length")
5622 {
5623 error(loc, "invalid method", fnCall->getName().c_str());
5624 }
5625 else if (!arguments->empty())
5626 {
5627 error(loc, "method takes no parameters", "length");
5628 }
5629 else if (typedThis == nullptr || !typedThis->isArray())
5630 {
5631 error(loc, "length can only be called on arrays", "length");
5632 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005633 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5634 mGeometryShaderInputPrimitiveType == EptUndefined)
5635 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08005636 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005637 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5638 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005639 else
5640 {
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005641 TIntermUnary *node = new TIntermUnary(EOpArrayLength, typedThis);
5642 node->setLine(loc);
5643 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005644 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005645 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005646}
5647
5648TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
5649 TIntermSequence *arguments,
5650 const TSourceLoc &loc)
5651{
5652 // First find by unmangled name to check whether the function name has been
5653 // hidden by a variable name or struct typename.
5654 // If a function is found, check for one with a matching argument list.
5655 bool builtIn;
5656 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
5657 if (symbol != nullptr && !symbol->isFunction())
5658 {
5659 error(loc, "function name expected", fnCall->getName().c_str());
5660 }
5661 else
5662 {
5663 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
5664 mShaderVersion, &builtIn);
5665 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005666 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005667 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
5668 }
5669 else
5670 {
5671 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005672 //
5673 // A declared function.
5674 //
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03005675 if (builtIn && fnCandidate->getExtension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005676 {
Olli Etuaho856c4972016-08-08 11:38:39 +03005677 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005678 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005679 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005680 if (builtIn && op != EOpNull)
5681 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005682 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005683 if (fnCandidate->getParamCount() == 1)
5684 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005685 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005686 TIntermNode *unaryParamNode = arguments->front();
5687 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005688 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005689 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005690 }
5691 else
5692 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005693 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005694 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005695 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005696
5697 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005698 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305699
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005700 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305701 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005702 // See if we can constant fold a built-in. Note that this may be possible
5703 // even if it is not const-qualified.
5704 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305705 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005706 else
5707 {
5708 return callNode;
5709 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005710 }
5711 }
5712 else
5713 {
5714 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005715 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005716
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005717 // If builtIn == false, the function is user defined - could be an overloaded
5718 // built-in as well.
5719 // if builtIn == true, it's a builtIn function with no op associated with it.
5720 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005721 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005722 {
Olli Etuahofe486322017-03-21 09:30:54 +00005723 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005724 checkTextureOffsetConst(callNode);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005725 checkTextureGather(callNode);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005726 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005727 }
5728 else
5729 {
Olli Etuahofe486322017-03-21 09:30:54 +00005730 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005731 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005732 }
5733
Jiajia Qinbc585152017-06-23 15:42:17 +08005734 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005735
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005736 callNode->setLine(loc);
5737
5738 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005739 }
5740 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005741 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005742
5743 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005744 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005745}
5746
Jamie Madillb98c3a82015-07-23 14:26:04 -04005747TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005748 TIntermTyped *trueExpression,
5749 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005750 const TSourceLoc &loc)
5751{
Olli Etuaho56229f12017-07-10 14:16:33 +03005752 if (!checkIsScalarBool(loc, cond))
5753 {
5754 return falseExpression;
5755 }
Olli Etuaho52901742015-04-15 13:42:45 +03005756
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005757 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005758 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005759 std::stringstream reasonStream;
5760 reasonStream << "mismatching ternary operator operand types '"
5761 << trueExpression->getCompleteString() << " and '"
5762 << falseExpression->getCompleteString() << "'";
5763 std::string reason = reasonStream.str();
5764 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005765 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005766 }
Olli Etuahode318b22016-10-25 16:18:25 +01005767 if (IsOpaqueType(trueExpression->getBasicType()))
5768 {
5769 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005770 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005771 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5772 // Note that structs containing opaque types don't need to be checked as structs are
5773 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005774 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005775 return falseExpression;
5776 }
5777
Jiajia Qinbc585152017-06-23 15:42:17 +08005778 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5779 falseExpression->getMemoryQualifier().writeonly)
5780 {
5781 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5782 return falseExpression;
5783 }
5784
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005785 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005786 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005787 // ESSL 3.00.6 section 5.7:
5788 // Ternary operator support is optional for arrays. No certainty that it works across all
5789 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5790 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005791 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005792 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005793 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005794 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005795 }
Olli Etuaho94050052017-05-08 14:17:44 +03005796 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5797 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005798 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005799 return falseExpression;
5800 }
5801
Corentin Wallez0d959252016-07-12 17:26:32 -04005802 // WebGL2 section 5.26, the following results in an error:
5803 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005804 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005805 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005806 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005807 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005808 }
5809
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005810 // Note that the node resulting from here can be a constant union without being qualified as
5811 // constant.
5812 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5813 node->setLine(loc);
5814
5815 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005816}
Olli Etuaho49300862015-02-20 14:54:49 +02005817
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005818//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005819// Parse an array of strings using yyparse.
5820//
5821// Returns 0 for success.
5822//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005823int PaParseStrings(size_t count,
5824 const char *const string[],
5825 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305826 TParseContext *context)
5827{
Yunchao He4f285442017-04-21 12:15:49 +08005828 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005829 return 1;
5830
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005831 if (glslang_initialize(context))
5832 return 1;
5833
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005834 int error = glslang_scan(count, string, length, context);
5835 if (!error)
5836 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005837
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005838 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005839
alokp@chromium.org6b495712012-06-29 00:06:58 +00005840 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005841}
Jamie Madill45bcc782016-11-07 13:58:48 -05005842
5843} // namespace sh