OSDN Git Service

2011-02-10 Jonathan Wakely <jwakely.gcc@gmail.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / doc / html / manual / ext_demangling.html
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml"><head><title>Chapter 27. Demangling</title><meta name="generator" content="DocBook XSL-NS Stylesheets V1.75.2"/><meta name="keywords" content="&#10;      ISO C++&#10;    , &#10;      library&#10;    "/><link rel="home" href="../spine.html" title="The GNU C++ Library"/><link rel="up" href="extensions.html" title="Part III.  Extensions"/><link rel="prev" href="ext_io.html" title="Chapter 26. Input and Output"/><link rel="next" href="ext_concurrency.html" title="Chapter 28. Concurrency"/></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 27. Demangling</th></tr><tr><td align="left"><a accesskey="p" href="ext_io.html">Prev</a> </td><th width="60%" align="center">Part III. 
4   Extensions
5   
6 </th><td align="right"> <a accesskey="n" href="ext_concurrency.html">Next</a></td></tr></table><hr/></div><div class="chapter" title="Chapter 27. Demangling"><div class="titlepage"><div><div><h1 class="title"><a id="manual.ext.demangle"/>Demangling</h1></div></div></div><p>
7     Transforming C++ ABI identifiers (like RTTI symbols) into the
8     original C++ source identifiers is called
9     <span class="quote">“<span class="quote">demangling.</span>”</span>
10   </p><p>
11     If you have read the <a class="link" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a01115.html" target="">source
12     documentation for <code class="code">namespace abi</code></a> then you are
13     aware of the cross-vendor C++ ABI in use by GCC.  One of the
14     exposed functions is used for demangling,
15     <code class="code">abi::__cxa_demangle</code>.
16   </p><p>
17     In programs like <span class="command"><strong>c++filt</strong></span>, the linker, and other tools
18     have the ability to decode C++ ABI names, and now so can you.
19   </p><p>
20     (The function itself might use different demanglers, but that's the
21     whole point of abstract interfaces.  If we change the implementation,
22     you won't notice.)
23   </p><p>
24     Probably the only times you'll be interested in demangling at runtime
25     are when you're seeing <code class="code">typeid</code> strings in RTTI, or when
26     you're handling the runtime-support exception classes.  For example:
27   </p><pre class="programlisting">
28 #include &lt;exception&gt;
29 #include &lt;iostream&gt;
30 #include &lt;cxxabi.h&gt;
31
32 struct empty { };
33
34 template &lt;typename T, int N&gt;
35   struct bar { };
36
37
38 int main()
39 {
40   int     status;
41   char   *realname;
42
43   // exception classes not in &lt;stdexcept&gt;, thrown by the implementation
44   // instead of the user
45   std::bad_exception  e;
46   realname = abi::__cxa_demangle(e.what(), 0, 0, &amp;status);
47   std::cout &lt;&lt; e.what() &lt;&lt; "\t=&gt; " &lt;&lt; realname &lt;&lt; "\t: " &lt;&lt; status &lt;&lt; '\n';
48   free(realname);
49
50
51   // typeid
52   bar&lt;empty,17&gt;          u;
53   const std::type_info  &amp;ti = typeid(u);
54
55   realname = abi::__cxa_demangle(ti.name(), 0, 0, &amp;status);
56   std::cout &lt;&lt; ti.name() &lt;&lt; "\t=&gt; " &lt;&lt; realname &lt;&lt; "\t: " &lt;&lt; status &lt;&lt; '\n';
57   free(realname);
58
59   return 0;
60 }
61    </pre><p>
62      This prints
63    </p><pre class="screen">
64    <code class="computeroutput">
65       St13bad_exception       =&gt; std::bad_exception   : 0
66       3barI5emptyLi17EE       =&gt; bar&lt;empty, 17&gt;       : 0
67    </code>
68    </pre><p>
69      The demangler interface is described in the source documentation
70      linked to above.  It is actually written in C, so you don't need to
71      be writing C++ in order to demangle C++.  (That also means we have to
72      use crummy memory management facilities, so don't forget to free()
73      the returned char array.)
74    </p></div><div class="navfooter"><hr/><table width="100%" summary="Navigation footer"><tr><td align="left"><a accesskey="p" href="ext_io.html">Prev</a> </td><td align="center"><a accesskey="u" href="extensions.html">Up</a></td><td align="right"> <a accesskey="n" href="ext_concurrency.html">Next</a></td></tr><tr><td align="left" valign="top">Chapter 26. Input and Output </td><td align="center"><a accesskey="h" href="../spine.html">Home</a></td><td align="right" valign="top"> Chapter 28. Concurrency</td></tr></table></div></body></html>