MicrosoftDemangle.h 10 KB

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