blob: 557e34e21963dd41fa85e2aac29fe34d99fcb60e [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
Olli Etuaho56229f12017-07-10 14:16:33 +030093 TIntermConstantUnion *addScalarLiteral(const TConstantUnion *constantUnion,
94 const TSourceLoc &line);
95
Jamie Madill5c097022014-08-20 16:38:32 -040096 // This method is guaranteed to succeed, even if no variable with 'name' exists.
Jamie Madilld7b1ab52016-12-12 14:42:19 -050097 const TVariable *getNamedVariable(const TSourceLoc &location,
98 const TString *name,
99 const TSymbol *symbol);
Olli Etuaho82c29ed2015-11-03 13:06:54 +0200100 TIntermTyped *parseVariableIdentifier(const TSourceLoc &location,
101 const TString *name,
102 const TSymbol *symbol);
Jamie Madill5c097022014-08-20 16:38:32 -0400103
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300104 // Look at a '.' field selector string and change it into offsets for a vector.
105 bool parseVectorFields(const TSourceLoc &line,
106 const TString &compString,
107 int vecSize,
108 TVector<int> *fieldOffsets);
alokp@chromium.org75fe6b72011-08-14 05:31:22 +0000109
Jamie Madill06145232015-05-13 13:10:01 -0400110 void assignError(const TSourceLoc &line, const char *op, TString left, TString right);
111 void unaryOpError(const TSourceLoc &line, const char *op, TString operand);
112 void binaryOpError(const TSourceLoc &line, const char *op, TString left, TString right);
Olli Etuaho856c4972016-08-08 11:38:39 +0300113
Olli Etuaho8a176262016-08-16 14:23:01 +0300114 // Check functions - the ones that return bool return false if an error was generated.
115
Olli Etuaho856c4972016-08-08 11:38:39 +0300116 bool checkIsNotReserved(const TSourceLoc &line, const TString &identifier);
117 void checkPrecisionSpecified(const TSourceLoc &line, TPrecision precision, TBasicType type);
118 bool checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node);
119 void checkIsConst(TIntermTyped *node);
120 void checkIsScalarInteger(TIntermTyped *node, const char *token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800121 bool checkIsAtGlobalLevel(const TSourceLoc &line, const char *token);
Olli Etuaho856c4972016-08-08 11:38:39 +0300122 bool checkConstructorArguments(const TSourceLoc &line,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800123 const TIntermSequence *arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300124 const TType &type);
125
126 // Returns a sanitized array size to use (the size is at least 1).
127 unsigned int checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr);
Olli Etuaho8a176262016-08-16 14:23:01 +0300128 bool checkIsValidQualifierForArray(const TSourceLoc &line, const TPublicType &elementQualifier);
129 bool checkIsValidTypeForArray(const TSourceLoc &line, const TPublicType &elementType);
Olli Etuaho856c4972016-08-08 11:38:39 +0300130 bool checkIsNonVoid(const TSourceLoc &line, const TString &identifier, const TBasicType &type);
Olli Etuaho56229f12017-07-10 14:16:33 +0300131 bool checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type);
Olli Etuaho856c4972016-08-08 11:38:39 +0300132 void checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType);
jchen10cc2a10e2017-05-03 14:05:12 +0800133 bool checkIsNotOpaqueType(const TSourceLoc &line,
134 const TTypeSpecifierNonArray &pType,
135 const char *reason);
Olli Etuaho856c4972016-08-08 11:38:39 +0300136 void checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line, const TPublicType &pType);
137 void checkLocationIsNotSpecified(const TSourceLoc &location,
138 const TLayoutQualifier &layoutQualifier);
Olli Etuaho856c4972016-08-08 11:38:39 +0300139 void checkIsParameterQualifierValid(const TSourceLoc &line,
Martin Radev70866b82016-07-22 15:27:42 +0300140 const TTypeQualifierBuilder &typeQualifierBuilder,
Olli Etuaho856c4972016-08-08 11:38:39 +0300141 TType *type);
142 bool checkCanUseExtension(const TSourceLoc &line, const TString &extension);
Olli Etuahobb7e5a72017-04-24 10:16:44 +0300143
144 // Done for all declarations, whether empty or not.
145 void declarationQualifierErrorCheck(const sh::TQualifier qualifier,
146 const sh::TLayoutQualifier &layoutQualifier,
147 const TSourceLoc &location);
148 // Done for the first non-empty declarator in a declaration.
149 void nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
150 const TSourceLoc &identifierLocation);
151 // Done only for empty declarations.
Martin Radevb8b01222016-11-20 23:25:53 +0200152 void emptyDeclarationErrorCheck(const TPublicType &publicType, const TSourceLoc &location);
Olli Etuahobb7e5a72017-04-24 10:16:44 +0300153
Olli Etuaho856c4972016-08-08 11:38:39 +0300154 void checkLayoutQualifierSupported(const TSourceLoc &location,
155 const TString &layoutQualifierName,
156 int versionRequired);
157 bool checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
158 const TLayoutQualifier &layoutQualifier);
Olli Etuaho856c4972016-08-08 11:38:39 +0300159 void functionCallLValueErrorCheck(const TFunction *fnCandidate, TIntermAggregate *fnCall);
Martin Radev70866b82016-07-22 15:27:42 +0300160 void checkInvariantVariableQualifier(bool invariant,
161 const TQualifier qualifier,
162 const TSourceLoc &invariantLocation);
Olli Etuaho856c4972016-08-08 11:38:39 +0300163 void checkInputOutputTypeIsValidES3(const TQualifier qualifier,
164 const TPublicType &type,
165 const TSourceLoc &qualifierLocation);
Martin Radev2cc85b32016-08-05 16:22:53 +0300166 void checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier);
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400167 const TPragma &pragma() const { return mDirectiveHandler.pragma(); }
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500168 const TExtensionBehavior &extensionBehavior() const
169 {
170 return mDirectiveHandler.extensionBehavior();
171 }
Jamie Madill06145232015-05-13 13:10:01 -0400172 bool supportsExtension(const char *extension);
173 bool isExtensionEnabled(const char *extension) const;
Olli Etuaho95468d12017-05-04 11:14:34 +0300174 bool isMultiviewExtensionEnabled() const
175 {
176 return mMultiviewAvailable &&
177 (isExtensionEnabled("GL_OVR_multiview") || isExtensionEnabled("GL_OVR_multiview2"));
178 }
Jamie Madill06145232015-05-13 13:10:01 -0400179 void handleExtensionDirective(const TSourceLoc &loc, const char *extName, const char *behavior);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500180 void handlePragmaDirective(const TSourceLoc &loc,
181 const char *name,
182 const char *value,
183 bool stdgl);
alokp@chromium.org75fe6b72011-08-14 05:31:22 +0000184
Olli Etuaho914b79a2017-06-19 16:03:19 +0300185 // Returns true on success. *initNode may still be nullptr on success in case the initialization
186 // is not needed in the AST.
Jamie Madill06145232015-05-13 13:10:01 -0400187 bool executeInitializer(const TSourceLoc &line,
188 const TString &identifier,
189 const TPublicType &pType,
190 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +0100191 TIntermBinary **initNode);
Olli Etuaho914b79a2017-06-19 16:03:19 +0300192 TIntermNode *addConditionInitializer(const TPublicType &pType,
193 const TString &identifier,
194 TIntermTyped *initializer,
195 const TSourceLoc &loc);
196 TIntermNode *addLoop(TLoopType type,
197 TIntermNode *init,
198 TIntermNode *cond,
199 TIntermTyped *expr,
200 TIntermNode *body,
201 const TSourceLoc &loc);
alokp@chromium.org75fe6b72011-08-14 05:31:22 +0000202
Olli Etuahocce89652017-06-19 16:04:09 +0300203 // For "if" test nodes. There are three children: a condition, a true path, and a false path.
204 // The two paths are in TIntermNodePair code.
205 TIntermNode *addIfElse(TIntermTyped *cond, TIntermNodePair code, const TSourceLoc &loc);
206
Olli Etuaho0e3aee32016-10-27 12:56:38 +0100207 void addFullySpecifiedType(TPublicType *typeSpecifier);
Martin Radev70866b82016-07-22 15:27:42 +0300208 TPublicType addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Jamie Madill06145232015-05-13 13:10:01 -0400209 const TPublicType &typeSpecifier);
Olli Etuahoe7847b02015-03-16 11:56:12 +0200210
Olli Etuaho13389b62016-10-16 11:48:18 +0100211 TIntermDeclaration *parseSingleDeclaration(TPublicType &publicType,
212 const TSourceLoc &identifierOrTypeLocation,
213 const TString &identifier);
214 TIntermDeclaration *parseSingleArrayDeclaration(TPublicType &publicType,
215 const TSourceLoc &identifierLocation,
216 const TString &identifier,
217 const TSourceLoc &indexLocation,
218 TIntermTyped *indexExpression);
219 TIntermDeclaration *parseSingleInitDeclaration(const TPublicType &publicType,
220 const TSourceLoc &identifierLocation,
221 const TString &identifier,
222 const TSourceLoc &initLocation,
223 TIntermTyped *initializer);
Jamie Madill47e3ec02014-08-20 16:38:33 -0400224
Olli Etuaho3875ffd2015-04-10 16:45:14 +0300225 // Parse a declaration like "type a[n] = initializer"
226 // Note that this does not apply to declarations like "type[n] a = initializer"
Olli Etuaho13389b62016-10-16 11:48:18 +0100227 TIntermDeclaration *parseSingleArrayInitDeclaration(TPublicType &publicType,
228 const TSourceLoc &identifierLocation,
229 const TString &identifier,
230 const TSourceLoc &indexLocation,
231 TIntermTyped *indexExpression,
232 const TSourceLoc &initLocation,
233 TIntermTyped *initializer);
Olli Etuaho3875ffd2015-04-10 16:45:14 +0300234
Olli Etuahobf4e1b72016-12-09 11:30:15 +0000235 TIntermInvariantDeclaration *parseInvariantDeclaration(
236 const TTypeQualifierBuilder &typeQualifierBuilder,
237 const TSourceLoc &identifierLoc,
238 const TString *identifier,
239 const TSymbol *symbol);
Olli Etuahoe7847b02015-03-16 11:56:12 +0200240
Olli Etuaho13389b62016-10-16 11:48:18 +0100241 void parseDeclarator(TPublicType &publicType,
242 const TSourceLoc &identifierLocation,
243 const TString &identifier,
244 TIntermDeclaration *declarationOut);
245 void parseArrayDeclarator(TPublicType &publicType,
246 const TSourceLoc &identifierLocation,
247 const TString &identifier,
248 const TSourceLoc &arrayLocation,
249 TIntermTyped *indexExpression,
250 TIntermDeclaration *declarationOut);
251 void parseInitDeclarator(const TPublicType &publicType,
252 const TSourceLoc &identifierLocation,
253 const TString &identifier,
254 const TSourceLoc &initLocation,
255 TIntermTyped *initializer,
256 TIntermDeclaration *declarationOut);
Olli Etuahoe7847b02015-03-16 11:56:12 +0200257
Olli Etuaho3875ffd2015-04-10 16:45:14 +0300258 // Parse a declarator like "a[n] = initializer"
Olli Etuaho13389b62016-10-16 11:48:18 +0100259 void parseArrayInitDeclarator(const TPublicType &publicType,
260 const TSourceLoc &identifierLocation,
261 const TString &identifier,
262 const TSourceLoc &indexLocation,
263 TIntermTyped *indexExpression,
264 const TSourceLoc &initLocation,
265 TIntermTyped *initializer,
266 TIntermDeclaration *declarationOut);
Olli Etuaho3875ffd2015-04-10 16:45:14 +0300267
Olli Etuahocce89652017-06-19 16:04:09 +0300268 void parseDefaultPrecisionQualifier(const TPrecision precision,
269 const TPublicType &type,
270 const TSourceLoc &loc);
Martin Radev70866b82016-07-22 15:27:42 +0300271 void parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder);
Olli Etuahocce89652017-06-19 16:04:09 +0300272
Olli Etuaho16c745a2017-01-16 17:02:27 +0000273 TIntermFunctionPrototype *addFunctionPrototypeDeclaration(const TFunction &parsedFunction,
274 const TSourceLoc &location);
Olli Etuaho8ad9e752017-01-16 19:55:20 +0000275 TIntermFunctionDefinition *addFunctionDefinition(TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +0100276 TIntermBlock *functionBody,
277 const TSourceLoc &location);
Olli Etuaho476197f2016-10-11 13:59:08 +0100278 void parseFunctionDefinitionHeader(const TSourceLoc &location,
279 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +0000280 TIntermFunctionPrototype **prototypeOut);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500281 TFunction *parseFunctionDeclarator(const TSourceLoc &location, TFunction *function);
Olli Etuaho9de84a52016-06-14 17:36:01 +0300282 TFunction *parseFunctionHeader(const TPublicType &type,
283 const TString *name,
284 const TSourceLoc &location);
Olli Etuahocce89652017-06-19 16:04:09 +0300285 TFunction *addNonConstructorFunc(const TString *name, const TSourceLoc &loc);
Jamie Madill06145232015-05-13 13:10:01 -0400286 TFunction *addConstructorFunc(const TPublicType &publicType);
Olli Etuahocce89652017-06-19 16:04:09 +0300287 TParameter parseParameterDeclarator(const TPublicType &publicType,
288 const TString *name,
289 const TSourceLoc &nameLoc);
290 TParameter parseParameterArrayDeclarator(const TString *identifier,
291 const TSourceLoc &identifierLoc,
292 TIntermTyped *arraySize,
293 const TSourceLoc &arrayLoc,
294 TPublicType *type);
Olli Etuaho90892fb2016-07-14 14:44:51 +0300295
Jamie Madill06145232015-05-13 13:10:01 -0400296 TIntermTyped *addIndexExpression(TIntermTyped *baseExpression,
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500297 const TSourceLoc &location,
Jamie Madill06145232015-05-13 13:10:01 -0400298 TIntermTyped *indexExpression);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500299 TIntermTyped *addFieldSelectionExpression(TIntermTyped *baseExpression,
Jamie Madill06145232015-05-13 13:10:01 -0400300 const TSourceLoc &dotLocation,
301 const TString &fieldString,
302 const TSourceLoc &fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +0000303
Olli Etuahocce89652017-06-19 16:04:09 +0300304 // Parse declarator for a single field
305 TField *parseStructDeclarator(TString *identifier, const TSourceLoc &loc);
306 TField *parseStructArrayDeclarator(TString *identifier,
307 const TSourceLoc &loc,
308 TIntermTyped *arraySize,
309 const TSourceLoc &arraySizeLoc);
310
Olli Etuaho4de340a2016-12-16 09:32:03 +0000311 TFieldList *combineStructFieldLists(TFieldList *processedFields,
312 const TFieldList *newlyAddedFields,
313 const TSourceLoc &location);
Martin Radev70866b82016-07-22 15:27:42 +0300314 TFieldList *addStructDeclaratorListWithQualifiers(
315 const TTypeQualifierBuilder &typeQualifierBuilder,
316 TPublicType *typeSpecifier,
317 TFieldList *fieldList);
Jamie Madill06145232015-05-13 13:10:01 -0400318 TFieldList *addStructDeclaratorList(const TPublicType &typeSpecifier, TFieldList *fieldList);
Martin Radev4a9cd802016-09-01 16:51:51 +0300319 TTypeSpecifierNonArray addStructure(const TSourceLoc &structLine,
320 const TSourceLoc &nameLine,
321 const TString *structName,
322 TFieldList *fieldList);
kbr@chromium.org476541f2011-10-27 21:14:51 +0000323
Olli Etuaho13389b62016-10-16 11:48:18 +0100324 TIntermDeclaration *addInterfaceBlock(const TTypeQualifierBuilder &typeQualifierBuilder,
325 const TSourceLoc &nameLine,
326 const TString &blockName,
327 TFieldList *fieldList,
328 const TString *instanceName,
329 const TSourceLoc &instanceLine,
330 TIntermTyped *arrayIndex,
331 const TSourceLoc &arrayIndexLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000332
Martin Radev802abe02016-08-04 17:48:32 +0300333 void parseLocalSize(const TString &qualifierType,
334 const TSourceLoc &qualifierTypeLine,
335 int intValue,
336 const TSourceLoc &intValueLine,
337 const std::string &intValueString,
338 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +0300339 sh::WorkGroupSize *localSize);
Olli Etuaho09b04a22016-12-15 13:30:26 +0000340 void parseNumViews(int intValue,
341 const TSourceLoc &intValueLine,
342 const std::string &intValueString,
343 int *numViews);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500344 TLayoutQualifier parseLayoutQualifier(const TString &qualifierType,
345 const TSourceLoc &qualifierTypeLine);
Jamie Madill06145232015-05-13 13:10:01 -0400346 TLayoutQualifier parseLayoutQualifier(const TString &qualifierType,
347 const TSourceLoc &qualifierTypeLine,
Jamie Madill06145232015-05-13 13:10:01 -0400348 int intValue,
349 const TSourceLoc &intValueLine);
Olli Etuaho613b9592016-09-05 12:05:53 +0300350 TTypeQualifierBuilder *createTypeQualifierBuilder(const TSourceLoc &loc);
Olli Etuahocce89652017-06-19 16:04:09 +0300351 TStorageQualifierWrapper *parseGlobalStorageQualifier(TQualifier qualifier,
352 const TSourceLoc &loc);
353 TStorageQualifierWrapper *parseVaryingQualifier(const TSourceLoc &loc);
354 TStorageQualifierWrapper *parseInQualifier(const TSourceLoc &loc);
355 TStorageQualifierWrapper *parseOutQualifier(const TSourceLoc &loc);
356 TStorageQualifierWrapper *parseInOutQualifier(const TSourceLoc &loc);
Martin Radev802abe02016-08-04 17:48:32 +0300357 TLayoutQualifier joinLayoutQualifiers(TLayoutQualifier leftQualifier,
358 TLayoutQualifier rightQualifier,
359 const TSourceLoc &rightQualifierLocation);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +0000360
kbr@chromium.org476541f2011-10-27 21:14:51 +0000361 // Performs an error check for embedded struct declarations.
Olli Etuaho383b7912016-08-05 11:22:59 +0300362 void enterStructDeclaration(const TSourceLoc &line, const TString &identifier);
kbr@chromium.org476541f2011-10-27 21:14:51 +0000363 void exitStructDeclaration();
364
Olli Etuaho8a176262016-08-16 14:23:01 +0300365 void checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field);
Olli Etuaho09b22472015-02-11 11:47:26 +0200366
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100367 TIntermSwitch *addSwitch(TIntermTyped *init,
368 TIntermBlock *statementList,
369 const TSourceLoc &loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +0200370 TIntermCase *addCase(TIntermTyped *condition, const TSourceLoc &loc);
371 TIntermCase *addDefault(const TSourceLoc &loc);
372
Jamie Madill06145232015-05-13 13:10:01 -0400373 TIntermTyped *addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc);
374 TIntermTyped *addUnaryMathLValue(TOperator op, TIntermTyped *child, const TSourceLoc &loc);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500375 TIntermTyped *addBinaryMath(TOperator op,
376 TIntermTyped *left,
377 TIntermTyped *right,
378 const TSourceLoc &loc);
379 TIntermTyped *addBinaryMathBooleanResult(TOperator op,
380 TIntermTyped *left,
381 TIntermTyped *right,
382 const TSourceLoc &loc);
383 TIntermTyped *addAssign(TOperator op,
384 TIntermTyped *left,
385 TIntermTyped *right,
386 const TSourceLoc &loc);
Olli Etuaho49300862015-02-20 14:54:49 +0200387
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +0200388 TIntermTyped *addComma(TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc);
389
Olli Etuaho49300862015-02-20 14:54:49 +0200390 TIntermBranch *addBranch(TOperator op, const TSourceLoc &loc);
Olli Etuahocce89652017-06-19 16:04:09 +0300391 TIntermBranch *addBranch(TOperator op, TIntermTyped *expression, const TSourceLoc &loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +0200392
Olli Etuahoe1a94c62015-11-16 17:35:25 +0200393 void checkTextureOffsetConst(TIntermAggregate *functionCall);
Martin Radev2cc85b32016-08-05 16:22:53 +0300394 void checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall);
395 void checkImageMemoryAccessForUserDefinedFunctions(const TFunction *functionDefinition,
396 const TIntermAggregate *functionCall);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800397 TIntermSequence *createEmptyArgumentsList();
Olli Etuaho72d10202017-01-19 15:58:30 +0000398
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800399 // fnCall is only storing the built-in op, and function name or constructor type. arguments
Olli Etuaho72d10202017-01-19 15:58:30 +0000400 // has the arguments.
Jamie Madill06145232015-05-13 13:10:01 -0400401 TIntermTyped *addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800402 TIntermSequence *arguments,
Jamie Madill06145232015-05-13 13:10:01 -0400403 TIntermNode *thisNode,
Olli Etuaho72d10202017-01-19 15:58:30 +0000404 const TSourceLoc &loc);
Olli Etuahofc1806e2015-03-17 13:03:11 +0200405
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300406 TIntermTyped *addTernarySelection(TIntermTyped *cond,
407 TIntermTyped *trueExpression,
408 TIntermTyped *falseExpression,
409 const TSourceLoc &line);
Olli Etuaho52901742015-04-15 13:42:45 +0300410
Olli Etuaho56229f12017-07-10 14:16:33 +0300411 // TODO(jmadill): make this private
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400412 TSymbolTable &symbolTable; // symbol table that goes with the language currently being parsed
413
Olli Etuahofc1806e2015-03-17 13:03:11 +0200414 private:
jchen104cdac9e2017-05-08 11:01:20 +0800415 class AtomicCounterBindingState;
416 constexpr static size_t kAtomicCounterSize = 4;
417 // UNIFORM_ARRAY_STRIDE for atomic counter arrays is an implementation-dependent value which
418 // can be queried after a program is linked according to ES 3.10 section 7.7.1. This is
419 // controversial with the offset inheritance as described in ESSL 3.10 section 4.4.6. Currently
420 // we treat it as always 4 in favour of the original interpretation in
421 // "ARB_shader_atomic_counters".
422 // TODO(jie.a.chen@intel.com): Double check this once the spec vagueness is resolved.
423 constexpr static size_t kAtomicCounterArrayStride = 4;
424
Olli Etuaho4de340a2016-12-16 09:32:03 +0000425 // Returns a clamped index. If it prints out an error message, the token is "[]".
Olli Etuaho90892fb2016-07-14 14:44:51 +0300426 int checkIndexOutOfRange(bool outOfRangeIndexIsError,
427 const TSourceLoc &location,
428 int index,
429 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +0000430 const char *reason);
Olli Etuaho90892fb2016-07-14 14:44:51 +0300431
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500432 bool declareVariable(const TSourceLoc &line,
433 const TString &identifier,
434 const TType &type,
435 TVariable **variable);
Olli Etuaho2935c582015-04-08 14:32:06 +0300436
Olli Etuaho856c4972016-08-08 11:38:39 +0300437 void checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
438 const TString &identifier,
439 TPublicType *type);
Olli Etuaho376f1b52015-04-13 13:23:41 +0300440
Olli Etuaho8a176262016-08-16 14:23:01 +0300441 bool checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
442 const TPublicType &elementType);
jchen104cdac9e2017-05-08 11:01:20 +0800443 // Done for all atomic counter declarations, whether empty or not.
444 void atomicCounterQualifierErrorCheck(const TPublicType &publicType,
445 const TSourceLoc &location);
Olli Etuaho8a176262016-08-16 14:23:01 +0300446
Olli Etuaho1dded802016-08-18 18:13:13 +0300447 // Assumes that multiplication op has already been set based on the types.
448 bool isMultiplicationTypeCombinationValid(TOperator op, const TType &left, const TType &right);
449
Martin Radev2cc85b32016-08-05 16:22:53 +0300450 void checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
451 TQualifier qualifier,
452 const TType &type);
Martin Radev2cc85b32016-08-05 16:22:53 +0300453
Olli Etuaho43364892017-02-13 16:00:12 +0000454 void checkInternalFormatIsNotSpecified(const TSourceLoc &location,
455 TLayoutImageInternalFormat internalFormat);
456 void checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
457 const TSourceLoc &location);
jchen104cdac9e2017-05-08 11:01:20 +0800458 void checkAtomicCounterOffsetIsNotOverlapped(TPublicType &publicType,
459 size_t size,
460 bool forceAppend,
461 const TSourceLoc &loc,
462 TType &type);
Olli Etuaho43364892017-02-13 16:00:12 +0000463 void checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type);
464 void checkBindingIsNotSpecified(const TSourceLoc &location, int binding);
jchen104cdac9e2017-05-08 11:01:20 +0800465 void checkOffsetIsNotSpecified(const TSourceLoc &location, int offset);
Olli Etuaho43364892017-02-13 16:00:12 +0000466 void checkImageBindingIsValid(const TSourceLoc &location, int binding, int arraySize);
467 void checkSamplerBindingIsValid(const TSourceLoc &location, int binding, int arraySize);
jchen10af713a22017-04-19 09:10:56 +0800468 void checkBlockBindingIsValid(const TSourceLoc &location, int binding, int arraySize);
jchen104cdac9e2017-05-08 11:01:20 +0800469 void checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding);
Olli Etuaho43364892017-02-13 16:00:12 +0000470
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000471 void checkUniformLocationInRange(const TSourceLoc &location,
472 int objectLocationCount,
473 const TLayoutQualifier &layoutQualifier);
474
Andrei Volykhina5527072017-03-22 16:46:30 +0300475 void checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv);
476
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500477 TIntermTyped *addBinaryMathInternal(TOperator op,
478 TIntermTyped *left,
479 TIntermTyped *right,
480 const TSourceLoc &loc);
Olli Etuaho13389b62016-10-16 11:48:18 +0100481 TIntermBinary *createAssign(TOperator op,
482 TIntermTyped *left,
483 TIntermTyped *right,
484 const TSourceLoc &loc);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800485 TIntermTyped *createUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc);
486
487 TIntermTyped *addMethod(TFunction *fnCall,
488 TIntermSequence *arguments,
489 TIntermNode *thisNode,
490 const TSourceLoc &loc);
491 TIntermTyped *addConstructor(TIntermSequence *arguments,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800492 TType type,
493 const TSourceLoc &line);
494 TIntermTyped *addNonConstructorFunctionCall(TFunction *fnCall,
495 TIntermSequence *arguments,
496 const TSourceLoc &loc);
Olli Etuahod6b14282015-03-17 14:31:35 +0200497
Olli Etuaho47fd36a2015-03-19 14:22:24 +0200498 // Return true if the checks pass
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500499 bool binaryOpCommonCheck(TOperator op,
500 TIntermTyped *left,
501 TIntermTyped *right,
502 const TSourceLoc &loc);
Olli Etuahofa33d582015-04-09 14:33:12 +0300503
Olli Etuaho8ad9e752017-01-16 19:55:20 +0000504 TIntermFunctionPrototype *createPrototypeNodeFromFunction(const TFunction &function,
505 const TSourceLoc &location,
506 bool insertParametersToSymbolTable);
507
jchen104cdac9e2017-05-08 11:01:20 +0800508 void setAtomicCounterBindingDefaultOffset(const TPublicType &declaration,
509 const TSourceLoc &location);
510
Olli Etuahobb7e5a72017-04-24 10:16:44 +0300511 // Set to true when the last/current declarator list was started with an empty declaration. The
512 // non-empty declaration error check will need to be performed if the empty declaration is
513 // followed by a declarator.
514 bool mDeferredNonEmptyDeclarationErrorCheck;
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400515
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500516 sh::GLenum mShaderType; // vertex or fragment language (future: pack or unpack)
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800517 ShShaderSpec mShaderSpec; // The language specification compiler conforms to - GLES2 or WebGL.
518 ShCompileOptions mCompileOptions; // Options passed to TCompiler
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400519 int mShaderVersion;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500520 TIntermBlock *mTreeRoot; // root of parse tree being created
521 int mLoopNestingLevel; // 0 if outside all loops
522 int mStructNestingLevel; // incremented while parsing a struct declaration
523 int mSwitchNestingLevel; // 0 if outside all switch statements
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800524 const TType
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500525 *mCurrentFunctionType; // the return type of the function that's currently being parsed
526 bool mFunctionReturnsValue; // true if a non-void function has a return
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800527 bool mChecksPrecisionErrors; // true if an error will be generated when a variable is declared
528 // without precision, explicit or implicit.
Olli Etuahoa6996682015-10-12 14:32:30 +0300529 bool mFragmentPrecisionHighOnESSL1; // true if highp precision is supported when compiling
530 // ESSL1.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400531 TLayoutMatrixPacking mDefaultMatrixPacking;
532 TLayoutBlockStorage mDefaultBlockStorage;
533 TString mHashErrMsg;
Olli Etuaho77ba4082016-12-16 12:01:18 +0000534 TDiagnostics *mDiagnostics;
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400535 TDirectiveHandler mDirectiveHandler;
536 pp::Preprocessor mPreprocessor;
537 void *mScanner;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500538 bool mUsesFragData; // track if we are using both gl_FragData and gl_FragColor
Jamie Madill14e95b32015-05-07 10:10:41 -0400539 bool mUsesFragColor;
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300540 bool mUsesSecondaryOutputs; // Track if we are using either gl_SecondaryFragData or
541 // gl_Secondary FragColor or both.
Olli Etuahoe1a94c62015-11-16 17:35:25 +0200542 int mMinProgramTexelOffset;
543 int mMaxProgramTexelOffset;
Martin Radev802abe02016-08-04 17:48:32 +0300544
Olli Etuaho09b04a22016-12-15 13:30:26 +0000545 bool mMultiviewAvailable;
546
Martin Radev802abe02016-08-04 17:48:32 +0300547 // keep track of local group size declared in layout. It should be declared only once.
548 bool mComputeShaderLocalSizeDeclared;
Martin Radev4c4c8e72016-08-04 12:25:34 +0300549 sh::WorkGroupSize mComputeShaderLocalSize;
Olli Etuaho09b04a22016-12-15 13:30:26 +0000550 // keep track of number of views declared in layout.
551 int mNumViews;
552 int mMaxNumViews;
Olli Etuaho43364892017-02-13 16:00:12 +0000553 int mMaxImageUnits;
554 int mMaxCombinedTextureImageUnits;
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000555 int mMaxUniformLocations;
jchen10af713a22017-04-19 09:10:56 +0800556 int mMaxUniformBufferBindings;
jchen104cdac9e2017-05-08 11:01:20 +0800557 int mMaxAtomicCounterBindings;
558
Martin Radev70866b82016-07-22 15:27:42 +0300559 // keeps track whether we are declaring / defining a function
560 bool mDeclaringFunction;
jchen104cdac9e2017-05-08 11:01:20 +0800561
562 // Track the state of each atomic counter binding.
563 std::map<int, AtomicCounterBindingState> mAtomicCounterBindingStates;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000564};
565
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500566int PaParseStrings(size_t count,
567 const char *const string[],
568 const int length[],
569 TParseContext *context);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000570
Jamie Madill45bcc782016-11-07 13:58:48 -0500571} // namespace sh
572
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500573#endif // COMPILER_TRANSLATOR_PARSECONTEXT_H_