1 //
2 // Copyright 2002 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 #ifndef COMPILER_TRANSLATOR_PARSECONTEXT_H_
7 #define COMPILER_TRANSLATOR_PARSECONTEXT_H_
8 
9 #include "compiler/preprocessor/Preprocessor.h"
10 #include "compiler/translator/Compiler.h"
11 #include "compiler/translator/Declarator.h"
12 #include "compiler/translator/Diagnostics.h"
13 #include "compiler/translator/DirectiveHandler.h"
14 #include "compiler/translator/FunctionLookup.h"
15 #include "compiler/translator/QualifierTypes.h"
16 #include "compiler/translator/SymbolTable.h"
17 
18 namespace sh
19 {
20 
21 struct TMatrixFields
22 {
23     bool wholeRow;
24     bool wholeCol;
25     int row;
26     int col;
27 };
28 
29 //
30 // The following are extra variables needed during parsing, grouped together so
31 // they can be passed to the parser without needing a global.
32 //
33 class TParseContext : angle::NonCopyable
34 {
35   public:
36     TParseContext(TSymbolTable &symt,
37                   TExtensionBehavior &ext,
38                   sh::GLenum type,
39                   ShShaderSpec spec,
40                   ShCompileOptions options,
41                   bool checksPrecErrors,
42                   TDiagnostics *diagnostics,
43                   const ShBuiltInResources &resources,
44                   ShShaderOutput outputType);
45     ~TParseContext();
46 
47     bool anyMultiviewExtensionAvailable();
getPreprocessor()48     const angle::pp::Preprocessor &getPreprocessor() const { return mPreprocessor; }
getPreprocessor()49     angle::pp::Preprocessor &getPreprocessor() { return mPreprocessor; }
getScanner()50     void *getScanner() const { return mScanner; }
setScanner(void * scanner)51     void setScanner(void *scanner) { mScanner = scanner; }
getShaderVersion()52     int getShaderVersion() const { return mShaderVersion; }
getShaderType()53     sh::GLenum getShaderType() const { return mShaderType; }
getShaderSpec()54     ShShaderSpec getShaderSpec() const { return mShaderSpec; }
numErrors()55     int numErrors() const { return mDiagnostics->numErrors(); }
56     void error(const TSourceLoc &loc, const char *reason, const char *token);
57     void error(const TSourceLoc &loc, const char *reason, const ImmutableString &token);
58     void warning(const TSourceLoc &loc, const char *reason, const char *token);
59 
60     // If isError is false, a warning will be reported instead.
61     void outOfRangeError(bool isError,
62                          const TSourceLoc &loc,
63                          const char *reason,
64                          const char *token);
65 
getTreeRoot()66     TIntermBlock *getTreeRoot() const { return mTreeRoot; }
setTreeRoot(TIntermBlock * treeRoot)67     void setTreeRoot(TIntermBlock *treeRoot) { mTreeRoot = treeRoot; }
68 
getFragmentPrecisionHigh()69     bool getFragmentPrecisionHigh() const
70     {
71         return mFragmentPrecisionHighOnESSL1 || mShaderVersion >= 300;
72     }
setFragmentPrecisionHighOnESSL1(bool fragmentPrecisionHigh)73     void setFragmentPrecisionHighOnESSL1(bool fragmentPrecisionHigh)
74     {
75         mFragmentPrecisionHighOnESSL1 = fragmentPrecisionHigh;
76     }
77 
isEarlyFragmentTestsSpecified()78     bool isEarlyFragmentTestsSpecified() const { return mEarlyFragmentTestsSpecified; }
79 
setLoopNestingLevel(int loopNestintLevel)80     void setLoopNestingLevel(int loopNestintLevel) { mLoopNestingLevel = loopNestintLevel; }
81 
incrLoopNestingLevel()82     void incrLoopNestingLevel() { ++mLoopNestingLevel; }
decrLoopNestingLevel()83     void decrLoopNestingLevel() { --mLoopNestingLevel; }
84 
incrSwitchNestingLevel()85     void incrSwitchNestingLevel() { ++mSwitchNestingLevel; }
decrSwitchNestingLevel()86     void decrSwitchNestingLevel() { --mSwitchNestingLevel; }
87 
isComputeShaderLocalSizeDeclared()88     bool isComputeShaderLocalSizeDeclared() const { return mComputeShaderLocalSizeDeclared; }
89     sh::WorkGroupSize getComputeShaderLocalSize() const;
90 
getNumViews()91     int getNumViews() const { return mNumViews; }
92 
enterFunctionDeclaration()93     void enterFunctionDeclaration() { mDeclaringFunction = true; }
94 
exitFunctionDeclaration()95     void exitFunctionDeclaration() { mDeclaringFunction = false; }
96 
declaringFunction()97     bool declaringFunction() const { return mDeclaringFunction; }
98 
99     TIntermConstantUnion *addScalarLiteral(const TConstantUnion *constantUnion,
100                                            const TSourceLoc &line);
101 
102     // This method is guaranteed to succeed, even if no variable with 'name' exists.
103     const TVariable *getNamedVariable(const TSourceLoc &location,
104                                       const ImmutableString &name,
105                                       const TSymbol *symbol);
106     TIntermTyped *parseVariableIdentifier(const TSourceLoc &location,
107                                           const ImmutableString &name,
108                                           const TSymbol *symbol);
109 
110     // Look at a '.' field selector string and change it into offsets for a vector.
111     bool parseVectorFields(const TSourceLoc &line,
112                            const ImmutableString &compString,
113                            int vecSize,
114                            TVector<int> *fieldOffsets);
115 
116     void assignError(const TSourceLoc &line, const char *op, const TType &left, const TType &right);
117     void unaryOpError(const TSourceLoc &line, const char *op, const TType &operand);
118     void binaryOpError(const TSourceLoc &line,
119                        const char *op,
120                        const TType &left,
121                        const TType &right);
122 
123     // Check functions - the ones that return bool return false if an error was generated.
124 
125     bool checkIsNotReserved(const TSourceLoc &line, const ImmutableString &identifier);
126     void checkPrecisionSpecified(const TSourceLoc &line, TPrecision precision, TBasicType type);
127     bool checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node);
128     void checkIsConst(TIntermTyped *node);
129     void checkIsScalarInteger(TIntermTyped *node, const char *token);
130     bool checkIsAtGlobalLevel(const TSourceLoc &line, const char *token);
131     bool checkConstructorArguments(const TSourceLoc &line,
132                                    const TIntermSequence &arguments,
133                                    const TType &type);
134 
135     // Returns a sanitized array size to use (the size is at least 1).
136     unsigned int checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr);
137     bool checkIsValidQualifierForArray(const TSourceLoc &line, const TPublicType &elementQualifier);
138     bool checkArrayElementIsNotArray(const TSourceLoc &line, const TPublicType &elementType);
139     bool checkArrayOfArraysInOut(const TSourceLoc &line,
140                                  const TPublicType &elementType,
141                                  const TType &arrayType);
142     bool checkIsNonVoid(const TSourceLoc &line,
143                         const ImmutableString &identifier,
144                         const TBasicType &type);
145     bool checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type);
146     void checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType);
147     bool checkIsNotOpaqueType(const TSourceLoc &line,
148                               const TTypeSpecifierNonArray &pType,
149                               const char *reason);
150     void checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line, const TPublicType &pType);
151     void checkLocationIsNotSpecified(const TSourceLoc &location,
152                                      const TLayoutQualifier &layoutQualifier);
153     void checkStd430IsForShaderStorageBlock(const TSourceLoc &location,
154                                             const TLayoutBlockStorage &blockStorage,
155                                             const TQualifier &qualifier);
156     void checkIsParameterQualifierValid(const TSourceLoc &line,
157                                         const TTypeQualifierBuilder &typeQualifierBuilder,
158                                         TType *type);
159 
160     // Check if at least one of the specified extensions can be used, and generate error/warning as
161     // appropriate according to the spec.
162     // This function is only needed for a few different small constant sizes of extension array, and
163     // we want to avoid unnecessary dynamic allocations. That's why checkCanUseOneOfExtensions is a
164     // template function rather than one taking a vector.
165     template <size_t size>
166     bool checkCanUseOneOfExtensions(const TSourceLoc &line,
167                                     const std::array<TExtension, size> &extensions);
168     bool checkCanUseExtension(const TSourceLoc &line, TExtension extension);
169 
170     // Done for all declarations, whether empty or not.
171     void declarationQualifierErrorCheck(const sh::TQualifier qualifier,
172                                         const sh::TLayoutQualifier &layoutQualifier,
173                                         const TSourceLoc &location);
174     // Done for the first non-empty declarator in a declaration.
175     void nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
176                                        const TSourceLoc &identifierLocation);
177     // Done only for empty declarations.
178     void emptyDeclarationErrorCheck(const TType &type, const TSourceLoc &location);
179 
180     void checkLayoutQualifierSupported(const TSourceLoc &location,
181                                        const ImmutableString &layoutQualifierName,
182                                        int versionRequired);
183     bool checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
184                                           const TLayoutQualifier &layoutQualifier);
185     void functionCallRValueLValueErrorCheck(const TFunction *fnCandidate, TIntermAggregate *fnCall);
186     void checkInvariantVariableQualifier(bool invariant,
187                                          const TQualifier qualifier,
188                                          const TSourceLoc &invariantLocation);
189     void checkInputOutputTypeIsValidES3(const TQualifier qualifier,
190                                         const TPublicType &type,
191                                         const TSourceLoc &qualifierLocation);
192     void checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier);
pragma()193     const TPragma &pragma() const { return mDirectiveHandler.pragma(); }
extensionBehavior()194     const TExtensionBehavior &extensionBehavior() const
195     {
196         return mDirectiveHandler.extensionBehavior();
197     }
198 
199     bool isExtensionEnabled(TExtension extension) const;
200     void handleExtensionDirective(const TSourceLoc &loc, const char *extName, const char *behavior);
201     void handlePragmaDirective(const TSourceLoc &loc,
202                                const char *name,
203                                const char *value,
204                                bool stdgl);
205 
206     // Returns true on success. *initNode may still be nullptr on success in case the initialization
207     // is not needed in the AST.
208     bool executeInitializer(const TSourceLoc &line,
209                             const ImmutableString &identifier,
210                             TType *type,
211                             TIntermTyped *initializer,
212                             TIntermBinary **initNode);
213     TIntermNode *addConditionInitializer(const TPublicType &pType,
214                                          const ImmutableString &identifier,
215                                          TIntermTyped *initializer,
216                                          const TSourceLoc &loc);
217     TIntermNode *addLoop(TLoopType type,
218                          TIntermNode *init,
219                          TIntermNode *cond,
220                          TIntermTyped *expr,
221                          TIntermNode *body,
222                          const TSourceLoc &loc);
223 
224     // For "if" test nodes. There are three children: a condition, a true path, and a false path.
225     // The two paths are in TIntermNodePair code.
226     TIntermNode *addIfElse(TIntermTyped *cond, TIntermNodePair code, const TSourceLoc &loc);
227 
228     void addFullySpecifiedType(TPublicType *typeSpecifier);
229     TPublicType addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
230                                       const TPublicType &typeSpecifier);
231 
232     TIntermDeclaration *parseSingleDeclaration(TPublicType &publicType,
233                                                const TSourceLoc &identifierOrTypeLocation,
234                                                const ImmutableString &identifier);
235     TIntermDeclaration *parseSingleArrayDeclaration(TPublicType &elementType,
236                                                     const TSourceLoc &identifierLocation,
237                                                     const ImmutableString &identifier,
238                                                     const TSourceLoc &indexLocation,
239                                                     const TVector<unsigned int> &arraySizes);
240     TIntermDeclaration *parseSingleInitDeclaration(const TPublicType &publicType,
241                                                    const TSourceLoc &identifierLocation,
242                                                    const ImmutableString &identifier,
243                                                    const TSourceLoc &initLocation,
244                                                    TIntermTyped *initializer);
245 
246     // Parse a declaration like "type a[n] = initializer"
247     // Note that this does not apply to declarations like "type[n] a = initializer"
248     TIntermDeclaration *parseSingleArrayInitDeclaration(TPublicType &elementType,
249                                                         const TSourceLoc &identifierLocation,
250                                                         const ImmutableString &identifier,
251                                                         const TSourceLoc &indexLocation,
252                                                         const TVector<unsigned int> &arraySizes,
253                                                         const TSourceLoc &initLocation,
254                                                         TIntermTyped *initializer);
255 
256     TIntermGlobalQualifierDeclaration *parseGlobalQualifierDeclaration(
257         const TTypeQualifierBuilder &typeQualifierBuilder,
258         const TSourceLoc &identifierLoc,
259         const ImmutableString &identifier,
260         const TSymbol *symbol);
261 
262     void parseDeclarator(TPublicType &publicType,
263                          const TSourceLoc &identifierLocation,
264                          const ImmutableString &identifier,
265                          TIntermDeclaration *declarationOut);
266     void parseArrayDeclarator(TPublicType &elementType,
267                               const TSourceLoc &identifierLocation,
268                               const ImmutableString &identifier,
269                               const TSourceLoc &arrayLocation,
270                               const TVector<unsigned int> &arraySizes,
271                               TIntermDeclaration *declarationOut);
272     void parseInitDeclarator(const TPublicType &publicType,
273                              const TSourceLoc &identifierLocation,
274                              const ImmutableString &identifier,
275                              const TSourceLoc &initLocation,
276                              TIntermTyped *initializer,
277                              TIntermDeclaration *declarationOut);
278 
279     // Parse a declarator like "a[n] = initializer"
280     void parseArrayInitDeclarator(const TPublicType &elementType,
281                                   const TSourceLoc &identifierLocation,
282                                   const ImmutableString &identifier,
283                                   const TSourceLoc &indexLocation,
284                                   const TVector<unsigned int> &arraySizes,
285                                   const TSourceLoc &initLocation,
286                                   TIntermTyped *initializer,
287                                   TIntermDeclaration *declarationOut);
288 
289     TIntermNode *addEmptyStatement(const TSourceLoc &location);
290 
291     void parseDefaultPrecisionQualifier(const TPrecision precision,
292                                         const TPublicType &type,
293                                         const TSourceLoc &loc);
294     void parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder);
295 
296     TIntermFunctionPrototype *addFunctionPrototypeDeclaration(const TFunction &parsedFunction,
297                                                               const TSourceLoc &location);
298     TIntermFunctionDefinition *addFunctionDefinition(TIntermFunctionPrototype *functionPrototype,
299                                                      TIntermBlock *functionBody,
300                                                      const TSourceLoc &location);
301     void parseFunctionDefinitionHeader(const TSourceLoc &location,
302                                        const TFunction *function,
303                                        TIntermFunctionPrototype **prototypeOut);
304     TFunction *parseFunctionDeclarator(const TSourceLoc &location, TFunction *function);
305     TFunction *parseFunctionHeader(const TPublicType &type,
306                                    const ImmutableString &name,
307                                    const TSourceLoc &location);
308 
309     TFunctionLookup *addNonConstructorFunc(const ImmutableString &name, const TSymbol *symbol);
310     TFunctionLookup *addConstructorFunc(const TPublicType &publicType);
311 
312     TParameter parseParameterDeclarator(const TPublicType &publicType,
313                                         const ImmutableString &name,
314                                         const TSourceLoc &nameLoc);
315 
316     TParameter parseParameterArrayDeclarator(const ImmutableString &name,
317                                              const TSourceLoc &nameLoc,
318                                              const TVector<unsigned int> &arraySizes,
319                                              const TSourceLoc &arrayLoc,
320                                              TPublicType *elementType);
321 
322     TIntermTyped *addIndexExpression(TIntermTyped *baseExpression,
323                                      const TSourceLoc &location,
324                                      TIntermTyped *indexExpression);
325     TIntermTyped *addFieldSelectionExpression(TIntermTyped *baseExpression,
326                                               const TSourceLoc &dotLocation,
327                                               const ImmutableString &fieldString,
328                                               const TSourceLoc &fieldLocation);
329 
330     // Parse declarator for a single field
331     TDeclarator *parseStructDeclarator(const ImmutableString &identifier, const TSourceLoc &loc);
332     TDeclarator *parseStructArrayDeclarator(const ImmutableString &identifier,
333                                             const TSourceLoc &loc,
334                                             const TVector<unsigned int> *arraySizes);
335 
336     void checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
337                                             const TFieldList::const_iterator end,
338                                             const ImmutableString &name,
339                                             const TSourceLoc &location);
340     TFieldList *addStructFieldList(TFieldList *fields, const TSourceLoc &location);
341     TFieldList *combineStructFieldLists(TFieldList *processedFields,
342                                         const TFieldList *newlyAddedFields,
343                                         const TSourceLoc &location);
344     TFieldList *addStructDeclaratorListWithQualifiers(
345         const TTypeQualifierBuilder &typeQualifierBuilder,
346         TPublicType *typeSpecifier,
347         const TDeclaratorList *declaratorList);
348     TFieldList *addStructDeclaratorList(const TPublicType &typeSpecifier,
349                                         const TDeclaratorList *declaratorList);
350     TTypeSpecifierNonArray addStructure(const TSourceLoc &structLine,
351                                         const TSourceLoc &nameLine,
352                                         const ImmutableString &structName,
353                                         TFieldList *fieldList);
354 
355     TIntermDeclaration *addInterfaceBlock(const TTypeQualifierBuilder &typeQualifierBuilder,
356                                           const TSourceLoc &nameLine,
357                                           const ImmutableString &blockName,
358                                           TFieldList *fieldList,
359                                           const ImmutableString &instanceName,
360                                           const TSourceLoc &instanceLine,
361                                           TIntermTyped *arrayIndex,
362                                           const TSourceLoc &arrayIndexLine);
363 
364     void parseLocalSize(const ImmutableString &qualifierType,
365                         const TSourceLoc &qualifierTypeLine,
366                         int intValue,
367                         const TSourceLoc &intValueLine,
368                         const std::string &intValueString,
369                         size_t index,
370                         sh::WorkGroupSize *localSize);
371     void parseNumViews(int intValue,
372                        const TSourceLoc &intValueLine,
373                        const std::string &intValueString,
374                        int *numViews);
375     void parseInvocations(int intValue,
376                           const TSourceLoc &intValueLine,
377                           const std::string &intValueString,
378                           int *numInvocations);
379     void parseMaxVertices(int intValue,
380                           const TSourceLoc &intValueLine,
381                           const std::string &intValueString,
382                           int *numMaxVertices);
383     void parseIndexLayoutQualifier(int intValue,
384                                    const TSourceLoc &intValueLine,
385                                    const std::string &intValueString,
386                                    int *index);
387     TLayoutQualifier parseLayoutQualifier(const ImmutableString &qualifierType,
388                                           const TSourceLoc &qualifierTypeLine);
389     TLayoutQualifier parseLayoutQualifier(const ImmutableString &qualifierType,
390                                           const TSourceLoc &qualifierTypeLine,
391                                           int intValue,
392                                           const TSourceLoc &intValueLine);
393     TTypeQualifierBuilder *createTypeQualifierBuilder(const TSourceLoc &loc);
394     TStorageQualifierWrapper *parseGlobalStorageQualifier(TQualifier qualifier,
395                                                           const TSourceLoc &loc);
396     TStorageQualifierWrapper *parseVaryingQualifier(const TSourceLoc &loc);
397     TStorageQualifierWrapper *parseInQualifier(const TSourceLoc &loc);
398     TStorageQualifierWrapper *parseOutQualifier(const TSourceLoc &loc);
399     TStorageQualifierWrapper *parseInOutQualifier(const TSourceLoc &loc);
400     TLayoutQualifier joinLayoutQualifiers(TLayoutQualifier leftQualifier,
401                                           TLayoutQualifier rightQualifier,
402                                           const TSourceLoc &rightQualifierLocation);
403 
404     // Performs an error check for embedded struct declarations.
405     void enterStructDeclaration(const TSourceLoc &line, const ImmutableString &identifier);
406     void exitStructDeclaration();
407 
408     void checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field);
409 
410     TIntermSwitch *addSwitch(TIntermTyped *init,
411                              TIntermBlock *statementList,
412                              const TSourceLoc &loc);
413     TIntermCase *addCase(TIntermTyped *condition, const TSourceLoc &loc);
414     TIntermCase *addDefault(const TSourceLoc &loc);
415 
416     TIntermTyped *addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc);
417     TIntermTyped *addUnaryMathLValue(TOperator op, TIntermTyped *child, const TSourceLoc &loc);
418     TIntermTyped *addBinaryMath(TOperator op,
419                                 TIntermTyped *left,
420                                 TIntermTyped *right,
421                                 const TSourceLoc &loc);
422     TIntermTyped *addBinaryMathBooleanResult(TOperator op,
423                                              TIntermTyped *left,
424                                              TIntermTyped *right,
425                                              const TSourceLoc &loc);
426     TIntermTyped *addAssign(TOperator op,
427                             TIntermTyped *left,
428                             TIntermTyped *right,
429                             const TSourceLoc &loc);
430 
431     TIntermTyped *addComma(TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc);
432 
433     TIntermBranch *addBranch(TOperator op, const TSourceLoc &loc);
434     TIntermBranch *addBranch(TOperator op, TIntermTyped *expression, const TSourceLoc &loc);
435 
436     void appendStatement(TIntermBlock *block, TIntermNode *statement);
437 
438     void checkTextureGather(TIntermAggregate *functionCall);
439     void checkTextureOffset(TIntermAggregate *functionCall);
440     void checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall);
441     void checkImageMemoryAccessForUserDefinedFunctions(const TFunction *functionDefinition,
442                                                        const TIntermAggregate *functionCall);
443     void checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall);
444     void checkInterpolationFS(TIntermAggregate *functionCall);
445 
446     // fnCall is only storing the built-in op, and function name or constructor type. arguments
447     // has the arguments.
448     TIntermTyped *addFunctionCallOrMethod(TFunctionLookup *fnCall, const TSourceLoc &loc);
449 
450     TIntermTyped *addTernarySelection(TIntermTyped *cond,
451                                       TIntermTyped *trueExpression,
452                                       TIntermTyped *falseExpression,
453                                       const TSourceLoc &line);
454 
getGeometryShaderMaxVertices()455     int getGeometryShaderMaxVertices() const { return mGeometryShaderMaxVertices; }
getGeometryShaderInvocations()456     int getGeometryShaderInvocations() const
457     {
458         return (mGeometryShaderInvocations > 0) ? mGeometryShaderInvocations : 1;
459     }
getGeometryShaderInputPrimitiveType()460     TLayoutPrimitiveType getGeometryShaderInputPrimitiveType() const
461     {
462         return mGeometryShaderInputPrimitiveType;
463     }
getGeometryShaderOutputPrimitiveType()464     TLayoutPrimitiveType getGeometryShaderOutputPrimitiveType() const
465     {
466         return mGeometryShaderOutputPrimitiveType;
467     }
468 
getOutputType()469     ShShaderOutput getOutputType() const { return mOutputType; }
470 
471     // TODO(jmadill): make this private
472     TSymbolTable &symbolTable;  // symbol table that goes with the language currently being parsed
473 
474   private:
475     class AtomicCounterBindingState;
476     constexpr static size_t kAtomicCounterSize = 4;
477     // UNIFORM_ARRAY_STRIDE for atomic counter arrays is an implementation-dependent value which
478     // can be queried after a program is linked according to ES 3.10 section 7.7.1. This is
479     // controversial with the offset inheritance as described in ESSL 3.10 section 4.4.6. Currently
480     // we treat it as always 4 in favour of the original interpretation in
481     // "ARB_shader_atomic_counters".
482     // TODO(jie.a.chen@intel.com): Double check this once the spec vagueness is resolved.
483     // Note that there may be tests in AtomicCounter_test that will need to be updated as well.
484     constexpr static size_t kAtomicCounterArrayStride = 4;
485 
486     void markStaticReadIfSymbol(TIntermNode *node);
487 
488     // Returns a clamped index. If it prints out an error message, the token is "[]".
489     int checkIndexLessThan(bool outOfRangeIndexIsError,
490                            const TSourceLoc &location,
491                            int index,
492                            int arraySize,
493                            const char *reason);
494 
495     bool declareVariable(const TSourceLoc &line,
496                          const ImmutableString &identifier,
497                          const TType *type,
498                          TVariable **variable);
499 
500     void checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
501                                               const ImmutableString &identifier,
502                                               TType *type);
503 
504     TParameter parseParameterDeclarator(TType *type,
505                                         const ImmutableString &name,
506                                         const TSourceLoc &nameLoc);
507 
508     bool checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
509                                               const TPublicType &elementType);
510     // Done for all atomic counter declarations, whether empty or not.
511     void atomicCounterQualifierErrorCheck(const TPublicType &publicType,
512                                           const TSourceLoc &location);
513 
514     // Assumes that multiplication op has already been set based on the types.
515     bool isMultiplicationTypeCombinationValid(TOperator op, const TType &left, const TType &right);
516 
517     void checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
518                                           TQualifier qualifier,
519                                           const TType &type);
520 
521     void checkInternalFormatIsNotSpecified(const TSourceLoc &location,
522                                            TLayoutImageInternalFormat internalFormat);
523     void checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
524                                             const TSourceLoc &location);
525     void checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
526                                                 const TSourceLoc &loc,
527                                                 TType *type);
528     void checkAtomicCounterOffsetAlignment(const TSourceLoc &location, const TType &type);
529 
530     void checkIndexIsNotSpecified(const TSourceLoc &location, int index);
531     void checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type);
532     void checkBindingIsNotSpecified(const TSourceLoc &location, int binding);
533     void checkOffsetIsNotSpecified(const TSourceLoc &location, int offset);
534     void checkImageBindingIsValid(const TSourceLoc &location,
535                                   int binding,
536                                   int arrayTotalElementCount);
537     void checkSamplerBindingIsValid(const TSourceLoc &location,
538                                     int binding,
539                                     int arrayTotalElementCount);
540     void checkBlockBindingIsValid(const TSourceLoc &location,
541                                   const TQualifier &qualifier,
542                                   int binding,
543                                   int arraySize);
544     void checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding);
545 
546     void checkUniformLocationInRange(const TSourceLoc &location,
547                                      int objectLocationCount,
548                                      const TLayoutQualifier &layoutQualifier);
549     void checkAttributeLocationInRange(const TSourceLoc &location,
550                                        int objectLocationCount,
551                                        const TLayoutQualifier &layoutQualifier);
552 
553     void checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv);
554 
555     void checkEarlyFragmentTestsIsNotSpecified(const TSourceLoc &location, bool earlyFragmentTests);
556 
557     bool checkUnsizedArrayConstructorArgumentDimensionality(const TIntermSequence &arguments,
558                                                             TType type,
559                                                             const TSourceLoc &line);
560     // Check texture offset is within range.
561     void checkSingleTextureOffset(const TSourceLoc &line,
562                                   const TConstantUnion *values,
563                                   size_t size,
564                                   int minOffsetValue,
565                                   int maxOffsetValue);
566 
567     // Will set the size of the outermost array according to geometry shader input layout.
568     void checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
569                                                  const ImmutableString &token,
570                                                  TType *type);
571 
572     // Will size any unsized array type so unsized arrays won't need to be taken into account
573     // further along the line in parsing.
574     void checkIsNotUnsizedArray(const TSourceLoc &line,
575                                 const char *errorMessage,
576                                 const ImmutableString &token,
577                                 TType *arrayType);
578 
579     TIntermTyped *addBinaryMathInternal(TOperator op,
580                                         TIntermTyped *left,
581                                         TIntermTyped *right,
582                                         const TSourceLoc &loc);
583     TIntermTyped *createUnaryMath(TOperator op,
584                                   TIntermTyped *child,
585                                   const TSourceLoc &loc,
586                                   const TFunction *func);
587 
588     TIntermTyped *addMethod(TFunctionLookup *fnCall, const TSourceLoc &loc);
589     TIntermTyped *addConstructor(TFunctionLookup *fnCall, const TSourceLoc &line);
590     TIntermTyped *addNonConstructorFunctionCall(TFunctionLookup *fnCall, const TSourceLoc &loc);
591 
592     // Return either the original expression or the folded version of the expression in case the
593     // folded node will validate the same way during subsequent parsing.
594     TIntermTyped *expressionOrFoldedResult(TIntermTyped *expression);
595 
596     // Return true if the checks pass
597     bool binaryOpCommonCheck(TOperator op,
598                              TIntermTyped *left,
599                              TIntermTyped *right,
600                              const TSourceLoc &loc);
601 
602     TIntermFunctionPrototype *createPrototypeNodeFromFunction(const TFunction &function,
603                                                               const TSourceLoc &location,
604                                                               bool insertParametersToSymbolTable);
605 
606     void setAtomicCounterBindingDefaultOffset(const TPublicType &declaration,
607                                               const TSourceLoc &location);
608 
609     bool checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier);
610     bool parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier);
611     bool parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier);
612     void setGeometryShaderInputArraySize(unsigned int inputArraySize, const TSourceLoc &line);
613 
614     // Set to true when the last/current declarator list was started with an empty declaration. The
615     // non-empty declaration error check will need to be performed if the empty declaration is
616     // followed by a declarator.
617     bool mDeferredNonEmptyDeclarationErrorCheck;
618 
619     sh::GLenum mShaderType;    // vertex or fragment language (future: pack or unpack)
620     ShShaderSpec mShaderSpec;  // The language specification compiler conforms to - GLES2 or WebGL.
621     ShCompileOptions mCompileOptions;  // Options passed to TCompiler
622     int mShaderVersion;
623     TIntermBlock *mTreeRoot;  // root of parse tree being created
624     int mLoopNestingLevel;    // 0 if outside all loops
625     int mStructNestingLevel;  // incremented while parsing a struct declaration
626     int mSwitchNestingLevel;  // 0 if outside all switch statements
627     const TType
628         *mCurrentFunctionType;    // the return type of the function that's currently being parsed
629     bool mFunctionReturnsValue;   // true if a non-void function has a return
630     bool mChecksPrecisionErrors;  // true if an error will be generated when a variable is declared
631                                   // without precision, explicit or implicit.
632     bool mFragmentPrecisionHighOnESSL1;  // true if highp precision is supported when compiling
633                                          // ESSL1.
634     bool mEarlyFragmentTestsSpecified;   // true if layout(early_fragment_tests) in; is specified.
635     TLayoutMatrixPacking mDefaultUniformMatrixPacking;
636     TLayoutBlockStorage mDefaultUniformBlockStorage;
637     TLayoutMatrixPacking mDefaultBufferMatrixPacking;
638     TLayoutBlockStorage mDefaultBufferBlockStorage;
639     TString mHashErrMsg;
640     TDiagnostics *mDiagnostics;
641     TDirectiveHandler mDirectiveHandler;
642     angle::pp::Preprocessor mPreprocessor;
643     void *mScanner;
644     int mMinProgramTexelOffset;
645     int mMaxProgramTexelOffset;
646 
647     int mMinProgramTextureGatherOffset;
648     int mMaxProgramTextureGatherOffset;
649 
650     // keep track of local group size declared in layout. It should be declared only once.
651     bool mComputeShaderLocalSizeDeclared;
652     sh::WorkGroupSize mComputeShaderLocalSize;
653     // keep track of number of views declared in layout.
654     int mNumViews;
655     int mMaxNumViews;
656     int mMaxImageUnits;
657     int mMaxCombinedTextureImageUnits;
658     int mMaxUniformLocations;
659     int mMaxUniformBufferBindings;
660     int mMaxVertexAttribs;
661     int mMaxAtomicCounterBindings;
662     int mMaxShaderStorageBufferBindings;
663 
664     // keeps track whether we are declaring / defining a function
665     bool mDeclaringFunction;
666 
667     // Track the state of each atomic counter binding.
668     std::map<int, AtomicCounterBindingState> mAtomicCounterBindingStates;
669 
670     // Track the geometry shader global parameters declared in layout.
671     TLayoutPrimitiveType mGeometryShaderInputPrimitiveType;
672     TLayoutPrimitiveType mGeometryShaderOutputPrimitiveType;
673     int mGeometryShaderInvocations;
674     int mGeometryShaderMaxVertices;
675     int mMaxGeometryShaderInvocations;
676     int mMaxGeometryShaderMaxVertices;
677 
678     // Track when we add new scope for func body in ESSL 1.00 spec
679     bool mFunctionBodyNewScope;
680 
681     ShShaderOutput mOutputType;
682 };
683 
684 int PaParseStrings(size_t count,
685                    const char *const string[],
686                    const int length[],
687                    TParseContext *context);
688 
689 }  // namespace sh
690 
691 #endif  // COMPILER_TRANSLATOR_PARSECONTEXT_H_
692