P4C
The P4 Compiler
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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
38// TODO: This should be std::filesystem::path.
39extern const char *p4includePath;
40extern const char *p4_14includePath;
41
43bool isSystemFile(cstring filename);
44
48class ParserOptions : public Util::Options {
50 std::set<cstring> disabledAnnotations;
51
53 mutable size_t dump_uid = 0;
54
55 protected:
58 void dumpPass(const char *manager, unsigned seq, const char *pass, const IR::Node *node) const;
59
62 virtual std::unique_ptr<ToP4> getToP4(std::ostream *, bool, std::filesystem::path) const;
63
64 public:
65 explicit ParserOptions(std::string_view defaultMessage = "Parse a P4 program");
66
67 std::vector<const char *> *process(int argc, char *const argv[]) override;
68 enum class FrontendVersion { P4_14, P4_16 };
69
71 static void closeFile(FILE *file);
73 using PreprocessorResult = std::unique_ptr<FILE, decltype(&closeFile)>;
74
78 FrontendVersion langVersion = FrontendVersion::P4_16;
80 cstring preprocessor_options = cstring::empty;
82 std::filesystem::path file;
84 bool doNotCompile = false;
88 bool doNotPreprocess = false;
90 std::vector<cstring> top4;
92 std::filesystem::path dumpFolder = ".";
96 void setInputFile();
98 const char *getIncludePath() const override;
100 std::optional<ParserOptions::PreprocessorResult> preprocess() const;
102 bool isv1() const;
105 DebugHook getDebugHook() const;
107 bool isAnnotationDisabled(const IR::Annotation *a) const;
110 static bool searchForIncludePath(const char *&includePathOut,
111 const std::vector<cstring> &userSpecifiedPaths,
112 const char *exename);
115 bool noIncludes = false;
116};
117
120class P4CContext : public BaseCompileContext {
121 public:
124 static P4CContext &get();
125
129 static const P4CConfiguration &getConfig();
130
131 P4CContext() {}
132
134 virtual ParserOptions &options() = 0;
135
140
145
150
155
157 void setDiagnosticAction(std::string_view diagnostic, DiagnosticAction action) {
158 errorReporter().setDiagnosticAction(diagnostic, action);
159 }
160
161 protected:
165 virtual bool isRecognizedDiagnostic(cstring diagnostic);
166
169 virtual const P4CConfiguration &getConfigImpl();
170};
171
175template <typename OptionsType>
176class P4CContextWithOptions final : public P4CContext {
177 public:
180 static P4CContextWithOptions &get() {
182 }
183
185
186 template <typename OptionsDerivedType>
187 P4CContextWithOptions(P4CContextWithOptions<OptionsDerivedType> &context) {
188 optionsInstance = context.options();
189 }
190
191 template <typename OptionsDerivedType>
192 P4CContextWithOptions &operator=(P4CContextWithOptions<OptionsDerivedType> &context) {
193 optionsInstance = context.options();
194 }
195
197 OptionsType &options() override { return optionsInstance; }
198
199 private:
201 OptionsType optionsInstance;
202};
203
204} // namespace P4
205
206#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: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:142
void setDefaultWarningDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to P4::warning().
Definition parser_options.h:152
void setDiagnosticAction(std::string_view diagnostic, DiagnosticAction action)
Set the action to take for the given diagnostic.
Definition parser_options.h:157
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:147
DiagnosticAction getDefaultInfoDiagnosticAction() final
Definition parser_options.h:137
Definition parser_options.h:176
OptionsType & options() override
Definition parser_options.h:197
static P4CContextWithOptions & get()
Definition parser_options.h:180
Definition parser_options.h:48
cstring compilerVersion
Compiler version.
Definition parser_options.h:86
std::filesystem::path file
file to compile (- for stdin)
Definition parser_options.h:82
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:115
cstring exe_name
Name of executable that is being run.
Definition parser_options.h:76
FrontendVersion langVersion
Which language to compile.
Definition parser_options.h:78
bool doNotCompile
if true preprocess only
Definition parser_options.h:84
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:94
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
std::vector< const char * > * process(int argc, char *const argv[]) override
Definition parser_options.cpp:374
std::vector< cstring > top4
substrings matched against pass names
Definition parser_options.h:90
std::unique_ptr< FILE, decltype(&closeFile)> PreprocessorResult
Records the result of the preprocessor.
Definition parser_options.h:73
cstring preprocessor_options
options to pass to preprocessor
Definition parser_options.h:80
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:88
std::filesystem::path dumpFolder
debugging dumps of programs written in this folder
Definition parser_options.h:92
static void closeFile(FILE *file)
Tries to close the input stream associated with the result.
Definition parser_options.cpp:54
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
static bool searchForIncludePath(const char *&includePathOut, const std::vector< cstring > &userSpecifiedPaths, const char *exename)
Definition parser_options.cpp:360
void setInputFile()
Expect that the only remaining argument is the input file.
Definition parser_options.cpp:345
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
bool isSystemFile(cstring file)
Try to guess whether a file is a "system" file.
Definition parser_options.cpp:52
DiagnosticAction
An action to take when a diagnostic message is triggered.
Definition error_reporter.h:37
const char * p4includePath
Definition parser_options.cpp:47
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
static CompileContextType & top()
Definition compile_context.h:48