1// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef COMPILER_PREPROCESSOR_DIRECTIVE_PARSER_H_
16#define COMPILER_PREPROCESSOR_DIRECTIVE_PARSER_H_
17
18#include "Lexer.h"
19#include "Macro.h"
20#include "pp_utils.h"
21#include "SourceLocation.h"
22
23namespace pp
24{
25
26class Diagnostics;
27class DirectiveHandler;
28class Tokenizer;
29
30class DirectiveParser : public Lexer
31{
32public:
33 DirectiveParser(Tokenizer *tokenizer,
34 MacroSet *macroSet,
35 Diagnostics *diagnostics,
36 DirectiveHandler *directiveHandler,
37 int maxMacroExpansionDepth);
38 ~DirectiveParser() override;
39
40 void lex(Token *token) override;
41
42private:
43 PP_DISALLOW_COPY_AND_ASSIGN(DirectiveParser);
44
45 void parseDirective(Token *token);
46 void parseDefine(Token *token);
47 void parseUndef(Token *token);
48 void parseIf(Token *token);
49 void parseIfdef(Token *token);
50 void parseIfndef(Token *token);
51 void parseElse(Token *token);
52 void parseElif(Token *token);
53 void parseEndif(Token *token);
54 void parseError(Token *token);
55 void parsePragma(Token *token);
56 void parseExtension(Token *token);
57 void parseVersion(Token *token);
58 void parseLine(Token *token);
59
60 bool skipping() const;
61 void parseConditionalIf(Token *token);
62 int parseExpressionIf(Token *token);
63 int parseExpressionIfdef(Token *token);
64
65 struct ConditionalBlock
66 {
67 std::string type;
68 SourceLocation location;
69 bool skipBlock;
70 bool skipGroup;
71 bool foundValidGroup;
72 bool foundElseGroup;
73
74 ConditionalBlock() :
75 skipBlock(false),
76 skipGroup(false),
77 foundValidGroup(false),
78 foundElseGroup(false)
79 {
80 }
81 };
82 bool mPastFirstStatement;
83 bool mSeenNonPreprocessorToken; // Tracks if a non-preprocessor token has been seen yet. Some
84 // macros, such as
85 // #extension must be declared before all shader code.
86 std::vector<ConditionalBlock> mConditionalStack;
87 Tokenizer *mTokenizer;
88 MacroSet *mMacroSet;
89 Diagnostics *mDiagnostics;
90 DirectiveHandler *mDirectiveHandler;
91 int mShaderVersion;
92 int mMaxMacroExpansionDepth;
93};
94
95} // namespace pp
96#endif // COMPILER_PREPROCESSOR_DIRECTIVE_PARSER_H_
97
98