FE 0.13.1
Header-only C++ frontend library
Loading...
Searching...
No Matches
dbg.h
Go to the documentation of this file.
1#pragma once
2
3#include <bit>
4#include <ostream>
5
6#ifdef FE_ABSL
7# include <absl/container/flat_hash_map.h>
8# include <absl/container/flat_hash_set.h>
9#else
10# include <unordered_map>
11# include <unordered_set>
12#endif
13
14#include "fe/format.h"
15#include "fe/hash.h"
16#include "fe/loc.h"
17#include "fe/sym.h"
18
19namespace fe {
20
21/// The debug info of an entity: where it came from and what it was called.
22struct Dbg {
23public:
24 /// @name Constructors
25 ///@{
26 constexpr Dbg() noexcept = default;
27 constexpr Dbg(const Dbg&) noexcept = default;
28 constexpr Dbg(Loc loc, Sym sym) noexcept
29 : loc_(loc)
30 , sym_(sym) {}
31 constexpr Dbg(Loc loc) noexcept
32 : Dbg(loc, {}) {}
33 constexpr Dbg(Sym sym) noexcept
34 : Dbg({}, sym) {}
35 Dbg& operator=(const Dbg&) noexcept = default;
36 ///@}
37
38 /// @name Getters
39 ///@{
40 Sym sym() const { return sym_; }
41 Loc loc() const { return loc_; }
42 bool is_anon() const { return !sym() || sym() == '_'; } ///< Assumes `_` as the anonymous name.
43 explicit operator bool() const { return sym().operator bool(); }
44 ///@}
45
46 /// @name Setters
47 ///@{
48 Dbg& set(Sym sym) { return sym_ = sym, *this; }
49 Dbg& set(Loc loc) { return loc_ = loc, *this; }
50 ///@}
51
52 /// @name Comparison and Hashing
53 ///@{
54 /// @note Like Loc::operator==, this only compares Loc::src by pointer identity.
55 bool operator==(const Dbg& other) const noexcept { return loc_ == other.loc_ && sym_ == other.sym_; }
56
57 struct Hash {
58 size_t operator()(Dbg dbg) const noexcept {
59 auto h = hash_begin(std::bit_cast<uintptr_t>(dbg.loc_.src));
60 h = hash_combine(h, dbg.loc_.begin.off);
61 h = hash_combine(h, dbg.loc_.end.off);
62 return hash_combine(h, Sym::Hash()(dbg.sym_));
63 }
64 };
65
66 struct Eq {
67 bool operator()(Dbg d1, Dbg d2) const noexcept { return d1 == d2; }
68 };
69
70#ifdef FE_ABSL
71 template<class H>
72 friend H AbslHashValue(H h, Dbg dbg) noexcept {
73 return H::combine(std::move(h), dbg.loc_.src, dbg.loc_.begin.off, dbg.loc_.end.off, dbg.sym_);
74 }
75#endif
76 ///@}
77
78private:
79 Loc loc_;
80 Sym sym_;
81
82 friend std::ostream& operator<<(std::ostream& os, const Dbg& dbg) { return os << dbg.sym(); }
83};
84
85/// @name DbgMap/DbgSet
86///@{
87#ifdef FE_ABSL
88template<class V>
89using DbgMap = absl::flat_hash_map<Dbg, V, Dbg::Hash, Dbg::Eq>;
90using DbgSet = absl::flat_hash_set<Dbg, Dbg::Hash, Dbg::Eq>;
91#else
92template<class V>
93using DbgMap = std::unordered_map<Dbg, V, Dbg::Hash, Dbg::Eq>;
94using DbgSet = std::unordered_set<Dbg, Dbg::Hash, Dbg::Eq>;
95#endif
96///@}
97
98/// Opaque handle to a Dbg interned in a Driver; see Driver::dbg.
99/// Handing one node another's key copies the handle verbatim: no Dbg is materialised and nothing is
100/// looked up in the Driver's table.
101/// @warning A key is only meaningful within the Driver that interned it.
102class DbgKey {
103public:
104 constexpr DbgKey() noexcept = default; ///< The empty Dbg.
105
106 constexpr explicit operator bool() const noexcept { return key_ != 0; } ///< Not the empty Dbg?
107 constexpr bool operator==(const DbgKey&) const noexcept = default;
108
109private:
110 constexpr explicit DbgKey(uint32_t key) noexcept
111 : key_(key) {}
112
113 uint32_t key_ = 0;
114
115 friend struct Driver;
116};
117
118} // namespace fe
119
120#ifndef DOXYGEN
121template<>
122struct std::formatter<fe::Dbg> : fe::ostream_formatter {};
123#endif
constexpr bool operator==(const DbgKey &) const noexcept=default
constexpr DbgKey() noexcept=default
The empty Dbg.
friend struct Driver
Definition dbg.h:115
A Symbol just wraps a pointer to Sym::String, so pass Sym itself around as value.
Definition sym.h:36
Definition algo.h:17
constexpr size_t hash_begin() noexcept
Seeds a hash chain with the FNV-1 offset basis.
Definition hash.h:64
std::unordered_set< Dbg, Dbg::Hash, Dbg::Eq > DbgSet
Definition dbg.h:94
basic_ostream_formatter< char > ostream_formatter
Definition format.h:64
constexpr size_t hash_combine(size_t seed, T v) noexcept
Mixes v into seed word-wise, reusing the FNV-1 prime as multiplier.
Definition hash.h:68
std::unordered_map< Dbg, V, Dbg::Hash, Dbg::Eq > DbgMap
Definition dbg.h:93
bool operator()(Dbg d1, Dbg d2) const noexcept
Definition dbg.h:67
size_t operator()(Dbg dbg) const noexcept
Definition dbg.h:58
The debug info of an entity: where it came from and what it was called.
Definition dbg.h:22
bool is_anon() const
Assumes _ as the anonymous name.
Definition dbg.h:42
Dbg & set(Sym sym)
Definition dbg.h:48
Sym sym() const
Definition dbg.h:40
Dbg & operator=(const Dbg &) noexcept=default
constexpr Dbg(Loc loc) noexcept
Definition dbg.h:31
friend std::ostream & operator<<(std::ostream &os, const Dbg &dbg)
Definition dbg.h:82
constexpr Dbg() noexcept=default
Dbg & set(Loc loc)
Definition dbg.h:49
Loc loc() const
Definition dbg.h:41
bool operator==(const Dbg &other) const noexcept
Definition dbg.h:55
constexpr Dbg(Sym sym) noexcept
Definition dbg.h:33
Location within a Src: the half-open byte range [Loc::begin, Loc::end).
Definition loc.h:46