FE 0.16.1
Fast, Effecient FrontEnds
Loading...
Searching...
No Matches
dbg.h
Go to the documentation of this file.
1#pragma once
2
3#include <bit>
4#include <ostream>
5#include <tuple>
6
7#include <ankerl/unordered_dense.h>
8
9#include "fe/format.h"
10#include "fe/loc.h"
11#include "fe/sym.h"
12
13namespace fe {
14
15/// The debug info of an entity: where it came from and what it was called.
16struct Dbg {
17 /// @name Constructors
18 ///@{
19 constexpr Dbg() noexcept = default;
20 constexpr Dbg(Loc loc, Sym sym) noexcept
21 : loc_(loc)
22 , sym_(sym) {}
23 constexpr Dbg(Loc loc) noexcept
24 : Dbg(loc, {}) {}
25 constexpr Dbg(Sym sym) noexcept
26 : Dbg({}, sym) {}
27 ///@}
28
29 /// @name Getters
30 ///@{
31 constexpr Sym sym() const noexcept { return sym_; }
32 constexpr Loc loc() const noexcept { return loc_; }
33 /// Assumes `_` as the anonymous name.
34 constexpr bool is_anon() const noexcept { return !sym() || sym() == '_'; }
35 constexpr explicit operator bool() const noexcept { return sym().operator bool(); }
36 ///@}
37
38 /// @name Setters
39 ///@{
40 constexpr Dbg& set(Sym sym) noexcept { return sym_ = sym, *this; }
41 constexpr Dbg& set(Loc loc) noexcept { return loc_ = loc, *this; }
42 ///@}
43
44 /// @name Comparison and Hashing
45 ///@{
46 /// @note Like Loc::operator==, this only compares Loc::src by pointer identity.
47 constexpr bool operator==(const Dbg& other) const noexcept { return loc_ == other.loc_ && sym_ == other.sym_; }
48
49 struct Hash {
50 using is_avalanching = void;
51
52 size_t operator()(Dbg dbg) const noexcept {
53 using Tuple = std::tuple<uintptr_t, uint32_t, uint32_t, uintptr_t>;
54 return ankerl::unordered_dense::hash<Tuple>()(
55 {std::bit_cast<uintptr_t>(dbg.loc_.src), dbg.loc_.begin.off, dbg.loc_.end.off, dbg.sym_.raw()});
56 }
57 };
58
59 struct Eq {
60 constexpr bool operator()(Dbg d1, Dbg d2) const noexcept { return d1 == d2; }
61 };
62 ///@}
63
64private:
65 Loc loc_;
66 Sym sym_;
67
68 friend std::ostream& operator<<(std::ostream& os, const Dbg& dbg) { return os << dbg.sym(); }
69};
70
71/// @name DbgMap/DbgSet
72///@{
73template<class V>
74using DbgMap = ankerl::unordered_dense::map<Dbg, V, Dbg::Hash, Dbg::Eq>;
75using DbgSet = ankerl::unordered_dense::set<Dbg, Dbg::Hash, Dbg::Eq>;
76///@}
77
78/// Opaque handle to a Dbg interned in a Driver; see Driver::dbg.
79/// Handing one node another's key copies the handle verbatim: no Dbg is materialised and nothing is
80/// looked up in the Driver's table.
81/// @warning A key is only meaningful within the Driver that interned it.
82class DbgKey {
83public:
84 constexpr DbgKey() noexcept = default; ///< The empty Dbg.
85
86 constexpr explicit operator bool() const noexcept { return key_ != 0; } ///< Not the empty Dbg?
87 constexpr bool operator==(const DbgKey&) const noexcept = default;
88
89private:
90 constexpr explicit DbgKey(uint32_t key) noexcept
91 : key_(key) {}
92
93 uint32_t key_ = 0;
94
95 friend struct Driver;
96};
97
98} // namespace fe
99
100#ifndef DOXYGEN
101template<>
102struct std::formatter<fe::Dbg> : fe::ostream_formatter {};
103#endif
constexpr bool operator==(const DbgKey &) const noexcept=default
constexpr DbgKey() noexcept=default
The empty Dbg.
friend struct Driver
Definition dbg.h:95
A Symbol just wraps a pointer to Sym::String, so pass Sym itself around as value.
Definition sym.h:30
Definition algo.h:17
ankerl::unordered_dense::map< Dbg, V, Dbg::Hash, Dbg::Eq > DbgMap
Definition dbg.h:74
ankerl::unordered_dense::set< Dbg, Dbg::Hash, Dbg::Eq > DbgSet
Definition dbg.h:75
basic_ostream_formatter< char > ostream_formatter
Definition format.h:61
constexpr bool operator()(Dbg d1, Dbg d2) const noexcept
Definition dbg.h:60
size_t operator()(Dbg dbg) const noexcept
Definition dbg.h:52
void is_avalanching
Definition dbg.h:50
constexpr Loc loc() const noexcept
Definition dbg.h:32
constexpr Dbg & set(Sym sym) noexcept
Definition dbg.h:40
constexpr bool operator==(const Dbg &other) const noexcept
Definition dbg.h:47
constexpr Dbg(Loc loc) noexcept
Definition dbg.h:23
friend std::ostream & operator<<(std::ostream &os, const Dbg &dbg)
Definition dbg.h:68
constexpr Dbg() noexcept=default
constexpr bool is_anon() const noexcept
Assumes _ as the anonymous name.
Definition dbg.h:34
constexpr Sym sym() const noexcept
Definition dbg.h:31
constexpr Dbg(Sym sym) noexcept
Definition dbg.h:25
constexpr Dbg & set(Loc loc) noexcept
Definition dbg.h:41
Location within a Src: the half-open byte range [Loc::begin, Loc::end).
Definition loc.h:42