blob: 72f5a488ba2ec3fce79878da7dab01f1a94e26e6 [file] [log] [blame]
John Bauman66b8ab22014-05-06 15:57:45 -04001//
2// Copyright (c) 2002-2012 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 _PARSER_HELPER_INCLUDED_
7#define _PARSER_HELPER_INCLUDED_
8
Nicolas Capenscc863da2015-01-21 15:50:55 -05009#include "Diagnostics.h"
10#include "DirectiveHandler.h"
11#include "localintermediate.h"
12#include "preprocessor/Preprocessor.h"
Nicolas Capensd8cbf392015-02-10 15:35:11 -050013#include "Compiler.h"
Nicolas Capenscc863da2015-01-21 15:50:55 -050014#include "SymbolTable.h"
John Bauman66b8ab22014-05-06 15:57:45 -040015
16struct TMatrixFields {
17 bool wholeRow;
18 bool wholeCol;
19 int row;
20 int col;
21};
22
23//
24// The following are extra variables needed during parsing, grouped together so
25// they can be passed to the parser without needing a global.
26//
27struct TParseContext {
Nicolas Capens08ca3c62015-02-13 16:06:45 -050028 TParseContext(TSymbolTable& symt, TExtensionBehavior& ext, TIntermediate& interm, GLenum type, int options, bool checksPrecErrors, const char* sourcePath, TInfoSink& is) :
John Bauman66b8ab22014-05-06 15:57:45 -040029 intermediate(interm),
30 symbolTable(symt),
31 shaderType(type),
John Bauman66b8ab22014-05-06 15:57:45 -040032 compileOptions(options),
33 sourcePath(sourcePath),
34 treeRoot(0),
35 lexAfterType(false),
36 loopNestingLevel(0),
37 structNestingLevel(0),
38 inTypeParen(false),
39 currentFunctionType(NULL),
40 functionReturnsValue(false),
41 checksPrecisionErrors(checksPrecErrors),
Alexis Hetuad6b8752015-06-09 16:15:30 -040042 defaultMatrixPacking(EmpColumnMajor),
43 defaultBlockStorage(EbsShared),
John Bauman66b8ab22014-05-06 15:57:45 -040044 diagnostics(is),
Nicolas Capens7d626792015-02-17 17:58:31 -050045 shaderVersion(100),
Nicolas Capensc6841852015-02-15 14:25:37 -050046 directiveHandler(ext, diagnostics, shaderVersion),
John Bauman66b8ab22014-05-06 15:57:45 -040047 preprocessor(&diagnostics, &directiveHandler),
48 scanner(NULL) { }
49 TIntermediate& intermediate; // to hold and build a parse tree
50 TSymbolTable& symbolTable; // symbol table that goes with the language currently being parsed
Nicolas Capens08ca3c62015-02-13 16:06:45 -050051 GLenum shaderType; // vertex or fragment language (future: pack or unpack)
Nicolas Capensc6841852015-02-15 14:25:37 -050052 int shaderVersion;
John Bauman66b8ab22014-05-06 15:57:45 -040053 int compileOptions;
54 const char* sourcePath; // Path of source file or NULL.
55 TIntermNode* treeRoot; // root of parse tree being created
56 bool lexAfterType; // true if we've recognized a type, so can only be looking for an identifier
57 int loopNestingLevel; // 0 if outside all loops
58 int structNestingLevel; // incremented while parsing a struct declaration
59 bool inTypeParen; // true if in parentheses, looking only for an identifier
60 const TType* currentFunctionType; // the return type of the function that's currently being parsed
61 bool functionReturnsValue; // true if a non-void function has a return
62 bool checksPrecisionErrors; // true if an error will be generated when a variable is declared without precision, explicit or implicit.
Alexis Hetuad6b8752015-06-09 16:15:30 -040063 TLayoutMatrixPacking defaultMatrixPacking;
64 TLayoutBlockStorage defaultBlockStorage;
John Bauman66b8ab22014-05-06 15:57:45 -040065 TString HashErrMsg;
66 bool AfterEOF;
67 TDiagnostics diagnostics;
68 TDirectiveHandler directiveHandler;
69 pp::Preprocessor preprocessor;
70 void* scanner;
71
Nicolas Capensc6841852015-02-15 14:25:37 -050072 int getShaderVersion() const { return shaderVersion; }
John Bauman66b8ab22014-05-06 15:57:45 -040073 int numErrors() const { return diagnostics.numErrors(); }
74 TInfoSink& infoSink() { return diagnostics.infoSink(); }
75 void error(TSourceLoc loc, const char *reason, const char* token,
76 const char* extraInfo="");
77 void warning(TSourceLoc loc, const char* reason, const char* token,
78 const char* extraInfo="");
79 void trace(const char* str);
80 void recover();
81
82 bool parseVectorFields(const TString&, int vecSize, TVectorFields&, int line);
Alexis Hetu00106d42015-04-23 11:45:35 -040083 bool parseMatrixFields(const TString&, int matCols, int matRows, TMatrixFields&, int line);
John Bauman66b8ab22014-05-06 15:57:45 -040084
85 bool reservedErrorCheck(int line, const TString& identifier);
86 void assignError(int line, const char* op, TString left, TString right);
87 void unaryOpError(int line, const char* op, TString operand);
88 void binaryOpError(int line, const char* op, TString left, TString right);
89 bool precisionErrorCheck(int line, TPrecision precision, TBasicType type);
90 bool lValueErrorCheck(int line, const char* op, TIntermTyped*);
91 bool constErrorCheck(TIntermTyped* node);
92 bool integerErrorCheck(TIntermTyped* node, const char* token);
93 bool globalErrorCheck(int line, bool global, const char* token);
94 bool constructorErrorCheck(int line, TIntermNode*, TFunction&, TOperator, TType*);
95 bool arraySizeErrorCheck(int line, TIntermTyped* expr, int& size);
96 bool arrayQualifierErrorCheck(int line, TPublicType type);
97 bool arrayTypeErrorCheck(int line, TPublicType type);
98 bool arrayErrorCheck(int line, TString& identifier, TPublicType type, TVariable*& variable);
99 bool voidErrorCheck(int, const TString&, const TPublicType&);
100 bool boolErrorCheck(int, const TIntermTyped*);
101 bool boolErrorCheck(int, const TPublicType&);
102 bool samplerErrorCheck(int line, const TPublicType& pType, const char* reason);
103 bool structQualifierErrorCheck(int line, const TPublicType& pType);
104 bool parameterSamplerErrorCheck(int line, TQualifier qualifier, const TType& type);
105 bool nonInitConstErrorCheck(int line, TString& identifier, TPublicType& type, bool array);
106 bool nonInitErrorCheck(int line, TString& identifier, TPublicType& type, TVariable*& variable);
107 bool paramErrorCheck(int line, TQualifier qualifier, TQualifier paramQualifier, TType* type);
108 bool extensionErrorCheck(int line, const TString&);
Alexis Hetuad6b8752015-06-09 16:15:30 -0400109 bool layoutLocationErrorCheck(const TSourceLoc& location, const TLayoutQualifier &layoutQualifier);
110 bool functionCallLValueErrorCheck(const TFunction *fnCandidate, TIntermAggregate *);
John Bauman66b8ab22014-05-06 15:57:45 -0400111
112 const TExtensionBehavior& extensionBehavior() const { return directiveHandler.extensionBehavior(); }
113 bool supportsExtension(const char* extension);
114 void handleExtensionDirective(int line, const char* extName, const char* behavior);
115
116 const TPragma& pragma() const { return directiveHandler.pragma(); }
117 void handlePragmaDirective(int line, const char* name, const char* value);
118
119 bool containsSampler(TType& type);
120 bool areAllChildConst(TIntermAggregate* aggrNode);
121 const TFunction* findFunction(int line, TFunction* pfnCall, bool *builtIn = 0);
122 bool executeInitializer(TSourceLoc line, TString& identifier, TPublicType& pType,
123 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable = 0);
Alexis Hetu42ff6b12015-06-03 16:03:48 -0400124
125 TPublicType addFullySpecifiedType(TQualifier qualifier, bool invariant, TLayoutQualifier layoutQualifier, const TPublicType &typeSpecifier);
John Bauman66b8ab22014-05-06 15:57:45 -0400126 bool arraySetMaxSize(TIntermSymbol*, TType*, int, bool, TSourceLoc);
127
128 TIntermTyped* addConstructor(TIntermNode*, const TType*, TOperator, TFunction*, TSourceLoc);
129 TIntermTyped* foldConstConstructor(TIntermAggregate* aggrNode, const TType& type);
John Bauman66b8ab22014-05-06 15:57:45 -0400130 TIntermTyped* addConstVectorNode(TVectorFields&, TIntermTyped*, TSourceLoc);
131 TIntermTyped* addConstMatrixNode(int , TIntermTyped*, TSourceLoc);
132 TIntermTyped* addConstArrayNode(int index, TIntermTyped* node, TSourceLoc line);
Alexis Hetuad6b8752015-06-09 16:15:30 -0400133 TIntermTyped* addConstStruct(const TString& , TIntermTyped*, TSourceLoc);
134 TIntermTyped *addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc& location, TIntermTyped *indexExpression);
135 TIntermTyped* addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc &dotLocation, const TString &fieldString, const TSourceLoc &fieldLocation);
136
137 TFieldList *addStructDeclaratorList(const TPublicType &typeSpecifier, TFieldList *fieldList);
138 TPublicType addStructure(const TSourceLoc &structLine, const TSourceLoc &nameLine, const TString *structName, TFieldList *fieldList);
John Bauman66b8ab22014-05-06 15:57:45 -0400139
Nicolas Capens7d626792015-02-17 17:58:31 -0500140 TLayoutQualifier parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine);
141 TLayoutQualifier parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine);
142 TLayoutQualifier joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier);
Alexis Hetu55a2cbc2015-04-16 10:49:45 -0400143 TPublicType joinInterpolationQualifiers(const TSourceLoc &interpolationLoc, TQualifier interpolationQualifier, const TSourceLoc &storageLoc, TQualifier storageQualifier);
Nicolas Capens7d626792015-02-17 17:58:31 -0500144
John Bauman66b8ab22014-05-06 15:57:45 -0400145 // Performs an error check for embedded struct declarations.
146 // Returns true if an error was raised due to the declaration of
147 // this struct.
148 bool enterStructDeclaration(TSourceLoc line, const TString& identifier);
149 void exitStructDeclaration();
150
Alexis Hetuad6b8752015-06-09 16:15:30 -0400151 bool structNestingErrorCheck(const TSourceLoc &line, const TField &field);
152
153 TIntermTyped *addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc);
154 TIntermTyped *addUnaryMathLValue(TOperator op, TIntermTyped *child, const TSourceLoc &loc);
155
156private:
157 // The funcReturnType parameter is expected to be non-null when the operation is a built-in function.
158 // It is expected to be null for other unary operators.
159 TIntermTyped *createUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc, const TType *funcReturnType);
160
161 // Return true if the checks pass
162 bool binaryOpCommonCheck(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc);
John Bauman66b8ab22014-05-06 15:57:45 -0400163};
164
165int PaParseStrings(int count, const char* const string[], const int length[],
166 TParseContext* context);
167
168#endif // _PARSER_HELPER_INCLUDED_