Demangle.h 4.8 KB

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