FE 0.17.1
Fast, Effecient 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 {
24
25 /// Installs @p diag right away, so Driver::error renders through it from the very first message.
26 /// @warning Never `nullptr`.
27 explicit Driver(std::unique_ptr<Diag> diag);
28
30 Driver(const Driver&) = delete;
31 Driver(Driver&&) = delete;
33
34 /// @name Diagnostics
35 /// Every frontend building block reports into Driver::error; Error::ack it before this Driver dies.
36 ///@{
37 Error& error() { return error_; }
38 const Error& error() const { return error_; }
39
40 Diag& diag() { return *diag_; }
41 const Diag& diag() const { return *diag_; }
42
43 /// Installs @p diag - a subclass of your own, typically - and yields the previous one.
44 /// @warning Never `nullptr`.
45 std::unique_ptr<Diag> diag(std::unique_ptr<Diag> diag) {
46 assert(diag && "a Driver always has a Diag");
47 return std::exchange(diag_, std::move(diag));
48 }
49 ///@}
50
51 /// @name Source Files
52 /// Register every file you lex here: a Loc is only as good as the SrcMap that keeps its Src alive.
53 ///@{
54 SrcMap& src() { return src_; }
55 const SrcMap& src() const { return src_; }
56 ///@}
57
58 /// @name Dbg Interning
59 /// A node only has to store the DbgKey instead of a full Dbg.
60 /// Both halves of a Dbg are already owned here:
61 /// Dbg::sym is interned in this Driver's SymPool and Dbg::loc points into Driver::src.
62 ///@{
63 Dbg dbg(DbgKey key) const { return dbgs_[key.key_]; }
64
65 /// Interns @p dbg and yields its DbgKey.
67 auto [i, fresh] = dbg2key_.try_emplace(dbg, uint32_t(dbgs_.size()));
68 if (fresh) dbgs_.emplace_back(dbg);
69 return DbgKey(i->second);
70 }
71 ///@}
72
73private:
74 std::unique_ptr<Diag> diag_;
75 Error error_;
76 SrcMap src_;
77 Vector<Dbg> dbgs_ = {Dbg()}; ///< Key `0` is the empty Dbg.
78 DbgMap<uint32_t> dbg2key_ = {
79 {Dbg(), 0}
80 };
81};
82
83} // namespace fe
Opaque handle to a Dbg interned in a Driver; see Driver::dbg.
Definition dbg.h:82
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:25
Interns the text - and the std::filesystem::path - of every file a Loc may point into.
Definition src.h:84
SymPool(const SymPool &)=delete
This is a thin wrapper for ankerl::svector<T, N, A> which is a drop-in replacement for std::vector<T,...
Definition vector.h:25
Definition algo.h:17
The debug info of an entity: where it came from and what it was called.
Definition dbg.h:16
constexpr Dbg() noexcept=default
const Error & error() const
Definition driver.h:38
Dbg dbg(DbgKey key) const
Definition driver.h:63
SrcMap & src()
Definition driver.h:54
Driver(const Driver &)=delete
Diag & diag()
Definition driver.h:40
Driver(Driver &&)=delete
DbgKey dbg(Dbg dbg)
Interns dbg and yields its DbgKey.
Definition driver.h:66
const SrcMap & src() const
Definition driver.h:55
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:45
Error & error()
Definition driver.h:37
Driver & operator=(Driver)=delete
const Diag & diag() const
Definition driver.h:41