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 "ir/configuration.h"
27#include "ir/pass_manager.h"
28#include "lib/compile_context.h"
29#include "lib/cstring.h"
30#include "lib/options.h"
31
32namespace P4 {
33
34class ToP4;
35
38extern const char *p4includePath;
39extern const char *p4_14includePath;
40
42bool isSystemFile(cstring filename);
43
49 std::set<cstring> disabledAnnotations;
50
52 mutable size_t dump_uid = 0;
53
54 protected:
57 void dumpPass(const char *manager, unsigned seq, const char *pass, const IR::Node *node) const;
58
61 virtual std::unique_ptr<ToP4> getToP4(std::ostream *, bool, std::filesystem::path) const;
62
63 public:
64 explicit ParserOptions(std::string_view defaultMessage = "Parse a P4 program");
65
66 std::vector<const char *> *process(int argc, char *const argv[]) override;
67 enum class FrontendVersion { P4_14, P4_16 };
68
70 static void closeFile(FILE *file);
72 using PreprocessorResult = std::unique_ptr<FILE, decltype(&closeFile)>;
73
77 FrontendVersion langVersion = FrontendVersion::P4_16;
79 cstring preprocessor_options = cstring::empty;
81 std::filesystem::path file;
83 bool doNotCompile = false;
87 bool doNotPreprocess = false;
89 std::vector<cstring> top4;
91 std::filesystem::path dumpFolder = ".";
95 void setInputFile();
97 const char *getIncludePath() const override;
99 std::optional<ParserOptions::PreprocessorResult> preprocess() const;
101 bool isv1() const;
104 DebugHook getDebugHook() const;
106 bool isAnnotationDisabled(const IR::Annotation *a) const;
109 bool searchForIncludePath(const char *&includePathOut, std::vector<cstring> relativePaths,
110 const char *);
113 bool noIncludes = false;
114};
115
119 public:
122 static P4CContext &get();
123
127 static const P4CConfiguration &getConfig();
128
129 P4CContext() {}
130
132 virtual ParserOptions &options() = 0;
133
138
143
148
153
155 void setDiagnosticAction(std::string_view diagnostic, DiagnosticAction action) {
156 errorReporter().setDiagnosticAction(diagnostic, action);
157 }
158
159 protected:
163 virtual bool isRecognizedDiagnostic(cstring diagnostic);
164
167 virtual const P4CConfiguration &getConfigImpl();
168};
169
173template <typename OptionsType>
174class P4CContextWithOptions final : public P4CContext {
175 public:
181
183
184 template <typename OptionsDerivedType>
185 P4CContextWithOptions(P4CContextWithOptions<OptionsDerivedType> &context) {
186 optionsInstance = context.options();
187 }
188
189 template <typename OptionsDerivedType>
190 P4CContextWithOptions &operator=(P4CContextWithOptions<OptionsDerivedType> &context) {
191 optionsInstance = context.options();
192 }
193
195 OptionsType &options() override { return optionsInstance; }
196
197 private:
199 OptionsType optionsInstance;
200};
201
202} // namespace P4
203
204#endif /* FRONTENDS_COMMON_PARSER_OPTIONS_H_*/
Definition compile_context.h:85
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:251
void setDiagnosticAction(std::string_view diagnostic, DiagnosticAction action)
Set the action to take for the given diagnostic.
Definition error_reporter.h:243
DiagnosticAction getDefaultWarningDiagnosticAction()
Definition error_reporter.h:248
DiagnosticAction getDefaultInfoDiagnosticAction()
Definition error_reporter.h:256
void setDefaultInfoDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to P4::info().
Definition error_reporter.h:259
Definition node.h:95
Definition ir/configuration.h:24
Definition parser_options.h:118
static P4CContext & get()
Definition parser_options.cpp:539
virtual bool isRecognizedDiagnostic(cstring diagnostic)
Definition parser_options.cpp:546
void setDefaultInfoDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to P4::info().
Definition parser_options.h:140
void setDefaultWarningDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to P4::warning().
Definition parser_options.h:150
void setDiagnosticAction(std::string_view diagnostic, DiagnosticAction action)
Set the action to take for the given diagnostic.
Definition parser_options.h:155
static const P4CConfiguration & getConfig()
Definition parser_options.cpp:541
virtual const P4CConfiguration & getConfigImpl()
Definition parser_options.cpp:552
virtual ParserOptions & options()=0
DiagnosticAction getDefaultWarningDiagnosticAction() final
Definition parser_options.h:145
DiagnosticAction getDefaultInfoDiagnosticAction() final
Definition parser_options.h:135
Definition parser_options.h:174
OptionsType & options() override
Definition parser_options.h:195
static P4CContextWithOptions & get()
Definition parser_options.h:178
Definition parser_options.h:47
cstring compilerVersion
Compiler version.
Definition parser_options.h:85
std::filesystem::path file
file to compile (- for stdin)
Definition parser_options.h:81
bool isv1() const
True if we are compiling a P4 v1.0 or v1.1 program.
Definition parser_options.cpp:473
bool noIncludes
Definition parser_options.h:113
cstring exe_name
Name of executable that is being run.
Definition parser_options.h:75
FrontendVersion langVersion
Which language to compile.
Definition parser_options.h:77
bool doNotCompile
if true preprocess only
Definition parser_options.h:83
void dumpPass(const char *manager, unsigned seq, const char *pass, const IR::Node *node) const
Definition parser_options.cpp:475
bool optimizeParserInlining
If false, optimization of callee parsers (subparsers) inlining is disabled.
Definition parser_options.h:93
bool isAnnotationDisabled(const IR::Annotation *a) const
Check whether this particular annotation was disabled.
Definition parser_options.cpp:524
std::optional< ParserOptions::PreprocessorResult > preprocess() const
Returns the output of the preprocessor.
Definition parser_options.cpp:424
std::vector< const char * > * process(int argc, char *const argv[]) override
Definition parser_options.cpp:397
std::vector< cstring > top4
substrings matched against pass names
Definition parser_options.h:89
std::unique_ptr< FILE, decltype(&closeFile)> PreprocessorResult
Records the result of the preprocessor.
Definition parser_options.h:72
cstring preprocessor_options
options to pass to preprocessor
Definition parser_options.h:79
const char * getIncludePath() const override
Return target specific include path.
Definition parser_options.cpp:411
bool searchForIncludePath(const char *&includePathOut, std::vector< cstring > relativePaths, const char *)
Definition parser_options.cpp:372
bool doNotPreprocess
if true skip preprocess
Definition parser_options.h:87
std::filesystem::path dumpFolder
debugging dumps of programs written in this folder
Definition parser_options.h:91
static void closeFile(FILE *file)
Tries to close the input stream associated with the result.
Definition parser_options.cpp:52
DebugHook getDebugHook() const
Definition parser_options.cpp:533
virtual std::unique_ptr< ToP4 > getToP4(std::ostream *, bool, std::filesystem::path) const
Definition parser_options.cpp:519
void setInputFile()
Expect that the only remaining argument is the input file.
Definition parser_options.cpp:343
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
bool isSystemFile(cstring file)
Try to guess whether a file is a "system" file.
Definition parser_options.cpp:50
DiagnosticAction
An action to take when a diagnostic message is triggered.
Definition error_reporter.h:37
const char * p4includePath
Definition parser_options.cpp:45
std::function< void(const char *manager, unsigned seqNo, const char *pass, const IR::Node *node)> DebugHook
Definition ir/pass_manager.h:38
static CompileContextType & top()
Definition compile_context.h:48