1 //===-- Mangled.h -----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_Mangled_h_
11 #define liblldb_Mangled_h_
12 #if defined(__cplusplus)
14 #include "lldb/Utility/ConstString.h"
15 #include "lldb/lldb-enumerations.h" // for LanguageType
16 #include "llvm/ADT/StringRef.h" // for StringRef
18 #include <stddef.h> // for size_t
20 namespace lldb_private {
21 class RegularExpression;
23 namespace lldb_private {
27 namespace lldb_private {
29 //----------------------------------------------------------------------
30 /// @class Mangled Mangled.h "lldb/Core/Mangled.h"
31 /// A class that handles mangled names.
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 //----------------------------------------------------------------------
46 ePreferDemangledWithoutArguments
50 eManglingSchemeNone = 0,
52 eManglingSchemeItanium
55 //----------------------------------------------------------------------
56 /// Default constructor.
58 /// Initialize with both mangled and demangled names empty.
59 //----------------------------------------------------------------------
62 //----------------------------------------------------------------------
63 /// Construct with name.
65 /// Constructor with an optional string and a boolean indicating if it is
66 /// the mangled version.
69 /// The already const name to copy into this object.
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);
78 //----------------------------------------------------------------------
79 /// Construct with name.
81 /// Constructor with an optional string and auto-detect if \a name is
85 /// The already const name to copy into this object.
86 //----------------------------------------------------------------------
87 explicit Mangled(const ConstString &name);
89 explicit Mangled(llvm::StringRef name);
91 //----------------------------------------------------------------------
94 /// Releases its ref counts on the mangled and demangled strings that live
95 /// in the global string pool.
96 //----------------------------------------------------------------------
99 //----------------------------------------------------------------------
100 /// Convert to pointer operator.
102 /// This allows code to check a Mangled object to see if it contains a valid
103 /// mangled name using code such as:
106 /// Mangled mangled(...);
112 /// A pointer to this object if either the mangled or unmangled
113 /// name is set, NULL otherwise.
114 //----------------------------------------------------------------------
115 operator void *() const;
117 //----------------------------------------------------------------------
118 /// Logical NOT operator.
120 /// This allows code to check a Mangled object to see if it contains an
121 /// empty mangled name using code such as:
124 /// Mangled mangled(...);
130 /// Returns \b true if the object has an empty mangled and
131 /// unmangled name, \b false otherwise.
132 //----------------------------------------------------------------------
133 bool operator!() const;
135 //----------------------------------------------------------------------
136 /// Clear the mangled and demangled values.
137 //----------------------------------------------------------------------
140 //----------------------------------------------------------------------
141 /// Compare the mangled string values
143 /// Compares the Mangled::GetName() string in \a lhs and \a rhs.
146 /// A const reference to the Left Hand Side object to compare.
149 /// A const reference to the Right Hand Side object to compare.
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);
158 //----------------------------------------------------------------------
159 /// Dump a description of this object to a Stream \a s.
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).
165 /// The stream to which to dump the object description.
166 //----------------------------------------------------------------------
167 void Dump(Stream *s) const;
169 //----------------------------------------------------------------------
170 /// Dump a debug description of this object to a Stream \a s.
173 /// The stream to which to dump the object description.
174 //----------------------------------------------------------------------
175 void DumpDebug(Stream *s) const;
177 //----------------------------------------------------------------------
178 /// Demangled name get accessor.
181 /// A const reference to the demangled name string object.
182 //----------------------------------------------------------------------
183 const ConstString &GetDemangledName(lldb::LanguageType language) const;
185 //----------------------------------------------------------------------
186 /// Display demangled name get accessor.
189 /// A const reference to the display demangled name string object.
190 //----------------------------------------------------------------------
191 ConstString GetDisplayDemangledName(lldb::LanguageType language) const;
193 void SetDemangledName(const ConstString &name) { m_demangled = name; }
195 void SetMangledName(const ConstString &name) { m_mangled = name; }
197 //----------------------------------------------------------------------
198 /// Mangled name get accessor.
201 /// A reference to the mangled name string object.
202 //----------------------------------------------------------------------
203 ConstString &GetMangledName() { return m_mangled; }
205 //----------------------------------------------------------------------
206 /// Mangled name get accessor.
209 /// A const reference to the mangled name string object.
210 //----------------------------------------------------------------------
211 const ConstString &GetMangledName() const { return m_mangled; }
213 //----------------------------------------------------------------------
214 /// Best name get accessor.
216 /// @param[in] preference
217 /// Which name would you prefer to get?
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;
227 //----------------------------------------------------------------------
228 /// Check if "name" matches either the mangled or demangled name.
231 /// A name to match against both strings.
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)
239 return GetDemangledName(language) == name;
242 bool NameMatches(const RegularExpression ®ex,
243 lldb::LanguageType language) const;
245 //----------------------------------------------------------------------
246 /// Get the memory cost of this object.
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
253 /// The number of bytes that this object occupies in memory.
255 /// @see ConstString::StaticMemorySize ()
256 //----------------------------------------------------------------------
257 size_t MemorySize() const;
259 //----------------------------------------------------------------------
260 /// Set the string value in this object.
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.
266 /// The already const version of the name for this object.
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);
274 //----------------------------------------------------------------------
275 /// Set the string value in this object.
277 /// This version auto detects if the string is mangled by inspecting the
278 /// string value and looking for common mangling prefixes.
281 /// The already const version of the name for this object.
282 //----------------------------------------------------------------------
283 void SetValue(const ConstString &name);
285 //----------------------------------------------------------------------
286 /// Try to guess the language from the mangling.
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
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).
298 /// The language for the mangled/demangled name, eLanguageTypeUnknown
299 /// if there is no mangled or demangled counterpart.
300 //----------------------------------------------------------------------
301 lldb::LanguageType GuessLanguage() const;
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
312 Stream &operator<<(Stream &s, const Mangled &obj);
314 } // namespace lldb_private
316 #endif // #if defined(__cplusplus)
317 #endif // liblldb_Mangled_h_