]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Core/Mangled.h
Update llvm/clang to r242221.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / 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     enum ManglingScheme
45     {
46         eManglingSchemeNone = 0,
47         eManglingSchemeMSVC,
48         eManglingSchemeItanium
49     };
50
51     //----------------------------------------------------------------------
52     /// Default constructor.
53     ///
54     /// Initialize with both mangled and demangled names empty.
55     //----------------------------------------------------------------------
56     Mangled ();
57
58     //----------------------------------------------------------------------
59     /// Construct with name.
60     ///
61     /// Constructor with an optional string and a boolean indicating if it is
62     /// the mangled version.
63     ///
64     /// @param[in] name
65     ///     The already const name to copy into this object.
66     ///
67     /// @param[in] is_mangled
68     ///     If \b true then \a name is a mangled name, if \b false then
69     ///     \a name is demangled.
70     //----------------------------------------------------------------------
71     explicit
72     Mangled (const ConstString &name, bool is_mangled);
73
74     //----------------------------------------------------------------------
75     /// Construct with name.
76     ///
77     /// Constructor with an optional string and auto-detect if \a name is
78     /// mangled or not.
79     ///
80     /// @param[in] name
81     ///     The already const name to copy into this object.
82     //----------------------------------------------------------------------
83     explicit
84     Mangled (const ConstString &name);
85
86     //----------------------------------------------------------------------
87     /// Destructor
88     ///
89     /// Releases its ref counts on the mangled and demangled strings that
90     /// live in the global string pool.
91     //----------------------------------------------------------------------
92     ~Mangled ();
93
94     //----------------------------------------------------------------------
95     /// Convert to pointer operator.
96     ///
97     /// This allows code to check a Mangled object to see if it contains
98     /// a valid mangled name using code such as:
99     ///
100     /// @code
101     /// Mangled mangled(...);
102     /// if (mangled)
103     /// { ...
104     /// @endcode
105     ///
106     /// @return
107     ///     A pointer to this object if either the mangled or unmangled
108     ///     name is set, NULL otherwise.
109     //----------------------------------------------------------------------
110     operator
111     void*() const;
112
113     //----------------------------------------------------------------------
114     /// Logical NOT operator.
115     ///
116     /// This allows code to check a Mangled object to see if it contains
117     /// an empty mangled name using code such as:
118     ///
119     /// @code
120     /// Mangled mangled(...);
121     /// if (!mangled)
122     /// { ...
123     /// @endcode
124     ///
125     /// @return
126     ///     Returns \b true if the object has an empty mangled and
127     ///     unmangled name, \b false otherwise.
128     //----------------------------------------------------------------------
129     bool
130     operator!() const;
131
132     //----------------------------------------------------------------------
133     /// Clear the mangled and demangled values.
134     //----------------------------------------------------------------------
135     void
136     Clear ();
137
138     //----------------------------------------------------------------------
139     /// Compare the mangled string values
140     ///
141     /// Compares the Mangled::GetName() string in \a lhs and \a rhs.
142     ///
143     /// @param[in] lhs
144     ///     A const reference to the Left Hand Side object to compare.
145     ///
146     /// @param[in] rhs
147     ///     A const reference to the Right Hand Side object to compare.
148     ///
149     /// @return
150     ///     @li -1 if \a lhs is less than \a rhs
151     ///     @li 0 if \a lhs is equal to \a rhs
152     ///     @li 1 if \a lhs is greater than \a rhs
153     //----------------------------------------------------------------------
154     static int
155     Compare (const Mangled& lhs, const Mangled& rhs);
156
157     //----------------------------------------------------------------------
158     /// Dump a description of this object to a Stream \a s.
159     ///
160     /// Dump a Mangled object to stream \a s. We don't force our
161     /// demangled name to be computed currently (we don't use the accessor).
162     ///
163     /// @param[in] s
164     ///     The stream to which to dump the object description.
165     //----------------------------------------------------------------------
166     void
167     Dump (Stream *s) const;
168
169     //----------------------------------------------------------------------
170     /// Dump a debug description of this object to a Stream \a s.
171     ///
172     /// @param[in] s
173     ///     The stream to which to dump the object description.
174     //----------------------------------------------------------------------
175     void
176     DumpDebug (Stream *s) const;
177
178     //----------------------------------------------------------------------
179     /// Demangled name get accessor.
180     ///
181     /// @return
182     ///     A const reference to the demangled name string object.
183     //----------------------------------------------------------------------
184     const ConstString&
185     GetDemangledName () const;
186
187     void
188     SetDemangledName (const ConstString &name)
189     {
190         m_demangled = name;
191     }
192
193     void
194     SetMangledName (const ConstString &name)
195     {
196         m_mangled = name;
197     }
198
199     //----------------------------------------------------------------------
200     /// Mangled name get accessor.
201     ///
202     /// @return
203     ///     A reference to the mangled name string object.
204     //----------------------------------------------------------------------
205     ConstString&
206     GetMangledName ()
207     {
208         return m_mangled;
209     }
210
211     //----------------------------------------------------------------------
212     /// Mangled name get accessor.
213     ///
214     /// @return
215     ///     A const reference to the mangled name string object.
216     //----------------------------------------------------------------------
217     const ConstString&
218     GetMangledName () const
219     {
220         return m_mangled;
221     }
222
223     //----------------------------------------------------------------------
224     /// Best name get accessor.
225     ///
226     /// @param[in] preference
227     ///     Which name would you prefer to get?
228     ///
229     /// @return
230     ///     A const reference to the preferred name string object if this
231     ///     object has a valid name of that kind, else a const reference to the
232     ///     other name is returned.
233     //----------------------------------------------------------------------
234     const ConstString&
235     GetName (NamePreference preference = ePreferDemangled) const;
236
237     //----------------------------------------------------------------------
238     /// Check if "name" matches either the mangled or demangled name.
239     ///
240     /// @param[in] name
241     ///     A name to match against both strings.
242     ///
243     /// @return
244     ///     \b True if \a name matches either name, \b false otherwise.
245     //----------------------------------------------------------------------
246     bool
247     NameMatches (const ConstString &name) const
248     {
249         if (m_mangled == name)
250             return true;
251         return GetDemangledName () == name;
252     }
253     
254     bool
255     NameMatches (const RegularExpression& regex) const;
256
257     //----------------------------------------------------------------------
258     /// Get the memory cost of this object.
259     ///
260     /// Return the size in bytes that this object takes in memory. This
261     /// returns the size in bytes of this object, not any shared string
262     /// values it may refer to.
263     ///
264     /// @return
265     ///     The number of bytes that this object occupies in memory.
266     ///
267     /// @see ConstString::StaticMemorySize ()
268     //----------------------------------------------------------------------
269     size_t
270     MemorySize () const;
271
272     //----------------------------------------------------------------------
273     /// Set the string value in this object.
274     ///
275     /// If \a is_mangled is \b true, then the mangled named is set to \a
276     /// name, else the demangled name is set to \a name.
277     ///
278     /// @param[in] name
279     ///     The already const version of the name for this object.
280     ///
281     /// @param[in] is_mangled
282     ///     If \b true then \a name is a mangled name, if \b false then
283     ///     \a name is demangled.
284     //----------------------------------------------------------------------
285     void
286     SetValue (const ConstString &name, bool is_mangled);
287
288     //----------------------------------------------------------------------
289     /// Set the string value in this object.
290     ///
291     /// This version auto detects if the string is mangled by inspecting the
292     /// string value and looking for common mangling prefixes.
293     ///
294     /// @param[in] name
295     ///     The already const version of the name for this object.
296     //----------------------------------------------------------------------
297     void
298     SetValue (const ConstString &name);
299
300     //----------------------------------------------------------------------
301     /// Try to guess the language from the mangling.
302     ///
303     /// For a mangled name to have a language it must have both a mangled
304     /// and a demangled name and it can be guessed from the mangling what
305     /// the language is.  Note: this will return C++ for any language that
306     /// uses Itanium ABI mangling.
307     ///
308     /// Standard C function names will return eLanguageTypeUnknown because
309     /// they aren't mangled and it isn't clear what language the name
310     /// represents (there will be no mangled name).
311     ///
312     /// @return
313     ///     The language for the mangled/demangled name, eLanguageTypeUnknown
314     ///     if there is no mangled or demangled counterpart.
315     //----------------------------------------------------------------------
316     lldb::LanguageType
317     GuessLanguage () const;
318
319 private:
320     //----------------------------------------------------------------------
321     /// Mangled member variables.
322     //----------------------------------------------------------------------
323             ConstString m_mangled;      ///< The mangled version of the name
324     mutable ConstString m_demangled;    ///< Mutable so we can get it on demand with a const version of this object
325 };
326
327
328 Stream& operator << (Stream& s, const Mangled& obj);
329
330 } // namespace lldb_private
331
332 #endif  // #if defined(__cplusplus)
333 #endif  // liblldb_Mangled_h_