1 //===-- DIERef.cpp --------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "DIERef.h"
10 #include "lldb/Utility/DataEncoder.h"
11 #include "lldb/Utility/DataExtractor.h"
12 #include "llvm/Support/Format.h"
13 #include <optional>
14 
15 using namespace lldb;
16 using namespace lldb_private;
17 
18 void llvm::format_provider<DIERef>::format(const DIERef &ref, raw_ostream &OS,
19                                            StringRef Style) {
20   if (ref.file_index())
21     OS << format_hex_no_prefix(*ref.file_index(), 8) << "/";
22   OS << (ref.section() == DIERef::DebugInfo ? "INFO" : "TYPE");
23   OS << "/" << format_hex_no_prefix(ref.die_offset(), 8);
24 }
25 
26 std::optional<DIERef> DIERef::Decode(const DataExtractor &data,
27                                      lldb::offset_t *offset_ptr) {
28   DIERef die_ref(data.GetU64(offset_ptr));
29 
30   // DIE offsets can't be zero and if we fail to decode something from data,
31   // it will return 0
32   if (!die_ref.die_offset())
33     return std::nullopt;
34 
35   return die_ref;
36 }
37 
38 void DIERef::Encode(DataEncoder &encoder) const { encoder.AppendU64(get_id()); }
39