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 <set>
23#include <unordered_map>
24
25#include "ir/configuration.h"
26#include "ir/pass_manager.h"
27#include "lib/compile_context.h"
28#include "lib/cstring.h"
29#include "lib/options.h"
30
31// Standard include paths for .p4 header files. The values are determined by
32// `configure`.
33extern const char *p4includePath;
34extern const char *p4_14includePath;
35
36// Base class for compiler options.
37// This class contains the options for the front-ends.
38// Each back-end should subclass this file.
40 bool close_input = false;
41 static const char *defaultMessage;
42
43 // annotation names that are to be ignored by the compiler
44 std::set<cstring> disabledAnnotations;
45
46 // used to generate dump file names
47 mutable size_t dump_uid = 0;
48
49 protected:
50 // Function that is returned by getDebugHook.
51 void dumpPass(const char *manager, unsigned seq, const char *pass, const IR::Node *node) const;
52 // Checks if parsed options make sense with respect to each-other.
53 virtual void validateOptions() const;
54
55 public:
57 std::vector<const char *> *process(int argc, char *const argv[]) override;
58 enum class FrontendVersion { P4_14, P4_16 };
59 // Name of executable that is being run.
60 cstring exe_name;
61 // Which language to compile
62 FrontendVersion langVersion = FrontendVersion::P4_16;
63 // options to pass to preprocessor
64 cstring preprocessor_options = "";
65 // file to compile (- for stdin)
66 cstring file = nullptr;
67 // if true preprocess only
68 bool doNotCompile = false;
69 // Compiler version.
70 cstring compilerVersion;
71 // if true skip preprocess
72 bool doNotPreprocess = false;
73 // substrings matched against pass names
74 std::vector<cstring> top4;
75 // debugging dumps of programs written in this folder
76 cstring dumpFolder = ".";
77 // If false, optimization of callee parsers (subparsers) inlining is disabled.
78 bool optimizeParserInlining = false;
79 // Expect that the only remaining argument is the input file.
80 void setInputFile();
81 // Return target specific include path.
82 const char *getIncludePath() override;
83 // Returns the output of the preprocessor.
84 FILE *preprocess();
85 // Closes the input stream returned by preprocess.
86 void closePreprocessedInput(FILE *input) const;
87 // True if we are compiling a P4 v1.0 or v1.1 program
88 bool isv1() const;
89 // Get a debug hook function suitable for insertion
90 // in the pass managers that are executed.
91 DebugHook getDebugHook() const;
92 // Check whether this particular annotation was disabled
93 bool isAnnotationDisabled(const IR::Annotation *a) const;
94 // Search and set 'includePathOut' to be the first valid path from the
95 // list of possible relative paths.
96 bool searchForIncludePath(const char *&includePathOut, std::vector<cstring> relativePaths,
97 const char *);
100 bool noIncludes = false;
101};
102
106 public:
109 static P4CContext &get();
110
114 static const P4CConfiguration &getConfig();
115
116 P4CContext() {}
117
119 virtual ParserOptions &options() = 0;
120
122 DiagnosticAction getDefaultInfoDiagnosticAction() final {
124 }
125
127 void setDefaultInfoDiagnosticAction(DiagnosticAction action) {
129 }
130
135
137 void setDefaultWarningDiagnosticAction(DiagnosticAction action) {
139 }
140
143 DiagnosticAction getDiagnosticAction(cstring diagnostic, DiagnosticAction defaultAction) final {
144 return errorReporter().getDiagnosticAction(diagnostic, defaultAction);
145 }
146
148 void setDiagnosticAction(cstring diagnostic, DiagnosticAction action) {
149 errorReporter().setDiagnosticAction(diagnostic, action);
150 }
151
152 protected:
156 virtual bool isRecognizedDiagnostic(cstring diagnostic);
157
160 virtual const P4CConfiguration &getConfigImpl();
161};
162
166template <typename OptionsType>
167class P4CContextWithOptions final : public P4CContext {
168 public:
172 return CompileContextStack::top<P4CContextWithOptions>();
173 }
174
176
177 template <typename OptionsDerivedType>
179 optionsInstance = context.options();
180 }
181
182 template <typename OptionsDerivedType>
184 optionsInstance = context.options();
185 }
186
188 OptionsType &options() override { return optionsInstance; }
189
190 private:
192 OptionsType optionsInstance;
193};
194
195#endif /* FRONTENDS_COMMON_PARSER_OPTIONS_H_*/
Definition compile_context.h:83
virtual ErrorReporter & errorReporter()
Definition compile_context.cpp:63
void setDefaultWarningDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to warning().
Definition error_reporter.h:262
void setDefaultInfoDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to ::info().
Definition error_reporter.h:270
DiagnosticAction getDiagnosticAction(cstring diagnostic, DiagnosticAction defaultAction)
Definition error_reporter.h:240
DiagnosticAction getDefaultInfoDiagnosticAction()
Definition error_reporter.h:267
void setDiagnosticAction(cstring diagnostic, DiagnosticAction action)
Set the action to take for the given diagnostic.
Definition error_reporter.h:254
DiagnosticAction getDefaultWarningDiagnosticAction()
Definition error_reporter.h:259
Definition parser_options.h:105
DiagnosticAction getDiagnosticAction(cstring diagnostic, DiagnosticAction defaultAction) final
Definition parser_options.h:143
virtual ParserOptions & options()=0
DiagnosticAction getDefaultInfoDiagnosticAction() final
Definition parser_options.h:122
void setDefaultWarningDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to warning().
Definition parser_options.h:137
void setDiagnosticAction(cstring diagnostic, DiagnosticAction action)
Set the action to take for the given diagnostic.
Definition parser_options.h:148
void setDefaultInfoDiagnosticAction(DiagnosticAction action)
set the default diagnostic action for calls to ::info().
Definition parser_options.h:127
static P4CContext & get()
Definition parser_options.cpp:522
virtual bool isRecognizedDiagnostic(cstring diagnostic)
Definition parser_options.cpp:529
DiagnosticAction getDefaultWarningDiagnosticAction() final
Definition parser_options.h:132
static const P4CConfiguration & getConfig()
Definition parser_options.cpp:524
virtual const P4CConfiguration & getConfigImpl()
Definition parser_options.cpp:536
Definition parser_options.h:167
OptionsType & options() override
Definition parser_options.h:188
static P4CContextWithOptions & get()
Definition parser_options.h:171
Definition parser_options.h:39
std::vector< const char * > * process(int argc, char *const argv[]) override
Definition parser_options.cpp:375
bool noIncludes
Definition parser_options.h:100
Definition options.h:33
Definition cstring.h:72