blob: 0b6a5e2f96d1fbf5bd61c5271039c565f2fce715 [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),
42 diagnostics(is),
Nicolas Capensc6841852015-02-15 14:25:37 -050043 shaderVersion(100),
44 directiveHandler(ext, diagnostics, shaderVersion),
John Bauman66b8ab22014-05-06 15:57:45 -040045 preprocessor(&diagnostics, &directiveHandler),
46 scanner(NULL) { }
47 TIntermediate& intermediate; // to hold and build a parse tree
48 TSymbolTable& symbolTable; // symbol table that goes with the language currently being parsed
Nicolas Capens08ca3c62015-02-13 16:06:45 -050049 GLenum shaderType; // vertex or fragment language (future: pack or unpack)
Nicolas Capensc6841852015-02-15 14:25:37 -050050 int shaderVersion;
John Bauman66b8ab22014-05-06 15:57:45 -040051 int compileOptions;
52 const char* sourcePath; // Path of source file or NULL.
53 TIntermNode* treeRoot; // root of parse tree being created
54 bool lexAfterType; // true if we've recognized a type, so can only be looking for an identifier
55 int loopNestingLevel; // 0 if outside all loops
56 int structNestingLevel; // incremented while parsing a struct declaration
57 bool inTypeParen; // true if in parentheses, looking only for an identifier
58 const TType* currentFunctionType; // the return type of the function that's currently being parsed
59 bool functionReturnsValue; // true if a non-void function has a return
60 bool checksPrecisionErrors; // true if an error will be generated when a variable is declared without precision, explicit or implicit.
61 TString HashErrMsg;
62 bool AfterEOF;
63 TDiagnostics diagnostics;
64 TDirectiveHandler directiveHandler;
65 pp::Preprocessor preprocessor;
66 void* scanner;
67
Nicolas Capensc6841852015-02-15 14:25:37 -050068 int getShaderVersion() const { return shaderVersion; }
John Bauman66b8ab22014-05-06 15:57:45 -040069 int numErrors() const { return diagnostics.numErrors(); }
70 TInfoSink& infoSink() { return diagnostics.infoSink(); }
71 void error(TSourceLoc loc, const char *reason, const char* token,
72 const char* extraInfo="");
73 void warning(TSourceLoc loc, const char* reason, const char* token,
74 const char* extraInfo="");
75 void trace(const char* str);
76 void recover();
77
78 bool parseVectorFields(const TString&, int vecSize, TVectorFields&, int line);
79 bool parseMatrixFields(const TString&, int matSize, TMatrixFields&, int line);
80
81 bool reservedErrorCheck(int line, const TString& identifier);
82 void assignError(int line, const char* op, TString left, TString right);
83 void unaryOpError(int line, const char* op, TString operand);
84 void binaryOpError(int line, const char* op, TString left, TString right);
85 bool precisionErrorCheck(int line, TPrecision precision, TBasicType type);
86 bool lValueErrorCheck(int line, const char* op, TIntermTyped*);
87 bool constErrorCheck(TIntermTyped* node);
88 bool integerErrorCheck(TIntermTyped* node, const char* token);
89 bool globalErrorCheck(int line, bool global, const char* token);
90 bool constructorErrorCheck(int line, TIntermNode*, TFunction&, TOperator, TType*);
91 bool arraySizeErrorCheck(int line, TIntermTyped* expr, int& size);
92 bool arrayQualifierErrorCheck(int line, TPublicType type);
93 bool arrayTypeErrorCheck(int line, TPublicType type);
94 bool arrayErrorCheck(int line, TString& identifier, TPublicType type, TVariable*& variable);
95 bool voidErrorCheck(int, const TString&, const TPublicType&);
96 bool boolErrorCheck(int, const TIntermTyped*);
97 bool boolErrorCheck(int, const TPublicType&);
98 bool samplerErrorCheck(int line, const TPublicType& pType, const char* reason);
99 bool structQualifierErrorCheck(int line, const TPublicType& pType);
100 bool parameterSamplerErrorCheck(int line, TQualifier qualifier, const TType& type);
101 bool nonInitConstErrorCheck(int line, TString& identifier, TPublicType& type, bool array);
102 bool nonInitErrorCheck(int line, TString& identifier, TPublicType& type, TVariable*& variable);
103 bool paramErrorCheck(int line, TQualifier qualifier, TQualifier paramQualifier, TType* type);
104 bool extensionErrorCheck(int line, const TString&);
105
106 const TExtensionBehavior& extensionBehavior() const { return directiveHandler.extensionBehavior(); }
107 bool supportsExtension(const char* extension);
108 void handleExtensionDirective(int line, const char* extName, const char* behavior);
109
110 const TPragma& pragma() const { return directiveHandler.pragma(); }
111 void handlePragmaDirective(int line, const char* name, const char* value);
112
113 bool containsSampler(TType& type);
114 bool areAllChildConst(TIntermAggregate* aggrNode);
115 const TFunction* findFunction(int line, TFunction* pfnCall, bool *builtIn = 0);
116 bool executeInitializer(TSourceLoc line, TString& identifier, TPublicType& pType,
117 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable = 0);
118 bool arraySetMaxSize(TIntermSymbol*, TType*, int, bool, TSourceLoc);
119
120 TIntermTyped* addConstructor(TIntermNode*, const TType*, TOperator, TFunction*, TSourceLoc);
121 TIntermTyped* foldConstConstructor(TIntermAggregate* aggrNode, const TType& type);
John Bauman66b8ab22014-05-06 15:57:45 -0400122 TIntermTyped* addConstVectorNode(TVectorFields&, TIntermTyped*, TSourceLoc);
123 TIntermTyped* addConstMatrixNode(int , TIntermTyped*, TSourceLoc);
124 TIntermTyped* addConstArrayNode(int index, TIntermTyped* node, TSourceLoc line);
125 TIntermTyped* addConstStruct(TString& , TIntermTyped*, TSourceLoc);
126
127 // Performs an error check for embedded struct declarations.
128 // Returns true if an error was raised due to the declaration of
129 // this struct.
130 bool enterStructDeclaration(TSourceLoc line, const TString& identifier);
131 void exitStructDeclaration();
132
133 bool structNestingErrorCheck(TSourceLoc line, const TType& fieldType);
134};
135
136int PaParseStrings(int count, const char* const string[], const int length[],
137 TParseContext* context);
138
139#endif // _PARSER_HELPER_INCLUDED_