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 uint64_t DWARFBaseDIE::GetAttributeValueAsReference(const dw_attr_t attr,
60                                                 uint64_t fail_value) const {
61   if (IsValid())
62     return m_die->GetAttributeValueAsReference(GetDWARF(), GetCU(), attr,
63                                                fail_value);
64   else
65     return fail_value;
66 }
67 
68 uint64_t DWARFBaseDIE::GetAttributeValueAsAddress(const dw_attr_t attr,
69                                               uint64_t fail_value) const {
70   if (IsValid())
71     return m_die->GetAttributeValueAsAddress(GetDWARF(), GetCU(), attr,
72                                              fail_value);
73   else
74     return fail_value;
75 }
76 
77 lldb::user_id_t DWARFBaseDIE::GetID() const {
78   if (IsValid())
79     return GetDWARF()->GetUID(*this);
80   return LLDB_INVALID_UID;
81 }
82 
83 const char *DWARFBaseDIE::GetName() const {
84   if (IsValid())
85     return m_die->GetName(GetDWARF(), m_cu);
86   else
87     return nullptr;
88 }
89 
90 lldb::LanguageType DWARFBaseDIE::GetLanguage() const {
91   if (IsValid())
92     return m_cu->GetLanguageType();
93   else
94     return lldb::eLanguageTypeUnknown;
95 }
96 
97 lldb::ModuleSP DWARFBaseDIE::GetModule() const {
98   SymbolFileDWARF *dwarf = GetDWARF();
99   if (dwarf)
100     return dwarf->GetObjectFile()->GetModule();
101   else
102     return lldb::ModuleSP();
103 }
104 
105 lldb_private::CompileUnit *DWARFBaseDIE::GetLLDBCompileUnit() const {
106   if (IsValid())
107     return GetDWARF()->GetCompUnitForDWARFCompUnit(GetCU());
108   else
109     return nullptr;
110 }
111 
112 dw_offset_t DWARFBaseDIE::GetOffset() const {
113   if (IsValid())
114     return m_die->GetOffset();
115   else
116     return DW_INVALID_OFFSET;
117 }
118 
119 SymbolFileDWARF *DWARFBaseDIE::GetDWARF() const {
120   if (m_cu)
121     return m_cu->GetSymbolFileDWARF();
122   else
123     return nullptr;
124 }
125 
126 lldb_private::TypeSystem *DWARFBaseDIE::GetTypeSystem() const {
127   if (m_cu)
128     return m_cu->GetTypeSystem();
129   else
130     return nullptr;
131 }
132 
133 DWARFASTParser *DWARFBaseDIE::GetDWARFParser() const {
134   lldb_private::TypeSystem *type_system = GetTypeSystem();
135   if (type_system)
136     return type_system->GetDWARFParser();
137   else
138     return nullptr;
139 }
140 
141 bool DWARFBaseDIE::HasChildren() const {
142   return m_die && m_die->HasChildren();
143 }
144 
145 bool DWARFBaseDIE::Supports_DW_AT_APPLE_objc_complete_type() const {
146   return IsValid() && GetDWARF()->Supports_DW_AT_APPLE_objc_complete_type(m_cu);
147 }
148 
149 size_t DWARFBaseDIE::GetAttributes(DWARFAttributes &attributes,
150                                uint32_t depth) const {
151   if (IsValid()) {
152     return m_die->GetAttributes(m_cu, m_cu->GetFixedFormSizes(), attributes,
153                                 depth);
154   }
155   if (depth == 0)
156     attributes.Clear();
157   return 0;
158 }
159 
160 bool operator==(const DWARFBaseDIE &lhs, const DWARFBaseDIE &rhs) {
161   return lhs.GetDIE() == rhs.GetDIE() && lhs.GetCU() == rhs.GetCU();
162 }
163 
164 bool operator!=(const DWARFBaseDIE &lhs, const DWARFBaseDIE &rhs) {
165   return !(lhs == rhs);
166 }
167 
168 const DWARFDataExtractor &DWARFBaseDIE::GetData() const {
169   // Clients must check if this DIE is valid before calling this function.
170   assert(IsValid());
171   return m_cu->GetData();
172 }
173