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 (lldb::LanguageType language) const; 186 187 //---------------------------------------------------------------------- 188 /// Display demangled name get accessor. 189 /// 190 /// @return 191 /// A const reference to the display demangled name string object. 192 //---------------------------------------------------------------------- 193 ConstString 194 GetDisplayDemangledName (lldb::LanguageType language) const; 195 196 void SetDemangledName(const ConstString & name)197 SetDemangledName (const ConstString &name) 198 { 199 m_demangled = name; 200 } 201 202 void SetMangledName(const ConstString & name)203 SetMangledName (const ConstString &name) 204 { 205 m_mangled = name; 206 } 207 208 //---------------------------------------------------------------------- 209 /// Mangled name get accessor. 210 /// 211 /// @return 212 /// A reference to the mangled name string object. 213 //---------------------------------------------------------------------- 214 ConstString& GetMangledName()215 GetMangledName () 216 { 217 return m_mangled; 218 } 219 220 //---------------------------------------------------------------------- 221 /// Mangled name get accessor. 222 /// 223 /// @return 224 /// A const reference to the mangled name string object. 225 //---------------------------------------------------------------------- 226 const ConstString& GetMangledName()227 GetMangledName () const 228 { 229 return m_mangled; 230 } 231 232 //---------------------------------------------------------------------- 233 /// Best name get accessor. 234 /// 235 /// @param[in] preference 236 /// Which name would you prefer to get? 237 /// 238 /// @return 239 /// A const reference to the preferred name string object if this 240 /// object has a valid name of that kind, else a const reference to the 241 /// other name is returned. 242 //---------------------------------------------------------------------- 243 ConstString 244 GetName (lldb::LanguageType language, NamePreference preference = ePreferDemangled) const; 245 246 //---------------------------------------------------------------------- 247 /// Check if "name" matches either the mangled or demangled name. 248 /// 249 /// @param[in] name 250 /// A name to match against both strings. 251 /// 252 /// @return 253 /// \b True if \a name matches either name, \b false otherwise. 254 //---------------------------------------------------------------------- 255 bool NameMatches(const ConstString & name,lldb::LanguageType language)256 NameMatches (const ConstString &name, lldb::LanguageType language) const 257 { 258 if (m_mangled == name) 259 return true; 260 return GetDemangledName (language) == name; 261 } 262 263 bool 264 NameMatches (const RegularExpression& regex, lldb::LanguageType language) const; 265 266 //---------------------------------------------------------------------- 267 /// Get the memory cost of this object. 268 /// 269 /// Return the size in bytes that this object takes in memory. This 270 /// returns the size in bytes of this object, not any shared string 271 /// values it may refer to. 272 /// 273 /// @return 274 /// The number of bytes that this object occupies in memory. 275 /// 276 /// @see ConstString::StaticMemorySize () 277 //---------------------------------------------------------------------- 278 size_t 279 MemorySize () const; 280 281 //---------------------------------------------------------------------- 282 /// Set the string value in this object. 283 /// 284 /// If \a is_mangled is \b true, then the mangled named is set to \a 285 /// name, else the demangled name is set to \a name. 286 /// 287 /// @param[in] name 288 /// The already const version of the name for this object. 289 /// 290 /// @param[in] is_mangled 291 /// If \b true then \a name is a mangled name, if \b false then 292 /// \a name is demangled. 293 //---------------------------------------------------------------------- 294 void 295 SetValue (const ConstString &name, bool is_mangled); 296 297 //---------------------------------------------------------------------- 298 /// Set the string value in this object. 299 /// 300 /// This version auto detects if the string is mangled by inspecting the 301 /// string value and looking for common mangling prefixes. 302 /// 303 /// @param[in] name 304 /// The already const version of the name for this object. 305 //---------------------------------------------------------------------- 306 void 307 SetValue (const ConstString &name); 308 309 //---------------------------------------------------------------------- 310 /// Try to guess the language from the mangling. 311 /// 312 /// For a mangled name to have a language it must have both a mangled 313 /// and a demangled name and it can be guessed from the mangling what 314 /// the language is. Note: this will return C++ for any language that 315 /// uses Itanium ABI mangling. 316 /// 317 /// Standard C function names will return eLanguageTypeUnknown because 318 /// they aren't mangled and it isn't clear what language the name 319 /// represents (there will be no mangled name). 320 /// 321 /// @return 322 /// The language for the mangled/demangled name, eLanguageTypeUnknown 323 /// if there is no mangled or demangled counterpart. 324 //---------------------------------------------------------------------- 325 lldb::LanguageType 326 GuessLanguage () const; 327 328 private: 329 //---------------------------------------------------------------------- 330 /// Mangled member variables. 331 //---------------------------------------------------------------------- 332 ConstString m_mangled; ///< The mangled version of the name 333 mutable ConstString m_demangled; ///< Mutable so we can get it on demand with a const version of this object 334 }; 335 336 337 Stream& operator << (Stream& s, const Mangled& obj); 338 339 } // namespace lldb_private 340 341 #endif // #if defined(__cplusplus) 342 #endif // liblldb_Mangled_h_ 343