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 
22 std::optional<DIERef> DWARFBaseDIE::GetDIERef() const {
23   if (!IsValid())
24     return std::nullopt;
25 
26   return DIERef(m_cu->GetSymbolFileDWARF().GetFileIndex(),
27                 m_cu->GetDebugSection(), 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 llvm::dwarf::DW_TAG_null;
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(GetCU(), attr, fail_value);
45   else
46     return fail_value;
47 }
48 
49 uint64_t DWARFBaseDIE::GetAttributeValueAsUnsigned(const dw_attr_t attr,
50                                                uint64_t fail_value) const {
51   if (IsValid())
52     return m_die->GetAttributeValueAsUnsigned(GetCU(), attr, fail_value);
53   else
54     return fail_value;
55 }
56 
57 std::optional<uint64_t>
58 DWARFBaseDIE::GetAttributeValueAsOptionalUnsigned(const dw_attr_t attr) const {
59   if (IsValid())
60     return m_die->GetAttributeValueAsOptionalUnsigned(GetCU(), attr);
61   return std::nullopt;
62 }
63 
64 uint64_t DWARFBaseDIE::GetAttributeValueAsAddress(const dw_attr_t attr,
65                                               uint64_t fail_value) const {
66   if (IsValid())
67     return m_die->GetAttributeValueAsAddress(GetCU(), attr, fail_value);
68   else
69     return fail_value;
70 }
71 
72 lldb::user_id_t DWARFBaseDIE::GetID() const {
73   const std::optional<DIERef> &ref = this->GetDIERef();
74   if (ref)
75     return ref->get_id();
76 
77   return LLDB_INVALID_UID;
78 }
79 
80 const char *DWARFBaseDIE::GetName() const {
81   if (IsValid())
82     return m_die->GetName(m_cu);
83   else
84     return nullptr;
85 }
86 
87 lldb::ModuleSP DWARFBaseDIE::GetModule() const {
88   SymbolFileDWARF *dwarf = GetDWARF();
89   if (dwarf)
90     return dwarf->GetObjectFile()->GetModule();
91   else
92     return lldb::ModuleSP();
93 }
94 
95 dw_offset_t DWARFBaseDIE::GetOffset() const {
96   if (IsValid())
97     return m_die->GetOffset();
98   else
99     return DW_INVALID_OFFSET;
100 }
101 
102 SymbolFileDWARF *DWARFBaseDIE::GetDWARF() const {
103   if (m_cu)
104     return &m_cu->GetSymbolFileDWARF();
105   else
106     return nullptr;
107 }
108 
109 bool DWARFBaseDIE::HasChildren() const {
110   return m_die && m_die->HasChildren();
111 }
112 
113 bool DWARFBaseDIE::Supports_DW_AT_APPLE_objc_complete_type() const {
114   return IsValid() && GetDWARF()->Supports_DW_AT_APPLE_objc_complete_type(m_cu);
115 }
116 
117 DWARFAttributes DWARFBaseDIE::GetAttributes(Recurse recurse) const {
118   if (IsValid())
119     return m_die->GetAttributes(m_cu, recurse);
120   return DWARFAttributes();
121 }
122 
123 bool operator==(const DWARFBaseDIE &lhs, const DWARFBaseDIE &rhs) {
124   return lhs.GetDIE() == rhs.GetDIE() && lhs.GetCU() == rhs.GetCU();
125 }
126 
127 bool operator!=(const DWARFBaseDIE &lhs, const DWARFBaseDIE &rhs) {
128   return !(lhs == rhs);
129 }
130 
131 const DWARFDataExtractor &DWARFBaseDIE::GetData() const {
132   // Clients must check if this DIE is valid before calling this function.
133   assert(IsValid());
134   return m_cu->GetData();
135 }
136