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