FE
0.15.0
A C++23 toolkit for writing compiler/interpreter frontends.
Toggle main menu visibility
Loading...
Searching...
No Matches
driver.h
Go to the documentation of this file.
1
#pragma once
2
3
#include <cassert>
4
5
#include <memory>
6
#include <utility>
7
8
#include "
fe/dbg.h
"
9
#include "
fe/diag.h
"
10
#include "
fe/error.h
"
11
#include "
fe/src.h
"
12
#include "
fe/sym.h
"
13
#include "
fe/vector.h
"
14
15
namespace
fe
{
16
17
/// Use/derive from this class for "global" variables that you need all over the place.
18
/// Well, there are not really global - that's the point of this class.
19
/// It manages a SymPool, a SrcMap, the interned Dbg%s, the Diag that lays out a diagnostic, and the Error they land in.
20
/// @note Deliberately free of virtual functions - Driver::diag is where you plug in behavior of your own.
21
/// @warning Not movable: Driver::error - and a Diag of your own - point back here.
22
struct
Driver
:
public
SymPool
{
23
public
:
24
Driver
();
25
26
/// Installs @p diag right away, so Driver::error renders through it from the very first message.
27
/// @warning Never `nullptr`.
28
explicit
Driver
(std::unique_ptr<Diag>
diag
);
29
30
~Driver
();
31
Driver
(
const
Driver
&) =
delete
;
32
Driver
(
Driver
&&) =
delete
;
33
Driver
&
operator=
(
Driver
) =
delete
;
34
35
/// @name Diagnostics
36
/// Every frontend building block reports into Driver::error; Error::ack it before this Driver dies.
37
///@{
38
Error
&
error
() {
return
error_; }
39
const
Error
&
error
()
const
{
return
error_; }
40
41
Diag
&
diag
() {
return
*diag_; }
42
const
Diag
&
diag
()
const
{
return
*diag_; }
43
44
/// Installs @p diag - a subclass of your own, typically - and yields the previous one.
45
/// @warning Never `nullptr`.
46
std::unique_ptr<Diag>
diag
(std::unique_ptr<Diag>
diag
) {
47
assert(
diag
&&
"a Driver always has a Diag"
);
48
return
std::exchange(diag_, std::move(
diag
));
49
}
50
///@}
51
52
/// @name Source Files
53
/// Register every file you lex here: a Loc is only as good as the SrcMap that keeps its Src alive.
54
///@{
55
SrcMap
&
src
() {
return
src_; }
56
const
SrcMap
&
src
()
const
{
return
src_; }
57
///@}
58
59
/// @name Dbg Interning
60
/// A node only has to store the DbgKey instead of a full Dbg.
61
/// Both halves of a Dbg are already owned here:
62
/// Dbg::sym is interned in this Driver's SymPool and Dbg::loc points into Driver::src.
63
///@{
64
Dbg
dbg
(
DbgKey
key)
const
{
return
dbgs_[key.key_]; }
65
66
/// Interns @p dbg and yields its DbgKey.
67
DbgKey
dbg
(
Dbg
dbg
) {
68
auto
[i, fresh] = dbg2key_.try_emplace(
dbg
, uint32_t(dbgs_.size()));
69
if
(fresh) dbgs_.emplace_back(
dbg
);
70
return
DbgKey
(i->second);
71
}
72
///@}
73
74
private
:
75
std::unique_ptr<Diag> diag_;
76
Error
error_;
77
SrcMap
src_;
78
Vector<Dbg>
dbgs_ = {
Dbg
()};
///< Key `0` is the empty Dbg.
79
DbgMap<uint32_t> dbg2key_ = {
80
{
Dbg
(), 0}
81
};
82
};
83
84
}
// namespace fe
fe::DbgKey
Opaque handle to a Dbg interned in a Driver; see Driver::dbg.
Definition
dbg.h:101
fe::DbgKey::DbgKey
constexpr DbgKey() noexcept=default
The empty Dbg.
fe::Diag
How a diagnostic lays out - and how much of it an Error keeps.
Definition
diag.h:20
fe::Error
Collects diagnostics and hands each to the Diag that lays it out.
Definition
error.h:23
fe::SrcMap
Interns the text - and the std::filesystem::path - of every file a Loc may point into.
Definition
src.h:93
fe::SymPool::SymPool
SymPool(const SymPool &)=delete
fe::Vector
This is a thin wrapper for absl::InlinedVector<T, N, A> which is a drop-in replacement for std::vecto...
Definition
vector.h:39
dbg.h
diag.h
error.h
fe
Definition
algo.h:17
src.h
fe::Dbg
The debug info of an entity: where it came from and what it was called.
Definition
dbg.h:22
fe::Dbg::Dbg
constexpr Dbg() noexcept=default
fe::Driver::error
const Error & error() const
Definition
driver.h:39
fe::Driver::dbg
Dbg dbg(DbgKey key) const
Definition
driver.h:64
fe::Driver::src
SrcMap & src()
Definition
driver.h:55
fe::Driver::~Driver
~Driver()
fe::Driver::Driver
Driver()
fe::Driver::Driver
Driver(const Driver &)=delete
fe::Driver::diag
Diag & diag()
Definition
driver.h:41
fe::Driver::Driver
Driver(Driver &&)=delete
fe::Driver::dbg
DbgKey dbg(Dbg dbg)
Interns dbg and yields its DbgKey.
Definition
driver.h:67
fe::Driver::src
const SrcMap & src() const
Definition
driver.h:56
fe::Driver::Driver
Driver(std::unique_ptr< Diag > diag)
Installs diag right away, so Driver::error renders through it from the very first message.
fe::Driver::diag
std::unique_ptr< Diag > diag(std::unique_ptr< Diag > diag)
Installs diag - a subclass of your own, typically - and yields the previous one.
Definition
driver.h:46
fe::Driver::error
Error & error()
Definition
driver.h:38
fe::Driver::operator=
Driver & operator=(Driver)=delete
fe::Driver::diag
const Diag & diag() const
Definition
driver.h:42
sym.h
vector.h
fe
driver.h
Generated by
1.18.0