Demangle.h 4.8 KB

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