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