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