1 //===-- DWARFAttribute.cpp --------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "DWARFAttribute.h"
11 #include "DWARFUnit.h"
12 #include "DWARFDebugInfo.h"
13 
DWARFAttributes()14 DWARFAttributes::DWARFAttributes() : m_infos() {}
15 
~DWARFAttributes()16 DWARFAttributes::~DWARFAttributes() {}
17 
FindAttributeIndex(dw_attr_t attr) const18 uint32_t DWARFAttributes::FindAttributeIndex(dw_attr_t attr) const {
19   collection::const_iterator end = m_infos.end();
20   collection::const_iterator beg = m_infos.begin();
21   collection::const_iterator pos;
22   for (pos = beg; pos != end; ++pos) {
23     if (pos->attr.get_attr() == attr)
24       return std::distance(beg, pos);
25   }
26   return UINT32_MAX;
27 }
28 
Append(const DWARFUnit * cu,dw_offset_t attr_die_offset,dw_attr_t attr,dw_form_t form)29 void DWARFAttributes::Append(const DWARFUnit *cu, dw_offset_t attr_die_offset,
30                              dw_attr_t attr, dw_form_t form) {
31   AttributeValue attr_value = {
32       cu, attr_die_offset, {attr, form, DWARFFormValue::ValueType()}};
33   m_infos.push_back(attr_value);
34 }
35 
ContainsAttribute(dw_attr_t attr) const36 bool DWARFAttributes::ContainsAttribute(dw_attr_t attr) const {
37   return FindAttributeIndex(attr) != UINT32_MAX;
38 }
39 
RemoveAttribute(dw_attr_t attr)40 bool DWARFAttributes::RemoveAttribute(dw_attr_t attr) {
41   uint32_t attr_index = FindAttributeIndex(attr);
42   if (attr_index != UINT32_MAX) {
43     m_infos.erase(m_infos.begin() + attr_index);
44     return true;
45   }
46   return false;
47 }
48 
ExtractFormValueAtIndex(uint32_t i,DWARFFormValue & form_value) const49 bool DWARFAttributes::ExtractFormValueAtIndex(
50     uint32_t i, DWARFFormValue &form_value) const {
51   const DWARFUnit *cu = CompileUnitAtIndex(i);
52   form_value.SetCompileUnit(cu);
53   form_value.SetForm(FormAtIndex(i));
54   lldb::offset_t offset = DIEOffsetAtIndex(i);
55   return form_value.ExtractValue(cu->GetData(), &offset);
56 }
57 
FormValueAsUnsigned(dw_attr_t attr,uint64_t fail_value) const58 uint64_t DWARFAttributes::FormValueAsUnsigned(dw_attr_t attr,
59                                               uint64_t fail_value) const {
60   const uint32_t attr_idx = FindAttributeIndex(attr);
61   if (attr_idx != UINT32_MAX)
62     return FormValueAsUnsignedAtIndex(attr_idx, fail_value);
63   return fail_value;
64 }
65 
66 uint64_t
FormValueAsUnsignedAtIndex(uint32_t i,uint64_t fail_value) const67 DWARFAttributes::FormValueAsUnsignedAtIndex(uint32_t i,
68                                             uint64_t fail_value) const {
69   DWARFFormValue form_value;
70   if (ExtractFormValueAtIndex(i, form_value))
71     return form_value.Reference();
72   return fail_value;
73 }
74