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