xref: /freebsd-src/contrib/llvm-project/lldb/source/Plugins/SymbolFile/DWARF/DWARFBaseDIE.cpp (revision a7dea1671b87c07d2d266f836bfa8b58efc7c134)
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 llvm::Optional<DIERef> DWARFBaseDIE::GetDIERef() const {
21   if (!IsValid())
22     return llvm::None;
23 
24   return DIERef(m_cu->GetSymbolFileDWARF().GetDwoNum(), m_cu->GetDebugSection(),
25                 m_die->GetOffset());
26 }
27 
28 dw_tag_t DWARFBaseDIE::Tag() const {
29   if (m_die)
30     return m_die->Tag();
31   else
32     return 0;
33 }
34 
35 const char *DWARFBaseDIE::GetTagAsCString() const {
36   return lldb_private::DW_TAG_value_to_name(Tag());
37 }
38 
39 const char *DWARFBaseDIE::GetAttributeValueAsString(const dw_attr_t attr,
40                                                 const char *fail_value) const {
41   if (IsValid())
42     return m_die->GetAttributeValueAsString(GetCU(), attr, fail_value);
43   else
44     return fail_value;
45 }
46 
47 uint64_t DWARFBaseDIE::GetAttributeValueAsUnsigned(const dw_attr_t attr,
48                                                uint64_t fail_value) const {
49   if (IsValid())
50     return m_die->GetAttributeValueAsUnsigned(GetCU(), attr, fail_value);
51   else
52     return fail_value;
53 }
54 
55 uint64_t DWARFBaseDIE::GetAttributeValueAsAddress(const dw_attr_t attr,
56                                               uint64_t fail_value) const {
57   if (IsValid())
58     return m_die->GetAttributeValueAsAddress(GetCU(), attr, fail_value);
59   else
60     return fail_value;
61 }
62 
63 lldb::user_id_t DWARFBaseDIE::GetID() const {
64   if (IsValid())
65     return GetDWARF()->GetUID(*this);
66   return LLDB_INVALID_UID;
67 }
68 
69 const char *DWARFBaseDIE::GetName() const {
70   if (IsValid())
71     return m_die->GetName(m_cu);
72   else
73     return nullptr;
74 }
75 
76 lldb::LanguageType DWARFBaseDIE::GetLanguage() const {
77   if (IsValid())
78     return m_cu->GetLanguageType();
79   else
80     return lldb::eLanguageTypeUnknown;
81 }
82 
83 lldb::ModuleSP DWARFBaseDIE::GetModule() const {
84   SymbolFileDWARF *dwarf = GetDWARF();
85   if (dwarf)
86     return dwarf->GetObjectFile()->GetModule();
87   else
88     return lldb::ModuleSP();
89 }
90 
91 dw_offset_t DWARFBaseDIE::GetOffset() const {
92   if (IsValid())
93     return m_die->GetOffset();
94   else
95     return DW_INVALID_OFFSET;
96 }
97 
98 SymbolFileDWARF *DWARFBaseDIE::GetDWARF() const {
99   if (m_cu)
100     return &m_cu->GetSymbolFileDWARF();
101   else
102     return nullptr;
103 }
104 
105 lldb_private::TypeSystem *DWARFBaseDIE::GetTypeSystem() const {
106   if (m_cu)
107     return m_cu->GetTypeSystem();
108   else
109     return nullptr;
110 }
111 
112 DWARFASTParser *DWARFBaseDIE::GetDWARFParser() const {
113   lldb_private::TypeSystem *type_system = GetTypeSystem();
114   if (type_system)
115     return type_system->GetDWARFParser();
116   else
117     return nullptr;
118 }
119 
120 bool DWARFBaseDIE::HasChildren() const {
121   return m_die && m_die->HasChildren();
122 }
123 
124 bool DWARFBaseDIE::Supports_DW_AT_APPLE_objc_complete_type() const {
125   return IsValid() && GetDWARF()->Supports_DW_AT_APPLE_objc_complete_type(m_cu);
126 }
127 
128 size_t DWARFBaseDIE::GetAttributes(DWARFAttributes &attributes,
129                                uint32_t depth) const {
130   if (IsValid())
131     return m_die->GetAttributes(m_cu, attributes, depth);
132   if (depth == 0)
133     attributes.Clear();
134   return 0;
135 }
136 
137 bool operator==(const DWARFBaseDIE &lhs, const DWARFBaseDIE &rhs) {
138   return lhs.GetDIE() == rhs.GetDIE() && lhs.GetCU() == rhs.GetCU();
139 }
140 
141 bool operator!=(const DWARFBaseDIE &lhs, const DWARFBaseDIE &rhs) {
142   return !(lhs == rhs);
143 }
144 
145 const DWARFDataExtractor &DWARFBaseDIE::GetData() const {
146   // Clients must check if this DIE is valid before calling this function.
147   assert(IsValid());
148   return m_cu->GetData();
149 }
150