1 //===-- DWARFBaseDIE.cpp ---------------------------------------*- C++ -*-===//
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 
18 using namespace lldb_private;
19 
20 DIERef DWARFBaseDIE::GetDIERef() const {
21   if (!IsValid())
22     return DIERef();
23 
24   dw_offset_t cu_offset = m_cu->GetOffset();
25   if (m_cu->GetBaseObjOffset() != DW_INVALID_OFFSET)
26     cu_offset = m_cu->GetBaseObjOffset();
27   return DIERef(cu_offset, m_die->GetOffset());
28 }
29 
30 dw_tag_t DWARFBaseDIE::Tag() const {
31   if (m_die)
32     return m_die->Tag();
33   else
34     return 0;
35 }
36 
37 const char *DWARFBaseDIE::GetTagAsCString() const {
38   return lldb_private::DW_TAG_value_to_name(Tag());
39 }
40 
41 const char *DWARFBaseDIE::GetAttributeValueAsString(const dw_attr_t attr,
42                                                 const char *fail_value) const {
43   if (IsValid())
44     return m_die->GetAttributeValueAsString(GetDWARF(), GetCU(), attr,
45                                             fail_value);
46   else
47     return fail_value;
48 }
49 
50 uint64_t DWARFBaseDIE::GetAttributeValueAsUnsigned(const dw_attr_t attr,
51                                                uint64_t fail_value) const {
52   if (IsValid())
53     return m_die->GetAttributeValueAsUnsigned(GetDWARF(), GetCU(), attr,
54                                               fail_value);
55   else
56     return fail_value;
57 }
58 
59 int64_t DWARFBaseDIE::GetAttributeValueAsSigned(const dw_attr_t attr,
60                                             int64_t fail_value) const {
61   if (IsValid())
62     return m_die->GetAttributeValueAsSigned(GetDWARF(), GetCU(), attr,
63                                             fail_value);
64   else
65     return fail_value;
66 }
67 
68 uint64_t DWARFBaseDIE::GetAttributeValueAsReference(const dw_attr_t attr,
69                                                 uint64_t fail_value) const {
70   if (IsValid())
71     return m_die->GetAttributeValueAsReference(GetDWARF(), GetCU(), attr,
72                                                fail_value);
73   else
74     return fail_value;
75 }
76 
77 uint64_t DWARFBaseDIE::GetAttributeValueAsAddress(const dw_attr_t attr,
78                                               uint64_t fail_value) const {
79   if (IsValid())
80     return m_die->GetAttributeValueAsAddress(GetDWARF(), GetCU(), attr,
81                                              fail_value);
82   else
83     return fail_value;
84 }
85 
86 lldb::user_id_t DWARFBaseDIE::GetID() const {
87   return GetDIERef().GetUID(GetDWARF());
88 }
89 
90 const char *DWARFBaseDIE::GetName() const {
91   if (IsValid())
92     return m_die->GetName(GetDWARF(), m_cu);
93   else
94     return nullptr;
95 }
96 
97 lldb::LanguageType DWARFBaseDIE::GetLanguage() const {
98   if (IsValid())
99     return m_cu->GetLanguageType();
100   else
101     return lldb::eLanguageTypeUnknown;
102 }
103 
104 lldb::ModuleSP DWARFBaseDIE::GetModule() const {
105   SymbolFileDWARF *dwarf = GetDWARF();
106   if (dwarf)
107     return dwarf->GetObjectFile()->GetModule();
108   else
109     return lldb::ModuleSP();
110 }
111 
112 lldb_private::CompileUnit *DWARFBaseDIE::GetLLDBCompileUnit() const {
113   if (IsValid())
114     return GetDWARF()->GetCompUnitForDWARFCompUnit(GetCU());
115   else
116     return nullptr;
117 }
118 
119 dw_offset_t DWARFBaseDIE::GetOffset() const {
120   if (IsValid())
121     return m_die->GetOffset();
122   else
123     return DW_INVALID_OFFSET;
124 }
125 
126 dw_offset_t DWARFBaseDIE::GetCompileUnitRelativeOffset() const {
127   if (IsValid())
128     return m_die->GetOffset() - m_cu->GetOffset();
129   else
130     return DW_INVALID_OFFSET;
131 }
132 
133 SymbolFileDWARF *DWARFBaseDIE::GetDWARF() const {
134   if (m_cu)
135     return m_cu->GetSymbolFileDWARF();
136   else
137     return nullptr;
138 }
139 
140 lldb_private::TypeSystem *DWARFBaseDIE::GetTypeSystem() const {
141   if (m_cu)
142     return m_cu->GetTypeSystem();
143   else
144     return nullptr;
145 }
146 
147 DWARFASTParser *DWARFBaseDIE::GetDWARFParser() const {
148   lldb_private::TypeSystem *type_system = GetTypeSystem();
149   if (type_system)
150     return type_system->GetDWARFParser();
151   else
152     return nullptr;
153 }
154 
155 bool DWARFBaseDIE::HasChildren() const {
156   return m_die && m_die->HasChildren();
157 }
158 
159 bool DWARFBaseDIE::Supports_DW_AT_APPLE_objc_complete_type() const {
160   return IsValid() && GetDWARF()->Supports_DW_AT_APPLE_objc_complete_type(m_cu);
161 }
162 
163 size_t DWARFBaseDIE::GetAttributes(DWARFAttributes &attributes,
164                                uint32_t depth) const {
165   if (IsValid()) {
166     return m_die->GetAttributes(m_cu, m_cu->GetFixedFormSizes(), attributes,
167                                 depth);
168   }
169   if (depth == 0)
170     attributes.Clear();
171   return 0;
172 }
173 
174 bool operator==(const DWARFBaseDIE &lhs, const DWARFBaseDIE &rhs) {
175   return lhs.GetDIE() == rhs.GetDIE() && lhs.GetCU() == rhs.GetCU();
176 }
177 
178 bool operator!=(const DWARFBaseDIE &lhs, const DWARFBaseDIE &rhs) {
179   return !(lhs == rhs);
180 }
181 
182 const DWARFDataExtractor &DWARFBaseDIE::GetData() const {
183   // Clients must check if this DIE is valid before calling this function.
184   assert(IsValid());
185   return m_cu->GetData();
186 }
187