1 //===--- Demangle.h ---------------------------------------------*- C++ -*-===// 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 LLVM_DEMANGLE_DEMANGLE_H 10 #define LLVM_DEMANGLE_DEMANGLE_H 11 12 #include <cstddef> 13 #include <optional> 14 #include <string> 15 #include <string_view> 16 17 namespace llvm { 18 /// This is a llvm local version of __cxa_demangle. Other than the name and 19 /// being in the llvm namespace it is identical. 20 /// 21 /// The mangled_name is demangled into buf and returned. If the buffer is not 22 /// large enough, realloc is used to expand it. 23 /// 24 /// The *status will be set to a value from the following enumeration 25 enum : int { 26 demangle_unknown_error = -4, 27 demangle_invalid_args = -3, 28 demangle_invalid_mangled_name = -2, 29 demangle_memory_alloc_failure = -1, 30 demangle_success = 0, 31 }; 32 33 /// Returns a non-NULL pointer to a NUL-terminated C style string 34 /// that should be explicitly freed, if successful. Otherwise, may return 35 /// nullptr if mangled_name is not a valid mangling or is nullptr. 36 char *itaniumDemangle(std::string_view mangled_name, bool ParseParams = true); 37 38 enum MSDemangleFlags { 39 MSDF_None = 0, 40 MSDF_DumpBackrefs = 1 << 0, 41 MSDF_NoAccessSpecifier = 1 << 1, 42 MSDF_NoCallingConvention = 1 << 2, 43 MSDF_NoReturnType = 1 << 3, 44 MSDF_NoMemberType = 1 << 4, 45 MSDF_NoVariableType = 1 << 5, 46 }; 47 48 /// Demangles the Microsoft symbol pointed at by mangled_name and returns it. 49 /// Returns a pointer to the start of a null-terminated demangled string on 50 /// success, or nullptr on error. 51 /// If n_read is non-null and demangling was successful, it receives how many 52 /// bytes of the input string were consumed. 53 /// status receives one of the demangle_ enum entries above if it's not nullptr. 54 /// Flags controls various details of the demangled representation. 55 char *microsoftDemangle(std::string_view mangled_name, size_t *n_read, 56 int *status, MSDemangleFlags Flags = MSDF_None); 57 58 std::optional<size_t> 59 getArm64ECInsertionPointInMangledName(std::string_view MangledName); 60 61 // Demangles a Rust v0 mangled symbol. 62 char *rustDemangle(std::string_view MangledName); 63 64 // Demangles a D mangled symbol. 65 char *dlangDemangle(std::string_view MangledName); 66 67 /// Attempt to demangle a string using different demangling schemes. 68 /// The function uses heuristics to determine which demangling scheme to use. 69 /// \param MangledName - reference to string to demangle. 70 /// \returns - the demangled string, or a copy of the input string if no 71 /// demangling occurred. 72 std::string demangle(std::string_view MangledName); 73 74 bool nonMicrosoftDemangle(std::string_view MangledName, std::string &Result, 75 bool CanHaveLeadingDot = true, 76 bool ParseParams = true); 77 78 /// "Partial" demangler. This supports demangling a string into an AST 79 /// (typically an intermediate stage in itaniumDemangle) and querying certain 80 /// properties or partially printing the demangled name. 81 struct ItaniumPartialDemangler { 82 ItaniumPartialDemangler(); 83 84 ItaniumPartialDemangler(ItaniumPartialDemangler &&Other); 85 ItaniumPartialDemangler &operator=(ItaniumPartialDemangler &&Other); 86 87 /// Demangle into an AST. Subsequent calls to the rest of the member functions 88 /// implicitly operate on the AST this produces. 89 /// \return true on error, false otherwise 90 bool partialDemangle(const char *MangledName); 91 92 /// Just print the entire mangled name into Buf. Buf and N behave like the 93 /// second and third parameters to __cxa_demangle. 94 char *finishDemangle(char *Buf, size_t *N) const; 95 96 /// Get the base name of a function. This doesn't include trailing template 97 /// arguments, ie for "a::b<int>" this function returns "b". 98 char *getFunctionBaseName(char *Buf, size_t *N) const; 99 100 /// Get the context name for a function. For "a::b::c", this function returns 101 /// "a::b". 102 char *getFunctionDeclContextName(char *Buf, size_t *N) const; 103 104 /// Get the entire name of this function. 105 char *getFunctionName(char *Buf, size_t *N) const; 106 107 /// Get the parameters for this function. 108 char *getFunctionParameters(char *Buf, size_t *N) const; 109 char *getFunctionReturnType(char *Buf, size_t *N) const; 110 111 /// If this function has any cv or reference qualifiers. These imply that 112 /// the function is a non-static member function. 113 bool hasFunctionQualifiers() const; 114 115 /// If this symbol describes a constructor or destructor. 116 bool isCtorOrDtor() const; 117 118 /// If this symbol describes a function. 119 bool isFunction() const; 120 121 /// If this symbol describes a variable. 122 bool isData() const; 123 124 /// If this symbol is a <special-name>. These are generally implicitly 125 /// generated by the implementation, such as vtables and typeinfo names. 126 bool isSpecialName() const; 127 128 ~ItaniumPartialDemangler(); 129 130 private: 131 void *RootNode; 132 void *Context; 133 }; 134 } // namespace llvm 135 136 #endif 137