MicrosoftDemangle.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===------------------------- MicrosoftDemangle.h --------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_DEMANGLE_MICROSOFT_DEMANGLE_H
  14. #define LLVM_DEMANGLE_MICROSOFT_DEMANGLE_H
  15. #include "llvm/Demangle/DemangleConfig.h"
  16. #include "llvm/Demangle/MicrosoftDemangleNodes.h"
  17. #include "llvm/Demangle/StringView.h"
  18. #include "llvm/Demangle/Utility.h"
  19. #include <utility>
  20. namespace llvm {
  21. namespace ms_demangle {
  22. // This memory allocator is extremely fast, but it doesn't call dtors
  23. // for allocated objects. That means you can't use STL containers
  24. // (such as std::vector) with this allocator. But it pays off --
  25. // the demangler is 3x faster with this allocator compared to one with
  26. // STL containers.
  27. constexpr size_t AllocUnit = 4096;
  28. class ArenaAllocator {
  29. struct AllocatorNode {
  30. uint8_t *Buf = nullptr;
  31. size_t Used = 0;
  32. size_t Capacity = 0;
  33. AllocatorNode *Next = nullptr;
  34. };
  35. void addNode(size_t Capacity) {
  36. AllocatorNode *NewHead = new AllocatorNode;
  37. NewHead->Buf = new uint8_t[Capacity];
  38. NewHead->Next = Head;
  39. NewHead->Capacity = Capacity;
  40. Head = NewHead;
  41. NewHead->Used = 0;
  42. }
  43. public:
  44. ArenaAllocator() { addNode(AllocUnit); }
  45. ~ArenaAllocator() {
  46. while (Head) {
  47. assert(Head->Buf);
  48. delete[] Head->Buf;
  49. AllocatorNode *Next = Head->Next;
  50. delete Head;
  51. Head = Next;
  52. }
  53. }
  54. char *allocUnalignedBuffer(size_t Size) {
  55. assert(Head && Head->Buf);
  56. uint8_t *P = Head->Buf + Head->Used;
  57. Head->Used += Size;
  58. if (Head->Used <= Head->Capacity)
  59. return reinterpret_cast<char *>(P);
  60. addNode(std::max(AllocUnit, Size));
  61. Head->Used = Size;
  62. return reinterpret_cast<char *>(Head->Buf);
  63. }
  64. template <typename T, typename... Args> T *allocArray(size_t Count) {
  65. size_t Size = Count * sizeof(T);
  66. assert(Head && Head->Buf);
  67. size_t P = (size_t)Head->Buf + Head->Used;
  68. uintptr_t AlignedP =
  69. (((size_t)P + alignof(T) - 1) & ~(size_t)(alignof(T) - 1));
  70. uint8_t *PP = (uint8_t *)AlignedP;
  71. size_t Adjustment = AlignedP - P;
  72. Head->Used += Size + Adjustment;
  73. if (Head->Used <= Head->Capacity)
  74. return new (PP) T[Count]();
  75. addNode(std::max(AllocUnit, Size));
  76. Head->Used = Size;
  77. return new (Head->Buf) T[Count]();
  78. }
  79. template <typename T, typename... Args> T *alloc(Args &&... ConstructorArgs) {
  80. constexpr size_t Size = sizeof(T);
  81. assert(Head && Head->Buf);
  82. size_t P = (size_t)Head->Buf + Head->Used;
  83. uintptr_t AlignedP =
  84. (((size_t)P + alignof(T) - 1) & ~(size_t)(alignof(T) - 1));
  85. uint8_t *PP = (uint8_t *)AlignedP;
  86. size_t Adjustment = AlignedP - P;
  87. Head->Used += Size + Adjustment;
  88. if (Head->Used <= Head->Capacity)
  89. return new (PP) T(std::forward<Args>(ConstructorArgs)...);
  90. static_assert(Size < AllocUnit, "");
  91. addNode(AllocUnit);
  92. Head->Used = Size;
  93. return new (Head->Buf) T(std::forward<Args>(ConstructorArgs)...);
  94. }
  95. private:
  96. AllocatorNode *Head = nullptr;
  97. };
  98. struct BackrefContext {
  99. static constexpr size_t Max = 10;
  100. TypeNode *FunctionParams[Max];
  101. size_t FunctionParamCount = 0;
  102. // The first 10 BackReferences in a mangled name can be back-referenced by
  103. // special name @[0-9]. This is a storage for the first 10 BackReferences.
  104. NamedIdentifierNode *Names[Max];
  105. size_t NamesCount = 0;
  106. };
  107. enum class QualifierMangleMode { Drop, Mangle, Result };
  108. enum NameBackrefBehavior : uint8_t {
  109. NBB_None = 0, // don't save any names as backrefs.
  110. NBB_Template = 1 << 0, // save template instanations.
  111. NBB_Simple = 1 << 1, // save simple names.
  112. };
  113. enum class FunctionIdentifierCodeGroup { Basic, Under, DoubleUnder };
  114. // Demangler class takes the main role in demangling symbols.
  115. // It has a set of functions to parse mangled symbols into Type instances.
  116. // It also has a set of functions to convert Type instances to strings.
  117. class Demangler {
  118. public:
  119. Demangler() = default;
  120. virtual ~Demangler() = default;
  121. // You are supposed to call parse() first and then check if error is true. If
  122. // it is false, call output() to write the formatted name to the given stream.
  123. SymbolNode *parse(StringView &MangledName);
  124. TagTypeNode *parseTagUniqueName(StringView &MangledName);
  125. // True if an error occurred.
  126. bool Error = false;
  127. void dumpBackReferences();
  128. private:
  129. SymbolNode *demangleEncodedSymbol(StringView &MangledName,
  130. QualifiedNameNode *QN);
  131. SymbolNode *demangleDeclarator(StringView &MangledName);
  132. SymbolNode *demangleMD5Name(StringView &MangledName);
  133. SymbolNode *demangleTypeinfoName(StringView &MangledName);
  134. VariableSymbolNode *demangleVariableEncoding(StringView &MangledName,
  135. StorageClass SC);
  136. FunctionSymbolNode *demangleFunctionEncoding(StringView &MangledName);
  137. Qualifiers demanglePointerExtQualifiers(StringView &MangledName);
  138. // Parser functions. This is a recursive-descent parser.
  139. TypeNode *demangleType(StringView &MangledName, QualifierMangleMode QMM);
  140. PrimitiveTypeNode *demanglePrimitiveType(StringView &MangledName);
  141. CustomTypeNode *demangleCustomType(StringView &MangledName);
  142. TagTypeNode *demangleClassType(StringView &MangledName);
  143. PointerTypeNode *demanglePointerType(StringView &MangledName);
  144. PointerTypeNode *demangleMemberPointerType(StringView &MangledName);
  145. FunctionSignatureNode *demangleFunctionType(StringView &MangledName,
  146. bool HasThisQuals);
  147. ArrayTypeNode *demangleArrayType(StringView &MangledName);
  148. NodeArrayNode *demangleFunctionParameterList(StringView &MangledName,
  149. bool &IsVariadic);
  150. NodeArrayNode *demangleTemplateParameterList(StringView &MangledName);
  151. std::pair<uint64_t, bool> demangleNumber(StringView &MangledName);
  152. uint64_t demangleUnsigned(StringView &MangledName);
  153. int64_t demangleSigned(StringView &MangledName);
  154. void memorizeString(StringView s);
  155. void memorizeIdentifier(IdentifierNode *Identifier);
  156. /// Allocate a copy of \p Borrowed into memory that we own.
  157. StringView copyString(StringView Borrowed);
  158. QualifiedNameNode *demangleFullyQualifiedTypeName(StringView &MangledName);
  159. QualifiedNameNode *demangleFullyQualifiedSymbolName(StringView &MangledName);
  160. IdentifierNode *demangleUnqualifiedTypeName(StringView &MangledName,
  161. bool Memorize);
  162. IdentifierNode *demangleUnqualifiedSymbolName(StringView &MangledName,
  163. NameBackrefBehavior NBB);
  164. QualifiedNameNode *demangleNameScopeChain(StringView &MangledName,
  165. IdentifierNode *UnqualifiedName);
  166. IdentifierNode *demangleNameScopePiece(StringView &MangledName);
  167. NamedIdentifierNode *demangleBackRefName(StringView &MangledName);
  168. IdentifierNode *demangleTemplateInstantiationName(StringView &MangledName,
  169. NameBackrefBehavior NBB);
  170. IntrinsicFunctionKind
  171. translateIntrinsicFunctionCode(char CH, FunctionIdentifierCodeGroup Group);
  172. IdentifierNode *demangleFunctionIdentifierCode(StringView &MangledName);
  173. IdentifierNode *
  174. demangleFunctionIdentifierCode(StringView &MangledName,
  175. FunctionIdentifierCodeGroup Group);
  176. StructorIdentifierNode *demangleStructorIdentifier(StringView &MangledName,
  177. bool IsDestructor);
  178. ConversionOperatorIdentifierNode *
  179. demangleConversionOperatorIdentifier(StringView &MangledName);
  180. LiteralOperatorIdentifierNode *
  181. demangleLiteralOperatorIdentifier(StringView &MangledName);
  182. SymbolNode *demangleSpecialIntrinsic(StringView &MangledName);
  183. SpecialTableSymbolNode *
  184. demangleSpecialTableSymbolNode(StringView &MangledName,
  185. SpecialIntrinsicKind SIK);
  186. LocalStaticGuardVariableNode *
  187. demangleLocalStaticGuard(StringView &MangledName, bool IsThread);
  188. VariableSymbolNode *demangleUntypedVariable(ArenaAllocator &Arena,
  189. StringView &MangledName,
  190. StringView VariableName);
  191. VariableSymbolNode *
  192. demangleRttiBaseClassDescriptorNode(ArenaAllocator &Arena,
  193. StringView &MangledName);
  194. FunctionSymbolNode *demangleInitFiniStub(StringView &MangledName,
  195. bool IsDestructor);
  196. NamedIdentifierNode *demangleSimpleName(StringView &MangledName,
  197. bool Memorize);
  198. NamedIdentifierNode *demangleAnonymousNamespaceName(StringView &MangledName);
  199. NamedIdentifierNode *demangleLocallyScopedNamePiece(StringView &MangledName);
  200. EncodedStringLiteralNode *demangleStringLiteral(StringView &MangledName);
  201. FunctionSymbolNode *demangleVcallThunkNode(StringView &MangledName);
  202. StringView demangleSimpleString(StringView &MangledName, bool Memorize);
  203. FuncClass demangleFunctionClass(StringView &MangledName);
  204. CallingConv demangleCallingConvention(StringView &MangledName);
  205. StorageClass demangleVariableStorageClass(StringView &MangledName);
  206. bool demangleThrowSpecification(StringView &MangledName);
  207. wchar_t demangleWcharLiteral(StringView &MangledName);
  208. uint8_t demangleCharLiteral(StringView &MangledName);
  209. std::pair<Qualifiers, bool> demangleQualifiers(StringView &MangledName);
  210. // Memory allocator.
  211. ArenaAllocator Arena;
  212. // A single type uses one global back-ref table for all function params.
  213. // This means back-refs can even go "into" other types. Examples:
  214. //
  215. // // Second int* is a back-ref to first.
  216. // void foo(int *, int*);
  217. //
  218. // // Second int* is not a back-ref to first (first is not a function param).
  219. // int* foo(int*);
  220. //
  221. // // Second int* is a back-ref to first (ALL function types share the same
  222. // // back-ref map.
  223. // using F = void(*)(int*);
  224. // F G(int *);
  225. BackrefContext Backrefs;
  226. };
  227. } // namespace ms_demangle
  228. } // namespace llvm
  229. #endif // LLVM_DEMANGLE_MICROSOFT_DEMANGLE_H
  230. #ifdef __GNUC__
  231. #pragma GCC diagnostic pop
  232. #endif