P4C
The P4 Compiler
Loading...
Searching...
No Matches
parser_options.h
1/*
2Copyright 2013-present Barefoot Networks, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17/* -*-C++-*- */
18
19#ifndef FRONTENDS_COMMON_PARSER_OPTIONS_H_
20#define FRONTENDS_COMMON_PARSER_OPTIONS_H_
21
22#include <cstdio>
23#include <filesystem>
24#include <set>
25
26#include "../p4/metrics/metricsStructure.h"
27#include "ir/configuration.h"
28#include "ir/pass_manager.h"
29#include "lib/compile_context.h"
30#include "lib/cstring.h"
31#include "lib/options.h"
32
33namespace P4 {
34
35class ToP4;
36
39extern std::filesystem::path p4includePath;
40extern std::filesystem::path p4_14includePath;
41
43bool isSystemFile(cstring filename);
44bool isSystemFile(const std::filesystem::path &filename);
45
49class ParserOptions : public Util::Options {
51 std::set<cstring> disabledAnnotations;
52
54 mutable size_t dump_uid = 0;
55
56 protected:
59 void dumpPass(const char *manager, unsigned seq, const char *pass, const IR::Node *node) const;
60
63 virtual std::unique_ptr<ToP4> getToP4(std::ostream *, bool, std::filesystem::path) const;
64
65 public:
66 explicit ParserOptions(std::string_view defaultMessage = "Parse a P4 program");
67
68 std::vector<const char *> *process(int argc, char *const argv[]) override;
69 enum class FrontendVersion { P4_14, P4_16 };
70
72 static void closeFile(FILE *file);
74 using PreprocessorResult = std::unique_ptr<FILE, decltype(&closeFile)>;
75
79 FrontendVersion langVersion = FrontendVersion::P4_16;
81 cstring preprocessor_options = cstring::empty;
83 std::filesystem::path file;
85 bool doNotCompile = false;
89 bool doNotPreprocess = false;
91 std::vector<cstring> top4;
93 std::filesystem::path dumpFolder = ".";
97 void setInputFile();
99 const char *getIncludePath() const override;
101 std::optional<ParserOptions::PreprocessorResult> preprocess() const;
103 bool isv1() const;
106 DebugHook getDebugHook() const;
108 bool isAnnotationDisabled(const IR::Annotation *a) const;
111 static bool searchForIncludePath(std::filesystem::path &includePathOut,
112 const std::vector<cstring> &userSpecifiedPaths,
113 const std::filesystem::path &exename);
116 bool noIncludes = false;
119};
120
123class P4CContext : public BaseCompileContext {
124 public:
127 static P4CContext &get();
128
132 static const P4CConfiguration &getConfig();
133
134 P4CContext() {}
135
137 virtual ParserOptions &options() = 0;
138
143
148
153
158
160 void setDiagnosticAction(std::string_view diagnostic, DiagnosticAction action) {
161 errorReporter().setDiagnosticAction(diagnostic, action);
162 }
163
164 protected:
168 virtual bool isRecognizedDiagnostic(cstring diagnostic);
169
172 virtual const P4CConfiguration &getConfigImpl();
173};
174
178template <typename OptionsType>
179class P4CContextWithOptions final : public P4CContext {
180 public:
183 static P4CContextWithOptions &get() {
185 }
186
188
189 template <typename OptionsDerivedType>
190 P4CContextWithOptions(P4CContextWithOptions<OptionsDerivedType> &context) {
191 optionsInstance = context.options();
192 }
193
194 template <typename OptionsDerivedType>
195 P4CContextWithOptions &operator=(P4CContextWithOptions<OptionsDerivedType> &context) {
196 optionsInstance = context.options();
197 }
198
200 OptionsType &options() override { return optionsInstance; }
201
202 private:
204 OptionsType optionsInstance;
205};
206
207} // namespace P4
208
209#endif /* FRONTENDS_COMMON_PARSER_OPTIONS_H_*/
virtual ErrorReporter & errorReporter()
Definition compile_context.cpp:65
void setDefaultWarningDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to P4::warning().
Definition error_reporter.h:253
void setDiagnosticAction(std::string_view diagnostic, DiagnosticAction action)
Set the action to take for the given diagnostic.
Definition error_reporter.h:245
DiagnosticAction getDefaultWarningDiagnosticAction()
Definition error_reporter.h:250
DiagnosticAction getDefaultInfoDiagnosticAction()
Definition error_reporter.h:258
void setDefaultInfoDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to P4::info().
Definition error_reporter.h:261
Definition node.h:94
Definition ir/configuration.h:24
static P4CContext & get()
Definition parser_options.cpp:527
virtual bool isRecognizedDiagnostic(cstring diagnostic)
Definition parser_options.cpp:534
void setDefaultInfoDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to P4::info().
Definition parser_options.h:145
void setDefaultWarningDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to P4::warning().
Definition parser_options.h:155
void setDiagnosticAction(std::string_view diagnostic, DiagnosticAction action)
Set the action to take for the given diagnostic.
Definition parser_options.h:160
static const P4CConfiguration & getConfig()
Definition parser_options.cpp:529
virtual const P4CConfiguration & getConfigImpl()
Definition parser_options.cpp:540
virtual ParserOptions & options()=0
DiagnosticAction getDefaultWarningDiagnosticAction() final
Definition parser_options.h:150
DiagnosticAction getDefaultInfoDiagnosticAction() final
Definition parser_options.h:140
Definition parser_options.h:179
OptionsType & options() override
Definition parser_options.h:200
static P4CContextWithOptions & get()
Definition parser_options.h:183
Definition parser_options.h:49
cstring compilerVersion
Compiler version.
Definition parser_options.h:87
std::filesystem::path file
file to compile (- for stdin)
Definition parser_options.h:83
static bool searchForIncludePath(std::filesystem::path &includePathOut, const std::vector< cstring > &userSpecifiedPaths, const std::filesystem::path &exename)
Definition parser_options.cpp:363
bool isv1() const
True if we are compiling a P4 v1.0 or v1.1 program.
Definition parser_options.cpp:461
bool noIncludes
Definition parser_options.h:116
cstring exe_name
Name of executable that is being run.
Definition parser_options.h:77
FrontendVersion langVersion
Which language to compile.
Definition parser_options.h:79
bool doNotCompile
if true preprocess only
Definition parser_options.h:85
void dumpPass(const char *manager, unsigned seq, const char *pass, const IR::Node *node) const
Definition parser_options.cpp:463
bool optimizeParserInlining
If false, optimization of callee parsers (subparsers) inlining is disabled.
Definition parser_options.h:95
bool isAnnotationDisabled(const IR::Annotation *a) const
Check whether this particular annotation was disabled.
Definition parser_options.cpp:512
std::optional< ParserOptions::PreprocessorResult > preprocess() const
Returns the output of the preprocessor.
Definition parser_options.cpp:412
Metrics metrics
Holds code metric values, makes them accessible during the entire compilation.
Definition parser_options.h:118
std::vector< const char * > * process(int argc, char *const argv[]) override
Definition parser_options.cpp:376
std::vector< cstring > top4
substrings matched against pass names
Definition parser_options.h:91
std::unique_ptr< FILE, decltype(&closeFile)> PreprocessorResult
Records the result of the preprocessor.
Definition parser_options.h:74
cstring preprocessor_options
options to pass to preprocessor
Definition parser_options.h:81
const char * getIncludePath() const override
Return target specific include path.
Definition parser_options.cpp:399
bool doNotPreprocess
if true skip preprocess
Definition parser_options.h:89
std::filesystem::path dumpFolder
debugging dumps of programs written in this folder
Definition parser_options.h:93
static void closeFile(FILE *file)
Tries to close the input stream associated with the result.
Definition parser_options.cpp:57
DebugHook getDebugHook() const
Definition parser_options.cpp:521
virtual std::unique_ptr< ToP4 > getToP4(std::ostream *, bool, std::filesystem::path) const
Definition parser_options.cpp:507
void setInputFile()
Expect that the only remaining argument is the input file.
Definition parser_options.cpp:348
Definition toP4.h:36
Definition lib/options.h:34
Definition cstring.h:85
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
DiagnosticAction
An action to take when a diagnostic message is triggered.
Definition error_reporter.h:37
std::function< void(const char *manager, unsigned seqNo, const char *pass, const IR::Node *node)> DebugHook
Definition ir/pass_manager.h:38
const char * exename(const char *argv0)
Definition exename.cpp:87
std::filesystem::path p4includePath
Definition parser_options.cpp:46
Definition metricsStructure.h:119
static CompileContextType & top()
Definition compile_context.h:48