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