P4C
The P4 Compiler
Loading...
Searching...
No Matches
compile_context.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#ifndef LIB_COMPILE_CONTEXT_H_
18#define LIB_COMPILE_CONTEXT_H_
19
20#include <typeinfo>
21#include <vector>
22
23#include "lib/cstring.h"
24#include "lib/error_reporter.h"
25
26namespace P4 {
27
33 protected:
34 virtual ~ICompileContext() = 0;
35};
36
40struct CompileContextStack final {
41 CompileContextStack() = delete;
42
47 template <typename CompileContextType>
48 static CompileContextType &top() {
49 auto &stack = getStack();
50 if (stack.empty()) reportNoContext();
51 auto *current = dynamic_cast<CompileContextType *>(stack.back());
52 if (!current) reportContextMismatch(typeid(CompileContextType).name());
53 return *current;
54 }
55
56 static bool isEmpty() { return getStack().empty(); }
57
58 private:
59 friend struct AutoCompileContext;
60
61 using StackType = std::vector<ICompileContext *>;
62
64 static void reportNoContext();
65 static void reportContextMismatch(const char *desiredContextType);
66
68 static void push(ICompileContext *context);
69 static void pop();
70 static StackType &getStack();
71};
72
81
86 protected:
87 BaseCompileContext() = default;
88 BaseCompileContext(const BaseCompileContext &other) = default;
89 BaseCompileContext &operator=(const BaseCompileContext &other) = default;
90
91 public:
94 static BaseCompileContext &get();
95
98
101
104
107
108 private:
110 ErrorReporter errorReporterInstance;
111};
112
113} // namespace P4
114
115#endif /* LIB_COMPILE_CONTEXT_H_ */
Definition compile_context.h:85
virtual ErrorReporter & errorReporter()
Definition compile_context.cpp:65
virtual DiagnosticAction getDefaultErrorDiagnosticAction()
Definition compile_context.cpp:75
virtual DiagnosticAction getDefaultWarningDiagnosticAction()
Definition compile_context.cpp:71
virtual DiagnosticAction getDefaultInfoDiagnosticAction()
Definition compile_context.cpp:67
static BaseCompileContext & get()
Definition compile_context.cpp:61
Definition error_reporter.h:49
Definition compile_context.h:32
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
Definition compile_context.h:77
Definition compile_context.h:40
static CompileContextType & top()
Definition compile_context.h:48