#ifndef LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DIEREF_H
#define LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DIEREF_H
#include "lldb/Core/dwarf.h"
#include "lldb/Utility/LLDBAssert.h"
#include <cassert>
#include <optional>
namespace lldb_private::plugin {
namespace dwarf {
class DIERef {
public:
enum Section : uint8_t { DebugInfo, DebugTypes };
DIERef(std::optional<uint32_t> file_index, Section section,
dw_offset_t die_offset)
: m_die_offset(die_offset), m_file_index(file_index.value_or(0)),
m_file_index_valid(file_index ? true : false), m_section(section) {
assert(this->file_index() == file_index && "File Index is out of range?");
}
explicit DIERef(lldb::user_id_t uid) {
m_die_offset = uid & k_die_offset_mask;
m_file_index_valid = (uid & k_file_index_valid_bit) != 0;
m_file_index = m_file_index_valid
? (uid >> k_die_offset_bit_size) & k_file_index_mask
: 0;
m_section =
(uid & k_section_bit) != 0 ? Section::DebugTypes : Section::DebugInfo;
}
lldb::user_id_t get_id() const {
if (m_die_offset == k_die_offset_mask)
return LLDB_INVALID_UID;
return lldb::user_id_t(file_index().value_or(0)) << k_die_offset_bit_size |
die_offset() | (m_file_index_valid ? k_file_index_valid_bit : 0) |
(section() == Section::DebugTypes ? k_section_bit : 0);
}
std::optional<uint32_t> file_index() const {
if (m_file_index_valid)
return m_file_index;
return std::nullopt;
}
Section section() const { return static_cast<Section>(m_section); }
dw_offset_t die_offset() const { return m_die_offset; }
bool operator<(DIERef other) const {
if (m_file_index_valid != other.m_file_index_valid)
return m_file_index_valid < other.m_file_index_valid;
if (m_file_index_valid && (m_file_index != other.m_file_index))
return m_file_index < other.m_file_index;
if (m_section != other.m_section)
return m_section < other.m_section;
return m_die_offset < other.m_die_offset;
}
bool operator==(const DIERef &rhs) const {
return file_index() == rhs.file_index() && m_section == rhs.m_section &&
m_die_offset == rhs.m_die_offset;
}
bool operator!=(const DIERef &rhs) const { return !(*this == rhs); }
static std::optional<DIERef> Decode(const DataExtractor &data,
lldb::offset_t *offset_ptr);
void Encode(DataEncoder &encoder) const;
static constexpr uint64_t k_die_offset_bit_size = DW_DIE_OFFSET_MAX_BITSIZE;
static constexpr uint64_t k_file_index_bit_size =
64 - DW_DIE_OFFSET_MAX_BITSIZE - 2;
static constexpr uint64_t k_file_index_valid_bit =
(1ull << (k_file_index_bit_size + k_die_offset_bit_size));
static constexpr uint64_t k_section_bit =
(1ull << (k_file_index_bit_size + k_die_offset_bit_size + 1));
static constexpr uint64_t
k_file_index_mask = (~0ull) >> (64 - k_file_index_bit_size);
static constexpr uint64_t k_die_offset_mask = (~0ull) >>
(64 - k_die_offset_bit_size);
private:
dw_offset_t m_die_offset : k_die_offset_bit_size;
dw_offset_t m_file_index : k_file_index_bit_size;
dw_offset_t m_file_index_valid : 1;
dw_offset_t m_section : 1;
};
static_assert(sizeof(DIERef) == 8);
typedef std::vector<DIERef> DIEArray;
}
}
namespace llvm {
template <> struct format_provider<lldb_private::plugin::dwarf::DIERef> {
static void format(const lldb_private::plugin::dwarf::DIERef &ref,
raw_ostream &OS, StringRef Style);
};
}
#endif