xref: /freebsd/contrib/llvm-project/lldb/source/Plugins/SymbolFile/DWARF/DWARFAttribute.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1 //===-- DWARFAttribute.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 "DWARFAttribute.h"
10 #include "DWARFUnit.h"
11 #include "DWARFDebugInfo.h"
12 
13 using namespace lldb_private::dwarf;
14 using namespace lldb_private::plugin::dwarf;
15 
16 DWARFAttributes::DWARFAttributes() : m_infos() {}
17 
18 DWARFAttributes::~DWARFAttributes() = default;
19 
20 uint32_t DWARFAttributes::FindAttributeIndex(dw_attr_t attr) const {
21   collection::const_iterator end = m_infos.end();
22   collection::const_iterator beg = m_infos.begin();
23   collection::const_iterator pos;
24   for (pos = beg; pos != end; ++pos) {
25     if (pos->attr.get_attr() == attr)
26       return std::distance(beg, pos);
27   }
28   return UINT32_MAX;
29 }
30 
31 void DWARFAttributes::Append(const DWARFFormValue &form_value,
32                              dw_offset_t attr_die_offset, dw_attr_t attr) {
33   AttributeValue attr_value = {const_cast<DWARFUnit *>(form_value.GetUnit()),
34                                attr_die_offset,
35                                {attr, form_value.Form(), form_value.Value()}};
36   m_infos.push_back(attr_value);
37 }
38 
39 bool DWARFAttributes::ExtractFormValueAtIndex(
40     uint32_t i, DWARFFormValue &form_value) const {
41   const DWARFUnit *cu = CompileUnitAtIndex(i);
42   form_value.SetUnit(cu);
43   form_value.SetForm(FormAtIndex(i));
44   if (form_value.Form() == DW_FORM_implicit_const) {
45     form_value.SetValue(ValueAtIndex(i));
46     return true;
47   }
48   lldb::offset_t offset = DIEOffsetAtIndex(i);
49   return form_value.ExtractValue(cu->GetData(), &offset);
50 }
51 
52 DWARFDIE
53 DWARFAttributes::FormValueAsReference(dw_attr_t attr) const {
54   const uint32_t attr_idx = FindAttributeIndex(attr);
55   if (attr_idx != UINT32_MAX)
56     return FormValueAsReferenceAtIndex(attr_idx);
57   return {};
58 }
59 
60 DWARFDIE
61 DWARFAttributes::FormValueAsReferenceAtIndex(uint32_t i) const {
62   DWARFFormValue form_value;
63   if (ExtractFormValueAtIndex(i, form_value))
64     return form_value.Reference();
65   return {};
66 }
67