blob: be0a15670d6a17288e2c17cd49531b619be0158f [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capens16004fc2014-06-11 11:29:11 -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//
Geoff Lang0a73dd82014-11-19 16:18:08 -05006#ifndef COMPILER_TRANSLATOR_PARSECONTEXT_H_
7#define COMPILER_TRANSLATOR_PARSECONTEXT_H_
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008
Jamie Madilld4a3a312014-06-25 16:04:56 -04009#include "compiler/translator/Compiler.h"
Geoff Lang17732822013-08-29 13:46:49 -040010#include "compiler/translator/Diagnostics.h"
11#include "compiler/translator/DirectiveHandler.h"
Jamie Madillb1a85f42014-08-19 15:23:24 -040012#include "compiler/translator/Intermediate.h"
Geoff Lang17732822013-08-29 13:46:49 -040013#include "compiler/translator/SymbolTable.h"
Martin Radev70866b82016-07-22 15:27:42 +030014#include "compiler/translator/QualifierTypes.h"
daniel@transgaming.comb401a922012-10-26 18:58:24 +000015#include "compiler/preprocessor/Preprocessor.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000016
Jamie Madill45bcc782016-11-07 13:58:48 -050017namespace sh
18{
19
Jamie Madill06145232015-05-13 13:10:01 -040020struct TMatrixFields
21{
daniel@transgaming.com0578f812010-05-17 09:58:39 +000022 bool wholeRow;
23 bool wholeCol;
24 int row;
25 int col;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000026};
27
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000028//
29// The following are extra variables needed during parsing, grouped together so
30// they can be passed to the parser without needing a global.
31//
Jamie Madill6e06b1f2015-05-14 10:01:17 -040032class TParseContext : angle::NonCopyable
Jamie Madill06145232015-05-13 13:10:01 -040033{
Jamie Madill6e06b1f2015-05-14 10:01:17 -040034 public:
Jamie Madill06145232015-05-13 13:10:01 -040035 TParseContext(TSymbolTable &symt,
36 TExtensionBehavior &ext,
Jamie Madill06145232015-05-13 13:10:01 -040037 sh::GLenum type,
38 ShShaderSpec spec,
Qiankun Miao7ebb97f2016-09-08 18:01:50 +080039 ShCompileOptions options,
Jamie Madill06145232015-05-13 13:10:01 -040040 bool checksPrecErrors,
Olli Etuaho77ba4082016-12-16 12:01:18 +000041 TDiagnostics *diagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -050042 const ShBuiltInResources &resources);
jchen104cdac9e2017-05-08 11:01:20 +080043 ~TParseContext();
Jamie Madill06145232015-05-13 13:10:01 -040044
Jamie Madill6e06b1f2015-05-14 10:01:17 -040045 const pp::Preprocessor &getPreprocessor() const { return mPreprocessor; }
46 pp::Preprocessor &getPreprocessor() { return mPreprocessor; }
47 void *getScanner() const { return mScanner; }
48 void setScanner(void *scanner) { mScanner = scanner; }
49 int getShaderVersion() const { return mShaderVersion; }
50 sh::GLenum getShaderType() const { return mShaderType; }
51 ShShaderSpec getShaderSpec() const { return mShaderSpec; }
Olli Etuaho77ba4082016-12-16 12:01:18 +000052 int numErrors() const { return mDiagnostics->numErrors(); }
Olli Etuaho4de340a2016-12-16 09:32:03 +000053 void error(const TSourceLoc &loc, const char *reason, const char *token);
54 void warning(const TSourceLoc &loc, const char *reason, const char *token);
Jamie Madill14e95b32015-05-07 10:10:41 -040055
Olli Etuaho7c3848e2015-11-04 13:19:17 +020056 // If isError is false, a warning will be reported instead.
57 void outOfRangeError(bool isError,
58 const TSourceLoc &loc,
59 const char *reason,
Olli Etuaho4de340a2016-12-16 09:32:03 +000060 const char *token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +020061
Olli Etuaho6d40bbd2016-09-30 13:49:38 +010062 TIntermBlock *getTreeRoot() const { return mTreeRoot; }
63 void setTreeRoot(TIntermBlock *treeRoot) { mTreeRoot = treeRoot; }
Jamie Madill6e06b1f2015-05-14 10:01:17 -040064
Olli Etuahoa6996682015-10-12 14:32:30 +030065 bool getFragmentPrecisionHigh() const
Jamie Madill6e06b1f2015-05-14 10:01:17 -040066 {
Olli Etuahoa6996682015-10-12 14:32:30 +030067 return mFragmentPrecisionHighOnESSL1 || mShaderVersion >= 300;
68 }
69 void setFragmentPrecisionHighOnESSL1(bool fragmentPrecisionHigh)
70 {
71 mFragmentPrecisionHighOnESSL1 = fragmentPrecisionHigh;
Jamie Madill6e06b1f2015-05-14 10:01:17 -040072 }
73
Jamie Madilld7b1ab52016-12-12 14:42:19 -050074 void setLoopNestingLevel(int loopNestintLevel) { mLoopNestingLevel = loopNestintLevel; }
Jamie Madill6e06b1f2015-05-14 10:01:17 -040075
Jamie Madill6e06b1f2015-05-14 10:01:17 -040076 void incrLoopNestingLevel() { ++mLoopNestingLevel; }
77 void decrLoopNestingLevel() { --mLoopNestingLevel; }
78
79 void incrSwitchNestingLevel() { ++mSwitchNestingLevel; }
80 void decrSwitchNestingLevel() { --mSwitchNestingLevel; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000081
Martin Radev802abe02016-08-04 17:48:32 +030082 bool isComputeShaderLocalSizeDeclared() const { return mComputeShaderLocalSizeDeclared; }
Martin Radev4c4c8e72016-08-04 12:25:34 +030083 sh::WorkGroupSize getComputeShaderLocalSize() const;
Martin Radev802abe02016-08-04 17:48:32 +030084
Olli Etuaho09b04a22016-12-15 13:30:26 +000085 int getNumViews() const { return mNumViews; }
86
Martin Radev70866b82016-07-22 15:27:42 +030087 void enterFunctionDeclaration() { mDeclaringFunction = true; }
88
89 void exitFunctionDeclaration() { mDeclaringFunction = false; }
90
91 bool declaringFunction() const { return mDeclaringFunction; }
92
Jamie Madill5c097022014-08-20 16:38:32 -040093 // This method is guaranteed to succeed, even if no variable with 'name' exists.
Jamie Madilld7b1ab52016-12-12 14:42:19 -050094 const TVariable *getNamedVariable(const TSourceLoc &location,
95 const TString *name,
96 const TSymbol *symbol);
Olli Etuaho82c29ed2015-11-03 13:06:54 +020097 TIntermTyped *parseVariableIdentifier(const TSourceLoc &location,
98 const TString *name,
99 const TSymbol *symbol);
Jamie Madill5c097022014-08-20 16:38:32 -0400100
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300101 // Look at a '.' field selector string and change it into offsets for a vector.
102 bool parseVectorFields(const TSourceLoc &line,
103 const TString &compString,
104 int vecSize,
105 TVector<int> *fieldOffsets);
alokp@chromium.org75fe6b72011-08-14 05:31:22 +0000106
Jamie Madill06145232015-05-13 13:10:01 -0400107 void assignError(const TSourceLoc &line, const char *op, TString left, TString right);
108 void unaryOpError(const TSourceLoc &line, const char *op, TString operand);
109 void binaryOpError(const TSourceLoc &line, const char *op, TString left, TString right);
Olli Etuaho856c4972016-08-08 11:38:39 +0300110
Olli Etuaho8a176262016-08-16 14:23:01 +0300111 // Check functions - the ones that return bool return false if an error was generated.
112
Olli Etuaho856c4972016-08-08 11:38:39 +0300113 bool checkIsNotReserved(const TSourceLoc &line, const TString &identifier);
114 void checkPrecisionSpecified(const TSourceLoc &line, TPrecision precision, TBasicType type);
115 bool checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node);
116 void checkIsConst(TIntermTyped *node);
117 void checkIsScalarInteger(TIntermTyped *node, const char *token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800118 bool checkIsAtGlobalLevel(const TSourceLoc &line, const char *token);
Olli Etuaho856c4972016-08-08 11:38:39 +0300119 bool checkConstructorArguments(const TSourceLoc &line,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800120 const TIntermSequence *arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300121 const TType &type);
122
123 // Returns a sanitized array size to use (the size is at least 1).
124 unsigned int checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr);
Olli Etuaho8a176262016-08-16 14:23:01 +0300125 bool checkIsValidQualifierForArray(const TSourceLoc &line, const TPublicType &elementQualifier);
126 bool checkIsValidTypeForArray(const TSourceLoc &line, const TPublicType &elementType);
Olli Etuaho856c4972016-08-08 11:38:39 +0300127 bool checkIsNonVoid(const TSourceLoc &line, const TString &identifier, const TBasicType &type);
128 void checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type);
129 void checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType);
jchen10cc2a10e2017-05-03 14:05:12 +0800130 bool checkIsNotOpaqueType(const TSourceLoc &line,
131 const TTypeSpecifierNonArray &pType,
132 const char *reason);
Olli Etuaho856c4972016-08-08 11:38:39 +0300133 void checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line, const TPublicType &pType);
134 void checkLocationIsNotSpecified(const TSourceLoc &location,
135 const TLayoutQualifier &layoutQualifier);
Olli Etuaho856c4972016-08-08 11:38:39 +0300136 void checkIsParameterQualifierValid(const TSourceLoc &line,
Martin Radev70866b82016-07-22 15:27:42 +0300137 const TTypeQualifierBuilder &typeQualifierBuilder,
Olli Etuaho856c4972016-08-08 11:38:39 +0300138 TType *type);
139 bool checkCanUseExtension(const TSourceLoc &line, const TString &extension);
Olli Etuahobb7e5a72017-04-24 10:16:44 +0300140
141 // Done for all declarations, whether empty or not.
142 void declarationQualifierErrorCheck(const sh::TQualifier qualifier,
143 const sh::TLayoutQualifier &layoutQualifier,
144 const TSourceLoc &location);
145 // Done for the first non-empty declarator in a declaration.
146 void nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
147 const TSourceLoc &identifierLocation);
148 // Done only for empty declarations.
Martin Radevb8b01222016-11-20 23:25:53 +0200149 void emptyDeclarationErrorCheck(const TPublicType &publicType, const TSourceLoc &location);
Olli Etuahobb7e5a72017-04-24 10:16:44 +0300150
Olli Etuaho856c4972016-08-08 11:38:39 +0300151 void checkLayoutQualifierSupported(const TSourceLoc &location,
152 const TString &layoutQualifierName,
153 int versionRequired);
154 bool checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
155 const TLayoutQualifier &layoutQualifier);
Olli Etuaho856c4972016-08-08 11:38:39 +0300156 void functionCallLValueErrorCheck(const TFunction *fnCandidate, TIntermAggregate *fnCall);
Martin Radev70866b82016-07-22 15:27:42 +0300157 void checkInvariantVariableQualifier(bool invariant,
158 const TQualifier qualifier,
159 const TSourceLoc &invariantLocation);
Olli Etuaho856c4972016-08-08 11:38:39 +0300160 void checkInputOutputTypeIsValidES3(const TQualifier qualifier,
161 const TPublicType &type,
162 const TSourceLoc &qualifierLocation);
Martin Radev2cc85b32016-08-05 16:22:53 +0300163 void checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier);
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400164 const TPragma &pragma() const { return mDirectiveHandler.pragma(); }
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500165 const TExtensionBehavior &extensionBehavior() const
166 {
167 return mDirectiveHandler.extensionBehavior();
168 }
Jamie Madill06145232015-05-13 13:10:01 -0400169 bool supportsExtension(const char *extension);
170 bool isExtensionEnabled(const char *extension) const;
Olli Etuaho95468d12017-05-04 11:14:34 +0300171 bool isMultiviewExtensionEnabled() const
172 {
173 return mMultiviewAvailable &&
174 (isExtensionEnabled("GL_OVR_multiview") || isExtensionEnabled("GL_OVR_multiview2"));
175 }
Jamie Madill06145232015-05-13 13:10:01 -0400176 void handleExtensionDirective(const TSourceLoc &loc, const char *extName, const char *behavior);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500177 void handlePragmaDirective(const TSourceLoc &loc,
178 const char *name,
179 const char *value,
180 bool stdgl);
alokp@chromium.org75fe6b72011-08-14 05:31:22 +0000181
Olli Etuaho914b79a2017-06-19 16:03:19 +0300182 // Returns true on success. *initNode may still be nullptr on success in case the initialization
183 // is not needed in the AST.
Jamie Madill06145232015-05-13 13:10:01 -0400184 bool executeInitializer(const TSourceLoc &line,
185 const TString &identifier,
186 const TPublicType &pType,
187 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +0100188 TIntermBinary **initNode);
Olli Etuaho914b79a2017-06-19 16:03:19 +0300189 TIntermNode *addConditionInitializer(const TPublicType &pType,
190 const TString &identifier,
191 TIntermTyped *initializer,
192 const TSourceLoc &loc);
193 TIntermNode *addLoop(TLoopType type,
194 TIntermNode *init,
195 TIntermNode *cond,
196 TIntermTyped *expr,
197 TIntermNode *body,
198 const TSourceLoc &loc);
alokp@chromium.org75fe6b72011-08-14 05:31:22 +0000199
Olli Etuahocce89652017-06-19 16:04:09 +0300200 // For "if" test nodes. There are three children: a condition, a true path, and a false path.
201 // The two paths are in TIntermNodePair code.
202 TIntermNode *addIfElse(TIntermTyped *cond, TIntermNodePair code, const TSourceLoc &loc);
203
Olli Etuaho0e3aee32016-10-27 12:56:38 +0100204 void addFullySpecifiedType(TPublicType *typeSpecifier);
Martin Radev70866b82016-07-22 15:27:42 +0300205 TPublicType addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Jamie Madill06145232015-05-13 13:10:01 -0400206 const TPublicType &typeSpecifier);
Olli Etuahoe7847b02015-03-16 11:56:12 +0200207
Olli Etuaho13389b62016-10-16 11:48:18 +0100208 TIntermDeclaration *parseSingleDeclaration(TPublicType &publicType,
209 const TSourceLoc &identifierOrTypeLocation,
210 const TString &identifier);
211 TIntermDeclaration *parseSingleArrayDeclaration(TPublicType &publicType,
212 const TSourceLoc &identifierLocation,
213 const TString &identifier,
214 const TSourceLoc &indexLocation,
215 TIntermTyped *indexExpression);
216 TIntermDeclaration *parseSingleInitDeclaration(const TPublicType &publicType,
217 const TSourceLoc &identifierLocation,
218 const TString &identifier,
219 const TSourceLoc &initLocation,
220 TIntermTyped *initializer);
Jamie Madill47e3ec02014-08-20 16:38:33 -0400221
Olli Etuaho3875ffd2015-04-10 16:45:14 +0300222 // Parse a declaration like "type a[n] = initializer"
223 // Note that this does not apply to declarations like "type[n] a = initializer"
Olli Etuaho13389b62016-10-16 11:48:18 +0100224 TIntermDeclaration *parseSingleArrayInitDeclaration(TPublicType &publicType,
225 const TSourceLoc &identifierLocation,
226 const TString &identifier,
227 const TSourceLoc &indexLocation,
228 TIntermTyped *indexExpression,
229 const TSourceLoc &initLocation,
230 TIntermTyped *initializer);
Olli Etuaho3875ffd2015-04-10 16:45:14 +0300231
Olli Etuahobf4e1b72016-12-09 11:30:15 +0000232 TIntermInvariantDeclaration *parseInvariantDeclaration(
233 const TTypeQualifierBuilder &typeQualifierBuilder,
234 const TSourceLoc &identifierLoc,
235 const TString *identifier,
236 const TSymbol *symbol);
Olli Etuahoe7847b02015-03-16 11:56:12 +0200237
Olli Etuaho13389b62016-10-16 11:48:18 +0100238 void parseDeclarator(TPublicType &publicType,
239 const TSourceLoc &identifierLocation,
240 const TString &identifier,
241 TIntermDeclaration *declarationOut);
242 void parseArrayDeclarator(TPublicType &publicType,
243 const TSourceLoc &identifierLocation,
244 const TString &identifier,
245 const TSourceLoc &arrayLocation,
246 TIntermTyped *indexExpression,
247 TIntermDeclaration *declarationOut);
248 void parseInitDeclarator(const TPublicType &publicType,
249 const TSourceLoc &identifierLocation,
250 const TString &identifier,
251 const TSourceLoc &initLocation,
252 TIntermTyped *initializer,
253 TIntermDeclaration *declarationOut);
Olli Etuahoe7847b02015-03-16 11:56:12 +0200254
Olli Etuaho3875ffd2015-04-10 16:45:14 +0300255 // Parse a declarator like "a[n] = initializer"
Olli Etuaho13389b62016-10-16 11:48:18 +0100256 void parseArrayInitDeclarator(const TPublicType &publicType,
257 const TSourceLoc &identifierLocation,
258 const TString &identifier,
259 const TSourceLoc &indexLocation,
260 TIntermTyped *indexExpression,
261 const TSourceLoc &initLocation,
262 TIntermTyped *initializer,
263 TIntermDeclaration *declarationOut);
Olli Etuaho3875ffd2015-04-10 16:45:14 +0300264
Olli Etuahocce89652017-06-19 16:04:09 +0300265 void parseDefaultPrecisionQualifier(const TPrecision precision,
266 const TPublicType &type,
267 const TSourceLoc &loc);
Martin Radev70866b82016-07-22 15:27:42 +0300268 void parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder);
Olli Etuahocce89652017-06-19 16:04:09 +0300269
Olli Etuaho16c745a2017-01-16 17:02:27 +0000270 TIntermFunctionPrototype *addFunctionPrototypeDeclaration(const TFunction &parsedFunction,
271 const TSourceLoc &location);
Olli Etuaho8ad9e752017-01-16 19:55:20 +0000272 TIntermFunctionDefinition *addFunctionDefinition(TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +0100273 TIntermBlock *functionBody,
274 const TSourceLoc &location);
Olli Etuaho476197f2016-10-11 13:59:08 +0100275 void parseFunctionDefinitionHeader(const TSourceLoc &location,
276 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +0000277 TIntermFunctionPrototype **prototypeOut);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500278 TFunction *parseFunctionDeclarator(const TSourceLoc &location, TFunction *function);
Olli Etuaho9de84a52016-06-14 17:36:01 +0300279 TFunction *parseFunctionHeader(const TPublicType &type,
280 const TString *name,
281 const TSourceLoc &location);
Olli Etuahocce89652017-06-19 16:04:09 +0300282 TFunction *addNonConstructorFunc(const TString *name, const TSourceLoc &loc);
Jamie Madill06145232015-05-13 13:10:01 -0400283 TFunction *addConstructorFunc(const TPublicType &publicType);
Olli Etuahocce89652017-06-19 16:04:09 +0300284 TParameter parseParameterDeclarator(const TPublicType &publicType,
285 const TString *name,
286 const TSourceLoc &nameLoc);
287 TParameter parseParameterArrayDeclarator(const TString *identifier,
288 const TSourceLoc &identifierLoc,
289 TIntermTyped *arraySize,
290 const TSourceLoc &arrayLoc,
291 TPublicType *type);
Olli Etuaho90892fb2016-07-14 14:44:51 +0300292
Jamie Madill06145232015-05-13 13:10:01 -0400293 TIntermTyped *addIndexExpression(TIntermTyped *baseExpression,
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500294 const TSourceLoc &location,
Jamie Madill06145232015-05-13 13:10:01 -0400295 TIntermTyped *indexExpression);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500296 TIntermTyped *addFieldSelectionExpression(TIntermTyped *baseExpression,
Jamie Madill06145232015-05-13 13:10:01 -0400297 const TSourceLoc &dotLocation,
298 const TString &fieldString,
299 const TSourceLoc &fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +0000300
Olli Etuahocce89652017-06-19 16:04:09 +0300301 // Parse declarator for a single field
302 TField *parseStructDeclarator(TString *identifier, const TSourceLoc &loc);
303 TField *parseStructArrayDeclarator(TString *identifier,
304 const TSourceLoc &loc,
305 TIntermTyped *arraySize,
306 const TSourceLoc &arraySizeLoc);
307
Olli Etuaho4de340a2016-12-16 09:32:03 +0000308 TFieldList *combineStructFieldLists(TFieldList *processedFields,
309 const TFieldList *newlyAddedFields,
310 const TSourceLoc &location);
Martin Radev70866b82016-07-22 15:27:42 +0300311 TFieldList *addStructDeclaratorListWithQualifiers(
312 const TTypeQualifierBuilder &typeQualifierBuilder,
313 TPublicType *typeSpecifier,
314 TFieldList *fieldList);
Jamie Madill06145232015-05-13 13:10:01 -0400315 TFieldList *addStructDeclaratorList(const TPublicType &typeSpecifier, TFieldList *fieldList);
Martin Radev4a9cd802016-09-01 16:51:51 +0300316 TTypeSpecifierNonArray addStructure(const TSourceLoc &structLine,
317 const TSourceLoc &nameLine,
318 const TString *structName,
319 TFieldList *fieldList);
kbr@chromium.org476541f2011-10-27 21:14:51 +0000320
Olli Etuaho13389b62016-10-16 11:48:18 +0100321 TIntermDeclaration *addInterfaceBlock(const TTypeQualifierBuilder &typeQualifierBuilder,
322 const TSourceLoc &nameLine,
323 const TString &blockName,
324 TFieldList *fieldList,
325 const TString *instanceName,
326 const TSourceLoc &instanceLine,
327 TIntermTyped *arrayIndex,
328 const TSourceLoc &arrayIndexLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000329
Martin Radev802abe02016-08-04 17:48:32 +0300330 void parseLocalSize(const TString &qualifierType,
331 const TSourceLoc &qualifierTypeLine,
332 int intValue,
333 const TSourceLoc &intValueLine,
334 const std::string &intValueString,
335 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +0300336 sh::WorkGroupSize *localSize);
Olli Etuaho09b04a22016-12-15 13:30:26 +0000337 void parseNumViews(int intValue,
338 const TSourceLoc &intValueLine,
339 const std::string &intValueString,
340 int *numViews);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500341 TLayoutQualifier parseLayoutQualifier(const TString &qualifierType,
342 const TSourceLoc &qualifierTypeLine);
Jamie Madill06145232015-05-13 13:10:01 -0400343 TLayoutQualifier parseLayoutQualifier(const TString &qualifierType,
344 const TSourceLoc &qualifierTypeLine,
Jamie Madill06145232015-05-13 13:10:01 -0400345 int intValue,
346 const TSourceLoc &intValueLine);
Olli Etuaho613b9592016-09-05 12:05:53 +0300347 TTypeQualifierBuilder *createTypeQualifierBuilder(const TSourceLoc &loc);
Olli Etuahocce89652017-06-19 16:04:09 +0300348 TStorageQualifierWrapper *parseGlobalStorageQualifier(TQualifier qualifier,
349 const TSourceLoc &loc);
350 TStorageQualifierWrapper *parseVaryingQualifier(const TSourceLoc &loc);
351 TStorageQualifierWrapper *parseInQualifier(const TSourceLoc &loc);
352 TStorageQualifierWrapper *parseOutQualifier(const TSourceLoc &loc);
353 TStorageQualifierWrapper *parseInOutQualifier(const TSourceLoc &loc);
Martin Radev802abe02016-08-04 17:48:32 +0300354 TLayoutQualifier joinLayoutQualifiers(TLayoutQualifier leftQualifier,
355 TLayoutQualifier rightQualifier,
356 const TSourceLoc &rightQualifierLocation);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +0000357
kbr@chromium.org476541f2011-10-27 21:14:51 +0000358 // Performs an error check for embedded struct declarations.
Olli Etuaho383b7912016-08-05 11:22:59 +0300359 void enterStructDeclaration(const TSourceLoc &line, const TString &identifier);
kbr@chromium.org476541f2011-10-27 21:14:51 +0000360 void exitStructDeclaration();
361
Olli Etuaho8a176262016-08-16 14:23:01 +0300362 void checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field);
Olli Etuaho09b22472015-02-11 11:47:26 +0200363
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100364 TIntermSwitch *addSwitch(TIntermTyped *init,
365 TIntermBlock *statementList,
366 const TSourceLoc &loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +0200367 TIntermCase *addCase(TIntermTyped *condition, const TSourceLoc &loc);
368 TIntermCase *addDefault(const TSourceLoc &loc);
369
Jamie Madill06145232015-05-13 13:10:01 -0400370 TIntermTyped *addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc);
371 TIntermTyped *addUnaryMathLValue(TOperator op, TIntermTyped *child, const TSourceLoc &loc);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500372 TIntermTyped *addBinaryMath(TOperator op,
373 TIntermTyped *left,
374 TIntermTyped *right,
375 const TSourceLoc &loc);
376 TIntermTyped *addBinaryMathBooleanResult(TOperator op,
377 TIntermTyped *left,
378 TIntermTyped *right,
379 const TSourceLoc &loc);
380 TIntermTyped *addAssign(TOperator op,
381 TIntermTyped *left,
382 TIntermTyped *right,
383 const TSourceLoc &loc);
Olli Etuaho49300862015-02-20 14:54:49 +0200384
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +0200385 TIntermTyped *addComma(TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc);
386
Olli Etuaho49300862015-02-20 14:54:49 +0200387 TIntermBranch *addBranch(TOperator op, const TSourceLoc &loc);
Olli Etuahocce89652017-06-19 16:04:09 +0300388 TIntermBranch *addBranch(TOperator op, TIntermTyped *expression, const TSourceLoc &loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +0200389
Olli Etuahoe1a94c62015-11-16 17:35:25 +0200390 void checkTextureOffsetConst(TIntermAggregate *functionCall);
Martin Radev2cc85b32016-08-05 16:22:53 +0300391 void checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall);
392 void checkImageMemoryAccessForUserDefinedFunctions(const TFunction *functionDefinition,
393 const TIntermAggregate *functionCall);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800394 TIntermSequence *createEmptyArgumentsList();
Olli Etuaho72d10202017-01-19 15:58:30 +0000395
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800396 // fnCall is only storing the built-in op, and function name or constructor type. arguments
Olli Etuaho72d10202017-01-19 15:58:30 +0000397 // has the arguments.
Jamie Madill06145232015-05-13 13:10:01 -0400398 TIntermTyped *addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800399 TIntermSequence *arguments,
Jamie Madill06145232015-05-13 13:10:01 -0400400 TIntermNode *thisNode,
Olli Etuaho72d10202017-01-19 15:58:30 +0000401 const TSourceLoc &loc);
Olli Etuahofc1806e2015-03-17 13:03:11 +0200402
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300403 TIntermTyped *addTernarySelection(TIntermTyped *cond,
404 TIntermTyped *trueExpression,
405 TIntermTyped *falseExpression,
406 const TSourceLoc &line);
Olli Etuaho52901742015-04-15 13:42:45 +0300407
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400408 // TODO(jmadill): make these private
Olli Etuahof119a262016-08-19 15:54:22 +0300409 TIntermediate intermediate; // to build a parse tree
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400410 TSymbolTable &symbolTable; // symbol table that goes with the language currently being parsed
411
Olli Etuahofc1806e2015-03-17 13:03:11 +0200412 private:
jchen104cdac9e2017-05-08 11:01:20 +0800413 class AtomicCounterBindingState;
414 constexpr static size_t kAtomicCounterSize = 4;
415 // UNIFORM_ARRAY_STRIDE for atomic counter arrays is an implementation-dependent value which
416 // can be queried after a program is linked according to ES 3.10 section 7.7.1. This is
417 // controversial with the offset inheritance as described in ESSL 3.10 section 4.4.6. Currently
418 // we treat it as always 4 in favour of the original interpretation in
419 // "ARB_shader_atomic_counters".
420 // TODO(jie.a.chen@intel.com): Double check this once the spec vagueness is resolved.
421 constexpr static size_t kAtomicCounterArrayStride = 4;
422
Olli Etuaho4de340a2016-12-16 09:32:03 +0000423 // Returns a clamped index. If it prints out an error message, the token is "[]".
Olli Etuaho90892fb2016-07-14 14:44:51 +0300424 int checkIndexOutOfRange(bool outOfRangeIndexIsError,
425 const TSourceLoc &location,
426 int index,
427 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +0000428 const char *reason);
Olli Etuaho90892fb2016-07-14 14:44:51 +0300429
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500430 bool declareVariable(const TSourceLoc &line,
431 const TString &identifier,
432 const TType &type,
433 TVariable **variable);
Olli Etuaho2935c582015-04-08 14:32:06 +0300434
Olli Etuaho856c4972016-08-08 11:38:39 +0300435 void checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
436 const TString &identifier,
437 TPublicType *type);
Olli Etuaho376f1b52015-04-13 13:23:41 +0300438
Olli Etuaho8a176262016-08-16 14:23:01 +0300439 bool checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
440 const TPublicType &elementType);
jchen104cdac9e2017-05-08 11:01:20 +0800441 // Done for all atomic counter declarations, whether empty or not.
442 void atomicCounterQualifierErrorCheck(const TPublicType &publicType,
443 const TSourceLoc &location);
Olli Etuaho8a176262016-08-16 14:23:01 +0300444
Olli Etuaho1dded802016-08-18 18:13:13 +0300445 // Assumes that multiplication op has already been set based on the types.
446 bool isMultiplicationTypeCombinationValid(TOperator op, const TType &left, const TType &right);
447
Martin Radev2cc85b32016-08-05 16:22:53 +0300448 void checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
449 TQualifier qualifier,
450 const TType &type);
Martin Radev2cc85b32016-08-05 16:22:53 +0300451
Olli Etuaho43364892017-02-13 16:00:12 +0000452 void checkInternalFormatIsNotSpecified(const TSourceLoc &location,
453 TLayoutImageInternalFormat internalFormat);
454 void checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
455 const TSourceLoc &location);
jchen104cdac9e2017-05-08 11:01:20 +0800456 void checkAtomicCounterOffsetIsNotOverlapped(TPublicType &publicType,
457 size_t size,
458 bool forceAppend,
459 const TSourceLoc &loc,
460 TType &type);
Olli Etuaho43364892017-02-13 16:00:12 +0000461 void checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type);
462 void checkBindingIsNotSpecified(const TSourceLoc &location, int binding);
jchen104cdac9e2017-05-08 11:01:20 +0800463 void checkOffsetIsNotSpecified(const TSourceLoc &location, int offset);
Olli Etuaho43364892017-02-13 16:00:12 +0000464 void checkImageBindingIsValid(const TSourceLoc &location, int binding, int arraySize);
465 void checkSamplerBindingIsValid(const TSourceLoc &location, int binding, int arraySize);
jchen10af713a22017-04-19 09:10:56 +0800466 void checkBlockBindingIsValid(const TSourceLoc &location, int binding, int arraySize);
jchen104cdac9e2017-05-08 11:01:20 +0800467 void checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding);
Olli Etuaho43364892017-02-13 16:00:12 +0000468
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000469 void checkUniformLocationInRange(const TSourceLoc &location,
470 int objectLocationCount,
471 const TLayoutQualifier &layoutQualifier);
472
Andrei Volykhina5527072017-03-22 16:46:30 +0300473 void checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv);
474
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500475 TIntermTyped *addBinaryMathInternal(TOperator op,
476 TIntermTyped *left,
477 TIntermTyped *right,
478 const TSourceLoc &loc);
Olli Etuaho13389b62016-10-16 11:48:18 +0100479 TIntermBinary *createAssign(TOperator op,
480 TIntermTyped *left,
481 TIntermTyped *right,
482 const TSourceLoc &loc);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800483 TIntermTyped *createUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc);
484
485 TIntermTyped *addMethod(TFunction *fnCall,
486 TIntermSequence *arguments,
487 TIntermNode *thisNode,
488 const TSourceLoc &loc);
489 TIntermTyped *addConstructor(TIntermSequence *arguments,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800490 TType type,
491 const TSourceLoc &line);
492 TIntermTyped *addNonConstructorFunctionCall(TFunction *fnCall,
493 TIntermSequence *arguments,
494 const TSourceLoc &loc);
Olli Etuahod6b14282015-03-17 14:31:35 +0200495
Olli Etuaho47fd36a2015-03-19 14:22:24 +0200496 // Return true if the checks pass
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500497 bool binaryOpCommonCheck(TOperator op,
498 TIntermTyped *left,
499 TIntermTyped *right,
500 const TSourceLoc &loc);
Olli Etuahofa33d582015-04-09 14:33:12 +0300501
Olli Etuaho8ad9e752017-01-16 19:55:20 +0000502 TIntermFunctionPrototype *createPrototypeNodeFromFunction(const TFunction &function,
503 const TSourceLoc &location,
504 bool insertParametersToSymbolTable);
505
jchen104cdac9e2017-05-08 11:01:20 +0800506 void setAtomicCounterBindingDefaultOffset(const TPublicType &declaration,
507 const TSourceLoc &location);
508
Olli Etuahobb7e5a72017-04-24 10:16:44 +0300509 // Set to true when the last/current declarator list was started with an empty declaration. The
510 // non-empty declaration error check will need to be performed if the empty declaration is
511 // followed by a declarator.
512 bool mDeferredNonEmptyDeclarationErrorCheck;
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400513
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500514 sh::GLenum mShaderType; // vertex or fragment language (future: pack or unpack)
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800515 ShShaderSpec mShaderSpec; // The language specification compiler conforms to - GLES2 or WebGL.
516 ShCompileOptions mCompileOptions; // Options passed to TCompiler
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400517 int mShaderVersion;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500518 TIntermBlock *mTreeRoot; // root of parse tree being created
519 int mLoopNestingLevel; // 0 if outside all loops
520 int mStructNestingLevel; // incremented while parsing a struct declaration
521 int mSwitchNestingLevel; // 0 if outside all switch statements
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800522 const TType
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500523 *mCurrentFunctionType; // the return type of the function that's currently being parsed
524 bool mFunctionReturnsValue; // true if a non-void function has a return
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800525 bool mChecksPrecisionErrors; // true if an error will be generated when a variable is declared
526 // without precision, explicit or implicit.
Olli Etuahoa6996682015-10-12 14:32:30 +0300527 bool mFragmentPrecisionHighOnESSL1; // true if highp precision is supported when compiling
528 // ESSL1.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400529 TLayoutMatrixPacking mDefaultMatrixPacking;
530 TLayoutBlockStorage mDefaultBlockStorage;
531 TString mHashErrMsg;
Olli Etuaho77ba4082016-12-16 12:01:18 +0000532 TDiagnostics *mDiagnostics;
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400533 TDirectiveHandler mDirectiveHandler;
534 pp::Preprocessor mPreprocessor;
535 void *mScanner;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500536 bool mUsesFragData; // track if we are using both gl_FragData and gl_FragColor
Jamie Madill14e95b32015-05-07 10:10:41 -0400537 bool mUsesFragColor;
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300538 bool mUsesSecondaryOutputs; // Track if we are using either gl_SecondaryFragData or
539 // gl_Secondary FragColor or both.
Olli Etuahoe1a94c62015-11-16 17:35:25 +0200540 int mMinProgramTexelOffset;
541 int mMaxProgramTexelOffset;
Martin Radev802abe02016-08-04 17:48:32 +0300542
Olli Etuaho09b04a22016-12-15 13:30:26 +0000543 bool mMultiviewAvailable;
544
Martin Radev802abe02016-08-04 17:48:32 +0300545 // keep track of local group size declared in layout. It should be declared only once.
546 bool mComputeShaderLocalSizeDeclared;
Martin Radev4c4c8e72016-08-04 12:25:34 +0300547 sh::WorkGroupSize mComputeShaderLocalSize;
Olli Etuaho09b04a22016-12-15 13:30:26 +0000548 // keep track of number of views declared in layout.
549 int mNumViews;
550 int mMaxNumViews;
Olli Etuaho43364892017-02-13 16:00:12 +0000551 int mMaxImageUnits;
552 int mMaxCombinedTextureImageUnits;
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000553 int mMaxUniformLocations;
jchen10af713a22017-04-19 09:10:56 +0800554 int mMaxUniformBufferBindings;
jchen104cdac9e2017-05-08 11:01:20 +0800555 int mMaxAtomicCounterBindings;
556
Martin Radev70866b82016-07-22 15:27:42 +0300557 // keeps track whether we are declaring / defining a function
558 bool mDeclaringFunction;
jchen104cdac9e2017-05-08 11:01:20 +0800559
560 // Track the state of each atomic counter binding.
561 std::map<int, AtomicCounterBindingState> mAtomicCounterBindingStates;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000562};
563
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500564int PaParseStrings(size_t count,
565 const char *const string[],
566 const int length[],
567 TParseContext *context);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000568
Jamie Madill45bcc782016-11-07 13:58:48 -0500569} // namespace sh
570
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500571#endif // COMPILER_TRANSLATOR_PARSECONTEXT_H_