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