P4C
The P4 Compiler
Loading...
Searching...
No Matches
compile_context.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#ifndef LIB_COMPILE_CONTEXT_H_
9#define LIB_COMPILE_CONTEXT_H_
10
11#include <memory>
12#include <typeinfo>
13#include <vector>
14
15#include "lib/cstring.h"
16#include "lib/error_reporter.h"
17
18namespace P4 {
19
25 public:
26 virtual ~ICompileContext() = 0;
27};
28
32struct CompileContextStack final {
33 CompileContextStack() = delete;
34
39 template <typename CompileContextType>
40 static CompileContextType &top() {
41 auto &stack = getStack();
42 if (stack.empty()) reportNoContext();
43 auto *current = dynamic_cast<CompileContextType *>(stack.back());
44 if (!current) reportContextMismatch(typeid(CompileContextType).name());
45 return *current;
46 }
47
48 static bool isEmpty() { return getStack().empty(); }
49
50 private:
51 friend struct AutoCompileContext;
52
53 using StackType = std::vector<ICompileContext *>;
54
56 static void reportNoContext();
57 static void reportContextMismatch(const char *desiredContextType);
58
60 static void push(ICompileContext *context);
61 static void pop();
62 static StackType &getStack();
63};
64
70 private:
71 std::unique_ptr<ICompileContext> ownedContext;
72
73 public:
75 explicit AutoCompileContext(ICompileContext *context);
77 explicit AutoCompileContext(std::unique_ptr<ICompileContext> context);
78 AutoCompileContext(const AutoCompileContext &) = delete;
79 AutoCompileContext &operator=(const AutoCompileContext &) = delete;
81};
82
86class BaseCompileContext : public ICompileContext {
87 protected:
88 BaseCompileContext() = default;
89 BaseCompileContext(const BaseCompileContext &other) = default;
90 BaseCompileContext &operator=(const BaseCompileContext &other) = default;
91
92 public:
95 static BaseCompileContext &get();
96
99
102
105
108
109 private:
111 ErrorReporter errorReporterInstance;
112};
113
114} // namespace P4
115
116#endif /* LIB_COMPILE_CONTEXT_H_ */
virtual ErrorReporter & errorReporter()
Definition compile_context.cpp:59
virtual DiagnosticAction getDefaultErrorDiagnosticAction()
Definition compile_context.cpp:69
virtual DiagnosticAction getDefaultWarningDiagnosticAction()
Definition compile_context.cpp:65
virtual DiagnosticAction getDefaultInfoDiagnosticAction()
Definition compile_context.cpp:61
static BaseCompileContext & get()
Definition compile_context.cpp:55
Definition error_reporter.h:40
Definition compile_context.h:24
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:29
AutoCompileContext(ICompileContext *context)
Borrow a context which outlives this stack entry.
Definition compile_context.cpp:44
static CompileContextType & top()
Definition compile_context.h:40