FE 0.15.0
A C++23 toolkit for writing compiler/interpreter frontends.
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
15namespace 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.
22struct Driver : public SymPool {
23public:
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
31 Driver(const Driver&) = delete;
32 Driver(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.
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
74private:
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
Opaque handle to a Dbg interned in a Driver; see Driver::dbg.
Definition dbg.h:101
constexpr DbgKey() noexcept=default
The empty Dbg.
How a diagnostic lays out - and how much of it an Error keeps.
Definition diag.h:20
Collects diagnostics and hands each to the Diag that lays it out.
Definition error.h:23
Interns the text - and the std::filesystem::path - of every file a Loc may point into.
Definition src.h:93
SymPool(const SymPool &)=delete
This is a thin wrapper for absl::InlinedVector<T, N, A> which is a drop-in replacement for std::vecto...
Definition vector.h:39
Definition algo.h:17
The debug info of an entity: where it came from and what it was called.
Definition dbg.h:22
constexpr Dbg() noexcept=default
const Error & error() const
Definition driver.h:39
Dbg dbg(DbgKey key) const
Definition driver.h:64
SrcMap & src()
Definition driver.h:55
Driver(const Driver &)=delete
Diag & diag()
Definition driver.h:41
Driver(Driver &&)=delete
DbgKey dbg(Dbg dbg)
Interns dbg and yields its DbgKey.
Definition driver.h:67
const SrcMap & src() const
Definition driver.h:56
Driver(std::unique_ptr< Diag > diag)
Installs diag right away, so Driver::error renders through it from the very first message.
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
Error & error()
Definition driver.h:38
Driver & operator=(Driver)=delete
const Diag & diag() const
Definition driver.h:42