LLVM 24.0.0git
Demangle.h
Go to the documentation of this file.
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 "DemangleConfig.h"
13#include <cstddef>
14#include <optional>
15#include <string>
16#include <string_view>
17
18namespace llvm {
19/// This is a llvm local version of __cxa_demangle. Other than the name and
20/// being in the llvm namespace it is identical.
21///
22/// The mangled_name is demangled into buf and returned. If the buffer is not
23/// large enough, realloc is used to expand it.
24///
25/// The *status will be set to a value from the following enumeration
26enum : int {
32};
33
34/// Returns a non-NULL pointer to a NUL-terminated C style string
35/// that should be explicitly freed, if successful. Otherwise, may return
36/// nullptr if mangled_name is not a valid mangling or is nullptr.
37DEMANGLE_ABI char *itaniumDemangle(std::string_view mangled_name,
38 bool ParseParams = true);
39
44 /// Don't write the calling convention (`__cdecl`, `__stdcall`...) in function
45 /// and function pointers.
47 /// Don't write the return type for functions. That flag doesn't apply to the
48 /// return type of function pointers since it is an essential part of their
49 /// type.
54 /// Don't write "(void)" for functions that take no parameters.
56 /// Don't add decoration to RTTI type descriptors:
57 /// struct MyStruct `RTTI Type Descriptor Name'
58 /// will instead output
59 /// struct MyStruct
61};
62
63/// Demangles the Microsoft symbol pointed at by mangled_name and returns it.
64/// Returns a pointer to the start of a null-terminated demangled string on
65/// success, or nullptr on error.
66/// If n_read is non-null and demangling was successful, it receives how many
67/// bytes of the input string were consumed.
68/// status receives one of the demangle_ enum entries above if it's not nullptr.
69/// Flags controls various details of the demangled representation.
70DEMANGLE_ABI char *microsoftDemangle(std::string_view mangled_name,
71 size_t *n_read, int *status,
73
74DEMANGLE_ABI std::optional<size_t>
75getArm64ECInsertionPointInMangledName(std::string_view MangledName);
76
77// Demangles a Rust v0 mangled symbol.
78DEMANGLE_ABI char *rustDemangle(std::string_view MangledName);
79
80// Demangles a D mangled symbol.
81DEMANGLE_ABI char *dlangDemangle(std::string_view MangledName);
82
83/// Attempt to demangle a string using different demangling schemes.
84/// The function uses heuristics to determine which demangling scheme to use.
85/// \param MangledName - reference to string to demangle.
86/// \returns - the demangled string, or a copy of the input string if no
87/// demangling occurred.
88DEMANGLE_ABI std::string demangle(std::string_view MangledName);
89
90DEMANGLE_ABI bool nonMicrosoftDemangle(std::string_view MangledName,
91 std::string &Result,
92 bool CanHaveLeadingDot = true,
93 bool ParseParams = true);
94
95/// "Partial" demangler. This supports demangling a string into an AST
96/// (typically an intermediate stage in itaniumDemangle) and querying certain
97/// properties or partially printing the demangled name.
100
104
105 /// Demangle into an AST. Subsequent calls to the rest of the member functions
106 /// implicitly operate on the AST this produces.
107 /// \return true on error, false otherwise
108 DEMANGLE_ABI bool partialDemangle(const char *MangledName);
109
110 /// Just print the entire mangled name into Buf. Buf and N behave like the
111 /// second and third parameters to __cxa_demangle.
112 DEMANGLE_ABI char *finishDemangle(char *Buf, size_t *N) const;
113
114 /// See \ref finishDemangle
115 ///
116 /// \param[in] OB A llvm::itanium_demangle::OutputBuffer that the demangled
117 /// name will be printed into.
118 ///
119 DEMANGLE_ABI char *finishDemangle(void *OB) const;
120
121 /// Get the base name of a function. This doesn't include trailing template
122 /// arguments, ie for "a::b<int>" this function returns "b".
123 DEMANGLE_ABI char *getFunctionBaseName(char *Buf, size_t *N) const;
124
125 /// Get the context name for a function. For "a::b::c", this function returns
126 /// "a::b".
127 DEMANGLE_ABI char *getFunctionDeclContextName(char *Buf, size_t *N) const;
128
129 /// Get the entire name of this function.
130 DEMANGLE_ABI char *getFunctionName(char *Buf, size_t *N) const;
131
132 /// Get the parameters for this function.
133 DEMANGLE_ABI char *getFunctionParameters(char *Buf, size_t *N) const;
134 DEMANGLE_ABI char *getFunctionReturnType(char *Buf, size_t *N) const;
135
136 /// If this function has any cv or reference qualifiers. These imply that
137 /// the function is a non-static member function.
139
140 /// If this symbol describes a constructor or destructor.
141 DEMANGLE_ABI bool isCtorOrDtor() const;
142
143 /// If this symbol describes a function.
144 DEMANGLE_ABI bool isFunction() const;
145
146 /// If this symbol describes a variable.
147 DEMANGLE_ABI bool isData() const;
148
149 /// If this symbol is a <special-name>. These are generally implicitly
150 /// generated by the implementation, such as vtables and typeinfo names.
151 DEMANGLE_ABI bool isSpecialName() const;
152
154
155private:
156 void *RootNode;
157 void *Context;
158};
159} // namespace llvm
160
161#endif
#define DEMANGLE_ABI
DEMANGLE_ABI is the export/visibility macro used to mark symbols declared in llvm/Demangle as exporte...
This is an optimization pass for GlobalISel generic memory operations.
DEMANGLE_ABI bool nonMicrosoftDemangle(std::string_view MangledName, std::string &Result, bool CanHaveLeadingDot=true, bool ParseParams=true)
Definition Demangle.cpp:60
@ demangle_unknown_error
Definition Demangle.h:27
@ demangle_success
Definition Demangle.h:31
@ demangle_invalid_mangled_name
Definition Demangle.h:29
@ demangle_invalid_args
Definition Demangle.h:28
@ demangle_memory_alloc_failure
Definition Demangle.h:30
DEMANGLE_ABI std::optional< size_t > getArm64ECInsertionPointInMangledName(std::string_view MangledName)
DEMANGLE_ABI char * itaniumDemangle(std::string_view mangled_name, bool ParseParams=true)
Returns a non-NULL pointer to a NUL-terminated C style string that should be explicitly freed,...
DEMANGLE_ABI char * dlangDemangle(std::string_view MangledName)
DEMANGLE_ABI char * rustDemangle(std::string_view MangledName)
@ Other
Any other memory.
Definition ModRef.h:68
DEMANGLE_ABI char * microsoftDemangle(std::string_view mangled_name, size_t *n_read, int *status, MSDemangleFlags Flags=MSDF_None)
Demangles the Microsoft symbol pointed at by mangled_name and returns it.
MSDemangleFlags
Definition Demangle.h:40
@ MSDF_NoVoidParameter
Don't write "(void)" for functions that take no parameters.
Definition Demangle.h:55
@ MSDF_NoTagSpecifier
Definition Demangle.h:53
@ MSDF_NoReturnType
Don't write the return type for functions.
Definition Demangle.h:50
@ MSDF_NoDecorativeRTTITypeDescriptor
Don't add decoration to RTTI type descriptors: struct MyStruct ‘RTTI Type Descriptor Name’ will inste...
Definition Demangle.h:60
@ MSDF_None
Definition Demangle.h:41
@ MSDF_DumpBackrefs
Definition Demangle.h:42
@ MSDF_NoMemberType
Definition Demangle.h:51
@ MSDF_NoVariableType
Definition Demangle.h:52
@ MSDF_NoCallingConvention
Don't write the calling convention (__cdecl, __stdcall...) in function and function pointers.
Definition Demangle.h:46
@ MSDF_NoAccessSpecifier
Definition Demangle.h:43
DEMANGLE_ABI std::string demangle(std::string_view MangledName)
Attempt to demangle a string using different demangling schemes.
Definition Demangle.cpp:21
#define N
DEMANGLE_ABI char * getFunctionParameters(char *Buf, size_t *N) const
Get the parameters for this function.
DEMANGLE_ABI bool isFunction() const
If this symbol describes a function.
DEMANGLE_ABI char * getFunctionBaseName(char *Buf, size_t *N) const
Get the base name of a function.
DEMANGLE_ABI bool isSpecialName() const
If this symbol is a <special-name>.
DEMANGLE_ABI bool partialDemangle(const char *MangledName)
Demangle into an AST.
DEMANGLE_ABI char * getFunctionName(char *Buf, size_t *N) const
Get the entire name of this function.
DEMANGLE_ABI bool hasFunctionQualifiers() const
If this function has any cv or reference qualifiers.
DEMANGLE_ABI char * finishDemangle(char *Buf, size_t *N) const
Just print the entire mangled name into Buf.
DEMANGLE_ABI char * getFunctionReturnType(char *Buf, size_t *N) const
DEMANGLE_ABI ItaniumPartialDemangler & operator=(ItaniumPartialDemangler &&Other)
DEMANGLE_ABI bool isCtorOrDtor() const
If this symbol describes a constructor or destructor.
DEMANGLE_ABI char * getFunctionDeclContextName(char *Buf, size_t *N) const
Get the context name for a function.
DEMANGLE_ABI bool isData() const
If this symbol describes a variable.