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