P4C
The P4 Compiler
Loading...
Searching...
No Matches
parser_options.h
1/*
2 * SPDX-FileCopyrightText: 2013 Barefoot Networks, Inc.
3 * Copyright 2013-present Barefoot Networks, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8/* -*-C++-*- */
9
10#ifndef FRONTENDS_COMMON_PARSER_OPTIONS_H_
11#define FRONTENDS_COMMON_PARSER_OPTIONS_H_
12
13#include <cstdio>
14#include <filesystem>
15#include <set>
16
17#include "../p4/metrics/metricsStructure.h"
18#include "ir/configuration.h"
19#include "ir/pass_manager.h"
20#include "lib/compile_context.h"
21#include "lib/cstring.h"
22#include "lib/options.h"
23
24namespace P4 {
25
26class ToP4;
27
30extern std::filesystem::path p4includePath;
31extern std::filesystem::path p4_14includePath;
32
34bool isSystemFile(cstring filename);
35bool isSystemFile(const std::filesystem::path &filename);
36
40class ParserOptions : public Util::Options {
42 std::set<cstring> disabledAnnotations;
43
45 mutable size_t dump_uid = 0;
46
47 protected:
50 void dumpPass(const char *manager, unsigned seq, const char *pass, const IR::Node *node) const;
51
54 virtual std::unique_ptr<ToP4> getToP4(std::ostream *, bool, std::filesystem::path) const;
55
56 public:
57 explicit ParserOptions(std::string_view defaultMessage = "Parse a P4 program");
58
59 std::vector<const char *> *process(int argc, char *const argv[]) override;
60 enum class FrontendVersion { P4_14, P4_16 };
61
63 static void closeFile(FILE *file);
65 using PreprocessorResult = std::unique_ptr<FILE, decltype(&closeFile)>;
66
70 FrontendVersion langVersion = FrontendVersion::P4_16;
72 cstring preprocessor_options = cstring::empty;
74 std::filesystem::path file;
76 bool doNotCompile = false;
80 bool doNotPreprocess = false;
82 std::vector<cstring> top4;
84 std::filesystem::path dumpFolder = ".";
88 void setInputFile();
90 const char *getIncludePath() const override;
92 std::optional<ParserOptions::PreprocessorResult> preprocess() const;
94 bool isv1() const;
97 DebugHook getDebugHook() const;
99 bool isAnnotationDisabled(const IR::Annotation *a) const;
102 static bool searchForIncludePath(std::filesystem::path &includePathOut,
103 const std::vector<cstring> &userSpecifiedPaths,
104 const std::filesystem::path &exename);
107 bool noIncludes = false;
110};
111
114class P4CContext : public BaseCompileContext {
115 public:
118 static P4CContext &get();
119
123 static const P4CConfiguration &getConfig();
124
125 P4CContext() {}
126
128 virtual ParserOptions &options() = 0;
129
134
139
144
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:54
void setDefaultWarningDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to P4::warning().
Definition error_reporter.h:244
void setDiagnosticAction(std::string_view diagnostic, DiagnosticAction action)
Set the action to take for the given diagnostic.
Definition error_reporter.h:236
DiagnosticAction getDefaultWarningDiagnosticAction()
Definition error_reporter.h:241
DiagnosticAction getDefaultInfoDiagnosticAction()
Definition error_reporter.h:249
void setDefaultInfoDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to P4::info().
Definition error_reporter.h:252
Definition node.h:53
Definition ir/configuration.h:24
static P4CContext & get()
Definition parser_options.cpp:523
virtual bool isRecognizedDiagnostic(cstring diagnostic)
Definition parser_options.cpp:530
void setDefaultInfoDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to P4::info().
Definition parser_options.h:136
void setDefaultWarningDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to P4::warning().
Definition parser_options.h:146
void setDiagnosticAction(std::string_view diagnostic, DiagnosticAction action)
Set the action to take for the given diagnostic.
Definition parser_options.h:151
static const P4CConfiguration & getConfig()
Definition parser_options.cpp:525
virtual const P4CConfiguration & getConfigImpl()
Definition parser_options.cpp:536
virtual ParserOptions & options()=0
DiagnosticAction getDefaultWarningDiagnosticAction() final
Definition parser_options.h:141
DiagnosticAction getDefaultInfoDiagnosticAction() final
Definition parser_options.h:131
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:40
cstring compilerVersion
Compiler version.
Definition parser_options.h:78
std::filesystem::path file
file to compile (- for stdin)
Definition parser_options.h:74
static bool searchForIncludePath(std::filesystem::path &includePathOut, const std::vector< cstring > &userSpecifiedPaths, const std::filesystem::path &exename)
Definition parser_options.cpp:359
bool isv1() const
True if we are compiling a P4 v1.0 or v1.1 program.
Definition parser_options.cpp:457
bool noIncludes
Definition parser_options.h:107
cstring exe_name
Name of executable that is being run.
Definition parser_options.h:68
FrontendVersion langVersion
Which language to compile.
Definition parser_options.h:70
bool doNotCompile
if true preprocess only
Definition parser_options.h:76
void dumpPass(const char *manager, unsigned seq, const char *pass, const IR::Node *node) const
Definition parser_options.cpp:459
bool optimizeParserInlining
If false, optimization of callee parsers (subparsers) inlining is disabled.
Definition parser_options.h:86
bool isAnnotationDisabled(const IR::Annotation *a) const
Check whether this particular annotation was disabled.
Definition parser_options.cpp:508
std::optional< ParserOptions::PreprocessorResult > preprocess() const
Returns the output of the preprocessor.
Definition parser_options.cpp:408
Metrics metrics
Holds code metric values, makes them accessible during the entire compilation.
Definition parser_options.h:109
std::vector< const char * > * process(int argc, char *const argv[]) override
Definition parser_options.cpp:372
std::vector< cstring > top4
substrings matched against pass names
Definition parser_options.h:82
std::unique_ptr< FILE, decltype(&closeFile)> PreprocessorResult
Records the result of the preprocessor.
Definition parser_options.h:65
cstring preprocessor_options
options to pass to preprocessor
Definition parser_options.h:72
const char * getIncludePath() const override
Return target specific include path.
Definition parser_options.cpp:395
bool doNotPreprocess
if true skip preprocess
Definition parser_options.h:80
std::filesystem::path dumpFolder
debugging dumps of programs written in this folder
Definition parser_options.h:84
static void closeFile(FILE *file)
Tries to close the input stream associated with the result.
Definition parser_options.cpp:46
DebugHook getDebugHook() const
Definition parser_options.cpp:517
virtual std::unique_ptr< ToP4 > getToP4(std::ostream *, bool, std::filesystem::path) const
Definition parser_options.cpp:503
void setInputFile()
Expect that the only remaining argument is the input file.
Definition parser_options.cpp:344
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:13
DiagnosticAction
An action to take when a diagnostic message is triggered.
Definition error_reporter.h:28
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:76
std::filesystem::path p4includePath
Definition parser_options.cpp:35
Definition metricsStructure.h:120
static CompileContextType & top()
Definition compile_context.h:39