LLDB mainline
Mangled.h
Go to the documentation of this file.
1//===-- Mangled.h ---------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_CORE_MANGLED_H
10#define LLDB_CORE_MANGLED_H
11
15#include "lldb/lldb-forward.h"
16#include "lldb/lldb-types.h"
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/StringRef.h"
19
20#include <cstddef>
21#include <cstdint>
22#include <optional>
23
24namespace lldb_private {
25
26/// \class Mangled Mangled.h "lldb/Core/Mangled.h"
27/// A class that handles mangled names.
28///
29/// Designed to handle mangled names. The demangled version of any names will
30/// be computed when the demangled name is accessed through the Demangled()
31/// accessor. This class can also tokenize the demangled version of the name
32/// for powerful searches. Functions and symbols could make instances of this
33/// class for their mangled names. Uniqued string pools are used for the
34/// mangled, demangled, and token string values to allow for faster
35/// comparisons and for efficient memory use.
36class Mangled {
37public:
43
48
49 /// The number of possible mangling schemes, which sizes the tables that are
50 /// indexed by ManglingScheme.
51 static constexpr size_t g_num_mangling_schemes = 16;
52
53 /// What a mangled function name says about the function's declaration.
61
62 /// The parts of a mangled function name used in the symbol table's indices.
64 /// If empty, the symbol is only indexed by its mangled name.
66 /// The enclosing context. The symbol table only uses it to work
67 /// out which contexts are classes: a ConstructorOrDestructor
68 /// shows that its context is one, and an Unknown function in such
69 /// a context is a method.
72 };
73
74 /// Default constructor.
75 ///
76 /// Initialize with both mangled and demangled names empty.
77 Mangled() = default;
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 explicit Mangled(ConstString name);
87
88 explicit Mangled(llvm::StringRef name);
89
90 bool operator==(const Mangled &rhs) const {
91 return m_mangled == rhs.m_mangled &&
93 }
94
95 bool operator!=(const Mangled &rhs) const {
96 return !(*this == rhs);
97 }
98
99 /// Convert to bool operator.
100 ///
101 /// This allows code to check any Mangled objects to see if they contain
102 /// anything valid using code such as:
103 ///
104 /// \code
105 /// Mangled mangled(...);
106 /// if (mangled)
107 /// { ...
108 /// \endcode
109 ///
110 /// \return
111 /// Returns \b true if either the mangled or unmangled name is set,
112 /// \b false if the object has an empty mangled and unmangled name.
113 explicit operator bool() const;
114
115 /// Clear the mangled and demangled values.
116 void Clear();
117
118 /// Compare the mangled string values
119 ///
120 /// Compares the Mangled::GetName() string in \a lhs and \a rhs.
121 ///
122 /// \param[in] lhs
123 /// A const reference to the Left Hand Side object to compare.
124 ///
125 /// \param[in] rhs
126 /// A const reference to the Right Hand Side object to compare.
127 ///
128 /// \return
129 /// -1 if \a lhs is less than \a rhs
130 /// 0 if \a lhs is equal to \a rhs
131 /// 1 if \a lhs is greater than \a rhs
132 static int Compare(const Mangled &lhs, const Mangled &rhs);
133
134 /// Dump a description of this object to a Stream \a s.
135 ///
136 /// Dump a Mangled object to stream \a s. We don't force our demangled name
137 /// to be computed currently (we don't use the accessor).
138 ///
139 /// \param[in] s
140 /// The stream to which to dump the object description.
141 void Dump(Stream *s) const;
142
143 /// Dump a debug description of this object to a Stream \a s.
144 ///
145 /// \param[in] s
146 /// The stream to which to dump the object description.
147 void DumpDebug(Stream *s) const;
148
149 /// Demangled name get accessor.
150 ///
151 /// \return
152 /// A const reference to the demangled name string object.
154
155 /// Display demangled name get accessor.
156 ///
157 /// \return
158 /// A const reference to the display demangled name string object.
160
162
163 void SetMangledName(ConstString name) { m_mangled = name; }
164
165 /// Mangled name get accessor.
166 ///
167 /// \return
168 /// The mangled name string object.
170
171 /// Best name get accessor.
172 ///
173 /// \param[in] preference
174 /// Which name would you prefer to get?
175 ///
176 /// \return
177 /// A const reference to the preferred name string object if this
178 /// object has a valid name of that kind, else a const reference to the
179 /// other name is returned.
180 ConstString GetName(NamePreference preference = ePreferDemangled) const;
181
182 /// Check if "name" matches either the mangled or demangled name.
183 ///
184 /// \param[in] name
185 /// A name to match against both strings.
186 ///
187 /// \return
188 /// \b True if \a name matches either name, \b false otherwise.
189 bool NameMatches(ConstString name) const {
190 if (m_mangled == name)
191 return true;
192 return GetDemangledName() == name;
193 }
194 bool NameMatches(const RegularExpression &regex) const;
195
196 /// Set the string value in this object.
197 ///
198 /// This version auto detects if the string is mangled by inspecting the
199 /// string value and looking for the prefixes of the registered mangling
200 /// schemes.
201 ///
202 /// \param[in] name
203 /// The already const version of the name for this object.
204 void SetValue(ConstString name);
205
206 /// Try to guess the language from the mangling.
207 ///
208 /// For a mangled name to have a language it must have both a mangled and a
209 /// demangled name and it can be guessed from the mangling what the language
210 /// is. Note: this will return C++ for any language that uses Itanium ABI
211 /// mangling.
212 ///
213 /// Standard C function names will return eLanguageTypeUnknown because they
214 /// aren't mangled and it isn't clear what language the name represents
215 /// (there will be no mangled name).
216 ///
217 /// \return
218 /// The language for the mangled/demangled name, eLanguageTypeUnknown
219 /// if there is no mangled or demangled counterpart.
221
222 /// Try to identify the mangling scheme used.
223 /// \param[in] name
224 /// The name we are attempting to identify the mangling scheme for.
225 ///
226 /// \return
227 /// eManglingSchemeNone if no registered mangling scheme matches \p name,
228 /// otherwise the scheme that does.
229 static Mangled::ManglingScheme GetManglingScheme(llvm::StringRef name);
230
231 static bool IsMangledName(llvm::StringRef name);
232
233 using ManglingSchemeMatcher = bool (*)(llvm::StringRef name);
234
235 /// Register a mangling scheme for \p language. All mangled names in
236 /// this scheme start with one of \p prefixes. If the prefixes are
237 /// insufficient to unambiguously identify a scheme, \p matches
238 /// checks the rest of each name.
239 ///
240 /// Registering the same scheme again returns the same scheme.
241 ///
242 /// \return
243 /// The new scheme, or eManglingSchemeNone if there is no slot left.
244 static ManglingScheme
245 RegisterManglingScheme(llvm::ArrayRef<llvm::StringRef> prefixes,
246 lldb::LanguageType language,
247 ManglingSchemeMatcher matches = nullptr);
248
249 /// Stop recognizing the names of \p scheme, a scheme returned by
250 /// RegisterManglingScheme().
251 static void UnregisterManglingScheme(ManglingScheme scheme);
252
253 /// Returns the language that registered \p scheme, or eLanguageTypeUnknown
254 /// if no language did.
256
257 /// Decode a serialized version of this object from data.
258 ///
259 /// \param data
260 /// The decoder object that references the serialized data.
261 ///
262 /// \param offset_ptr
263 /// A pointer that contains the offset from which the data will be decoded
264 /// from that gets updated as data gets decoded.
265 ///
266 /// \param strtab
267 /// All strings in cache files are put into string tables for efficiency
268 /// and cache file size reduction. Strings are stored as uint32_t string
269 /// table offsets in the cache data.
270 bool Decode(const DataExtractor &data, lldb::offset_t *offset_ptr,
271 const StringTableReader &strtab);
272
273 /// Encode this object into a data encoder object.
274 ///
275 /// This allows this object to be serialized to disk.
276 ///
277 /// \param encoder
278 /// A data encoder object that serialized bytes will be encoded into.
279 ///
280 /// \param strtab
281 /// All strings in cache files are put into string tables for efficiency
282 /// and cache file size reduction. Strings are stored as uint32_t string
283 /// table offsets in the cache data.
284 void Encode(DataEncoder &encoder, ConstStringTable &strtab) const;
285
286 /// Compute the \c DemangledNameInfo of the demangled name.
287 ///
288 /// Note that this always re-runs the demangler. \see DemangledNameInfoCache
289 /// for caching the result.
290 ///
291 /// \return
292 /// std::nullopt if no info could be computed (for example because the
293 /// name is mangled with a scheme that doesn't provide any info).
294 std::optional<DemangledNameInfo> ComputeDemangledInfo() const;
295
296 /// Compute the base name (without namespace/class qualifiers) from the
297 /// demangled name.
298 ///
299 /// For a demangled name like "ns::MyClass<int>::templateFunc", this returns
300 /// just "templateFunc".
301 ///
302 /// \return
303 /// A ConstString containing the basename, or nullptr if computation
304 /// fails.
305 ConstString GetBaseName() const;
306
307private:
308 /// The mangled version of the name.
310
311 /// Mutable so we can get it on demand with
312 /// a const version of this object.
314};
315static_assert(sizeof(Mangled) <= 2 * sizeof(ConstString),
316 "High-volume object, size of object must be increased with care");
317
319
320} // namespace lldb_private
321
322#endif // LLDB_CORE_MANGLED_H
Many cache files require string tables to store data efficiently.
A uniqued constant string class.
Definition ConstString.h:40
An binary data encoding class.
Definition DataEncoder.h:42
An data extractor class.
A class that handles mangled names.
Definition Mangled.h:36
void Encode(DataEncoder &encoder, ConstStringTable &strtab) const
Encode this object into a data encoder object.
Definition Mangled.cpp:455
bool NameMatches(ConstString name) const
Check if "name" matches either the mangled or demangled name.
Definition Mangled.h:189
static int Compare(const Mangled &lhs, const Mangled &rhs)
Compare the mangled string values.
Definition Mangled.cpp:214
static ManglingScheme RegisterManglingScheme(llvm::ArrayRef< llvm::StringRef > prefixes, lldb::LanguageType language, ManglingSchemeMatcher matches=nullptr)
Register a mangling scheme for language.
Definition Mangled.cpp:163
static bool IsMangledName(llvm::StringRef name)
Definition Mangled.cpp:178
@ ePreferDemangledWithoutArguments
Definition Mangled.h:41
Mangled()=default
Default constructor.
void SetDemangledName(ConstString name)
Definition Mangled.h:161
void DumpDebug(Stream *s) const
Dump a debug description of this object to a Stream s.
Definition Mangled.cpp:330
std::optional< DemangledNameInfo > ComputeDemangledInfo() const
Compute the DemangledNameInfo of the demangled name.
Definition Mangled.cpp:268
ConstString GetMangledName() const
Mangled name get accessor.
Definition Mangled.h:169
FunctionNameKind
What a mangled function name says about the function's declaration.
Definition Mangled.h:54
ConstString GetDemangledName() const
Demangled name get accessor.
Definition Mangled.cpp:238
ConstString GetBaseName() const
Compute the base name (without namespace/class qualifiers) from the demangled name.
Definition Mangled.cpp:487
bool(*)(llvm::StringRef name) ManglingSchemeMatcher
Definition Mangled.h:233
lldb::LanguageType GuessLanguage() const
Try to guess the language from the mangling.
Definition Mangled.cpp:344
bool Decode(const DataExtractor &data, lldb::offset_t *offset_ptr, const StringTableReader &strtab)
Decode a serialized version of this object from data.
Definition Mangled.cpp:404
static constexpr size_t g_num_mangling_schemes
The number of possible mangling schemes, which sizes the tables that are indexed by ManglingScheme.
Definition Mangled.h:51
static lldb::LanguageType GetLanguageForManglingScheme(ManglingScheme scheme)
Returns the language that registered scheme, or eLanguageTypeUnknown if no language did.
Definition Mangled.cpp:174
void SetMangledName(ConstString name)
Definition Mangled.h:163
bool operator==(const Mangled &rhs) const
Definition Mangled.h:90
void SetValue(ConstString name)
Set the string value in this object.
Definition Mangled.cpp:219
ConstString GetName(NamePreference preference=ePreferDemangled) const
Best name get accessor.
Definition Mangled.cpp:297
static Mangled::ManglingScheme GetManglingScheme(llvm::StringRef name)
Try to identify the mangling scheme used.
Definition Mangled.cpp:182
bool operator!=(const Mangled &rhs) const
Definition Mangled.h:95
ConstString m_mangled
The mangled version of the name.
Definition Mangled.h:309
ConstString m_demangled
Mutable so we can get it on demand with a const version of this object.
Definition Mangled.h:313
ConstString GetDisplayDemangledName() const
Display demangled name get accessor.
Definition Mangled.cpp:282
void Dump(Stream *s) const
Dump a description of this object to a Stream s.
Definition Mangled.cpp:320
static void UnregisterManglingScheme(ManglingScheme scheme)
Stop recognizing the names of scheme, a scheme returned by RegisterManglingScheme().
Definition Mangled.cpp:169
void Clear()
Clear the mangled and demangled values.
Definition Mangled.cpp:208
A stream class that can stream formatted output to a file.
Definition Stream.h:28
Many cache files require string tables to store data efficiently.
A class that represents a running process on the host machine.
Stream & operator<<(Stream &s, const Mangled &obj)
uint64_t offset_t
Definition lldb-types.h:86
LanguageType
Programming language type.
The parts of a mangled function name used in the symbol table's indices.
Definition Mangled.h:63
ConstString basename
If empty, the symbol is only indexed by its mangled name.
Definition Mangled.h:65
ConstString decl_context
The enclosing context.
Definition Mangled.h:70