summaryrefslogtreecommitdiffstats
path: root/libdw/c++/data-values.hh
blob: 71244ee993ab9c0a34cc54ebcc5c92604070f321 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* elfutils::dwarf_data common internal templates.
   Copyright (C) 2009 Red Hat, Inc.
   This file is part of elfutils.

   This file is free software; you can redistribute it and/or modify
   it under the terms of either

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at
       your option) any later version

   or

     * the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at
       your option) any later version

   or both in parallel, as here.

   elfutils is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see <http://www.gnu.org/licenses/>.  */

#include "dwarf_data"

template<typename v, typename subtype>
static inline bool is_a (const typename v::value_dispatch *value)
{
  return dynamic_cast<const subtype *> (value) != NULL;
}

namespace elfutils
{
  template<class impl, typename v>
  dwarf::value_space
  dwarf_data::attr_value<impl, v>::what_space () const
  {
    if (is_a<v, typename v::value_flag> (_m_value))
      return dwarf::VS_flag;
    if (is_a<v, typename v::value_dwarf_constant> (_m_value))
      return dwarf::VS_dwarf_constant;
    if (is_a<v, typename v::value_reference> (_m_value))
      return dwarf::VS_reference;
    if (is_a<v, typename v::value_lineptr> (_m_value))
      return dwarf::VS_lineptr;
    if (is_a<v, typename v::value_macptr> (_m_value))
      return dwarf::VS_macptr;
    if (is_a<v, typename v::value_rangelistptr> (_m_value))
      return dwarf::VS_rangelistptr;
    if (is_a<v, typename v::value_identifier> (_m_value))
      return dwarf::VS_identifier;
    if (is_a<v, typename v::value_string> (_m_value))
      return dwarf::VS_string;
    if (is_a<v, typename v::value_source_file> (_m_value))
      return dwarf::VS_source_file;
    if (is_a<v, typename v::value_source_line> (_m_value))
      return dwarf::VS_source_line;
    if (is_a<v, typename v::value_source_column> (_m_value))
      return dwarf::VS_source_column;
    if (is_a<v, typename v::value_address> (_m_value))
      return dwarf::VS_address;
    if (is_a<v, typename v::value_constant> (_m_value)
	|| is_a<v, typename v::value_constant_block> (_m_value))
      return dwarf::VS_constant;
    if (is_a<v, typename v::value_location> (_m_value))
      return dwarf::VS_location;

    throw std::logic_error ("attr_value has no known value_space!");
  }

  template<typename attr_pair>
  static inline std::string
  attribute_string (const attr_pair &attr)
  {
    std::string result = dwarf::attributes::name (attr.first);
    result += "=";
    result += attr.second.to_string ();
    return result;
  }

  template<typename die_type>
  std::string
  die_string (const die_type &die)
  {
    std::string result ("<");
    result += dwarf::tags::name (die.tag ());

    typename die_type::attributes_type::const_iterator name_attr
      = die.attributes ().find (::DW_AT_name);
    if (name_attr != die.attributes ().end ())
      {
	result += " ";
	result += to_string (*name_attr);
      }

    result += die.has_children () ? ">" : "/>";
    return result;
  }

};