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
36extern const char *p4includePath;
37extern const char *p4_14includePath;
38
42class ParserOptions : public Util::Options {
44 std::set<cstring> disabledAnnotations;
45
47 mutable size_t dump_uid = 0;
48
49 protected:
51 void dumpPass(const char *manager, unsigned seq, const char *pass, const IR::Node *node) const;
52
53 public:
54 explicit ParserOptions(std::string_view defaultMessage = "Parse a P4 program");
55
56 std::vector<const char *> *process(int argc, char *const argv[]) override;
57 enum class FrontendVersion { P4_14, P4_16 };
58
60 static void closeFile(FILE *file);
62 using PreprocessorResult = std::unique_ptr<FILE, decltype(&closeFile)>;
63
67 FrontendVersion langVersion = FrontendVersion::P4_16;
69 cstring preprocessor_options = cstring::empty;
71 std::filesystem::path file;
73 bool doNotCompile = false;
77 bool doNotPreprocess = false;
79 std::vector<cstring> top4;
81 std::filesystem::path dumpFolder = ".";
85 void setInputFile();
87 const char *getIncludePath() const override;
89 std::optional<ParserOptions::PreprocessorResult> preprocess() const;
91 bool isv1() const;
94 DebugHook getDebugHook() const;
96 bool isAnnotationDisabled(const IR::Annotation *a) const;
99 bool searchForIncludePath(const char *&includePathOut, std::vector<cstring> relativePaths,
100 const char *);
103 bool noIncludes = false;
104};
105
108class P4CContext : public BaseCompileContext {
109 public:
112 static P4CContext &get();
113
117 static const P4CConfiguration &getConfig();
118
119 P4CContext() {}
120
122 virtual ParserOptions &options() = 0;
123
128
133
138
143
147 return errorReporter().getDiagnosticAction(diagnostic, defaultAction);
148 }
149
151 void setDiagnosticAction(std::string_view diagnostic, DiagnosticAction action) {
152 errorReporter().setDiagnosticAction(diagnostic, action);
153 }
154
155 protected:
159 virtual bool isRecognizedDiagnostic(cstring diagnostic);
160
163 virtual const P4CConfiguration &getConfigImpl();
164};
165
169template <typename OptionsType>
170class P4CContextWithOptions final : public P4CContext {
171 public:
174 static P4CContextWithOptions &get() {
176 }
177
179
180 template <typename OptionsDerivedType>
181 P4CContextWithOptions(P4CContextWithOptions<OptionsDerivedType> &context) {
182 optionsInstance = context.options();
183 }
184
185 template <typename OptionsDerivedType>
186 P4CContextWithOptions &operator=(P4CContextWithOptions<OptionsDerivedType> &context) {
187 optionsInstance = context.options();
188 }
189
191 OptionsType &options() override { return optionsInstance; }
192
193 private:
195 OptionsType optionsInstance;
196};
197
198} // namespace P4
199
200#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:250
void setDiagnosticAction(std::string_view diagnostic, DiagnosticAction action)
Set the action to take for the given diagnostic.
Definition error_reporter.h:242
DiagnosticAction getDefaultWarningDiagnosticAction()
Definition error_reporter.h:247
DiagnosticAction getDefaultInfoDiagnosticAction()
Definition error_reporter.h:255
DiagnosticAction getDiagnosticAction(cstring diagnostic, DiagnosticAction defaultAction)
Definition error_reporter.h:228
void setDefaultInfoDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to P4::info().
Definition error_reporter.h:258
Definition node.h:95
Definition ir/configuration.h:24
virtual bool isRecognizedDiagnostic(cstring diagnostic)
Definition parser_options.cpp:540
virtual const P4CConfiguration & getConfigImpl()
Definition parser_options.cpp:546
DiagnosticAction getDiagnosticAction(cstring diagnostic, DiagnosticAction defaultAction) final
Definition parser_options.h:146
void setDefaultInfoDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to P4::info().
Definition parser_options.h:130
void setDefaultWarningDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to P4::warning().
Definition parser_options.h:140
void setDiagnosticAction(std::string_view diagnostic, DiagnosticAction action)
Set the action to take for the given diagnostic.
Definition parser_options.h:151
static P4CContext & get()
Definition parser_options.cpp:533
static const P4CConfiguration & getConfig()
Definition parser_options.cpp:535
virtual ParserOptions & options()=0
DiagnosticAction getDefaultWarningDiagnosticAction() final
Definition parser_options.h:135
DiagnosticAction getDefaultInfoDiagnosticAction() final
Definition parser_options.h:125
Definition parser_options.h:170
OptionsType & options() override
Definition parser_options.h:191
static P4CContextWithOptions & get()
Definition parser_options.h:174
Definition parser_options.h:42
cstring compilerVersion
Compiler version.
Definition parser_options.h:75
std::filesystem::path file
file to compile (- for stdin)
Definition parser_options.h:71
bool isv1() const
True if we are compiling a P4 v1.0 or v1.1 program.
Definition parser_options.cpp:471
bool noIncludes
Definition parser_options.h:103
cstring exe_name
Name of executable that is being run.
Definition parser_options.h:65
FrontendVersion langVersion
Which language to compile.
Definition parser_options.h:67
bool doNotCompile
if true preprocess only
Definition parser_options.h:73
void dumpPass(const char *manager, unsigned seq, const char *pass, const IR::Node *node) const
Function that is returned by getDebugHook.
Definition parser_options.cpp:473
bool optimizeParserInlining
If false, optimization of callee parsers (subparsers) inlining is disabled.
Definition parser_options.h:83
bool isAnnotationDisabled(const IR::Annotation *a) const
Check whether this particular annotation was disabled.
Definition parser_options.cpp:518
std::optional< ParserOptions::PreprocessorResult > preprocess() const
Returns the output of the preprocessor.
Definition parser_options.cpp:422
std::vector< const char * > * process(int argc, char *const argv[]) override
Definition parser_options.cpp:395
std::vector< cstring > top4
substrings matched against pass names
Definition parser_options.h:79
std::unique_ptr< FILE, decltype(&closeFile)> PreprocessorResult
Records the result of the preprocessor.
Definition parser_options.h:62
cstring preprocessor_options
options to pass to preprocessor
Definition parser_options.h:69
const char * getIncludePath() const override
Return target specific include path.
Definition parser_options.cpp:409
bool searchForIncludePath(const char *&includePathOut, std::vector< cstring > relativePaths, const char *)
Definition parser_options.cpp:370
bool doNotPreprocess
if true skip preprocess
Definition parser_options.h:77
std::filesystem::path dumpFolder
debugging dumps of programs written in this folder
Definition parser_options.h:81
static void closeFile(FILE *file)
Tries to close the input stream associated with the result.
Definition parser_options.cpp:51
DebugHook getDebugHook() const
Definition parser_options.cpp:527
void setInputFile()
Expect that the only remaining argument is the input file.
Definition parser_options.cpp:341
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
const char * p4includePath
Definition parser_options.cpp:46
std::function< void(const char *manager, unsigned seqNo, const char *pass, const IR::Node *node)> DebugHook
Definition pass_manager.h:38
static CompileContextType & top()
Definition compile_context.h:48