-
Notifications
You must be signed in to change notification settings - Fork 510
Expand file tree
/
Copy pathparser_options.h
More file actions
208 lines (170 loc) · 7.76 KB
/
parser_options.h
File metadata and controls
208 lines (170 loc) · 7.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
Copyright 2013-present Barefoot Networks, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/* -*-C++-*- */
#ifndef FRONTENDS_COMMON_PARSER_OPTIONS_H_
#define FRONTENDS_COMMON_PARSER_OPTIONS_H_
#include <cstdio>
#include <filesystem>
#include <set>
#include "ir/configuration.h"
#include "ir/pass_manager.h"
#include "lib/compile_context.h"
#include "lib/cstring.h"
#include "lib/options.h"
namespace P4 {
class ToP4;
/// Standard include paths for .p4 header files. The values are determined by
/// `configure`.
// TODO: This should be std::filesystem::path.
extern const char *p4includePath;
extern const char *p4_14includePath;
/// Try to guess whether a file is a "system" file
bool isSystemFile(cstring filename);
/// Base class for compiler options.
/// This class contains the options for the front-ends.
/// Each back-end should subclass this file.
class ParserOptions : public Util::Options {
/// Annotation names that are to be ignored by the compiler.
std::set<cstring> disabledAnnotations;
/// Used to generate dump file names.
mutable size_t dump_uid = 0;
protected:
/// Implements function that is returned by getDebugHook. The hook will take the same arguments.
/// The hook uses \ref getToP4 to obtain the P4 printer.
void dumpPass(const char *manager, unsigned seq, const char *pass, const IR::Node *node) const;
/// Obtain an instance of ToP4 or its descendant. The arguments correspond to constructor
/// arguments of ToP4.
virtual std::unique_ptr<ToP4> getToP4(std::ostream *, bool, std::filesystem::path) const;
public:
explicit ParserOptions(std::string_view defaultMessage = "Parse a P4 program");
std::vector<const char *> *process(int argc, char *const argv[]) override;
enum class FrontendVersion { P4_14, P4_16 };
/// Tries to close the input stream associated with the result.
static void closeFile(FILE *file);
/// Records the result of the preprocessor.
using PreprocessorResult = std::unique_ptr<FILE, decltype(&closeFile)>;
/// Name of executable that is being run.
cstring exe_name;
/// Which language to compile
FrontendVersion langVersion = FrontendVersion::P4_16;
/// options to pass to preprocessor
cstring preprocessor_options = cstring::empty;
/// file to compile (- for stdin)
std::filesystem::path file;
/// if true preprocess only
bool doNotCompile = false;
/// if true save preprocessed P4 to filename.p4pp
bool savePreprocessed = false;
/// Compiler version.
cstring compilerVersion;
/// if true skip preprocess
bool doNotPreprocess = false;
/// substrings matched against pass names
std::vector<cstring> top4;
/// debugging dumps of programs written in this folder
std::filesystem::path dumpFolder = ".";
/// If false, optimization of callee parsers (subparsers) inlining is disabled.
bool optimizeParserInlining = false;
/// Expect that the only remaining argument is the input file.
void setInputFile();
/// Return target specific include path.
const char *getIncludePath() const override;
/// Returns the output of the preprocessor.
std::optional<ParserOptions::PreprocessorResult> preprocess() const;
/// True if we are compiling a P4 v1.0 or v1.1 program
bool isv1() const;
/// Get a debug hook function suitable for insertion in the pass managers. The hook is
/// responsible for dumping P4 according to th --top4 and related options.
DebugHook getDebugHook() const;
/// Check whether this particular annotation was disabled
bool isAnnotationDisabled(const IR::Annotation *a) const;
/// Search and set 'includePathOut' to be the first valid path from the
/// list of possible relative paths.
static bool searchForIncludePath(const char *&includePathOut,
const std::vector<cstring> &userSpecifiedPaths,
const char *exename);
/// If true do not generate #include statements.
/// Used for debugging.
bool noIncludes = false;
};
/// A compilation context which exposes compiler options and a compiler
/// configuration.
class P4CContext : public BaseCompileContext {
public:
/// @return the current compilation context, which must inherit from
/// P4CContext.
static P4CContext &get();
/// @return the compiler configuration for the current compilation context.
/// If there is no current compilation context, the default configuration is
/// returned.
static const P4CConfiguration &getConfig();
P4CContext() {}
/// @return the compiler options for this compilation context.
virtual ParserOptions &options() = 0;
/// @return the default diagnostic action for calls to `::P4::info()`.
DiagnosticAction getDefaultInfoDiagnosticAction() final {
return errorReporter().getDefaultInfoDiagnosticAction();
}
/// set the default diagnostic action for calls to `::P4::info()`.
void setDefaultInfoDiagnosticAction(DiagnosticAction action) {
errorReporter().setDefaultInfoDiagnosticAction(action);
}
/// @return the default diagnostic action for calls to `::P4::warning()`.
DiagnosticAction getDefaultWarningDiagnosticAction() final {
return errorReporter().getDefaultWarningDiagnosticAction();
}
/// set the default diagnostic action for calls to `::P4::warning()`.
void setDefaultWarningDiagnosticAction(DiagnosticAction action) {
errorReporter().setDefaultWarningDiagnosticAction(action);
}
/// Set the action to take for the given diagnostic.
void setDiagnosticAction(std::string_view diagnostic, DiagnosticAction action) {
errorReporter().setDiagnosticAction(diagnostic, action);
}
protected:
/// @return true if the given diagnostic is known to be valid. This is
/// intended to help the user find misspelled diagnostics and the like; it
/// doesn't affect functionality.
virtual bool isRecognizedDiagnostic(cstring diagnostic);
/// @return the compiler configuration associated with this type of
/// compilation context.
virtual const P4CConfiguration &getConfigImpl();
};
/// A utility template which can be used to easily make subclasses of P4CContext
/// which expose a particular subclass of CompilerOptions. This is provided as a
/// convenience since this is all many backends need.
template <typename OptionsType>
class P4CContextWithOptions final : public P4CContext {
public:
/// @return the current compilation context, which must be of type
/// P4CContextWithOptions<OptionsType>.
static P4CContextWithOptions &get() {
return CompileContextStack::top<P4CContextWithOptions>();
}
P4CContextWithOptions() {}
template <typename OptionsDerivedType>
P4CContextWithOptions(P4CContextWithOptions<OptionsDerivedType> &context) {
optionsInstance = context.options();
}
template <typename OptionsDerivedType>
P4CContextWithOptions &operator=(P4CContextWithOptions<OptionsDerivedType> &context) {
optionsInstance = context.options();
}
/// @return the compiler options for this compilation context.
OptionsType &options() override { return optionsInstance; }
private:
/// Compiler options for this compilation context.
OptionsType optionsInstance;
};
} // namespace P4
#endif /* FRONTENDS_COMMON_PARSER_OPTIONS_H_*/