LLVM  9.0.0svn
MicrosoftDemangle.h
Go to the documentation of this file.
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 
9 #ifndef LLVM_DEMANGLE_MICROSOFT_DEMANGLE_H
10 #define LLVM_DEMANGLE_MICROSOFT_DEMANGLE_H
11 
15 #include "llvm/Demangle/Utility.h"
16 
17 #include <utility>
18 
19 namespace llvm {
20 namespace ms_demangle {
21 // This memory allocator is extremely fast, but it doesn't call dtors
22 // for allocated objects. That means you can't use STL containers
23 // (such as std::vector) with this allocator. But it pays off --
24 // the demangler is 3x faster with this allocator compared to one with
25 // STL containers.
26 constexpr size_t AllocUnit = 4096;
27 
29  struct AllocatorNode {
30  uint8_t *Buf = nullptr;
31  size_t Used = 0;
32  size_t Capacity = 0;
33  AllocatorNode *Next = nullptr;
34  };
35 
36  void addNode(size_t Capacity) {
37  AllocatorNode *NewHead = new AllocatorNode;
38  NewHead->Buf = new uint8_t[Capacity];
39  NewHead->Next = Head;
40  NewHead->Capacity = Capacity;
41  Head = NewHead;
42  NewHead->Used = 0;
43  }
44 
45 public:
46  ArenaAllocator() { addNode(AllocUnit); }
47 
49  while (Head) {
50  assert(Head->Buf);
51  delete[] Head->Buf;
52  AllocatorNode *Next = Head->Next;
53  delete Head;
54  Head = Next;
55  }
56  }
57 
58  char *allocUnalignedBuffer(size_t Size) {
59  assert(Head && Head->Buf);
60 
61  uint8_t *P = Head->Buf + Head->Used;
62 
63  Head->Used += Size;
64  if (Head->Used <= Head->Capacity)
65  return reinterpret_cast<char *>(P);
66 
67  addNode(std::max(AllocUnit, Size));
68  Head->Used = Size;
69  return reinterpret_cast<char *>(Head->Buf);
70  }
71 
72  template <typename T, typename... Args> T *allocArray(size_t Count) {
73  size_t Size = Count * sizeof(T);
74  assert(Head && Head->Buf);
75 
76  size_t P = (size_t)Head->Buf + Head->Used;
77  uintptr_t AlignedP =
78  (((size_t)P + alignof(T) - 1) & ~(size_t)(alignof(T) - 1));
79  uint8_t *PP = (uint8_t *)AlignedP;
80  size_t Adjustment = AlignedP - P;
81 
82  Head->Used += Size + Adjustment;
83  if (Head->Used <= Head->Capacity)
84  return new (PP) T[Count]();
85 
86  addNode(std::max(AllocUnit, Size));
87  Head->Used = Size;
88  return new (Head->Buf) T[Count]();
89  }
90 
91  template <typename T, typename... Args> T *alloc(Args &&... ConstructorArgs) {
92  constexpr size_t Size = sizeof(T);
93  assert(Head && Head->Buf);
94 
95  size_t P = (size_t)Head->Buf + Head->Used;
96  uintptr_t AlignedP =
97  (((size_t)P + alignof(T) - 1) & ~(size_t)(alignof(T) - 1));
98  uint8_t *PP = (uint8_t *)AlignedP;
99  size_t Adjustment = AlignedP - P;
100 
101  Head->Used += Size + Adjustment;
102  if (Head->Used <= Head->Capacity)
103  return new (PP) T(std::forward<Args>(ConstructorArgs)...);
104 
105  static_assert(Size < AllocUnit, "");
106  addNode(AllocUnit);
107  Head->Used = Size;
108  return new (Head->Buf) T(std::forward<Args>(ConstructorArgs)...);
109  }
110 
111 private:
112  AllocatorNode *Head = nullptr;
113 };
114 
116  static constexpr size_t Max = 10;
117 
118  TypeNode *FunctionParams[Max];
119  size_t FunctionParamCount = 0;
120 
121  // The first 10 BackReferences in a mangled name can be back-referenced by
122  // special name @[0-9]. This is a storage for the first 10 BackReferences.
123  NamedIdentifierNode *Names[Max];
124  size_t NamesCount = 0;
125 };
126 
128 
129 enum NameBackrefBehavior : uint8_t {
130  NBB_None = 0, // don't save any names as backrefs.
131  NBB_Template = 1 << 0, // save template instanations.
132  NBB_Simple = 1 << 1, // save simple names.
133 };
134 
136 
137 // Demangler class takes the main role in demangling symbols.
138 // It has a set of functions to parse mangled symbols into Type instances.
139 // It also has a set of functions to convert Type instances to strings.
140 class Demangler {
141 public:
142  Demangler() = default;
143  virtual ~Demangler() = default;
144 
145  // You are supposed to call parse() first and then check if error is true. If
146  // it is false, call output() to write the formatted name to the given stream.
147  SymbolNode *parse(StringView &MangledName);
148 
149  TagTypeNode *parseTagUniqueName(StringView &MangledName);
150 
151  // True if an error occurred.
152  bool Error = false;
153 
154  void dumpBackReferences();
155 
156 private:
157  SymbolNode *demangleEncodedSymbol(StringView &MangledName,
158  QualifiedNameNode *QN);
159  SymbolNode *demangleDeclarator(StringView &MangledName);
160 
161  VariableSymbolNode *demangleVariableEncoding(StringView &MangledName,
162  StorageClass SC);
163  FunctionSymbolNode *demangleFunctionEncoding(StringView &MangledName);
164 
165  Qualifiers demanglePointerExtQualifiers(StringView &MangledName);
166 
167  // Parser functions. This is a recursive-descent parser.
168  TypeNode *demangleType(StringView &MangledName, QualifierMangleMode QMM);
169  PrimitiveTypeNode *demanglePrimitiveType(StringView &MangledName);
170  CustomTypeNode *demangleCustomType(StringView &MangledName);
171  TagTypeNode *demangleClassType(StringView &MangledName);
172  PointerTypeNode *demanglePointerType(StringView &MangledName);
173  PointerTypeNode *demangleMemberPointerType(StringView &MangledName);
174  FunctionSignatureNode *demangleFunctionType(StringView &MangledName,
175  bool HasThisQuals);
176 
177  ArrayTypeNode *demangleArrayType(StringView &MangledName);
178 
179  NodeArrayNode *demangleTemplateParameterList(StringView &MangledName);
180  NodeArrayNode *demangleFunctionParameterList(StringView &MangledName);
181 
182  std::pair<uint64_t, bool> demangleNumber(StringView &MangledName);
183  uint64_t demangleUnsigned(StringView &MangledName);
184  int64_t demangleSigned(StringView &MangledName);
185 
186  void memorizeString(StringView s);
187  void memorizeIdentifier(IdentifierNode *Identifier);
188 
189  /// Allocate a copy of \p Borrowed into memory that we own.
190  StringView copyString(StringView Borrowed);
191 
192  QualifiedNameNode *demangleFullyQualifiedTypeName(StringView &MangledName);
193  QualifiedNameNode *demangleFullyQualifiedSymbolName(StringView &MangledName);
194 
195  IdentifierNode *demangleUnqualifiedTypeName(StringView &MangledName,
196  bool Memorize);
197  IdentifierNode *demangleUnqualifiedSymbolName(StringView &MangledName,
198  NameBackrefBehavior NBB);
199 
200  QualifiedNameNode *demangleNameScopeChain(StringView &MangledName,
201  IdentifierNode *UnqualifiedName);
202  IdentifierNode *demangleNameScopePiece(StringView &MangledName);
203 
204  NamedIdentifierNode *demangleBackRefName(StringView &MangledName);
205  IdentifierNode *demangleTemplateInstantiationName(StringView &MangledName,
206  NameBackrefBehavior NBB);
208  translateIntrinsicFunctionCode(char CH, FunctionIdentifierCodeGroup Group);
209  IdentifierNode *demangleFunctionIdentifierCode(StringView &MangledName);
211  demangleFunctionIdentifierCode(StringView &MangledName,
213  StructorIdentifierNode *demangleStructorIdentifier(StringView &MangledName,
214  bool IsDestructor);
216  demangleConversionOperatorIdentifier(StringView &MangledName);
218  demangleLiteralOperatorIdentifier(StringView &MangledName);
219 
220  SymbolNode *demangleSpecialIntrinsic(StringView &MangledName);
222  demangleSpecialTableSymbolNode(StringView &MangledName,
225  demangleLocalStaticGuard(StringView &MangledName);
226  VariableSymbolNode *demangleUntypedVariable(ArenaAllocator &Arena,
227  StringView &MangledName,
228  StringView VariableName);
230  demangleRttiBaseClassDescriptorNode(ArenaAllocator &Arena,
231  StringView &MangledName);
232  FunctionSymbolNode *demangleInitFiniStub(StringView &MangledName,
233  bool IsDestructor);
234 
235  NamedIdentifierNode *demangleSimpleName(StringView &MangledName,
236  bool Memorize);
237  NamedIdentifierNode *demangleAnonymousNamespaceName(StringView &MangledName);
238  NamedIdentifierNode *demangleLocallyScopedNamePiece(StringView &MangledName);
239  EncodedStringLiteralNode *demangleStringLiteral(StringView &MangledName);
240  FunctionSymbolNode *demangleVcallThunkNode(StringView &MangledName);
241 
242  StringView demangleSimpleString(StringView &MangledName, bool Memorize);
243 
244  FuncClass demangleFunctionClass(StringView &MangledName);
245  CallingConv demangleCallingConvention(StringView &MangledName);
246  StorageClass demangleVariableStorageClass(StringView &MangledName);
247  bool demangleThrowSpecification(StringView &MangledName);
248  wchar_t demangleWcharLiteral(StringView &MangledName);
249  uint8_t demangleCharLiteral(StringView &MangledName);
250 
251  std::pair<Qualifiers, bool> demangleQualifiers(StringView &MangledName);
252 
253  // Memory allocator.
254  ArenaAllocator Arena;
255 
256  // A single type uses one global back-ref table for all function params.
257  // This means back-refs can even go "into" other types. Examples:
258  //
259  // // Second int* is a back-ref to first.
260  // void foo(int *, int*);
261  //
262  // // Second int* is not a back-ref to first (first is not a function param).
263  // int* foo(int*);
264  //
265  // // Second int* is a back-ref to first (ALL function types share the same
266  // // back-ref map.
267  // using F = void(*)(int*);
268  // F G(int *);
269  BackrefContext Backrefs;
270 };
271 
272 } // namespace ms_demangle
273 } // namespace llvm
274 
275 #endif // LLVM_DEMANGLE_MICROSOFT_DEMANGLE_H
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
char * allocUnalignedBuffer(size_t Size)
constexpr size_t AllocUnit
#define T
T * alloc(Args &&... ConstructorArgs)
#define P(N)
itanium_demangle::ManglingParser< DefaultAllocator > Demangler
llvm::Expected< Value > parse(llvm::StringRef JSON)
Parses the provided JSON source, or returns a ParseError.
Definition: JSON.cpp:510
CHAIN = SC CHAIN, Imm128 - System call.
uint32_t Size
Definition: Profile.cpp:46
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
constexpr char Args[]
Key for Kernel::Metadata::mArgs.