/* elfutils::dwarf_data -- internal DWARF data representations in -*- C++ -*- Copyright (C) 2009 Red Hat, Inc. This file is part of Red Hat elfutils. Red Hat elfutils is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. Red Hat 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 a copy of the GNU General Public License along with Red Hat elfutils; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. In addition, as a special exception, Red Hat, Inc. gives You the additional right to link the code of Red Hat elfutils with code licensed under any Open Source Initiative certified open source license (http://www.opensource.org/licenses/index.php) which requires the distribution of source code with any binary distribution and to distribute linked combinations of the two. Non-GPL Code permitted under this exception must only link to the code of Red Hat elfutils through those well defined interfaces identified in the file named EXCEPTION found in the source code files (the "Approved Interfaces"). The files of Non-GPL Code may instantiate templates or use macros or inline functions from the Approved Interfaces without causing the resulting work to be covered by the GNU General Public License. Only Red Hat, Inc. may make changes or additions to the list of Approved Interfaces. Red Hat's grant of this exception is conditioned upon your not adding any new exceptions. If you wish to add a new Approved Interface or exception, please contact Red Hat. You must obey the GNU General Public License in all respects for all of the Red Hat elfutils code and other code used in conjunction with Red Hat elfutils except the Non-GPL Code covered by this exception. If you modify this file, you may extend this exception to your version of the file, but you are not obligated to do so. If you do not wish to provide this exception without modification, you must delete this exception statement from your version and license this file solely under the GPL without exception. Red Hat elfutils is an included package of the Open Invention Network. An included package of the Open Invention Network is a package for which Open Invention Network licensees cross-license their patents. No patent license is granted, either expressly or impliedly, by designation as an included package. Should you wish to participate in the Open Invention Network licensing program, please visit www.openinventionnetwork.com . */ #ifndef _ELFUTILS_DWARF_DATA #define _ELFUTILS_DWARF_DATA 1 #include "dwarf" #include #include /* This contains common classes/templates used by dwarf_output and dwarf_edit. These are implementations of the "boring" components of the dwarf object interface. */ namespace elfutils { // This is a class only for scoping purposes. // It contains no members, only inner classes. class dwarf_data { public: // Main container anchoring all the output. template class compile_units : public std::list { friend class subr::create_container; protected: typedef std::list _base; // Constructor copying CUs from input container. template inline compile_units (const input &other, arg_type &arg) : _base (subr::argify (other.begin (), arg), subr::argify (other.end (), arg)) {} public: // Default constructor: an empty container, no CUs. inline compile_units () {} template bool operator== (const other_children &other) const { return subr::container_equal (*this, other); } template bool operator!= (const other_children &other) const { return !(*this == other); } }; template class compile_unit : public impl::debug_info_entry { friend class subr::create_container; friend class impl::compile_units; protected: template static inline const input &require_cu (const input &cu) { if (cu.tag () != ::DW_TAG_compile_unit) throw std::runtime_error ("top-level debug_info_entry must be DW_TAG_compile_unit"); return cu; } template inline void set (const die_type &die, arg_type &arg) { impl::debug_info_entry::set (require_cu (die), arg); } public: explicit inline compile_unit () : impl::debug_info_entry () { this->_m_tag = ::DW_TAG_compile_unit; } inline compile_unit (const compile_unit &other) : impl::debug_info_entry (require_cu (other)) {} template inline compile_unit (const input &cu, arg_type &arg) : impl::debug_info_entry (typename impl::debug_info_entry::pointer (), require_cu (cu), arg) {} /* Assignment details are up to the base class. We just ensure it's really a compile_unit. */ inline compile_unit & operator= (const typename impl::debug_info_entry &other) { impl::debug_info_entry::operator= (require_cu (other)); return *this; } // Fetch the CU's DW_AT_stmt_list. inline const typename impl::line_info_table &line_info () const { return this->attributes ()[::DW_AT_stmt_list].line_info (); } inline typename impl::line_info_table &line_info () { return this->attributes ()[::DW_AT_stmt_list].line_info (); } // Convenience methods for line_info_table sub-containers. inline const typename impl::directory_table &include_directories () const { return line_info ().include_directories (); } inline typename impl::directory_table &include_directories () { return line_info ().include_directories (); } inline const typename impl::line_table &lines () const { return line_info ().lines (); } inline typename impl::line_table &lines () { return line_info ().lines (); } }; class source_file { private: std::string _m_name; ::Dwarf_Word _m_mtime; ::Dwarf_Word _m_size; public: struct hasher : public std::unary_function { size_t operator () (const source_file &v) const { size_t hash = 0; subr::hash_combine (hash, v._m_name); subr::hash_combine (hash, v._m_mtime); subr::hash_combine (hash, v._m_size); return hash; } }; source_file () : _m_name (), _m_mtime (0), _m_size (0) {} source_file (const std::string &n, ::Dwarf_Word m = 0, ::Dwarf_Word s = 0) : _m_name (n), _m_mtime (m), _m_size (s) {} source_file (const char *n, ::Dwarf_Word m = 0, ::Dwarf_Word s = 0) : _m_name (n), _m_mtime (m), _m_size (s) {} template source_file (const file &other) : _m_name (other.name ()), _m_mtime (other.mtime ()), _m_size (other.size ()) {} template inline source_file &operator= (const file &other) { _m_name = other.name (); _m_mtime = other.mtime (); _m_size = other.size (); return *this; } inline source_file &operator= (const std::string &n) { _m_name = n; _m_mtime = 0; _m_size = 0; return *this; } inline source_file &operator= (const char *n) { _m_name = n; _m_mtime = 0; _m_size = 0; return *this; } std::string to_string () const; inline std::string &name () { return _m_name; } inline const std::string &name () const { return _m_name; } inline ::Dwarf_Word &mtime () { return _m_mtime; } inline ::Dwarf_Word mtime () const { return _m_mtime; } inline ::Dwarf_Word &size () { return _m_size; } inline ::Dwarf_Word size () const { return _m_size; } template bool operator== (const other_file &other) const { if (mtime () != 0) { ::Dwarf_Word other_mtime = other.mtime (); if (other_mtime != 0 && other_mtime != mtime ()) return false; } if (size () != 0) { ::Dwarf_Word other_size = other.size (); if (other_size != 0 && other_size != size ()) return false; } return name () == other.name (); } template inline bool operator!= (const other_file &other) const { return !(*this == other); } }; // This describes a CU's directory table, a simple array of strings. class directory_table : public std::vector { private: typedef std::vector _base; public: struct hasher : public subr::container_hasher {}; directory_table () {} template directory_table (const table &other) : _base (other.begin (), other.end ()) {} std::string to_string () const; template inline bool operator== (const table &other) const { return size () == other.size () && subr::container_equal (*this, other); } template inline bool operator!= (const table &other) const { return !(*this == other); } }; private: /* This is the common base class for all line_entry instantiations. For some reason beyond my ken, std::bitset::reference as a return type is rejected by the compiler when used in a template class, but not a non-template class. Go figure. */ class line_entry_common { protected: unsigned int _m_line; unsigned int _m_column; enum flag_bit { flag_statement, flag_basic_block, flag_end_sequence, flag_prologue_end, flag_epilogue_begin, flag_count }; std::bitset _m_flags; public: line_entry_common () : _m_line (0), _m_column (0) {} inline unsigned int &line () { return _m_line; } inline unsigned int line () const { return _m_line; } inline unsigned int &column () { return _m_column; } inline unsigned int column () const { return _m_column; } #define _DWARF_EDIT_LE_FLAG(what) \ bool what () const \ { \ return _m_flags[flag_##what]; \ } \ std::bitset::reference what () \ { \ return _m_flags[flag_##what]; \ } _DWARF_EDIT_LE_FLAG (statement) _DWARF_EDIT_LE_FLAG (basic_block) _DWARF_EDIT_LE_FLAG (end_sequence) _DWARF_EDIT_LE_FLAG (prologue_end) _DWARF_EDIT_LE_FLAG (epilogue_begin) #undef _DWARF_EDIT_LE_FLAG }; public: /* This holds a line table entry. It's parameterized by the source_file representation. */ template class line_entry : public line_entry_common { private: ::Dwarf_Addr _m_addr; // XXX dwfl, reloc source_file _m_file; public: struct hasher : public std::unary_function { size_t operator () (const line_entry &v) const { size_t hash = 0; subr::hash_combine (hash, v._m_addr); subr::hash_combine (hash, v._m_file); subr::hash_combine (hash, v._m_line); subr::hash_combine (hash, v._m_column); return hash; } }; line_entry (::Dwarf_Addr addr) : line_entry_common (), _m_addr (addr), _m_file () {} template line_entry (const entry &other) : line_entry_common (), _m_addr (0), _m_file () { *this = other; } template line_entry &operator= (const entry &other) { _m_addr = other.address (); _m_file = other.file (); _m_line = other.line (); _m_column = other.column (); statement () = other.statement (); basic_block () = other.basic_block (); end_sequence () = other.end_sequence (); prologue_end () = other.prologue_end (); epilogue_begin () = other.epilogue_begin (); return *this; } inline ::Dwarf_Addr &address () { return _m_addr; } inline ::Dwarf_Addr address () const { return _m_addr; } inline source_file &file () { return _m_file; } inline const source_file &file () const { return _m_file; } template bool operator< (const entry &other) const { return address () < other.address (); } template bool operator> (const entry &other) const { return address () > other.address (); } template bool operator<= (const entry &other) const { return address () <= other.address (); } template bool operator>= (const entry &other) const { return address () >= other.address (); } template inline bool operator== (const entry &other) const { return (address () == other.address () && line () == other.line () && column () == other.column () && statement () == other.statement () && basic_block () == other.basic_block () && end_sequence () == other.end_sequence () && prologue_end () == other.prologue_end () && epilogue_begin () == other.epilogue_begin () && file () == other.file ()); } template inline bool operator!= (const entry &other) const { return !(*this == other); } }; /* This holds a line table. It's parameterized by the line_entry representation. */ template class line_table : public std::vector { private: typedef std::vector _base; public: typedef typename _base::size_type size_type; typedef typename _base::difference_type difference_type; typedef typename _base::value_type value_type; typedef typename _base::iterator iterator; typedef typename _base::const_iterator const_iterator; struct hasher : public subr::container_hasher {}; line_table () {} template line_table (const table &other) : _base (other.begin (), other.end ()) {} std::string to_string () const; template inline bool operator== (const table &other) const { return (_base::size () == other.size () && subr::container_equal (*this, other)); } template inline bool operator!= (const table &other) const { return !(*this == other); } // Look up by matching address. iterator find (::Dwarf_Addr); const_iterator find (::Dwarf_Addr) const; }; /* This holds the entirety of line information. It's parameterized by the directory_table and line_table representations. */ template class line_info_table : private std::pair { private: typedef std::pair _base; public: friend class subr::base_hasher; typedef subr::base_hasher hasher; inline line_info_table () : _base () {} template inline line_info_table (const table &other) : _base (other.include_directories (), other.lines ()) {} template inline line_info_table &operator= (const table &other) { this->first = directory_table (other.include_directories ()); this->second = line_table (other.lines ()); return *this; } std::string to_string () const; inline directory_table &include_directories () { return this->first; } inline const directory_table &include_directories () const { return this->first; } inline line_table &lines () { return this->second; } inline const line_table &lines () const { return this->second; } template inline bool operator== (const table &other) const { return (include_directories () == other.include_directories () && lines () == other.lines ()); } template inline bool operator!= (const table &other) const { return !(*this == other); } }; class dwarf_enum : private std::pair< ::Dwarf_Word, unsigned int> { private: typedef std::pair< ::Dwarf_Word, unsigned int> _base; inline dwarf_enum () : _base (0, 0) {} public: friend class subr::base_hasher; typedef subr::base_hasher hasher; inline dwarf_enum (unsigned int attr, unsigned int value) : _base (value, attr) {} template inline dwarf_enum (const constant &other) : _base (static_cast (other), other.which ()) {} // Return the DW_AT_* indicating which enum this value belongs to. inline unsigned int which () const { return this->second; } inline operator unsigned int () const { return this->first; } inline dwarf_enum &operator= (::Dwarf_Word value) { this->first = value; return *this; } inline dwarf_enum &operator= (const dwarf_enum& other) { if (this->second == 0) { throw std::logic_error ("dwarf_enum default constructed"); this->second = other.second; } else if (this->second != other.second) throw std::runtime_error ("cannot assign dwarf_constant () from " + dwarf::attributes::name (other.second) + "to " + dwarf::attributes::name (this->second)); this->first = other.first; return *this; } template inline dwarf_enum &operator= (const constant& other) { return *this = dwarf_enum (other.which (), other); } std::string to_string () const; const char *identifier () const; const char *name () const; template inline bool operator== (const constant &other) const { return (static_cast (*this) == static_cast (other)); } template inline bool operator!= (const constant &other) const { return !(*this == other); } }; // Same as set>. typedef dwarf::arange_list range_list; class location_attr : public std::map > { private: typedef std::map > _base; template struct nonempty : public std::unary_function { inline bool operator () (const pair &x) { return !x.second.empty (); } }; public: typedef _base::size_type size_type; typedef _base::difference_type difference_type; typedef _base::key_type key_type; typedef _base::mapped_type mapped_type; typedef _base::value_type value_type; typedef _base::iterator iterator; typedef _base::const_iterator const_iterator; struct hasher : public subr::container_hasher {}; inline location_attr () : _base () {} inline location_attr (const location_attr &other) : _base (static_cast (other)) {} template inline location_attr (const loc &other) : _base () { *this = other; } template inline location_attr &operator= (const loc &other) { clear (); if (other.empty ()) ; else if (other.is_list ()) for (typename loc::const_iterator i = other.begin (); i != other.end (); ++i) { const typename loc::mapped_type &x = (*i).second; (*this)[(*i).first] = mapped_type (x.begin (), x.end ()); } else { mapped_type v = other.location (); (*this)[key_type (0, -1)] = v; } return *this; } inline bool is_list () const { if (empty ()) return false; if (size () > 1) return true; const key_type &elt = begin ()->first; return !(elt.first == 0 && elt.second == (Dwarf_Addr) -1); } inline mapped_type &location () { if (empty ()) return (*this)[key_type (0, -1)]; value_type &v = *begin (); if (v.first.first != 0 || v.first.second != (Dwarf_Addr) -1 || size () > 1) throw std::runtime_error ("location is list, not single location"); return v.second; } inline const mapped_type &location () const { if (size () == 1) { const value_type &v = *begin (); if (v.first.first == 0 && v.first.second == (Dwarf_Addr) -1) return v.second; } throw std::runtime_error ("location is list, not single location"); } template bool operator== (const other_attr &other) const { if (empty ()) return (other.empty () || std::find_if (other.begin (), other.end (), nonempty () ) == other.end ()); if (!is_list ()) return (!other.is_list () && !other.empty () && subr::container_equal (location (), other.location ())); return other.is_list () && subr::container_equal (*this, other); } template inline bool operator!= (const other_attr &other) const { return !(*this == other); } std::string to_string () const; }; template struct value { struct value_dispatch { virtual ~value_dispatch () {} }; typedef value_dispatch value_cell_type; static const bool delete_value = alloc_values; template static inline flavor & variant (flavor *&, const value_dispatch *&) { assert (!alloc_values); throw std::logic_error ("can't happen!"); } template static inline flavor & variant (flavor *&result, value_dispatch *&value) { assert (alloc_values); if (value == NULL) { result = new flavor; value = result; return *result; } result = dynamic_cast (value); if (result == NULL) throw std::runtime_error ("wrong value type"); return *result; } template struct maker { inline explicit maker (const arg_type &) {} template static inline void make (value_dispatch *&v, flavor *&result, int /*whatattr*/, const input &x, arg_type &arg) { assert (alloc_values); v = result = new flavor (x, arg); } }; template static inline maker make (arg_type &arg) { return maker (arg); } struct value_string : public value_dispatch, public std::string { typedef subr::hash hasher; inline value_string () {} template inline value_string (const string &s, arg_type &) : std::string (s) {} template inline value_string (const string &s) : std::string (s) {} std::string to_string () const { std::string result ("\""); result += *this; result += "\""; return result; } }; struct value_identifier : public value_string { inline value_identifier () {} template inline value_identifier (const id &s, arg_type &arg) : value_string (s, arg) {} template inline value_identifier (const id &s) : value_string (s) {} }; struct value_reference : public value_dispatch { typedef typename impl::debug_info_entry::pointer value_type; value_type ref; // Default constructor: reference to nowhere, invalid. inline value_reference () : ref () {} /* This is only kosher for a pointer into the same dwarf_edit object. This is what plain assignment does. This just uses this pointer, rather than translating it from another file into this one (which requires a tracker). */ inline value_reference (const value_type &i, subr::nothing &) : ref (i) {} template inline value_reference (const iter &i, tracker &t) : ref () // Invalid until t.finish (). { t.refer (&ref, i); } }; struct value_flag : public value_dispatch { bool flag; inline value_flag (bool t = true) : flag (t) {} template inline value_flag (bool t, arg_type &) : flag (t) {} }; struct value_address : public value_dispatch { // XXX dwfl, reloc ::Dwarf_Addr addr; inline value_address (::Dwarf_Addr x = 0) : addr (x) {} template inline value_address (::Dwarf_Addr x, arg_type &) : addr (x) {} struct hasher : public std::unary_function { inline size_t operator () (const value_address &c) const { return c.addr; } }; inline operator ::Dwarf_Addr () const { return addr; } inline bool operator== (::Dwarf_Addr x) const { return addr == x; } }; struct value_rangelistptr : public value_dispatch, public range_list { inline value_rangelistptr () {} template inline value_rangelistptr (const list &other, arg_type &) : range_list (other) {} template inline value_rangelistptr (const list &other) : range_list (other) {} }; struct value_lineptr : public value_dispatch, public impl::line_info_table { inline value_lineptr () {} template inline value_lineptr (const table &other, arg_type &) : impl::line_info_table (other) {} }; struct value_constant : public value_dispatch { union { ::Dwarf_Word word; ::Dwarf_Sword sword; }; inline value_constant (::Dwarf_Word value = 0) : word (value) {} template inline value_constant (::Dwarf_Word x, arg_type &) : word (x) {} struct hasher : public std::unary_function { inline size_t operator () (const value_constant &c) const { return c.word; } }; inline operator ::Dwarf_Word () const { return word; } inline bool operator== (::Dwarf_Word x) const { return word == x; } }; struct value_constant_block : public value_dispatch, public std::vector { typedef subr::hash > hasher; inline value_constant_block () {} template inline value_constant_block (const block &b, arg_type &) : std::vector (b.begin (), b.end ()) {} template inline value_constant_block (const block &b) : std::vector (b.begin (), b.end ()) {} }; struct value_dwarf_constant : public value_dispatch, public dwarf_enum { inline value_dwarf_constant () {} template inline value_dwarf_constant (const constant &other) : dwarf_enum (other) {} template inline value_dwarf_constant (const constant &other, arg_type &) : dwarf_enum (other) {} }; struct value_source_file : public value_dispatch, public source_file { inline value_source_file () {} template inline value_source_file (const file &other, arg_type &) : source_file (other) {} template inline value_source_file (const file &other) : source_file (other) {} }; struct value_source_line : public value_dispatch { unsigned int n; inline value_source_line (unsigned int x = 0) : n (x) {} template inline value_source_line (unsigned int m, arg_type &) : n (m) {} struct hasher : public std::unary_function { inline size_t operator () (const value_source_line &c) const { return c.n; } }; inline operator unsigned int () const { return n; } inline bool operator== (unsigned int x) const { return n == x; } }; class value_source_column : public value_source_line { }; struct value_macptr : public value_dispatch {}; struct value_location : public value_dispatch, public location_attr { inline value_location () {} template inline value_location (const loc &other, arg_type &) : location_attr (other) {} template inline value_location (const loc &other) : location_attr (other) {} }; }; // Forward decl. template > class attributes_type; template > class attr_value { friend class attributes_type; friend class value; protected: typename vw::value_cell_type *_m_value; typedef typename impl::debug_info_entry::pointer die_ptr; template struct init { inline init (attr_value *av, int whatattr, const value &other, arg_type &arg) { do_init (av, whatattr, other, arg); } inline init (attr_value *av, int whatattr, const value &other) { arg_type dummy; do_init (av, whatattr, other, dummy); } static inline void do_init (attr_value *av, int whatattr, const value &other, arg_type &arg) { switch (other.what_space ()) { #define _DWARF_DATA_AV_MAKE(flavor, fetch) \ case dwarf::VS_##flavor: \ { \ typename vw::value_##flavor *p; \ vw::make (arg) \ .make (av->_m_value, p, whatattr, other.fetch (), arg); \ } \ break _DWARF_DATA_AV_MAKE (identifier, identifier); _DWARF_DATA_AV_MAKE (string, string); _DWARF_DATA_AV_MAKE (flag, flag); _DWARF_DATA_AV_MAKE (rangelistptr, ranges); _DWARF_DATA_AV_MAKE (lineptr, line_info); _DWARF_DATA_AV_MAKE (address, address); _DWARF_DATA_AV_MAKE (source_line, source_line); _DWARF_DATA_AV_MAKE (source_file, source_file); _DWARF_DATA_AV_MAKE (dwarf_constant, dwarf_constant); _DWARF_DATA_AV_MAKE (reference, reference); _DWARF_DATA_AV_MAKE (location, location); //_DWARF_DATA_AV_MAKE (macptr, macros); XXX case dwarf::VS_constant: if (other.constant_is_integer ()) { typename vw::value_constant *p; vw::make (arg).make (av->_m_value, p, whatattr, other.constant (), arg); } else { typename vw::value_constant_block *p; vw::make (arg).make (av->_m_value, p, whatattr, other.constant_block (), arg); } break; default: case dwarf::VS_discr_list: throw std::runtime_error ("XXX unimplemented"); #undef _DWARF_DATA_AV_MAKE } } }; // This is kosher only when freshly default-constructed. template inline void set (int whatattr, const value &other, arg_type &arg) { assert (_m_value == NULL); init me (this, whatattr, other, arg); } template inline flavor &const_variant () const { flavor *p = dynamic_cast (_m_value); if (p == NULL) throw std::runtime_error (_m_value != NULL ? "wrong value type" : "uninitialized attr_value (const?)"); return *p; } template inline const flavor &variant () const { return const_variant (); } template inline flavor &variant () { flavor *p; return vw::variant (p, _m_value); } template inline attr_value © (const value &other) { if (_m_value != NULL) { delete _m_value; _m_value = NULL; } init me (this, 0, other); return *this; } public: attr_value (const attr_value &other) : _m_value (NULL) { if (other._m_value != NULL) init me (this, 0, other); } inline attr_value () : _m_value (NULL) {} ~attr_value () { if (vw::delete_value && _m_value != NULL) delete _m_value; } template inline attr_value &operator= (const value &other) { return copy (other); } /* This is the same as the generic template one, but we have to define it explicitly to override the default assignment operator. */ inline attr_value &operator= (const attr_value &other) { return copy (other); } dwarf::value_space what_space () const; inline std::string to_string () const; inline const bool &flag () const { return variant ().flag; } inline bool &flag () { return variant ().flag; } // XXX dwfl, reloc inline const ::Dwarf_Addr &address () const { return variant ().addr; } // XXX dwfl, reloc inline ::Dwarf_Addr &address () { return variant ().addr; } inline const die_ptr &reference () const { return variant ().ref; } inline die_ptr &reference () { return variant ().ref; } inline const location_attr &location () const { return static_cast (variant ()); } inline location_attr &location () { return static_cast (variant ()); } inline const std::string &string () const { if (dynamic_cast (_m_value)) return source_file ().name (); return static_cast (variant ()); } inline std::string &string () { if (dynamic_cast (_m_value)) return source_file ().name (); return static_cast (variant ()); } inline const std::string &identifier () const { return static_cast (variant ()); } inline std::string &identifier () { return static_cast (variant ()); } inline const typename impl::source_file &source_file () const { return static_cast (variant ()); } inline typename impl::source_file &source_file () { return static_cast (variant ()); } inline const unsigned int &source_line () const { return variant ().n; } inline unsigned int &source_line () { return variant ().n; } inline const unsigned int &source_column () const { return variant ().n; } inline unsigned int &source_column () { return variant ().n; } inline const ::Dwarf_Word &constant () const { return variant ().word; } inline ::Dwarf_Word &constant () { return variant ().word; } inline const ::Dwarf_Sword &signed_constant () const { return variant ().sword; } inline ::Dwarf_Sword &signed_constant () { return variant ().sword; } inline const std::vector &constant_block () const { return static_cast &> (variant ()); } inline std::vector &constant_block () { return static_cast &> (variant ()); } inline const typename impl::dwarf_enum &dwarf_constant () const { return variant (); } inline typename impl::dwarf_enum &dwarf_constant () { return variant (); } inline bool constant_is_integer () const { return (dynamic_cast (_m_value) != NULL); } inline const typename impl::range_list &ranges () const { return static_cast (variant ()); } inline typename impl::range_list &ranges () { return static_cast (variant ()); } inline const typename impl::line_info_table &line_info () const { return static_cast (variant ()); } inline typename impl::line_info_table &line_info () { return static_cast (variant ()); } // macptr template inline bool operator== (const value &other) const { const dwarf::value_space what = what_space (); if (likely (other.what_space () == what)) switch (what) { case dwarf::VS_identifier: return identifier () == other.identifier (); case dwarf::VS_string: return string () == other.string (); case dwarf::VS_reference: return reference () == other.reference (); case dwarf::VS_flag: return flag () == other.flag (); case dwarf::VS_rangelistptr: return ranges () == other.ranges (); case dwarf::VS_lineptr: return line_info () == other.line_info (); case dwarf::VS_constant: if (constant_is_integer ()) return (other.constant_is_integer () && constant () == other.constant ()); return (!other.constant_is_integer () && constant_block () == other.constant_block ()); case dwarf::VS_source_file: return source_file () == other.source_file (); case dwarf::VS_source_line: return source_line () == other.source_line (); case dwarf::VS_source_column: return source_column () == other.source_column (); case dwarf::VS_address: return address () == other.address (); case dwarf::VS_location: return location () == other.location (); case dwarf::VS_dwarf_constant: return dwarf_constant () == other.dwarf_constant (); #if 0 case dwarf::VS_macptr: return macptr () == other.macptr (); #endif default: case dwarf::VS_discr_list: throw std::runtime_error ("XXX unimplemented"); } return false; } template inline bool operator!= (const value &other) const { return !(*this == other); } }; template class attributes_type : public std::map { friend class impl::me; protected: typedef std::map base_type; inline attributes_type () {} template inline attributes_type (const iter &from, const iter &to) : base_type (from, to) {} template inline void set (const input &other, arg_type &c) { for (typename input::const_iterator attr = other.begin (); attr != other.end (); ++attr) (*this)[(*attr).first].set ((*attr).first, (*attr).second, c); } public: // XXX should be protected /* We don't use the base_type (begin, end) iterator constructor here for good reason. The ref-maker needs to collect back-pointers into our mapped_value (attr_value) objects. It would not fly to have that done in a temporary attr_value object that gets copied into the map cell by assignment. We must make sure that when a value_reference is constructed, it is really the one sitting in our map that the ref-maker will want to update later. */ template inline attributes_type (const input &other, arg_type &c) : base_type () { set (other, c); } public: typedef typename base_type::key_type key_type; typedef typename base_type::value_type value_type; typedef typename base_type::mapped_type mapped_type; static inline bool ordered () { return true; } template inline operator attrs () const { return attrs (base_type::begin (), base_type::end ()); } }; }; // Explicit specializations. template<> std::string to_string (const dwarf_data::dwarf_enum&); inline std::string dwarf_data::dwarf_enum::to_string () const { return elfutils::to_string (*this); // Use that. } template inline std::string dwarf_data::attr_value::to_string () const { return elfutils::to_string (*this); // Use that. } }; #endif