blob: 6b4026220ddf9e4df792876162a0f0037b6ae4bf [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Jamie Madill6b9cb252013-10-17 10:45:47 -04007#include "compiler/translator/ParseContext.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00008
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009#include <stdarg.h>
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000010#include <stdio.h>
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011
jchen104cdac9e2017-05-08 11:01:20 +080012#include "common/mathutil.h"
daniel@transgaming.comb401a922012-10-26 18:58:24 +000013#include "compiler/preprocessor/SourceLocation.h"
Dmitry Skiba01971112015-07-10 14:54:00 -040014#include "compiler/translator/Cache.h"
Olli Etuaho3ec75682017-07-05 17:02:55 +030015#include "compiler/translator/IntermNode_util.h"
Olli Etuahob0c645e2015-05-12 14:25:36 +030016#include "compiler/translator/ValidateGlobalInitializer.h"
jchen104cdac9e2017-05-08 11:01:20 +080017#include "compiler/translator/ValidateSwitch.h"
18#include "compiler/translator/glslang.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030019#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000020
Jamie Madill45bcc782016-11-07 13:58:48 -050021namespace sh
22{
23
alokp@chromium.org8b851c62012-06-15 16:25:11 +000024///////////////////////////////////////////////////////////////////////
25//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000026// Sub- vector and matrix fields
27//
28////////////////////////////////////////////////////////////////////////
29
Martin Radev2cc85b32016-08-05 16:22:53 +030030namespace
31{
32
33const int kWebGLMaxStructNesting = 4;
34
Olli Etuaho0f684632017-07-13 12:42:15 +030035bool ContainsSampler(const TStructure *structType);
36
Martin Radev2cc85b32016-08-05 16:22:53 +030037bool ContainsSampler(const TType &type)
38{
39 if (IsSampler(type.getBasicType()))
Olli Etuaho0f684632017-07-13 12:42:15 +030040 {
Martin Radev2cc85b32016-08-05 16:22:53 +030041 return true;
Olli Etuaho0f684632017-07-13 12:42:15 +030042 }
jchen10cc2a10e2017-05-03 14:05:12 +080043 if (type.getBasicType() == EbtStruct)
Martin Radev2cc85b32016-08-05 16:22:53 +030044 {
Olli Etuaho0f684632017-07-13 12:42:15 +030045 return ContainsSampler(type.getStruct());
Martin Radev2cc85b32016-08-05 16:22:53 +030046 }
47
48 return false;
49}
50
Olli Etuaho0f684632017-07-13 12:42:15 +030051bool ContainsSampler(const TStructure *structType)
52{
53 for (const auto &field : structType->fields())
54 {
55 if (ContainsSampler(*field->type()))
56 return true;
57 }
58 return false;
59}
60
Olli Etuaho485eefd2017-02-14 17:40:06 +000061// Get a token from an image argument to use as an error message token.
62const char *GetImageArgumentToken(TIntermTyped *imageNode)
63{
64 ASSERT(IsImage(imageNode->getBasicType()));
65 while (imageNode->getAsBinaryNode() &&
66 (imageNode->getAsBinaryNode()->getOp() == EOpIndexIndirect ||
67 imageNode->getAsBinaryNode()->getOp() == EOpIndexDirect))
68 {
69 imageNode = imageNode->getAsBinaryNode()->getLeft();
70 }
71 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
72 if (imageSymbol)
73 {
74 return imageSymbol->getSymbol().c_str();
75 }
76 return "image";
77}
78
Olli Etuahocce89652017-06-19 16:04:09 +030079bool CanSetDefaultPrecisionOnType(const TPublicType &type)
80{
81 if (!SupportsPrecision(type.getBasicType()))
82 {
83 return false;
84 }
85 if (type.getBasicType() == EbtUInt)
86 {
87 // ESSL 3.00.4 section 4.5.4
88 return false;
89 }
90 if (type.isAggregate())
91 {
92 // Not allowed to set for aggregate types
93 return false;
94 }
95 return true;
96}
97
Jiawei Shaod8105a02017-08-08 09:54:36 +080098// Map input primitive types to input array sizes in a geometry shader.
99GLuint GetGeometryShaderInputArraySize(TLayoutPrimitiveType primitiveType)
100{
101 switch (primitiveType)
102 {
103 case EptPoints:
104 return 1u;
105 case EptLines:
106 return 2u;
107 case EptTriangles:
108 return 3u;
109 case EptLinesAdjacency:
110 return 4u;
111 case EptTrianglesAdjacency:
112 return 6u;
113 default:
114 UNREACHABLE();
115 return 0u;
116 }
117}
118
Martin Radev2cc85b32016-08-05 16:22:53 +0300119} // namespace
120
jchen104cdac9e2017-05-08 11:01:20 +0800121// This tracks each binding point's current default offset for inheritance of subsequent
122// variables using the same binding, and keeps offsets unique and non overlapping.
123// See GLSL ES 3.1, section 4.4.6.
124class TParseContext::AtomicCounterBindingState
125{
126 public:
127 AtomicCounterBindingState() : mDefaultOffset(0) {}
128 // Inserts a new span and returns -1 if overlapping, else returns the starting offset of
129 // newly inserted span.
130 int insertSpan(int start, size_t length)
131 {
132 gl::RangeI newSpan(start, start + static_cast<int>(length));
133 for (const auto &span : mSpans)
134 {
135 if (newSpan.intersects(span))
136 {
137 return -1;
138 }
139 }
140 mSpans.push_back(newSpan);
141 mDefaultOffset = newSpan.high();
142 return start;
143 }
144 // Inserts a new span starting from the default offset.
145 int appendSpan(size_t length) { return insertSpan(mDefaultOffset, length); }
146 void setDefaultOffset(int offset) { mDefaultOffset = offset; }
147
148 private:
149 int mDefaultOffset;
150 std::vector<gl::RangeI> mSpans;
151};
152
Jamie Madillacb4b812016-11-07 13:50:29 -0500153TParseContext::TParseContext(TSymbolTable &symt,
154 TExtensionBehavior &ext,
155 sh::GLenum type,
156 ShShaderSpec spec,
157 ShCompileOptions options,
158 bool checksPrecErrors,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000159 TDiagnostics *diagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500160 const ShBuiltInResources &resources)
Olli Etuaho56229f12017-07-10 14:16:33 +0300161 : symbolTable(symt),
Olli Etuahobb7e5a72017-04-24 10:16:44 +0300162 mDeferredNonEmptyDeclarationErrorCheck(false),
Jamie Madillacb4b812016-11-07 13:50:29 -0500163 mShaderType(type),
164 mShaderSpec(spec),
165 mCompileOptions(options),
166 mShaderVersion(100),
167 mTreeRoot(nullptr),
168 mLoopNestingLevel(0),
169 mStructNestingLevel(0),
170 mSwitchNestingLevel(0),
171 mCurrentFunctionType(nullptr),
172 mFunctionReturnsValue(false),
173 mChecksPrecisionErrors(checksPrecErrors),
174 mFragmentPrecisionHighOnESSL1(false),
Jiajia Qinbc585152017-06-23 15:42:17 +0800175 mDefaultUniformMatrixPacking(EmpColumnMajor),
176 mDefaultUniformBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
177 mDefaultBufferMatrixPacking(EmpColumnMajor),
178 mDefaultBufferBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000179 mDiagnostics(diagnostics),
Jamie Madillacb4b812016-11-07 13:50:29 -0500180 mDirectiveHandler(ext,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000181 *mDiagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500182 mShaderVersion,
183 mShaderType,
184 resources.WEBGL_debug_shader_precision == 1),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000185 mPreprocessor(mDiagnostics, &mDirectiveHandler, pp::PreprocessorSettings()),
Jamie Madillacb4b812016-11-07 13:50:29 -0500186 mScanner(nullptr),
187 mUsesFragData(false),
188 mUsesFragColor(false),
189 mUsesSecondaryOutputs(false),
190 mMinProgramTexelOffset(resources.MinProgramTexelOffset),
191 mMaxProgramTexelOffset(resources.MaxProgramTexelOffset),
Martin Radev84aa2dc2017-09-11 15:51:02 +0300192 mMinProgramTextureGatherOffset(resources.MinProgramTextureGatherOffset),
193 mMaxProgramTextureGatherOffset(resources.MaxProgramTextureGatherOffset),
Jamie Madillacb4b812016-11-07 13:50:29 -0500194 mComputeShaderLocalSizeDeclared(false),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000195 mNumViews(-1),
196 mMaxNumViews(resources.MaxViewsOVR),
Olli Etuaho43364892017-02-13 16:00:12 +0000197 mMaxImageUnits(resources.MaxImageUnits),
198 mMaxCombinedTextureImageUnits(resources.MaxCombinedTextureImageUnits),
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000199 mMaxUniformLocations(resources.MaxUniformLocations),
jchen10af713a22017-04-19 09:10:56 +0800200 mMaxUniformBufferBindings(resources.MaxUniformBufferBindings),
jchen104cdac9e2017-05-08 11:01:20 +0800201 mMaxAtomicCounterBindings(resources.MaxAtomicCounterBindings),
Jiajia Qinbc585152017-06-23 15:42:17 +0800202 mMaxShaderStorageBufferBindings(resources.MaxShaderStorageBufferBindings),
Shaob5cc1192017-07-06 10:47:20 +0800203 mDeclaringFunction(false),
204 mGeometryShaderInputPrimitiveType(EptUndefined),
205 mGeometryShaderOutputPrimitiveType(EptUndefined),
206 mGeometryShaderInvocations(0),
207 mGeometryShaderMaxVertices(-1),
208 mMaxGeometryShaderInvocations(resources.MaxGeometryShaderInvocations),
Jiawei Shaod8105a02017-08-08 09:54:36 +0800209 mMaxGeometryShaderMaxVertices(resources.MaxGeometryOutputVertices),
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800210 mGeometryShaderInputArraySize(0u)
Jamie Madillacb4b812016-11-07 13:50:29 -0500211{
212 mComputeShaderLocalSize.fill(-1);
213}
214
jchen104cdac9e2017-05-08 11:01:20 +0800215TParseContext::~TParseContext()
216{
217}
218
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300219bool TParseContext::parseVectorFields(const TSourceLoc &line,
220 const TString &compString,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400221 int vecSize,
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300222 TVector<int> *fieldOffsets)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000223{
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300224 ASSERT(fieldOffsets);
225 size_t fieldCount = compString.size();
226 if (fieldCount > 4u)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530227 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000228 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000229 return false;
230 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300231 fieldOffsets->resize(fieldCount);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000232
Jamie Madillb98c3a82015-07-23 14:26:04 -0400233 enum
234 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000235 exyzw,
236 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +0000237 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000238 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000239
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300240 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530241 {
242 switch (compString[i])
243 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400244 case 'x':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300245 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400246 fieldSet[i] = exyzw;
247 break;
248 case 'r':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300249 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400250 fieldSet[i] = ergba;
251 break;
252 case 's':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300253 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400254 fieldSet[i] = estpq;
255 break;
256 case 'y':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300257 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400258 fieldSet[i] = exyzw;
259 break;
260 case 'g':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300261 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400262 fieldSet[i] = ergba;
263 break;
264 case 't':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300265 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400266 fieldSet[i] = estpq;
267 break;
268 case 'z':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300269 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400270 fieldSet[i] = exyzw;
271 break;
272 case 'b':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300273 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400274 fieldSet[i] = ergba;
275 break;
276 case 'p':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300277 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400278 fieldSet[i] = estpq;
279 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530280
Jamie Madillb98c3a82015-07-23 14:26:04 -0400281 case 'w':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300282 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400283 fieldSet[i] = exyzw;
284 break;
285 case 'a':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300286 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400287 fieldSet[i] = ergba;
288 break;
289 case 'q':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300290 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400291 fieldSet[i] = estpq;
292 break;
293 default:
294 error(line, "illegal vector field selection", compString.c_str());
295 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000296 }
297 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000298
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300299 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530300 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300301 if ((*fieldOffsets)[i] >= vecSize)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530302 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400303 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000304 return false;
305 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000306
Arun Patole7e7e68d2015-05-22 12:02:25 +0530307 if (i > 0)
308 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400309 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530310 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400311 error(line, "illegal - vector component fields not from the same set",
312 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000313 return false;
314 }
315 }
316 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000317
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000318 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000319}
320
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000321///////////////////////////////////////////////////////////////////////
322//
323// Errors
324//
325////////////////////////////////////////////////////////////////////////
326
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000327//
328// Used by flex/bison to output all syntax and parsing errors.
329//
Olli Etuaho4de340a2016-12-16 09:32:03 +0000330void TParseContext::error(const TSourceLoc &loc, const char *reason, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000331{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000332 mDiagnostics->error(loc, reason, token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000333}
334
Olli Etuaho4de340a2016-12-16 09:32:03 +0000335void TParseContext::warning(const TSourceLoc &loc, const char *reason, const char *token)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530336{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000337 mDiagnostics->warning(loc, reason, token);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000338}
339
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200340void TParseContext::outOfRangeError(bool isError,
341 const TSourceLoc &loc,
342 const char *reason,
Olli Etuaho4de340a2016-12-16 09:32:03 +0000343 const char *token)
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200344{
345 if (isError)
346 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000347 error(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200348 }
349 else
350 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000351 warning(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200352 }
353}
354
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000355//
356// Same error message for all places assignments don't work.
357//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530358void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000359{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000360 std::stringstream reasonStream;
361 reasonStream << "cannot convert from '" << right << "' to '" << left << "'";
362 std::string reason = reasonStream.str();
363 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000364}
365
366//
367// Same error message for all places unary operations don't work.
368//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530369void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000370{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000371 std::stringstream reasonStream;
372 reasonStream << "wrong operand type - no operation '" << op
373 << "' exists that takes an operand of type " << operand
374 << " (or there is no acceptable conversion)";
375 std::string reason = reasonStream.str();
376 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000377}
378
379//
380// Same error message for all binary operations don't work.
381//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400382void TParseContext::binaryOpError(const TSourceLoc &line,
383 const char *op,
384 TString left,
385 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000386{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000387 std::stringstream reasonStream;
388 reasonStream << "wrong operand types - no operation '" << op
389 << "' exists that takes a left-hand operand of type '" << left
390 << "' and a right operand of type '" << right
391 << "' (or there is no acceptable conversion)";
392 std::string reason = reasonStream.str();
393 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000394}
395
Olli Etuaho856c4972016-08-08 11:38:39 +0300396void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
397 TPrecision precision,
398 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530399{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400400 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300401 return;
Martin Radev70866b82016-07-22 15:27:42 +0300402
403 if (precision != EbpUndefined && !SupportsPrecision(type))
404 {
405 error(line, "illegal type for precision qualifier", getBasicString(type));
406 }
407
Olli Etuaho183d7e22015-11-20 15:59:09 +0200408 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530409 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200410 switch (type)
411 {
412 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400413 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300414 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200415 case EbtInt:
416 case EbtUInt:
417 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400418 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300419 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200420 default:
jchen10cc2a10e2017-05-03 14:05:12 +0800421 if (IsOpaqueType(type))
Olli Etuaho183d7e22015-11-20 15:59:09 +0200422 {
jchen10cc2a10e2017-05-03 14:05:12 +0800423 error(line, "No precision specified", getBasicString(type));
Martin Radev2cc85b32016-08-05 16:22:53 +0300424 return;
425 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200426 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000427 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000428}
429
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000430// Both test and if necessary, spit out an error, to see if the node is really
431// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300432bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000433{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500434 TIntermSymbol *symNode = node->getAsSymbolNode();
435 TIntermBinary *binaryNode = node->getAsBinaryNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100436 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
437
438 if (swizzleNode)
439 {
440 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
441 if (ok && swizzleNode->hasDuplicateOffsets())
442 {
443 error(line, " l-value of swizzle cannot have duplicate components", op);
444 return false;
445 }
446 return ok;
447 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000448
Arun Patole7e7e68d2015-05-22 12:02:25 +0530449 if (binaryNode)
450 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400451 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530452 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400453 case EOpIndexDirect:
454 case EOpIndexIndirect:
455 case EOpIndexDirectStruct:
456 case EOpIndexDirectInterfaceBlock:
Olli Etuaho856c4972016-08-08 11:38:39 +0300457 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400458 default:
459 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000460 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000461 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300462 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000463 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000464
jchen10cc2a10e2017-05-03 14:05:12 +0800465 std::string message;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530466 switch (node->getQualifier())
467 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400468 case EvqConst:
469 message = "can't modify a const";
470 break;
471 case EvqConstReadOnly:
472 message = "can't modify a const";
473 break;
474 case EvqAttribute:
475 message = "can't modify an attribute";
476 break;
477 case EvqFragmentIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400478 case EvqVertexIn:
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800479 case EvqGeometryIn:
Jiawei Shaoe8ef2bc2017-08-29 13:38:57 +0800480 case EvqFlatIn:
481 case EvqSmoothIn:
482 case EvqCentroidIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400483 message = "can't modify an input";
484 break;
485 case EvqUniform:
486 message = "can't modify a uniform";
487 break;
488 case EvqVaryingIn:
489 message = "can't modify a varying";
490 break;
491 case EvqFragCoord:
492 message = "can't modify gl_FragCoord";
493 break;
494 case EvqFrontFacing:
495 message = "can't modify gl_FrontFacing";
496 break;
497 case EvqPointCoord:
498 message = "can't modify gl_PointCoord";
499 break;
Martin Radevb0883602016-08-04 17:48:58 +0300500 case EvqNumWorkGroups:
501 message = "can't modify gl_NumWorkGroups";
502 break;
503 case EvqWorkGroupSize:
504 message = "can't modify gl_WorkGroupSize";
505 break;
506 case EvqWorkGroupID:
507 message = "can't modify gl_WorkGroupID";
508 break;
509 case EvqLocalInvocationID:
510 message = "can't modify gl_LocalInvocationID";
511 break;
512 case EvqGlobalInvocationID:
513 message = "can't modify gl_GlobalInvocationID";
514 break;
515 case EvqLocalInvocationIndex:
516 message = "can't modify gl_LocalInvocationIndex";
517 break;
Olli Etuaho7142f6c2017-05-05 17:07:26 +0300518 case EvqViewIDOVR:
519 message = "can't modify gl_ViewID_OVR";
520 break;
Martin Radev802abe02016-08-04 17:48:32 +0300521 case EvqComputeIn:
522 message = "can't modify work group size variable";
523 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +0800524 case EvqPerVertexIn:
525 message = "can't modify any member in gl_in";
526 break;
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800527 case EvqPrimitiveIDIn:
528 message = "can't modify gl_PrimitiveIDIn";
529 break;
530 case EvqInvocationID:
531 message = "can't modify gl_InvocationID";
532 break;
533 case EvqPrimitiveID:
534 if (mShaderType == GL_FRAGMENT_SHADER)
535 {
536 message = "can't modify gl_PrimitiveID in a fragment shader";
537 }
538 break;
539 case EvqLayer:
540 if (mShaderType == GL_FRAGMENT_SHADER)
541 {
542 message = "can't modify gl_Layer in a fragment shader";
543 }
544 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400545 default:
546 //
547 // Type that can't be written to?
548 //
549 if (node->getBasicType() == EbtVoid)
550 {
551 message = "can't modify void";
552 }
jchen10cc2a10e2017-05-03 14:05:12 +0800553 if (IsOpaqueType(node->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -0400554 {
jchen10cc2a10e2017-05-03 14:05:12 +0800555 message = "can't modify a variable with type ";
556 message += getBasicString(node->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300557 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800558 else if (node->getMemoryQualifier().readonly)
559 {
560 message = "can't modify a readonly variable";
561 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000562 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000563
jchen10cc2a10e2017-05-03 14:05:12 +0800564 if (message.empty() && binaryNode == 0 && symNode == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530565 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000566 error(line, "l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000567
Olli Etuaho8a176262016-08-16 14:23:01 +0300568 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000569 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000570
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000571 //
572 // Everything else is okay, no error.
573 //
jchen10cc2a10e2017-05-03 14:05:12 +0800574 if (message.empty())
Olli Etuaho8a176262016-08-16 14:23:01 +0300575 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000576
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000577 //
578 // If we get here, we have an error and a message.
579 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530580 if (symNode)
581 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000582 const char *symbol = symNode->getSymbol().c_str();
583 std::stringstream reasonStream;
584 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
585 std::string reason = reasonStream.str();
586 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000587 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530588 else
589 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000590 std::stringstream reasonStream;
591 reasonStream << "l-value required (" << message << ")";
592 std::string reason = reasonStream.str();
593 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000594 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000595
Olli Etuaho8a176262016-08-16 14:23:01 +0300596 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000597}
598
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000599// Both test, and if necessary spit out an error, to see if the node is really
600// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300601void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000602{
Olli Etuaho383b7912016-08-05 11:22:59 +0300603 if (node->getQualifier() != EvqConst)
604 {
605 error(node->getLine(), "constant expression required", "");
606 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000607}
608
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000609// Both test, and if necessary spit out an error, to see if the node is really
610// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300611void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000612{
Olli Etuaho383b7912016-08-05 11:22:59 +0300613 if (!node->isScalarInt())
614 {
615 error(node->getLine(), "integer expression required", token);
616 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000617}
618
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000619// Both test, and if necessary spit out an error, to see if we are currently
620// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800621bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000622{
Olli Etuaho856c4972016-08-08 11:38:39 +0300623 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300624 {
625 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800626 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300627 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800628 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000629}
630
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300631// ESSL 3.00.5 sections 3.8 and 3.9.
632// If it starts "gl_" or contains two consecutive underscores, it's reserved.
633// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300634bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000635{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530636 static const char *reservedErrMsg = "reserved built-in name";
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300637 if (identifier.compare(0, 3, "gl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530638 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300639 error(line, reservedErrMsg, "gl_");
640 return false;
641 }
642 if (sh::IsWebGLBasedSpec(mShaderSpec))
643 {
644 if (identifier.compare(0, 6, "webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530645 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300646 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300647 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000648 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300649 if (identifier.compare(0, 7, "_webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530650 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300651 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300652 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000653 }
654 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300655 if (identifier.find("__") != TString::npos)
656 {
657 error(line,
658 "identifiers containing two consecutive underscores (__) are reserved as "
659 "possible future keywords",
660 identifier.c_str());
661 return false;
662 }
Olli Etuaho8a176262016-08-16 14:23:01 +0300663 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000664}
665
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300666// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300667bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800668 const TIntermSequence *arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300669 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000670{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800671 if (arguments->empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530672 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200673 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300674 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000675 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200676
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300677 for (TIntermNode *arg : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530678 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300679 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200680 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300681 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200682 {
jchen10cc2a10e2017-05-03 14:05:12 +0800683 std::string reason("cannot convert a variable with type ");
684 reason += getBasicString(argTyped->getBasicType());
685 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300686 return false;
687 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800688 else if (argTyped->getMemoryQualifier().writeonly)
689 {
690 error(line, "cannot convert a variable with writeonly", "constructor");
691 return false;
692 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200693 if (argTyped->getBasicType() == EbtVoid)
694 {
695 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300696 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200697 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000698 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000699
Olli Etuaho856c4972016-08-08 11:38:39 +0300700 if (type.isArray())
701 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300702 // The size of an unsized constructor should already have been determined.
703 ASSERT(!type.isUnsizedArray());
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300704 if (static_cast<size_t>(type.getOutermostArraySize()) != arguments->size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300705 {
706 error(line, "array constructor needs one argument per array element", "constructor");
707 return false;
708 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300709 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
710 // the array.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800711 for (TIntermNode *const &argNode : *arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300712 {
713 const TType &argType = argNode->getAsTyped()->getType();
Jamie Madill34bf2d92017-02-06 13:40:59 -0500714 if (argType.isArray())
715 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300716 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500717 return false;
718 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300719 if (!argType.isElementTypeOf(type))
Olli Etuaho856c4972016-08-08 11:38:39 +0300720 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000721 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300722 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300723 }
724 }
725 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300726 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300727 {
728 const TFieldList &fields = type.getStruct()->fields();
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300729 if (fields.size() != arguments->size())
730 {
731 error(line,
732 "Number of constructor parameters does not match the number of structure fields",
733 "constructor");
734 return false;
735 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300736
737 for (size_t i = 0; i < fields.size(); i++)
738 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800739 if (i >= arguments->size() ||
740 (*arguments)[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300741 {
742 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000743 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300744 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300745 }
746 }
747 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300748 else
749 {
750 // We're constructing a scalar, vector, or matrix.
751
752 // Note: It's okay to have too many components available, but not okay to have unused
753 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
754 // there is an extra argument, so 'overFull' will become true.
755
756 size_t size = 0;
757 bool full = false;
758 bool overFull = false;
759 bool matrixArg = false;
760 for (TIntermNode *arg : *arguments)
761 {
762 const TIntermTyped *argTyped = arg->getAsTyped();
763 ASSERT(argTyped != nullptr);
764
Olli Etuaho487b63a2017-05-23 15:55:09 +0300765 if (argTyped->getBasicType() == EbtStruct)
766 {
767 error(line, "a struct cannot be used as a constructor argument for this type",
768 "constructor");
769 return false;
770 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300771 if (argTyped->getType().isArray())
772 {
773 error(line, "constructing from a non-dereferenced array", "constructor");
774 return false;
775 }
776 if (argTyped->getType().isMatrix())
777 {
778 matrixArg = true;
779 }
780
781 size += argTyped->getType().getObjectSize();
782 if (full)
783 {
784 overFull = true;
785 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300786 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300787 {
788 full = true;
789 }
790 }
791
792 if (type.isMatrix() && matrixArg)
793 {
794 if (arguments->size() != 1)
795 {
796 error(line, "constructing matrix from matrix can only take one argument",
797 "constructor");
798 return false;
799 }
800 }
801 else
802 {
803 if (size != 1 && size < type.getObjectSize())
804 {
805 error(line, "not enough data provided for construction", "constructor");
806 return false;
807 }
808 if (overFull)
809 {
810 error(line, "too many arguments", "constructor");
811 return false;
812 }
813 }
814 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300815
Olli Etuaho8a176262016-08-16 14:23:01 +0300816 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000817}
818
Jamie Madillb98c3a82015-07-23 14:26:04 -0400819// This function checks to see if a void variable has been declared and raise an error message for
820// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000821//
822// returns true in case of an error
823//
Olli Etuaho856c4972016-08-08 11:38:39 +0300824bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400825 const TString &identifier,
826 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000827{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300828 if (type == EbtVoid)
829 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000830 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300831 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300832 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000833
Olli Etuaho8a176262016-08-16 14:23:01 +0300834 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000835}
836
Jamie Madillb98c3a82015-07-23 14:26:04 -0400837// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300838// or not.
Olli Etuaho56229f12017-07-10 14:16:33 +0300839bool TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000840{
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300841 if (type->getBasicType() != EbtBool || !type->isScalar())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530842 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000843 error(line, "boolean expression expected", "");
Olli Etuaho56229f12017-07-10 14:16:33 +0300844 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530845 }
Olli Etuaho56229f12017-07-10 14:16:33 +0300846 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000847}
848
Jamie Madillb98c3a82015-07-23 14:26:04 -0400849// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300850// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300851void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000852{
Martin Radev4a9cd802016-09-01 16:51:51 +0300853 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530854 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000855 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530856 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857}
858
jchen10cc2a10e2017-05-03 14:05:12 +0800859bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
860 const TTypeSpecifierNonArray &pType,
861 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530863 if (pType.type == EbtStruct)
864 {
Olli Etuaho0f684632017-07-13 12:42:15 +0300865 if (ContainsSampler(pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530866 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000867 std::stringstream reasonStream;
868 reasonStream << reason << " (structure contains a sampler)";
869 std::string reasonStr = reasonStream.str();
870 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300871 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000872 }
jchen10cc2a10e2017-05-03 14:05:12 +0800873 // only samplers need to be checked from structs, since other opaque types can't be struct
874 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300875 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530876 }
jchen10cc2a10e2017-05-03 14:05:12 +0800877 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530878 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000879 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300880 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000881 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000882
Olli Etuaho8a176262016-08-16 14:23:01 +0300883 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000884}
885
Olli Etuaho856c4972016-08-08 11:38:39 +0300886void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
887 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400888{
889 if (pType.layoutQualifier.location != -1)
890 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400891 error(line, "location must only be specified for a single input or output variable",
892 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400893 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400894}
895
Olli Etuaho856c4972016-08-08 11:38:39 +0300896void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
897 const TLayoutQualifier &layoutQualifier)
898{
899 if (layoutQualifier.location != -1)
900 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000901 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
902 if (mShaderVersion >= 310)
903 {
904 errorMsg =
Jiawei Shao4cc89e22017-08-31 14:25:54 +0800905 "invalid layout qualifier: only valid on shader inputs, outputs, and uniforms";
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000906 }
907 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300908 }
909}
910
Qin Jiajiaca68d982017-09-18 16:41:56 +0800911void TParseContext::checkStd430IsForShaderStorageBlock(const TSourceLoc &location,
912 const TLayoutBlockStorage &blockStorage,
913 const TQualifier &qualifier)
914{
915 if (blockStorage == EbsStd430 && qualifier != EvqBuffer)
916 {
917 error(location, "The std430 layout is supported only for shader storage blocks.", "std430");
918 }
919}
920
Martin Radev2cc85b32016-08-05 16:22:53 +0300921void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
922 TQualifier qualifier,
923 const TType &type)
924{
Martin Radev2cc85b32016-08-05 16:22:53 +0300925 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800926 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530927 {
jchen10cc2a10e2017-05-03 14:05:12 +0800928 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000929 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000930}
931
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000932// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300933unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000934{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530935 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000936
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200937 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
938 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
939 // fold as array size.
940 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000941 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000942 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300943 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000944 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000945
Olli Etuaho856c4972016-08-08 11:38:39 +0300946 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400947
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000948 if (constant->getBasicType() == EbtUInt)
949 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300950 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000951 }
952 else
953 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300954 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000955
Olli Etuaho856c4972016-08-08 11:38:39 +0300956 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000957 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400958 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300959 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000960 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400961
Olli Etuaho856c4972016-08-08 11:38:39 +0300962 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400963 }
964
Olli Etuaho856c4972016-08-08 11:38:39 +0300965 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400966 {
967 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300968 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400969 }
970
971 // The size of arrays is restricted here to prevent issues further down the
972 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
973 // 4096 registers so this should be reasonable even for aggressively optimizable code.
974 const unsigned int sizeLimit = 65536;
975
Olli Etuaho856c4972016-08-08 11:38:39 +0300976 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400977 {
978 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300979 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000980 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300981
982 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000983}
984
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000985// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300986bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
987 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000988{
Olli Etuaho8a176262016-08-16 14:23:01 +0300989 if ((elementQualifier.qualifier == EvqAttribute) ||
990 (elementQualifier.qualifier == EvqVertexIn) ||
991 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300992 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400993 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300994 TType(elementQualifier).getQualifierString());
995 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000996 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000997
Olli Etuaho8a176262016-08-16 14:23:01 +0300998 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000999}
1000
Olli Etuaho8a176262016-08-16 14:23:01 +03001001// See if this element type can be formed into an array.
Olli Etuahoe0803872017-08-23 15:30:23 +03001002bool TParseContext::checkArrayElementIsNotArray(const TSourceLoc &line,
1003 const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001004{
Olli Etuaho8a176262016-08-16 14:23:01 +03001005 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -04001006 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001007 error(line, "cannot declare arrays of arrays",
1008 TType(elementType).getCompleteString().c_str());
1009 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001010 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001011 return true;
1012}
1013
1014// Check if this qualified element type can be formed into an array. This is only called when array
1015// brackets are associated with an identifier in a declaration, like this:
1016// float a[2];
1017// Similar checks are done in addFullySpecifiedType for array declarations where the array brackets
1018// are associated with the type, like this:
1019// float[2] a;
1020bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
1021 const TPublicType &elementType)
1022{
1023 if (!checkArrayElementIsNotArray(indexLocation, elementType))
1024 {
1025 return false;
1026 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001027 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
1028 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
1029 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +03001030 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +03001031 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +03001032 {
Olli Etuahoe0803872017-08-23 15:30:23 +03001033 error(indexLocation, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001034 TType(elementType).getCompleteString().c_str());
1035 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +03001036 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001037 return checkIsValidQualifierForArray(indexLocation, elementType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001038}
1039
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001040// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001041void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
1042 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001043 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001044{
Olli Etuaho3739d232015-04-08 12:23:44 +03001045 ASSERT(type != nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03001046 if (type->getQualifier() == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001047 {
1048 // Make the qualifier make sense.
Olli Etuaho55bde912017-10-25 13:41:13 +03001049 type->setQualifier(EvqTemporary);
Olli Etuaho3739d232015-04-08 12:23:44 +03001050
1051 // Generate informative error messages for ESSL1.
1052 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001053 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001054 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301055 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001056 "structures containing arrays may not be declared constant since they cannot be "
1057 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +05301058 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001059 }
1060 else
1061 {
1062 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
1063 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001064 }
Olli Etuaho55bde912017-10-25 13:41:13 +03001065 // This will make the type sized if it isn't sized yet.
1066 checkIsNotUnsizedArray(line, "implicitly sized arrays need to be initialized",
1067 identifier.c_str(), type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001068}
1069
Olli Etuaho2935c582015-04-08 14:32:06 +03001070// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001071// and update the symbol table.
1072//
Olli Etuaho2935c582015-04-08 14:32:06 +03001073// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001074//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001075bool TParseContext::declareVariable(const TSourceLoc &line,
1076 const TString &identifier,
1077 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001078 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001079{
Olli Etuaho2935c582015-04-08 14:32:06 +03001080 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001081
Olli Etuaho43364892017-02-13 16:00:12 +00001082 checkBindingIsValid(line, type);
1083
Olli Etuaho856c4972016-08-08 11:38:39 +03001084 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001085
Olli Etuaho2935c582015-04-08 14:32:06 +03001086 // gl_LastFragData may be redeclared with a new precision qualifier
1087 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1088 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001089 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1090 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001091 if (type.isArrayOfArrays())
1092 {
1093 error(line, "redeclaration of gl_LastFragData as an array of arrays",
1094 identifier.c_str());
1095 return false;
1096 }
1097 else if (static_cast<int>(type.getOutermostArraySize()) ==
1098 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001099 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001100 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001101 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001102 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001103 }
1104 }
1105 else
1106 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001107 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1108 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001109 return false;
1110 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001111 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001112
Olli Etuaho8a176262016-08-16 14:23:01 +03001113 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001114 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001115
Olli Etuaho0f684632017-07-13 12:42:15 +03001116 (*variable) = symbolTable.declareVariable(&identifier, type);
1117 if (!(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001118 {
1119 error(line, "redefinition", identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001120 return false;
1121 }
1122
Olli Etuaho8a176262016-08-16 14:23:01 +03001123 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001124 return false;
1125
1126 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001127}
1128
Martin Radev70866b82016-07-22 15:27:42 +03001129void TParseContext::checkIsParameterQualifierValid(
1130 const TSourceLoc &line,
1131 const TTypeQualifierBuilder &typeQualifierBuilder,
1132 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301133{
Olli Etuahocce89652017-06-19 16:04:09 +03001134 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001135 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001136
1137 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301138 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001139 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1140 }
1141
1142 if (!IsImage(type->getBasicType()))
1143 {
Olli Etuaho43364892017-02-13 16:00:12 +00001144 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001145 }
1146 else
1147 {
1148 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001149 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001150
Martin Radev70866b82016-07-22 15:27:42 +03001151 type->setQualifier(typeQualifier.qualifier);
1152
1153 if (typeQualifier.precision != EbpUndefined)
1154 {
1155 type->setPrecision(typeQualifier.precision);
1156 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001157}
1158
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001159bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001160{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001161 ASSERT(extension != TExtension::UNDEFINED);
Jiawei Shao0e883132017-10-26 09:53:50 +08001162 ASSERT(extension != TExtension::EXT_geometry_shader);
Jamie Madillb98c3a82015-07-23 14:26:04 -04001163 const TExtensionBehavior &extBehavior = extensionBehavior();
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001164 TExtensionBehavior::const_iterator iter = extBehavior.find(extension);
Arun Patole7e7e68d2015-05-22 12:02:25 +05301165 if (iter == extBehavior.end())
1166 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001167 error(line, "extension is not supported", GetExtensionNameString(extension));
Olli Etuaho8a176262016-08-16 14:23:01 +03001168 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001169 }
zmo@google.comf5450912011-09-09 01:37:19 +00001170 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +05301171 if (iter->second == EBhDisable || iter->second == EBhUndefined)
1172 {
Jiawei Shao0e883132017-10-26 09:53:50 +08001173 // We also need to check EXT_geometry_shader because internally we always use
1174 // TExtension::OES_geometry_shader to represent both OES_geometry_shader and
1175 // EXT_geometry_shader.
1176 if (extension == TExtension::OES_geometry_shader)
1177 {
1178 TExtensionBehavior::const_iterator iterExt =
1179 extBehavior.find(TExtension::EXT_geometry_shader);
1180 ASSERT(iterExt != extBehavior.end());
1181 if (iterExt->second == EBhUndefined)
1182 {
1183 error(line, "extension is disabled",
1184 GetExtensionNameString(TExtension::OES_geometry_shader));
1185 return false;
1186 }
1187 if (iterExt->second == EBhDisable)
1188 {
1189 error(line, "extension is disabled",
1190 GetExtensionNameString(TExtension::EXT_geometry_shader));
1191 return false;
1192 }
1193 if (iterExt->second == EBhWarn)
1194 {
1195 warning(line, "extension is being used",
1196 GetExtensionNameString(TExtension::EXT_geometry_shader));
1197 }
1198
1199 return true;
1200 }
1201
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001202 error(line, "extension is disabled", GetExtensionNameString(extension));
Olli Etuaho8a176262016-08-16 14:23:01 +03001203 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001204 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301205 if (iter->second == EBhWarn)
1206 {
Jiawei Shao0e883132017-10-26 09:53:50 +08001207 if (extension == TExtension::OES_geometry_shader)
1208 {
1209 TExtensionBehavior::const_iterator iterExt =
1210 extBehavior.find(TExtension::EXT_geometry_shader);
1211 ASSERT(iterExt != extBehavior.end());
1212 // We should output no warnings when OES_geometry_shader is declared as "warn" and
1213 // EXT_geometry_shader is declared as "require" or "enable".
1214 if (iterExt->second == EBhRequire || iterExt->second == EBhEnable)
1215 {
1216 return true;
1217 }
1218 }
1219
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001220 warning(line, "extension is being used", GetExtensionNameString(extension));
Olli Etuaho8a176262016-08-16 14:23:01 +03001221 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001222 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001223
Olli Etuaho8a176262016-08-16 14:23:01 +03001224 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001225}
1226
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001227// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1228// compile-time or link-time errors are the same whether or not the declaration is empty".
1229// This function implements all the checks that are done on qualifiers regardless of if the
1230// declaration is empty.
1231void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1232 const sh::TLayoutQualifier &layoutQualifier,
1233 const TSourceLoc &location)
1234{
1235 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1236 {
1237 error(location, "Shared memory declarations cannot have layout specified", "layout");
1238 }
1239
1240 if (layoutQualifier.matrixPacking != EmpUnspecified)
1241 {
1242 error(location, "layout qualifier only valid for interface blocks",
1243 getMatrixPackingString(layoutQualifier.matrixPacking));
1244 return;
1245 }
1246
1247 if (layoutQualifier.blockStorage != EbsUnspecified)
1248 {
1249 error(location, "layout qualifier only valid for interface blocks",
1250 getBlockStorageString(layoutQualifier.blockStorage));
1251 return;
1252 }
1253
1254 if (qualifier == EvqFragmentOut)
1255 {
1256 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1257 {
1258 error(location, "invalid layout qualifier combination", "yuv");
1259 return;
1260 }
1261 }
1262 else
1263 {
1264 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1265 }
1266
Olli Etuaho95468d12017-05-04 11:14:34 +03001267 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1268 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001269 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1270 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001271 {
1272 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1273 }
1274
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001275 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001276 if (mShaderVersion >= 310)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001277 {
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001278 canHaveLocation = canHaveLocation || qualifier == EvqUniform || IsVarying(qualifier);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001279 // We're not checking whether the uniform location is in range here since that depends on
1280 // the type of the variable.
1281 // The type can only be fully determined for non-empty declarations.
1282 }
1283 if (!canHaveLocation)
1284 {
1285 checkLocationIsNotSpecified(location, layoutQualifier);
1286 }
1287}
1288
jchen104cdac9e2017-05-08 11:01:20 +08001289void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1290 const TSourceLoc &location)
1291{
1292 if (publicType.precision != EbpHigh)
1293 {
1294 error(location, "Can only be highp", "atomic counter");
1295 }
1296 // dEQP enforces compile error if location is specified. See uniform_location.test.
1297 if (publicType.layoutQualifier.location != -1)
1298 {
1299 error(location, "location must not be set for atomic_uint", "layout");
1300 }
1301 if (publicType.layoutQualifier.binding == -1)
1302 {
1303 error(location, "no binding specified", "atomic counter");
1304 }
1305}
1306
Olli Etuaho55bde912017-10-25 13:41:13 +03001307void TParseContext::emptyDeclarationErrorCheck(const TType &type, const TSourceLoc &location)
Martin Radevb8b01222016-11-20 23:25:53 +02001308{
Olli Etuaho55bde912017-10-25 13:41:13 +03001309 if (type.isUnsizedArray())
Martin Radevb8b01222016-11-20 23:25:53 +02001310 {
1311 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1312 // error. It is assumed that this applies to empty declarations as well.
1313 error(location, "empty array declaration needs to specify a size", "");
1314 }
Martin Radevb8b01222016-11-20 23:25:53 +02001315}
1316
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001317// These checks are done for all declarations that are non-empty. They're done for non-empty
1318// declarations starting a declarator list, and declarators that follow an empty declaration.
1319void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1320 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001321{
Olli Etuahofa33d582015-04-09 14:33:12 +03001322 switch (publicType.qualifier)
1323 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001324 case EvqVaryingIn:
1325 case EvqVaryingOut:
1326 case EvqAttribute:
1327 case EvqVertexIn:
1328 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001329 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001330 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001331 {
1332 error(identifierLocation, "cannot be used with a structure",
1333 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001334 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001335 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001336 break;
1337 case EvqBuffer:
1338 if (publicType.getBasicType() != EbtInterfaceBlock)
1339 {
1340 error(identifierLocation,
1341 "cannot declare buffer variables at global scope(outside a block)",
1342 getQualifierString(publicType.qualifier));
1343 return;
1344 }
1345 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001346 default:
1347 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001348 }
jchen10cc2a10e2017-05-03 14:05:12 +08001349 std::string reason(getBasicString(publicType.getBasicType()));
1350 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001351 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001352 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001353 {
1354 return;
1355 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001356
Andrei Volykhina5527072017-03-22 16:46:30 +03001357 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1358 publicType.qualifier != EvqConst) &&
1359 publicType.getBasicType() == EbtYuvCscStandardEXT)
1360 {
1361 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1362 getQualifierString(publicType.qualifier));
1363 return;
1364 }
1365
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001366 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1367 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001368 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1369 // But invalid shaders may still reach here with an unsized array declaration.
Olli Etuaho55bde912017-10-25 13:41:13 +03001370 TType type(publicType);
1371 if (!type.isUnsizedArray())
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001372 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001373 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1374 publicType.layoutQualifier);
1375 }
1376 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001377
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001378 // check for layout qualifier issues
1379 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001380
Martin Radev2cc85b32016-08-05 16:22:53 +03001381 if (IsImage(publicType.getBasicType()))
1382 {
1383
1384 switch (layoutQualifier.imageInternalFormat)
1385 {
1386 case EiifRGBA32F:
1387 case EiifRGBA16F:
1388 case EiifR32F:
1389 case EiifRGBA8:
1390 case EiifRGBA8_SNORM:
1391 if (!IsFloatImage(publicType.getBasicType()))
1392 {
1393 error(identifierLocation,
1394 "internal image format requires a floating image type",
1395 getBasicString(publicType.getBasicType()));
1396 return;
1397 }
1398 break;
1399 case EiifRGBA32I:
1400 case EiifRGBA16I:
1401 case EiifRGBA8I:
1402 case EiifR32I:
1403 if (!IsIntegerImage(publicType.getBasicType()))
1404 {
1405 error(identifierLocation,
1406 "internal image format requires an integer image type",
1407 getBasicString(publicType.getBasicType()));
1408 return;
1409 }
1410 break;
1411 case EiifRGBA32UI:
1412 case EiifRGBA16UI:
1413 case EiifRGBA8UI:
1414 case EiifR32UI:
1415 if (!IsUnsignedImage(publicType.getBasicType()))
1416 {
1417 error(identifierLocation,
1418 "internal image format requires an unsigned image type",
1419 getBasicString(publicType.getBasicType()));
1420 return;
1421 }
1422 break;
1423 case EiifUnspecified:
1424 error(identifierLocation, "layout qualifier", "No image internal format specified");
1425 return;
1426 default:
1427 error(identifierLocation, "layout qualifier", "unrecognized token");
1428 return;
1429 }
1430
1431 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1432 switch (layoutQualifier.imageInternalFormat)
1433 {
1434 case EiifR32F:
1435 case EiifR32I:
1436 case EiifR32UI:
1437 break;
1438 default:
1439 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1440 {
1441 error(identifierLocation, "layout qualifier",
1442 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1443 "image variables must be qualified readonly and/or writeonly");
1444 return;
1445 }
1446 break;
1447 }
1448 }
1449 else
1450 {
Olli Etuaho43364892017-02-13 16:00:12 +00001451 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001452 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1453 }
jchen104cdac9e2017-05-08 11:01:20 +08001454
1455 if (IsAtomicCounter(publicType.getBasicType()))
1456 {
1457 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1458 }
1459 else
1460 {
1461 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1462 }
Olli Etuaho43364892017-02-13 16:00:12 +00001463}
Martin Radev2cc85b32016-08-05 16:22:53 +03001464
Olli Etuaho43364892017-02-13 16:00:12 +00001465void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1466{
1467 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001468 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1469 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1470 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1471 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1472 // when it comes to which shaders are accepted by the compiler.
1473 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001474 if (IsImage(type.getBasicType()))
1475 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001476 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1477 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001478 }
1479 else if (IsSampler(type.getBasicType()))
1480 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001481 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1482 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001483 }
jchen104cdac9e2017-05-08 11:01:20 +08001484 else if (IsAtomicCounter(type.getBasicType()))
1485 {
1486 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1487 }
Olli Etuaho43364892017-02-13 16:00:12 +00001488 else
1489 {
1490 ASSERT(!IsOpaqueType(type.getBasicType()));
1491 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001492 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001493}
1494
Olli Etuaho856c4972016-08-08 11:38:39 +03001495void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1496 const TString &layoutQualifierName,
1497 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001498{
1499
1500 if (mShaderVersion < versionRequired)
1501 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001502 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001503 }
1504}
1505
Olli Etuaho856c4972016-08-08 11:38:39 +03001506bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1507 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001508{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001509 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001510 for (size_t i = 0u; i < localSize.size(); ++i)
1511 {
1512 if (localSize[i] != -1)
1513 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001514 error(location,
1515 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1516 "global layout declaration",
1517 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001518 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001519 }
1520 }
1521
Olli Etuaho8a176262016-08-16 14:23:01 +03001522 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001523}
1524
Olli Etuaho43364892017-02-13 16:00:12 +00001525void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001526 TLayoutImageInternalFormat internalFormat)
1527{
1528 if (internalFormat != EiifUnspecified)
1529 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001530 error(location, "invalid layout qualifier: only valid when used with images",
1531 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001532 }
Olli Etuaho43364892017-02-13 16:00:12 +00001533}
1534
1535void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1536{
1537 if (binding != -1)
1538 {
1539 error(location,
1540 "invalid layout qualifier: only valid when used with opaque types or blocks",
1541 "binding");
1542 }
1543}
1544
jchen104cdac9e2017-05-08 11:01:20 +08001545void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1546{
1547 if (offset != -1)
1548 {
1549 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1550 "offset");
1551 }
1552}
1553
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001554void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1555 int binding,
1556 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001557{
1558 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001559 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001560 {
1561 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1562 }
1563}
1564
1565void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1566 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001567 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001568{
1569 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001570 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001571 {
1572 error(location, "sampler binding greater than maximum texture units", "binding");
1573 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001574}
1575
Jiajia Qinbc585152017-06-23 15:42:17 +08001576void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1577 const TQualifier &qualifier,
1578 int binding,
1579 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001580{
1581 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001582 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001583 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001584 if (binding + size > mMaxUniformBufferBindings)
1585 {
1586 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1587 "binding");
1588 }
1589 }
1590 else if (qualifier == EvqBuffer)
1591 {
1592 if (binding + size > mMaxShaderStorageBufferBindings)
1593 {
1594 error(location,
1595 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1596 "binding");
1597 }
jchen10af713a22017-04-19 09:10:56 +08001598 }
1599}
jchen104cdac9e2017-05-08 11:01:20 +08001600void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1601{
1602 if (binding >= mMaxAtomicCounterBindings)
1603 {
1604 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1605 "binding");
1606 }
1607}
jchen10af713a22017-04-19 09:10:56 +08001608
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001609void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1610 int objectLocationCount,
1611 const TLayoutQualifier &layoutQualifier)
1612{
1613 int loc = layoutQualifier.location;
1614 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1615 {
1616 error(location, "Uniform location out of range", "location");
1617 }
1618}
1619
Andrei Volykhina5527072017-03-22 16:46:30 +03001620void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1621{
1622 if (yuv != false)
1623 {
1624 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1625 }
1626}
1627
Jiajia Qinbc585152017-06-23 15:42:17 +08001628void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1629 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001630{
1631 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1632 {
1633 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001634 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1635 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1636 qual == EvqInOut || qual == EvqConstReadOnly))
1637 {
1638 if (argument->getMemoryQualifier().writeonly)
1639 {
1640 error(argument->getLine(),
1641 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
1642 fnCall->getFunctionSymbolInfo()->getName().c_str());
1643 return;
1644 }
1645 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001646 if (qual == EvqOut || qual == EvqInOut)
1647 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001648 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001649 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001650 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001651 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuahoec9232b2017-03-27 17:01:37 +03001652 fnCall->getFunctionSymbolInfo()->getName().c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001653 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001654 }
1655 }
1656 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001657}
1658
Martin Radev70866b82016-07-22 15:27:42 +03001659void TParseContext::checkInvariantVariableQualifier(bool invariant,
1660 const TQualifier qualifier,
1661 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001662{
Martin Radev70866b82016-07-22 15:27:42 +03001663 if (!invariant)
1664 return;
1665
1666 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001667 {
Martin Radev70866b82016-07-22 15:27:42 +03001668 // input variables in the fragment shader can be also qualified as invariant
1669 if (!sh::CanBeInvariantESSL1(qualifier))
1670 {
1671 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1672 }
1673 }
1674 else
1675 {
1676 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1677 {
1678 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1679 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001680 }
1681}
1682
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001683bool TParseContext::supportsExtension(TExtension extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001684{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001685 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001686 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1687 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001688}
1689
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001690bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001691{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001692 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001693}
1694
Jamie Madillb98c3a82015-07-23 14:26:04 -04001695void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1696 const char *extName,
1697 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001698{
1699 pp::SourceLocation srcLoc;
1700 srcLoc.file = loc.first_file;
1701 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001702 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001703}
1704
Jamie Madillb98c3a82015-07-23 14:26:04 -04001705void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1706 const char *name,
1707 const char *value,
1708 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001709{
1710 pp::SourceLocation srcLoc;
1711 srcLoc.file = loc.first_file;
1712 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001713 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001714}
1715
Martin Radev4c4c8e72016-08-04 12:25:34 +03001716sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001717{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001718 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001719 for (size_t i = 0u; i < result.size(); ++i)
1720 {
1721 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1722 {
1723 result[i] = 1;
1724 }
1725 else
1726 {
1727 result[i] = mComputeShaderLocalSize[i];
1728 }
1729 }
1730 return result;
1731}
1732
Olli Etuaho56229f12017-07-10 14:16:33 +03001733TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1734 const TSourceLoc &line)
1735{
1736 TIntermConstantUnion *node = new TIntermConstantUnion(
1737 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1738 node->setLine(line);
1739 return node;
1740}
1741
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001742/////////////////////////////////////////////////////////////////////////////////
1743//
1744// Non-Errors.
1745//
1746/////////////////////////////////////////////////////////////////////////////////
1747
Jamie Madill5c097022014-08-20 16:38:32 -04001748const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1749 const TString *name,
1750 const TSymbol *symbol)
1751{
Jamie Madill5c097022014-08-20 16:38:32 -04001752 if (!symbol)
1753 {
1754 error(location, "undeclared identifier", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001755 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001756 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001757
1758 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001759 {
1760 error(location, "variable expected", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001761 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001762 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001763
1764 const TVariable *variable = static_cast<const TVariable *>(symbol);
1765
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001766 if (variable->getExtension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001767 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001768 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001769 }
1770
Olli Etuaho0f684632017-07-13 12:42:15 +03001771 // Reject shaders using both gl_FragData and gl_FragColor
1772 TQualifier qualifier = variable->getType().getQualifier();
1773 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001774 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001775 mUsesFragData = true;
1776 }
1777 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1778 {
1779 mUsesFragColor = true;
1780 }
1781 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1782 {
1783 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001784 }
1785
Olli Etuaho0f684632017-07-13 12:42:15 +03001786 // This validation is not quite correct - it's only an error to write to
1787 // both FragData and FragColor. For simplicity, and because users shouldn't
1788 // be rewarded for reading from undefined varaibles, return an error
1789 // if they are both referenced, rather than assigned.
1790 if (mUsesFragData && mUsesFragColor)
1791 {
1792 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1793 if (mUsesSecondaryOutputs)
1794 {
1795 errorMessage =
1796 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1797 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1798 }
1799 error(location, errorMessage, name->c_str());
1800 }
1801
1802 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1803 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1804 qualifier == EvqWorkGroupSize)
1805 {
1806 error(location,
1807 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1808 "gl_WorkGroupSize");
1809 }
Jamie Madill5c097022014-08-20 16:38:32 -04001810 return variable;
1811}
1812
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001813TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1814 const TString *name,
1815 const TSymbol *symbol)
1816{
1817 const TVariable *variable = getNamedVariable(location, name, symbol);
1818
Olli Etuaho0f684632017-07-13 12:42:15 +03001819 if (!variable)
1820 {
1821 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1822 node->setLine(location);
1823 return node;
1824 }
1825
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001826 const TType &variableType = variable->getType();
Olli Etuaho56229f12017-07-10 14:16:33 +03001827 TIntermTyped *node = nullptr;
1828
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001829 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001830 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001831 const TConstantUnion *constArray = variable->getConstPointer();
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001832 node = new TIntermConstantUnion(constArray, variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001833 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001834 else if (variableType.getQualifier() == EvqWorkGroupSize && mComputeShaderLocalSizeDeclared)
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001835 {
1836 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1837 // needs to be added to the AST as a constant and not as a symbol.
1838 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1839 TConstantUnion *constArray = new TConstantUnion[3];
1840 for (size_t i = 0; i < 3; ++i)
1841 {
1842 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1843 }
1844
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001845 ASSERT(variableType.getBasicType() == EbtUInt);
1846 ASSERT(variableType.getObjectSize() == 3);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001847
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001848 TType type(variableType);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001849 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001850 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001851 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001852 else if ((mGeometryShaderInputPrimitiveType != EptUndefined) &&
1853 (variableType.getQualifier() == EvqPerVertexIn))
Jiawei Shaod8105a02017-08-08 09:54:36 +08001854 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001855 ASSERT(mGeometryShaderInputArraySize > 0u);
1856
1857 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), variableType);
Olli Etuaho55bde912017-10-25 13:41:13 +03001858 node->getTypePointer()->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
Jiawei Shaod8105a02017-08-08 09:54:36 +08001859 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001860 else
1861 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001862 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001863 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001864 ASSERT(node != nullptr);
1865 node->setLine(location);
1866 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001867}
1868
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001869// Initializers show up in several places in the grammar. Have one set of
1870// code to handle them here.
1871//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001872// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001873bool TParseContext::executeInitializer(const TSourceLoc &line,
1874 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001875 TType type,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001876 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001877 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001878{
Olli Etuaho13389b62016-10-16 11:48:18 +01001879 ASSERT(initNode != nullptr);
1880 ASSERT(*initNode == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001881
Olli Etuaho2935c582015-04-08 14:32:06 +03001882 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001883 if (type.isUnsizedArray())
1884 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001885 // In case initializer is not an array or type has more dimensions than initializer, this
1886 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1887 // actually is an array or not. Having a non-array initializer for an unsized array will
1888 // result in an error later, so we don't generate an error message here.
1889 type.sizeUnsizedArrays(initializer->getType().getArraySizes());
Olli Etuaho376f1b52015-04-13 13:23:41 +03001890 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001891 if (!declareVariable(line, identifier, type, &variable))
1892 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001893 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001894 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001895
Olli Etuahob0c645e2015-05-12 14:25:36 +03001896 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001897 if (symbolTable.atGlobalLevel() &&
1898 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001899 {
1900 // Error message does not completely match behavior with ESSL 1.00, but
1901 // we want to steer developers towards only using constant expressions.
1902 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001903 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001904 }
1905 if (globalInitWarning)
1906 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001907 warning(
1908 line,
1909 "global variable initializers should be constant expressions "
1910 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1911 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001912 }
1913
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001914 //
1915 // identifier must be of type constant, a global, or a temporary
1916 //
1917 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301918 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1919 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001920 error(line, " cannot initialize this type of qualifier ",
1921 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001922 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001923 }
1924 //
1925 // test for and propagate constant
1926 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001927
Arun Patole7e7e68d2015-05-22 12:02:25 +05301928 if (qualifier == EvqConst)
1929 {
1930 if (qualifier != initializer->getType().getQualifier())
1931 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001932 std::stringstream reasonStream;
1933 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1934 << "'";
1935 std::string reason = reasonStream.str();
1936 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001937 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001938 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001939 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301940 if (type != initializer->getType())
1941 {
1942 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001943 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001944 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001945 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001946 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001947
1948 // Save the constant folded value to the variable if possible. For example array
1949 // initializers are not folded, since that way copying the array literal to multiple places
1950 // in the shader is avoided.
1951 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1952 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301953 if (initializer->getAsConstantUnion())
1954 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001955 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001956 ASSERT(*initNode == nullptr);
1957 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301958 }
1959 else if (initializer->getAsSymbolNode())
1960 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001961 const TSymbol *symbol =
1962 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1963 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001964
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001965 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001966 if (constArray)
1967 {
1968 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001969 ASSERT(*initNode == nullptr);
1970 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001971 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001972 }
1973 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001974
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001975 TIntermSymbol *intermSymbol =
1976 new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
1977 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001978 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1979 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001980 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001981 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001982 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001983 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001984
Olli Etuaho914b79a2017-06-19 16:03:19 +03001985 return true;
1986}
1987
1988TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
1989 const TString &identifier,
1990 TIntermTyped *initializer,
1991 const TSourceLoc &loc)
1992{
1993 checkIsScalarBool(loc, pType);
1994 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03001995 TType type(pType);
1996 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03001997 {
1998 // The initializer is valid. The init condition needs to have a node - either the
1999 // initializer node, or a constant node in case the initialized variable is const and won't
2000 // be recorded in the AST.
2001 if (initNode == nullptr)
2002 {
2003 return initializer;
2004 }
2005 else
2006 {
2007 TIntermDeclaration *declaration = new TIntermDeclaration();
2008 declaration->appendDeclarator(initNode);
2009 return declaration;
2010 }
2011 }
2012 return nullptr;
2013}
2014
2015TIntermNode *TParseContext::addLoop(TLoopType type,
2016 TIntermNode *init,
2017 TIntermNode *cond,
2018 TIntermTyped *expr,
2019 TIntermNode *body,
2020 const TSourceLoc &line)
2021{
2022 TIntermNode *node = nullptr;
2023 TIntermTyped *typedCond = nullptr;
2024 if (cond)
2025 {
2026 typedCond = cond->getAsTyped();
2027 }
2028 if (cond == nullptr || typedCond)
2029 {
Olli Etuahocce89652017-06-19 16:04:09 +03002030 if (type == ELoopDoWhile)
2031 {
2032 checkIsScalarBool(line, typedCond);
2033 }
2034 // In the case of other loops, it was checked before that the condition is a scalar boolean.
2035 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
2036 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
2037 !typedCond->isVector()));
2038
Olli Etuaho3ec75682017-07-05 17:02:55 +03002039 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002040 node->setLine(line);
2041 return node;
2042 }
2043
Olli Etuahocce89652017-06-19 16:04:09 +03002044 ASSERT(type != ELoopDoWhile);
2045
Olli Etuaho914b79a2017-06-19 16:03:19 +03002046 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2047 ASSERT(declaration);
2048 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2049 ASSERT(declarator->getLeft()->getAsSymbolNode());
2050
2051 // The condition is a declaration. In the AST representation we don't support declarations as
2052 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2053 // the loop.
2054 TIntermBlock *block = new TIntermBlock();
2055
2056 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2057 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2058 block->appendStatement(declareCondition);
2059
2060 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2061 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002062 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002063 block->appendStatement(loop);
2064 loop->setLine(line);
2065 block->setLine(line);
2066 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002067}
2068
Olli Etuahocce89652017-06-19 16:04:09 +03002069TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2070 TIntermNodePair code,
2071 const TSourceLoc &loc)
2072{
Olli Etuaho56229f12017-07-10 14:16:33 +03002073 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002074
2075 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002076 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002077 {
2078 if (cond->getAsConstantUnion()->getBConst(0) == true)
2079 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002080 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002081 }
2082 else
2083 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002084 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002085 }
2086 }
2087
Olli Etuaho3ec75682017-07-05 17:02:55 +03002088 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002089 node->setLine(loc);
2090
2091 return node;
2092}
2093
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002094void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2095{
2096 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2097 typeSpecifier->getBasicType());
2098
2099 if (mShaderVersion < 300 && typeSpecifier->array)
2100 {
2101 error(typeSpecifier->getLine(), "not supported", "first-class array");
2102 typeSpecifier->clearArrayness();
2103 }
2104}
2105
Martin Radev70866b82016-07-22 15:27:42 +03002106TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302107 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002108{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002109 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002110
Martin Radev70866b82016-07-22 15:27:42 +03002111 TPublicType returnType = typeSpecifier;
2112 returnType.qualifier = typeQualifier.qualifier;
2113 returnType.invariant = typeQualifier.invariant;
2114 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002115 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002116 returnType.precision = typeSpecifier.precision;
2117
2118 if (typeQualifier.precision != EbpUndefined)
2119 {
2120 returnType.precision = typeQualifier.precision;
2121 }
2122
Martin Radev4a9cd802016-09-01 16:51:51 +03002123 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2124 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002125
Martin Radev4a9cd802016-09-01 16:51:51 +03002126 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2127 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002128
Martin Radev4a9cd802016-09-01 16:51:51 +03002129 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002130
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002131 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002132 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002133 if (typeSpecifier.array)
2134 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002135 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002136 returnType.clearArrayness();
2137 }
2138
Martin Radev70866b82016-07-22 15:27:42 +03002139 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002140 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002141 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002142 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002143 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002144 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002145
Martin Radev70866b82016-07-22 15:27:42 +03002146 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002147 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002148 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002149 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002150 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002151 }
2152 }
2153 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002154 {
Martin Radev70866b82016-07-22 15:27:42 +03002155 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002156 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002157 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002158 }
Martin Radev70866b82016-07-22 15:27:42 +03002159 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2160 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002161 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002162 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2163 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002164 }
Martin Radev70866b82016-07-22 15:27:42 +03002165 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002166 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002167 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002168 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002169 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002170 }
2171
2172 return returnType;
2173}
2174
Olli Etuaho856c4972016-08-08 11:38:39 +03002175void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2176 const TPublicType &type,
2177 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002178{
2179 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002180 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002181 {
2182 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002183 }
2184
2185 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2186 switch (qualifier)
2187 {
2188 case EvqVertexIn:
2189 // ESSL 3.00 section 4.3.4
2190 if (type.array)
2191 {
2192 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002193 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002194 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002195 return;
2196 case EvqFragmentOut:
2197 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002198 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002199 {
2200 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002201 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002202 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002203 return;
2204 default:
2205 break;
2206 }
2207
2208 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2209 // restrictions.
2210 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002211 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2212 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002213 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2214 {
2215 error(qualifierLocation, "must use 'flat' interpolation here",
2216 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002217 }
2218
Martin Radev4a9cd802016-09-01 16:51:51 +03002219 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002220 {
2221 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2222 // These restrictions are only implied by the ESSL 3.00 spec, but
2223 // the ESSL 3.10 spec lists these restrictions explicitly.
2224 if (type.array)
2225 {
2226 error(qualifierLocation, "cannot be an array of structures",
2227 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002228 }
2229 if (type.isStructureContainingArrays())
2230 {
2231 error(qualifierLocation, "cannot be a structure containing an array",
2232 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002233 }
2234 if (type.isStructureContainingType(EbtStruct))
2235 {
2236 error(qualifierLocation, "cannot be a structure containing a structure",
2237 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002238 }
2239 if (type.isStructureContainingType(EbtBool))
2240 {
2241 error(qualifierLocation, "cannot be a structure containing a bool",
2242 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002243 }
2244 }
2245}
2246
Martin Radev2cc85b32016-08-05 16:22:53 +03002247void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2248{
2249 if (qualifier.getType() == QtStorage)
2250 {
2251 const TStorageQualifierWrapper &storageQualifier =
2252 static_cast<const TStorageQualifierWrapper &>(qualifier);
2253 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2254 !symbolTable.atGlobalLevel())
2255 {
2256 error(storageQualifier.getLine(),
2257 "Local variables can only use the const storage qualifier.",
2258 storageQualifier.getQualifierString().c_str());
2259 }
2260 }
2261}
2262
Olli Etuaho43364892017-02-13 16:00:12 +00002263void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002264 const TSourceLoc &location)
2265{
Jiajia Qinbc585152017-06-23 15:42:17 +08002266 const std::string reason(
2267 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2268 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002269 if (memoryQualifier.readonly)
2270 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002271 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002272 }
2273 if (memoryQualifier.writeonly)
2274 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002275 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002276 }
Martin Radev049edfa2016-11-11 14:35:37 +02002277 if (memoryQualifier.coherent)
2278 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002279 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002280 }
2281 if (memoryQualifier.restrictQualifier)
2282 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002283 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002284 }
2285 if (memoryQualifier.volatileQualifier)
2286 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002287 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002288 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002289}
2290
jchen104cdac9e2017-05-08 11:01:20 +08002291// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2292// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002293void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2294 const TSourceLoc &loc,
2295 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002296{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002297 if (!IsAtomicCounter(type->getBasicType()))
2298 {
2299 return;
2300 }
2301
2302 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2303 : kAtomicCounterSize;
2304 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2305 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002306 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002307 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002308 {
2309 offset = bindingState.appendSpan(size);
2310 }
2311 else
2312 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002313 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002314 }
2315 if (offset == -1)
2316 {
2317 error(loc, "Offset overlapping", "atomic counter");
2318 return;
2319 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002320 layoutQualifier.offset = offset;
2321 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002322}
2323
Olli Etuaho454c34c2017-10-25 16:35:56 +03002324void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
2325 const char *token,
2326 TType *type)
2327{
2328 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2329 {
2330 if (type->isArray() && type->getOutermostArraySize() == 0u)
2331 {
2332 // Set size for the unsized geometry shader inputs if they are declared after a valid
2333 // input primitive declaration.
2334 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2335 {
2336 ASSERT(mGeometryShaderInputArraySize > 0u);
2337 type->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
2338 }
2339 else
2340 {
2341 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2342 // An input can be declared without an array size if there is a previous layout
2343 // which specifies the size.
2344 error(location,
2345 "Missing a valid input primitive declaration before declaring an unsized "
2346 "array input",
2347 token);
2348 }
2349 }
2350 else if (type->isArray())
2351 {
2352 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2353 }
2354 else
2355 {
2356 error(location, "Geometry shader input variable must be declared as an array", token);
2357 }
2358 }
2359}
2360
Olli Etuaho13389b62016-10-16 11:48:18 +01002361TIntermDeclaration *TParseContext::parseSingleDeclaration(
2362 TPublicType &publicType,
2363 const TSourceLoc &identifierOrTypeLocation,
2364 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002365{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002366 TType type(publicType);
2367 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2368 mDirectiveHandler.pragma().stdgl.invariantAll)
2369 {
2370 TQualifier qualifier = type.getQualifier();
2371
2372 // The directive handler has already taken care of rejecting invalid uses of this pragma
2373 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2374 // affected variable declarations:
2375 //
2376 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2377 // elsewhere, in TranslatorGLSL.)
2378 //
2379 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2380 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2381 // the way this is currently implemented we have to enable this compiler option before
2382 // parsing the shader and determining the shading language version it uses. If this were
2383 // implemented as a post-pass, the workaround could be more targeted.
2384 //
2385 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2386 // the specification, but there are desktop OpenGL drivers that expect that this is the
2387 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2388 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2389 {
2390 type.setInvariant(true);
2391 }
2392 }
2393
Olli Etuaho454c34c2017-10-25 16:35:56 +03002394 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier.c_str(), &type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002395
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002396 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2397 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002398
Olli Etuahobab4c082015-04-24 16:38:49 +03002399 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002400 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002401
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002402 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002403 if (emptyDeclaration)
2404 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002405 emptyDeclarationErrorCheck(type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002406 // In most cases we don't need to create a symbol node for an empty declaration.
2407 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2408 if (type.getBasicType() == EbtStruct)
2409 {
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03002410 symbol = new TIntermSymbol(symbolTable.getEmptySymbolId(), "", type);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002411 }
jchen104cdac9e2017-05-08 11:01:20 +08002412 else if (IsAtomicCounter(publicType.getBasicType()))
2413 {
2414 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2415 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002416 }
2417 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002418 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002419 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002420
Olli Etuaho55bde912017-10-25 13:41:13 +03002421 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002422
Olli Etuaho55bc9052017-10-25 17:33:06 +03002423 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, &type);
jchen104cdac9e2017-05-08 11:01:20 +08002424
Olli Etuaho2935c582015-04-08 14:32:06 +03002425 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002426 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002427
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002428 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002429 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002430 symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
Olli Etuaho13389b62016-10-16 11:48:18 +01002431 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002432 }
2433
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002434 TIntermDeclaration *declaration = new TIntermDeclaration();
2435 declaration->setLine(identifierOrTypeLocation);
2436 if (symbol)
2437 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002438 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002439 declaration->appendDeclarator(symbol);
2440 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002441 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002442}
2443
Olli Etuaho55bde912017-10-25 13:41:13 +03002444TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002445 const TSourceLoc &identifierLocation,
2446 const TString &identifier,
2447 const TSourceLoc &indexLocation,
Olli Etuaho55bde912017-10-25 13:41:13 +03002448 unsigned int arraySize)
Jamie Madill60ed9812013-06-06 11:56:46 -04002449{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002450 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002451
Olli Etuaho55bde912017-10-25 13:41:13 +03002452 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002453 identifierLocation);
2454
Olli Etuaho55bde912017-10-25 13:41:13 +03002455 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002456
Olli Etuaho55bde912017-10-25 13:41:13 +03002457 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002458
Olli Etuaho55bde912017-10-25 13:41:13 +03002459 TType arrayType(elementType);
2460 arrayType.makeArray(arraySize);
Jamie Madill60ed9812013-06-06 11:56:46 -04002461
Olli Etuaho454c34c2017-10-25 16:35:56 +03002462 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier.c_str(), &arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002463
2464 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2465
Olli Etuaho55bc9052017-10-25 17:33:06 +03002466 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002467
Olli Etuaho2935c582015-04-08 14:32:06 +03002468 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002469 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002470
Olli Etuaho13389b62016-10-16 11:48:18 +01002471 TIntermDeclaration *declaration = new TIntermDeclaration();
2472 declaration->setLine(identifierLocation);
2473
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002474 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002475 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002476 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2477 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002478 declaration->appendDeclarator(symbol);
2479 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002480
Olli Etuaho13389b62016-10-16 11:48:18 +01002481 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002482}
2483
Olli Etuaho13389b62016-10-16 11:48:18 +01002484TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2485 const TSourceLoc &identifierLocation,
2486 const TString &identifier,
2487 const TSourceLoc &initLocation,
2488 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002489{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002490 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002491
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002492 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2493 identifierLocation);
2494
2495 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002496
Olli Etuaho13389b62016-10-16 11:48:18 +01002497 TIntermDeclaration *declaration = new TIntermDeclaration();
2498 declaration->setLine(identifierLocation);
2499
2500 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002501 TType type(publicType);
2502 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002503 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002504 if (initNode)
2505 {
2506 declaration->appendDeclarator(initNode);
2507 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002508 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002509 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002510}
2511
Olli Etuaho13389b62016-10-16 11:48:18 +01002512TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002513 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002514 const TSourceLoc &identifierLocation,
2515 const TString &identifier,
2516 const TSourceLoc &indexLocation,
Olli Etuaho55bde912017-10-25 13:41:13 +03002517 unsigned int arraySize,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002518 const TSourceLoc &initLocation,
2519 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002520{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002521 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002522
Olli Etuaho55bde912017-10-25 13:41:13 +03002523 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002524 identifierLocation);
2525
Olli Etuaho55bde912017-10-25 13:41:13 +03002526 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002527
Olli Etuaho55bde912017-10-25 13:41:13 +03002528 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002529
Olli Etuaho55bde912017-10-25 13:41:13 +03002530 TType arrayType(elementType);
2531 arrayType.makeArray(arraySize);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002532
Olli Etuaho13389b62016-10-16 11:48:18 +01002533 TIntermDeclaration *declaration = new TIntermDeclaration();
2534 declaration->setLine(identifierLocation);
2535
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002536 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002537 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002538 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002539 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002540 if (initNode)
2541 {
2542 declaration->appendDeclarator(initNode);
2543 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002544 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002545
2546 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002547}
2548
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002549TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002550 const TTypeQualifierBuilder &typeQualifierBuilder,
2551 const TSourceLoc &identifierLoc,
2552 const TString *identifier,
2553 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002554{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002555 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002556
Martin Radev70866b82016-07-22 15:27:42 +03002557 if (!typeQualifier.invariant)
2558 {
2559 error(identifierLoc, "Expected invariant", identifier->c_str());
2560 return nullptr;
2561 }
2562 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2563 {
2564 return nullptr;
2565 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002566 if (!symbol)
2567 {
2568 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002569 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002570 }
Martin Radev70866b82016-07-22 15:27:42 +03002571 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002572 {
Martin Radev70866b82016-07-22 15:27:42 +03002573 error(identifierLoc, "invariant declaration specifies qualifier",
2574 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002575 }
Martin Radev70866b82016-07-22 15:27:42 +03002576 if (typeQualifier.precision != EbpUndefined)
2577 {
2578 error(identifierLoc, "invariant declaration specifies precision",
2579 getPrecisionString(typeQualifier.precision));
2580 }
2581 if (!typeQualifier.layoutQualifier.isEmpty())
2582 {
2583 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2584 }
2585
2586 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002587 if (!variable)
2588 {
2589 return nullptr;
2590 }
Martin Radev70866b82016-07-22 15:27:42 +03002591 const TType &type = variable->getType();
2592
2593 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2594 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002595 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002596
2597 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2598
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002599 TIntermSymbol *intermSymbol = new TIntermSymbol(variable->getUniqueId(), *identifier, type);
2600 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002601
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002602 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002603}
2604
Olli Etuaho13389b62016-10-16 11:48:18 +01002605void TParseContext::parseDeclarator(TPublicType &publicType,
2606 const TSourceLoc &identifierLocation,
2607 const TString &identifier,
2608 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002609{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002610 // If the declaration starting this declarator list was empty (example: int,), some checks were
2611 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002612 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002613 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002614 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2615 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002616 }
2617
Olli Etuaho856c4972016-08-08 11:38:39 +03002618 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002619
Olli Etuaho2935c582015-04-08 14:32:06 +03002620 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002621 TType type(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002622
2623 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &type);
2624
Olli Etuaho55bde912017-10-25 13:41:13 +03002625 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &type);
2626
Olli Etuaho55bc9052017-10-25 17:33:06 +03002627 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &type);
2628
Olli Etuaho43364892017-02-13 16:00:12 +00002629 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002630
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002631 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002632 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002633 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
2634 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002635 declarationOut->appendDeclarator(symbol);
2636 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002637}
2638
Olli Etuaho55bde912017-10-25 13:41:13 +03002639void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002640 const TSourceLoc &identifierLocation,
2641 const TString &identifier,
2642 const TSourceLoc &arrayLocation,
Olli Etuaho55bde912017-10-25 13:41:13 +03002643 unsigned int arraySize,
Olli Etuaho13389b62016-10-16 11:48:18 +01002644 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002645{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002646 // If the declaration starting this declarator list was empty (example: int,), some checks were
2647 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002648 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002649 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002650 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002651 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002652 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002653
Olli Etuaho55bde912017-10-25 13:41:13 +03002654 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002655
Olli Etuaho55bde912017-10-25 13:41:13 +03002656 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002657 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002658 TType arrayType(elementType);
2659 arrayType.makeArray(arraySize);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002660
Olli Etuaho454c34c2017-10-25 16:35:56 +03002661 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &arrayType);
2662
Olli Etuaho55bde912017-10-25 13:41:13 +03002663 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2664
Olli Etuaho55bc9052017-10-25 17:33:06 +03002665 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002666
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002667 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002668 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002669
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002670 if (variable)
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002671 {
2672 TIntermSymbol *symbol =
2673 new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2674 symbol->setLine(identifierLocation);
2675 declarationOut->appendDeclarator(symbol);
2676 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002677 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002678}
2679
Olli Etuaho13389b62016-10-16 11:48:18 +01002680void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2681 const TSourceLoc &identifierLocation,
2682 const TString &identifier,
2683 const TSourceLoc &initLocation,
2684 TIntermTyped *initializer,
2685 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002686{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002687 // If the declaration starting this declarator list was empty (example: int,), some checks were
2688 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002689 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002690 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002691 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2692 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002693 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002694
Olli Etuaho856c4972016-08-08 11:38:39 +03002695 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002696
Olli Etuaho13389b62016-10-16 11:48:18 +01002697 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002698 TType type(publicType);
2699 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002700 {
2701 //
2702 // build the intermediate representation
2703 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002704 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002705 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002706 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002707 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002708 }
2709}
2710
Olli Etuaho55bde912017-10-25 13:41:13 +03002711void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002712 const TSourceLoc &identifierLocation,
2713 const TString &identifier,
2714 const TSourceLoc &indexLocation,
Olli Etuaho55bde912017-10-25 13:41:13 +03002715 unsigned int arraySize,
Olli Etuaho13389b62016-10-16 11:48:18 +01002716 const TSourceLoc &initLocation,
2717 TIntermTyped *initializer,
2718 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002719{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002720 // If the declaration starting this declarator list was empty (example: int,), some checks were
2721 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002722 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002723 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002724 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002725 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002726 }
2727
Olli Etuaho55bde912017-10-25 13:41:13 +03002728 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002729
Olli Etuaho55bde912017-10-25 13:41:13 +03002730 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002731
Olli Etuaho55bde912017-10-25 13:41:13 +03002732 TType arrayType(elementType);
2733 arrayType.makeArray(arraySize);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002734
2735 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002736 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002737 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002738 {
2739 if (initNode)
2740 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002741 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002742 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002743 }
2744}
2745
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002746TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2747{
2748 // It's simpler to parse an empty statement as a constant expression rather than having a
2749 // different type of node just for empty statements, that will be pruned from the AST anyway.
2750 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2751 node->setLine(location);
2752 return node;
2753}
2754
jchen104cdac9e2017-05-08 11:01:20 +08002755void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2756 const TSourceLoc &location)
2757{
2758 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2759 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2760 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2761 {
2762 error(location, "Requires both binding and offset", "layout");
2763 return;
2764 }
2765 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2766}
2767
Olli Etuahocce89652017-06-19 16:04:09 +03002768void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2769 const TPublicType &type,
2770 const TSourceLoc &loc)
2771{
2772 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2773 !getFragmentPrecisionHigh())
2774 {
2775 error(loc, "precision is not supported in fragment shader", "highp");
2776 }
2777
2778 if (!CanSetDefaultPrecisionOnType(type))
2779 {
2780 error(loc, "illegal type argument for default precision qualifier",
2781 getBasicString(type.getBasicType()));
2782 return;
2783 }
2784 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2785}
2786
Shaob5cc1192017-07-06 10:47:20 +08002787bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2788{
2789 switch (typeQualifier.layoutQualifier.primitiveType)
2790 {
2791 case EptLines:
2792 case EptLinesAdjacency:
2793 case EptTriangles:
2794 case EptTrianglesAdjacency:
2795 return typeQualifier.qualifier == EvqGeometryIn;
2796
2797 case EptLineStrip:
2798 case EptTriangleStrip:
2799 return typeQualifier.qualifier == EvqGeometryOut;
2800
2801 case EptPoints:
2802 return true;
2803
2804 default:
2805 UNREACHABLE();
2806 return false;
2807 }
2808}
2809
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002810void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2811 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002812{
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002813 if (mGeometryShaderInputArraySize == 0u)
2814 {
2815 mGeometryShaderInputArraySize = inputArraySize;
2816 }
2817 else if (mGeometryShaderInputArraySize != inputArraySize)
2818 {
2819 error(line,
2820 "Array size or input primitive declaration doesn't match the size of earlier sized "
2821 "array inputs.",
2822 "layout");
2823 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002824}
2825
Shaob5cc1192017-07-06 10:47:20 +08002826bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2827{
2828 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2829
2830 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2831
2832 if (layoutQualifier.maxVertices != -1)
2833 {
2834 error(typeQualifier.line,
2835 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2836 return false;
2837 }
2838
2839 // Set mGeometryInputPrimitiveType if exists
2840 if (layoutQualifier.primitiveType != EptUndefined)
2841 {
2842 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2843 {
2844 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2845 return false;
2846 }
2847
2848 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2849 {
2850 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002851 setGeometryShaderInputArraySize(
2852 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2853 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002854 }
2855 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2856 {
2857 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2858 "layout");
2859 return false;
2860 }
2861 }
2862
2863 // Set mGeometryInvocations if exists
2864 if (layoutQualifier.invocations > 0)
2865 {
2866 if (mGeometryShaderInvocations == 0)
2867 {
2868 mGeometryShaderInvocations = layoutQualifier.invocations;
2869 }
2870 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2871 {
2872 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2873 "layout");
2874 return false;
2875 }
2876 }
2877
2878 return true;
2879}
2880
2881bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2882{
2883 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2884
2885 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2886
2887 if (layoutQualifier.invocations > 0)
2888 {
2889 error(typeQualifier.line,
2890 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2891 return false;
2892 }
2893
2894 // Set mGeometryOutputPrimitiveType if exists
2895 if (layoutQualifier.primitiveType != EptUndefined)
2896 {
2897 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2898 {
2899 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2900 return false;
2901 }
2902
2903 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2904 {
2905 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2906 }
2907 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2908 {
2909 error(typeQualifier.line,
2910 "primitive doesn't match earlier output primitive declaration", "layout");
2911 return false;
2912 }
2913 }
2914
2915 // Set mGeometryMaxVertices if exists
2916 if (layoutQualifier.maxVertices > -1)
2917 {
2918 if (mGeometryShaderMaxVertices == -1)
2919 {
2920 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2921 }
2922 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2923 {
2924 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2925 "layout");
2926 return false;
2927 }
2928 }
2929
2930 return true;
2931}
2932
Martin Radev70866b82016-07-22 15:27:42 +03002933void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002934{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002935 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002936 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002937
Martin Radev70866b82016-07-22 15:27:42 +03002938 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2939 typeQualifier.line);
2940
Jamie Madillc2128ff2016-07-04 10:26:17 -04002941 // It should never be the case, but some strange parser errors can send us here.
2942 if (layoutQualifier.isEmpty())
2943 {
2944 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002945 return;
2946 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002947
Martin Radev802abe02016-08-04 17:48:32 +03002948 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002949 {
Olli Etuaho43364892017-02-13 16:00:12 +00002950 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002951 return;
2952 }
2953
Olli Etuaho43364892017-02-13 16:00:12 +00002954 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2955
2956 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002957
2958 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2959
Andrei Volykhina5527072017-03-22 16:46:30 +03002960 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2961
jchen104cdac9e2017-05-08 11:01:20 +08002962 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2963
Qin Jiajiaca68d982017-09-18 16:41:56 +08002964 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
2965 typeQualifier.qualifier);
2966
Martin Radev802abe02016-08-04 17:48:32 +03002967 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002968 {
Martin Radev802abe02016-08-04 17:48:32 +03002969 if (mComputeShaderLocalSizeDeclared &&
2970 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2971 {
2972 error(typeQualifier.line, "Work group size does not match the previous declaration",
2973 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002974 return;
2975 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002976
Martin Radev802abe02016-08-04 17:48:32 +03002977 if (mShaderVersion < 310)
2978 {
2979 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002980 return;
2981 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002982
Martin Radev4c4c8e72016-08-04 12:25:34 +03002983 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002984 {
2985 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002986 return;
2987 }
2988
2989 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2990 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2991
2992 const TConstantUnion *maxComputeWorkGroupSizeData =
2993 maxComputeWorkGroupSize->getConstPointer();
2994
2995 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2996 {
2997 if (layoutQualifier.localSize[i] != -1)
2998 {
2999 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
3000 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
3001 if (mComputeShaderLocalSize[i] < 1 ||
3002 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
3003 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003004 std::stringstream reasonStream;
3005 reasonStream << "invalid value: Value must be at least 1 and no greater than "
3006 << maxComputeWorkGroupSizeValue;
3007 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03003008
Olli Etuaho4de340a2016-12-16 09:32:03 +00003009 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003010 return;
3011 }
3012 }
3013 }
3014
3015 mComputeShaderLocalSizeDeclared = true;
3016 }
Shaob5cc1192017-07-06 10:47:20 +08003017 else if (typeQualifier.qualifier == EvqGeometryIn)
3018 {
3019 if (mShaderVersion < 310)
3020 {
3021 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
3022 return;
3023 }
3024
3025 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
3026 {
3027 return;
3028 }
3029 }
3030 else if (typeQualifier.qualifier == EvqGeometryOut)
3031 {
3032 if (mShaderVersion < 310)
3033 {
3034 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3035 "layout");
3036 return;
3037 }
3038
3039 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3040 {
3041 return;
3042 }
3043 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003044 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3045 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003046 {
3047 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3048 // specification.
3049 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3050 {
3051 error(typeQualifier.line, "Number of views does not match the previous declaration",
3052 "layout");
3053 return;
3054 }
3055
3056 if (layoutQualifier.numViews == -1)
3057 {
3058 error(typeQualifier.line, "No num_views specified", "layout");
3059 return;
3060 }
3061
3062 if (layoutQualifier.numViews > mMaxNumViews)
3063 {
3064 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3065 "layout");
3066 return;
3067 }
3068
3069 mNumViews = layoutQualifier.numViews;
3070 }
Martin Radev802abe02016-08-04 17:48:32 +03003071 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003072 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003073 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003074 {
Martin Radev802abe02016-08-04 17:48:32 +03003075 return;
3076 }
3077
Jiajia Qinbc585152017-06-23 15:42:17 +08003078 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003079 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003080 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003081 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003082 return;
3083 }
3084
3085 if (mShaderVersion < 300)
3086 {
3087 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3088 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003089 return;
3090 }
3091
Olli Etuaho09b04a22016-12-15 13:30:26 +00003092 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003093
3094 if (layoutQualifier.matrixPacking != EmpUnspecified)
3095 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003096 if (typeQualifier.qualifier == EvqUniform)
3097 {
3098 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3099 }
3100 else if (typeQualifier.qualifier == EvqBuffer)
3101 {
3102 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3103 }
Martin Radev802abe02016-08-04 17:48:32 +03003104 }
3105
3106 if (layoutQualifier.blockStorage != EbsUnspecified)
3107 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003108 if (typeQualifier.qualifier == EvqUniform)
3109 {
3110 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3111 }
3112 else if (typeQualifier.qualifier == EvqBuffer)
3113 {
3114 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3115 }
Martin Radev802abe02016-08-04 17:48:32 +03003116 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003117 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003118}
3119
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003120TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3121 const TFunction &function,
3122 const TSourceLoc &location,
3123 bool insertParametersToSymbolTable)
3124{
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003125 checkIsNotReserved(location, function.getName());
3126
Olli Etuahofe486322017-03-21 09:30:54 +00003127 TIntermFunctionPrototype *prototype =
3128 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003129 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
3130 // point to the data that already exists in the symbol table.
3131 prototype->getFunctionSymbolInfo()->setFromFunction(function);
3132 prototype->setLine(location);
3133
3134 for (size_t i = 0; i < function.getParamCount(); i++)
3135 {
3136 const TConstParameter &param = function.getParam(i);
3137
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003138 TIntermSymbol *symbol = nullptr;
3139
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003140 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3141 // be used for unused args).
3142 if (param.name != nullptr)
3143 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003144 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003145 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003146 {
Olli Etuaho0f684632017-07-13 12:42:15 +03003147 TVariable *variable = symbolTable.declareVariable(param.name, *param.type);
3148 if (variable)
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003149 {
3150 symbol = new TIntermSymbol(variable->getUniqueId(), variable->getName(),
3151 variable->getType());
3152 }
3153 else
3154 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003155 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003156 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003157 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003158 // Unsized type of a named parameter should have already been checked and sanitized.
3159 ASSERT(!param.type->isUnsizedArray());
3160 }
3161 else
3162 {
3163 if (param.type->isUnsizedArray())
3164 {
3165 error(location, "function parameter array must be sized at compile time", "[]");
3166 // We don't need to size the arrays since the parameter is unnamed and hence
3167 // inaccessible.
3168 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003169 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003170 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003171 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003172 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3173 // symbol.
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003174 symbol = new TIntermSymbol(symbolTable.getEmptySymbolId(), "", *param.type);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003175 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003176 symbol->setLine(location);
3177 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003178 }
3179 return prototype;
3180}
3181
Olli Etuaho16c745a2017-01-16 17:02:27 +00003182TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3183 const TFunction &parsedFunction,
3184 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003185{
Olli Etuaho476197f2016-10-11 13:59:08 +01003186 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3187 // first declaration. Either way the instance in the symbol table is used to track whether the
3188 // function is declared multiple times.
3189 TFunction *function = static_cast<TFunction *>(
3190 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3191 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003192 {
3193 // ESSL 1.00.17 section 4.2.7.
3194 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3195 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003196 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003197 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003198
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003199 TIntermFunctionPrototype *prototype =
3200 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003201
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003202 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003203
3204 if (!symbolTable.atGlobalLevel())
3205 {
3206 // ESSL 3.00.4 section 4.2.4.
3207 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003208 }
3209
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003210 return prototype;
3211}
3212
Olli Etuaho336b1472016-10-05 16:37:55 +01003213TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003214 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003215 TIntermBlock *functionBody,
3216 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003217{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003218 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003219 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3220 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003221 error(location, "function does not return a value:",
3222 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003223 }
3224
Olli Etuahof51fdd22016-10-03 10:03:40 +01003225 if (functionBody == nullptr)
3226 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003227 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003228 functionBody->setLine(location);
3229 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003230 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003231 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003232 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003233
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003234 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003235 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003236}
3237
Olli Etuaho476197f2016-10-11 13:59:08 +01003238void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3239 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003240 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003241{
Olli Etuaho476197f2016-10-11 13:59:08 +01003242 ASSERT(function);
3243 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003244 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003245 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003246
3247 if (builtIn)
3248 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003249 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003250 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003251 else
Jamie Madill185fb402015-06-12 15:48:48 -04003252 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003253 TFunction *prevDec = static_cast<TFunction *>(
3254 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3255
3256 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3257 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3258 // occurance.
3259 if (*function != prevDec)
3260 {
3261 // Swap the parameters of the previous declaration to the parameters of the function
3262 // definition (parameter names may differ).
3263 prevDec->swapParameters(**function);
3264
3265 // The function definition will share the same symbol as any previous declaration.
3266 *function = prevDec;
3267 }
3268
3269 if ((*function)->isDefined())
3270 {
3271 error(location, "function already has a body", (*function)->getName().c_str());
3272 }
3273
3274 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003275 }
Jamie Madill185fb402015-06-12 15:48:48 -04003276
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003277 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003278 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003279 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003280
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003281 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003282 setLoopNestingLevel(0);
3283}
3284
Jamie Madillb98c3a82015-07-23 14:26:04 -04003285TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003286{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003287 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003288 // We don't know at this point whether this is a function definition or a prototype.
3289 // The definition production code will check for redefinitions.
3290 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003291 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003292 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3293 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003294 //
3295 TFunction *prevDec =
3296 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303297
Olli Etuahod80f2942017-11-06 12:44:45 +02003298 for (size_t i = 0u; i < function->getParamCount(); ++i)
3299 {
3300 auto &param = function->getParam(i);
3301 if (param.type->isStructSpecifier())
3302 {
3303 // ESSL 3.00.6 section 12.10.
3304 error(location, "Function parameter type cannot be a structure definition",
3305 param.name->c_str());
3306 }
3307 }
3308
Martin Radevda6254b2016-12-14 17:00:36 +02003309 if (getShaderVersion() >= 300 &&
3310 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
3311 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303312 {
Martin Radevda6254b2016-12-14 17:00:36 +02003313 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303314 // Therefore overloading or redefining builtin functions is an error.
3315 error(location, "Name of a built-in function cannot be redeclared as function",
3316 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303317 }
3318 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003319 {
3320 if (prevDec->getReturnType() != function->getReturnType())
3321 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003322 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003323 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003324 }
3325 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3326 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003327 if (prevDec->getParam(i).type->getQualifier() !=
3328 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003329 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003330 error(location,
3331 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003332 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003333 }
3334 }
3335 }
3336
3337 //
3338 // Check for previously declared variables using the same name.
3339 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003340 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003341 if (prevSym)
3342 {
3343 if (!prevSym->isFunction())
3344 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003345 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003346 }
3347 }
3348 else
3349 {
3350 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003351 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003352 }
3353
3354 // We're at the inner scope level of the function's arguments and body statement.
3355 // Add the function prototype to the surrounding scope instead.
3356 symbolTable.getOuterLevel()->insert(function);
3357
Olli Etuaho78d13742017-01-18 13:06:10 +00003358 // Raise error message if main function takes any parameters or return anything other than void
3359 if (function->getName() == "main")
3360 {
3361 if (function->getParamCount() > 0)
3362 {
3363 error(location, "function cannot take any parameter(s)", "main");
3364 }
3365 if (function->getReturnType().getBasicType() != EbtVoid)
3366 {
3367 error(location, "main function cannot return a value",
3368 function->getReturnType().getBasicString());
3369 }
3370 }
3371
Jamie Madill185fb402015-06-12 15:48:48 -04003372 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003373 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3374 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003375 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3376 //
3377 return function;
3378}
3379
Olli Etuaho9de84a52016-06-14 17:36:01 +03003380TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3381 const TString *name,
3382 const TSourceLoc &location)
3383{
3384 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3385 {
3386 error(location, "no qualifiers allowed for function return",
3387 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003388 }
3389 if (!type.layoutQualifier.isEmpty())
3390 {
3391 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003392 }
jchen10cc2a10e2017-05-03 14:05:12 +08003393 // make sure an opaque type is not involved as well...
3394 std::string reason(getBasicString(type.getBasicType()));
3395 reason += "s can't be function return values";
3396 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003397 if (mShaderVersion < 300)
3398 {
3399 // Array return values are forbidden, but there's also no valid syntax for declaring array
3400 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00003401 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003402
3403 if (type.isStructureContainingArrays())
3404 {
3405 // ESSL 1.00.17 section 6.1 Function Definitions
3406 error(location, "structures containing arrays can't be function return values",
3407 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003408 }
3409 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003410
3411 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003412 return new TFunction(&symbolTable, name, new TType(type));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003413}
3414
Olli Etuahocce89652017-06-19 16:04:09 +03003415TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3416{
Olli Etuahocce89652017-06-19 16:04:09 +03003417 const TType *returnType = TCache::getType(EbtVoid, EbpUndefined);
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003418 return new TFunction(&symbolTable, name, returnType);
Olli Etuahocce89652017-06-19 16:04:09 +03003419}
3420
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003421TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003422{
Olli Etuahocce89652017-06-19 16:04:09 +03003423 if (mShaderVersion < 300 && publicType.array)
3424 {
3425 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3426 "[]");
3427 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003428 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003429 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003430 error(publicType.getLine(), "constructor can't be a structure definition",
3431 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003432 }
3433
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003434 TType *type = new TType(publicType);
3435 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003436 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003437 error(publicType.getLine(), "cannot construct this type",
3438 getBasicString(publicType.getBasicType()));
3439 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003440 }
3441
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003442 return new TFunction(&symbolTable, nullptr, type, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003443}
3444
Olli Etuaho55bde912017-10-25 13:41:13 +03003445void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3446 const char *errorMessage,
3447 const char *token,
3448 TType *arrayType)
3449{
3450 if (arrayType->isUnsizedArray())
3451 {
3452 error(line, errorMessage, token);
3453 arrayType->sizeUnsizedArrays(TVector<unsigned int>());
3454 }
3455}
3456
3457TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahocce89652017-06-19 16:04:09 +03003458 const TString *name,
3459 const TSourceLoc &nameLoc)
3460{
Olli Etuaho55bde912017-10-25 13:41:13 +03003461 ASSERT(type);
3462 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name->c_str(),
3463 type);
3464 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003465 {
3466 error(nameLoc, "illegal use of type 'void'", name->c_str());
3467 }
3468 checkIsNotReserved(nameLoc, *name);
Olli Etuahocce89652017-06-19 16:04:09 +03003469 TParameter param = {name, type};
3470 return param;
3471}
3472
Olli Etuaho55bde912017-10-25 13:41:13 +03003473TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3474 const TString *name,
3475 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003476{
Olli Etuaho55bde912017-10-25 13:41:13 +03003477 TType *type = new TType(publicType);
3478 return parseParameterDeclarator(type, name, nameLoc);
3479}
3480
3481TParameter TParseContext::parseParameterArrayDeclarator(const TString *name,
3482 const TSourceLoc &nameLoc,
3483 unsigned int arraySize,
3484 const TSourceLoc &arrayLoc,
3485 TPublicType *elementType)
3486{
3487 checkArrayElementIsNotArray(arrayLoc, *elementType);
3488 TType *arrayType = new TType(*elementType);
3489 arrayType->makeArray(arraySize);
3490 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003491}
3492
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003493bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(TIntermSequence *arguments,
3494 TType type,
3495 const TSourceLoc &line)
3496{
3497 if (arguments->empty())
3498 {
3499 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3500 return false;
3501 }
3502 for (TIntermNode *arg : *arguments)
3503 {
3504 TIntermTyped *element = arg->getAsTyped();
3505 ASSERT(element);
3506 size_t dimensionalityFromElement = element->getType().getArraySizes().size() + 1u;
3507 if (dimensionalityFromElement > type.getArraySizes().size())
3508 {
3509 error(line, "constructing from a non-dereferenced array", "constructor");
3510 return false;
3511 }
3512 else if (dimensionalityFromElement < type.getArraySizes().size())
3513 {
3514 if (dimensionalityFromElement == 1u)
3515 {
3516 error(line, "implicitly sized array of arrays constructor argument is not an array",
3517 "constructor");
3518 }
3519 else
3520 {
3521 error(line,
3522 "implicitly sized array of arrays constructor argument dimensionality is too "
3523 "low",
3524 "constructor");
3525 }
3526 return false;
3527 }
3528 }
3529 return true;
3530}
3531
Jamie Madillb98c3a82015-07-23 14:26:04 -04003532// This function is used to test for the correctness of the parameters passed to various constructor
3533// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003534//
Olli Etuaho856c4972016-08-08 11:38:39 +03003535// 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 +00003536//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003537TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003538 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303539 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003540{
Olli Etuaho856c4972016-08-08 11:38:39 +03003541 if (type.isUnsizedArray())
3542 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003543 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003544 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003545 type.sizeUnsizedArrays(TVector<unsigned int>());
Olli Etuaho3ec75682017-07-05 17:02:55 +03003546 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003547 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003548 TIntermTyped *firstElement = arguments->at(0)->getAsTyped();
3549 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003550 if (type.getOutermostArraySize() == 0u)
3551 {
3552 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments->size()));
3553 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003554 for (size_t i = 0; i < firstElement->getType().getArraySizes().size(); ++i)
3555 {
3556 if (type.getArraySizes()[i] == 0u)
3557 {
3558 type.setArraySize(i, firstElement->getType().getArraySizes().at(i));
3559 }
3560 }
3561 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003562 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003563
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003564 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003565 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003566 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003567 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003568
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003569 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003570 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003571
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003572 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3573 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003574 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003575 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003576 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003577 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003578}
3579
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003580//
3581// Interface/uniform blocks
Jiawei Shaod8105a02017-08-08 09:54:36 +08003582// TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003583//
Olli Etuaho13389b62016-10-16 11:48:18 +01003584TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003585 const TTypeQualifierBuilder &typeQualifierBuilder,
3586 const TSourceLoc &nameLine,
3587 const TString &blockName,
3588 TFieldList *fieldList,
3589 const TString *instanceName,
3590 const TSourceLoc &instanceLine,
3591 TIntermTyped *arrayIndex,
3592 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003593{
Olli Etuaho856c4972016-08-08 11:38:39 +03003594 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003595
Olli Etuaho77ba4082016-12-16 12:01:18 +00003596 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003597
Jiajia Qinbc585152017-06-23 15:42:17 +08003598 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003599 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003600 error(typeQualifier.line,
3601 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3602 "3.10",
3603 getQualifierString(typeQualifier.qualifier));
3604 }
3605 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3606 {
3607 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003608 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003609 }
3610
Martin Radev70866b82016-07-22 15:27:42 +03003611 if (typeQualifier.invariant)
3612 {
3613 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3614 }
3615
Jiajia Qinbc585152017-06-23 15:42:17 +08003616 if (typeQualifier.qualifier != EvqBuffer)
3617 {
3618 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3619 }
Olli Etuaho43364892017-02-13 16:00:12 +00003620
jchen10af713a22017-04-19 09:10:56 +08003621 // add array index
3622 unsigned int arraySize = 0;
3623 if (arrayIndex != nullptr)
3624 {
3625 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3626 }
3627
3628 if (mShaderVersion < 310)
3629 {
3630 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3631 }
3632 else
3633 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003634 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3635 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003636 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003637
Andrei Volykhina5527072017-03-22 16:46:30 +03003638 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3639
Jamie Madill099c0f32013-06-20 11:55:52 -04003640 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003641 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003642 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3643 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003644
Jamie Madill099c0f32013-06-20 11:55:52 -04003645 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3646 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003647 if (typeQualifier.qualifier == EvqUniform)
3648 {
3649 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3650 }
3651 else if (typeQualifier.qualifier == EvqBuffer)
3652 {
3653 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3654 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003655 }
3656
Jamie Madill1566ef72013-06-20 11:55:54 -04003657 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3658 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003659 if (typeQualifier.qualifier == EvqUniform)
3660 {
3661 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3662 }
3663 else if (typeQualifier.qualifier == EvqBuffer)
3664 {
3665 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3666 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003667 }
3668
Olli Etuaho856c4972016-08-08 11:38:39 +03003669 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003670
Martin Radev2cc85b32016-08-05 16:22:53 +03003671 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3672
Olli Etuaho0f684632017-07-13 12:42:15 +03003673 if (!symbolTable.declareInterfaceBlockName(&blockName))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303674 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003675 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003676 }
3677
Jamie Madill98493dd2013-07-08 14:39:03 -04003678 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303679 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3680 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003681 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303682 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003683 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303684 {
jchen10cc2a10e2017-05-03 14:05:12 +08003685 std::string reason("unsupported type - ");
3686 reason += fieldType->getBasicString();
3687 reason += " types are not allowed in interface blocks";
3688 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003689 }
3690
Jamie Madill98493dd2013-07-08 14:39:03 -04003691 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003692 switch (qualifier)
3693 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003694 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003695 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003696 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003697 if (typeQualifier.qualifier == EvqBuffer)
3698 {
3699 error(field->line(), "invalid qualifier on shader storage block member",
3700 getQualifierString(qualifier));
3701 }
3702 break;
3703 case EvqBuffer:
3704 if (typeQualifier.qualifier == EvqUniform)
3705 {
3706 error(field->line(), "invalid qualifier on uniform block member",
3707 getQualifierString(qualifier));
3708 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003709 break;
3710 default:
3711 error(field->line(), "invalid qualifier on interface block member",
3712 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003713 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003714 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003715
Martin Radev70866b82016-07-22 15:27:42 +03003716 if (fieldType->isInvariant())
3717 {
3718 error(field->line(), "invalid qualifier on interface block member", "invariant");
3719 }
3720
Jamie Madilla5efff92013-06-06 11:56:47 -04003721 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003722 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003723 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003724 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003725
Jamie Madill98493dd2013-07-08 14:39:03 -04003726 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003727 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003728 error(field->line(), "invalid layout qualifier: cannot be used here",
3729 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003730 }
3731
Jamie Madill98493dd2013-07-08 14:39:03 -04003732 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003733 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003734 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003735 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003736 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003737 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003738 warning(field->line(),
3739 "extraneous layout qualifier: only has an effect on matrix types",
3740 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003741 }
3742
Jamie Madill98493dd2013-07-08 14:39:03 -04003743 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003744
3745 if (typeQualifier.qualifier == EvqBuffer)
3746 {
3747 // set memory qualifiers
3748 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3749 // qualified with a memory qualifier, it is as if all of its members were declared with
3750 // the same memory qualifier.
3751 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3752 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3753 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3754 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3755 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3756 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3757 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3758 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3759 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3760 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3761 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003762 }
3763
Jamie Madillb98c3a82015-07-23 14:26:04 -04003764 TInterfaceBlock *interfaceBlock =
Shaob18c33e2017-08-16 12:37:51 +08003765 new TInterfaceBlock(&blockName, fieldList, instanceName, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003766 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
3767 if (arrayIndex != nullptr)
3768 {
3769 interfaceBlockType.makeArray(arraySize);
3770 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003771
3772 TString symbolName = "";
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003773 const TSymbolUniqueId *symbolId = nullptr;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003774
Jamie Madill98493dd2013-07-08 14:39:03 -04003775 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003776 {
3777 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003778 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3779 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003780 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303781 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003782
3783 // set parent pointer of the field variable
3784 fieldType->setInterfaceBlock(interfaceBlock);
3785
Olli Etuaho0f684632017-07-13 12:42:15 +03003786 TVariable *fieldVariable = symbolTable.declareVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003787
Olli Etuaho0f684632017-07-13 12:42:15 +03003788 if (fieldVariable)
3789 {
3790 fieldVariable->setQualifier(typeQualifier.qualifier);
3791 }
3792 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303793 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003794 error(field->line(), "redefinition of an interface block member name",
3795 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003796 }
3797 }
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003798 symbolId = &symbolTable.getEmptySymbolId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003799 }
3800 else
3801 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003802 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003803
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003804 // add a symbol for this interface block
Olli Etuaho0f684632017-07-13 12:42:15 +03003805 TVariable *instanceTypeDef = symbolTable.declareVariable(instanceName, interfaceBlockType);
3806 if (instanceTypeDef)
3807 {
3808 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003809 symbolId = &instanceTypeDef->getUniqueId();
Olli Etuaho0f684632017-07-13 12:42:15 +03003810 }
3811 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303812 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003813 error(instanceLine, "redefinition of an interface block instance name",
3814 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003815 }
Olli Etuaho0f684632017-07-13 12:42:15 +03003816 symbolName = *instanceName;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003817 }
3818
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003819 TIntermDeclaration *declaration = nullptr;
3820
3821 if (symbolId)
3822 {
3823 TIntermSymbol *blockSymbol = new TIntermSymbol(*symbolId, symbolName, interfaceBlockType);
3824 blockSymbol->setLine(typeQualifier.line);
3825 declaration = new TIntermDeclaration();
3826 declaration->appendDeclarator(blockSymbol);
3827 declaration->setLine(nameLine);
3828 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003829
3830 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003831 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003832}
3833
Olli Etuaho383b7912016-08-05 11:22:59 +03003834void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003835{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003836 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003837
3838 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003839 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303840 if (mStructNestingLevel > 1)
3841 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003842 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003843 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003844}
3845
3846void TParseContext::exitStructDeclaration()
3847{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003848 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003849}
3850
Olli Etuaho8a176262016-08-16 14:23:01 +03003851void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003852{
Jamie Madillacb4b812016-11-07 13:50:29 -05003853 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303854 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003855 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003856 }
3857
Arun Patole7e7e68d2015-05-22 12:02:25 +05303858 if (field.type()->getBasicType() != EbtStruct)
3859 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003860 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003861 }
3862
3863 // We're already inside a structure definition at this point, so add
3864 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303865 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3866 {
Jamie Madill41a49272014-03-18 16:10:13 -04003867 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003868 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3869 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003870 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003871 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003872 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003873 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003874}
3875
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003876//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003877// Parse an array index expression
3878//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003879TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3880 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303881 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003882{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003883 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3884 {
3885 if (baseExpression->getAsSymbolNode())
3886 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303887 error(location, " left of '[' is not of type array, matrix, or vector ",
3888 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003889 }
3890 else
3891 {
3892 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3893 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003894
Olli Etuaho3ec75682017-07-05 17:02:55 +03003895 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003896 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003897
Jiawei Shaod8105a02017-08-08 09:54:36 +08003898 if (baseExpression->getQualifier() == EvqPerVertexIn)
3899 {
3900 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
3901 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3902 {
3903 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3904 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3905 }
3906 }
3907
Jamie Madill21c1e452014-12-29 11:33:41 -05003908 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3909
Olli Etuaho36b05142015-11-12 13:10:42 +02003910 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3911 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3912 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3913 // index is a constant expression.
3914 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3915 {
3916 if (baseExpression->isInterfaceBlock())
3917 {
Jiawei Shaod8105a02017-08-08 09:54:36 +08003918 // TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
3919 switch (baseExpression->getQualifier())
3920 {
3921 case EvqPerVertexIn:
3922 break;
3923 case EvqUniform:
3924 case EvqBuffer:
3925 error(location,
3926 "array indexes for uniform block arrays and shader storage block arrays "
3927 "must be constant integral expressions",
3928 "[");
3929 break;
3930 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003931 // We can reach here only in error cases.
3932 ASSERT(mDiagnostics->numErrors() > 0);
3933 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003934 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003935 }
3936 else if (baseExpression->getQualifier() == EvqFragmentOut)
3937 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003938 error(location,
3939 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003940 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003941 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3942 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003943 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003944 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003945 }
3946
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003947 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003948 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003949 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3950 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3951 // constant fold expressions that are not constant expressions). The most compatible way to
3952 // handle this case is to report a warning instead of an error and force the index to be in
3953 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003954 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003955 int index = 0;
3956 if (indexConstantUnion->getBasicType() == EbtInt)
3957 {
3958 index = indexConstantUnion->getIConst(0);
3959 }
3960 else if (indexConstantUnion->getBasicType() == EbtUInt)
3961 {
3962 index = static_cast<int>(indexConstantUnion->getUConst(0));
3963 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003964
3965 int safeIndex = -1;
3966
3967 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003968 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003969 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003970 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003971 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003972 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003973 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003974 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003975 "GL_EXT_draw_buffers is disabled",
3976 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003977 safeIndex = 0;
3978 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003979 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003980 // Only do generic out-of-range check if similar error hasn't already been reported.
3981 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003982 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003983 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003984 baseExpression->getOutermostArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003985 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003986 }
3987 }
3988 else if (baseExpression->isMatrix())
3989 {
3990 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003991 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003992 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003993 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003994 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003995 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003996 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3997 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003998 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003999 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004000
4001 ASSERT(safeIndex >= 0);
4002 // Data of constant unions can't be changed, because it may be shared with other
4003 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4004 // sanitized object.
Olli Etuaho56229f12017-07-10 14:16:33 +03004005 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004006 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004007 TConstantUnion *safeConstantUnion = new TConstantUnion();
4008 safeConstantUnion->setIConst(safeIndex);
4009 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
Olli Etuaho56229f12017-07-10 14:16:33 +03004010 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004011 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004012
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004013 TIntermBinary *node = new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4014 node->setLine(location);
4015 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004016 }
Jamie Madill7164cf42013-07-08 13:30:59 -04004017 else
4018 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004019 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4020 node->setLine(location);
4021 // Indirect indexing can never be constant folded.
4022 return node;
Jamie Madill7164cf42013-07-08 13:30:59 -04004023 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004024}
4025
Olli Etuaho90892fb2016-07-14 14:44:51 +03004026int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
4027 const TSourceLoc &location,
4028 int index,
4029 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00004030 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004031{
4032 if (index >= arraySize || index < 0)
4033 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004034 std::stringstream reasonStream;
4035 reasonStream << reason << " '" << index << "'";
4036 std::string token = reasonStream.str();
4037 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03004038 if (index < 0)
4039 {
4040 return 0;
4041 }
4042 else
4043 {
4044 return arraySize - 1;
4045 }
4046 }
4047 return index;
4048}
4049
Jamie Madillb98c3a82015-07-23 14:26:04 -04004050TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4051 const TSourceLoc &dotLocation,
4052 const TString &fieldString,
4053 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004054{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004055 if (baseExpression->isArray())
4056 {
4057 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004058 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004059 }
4060
4061 if (baseExpression->isVector())
4062 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004063 TVector<int> fieldOffsets;
4064 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4065 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004066 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004067 fieldOffsets.resize(1);
4068 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004069 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004070 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4071 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004072
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004073 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004074 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004075 else if (baseExpression->getBasicType() == EbtStruct)
4076 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304077 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004078 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004079 {
4080 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004081 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004082 }
4083 else
4084 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004085 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004086 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004087 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004088 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004089 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004090 {
4091 fieldFound = true;
4092 break;
4093 }
4094 }
4095 if (fieldFound)
4096 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004097 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004098 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004099 TIntermBinary *node =
4100 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4101 node->setLine(dotLocation);
4102 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004103 }
4104 else
4105 {
4106 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004107 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004108 }
4109 }
4110 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004111 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004112 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304113 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004114 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004115 {
4116 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004117 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004118 }
4119 else
4120 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004121 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004122 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004123 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004124 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004125 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004126 {
4127 fieldFound = true;
4128 break;
4129 }
4130 }
4131 if (fieldFound)
4132 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004133 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004134 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004135 TIntermBinary *node =
4136 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4137 node->setLine(dotLocation);
4138 // Indexing interface blocks can never be constant folded.
4139 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004140 }
4141 else
4142 {
4143 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004144 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004145 }
4146 }
4147 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004148 else
4149 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004150 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004151 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004152 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304153 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004154 }
4155 else
4156 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304157 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004158 " field selection requires structure, vector, or interface block on left hand "
4159 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304160 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004161 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004162 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004163 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004164}
4165
Jamie Madillb98c3a82015-07-23 14:26:04 -04004166TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4167 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004168{
Martin Radev802abe02016-08-04 17:48:32 +03004169 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004170
4171 if (qualifierType == "shared")
4172 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004173 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004174 {
4175 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4176 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004177 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004178 }
4179 else if (qualifierType == "packed")
4180 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004181 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004182 {
4183 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4184 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004185 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004186 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004187 else if (qualifierType == "std430")
4188 {
4189 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4190 qualifier.blockStorage = EbsStd430;
4191 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004192 else if (qualifierType == "std140")
4193 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004194 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004195 }
4196 else if (qualifierType == "row_major")
4197 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004198 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004199 }
4200 else if (qualifierType == "column_major")
4201 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004202 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004203 }
4204 else if (qualifierType == "location")
4205 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004206 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4207 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004208 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004209 else if (qualifierType == "yuv" && isExtensionEnabled(TExtension::EXT_YUV_target) &&
Andrei Volykhina5527072017-03-22 16:46:30 +03004210 mShaderType == GL_FRAGMENT_SHADER)
4211 {
4212 qualifier.yuv = true;
4213 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004214 else if (qualifierType == "rgba32f")
4215 {
4216 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4217 qualifier.imageInternalFormat = EiifRGBA32F;
4218 }
4219 else if (qualifierType == "rgba16f")
4220 {
4221 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4222 qualifier.imageInternalFormat = EiifRGBA16F;
4223 }
4224 else if (qualifierType == "r32f")
4225 {
4226 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4227 qualifier.imageInternalFormat = EiifR32F;
4228 }
4229 else if (qualifierType == "rgba8")
4230 {
4231 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4232 qualifier.imageInternalFormat = EiifRGBA8;
4233 }
4234 else if (qualifierType == "rgba8_snorm")
4235 {
4236 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4237 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4238 }
4239 else if (qualifierType == "rgba32i")
4240 {
4241 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4242 qualifier.imageInternalFormat = EiifRGBA32I;
4243 }
4244 else if (qualifierType == "rgba16i")
4245 {
4246 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4247 qualifier.imageInternalFormat = EiifRGBA16I;
4248 }
4249 else if (qualifierType == "rgba8i")
4250 {
4251 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4252 qualifier.imageInternalFormat = EiifRGBA8I;
4253 }
4254 else if (qualifierType == "r32i")
4255 {
4256 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4257 qualifier.imageInternalFormat = EiifR32I;
4258 }
4259 else if (qualifierType == "rgba32ui")
4260 {
4261 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4262 qualifier.imageInternalFormat = EiifRGBA32UI;
4263 }
4264 else if (qualifierType == "rgba16ui")
4265 {
4266 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4267 qualifier.imageInternalFormat = EiifRGBA16UI;
4268 }
4269 else if (qualifierType == "rgba8ui")
4270 {
4271 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4272 qualifier.imageInternalFormat = EiifRGBA8UI;
4273 }
4274 else if (qualifierType == "r32ui")
4275 {
4276 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4277 qualifier.imageInternalFormat = EiifR32UI;
4278 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004279 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4280 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004281 {
4282 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4283 qualifier.primitiveType = EptPoints;
4284 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004285 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4286 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004287 {
4288 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4289 qualifier.primitiveType = EptLines;
4290 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004291 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4292 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004293 {
4294 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4295 qualifier.primitiveType = EptLinesAdjacency;
4296 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004297 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4298 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004299 {
4300 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4301 qualifier.primitiveType = EptTriangles;
4302 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004303 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4304 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004305 {
4306 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4307 qualifier.primitiveType = EptTrianglesAdjacency;
4308 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004309 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4310 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004311 {
4312 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4313 qualifier.primitiveType = EptLineStrip;
4314 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004315 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4316 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004317 {
4318 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4319 qualifier.primitiveType = EptTriangleStrip;
4320 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004321
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004322 else
4323 {
4324 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004325 }
4326
Jamie Madilla5efff92013-06-06 11:56:47 -04004327 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004328}
4329
Martin Radev802abe02016-08-04 17:48:32 +03004330void TParseContext::parseLocalSize(const TString &qualifierType,
4331 const TSourceLoc &qualifierTypeLine,
4332 int intValue,
4333 const TSourceLoc &intValueLine,
4334 const std::string &intValueString,
4335 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004336 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004337{
Olli Etuaho856c4972016-08-08 11:38:39 +03004338 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004339 if (intValue < 1)
4340 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004341 std::stringstream reasonStream;
4342 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4343 std::string reason = reasonStream.str();
4344 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004345 }
4346 (*localSize)[index] = intValue;
4347}
4348
Olli Etuaho09b04a22016-12-15 13:30:26 +00004349void TParseContext::parseNumViews(int intValue,
4350 const TSourceLoc &intValueLine,
4351 const std::string &intValueString,
4352 int *numViews)
4353{
4354 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4355 // specification.
4356 if (intValue < 1)
4357 {
4358 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4359 }
4360 *numViews = intValue;
4361}
4362
Shaob5cc1192017-07-06 10:47:20 +08004363void TParseContext::parseInvocations(int intValue,
4364 const TSourceLoc &intValueLine,
4365 const std::string &intValueString,
4366 int *numInvocations)
4367{
4368 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4369 // it doesn't make sense to accept invocations <= 0.
4370 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4371 {
4372 error(intValueLine,
4373 "out of range: invocations must be in the range of [1, "
4374 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4375 intValueString.c_str());
4376 }
4377 else
4378 {
4379 *numInvocations = intValue;
4380 }
4381}
4382
4383void TParseContext::parseMaxVertices(int intValue,
4384 const TSourceLoc &intValueLine,
4385 const std::string &intValueString,
4386 int *maxVertices)
4387{
4388 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4389 // it doesn't make sense to accept max_vertices < 0.
4390 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4391 {
4392 error(
4393 intValueLine,
4394 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4395 intValueString.c_str());
4396 }
4397 else
4398 {
4399 *maxVertices = intValue;
4400 }
4401}
4402
Jamie Madillb98c3a82015-07-23 14:26:04 -04004403TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4404 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004405 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304406 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004407{
Martin Radev802abe02016-08-04 17:48:32 +03004408 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004409
Martin Radev802abe02016-08-04 17:48:32 +03004410 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004411
Martin Radev802abe02016-08-04 17:48:32 +03004412 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004413 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004414 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004415 if (intValue < 0)
4416 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004417 error(intValueLine, "out of range: location must be non-negative",
4418 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004419 }
4420 else
4421 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004422 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004423 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004424 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004425 }
Olli Etuaho43364892017-02-13 16:00:12 +00004426 else if (qualifierType == "binding")
4427 {
4428 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4429 if (intValue < 0)
4430 {
4431 error(intValueLine, "out of range: binding must be non-negative",
4432 intValueString.c_str());
4433 }
4434 else
4435 {
4436 qualifier.binding = intValue;
4437 }
4438 }
jchen104cdac9e2017-05-08 11:01:20 +08004439 else if (qualifierType == "offset")
4440 {
4441 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4442 if (intValue < 0)
4443 {
4444 error(intValueLine, "out of range: offset must be non-negative",
4445 intValueString.c_str());
4446 }
4447 else
4448 {
4449 qualifier.offset = intValue;
4450 }
4451 }
Martin Radev802abe02016-08-04 17:48:32 +03004452 else if (qualifierType == "local_size_x")
4453 {
4454 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4455 &qualifier.localSize);
4456 }
4457 else if (qualifierType == "local_size_y")
4458 {
4459 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4460 &qualifier.localSize);
4461 }
4462 else if (qualifierType == "local_size_z")
4463 {
4464 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4465 &qualifier.localSize);
4466 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004467 else if (qualifierType == "num_views" && isExtensionEnabled(TExtension::OVR_multiview) &&
Olli Etuaho09b04a22016-12-15 13:30:26 +00004468 mShaderType == GL_VERTEX_SHADER)
4469 {
4470 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4471 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004472 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4473 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004474 {
4475 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4476 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004477 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4478 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004479 {
4480 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4481 }
4482
Martin Radev802abe02016-08-04 17:48:32 +03004483 else
4484 {
4485 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004486 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004487
Jamie Madilla5efff92013-06-06 11:56:47 -04004488 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004489}
4490
Olli Etuaho613b9592016-09-05 12:05:53 +03004491TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4492{
4493 return new TTypeQualifierBuilder(
4494 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4495 mShaderVersion);
4496}
4497
Olli Etuahocce89652017-06-19 16:04:09 +03004498TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4499 const TSourceLoc &loc)
4500{
4501 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4502 return new TStorageQualifierWrapper(qualifier, loc);
4503}
4504
4505TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4506{
4507 if (getShaderType() == GL_VERTEX_SHADER)
4508 {
4509 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4510 }
4511 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4512}
4513
4514TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4515{
4516 if (declaringFunction())
4517 {
4518 return new TStorageQualifierWrapper(EvqIn, loc);
4519 }
Shaob5cc1192017-07-06 10:47:20 +08004520
4521 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004522 {
Shaob5cc1192017-07-06 10:47:20 +08004523 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004524 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004525 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004526 {
4527 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4528 }
4529 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004530 }
Shaob5cc1192017-07-06 10:47:20 +08004531 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004532 {
Shaob5cc1192017-07-06 10:47:20 +08004533 if (mShaderVersion < 300)
4534 {
4535 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4536 }
4537 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004538 }
Shaob5cc1192017-07-06 10:47:20 +08004539 case GL_COMPUTE_SHADER:
4540 {
4541 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4542 }
4543 case GL_GEOMETRY_SHADER_OES:
4544 {
4545 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4546 }
4547 default:
4548 {
4549 UNREACHABLE();
4550 return new TStorageQualifierWrapper(EvqLast, loc);
4551 }
Olli Etuahocce89652017-06-19 16:04:09 +03004552 }
Olli Etuahocce89652017-06-19 16:04:09 +03004553}
4554
4555TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4556{
4557 if (declaringFunction())
4558 {
4559 return new TStorageQualifierWrapper(EvqOut, loc);
4560 }
Shaob5cc1192017-07-06 10:47:20 +08004561 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004562 {
Shaob5cc1192017-07-06 10:47:20 +08004563 case GL_VERTEX_SHADER:
4564 {
4565 if (mShaderVersion < 300)
4566 {
4567 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4568 }
4569 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4570 }
4571 case GL_FRAGMENT_SHADER:
4572 {
4573 if (mShaderVersion < 300)
4574 {
4575 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4576 }
4577 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4578 }
4579 case GL_COMPUTE_SHADER:
4580 {
4581 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4582 return new TStorageQualifierWrapper(EvqLast, loc);
4583 }
4584 case GL_GEOMETRY_SHADER_OES:
4585 {
4586 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4587 }
4588 default:
4589 {
4590 UNREACHABLE();
4591 return new TStorageQualifierWrapper(EvqLast, loc);
4592 }
Olli Etuahocce89652017-06-19 16:04:09 +03004593 }
Olli Etuahocce89652017-06-19 16:04:09 +03004594}
4595
4596TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4597{
4598 if (!declaringFunction())
4599 {
4600 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4601 }
4602 return new TStorageQualifierWrapper(EvqInOut, loc);
4603}
4604
Jamie Madillb98c3a82015-07-23 14:26:04 -04004605TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004606 TLayoutQualifier rightQualifier,
4607 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004608{
Martin Radevc28888b2016-07-22 15:27:42 +03004609 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004610 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004611}
4612
Olli Etuahocce89652017-06-19 16:04:09 +03004613TField *TParseContext::parseStructDeclarator(TString *identifier, const TSourceLoc &loc)
4614{
4615 checkIsNotReserved(loc, *identifier);
4616 TType *type = new TType(EbtVoid, EbpUndefined);
4617 return new TField(type, identifier, loc);
4618}
4619
4620TField *TParseContext::parseStructArrayDeclarator(TString *identifier,
4621 const TSourceLoc &loc,
Olli Etuaho4ddae352017-10-26 16:20:18 +03004622 unsigned int arraySize,
Olli Etuahocce89652017-06-19 16:04:09 +03004623 const TSourceLoc &arraySizeLoc)
4624{
4625 checkIsNotReserved(loc, *identifier);
4626
4627 TType *type = new TType(EbtVoid, EbpUndefined);
Olli Etuaho4ddae352017-10-26 16:20:18 +03004628 type->makeArray(arraySize);
Olli Etuahocce89652017-06-19 16:04:09 +03004629
4630 return new TField(type, identifier, loc);
4631}
4632
Olli Etuaho722bfb52017-10-26 17:00:11 +03004633void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4634 const TFieldList::const_iterator end,
4635 const TString &name,
4636 const TSourceLoc &location)
4637{
4638 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4639 {
4640 if ((*fieldIter)->name() == name)
4641 {
4642 error(location, "duplicate field name in structure", name.c_str());
4643 }
4644 }
4645}
4646
4647TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4648{
4649 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4650 ++fieldIter)
4651 {
4652 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4653 location);
4654 }
4655 return fields;
4656}
4657
Olli Etuaho4de340a2016-12-16 09:32:03 +00004658TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4659 const TFieldList *newlyAddedFields,
4660 const TSourceLoc &location)
4661{
4662 for (TField *field : *newlyAddedFields)
4663 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004664 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4665 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004666 processedFields->push_back(field);
4667 }
4668 return processedFields;
4669}
4670
Martin Radev70866b82016-07-22 15:27:42 +03004671TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4672 const TTypeQualifierBuilder &typeQualifierBuilder,
4673 TPublicType *typeSpecifier,
4674 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004675{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004676 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004677
Martin Radev70866b82016-07-22 15:27:42 +03004678 typeSpecifier->qualifier = typeQualifier.qualifier;
4679 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004680 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004681 typeSpecifier->invariant = typeQualifier.invariant;
4682 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304683 {
Martin Radev70866b82016-07-22 15:27:42 +03004684 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004685 }
Martin Radev70866b82016-07-22 15:27:42 +03004686 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004687}
4688
Jamie Madillb98c3a82015-07-23 14:26:04 -04004689TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004690 TFieldList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004691{
Martin Radev4a9cd802016-09-01 16:51:51 +03004692 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4693 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004694
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004695 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
4696 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004697
Martin Radev4a9cd802016-09-01 16:51:51 +03004698 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004699
Olli Etuaho55bde912017-10-25 13:41:13 +03004700 for (TField *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304701 {
Olli Etuaho55bde912017-10-25 13:41:13 +03004702 auto declaratorArraySizes = declarator->type()->getArraySizes();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004703 // don't allow arrays of arrays
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004704 if (!declaratorArraySizes.empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304705 {
Olli Etuahoe0803872017-08-23 15:30:23 +03004706 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004707 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004708
Olli Etuaho55bde912017-10-25 13:41:13 +03004709 TType *type = declarator->type();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004710 *type = TType(typeSpecifier);
4711 for (unsigned int arraySize : declaratorArraySizes)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304712 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004713 type->makeArray(arraySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004714 }
Olli Etuaho55bde912017-10-25 13:41:13 +03004715 checkIsNotUnsizedArray(typeSpecifier.getLine(),
4716 "array members of structs must specify a size",
4717 declarator->name().c_str(), type);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004718
Olli Etuaho55bde912017-10-25 13:41:13 +03004719 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *declarator);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004720 }
4721
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004722 return declaratorList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004723}
4724
Martin Radev4a9cd802016-09-01 16:51:51 +03004725TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4726 const TSourceLoc &nameLine,
4727 const TString *structName,
4728 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004729{
Olli Etuahoa5e693a2017-07-13 16:07:26 +03004730 TStructure *structure = new TStructure(&symbolTable, structName, fieldList);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004731
Jamie Madill9b820842015-02-12 10:40:10 -05004732 // Store a bool in the struct if we're at global scope, to allow us to
4733 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004734 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004735
Jamie Madill98493dd2013-07-08 14:39:03 -04004736 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004737 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004738 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004739 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304740 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004741 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004742 }
4743 }
4744
4745 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004746 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004747 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004748 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004749 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004750 switch (qualifier)
4751 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004752 case EvqGlobal:
4753 case EvqTemporary:
4754 break;
4755 default:
4756 error(field.line(), "invalid qualifier on struct member",
4757 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004758 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004759 }
Martin Radev70866b82016-07-22 15:27:42 +03004760 if (field.type()->isInvariant())
4761 {
4762 error(field.line(), "invalid qualifier on struct member", "invariant");
4763 }
jchen104cdac9e2017-05-08 11:01:20 +08004764 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4765 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004766 {
4767 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4768 }
4769
Olli Etuaho43364892017-02-13 16:00:12 +00004770 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4771
4772 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004773
4774 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004775 }
4776
Martin Radev4a9cd802016-09-01 16:51:51 +03004777 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004778 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004779 exitStructDeclaration();
4780
Martin Radev4a9cd802016-09-01 16:51:51 +03004781 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004782}
4783
Jamie Madillb98c3a82015-07-23 14:26:04 -04004784TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004785 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004786 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004787{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004788 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004789 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004790 init->isVector())
4791 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004792 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4793 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004794 return nullptr;
4795 }
4796
Olli Etuaho923ecef2017-10-11 12:01:38 +03004797 ASSERT(statementList);
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004798 if (!ValidateSwitchStatementList(switchType, mShaderVersion, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004799 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004800 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004801 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004802 }
4803
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004804 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4805 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004806 return node;
4807}
4808
4809TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4810{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004811 if (mSwitchNestingLevel == 0)
4812 {
4813 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004814 return nullptr;
4815 }
4816 if (condition == nullptr)
4817 {
4818 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004819 return nullptr;
4820 }
4821 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004822 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004823 {
4824 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004825 }
4826 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004827 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4828 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4829 // fold in case labels.
4830 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004831 {
4832 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004833 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004834 TIntermCase *node = new TIntermCase(condition);
4835 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004836 return node;
4837}
4838
4839TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4840{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004841 if (mSwitchNestingLevel == 0)
4842 {
4843 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004844 return nullptr;
4845 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004846 TIntermCase *node = new TIntermCase(nullptr);
4847 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004848 return node;
4849}
4850
Jamie Madillb98c3a82015-07-23 14:26:04 -04004851TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4852 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004853 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004854{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004855 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004856
4857 switch (op)
4858 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004859 case EOpLogicalNot:
4860 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4861 child->isVector())
4862 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004863 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004864 return nullptr;
4865 }
4866 break;
4867 case EOpBitwiseNot:
4868 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4869 child->isMatrix() || child->isArray())
4870 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004871 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004872 return nullptr;
4873 }
4874 break;
4875 case EOpPostIncrement:
4876 case EOpPreIncrement:
4877 case EOpPostDecrement:
4878 case EOpPreDecrement:
4879 case EOpNegative:
4880 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004881 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4882 child->getBasicType() == EbtBool || child->isArray() ||
4883 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004884 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004885 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004886 return nullptr;
4887 }
4888 // Operators for built-ins are already type checked against their prototype.
4889 default:
4890 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004891 }
4892
Jiajia Qinbc585152017-06-23 15:42:17 +08004893 if (child->getMemoryQualifier().writeonly)
4894 {
4895 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4896 return nullptr;
4897 }
4898
Olli Etuahof119a262016-08-19 15:54:22 +03004899 TIntermUnary *node = new TIntermUnary(op, child);
4900 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004901
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004902 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004903}
4904
Olli Etuaho09b22472015-02-11 11:47:26 +02004905TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4906{
Olli Etuahocce89652017-06-19 16:04:09 +03004907 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004908 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004909 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004910 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004911 return child;
4912 }
4913 return node;
4914}
4915
Jamie Madillb98c3a82015-07-23 14:26:04 -04004916TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4917 TIntermTyped *child,
4918 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004919{
Olli Etuaho856c4972016-08-08 11:38:39 +03004920 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004921 return addUnaryMath(op, child, loc);
4922}
4923
Jamie Madillb98c3a82015-07-23 14:26:04 -04004924bool TParseContext::binaryOpCommonCheck(TOperator op,
4925 TIntermTyped *left,
4926 TIntermTyped *right,
4927 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004928{
jchen10b4cf5652017-05-05 18:51:17 +08004929 // Check opaque types are not allowed to be operands in expressions other than array indexing
4930 // and structure member selection.
4931 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4932 {
4933 switch (op)
4934 {
4935 case EOpIndexDirect:
4936 case EOpIndexIndirect:
4937 break;
4938 case EOpIndexDirectStruct:
4939 UNREACHABLE();
4940
4941 default:
4942 error(loc, "Invalid operation for variables with an opaque type",
4943 GetOperatorString(op));
4944 return false;
4945 }
4946 }
jchen10cc2a10e2017-05-03 14:05:12 +08004947
Jiajia Qinbc585152017-06-23 15:42:17 +08004948 if (right->getMemoryQualifier().writeonly)
4949 {
4950 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4951 return false;
4952 }
4953
4954 if (left->getMemoryQualifier().writeonly)
4955 {
4956 switch (op)
4957 {
4958 case EOpAssign:
4959 case EOpInitialize:
4960 case EOpIndexDirect:
4961 case EOpIndexIndirect:
4962 case EOpIndexDirectStruct:
4963 case EOpIndexDirectInterfaceBlock:
4964 break;
4965 default:
4966 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4967 return false;
4968 }
4969 }
4970
Olli Etuaho244be012016-08-18 15:26:02 +03004971 if (left->getType().getStruct() || right->getType().getStruct())
4972 {
4973 switch (op)
4974 {
4975 case EOpIndexDirectStruct:
4976 ASSERT(left->getType().getStruct());
4977 break;
4978 case EOpEqual:
4979 case EOpNotEqual:
4980 case EOpAssign:
4981 case EOpInitialize:
4982 if (left->getType() != right->getType())
4983 {
4984 return false;
4985 }
4986 break;
4987 default:
4988 error(loc, "Invalid operation for structs", GetOperatorString(op));
4989 return false;
4990 }
4991 }
4992
Olli Etuaho94050052017-05-08 14:17:44 +03004993 if (left->isInterfaceBlock() || right->isInterfaceBlock())
4994 {
4995 switch (op)
4996 {
4997 case EOpIndexDirectInterfaceBlock:
4998 ASSERT(left->getType().getInterfaceBlock());
4999 break;
5000 default:
5001 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5002 return false;
5003 }
5004 }
5005
Olli Etuahod6b14282015-03-17 14:31:35 +02005006 if (left->isArray() || right->isArray())
5007 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005008 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005009 {
5010 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5011 return false;
5012 }
5013
5014 if (left->isArray() != right->isArray())
5015 {
5016 error(loc, "array / non-array mismatch", GetOperatorString(op));
5017 return false;
5018 }
5019
5020 switch (op)
5021 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005022 case EOpEqual:
5023 case EOpNotEqual:
5024 case EOpAssign:
5025 case EOpInitialize:
5026 break;
5027 default:
5028 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5029 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005030 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005031 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03005032 if (left->getType().getArraySizes() != right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005033 {
5034 error(loc, "array size mismatch", GetOperatorString(op));
5035 return false;
5036 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005037 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005038
5039 // Check ops which require integer / ivec parameters
5040 bool isBitShift = false;
5041 switch (op)
5042 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005043 case EOpBitShiftLeft:
5044 case EOpBitShiftRight:
5045 case EOpBitShiftLeftAssign:
5046 case EOpBitShiftRightAssign:
5047 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5048 // check that the basic type is an integer type.
5049 isBitShift = true;
5050 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5051 {
5052 return false;
5053 }
5054 break;
5055 case EOpBitwiseAnd:
5056 case EOpBitwiseXor:
5057 case EOpBitwiseOr:
5058 case EOpBitwiseAndAssign:
5059 case EOpBitwiseXorAssign:
5060 case EOpBitwiseOrAssign:
5061 // It is enough to check the type of only one operand, since later it
5062 // is checked that the operand types match.
5063 if (!IsInteger(left->getBasicType()))
5064 {
5065 return false;
5066 }
5067 break;
5068 default:
5069 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005070 }
5071
5072 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5073 // So the basic type should usually match.
5074 if (!isBitShift && left->getBasicType() != right->getBasicType())
5075 {
5076 return false;
5077 }
5078
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005079 // Check that:
5080 // 1. Type sizes match exactly on ops that require that.
5081 // 2. Restrictions for structs that contain arrays or samplers are respected.
5082 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005083 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005084 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005085 case EOpAssign:
5086 case EOpInitialize:
5087 case EOpEqual:
5088 case EOpNotEqual:
5089 // ESSL 1.00 sections 5.7, 5.8, 5.9
5090 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5091 {
5092 error(loc, "undefined operation for structs containing arrays",
5093 GetOperatorString(op));
5094 return false;
5095 }
5096 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5097 // we interpret the spec so that this extends to structs containing samplers,
5098 // similarly to ESSL 1.00 spec.
5099 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5100 left->getType().isStructureContainingSamplers())
5101 {
5102 error(loc, "undefined operation for structs containing samplers",
5103 GetOperatorString(op));
5104 return false;
5105 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005106
Olli Etuahoe1805592017-01-02 16:41:20 +00005107 if ((left->getNominalSize() != right->getNominalSize()) ||
5108 (left->getSecondarySize() != right->getSecondarySize()))
5109 {
5110 error(loc, "dimension mismatch", GetOperatorString(op));
5111 return false;
5112 }
5113 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005114 case EOpLessThan:
5115 case EOpGreaterThan:
5116 case EOpLessThanEqual:
5117 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005118 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005119 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005120 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005121 return false;
5122 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005123 break;
5124 case EOpAdd:
5125 case EOpSub:
5126 case EOpDiv:
5127 case EOpIMod:
5128 case EOpBitShiftLeft:
5129 case EOpBitShiftRight:
5130 case EOpBitwiseAnd:
5131 case EOpBitwiseXor:
5132 case EOpBitwiseOr:
5133 case EOpAddAssign:
5134 case EOpSubAssign:
5135 case EOpDivAssign:
5136 case EOpIModAssign:
5137 case EOpBitShiftLeftAssign:
5138 case EOpBitShiftRightAssign:
5139 case EOpBitwiseAndAssign:
5140 case EOpBitwiseXorAssign:
5141 case EOpBitwiseOrAssign:
5142 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5143 {
5144 return false;
5145 }
5146
5147 // Are the sizes compatible?
5148 if (left->getNominalSize() != right->getNominalSize() ||
5149 left->getSecondarySize() != right->getSecondarySize())
5150 {
5151 // If the nominal sizes of operands do not match:
5152 // One of them must be a scalar.
5153 if (!left->isScalar() && !right->isScalar())
5154 return false;
5155
5156 // In the case of compound assignment other than multiply-assign,
5157 // the right side needs to be a scalar. Otherwise a vector/matrix
5158 // would be assigned to a scalar. A scalar can't be shifted by a
5159 // vector either.
5160 if (!right->isScalar() &&
5161 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5162 return false;
5163 }
5164 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005165 default:
5166 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005167 }
5168
Olli Etuahod6b14282015-03-17 14:31:35 +02005169 return true;
5170}
5171
Olli Etuaho1dded802016-08-18 18:13:13 +03005172bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5173 const TType &left,
5174 const TType &right)
5175{
5176 switch (op)
5177 {
5178 case EOpMul:
5179 case EOpMulAssign:
5180 return left.getNominalSize() == right.getNominalSize() &&
5181 left.getSecondarySize() == right.getSecondarySize();
5182 case EOpVectorTimesScalar:
5183 return true;
5184 case EOpVectorTimesScalarAssign:
5185 ASSERT(!left.isMatrix() && !right.isMatrix());
5186 return left.isVector() && !right.isVector();
5187 case EOpVectorTimesMatrix:
5188 return left.getNominalSize() == right.getRows();
5189 case EOpVectorTimesMatrixAssign:
5190 ASSERT(!left.isMatrix() && right.isMatrix());
5191 return left.isVector() && left.getNominalSize() == right.getRows() &&
5192 left.getNominalSize() == right.getCols();
5193 case EOpMatrixTimesVector:
5194 return left.getCols() == right.getNominalSize();
5195 case EOpMatrixTimesScalar:
5196 return true;
5197 case EOpMatrixTimesScalarAssign:
5198 ASSERT(left.isMatrix() && !right.isMatrix());
5199 return !right.isVector();
5200 case EOpMatrixTimesMatrix:
5201 return left.getCols() == right.getRows();
5202 case EOpMatrixTimesMatrixAssign:
5203 ASSERT(left.isMatrix() && right.isMatrix());
5204 // We need to check two things:
5205 // 1. The matrix multiplication step is valid.
5206 // 2. The result will have the same number of columns as the lvalue.
5207 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5208
5209 default:
5210 UNREACHABLE();
5211 return false;
5212 }
5213}
5214
Jamie Madillb98c3a82015-07-23 14:26:04 -04005215TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5216 TIntermTyped *left,
5217 TIntermTyped *right,
5218 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005219{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005220 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005221 return nullptr;
5222
Olli Etuahofc1806e2015-03-17 13:03:11 +02005223 switch (op)
5224 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005225 case EOpEqual:
5226 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005227 case EOpLessThan:
5228 case EOpGreaterThan:
5229 case EOpLessThanEqual:
5230 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005231 break;
5232 case EOpLogicalOr:
5233 case EOpLogicalXor:
5234 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005235 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5236 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005237 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005238 {
5239 return nullptr;
5240 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005241 // Basic types matching should have been already checked.
5242 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005243 break;
5244 case EOpAdd:
5245 case EOpSub:
5246 case EOpDiv:
5247 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005248 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5249 !right->getType().getStruct());
5250 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005251 {
5252 return nullptr;
5253 }
5254 break;
5255 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005256 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5257 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005258 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005259 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005260 {
5261 return nullptr;
5262 }
5263 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005264 default:
5265 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005266 }
5267
Olli Etuaho1dded802016-08-18 18:13:13 +03005268 if (op == EOpMul)
5269 {
5270 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5271 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5272 {
5273 return nullptr;
5274 }
5275 }
5276
Olli Etuaho3fdec912016-08-18 15:08:06 +03005277 TIntermBinary *node = new TIntermBinary(op, left, right);
5278 node->setLine(loc);
5279
Olli Etuaho3fdec912016-08-18 15:08:06 +03005280 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005281 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005282}
5283
Jamie Madillb98c3a82015-07-23 14:26:04 -04005284TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5285 TIntermTyped *left,
5286 TIntermTyped *right,
5287 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005288{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005289 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005290 if (node == 0)
5291 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005292 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5293 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005294 return left;
5295 }
5296 return node;
5297}
5298
Jamie Madillb98c3a82015-07-23 14:26:04 -04005299TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5300 TIntermTyped *left,
5301 TIntermTyped *right,
5302 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005303{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005304 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005305 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005306 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005307 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5308 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005309 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005310 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005311 }
5312 return node;
5313}
5314
Olli Etuaho13389b62016-10-16 11:48:18 +01005315TIntermBinary *TParseContext::createAssign(TOperator op,
5316 TIntermTyped *left,
5317 TIntermTyped *right,
5318 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005319{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005320 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005321 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005322 if (op == EOpMulAssign)
5323 {
5324 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5325 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5326 {
5327 return nullptr;
5328 }
5329 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005330 TIntermBinary *node = new TIntermBinary(op, left, right);
5331 node->setLine(loc);
5332
Olli Etuaho3fdec912016-08-18 15:08:06 +03005333 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005334 }
5335 return nullptr;
5336}
5337
Jamie Madillb98c3a82015-07-23 14:26:04 -04005338TIntermTyped *TParseContext::addAssign(TOperator op,
5339 TIntermTyped *left,
5340 TIntermTyped *right,
5341 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005342{
Olli Etuahocce89652017-06-19 16:04:09 +03005343 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005344 TIntermTyped *node = createAssign(op, left, right, loc);
5345 if (node == nullptr)
5346 {
5347 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005348 return left;
5349 }
5350 return node;
5351}
5352
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005353TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5354 TIntermTyped *right,
5355 const TSourceLoc &loc)
5356{
Corentin Wallez0d959252016-07-12 17:26:32 -04005357 // WebGL2 section 5.26, the following results in an error:
5358 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005359 if (mShaderSpec == SH_WEBGL2_SPEC &&
5360 (left->isArray() || left->getBasicType() == EbtVoid ||
5361 left->getType().isStructureContainingArrays() || right->isArray() ||
5362 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005363 {
5364 error(loc,
5365 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5366 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005367 }
5368
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005369 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5370 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5371 commaNode->getTypePointer()->setQualifier(resultQualifier);
5372 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005373}
5374
Olli Etuaho49300862015-02-20 14:54:49 +02005375TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5376{
5377 switch (op)
5378 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005379 case EOpContinue:
5380 if (mLoopNestingLevel <= 0)
5381 {
5382 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005383 }
5384 break;
5385 case EOpBreak:
5386 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5387 {
5388 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005389 }
5390 break;
5391 case EOpReturn:
5392 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5393 {
5394 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005395 }
5396 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005397 case EOpKill:
5398 if (mShaderType != GL_FRAGMENT_SHADER)
5399 {
5400 error(loc, "discard supported in fragment shaders only", "discard");
5401 }
5402 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005403 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005404 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005405 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005406 }
Olli Etuahocce89652017-06-19 16:04:09 +03005407 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005408}
5409
Jamie Madillb98c3a82015-07-23 14:26:04 -04005410TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005411 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005412 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005413{
Olli Etuahocce89652017-06-19 16:04:09 +03005414 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005415 {
Olli Etuahocce89652017-06-19 16:04:09 +03005416 ASSERT(op == EOpReturn);
5417 mFunctionReturnsValue = true;
5418 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5419 {
5420 error(loc, "void function cannot return a value", "return");
5421 }
5422 else if (*mCurrentFunctionType != expression->getType())
5423 {
5424 error(loc, "function return is not matching type:", "return");
5425 }
Olli Etuaho49300862015-02-20 14:54:49 +02005426 }
Olli Etuahocce89652017-06-19 16:04:09 +03005427 TIntermBranch *node = new TIntermBranch(op, expression);
5428 node->setLine(loc);
5429 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005430}
5431
Martin Radev84aa2dc2017-09-11 15:51:02 +03005432void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5433{
5434 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
5435 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5436 bool isTextureGather = (name == "textureGather");
5437 bool isTextureGatherOffset = (name == "textureGatherOffset");
5438 if (isTextureGather || isTextureGatherOffset)
5439 {
5440 TIntermNode *componentNode = nullptr;
5441 TIntermSequence *arguments = functionCall->getSequence();
5442 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5443 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5444 ASSERT(sampler != nullptr);
5445 switch (sampler->getBasicType())
5446 {
5447 case EbtSampler2D:
5448 case EbtISampler2D:
5449 case EbtUSampler2D:
5450 case EbtSampler2DArray:
5451 case EbtISampler2DArray:
5452 case EbtUSampler2DArray:
5453 if ((isTextureGather && arguments->size() == 3u) ||
5454 (isTextureGatherOffset && arguments->size() == 4u))
5455 {
5456 componentNode = arguments->back();
5457 }
5458 break;
5459 case EbtSamplerCube:
5460 case EbtISamplerCube:
5461 case EbtUSamplerCube:
5462 ASSERT(!isTextureGatherOffset);
5463 if (arguments->size() == 3u)
5464 {
5465 componentNode = arguments->back();
5466 }
5467 break;
5468 case EbtSampler2DShadow:
5469 case EbtSampler2DArrayShadow:
5470 case EbtSamplerCubeShadow:
5471 break;
5472 default:
5473 UNREACHABLE();
5474 break;
5475 }
5476 if (componentNode)
5477 {
5478 const TIntermConstantUnion *componentConstantUnion =
5479 componentNode->getAsConstantUnion();
5480 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5481 {
5482 error(functionCall->getLine(), "Texture component must be a constant expression",
5483 name.c_str());
5484 }
5485 else
5486 {
5487 int component = componentConstantUnion->getIConst(0);
5488 if (component < 0 || component > 3)
5489 {
5490 error(functionCall->getLine(), "Component must be in the range [0;3]",
5491 name.c_str());
5492 }
5493 }
5494 }
5495 }
5496}
5497
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005498void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5499{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005500 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01005501 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005502 TIntermNode *offset = nullptr;
5503 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005504 bool useTextureGatherOffsetConstraints = false;
Olli Etuahoec9232b2017-03-27 17:01:37 +03005505 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5506 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5507 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005508 {
5509 offset = arguments->back();
5510 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005511 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005512 {
5513 // A bias parameter might follow the offset parameter.
5514 ASSERT(arguments->size() >= 3);
5515 offset = (*arguments)[2];
5516 }
Martin Radev84aa2dc2017-09-11 15:51:02 +03005517 else if (name == "textureGatherOffset")
5518 {
5519 ASSERT(arguments->size() >= 3u);
5520 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5521 ASSERT(sampler != nullptr);
5522 switch (sampler->getBasicType())
5523 {
5524 case EbtSampler2D:
5525 case EbtISampler2D:
5526 case EbtUSampler2D:
5527 case EbtSampler2DArray:
5528 case EbtISampler2DArray:
5529 case EbtUSampler2DArray:
5530 offset = (*arguments)[2];
5531 break;
5532 case EbtSampler2DShadow:
5533 case EbtSampler2DArrayShadow:
5534 offset = (*arguments)[3];
5535 break;
5536 default:
5537 UNREACHABLE();
5538 break;
5539 }
5540 useTextureGatherOffsetConstraints = true;
5541 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005542 if (offset != nullptr)
5543 {
5544 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5545 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5546 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005547 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005548 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005549 }
5550 else
5551 {
5552 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5553 size_t size = offsetConstantUnion->getType().getObjectSize();
5554 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005555 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5556 : mMinProgramTexelOffset;
5557 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5558 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005559 for (size_t i = 0u; i < size; ++i)
5560 {
5561 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005562 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005563 {
5564 std::stringstream tokenStream;
5565 tokenStream << offsetValue;
5566 std::string token = tokenStream.str();
5567 error(offset->getLine(), "Texture offset value out of valid range",
5568 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005569 }
5570 }
5571 }
5572 }
5573}
5574
Martin Radev2cc85b32016-08-05 16:22:53 +03005575// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5576void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5577{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005578 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005579 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5580
5581 if (name.compare(0, 5, "image") == 0)
5582 {
5583 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005584 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005585
Olli Etuaho485eefd2017-02-14 17:40:06 +00005586 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005587
5588 if (name.compare(5, 5, "Store") == 0)
5589 {
5590 if (memoryQualifier.readonly)
5591 {
5592 error(imageNode->getLine(),
5593 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005594 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005595 }
5596 }
5597 else if (name.compare(5, 4, "Load") == 0)
5598 {
5599 if (memoryQualifier.writeonly)
5600 {
5601 error(imageNode->getLine(),
5602 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005603 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005604 }
5605 }
5606 }
5607}
5608
5609// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5610void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5611 const TFunction *functionDefinition,
5612 const TIntermAggregate *functionCall)
5613{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005614 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005615
5616 const TIntermSequence &arguments = *functionCall->getSequence();
5617
5618 ASSERT(functionDefinition->getParamCount() == arguments.size());
5619
5620 for (size_t i = 0; i < arguments.size(); ++i)
5621 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005622 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5623 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005624 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5625 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5626
5627 if (IsImage(functionArgumentType.getBasicType()))
5628 {
5629 const TMemoryQualifier &functionArgumentMemoryQualifier =
5630 functionArgumentType.getMemoryQualifier();
5631 const TMemoryQualifier &functionParameterMemoryQualifier =
5632 functionParameterType.getMemoryQualifier();
5633 if (functionArgumentMemoryQualifier.readonly &&
5634 !functionParameterMemoryQualifier.readonly)
5635 {
5636 error(functionCall->getLine(),
5637 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005638 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005639 }
5640
5641 if (functionArgumentMemoryQualifier.writeonly &&
5642 !functionParameterMemoryQualifier.writeonly)
5643 {
5644 error(functionCall->getLine(),
5645 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005646 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005647 }
Martin Radev049edfa2016-11-11 14:35:37 +02005648
5649 if (functionArgumentMemoryQualifier.coherent &&
5650 !functionParameterMemoryQualifier.coherent)
5651 {
5652 error(functionCall->getLine(),
5653 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005654 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005655 }
5656
5657 if (functionArgumentMemoryQualifier.volatileQualifier &&
5658 !functionParameterMemoryQualifier.volatileQualifier)
5659 {
5660 error(functionCall->getLine(),
5661 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005662 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005663 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005664 }
5665 }
5666}
5667
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005668TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005669{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005670 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005671}
5672
5673TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005674 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005675 TIntermNode *thisNode,
5676 const TSourceLoc &loc)
5677{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005678 if (thisNode != nullptr)
5679 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005680 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005681 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005682
5683 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005684 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005685 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005686 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005687 }
5688 else
5689 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005690 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005691 return addNonConstructorFunctionCall(fnCall, arguments, loc);
5692 }
5693}
5694
5695TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
5696 TIntermSequence *arguments,
5697 TIntermNode *thisNode,
5698 const TSourceLoc &loc)
5699{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005700 TIntermTyped *typedThis = thisNode->getAsTyped();
5701 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5702 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5703 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
5704 // So accessing fnCall->getName() below is safe.
5705 if (fnCall->getName() != "length")
5706 {
5707 error(loc, "invalid method", fnCall->getName().c_str());
5708 }
5709 else if (!arguments->empty())
5710 {
5711 error(loc, "method takes no parameters", "length");
5712 }
5713 else if (typedThis == nullptr || !typedThis->isArray())
5714 {
5715 error(loc, "length can only be called on arrays", "length");
5716 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005717 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5718 mGeometryShaderInputPrimitiveType == EptUndefined)
5719 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08005720 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005721 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5722 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005723 else
5724 {
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005725 TIntermUnary *node = new TIntermUnary(EOpArrayLength, typedThis);
5726 node->setLine(loc);
5727 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005728 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005729 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005730}
5731
5732TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
5733 TIntermSequence *arguments,
5734 const TSourceLoc &loc)
5735{
5736 // First find by unmangled name to check whether the function name has been
5737 // hidden by a variable name or struct typename.
5738 // If a function is found, check for one with a matching argument list.
5739 bool builtIn;
5740 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
5741 if (symbol != nullptr && !symbol->isFunction())
5742 {
5743 error(loc, "function name expected", fnCall->getName().c_str());
5744 }
5745 else
5746 {
5747 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
5748 mShaderVersion, &builtIn);
5749 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005750 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005751 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
5752 }
5753 else
5754 {
5755 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005756 //
5757 // A declared function.
5758 //
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03005759 if (builtIn && fnCandidate->getExtension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005760 {
Olli Etuaho856c4972016-08-08 11:38:39 +03005761 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005762 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005763 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005764 if (builtIn && op != EOpNull)
5765 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005766 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005767 if (fnCandidate->getParamCount() == 1)
5768 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005769 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005770 TIntermNode *unaryParamNode = arguments->front();
5771 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005772 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005773 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005774 }
5775 else
5776 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005777 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005778 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005779 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005780
5781 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005782 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305783
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005784 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305785 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005786 // See if we can constant fold a built-in. Note that this may be possible
5787 // even if it is not const-qualified.
5788 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305789 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005790 else
5791 {
5792 return callNode;
5793 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005794 }
5795 }
5796 else
5797 {
5798 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005799 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005800
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005801 // If builtIn == false, the function is user defined - could be an overloaded
5802 // built-in as well.
5803 // if builtIn == true, it's a builtIn function with no op associated with it.
5804 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005805 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005806 {
Olli Etuahofe486322017-03-21 09:30:54 +00005807 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005808 checkTextureOffsetConst(callNode);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005809 checkTextureGather(callNode);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005810 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005811 }
5812 else
5813 {
Olli Etuahofe486322017-03-21 09:30:54 +00005814 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005815 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005816 }
5817
Jiajia Qinbc585152017-06-23 15:42:17 +08005818 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005819
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005820 callNode->setLine(loc);
5821
5822 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005823 }
5824 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005825 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005826
5827 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005828 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005829}
5830
Jamie Madillb98c3a82015-07-23 14:26:04 -04005831TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005832 TIntermTyped *trueExpression,
5833 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005834 const TSourceLoc &loc)
5835{
Olli Etuaho56229f12017-07-10 14:16:33 +03005836 if (!checkIsScalarBool(loc, cond))
5837 {
5838 return falseExpression;
5839 }
Olli Etuaho52901742015-04-15 13:42:45 +03005840
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005841 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005842 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005843 std::stringstream reasonStream;
5844 reasonStream << "mismatching ternary operator operand types '"
5845 << trueExpression->getCompleteString() << " and '"
5846 << falseExpression->getCompleteString() << "'";
5847 std::string reason = reasonStream.str();
5848 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005849 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005850 }
Olli Etuahode318b22016-10-25 16:18:25 +01005851 if (IsOpaqueType(trueExpression->getBasicType()))
5852 {
5853 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005854 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005855 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5856 // Note that structs containing opaque types don't need to be checked as structs are
5857 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005858 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005859 return falseExpression;
5860 }
5861
Jiajia Qinbc585152017-06-23 15:42:17 +08005862 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5863 falseExpression->getMemoryQualifier().writeonly)
5864 {
5865 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5866 return falseExpression;
5867 }
5868
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005869 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005870 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005871 // ESSL 3.00.6 section 5.7:
5872 // Ternary operator support is optional for arrays. No certainty that it works across all
5873 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5874 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005875 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005876 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005877 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005878 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005879 }
Olli Etuaho94050052017-05-08 14:17:44 +03005880 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5881 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005882 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005883 return falseExpression;
5884 }
5885
Corentin Wallez0d959252016-07-12 17:26:32 -04005886 // WebGL2 section 5.26, the following results in an error:
5887 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005888 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005889 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005890 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005891 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005892 }
5893
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005894 // Note that the node resulting from here can be a constant union without being qualified as
5895 // constant.
5896 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5897 node->setLine(loc);
5898
5899 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005900}
Olli Etuaho49300862015-02-20 14:54:49 +02005901
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005902//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005903// Parse an array of strings using yyparse.
5904//
5905// Returns 0 for success.
5906//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005907int PaParseStrings(size_t count,
5908 const char *const string[],
5909 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305910 TParseContext *context)
5911{
Yunchao He4f285442017-04-21 12:15:49 +08005912 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005913 return 1;
5914
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005915 if (glslang_initialize(context))
5916 return 1;
5917
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005918 int error = glslang_scan(count, string, length, context);
5919 if (!error)
5920 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005921
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005922 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005923
alokp@chromium.org6b495712012-06-29 00:06:58 +00005924 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005925}
Jamie Madill45bcc782016-11-07 13:58:48 -05005926
5927} // namespace sh