]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/lldb/Core/Mangled.h
Import LLDB as of upstream SVN r225923 (git 2b588ecd)
[FreeBSD/FreeBSD.git] / include / lldb / Core / Mangled.h
1 //===-- Mangled.h -----------------------------------------------*- 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 #ifndef liblldb_Mangled_h_
11 #define liblldb_Mangled_h_
12 #if defined(__cplusplus)
13
14
15 #include "lldb/lldb-private.h"
16 #include "lldb/Core/ConstString.h"
17 #include <vector>
18
19 namespace lldb_private {
20
21 //----------------------------------------------------------------------
22 /// @class Mangled Mangled.h "lldb/Core/Mangled.h"
23 /// @brief A class that handles mangled names.
24 ///
25 /// Designed to handle mangled names. The demangled version of any names
26 /// will be computed when the demangled name is accessed through the
27 /// Demangled() acccessor. This class can also tokenize the demangled
28 /// version of the name for powerful searches. Functions and symbols
29 /// could make instances of this class for their mangled names. Uniqued
30 /// string pools are used for the mangled, demangled, and token string
31 /// values to allow for faster comparisons and for efficient memory use.
32 //----------------------------------------------------------------------
33 class Mangled
34 {
35 public:
36     
37     enum NamePreference
38     {
39         ePreferMangled,
40         ePreferDemangled,
41         ePreferDemangledWithoutArguments
42     };
43
44     //----------------------------------------------------------------------
45     /// Default constructor.
46     ///
47     /// Initialize with both mangled and demangled names empty.
48     //----------------------------------------------------------------------
49     Mangled ();
50
51     //----------------------------------------------------------------------
52     /// Construct with name.
53     ///
54     /// Constructor with an optional string and a boolean indicating if it is
55     /// the mangled version.
56     ///
57     /// @param[in] name
58     ///     The already const name to copy into this object.
59     ///
60     /// @param[in] is_mangled
61     ///     If \b true then \a name is a mangled name, if \b false then
62     ///     \a name is demangled.
63     //----------------------------------------------------------------------
64     explicit
65     Mangled (const ConstString &name, bool is_mangled);
66
67     //----------------------------------------------------------------------
68     /// Construct with name.
69     ///
70     /// Constructor with an optional string and auto-detect if \a name is
71     /// mangled or not.
72     ///
73     /// @param[in] name
74     ///     The already const name to copy into this object.
75     //----------------------------------------------------------------------
76     explicit
77     Mangled (const ConstString &name);
78
79     //----------------------------------------------------------------------
80     /// Destructor
81     ///
82     /// Releases its ref counts on the mangled and demangled strings that
83     /// live in the global string pool.
84     //----------------------------------------------------------------------
85     ~Mangled ();
86
87     //----------------------------------------------------------------------
88     /// Convert to pointer operator.
89     ///
90     /// This allows code to check a Mangled object to see if it contains
91     /// a valid mangled name using code such as:
92     ///
93     /// @code
94     /// Mangled mangled(...);
95     /// if (mangled)
96     /// { ...
97     /// @endcode
98     ///
99     /// @return
100     ///     A pointer to this object if either the mangled or unmangled
101     ///     name is set, NULL otherwise.
102     //----------------------------------------------------------------------
103     operator
104     void*() const;
105
106     //----------------------------------------------------------------------
107     /// Logical NOT operator.
108     ///
109     /// This allows code to check a Mangled object to see if it contains
110     /// an empty mangled name using code such as:
111     ///
112     /// @code
113     /// Mangled mangled(...);
114     /// if (!mangled)
115     /// { ...
116     /// @endcode
117     ///
118     /// @return
119     ///     Returns \b true if the object has an empty mangled and
120     ///     unmangled name, \b false otherwise.
121     //----------------------------------------------------------------------
122     bool
123     operator!() const;
124
125     //----------------------------------------------------------------------
126     /// Clear the mangled and demangled values.
127     //----------------------------------------------------------------------
128     void
129     Clear ();
130
131     //----------------------------------------------------------------------
132     /// Compare the mangled string values
133     ///
134     /// Compares the Mangled::GetName() string in \a lhs and \a rhs.
135     ///
136     /// @param[in] lhs
137     ///     A const reference to the Left Hand Side object to compare.
138     ///
139     /// @param[in] rhs
140     ///     A const reference to the Right Hand Side object to compare.
141     ///
142     /// @return
143     ///     @li -1 if \a lhs is less than \a rhs
144     ///     @li 0 if \a lhs is equal to \a rhs
145     ///     @li 1 if \a lhs is greater than \a rhs
146     //----------------------------------------------------------------------
147     static int
148     Compare (const Mangled& lhs, const Mangled& rhs);
149
150     //----------------------------------------------------------------------
151     /// Dump a description of this object to a Stream \a s.
152     ///
153     /// Dump a Mangled object to stream \a s. We don't force our
154     /// demangled name to be computed currently (we don't use the accessor).
155     ///
156     /// @param[in] s
157     ///     The stream to which to dump the object description.
158     //----------------------------------------------------------------------
159     void
160     Dump (Stream *s) const;
161
162     //----------------------------------------------------------------------
163     /// Dump a debug description of this object to a Stream \a s.
164     ///
165     /// @param[in] s
166     ///     The stream to which to dump the object description.
167     //----------------------------------------------------------------------
168     void
169     DumpDebug (Stream *s) const;
170
171     //----------------------------------------------------------------------
172     /// Demangled name get accessor.
173     ///
174     /// @return
175     ///     A const reference to the demangled name string object.
176     //----------------------------------------------------------------------
177     const ConstString&
178     GetDemangledName () const;
179
180     void
181     SetDemangledName (const ConstString &name)
182     {
183         m_demangled = name;
184     }
185
186     void
187     SetMangledName (const ConstString &name)
188     {
189         m_mangled = name;
190     }
191
192     //----------------------------------------------------------------------
193     /// Mangled name get accessor.
194     ///
195     /// @return
196     ///     A reference to the mangled name string object.
197     //----------------------------------------------------------------------
198     ConstString&
199     GetMangledName ()
200     {
201         return m_mangled;
202     }
203
204     //----------------------------------------------------------------------
205     /// Mangled name get accessor.
206     ///
207     /// @return
208     ///     A const reference to the mangled name string object.
209     //----------------------------------------------------------------------
210     const ConstString&
211     GetMangledName () const
212     {
213         return m_mangled;
214     }
215
216     //----------------------------------------------------------------------
217     /// Best name get accessor.
218     ///
219     /// @param[in] preference
220     ///     Which name would you prefer to get?
221     ///
222     /// @return
223     ///     A const reference to the preferred name string object if this
224     ///     object has a valid name of that kind, else a const reference to the
225     ///     other name is returned.
226     //----------------------------------------------------------------------
227     const ConstString&
228     GetName (NamePreference preference = ePreferDemangled) const;
229
230     //----------------------------------------------------------------------
231     /// Check if "name" matches either the mangled or demangled name.
232     ///
233     /// @param[in] name
234     ///     A name to match against both strings.
235     ///
236     /// @return
237     ///     \b True if \a name matches either name, \b false otherwise.
238     //----------------------------------------------------------------------
239     bool
240     NameMatches (const ConstString &name) const
241     {
242         if (m_mangled == name)
243             return true;
244         return GetDemangledName () == name;
245     }
246     
247     bool
248     NameMatches (const RegularExpression& regex) const;
249
250     //----------------------------------------------------------------------
251     /// Get the memory cost of this object.
252     ///
253     /// Return the size in bytes that this object takes in memory. This
254     /// returns the size in bytes of this object, not any shared string
255     /// values it may refer to.
256     ///
257     /// @return
258     ///     The number of bytes that this object occupies in memory.
259     ///
260     /// @see ConstString::StaticMemorySize ()
261     //----------------------------------------------------------------------
262     size_t
263     MemorySize () const;
264
265     //----------------------------------------------------------------------
266     /// Set the string value in this object.
267     ///
268     /// If \a is_mangled is \b true, then the mangled named is set to \a
269     /// name, else the demangled name is set to \a name.
270     ///
271     /// @param[in] name
272     ///     The already const version of the name for this object.
273     ///
274     /// @param[in] is_mangled
275     ///     If \b true then \a name is a mangled name, if \b false then
276     ///     \a name is demangled.
277     //----------------------------------------------------------------------
278     void
279     SetValue (const ConstString &name, bool is_mangled);
280
281     //----------------------------------------------------------------------
282     /// Set the string value in this object.
283     ///
284     /// This version auto detects if the string is mangled by inspecting the
285     /// string value and looking for common mangling prefixes.
286     ///
287     /// @param[in] name
288     ///     The already const version of the name for this object.
289     //----------------------------------------------------------------------
290     void
291     SetValue (const ConstString &name);
292
293 private:
294     //----------------------------------------------------------------------
295     /// Mangled member variables.
296     //----------------------------------------------------------------------
297             ConstString m_mangled;      ///< The mangled version of the name
298     mutable ConstString m_demangled;    ///< Mutable so we can get it on demand with a const version of this object
299 };
300
301
302 Stream& operator << (Stream& s, const Mangled& obj);
303
304 } // namespace lldb_private
305
306 #endif  // #if defined(__cplusplus)
307 #endif  // liblldb_Mangled_h_