xref: /llvm-project/lldb/source/Plugins/SymbolFile/DWARF/DWARFBaseDIE.cpp (revision f1763888bb96c9c4069d8d069083371965561111)
1 //===-- DWARFBaseDIE.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 "DWARFBaseDIE.h"
10 
11 #include "DWARFUnit.h"
12 #include "DWARFDebugInfoEntry.h"
13 #include "SymbolFileDWARF.h"
14 
15 #include "lldb/Core/Module.h"
16 #include "lldb/Symbol/ObjectFile.h"
17 #include "lldb/Utility/Log.h"
18 #include <optional>
19 
20 using namespace lldb_private;
21 using namespace lldb_private::plugin::dwarf;
22 
23 std::optional<DIERef> DWARFBaseDIE::GetDIERef() const {
24   if (!IsValid())
25     return std::nullopt;
26 
27   return DIERef(m_cu->GetSymbolFileDWARF().GetFileIndex(),
28                 m_cu->GetDebugSection(), m_die->GetOffset());
29 }
30 
31 dw_tag_t DWARFBaseDIE::Tag() const {
32   if (m_die)
33     return m_die->Tag();
34   else
35     return llvm::dwarf::DW_TAG_null;
36 }
37 
38 const char *DWARFBaseDIE::GetAttributeValueAsString(const dw_attr_t attr,
39                                                 const char *fail_value) const {
40   if (IsValid())
41     return m_die->GetAttributeValueAsString(GetCU(), attr, fail_value);
42   else
43     return fail_value;
44 }
45 
46 uint64_t DWARFBaseDIE::GetAttributeValueAsUnsigned(const dw_attr_t attr,
47                                                uint64_t fail_value) const {
48   if (IsValid())
49     return m_die->GetAttributeValueAsUnsigned(GetCU(), attr, fail_value);
50   else
51     return fail_value;
52 }
53 
54 std::optional<uint64_t>
55 DWARFBaseDIE::GetAttributeValueAsOptionalUnsigned(const dw_attr_t attr) const {
56   if (IsValid())
57     return m_die->GetAttributeValueAsOptionalUnsigned(GetCU(), attr);
58   return std::nullopt;
59 }
60 
61 uint64_t DWARFBaseDIE::GetAttributeValueAsAddress(const dw_attr_t attr,
62                                               uint64_t fail_value) const {
63   if (IsValid())
64     return m_die->GetAttributeValueAsAddress(GetCU(), attr, fail_value);
65   else
66     return fail_value;
67 }
68 
69 lldb::user_id_t DWARFBaseDIE::GetID() const {
70   const std::optional<DIERef> &ref = this->GetDIERef();
71   if (ref)
72     return ref->get_id();
73 
74   return LLDB_INVALID_UID;
75 }
76 
77 const char *DWARFBaseDIE::GetName() const {
78   if (IsValid())
79     return m_die->GetName(m_cu);
80   else
81     return nullptr;
82 }
83 
84 lldb::ModuleSP DWARFBaseDIE::GetModule() const {
85   SymbolFileDWARF *dwarf = GetDWARF();
86   if (dwarf)
87     return dwarf->GetObjectFile()->GetModule();
88   else
89     return lldb::ModuleSP();
90 }
91 
92 dw_offset_t DWARFBaseDIE::GetOffset() const {
93   if (IsValid())
94     return m_die->GetOffset();
95   else
96     return DW_INVALID_OFFSET;
97 }
98 
99 SymbolFileDWARF *DWARFBaseDIE::GetDWARF() const {
100   if (m_cu)
101     return &m_cu->GetSymbolFileDWARF();
102   else
103     return nullptr;
104 }
105 
106 bool DWARFBaseDIE::HasChildren() const {
107   return m_die && m_die->HasChildren();
108 }
109 
110 DWARFAttributes DWARFBaseDIE::GetAttributes(Recurse recurse) const {
111   if (IsValid())
112     return m_die->GetAttributes(m_cu, recurse);
113   return DWARFAttributes();
114 }
115 
116 namespace lldb_private::plugin {
117 namespace dwarf {
118 bool operator==(const DWARFBaseDIE &lhs, const DWARFBaseDIE &rhs) {
119   return lhs.GetDIE() == rhs.GetDIE() && lhs.GetCU() == rhs.GetCU();
120 }
121 
122 bool operator!=(const DWARFBaseDIE &lhs, const DWARFBaseDIE &rhs) {
123   return !(lhs == rhs);
124 }
125 } // namespace dwarf
126 } // namespace lldb_private::plugin
127 
128 const DWARFDataExtractor &DWARFBaseDIE::GetData() const {
129   // Clients must check if this DIE is valid before calling this function.
130   assert(IsValid());
131   return m_cu->GetData();
132 }
133