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 <typeinfo>
12#include <vector>
13
14#include "lib/cstring.h"
15#include "lib/error_reporter.h"
16
17namespace P4 {
18
24 protected:
25 virtual ~ICompileContext() = 0;
26};
27
31struct CompileContextStack final {
32 CompileContextStack() = delete;
33
38 template <typename CompileContextType>
39 static CompileContextType &top() {
40 auto &stack = getStack();
41 if (stack.empty()) reportNoContext();
42 auto *current = dynamic_cast<CompileContextType *>(stack.back());
43 if (!current) reportContextMismatch(typeid(CompileContextType).name());
44 return *current;
45 }
46
47 static bool isEmpty() { return getStack().empty(); }
48
49 private:
50 friend struct AutoCompileContext;
51
52 using StackType = std::vector<ICompileContext *>;
53
55 static void reportNoContext();
56 static void reportContextMismatch(const char *desiredContextType);
57
59 static void push(ICompileContext *context);
60 static void pop();
61 static StackType &getStack();
62};
63
68struct AutoCompileContext {
69 explicit AutoCompileContext(ICompileContext *context);
70 ~AutoCompileContext();
71};
72
76class BaseCompileContext : public ICompileContext {
77 protected:
78 BaseCompileContext() = default;
79 BaseCompileContext(const BaseCompileContext &other) = default;
80 BaseCompileContext &operator=(const BaseCompileContext &other) = default;
81
82 public:
85 static BaseCompileContext &get();
86
89
92
95
98
99 private:
101 ErrorReporter errorReporterInstance;
102};
103
104} // namespace P4
105
106#endif /* LIB_COMPILE_CONTEXT_H_ */
virtual ErrorReporter & errorReporter()
Definition compile_context.cpp:54
virtual DiagnosticAction getDefaultErrorDiagnosticAction()
Definition compile_context.cpp:64
virtual DiagnosticAction getDefaultWarningDiagnosticAction()
Definition compile_context.cpp:60
virtual DiagnosticAction getDefaultInfoDiagnosticAction()
Definition compile_context.cpp:56
static BaseCompileContext & get()
Definition compile_context.cpp:50
Definition error_reporter.h:40
Definition compile_context.h:23
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
static CompileContextType & top()
Definition compile_context.h:39