1 //===- MicrosoftDemangle.cpp ----------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a demangler for MSVC-style mangled symbols.
11 //
12 // This file has no dependencies on the rest of LLVM so that it can be
13 // easily reused in other programs such as libcxxabi.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #include "llvm/Demangle/Demangle.h"
18 
19 #include "llvm/Demangle/Compiler.h"
20 #include "llvm/Demangle/StringView.h"
21 #include "llvm/Demangle/Utility.h"
22 
23 #include <array>
24 #include <cctype>
25 #include <cstdio>
26 #include <tuple>
27 
28 // This memory allocator is extremely fast, but it doesn't call dtors
29 // for allocated objects. That means you can't use STL containers
30 // (such as std::vector) with this allocator. But it pays off --
31 // the demangler is 3x faster with this allocator compared to one with
32 // STL containers.
33 namespace {
34   constexpr size_t AllocUnit = 4096;
35 
36 class ArenaAllocator {
37   struct AllocatorNode {
38     uint8_t *Buf = nullptr;
39     size_t Used = 0;
40     size_t Capacity = 0;
41     AllocatorNode *Next = nullptr;
42   };
43 
44   void addNode(size_t Capacity) {
45     AllocatorNode *NewHead = new AllocatorNode;
46     NewHead->Buf = new uint8_t[Capacity];
47     NewHead->Next = Head;
48     NewHead->Capacity = Capacity;
49     Head = NewHead;
50     NewHead->Used = 0;
51   }
52 
53 public:
54   ArenaAllocator() { addNode(AllocUnit); }
55 
56   ~ArenaAllocator() {
57     while (Head) {
58       assert(Head->Buf);
59       delete[] Head->Buf;
60       AllocatorNode *Next = Head->Next;
61       delete Head;
62       Head = Next;
63     }
64   }
65 
66   char *allocUnalignedBuffer(size_t Length) {
67     uint8_t *Buf = Head->Buf + Head->Used;
68 
69     Head->Used += Length;
70     if (Head->Used > Head->Capacity) {
71       // It's possible we need a buffer which is larger than our default unit
72       // size, so we need to be careful to add a node with capacity that is at
73       // least as large as what we need.
74       addNode(std::max(AllocUnit, Length));
75       Head->Used = Length;
76       Buf = Head->Buf;
77     }
78 
79     return reinterpret_cast<char *>(Buf);
80   }
81 
82   template <typename T, typename... Args> T *alloc(Args &&... ConstructorArgs) {
83 
84     size_t Size = sizeof(T);
85     assert(Head && Head->Buf);
86 
87     size_t P = (size_t)Head->Buf + Head->Used;
88     uintptr_t AlignedP =
89         (((size_t)P + alignof(T) - 1) & ~(size_t)(alignof(T) - 1));
90     uint8_t *PP = (uint8_t *)AlignedP;
91     size_t Adjustment = AlignedP - P;
92 
93     Head->Used += Size + Adjustment;
94     if (Head->Used < Head->Capacity)
95       return new (PP) T(std::forward<Args>(ConstructorArgs)...);
96 
97     addNode(AllocUnit);
98     Head->Used = Size;
99     return new (Head->Buf) T(std::forward<Args>(ConstructorArgs)...);
100   }
101 
102 private:
103   AllocatorNode *Head = nullptr;
104 };
105 } // namespace
106 
107 static bool startsWithDigit(StringView S) {
108   return !S.empty() && std::isdigit(S.front());
109 }
110 
111 // Writes a space if the last token does not end with a punctuation.
112 static void outputSpaceIfNecessary(OutputStream &OS) {
113   if (OS.empty())
114     return;
115 
116   char C = OS.back();
117   if (isalnum(C) || C == '>')
118     OS << " ";
119 }
120 
121 // Storage classes
122 enum Qualifiers : uint8_t {
123   Q_None = 0,
124   Q_Const = 1 << 0,
125   Q_Volatile = 1 << 1,
126   Q_Far = 1 << 2,
127   Q_Huge = 1 << 3,
128   Q_Unaligned = 1 << 4,
129   Q_Restrict = 1 << 5,
130   Q_Pointer64 = 1 << 6
131 };
132 
133 enum class StorageClass : uint8_t {
134   None,
135   PrivateStatic,
136   ProtectedStatic,
137   PublicStatic,
138   Global,
139   FunctionLocalStatic,
140 };
141 
142 enum class QualifierMangleMode { Drop, Mangle, Result };
143 
144 enum class PointerAffinity { Pointer, Reference, RValueReference };
145 
146 // Calling conventions
147 enum class CallingConv : uint8_t {
148   None,
149   Cdecl,
150   Pascal,
151   Thiscall,
152   Stdcall,
153   Fastcall,
154   Clrcall,
155   Eabi,
156   Vectorcall,
157   Regcall,
158 };
159 
160 enum class ReferenceKind : uint8_t { None, LValueRef, RValueRef };
161 
162 // Types
163 enum class PrimTy : uint8_t {
164   Unknown,
165   None,
166   Function,
167   Ptr,
168   MemberPtr,
169   Array,
170 
171   Struct,
172   Union,
173   Class,
174   Enum,
175 
176   Void,
177   Bool,
178   Char,
179   Schar,
180   Uchar,
181   Char16,
182   Char32,
183   Short,
184   Ushort,
185   Int,
186   Uint,
187   Long,
188   Ulong,
189   Int64,
190   Uint64,
191   Wchar,
192   Float,
193   Double,
194   Ldouble,
195   Nullptr,
196   Custom,
197   Vftable,
198   Vbtable,
199   LocalStaticGuard
200 };
201 
202 enum class OperatorTy : uint8_t {
203   Ctor,                    // ?0 # Foo::Foo()
204   Dtor,                    // ?1 # Foo::~Foo()
205   New,                     // ?2 # operator new
206   Delete,                  // ?3 # operator delete
207   Assign,                  // ?4 # operator=
208   RightShift,              // ?5 # operator>>
209   LeftShift,               // ?6 # operator<<
210   LogicalNot,              // ?7 # operator!
211   Equals,                  // ?8 # operator==
212   NotEquals,               // ?9 # operator!=
213   ArraySubscript,          // ?A # operator[]
214   Conversion,              // ?B # Foo::operator <type>()
215   Pointer,                 // ?C # operator->
216   Dereference,             // ?D # operator*
217   Increment,               // ?E # operator++
218   Decrement,               // ?F # operator--
219   Minus,                   // ?G # operator-
220   Plus,                    // ?H # operator+
221   BitwiseAnd,              // ?I # operator&
222   MemberPointer,           // ?J # operator->*
223   Divide,                  // ?K # operator/
224   Modulus,                 // ?L # operator%
225   LessThan,                // ?M operator<
226   LessThanEqual,           // ?N operator<=
227   GreaterThan,             // ?O operator>
228   GreaterThanEqual,        // ?P operator>=
229   Comma,                   // ?Q operator,
230   Parens,                  // ?R operator()
231   BitwiseNot,              // ?S operator~
232   BitwiseXor,              // ?T operator^
233   BitwiseOr,               // ?U operator|
234   LogicalAnd,              // ?V operator&&
235   LogicalOr,               // ?W operator||
236   TimesEqual,              // ?X operator*=
237   PlusEqual,               // ?Y operator+=
238   MinusEqual,              // ?Z operator-=
239   DivEqual,                // ?_0 operator/=
240   ModEqual,                // ?_1 operator%=
241   RshEqual,                // ?_2 operator>>=
242   LshEqual,                // ?_3 operator<<=
243   BitwiseAndEqual,         // ?_4 operator&=
244   BitwiseOrEqual,          // ?_5 operator|=
245   BitwiseXorEqual,         // ?_6 operator^=
246   Vftable,                 // ?_7 # vftable
247   Vbtable,                 // ?_8 # vbtable
248   Vcall,                   // ?_9 # vcall
249   Typeof,                  // ?_A # typeof
250   LocalStaticGuard,        // ?_B # local static guard
251   StringLiteral,           // ?_C # string literal
252   VbaseDtor,               // ?_D # vbase destructor
253   VecDelDtor,              // ?_E # vector deleting destructor
254   DefaultCtorClosure,      // ?_F # default constructor closure
255   ScalarDelDtor,           // ?_G # scalar deleting destructor
256   VecCtorIter,             // ?_H # vector constructor iterator
257   VecDtorIter,             // ?_I # vector destructor iterator
258   VecVbaseCtorIter,        // ?_J # vector vbase constructor iterator
259   VdispMap,                // ?_K # virtual displacement map
260   EHVecCtorIter,           // ?_L # eh vector constructor iterator
261   EHVecDtorIter,           // ?_M # eh vector destructor iterator
262   EHVecVbaseCtorIter,      // ?_N # eh vector vbase constructor iterator
263   CopyCtorClosure,         // ?_O # copy constructor closure
264   UdtReturning,            // ?_P<name> # udt returning <name>
265   Unknown,                 // ?_Q # <unknown>
266   RttiTypeDescriptor,      // ?_R0 # RTTI Type Descriptor
267   RttiBaseClassDescriptor, // ?_R1 # RTTI Base Class Descriptor at (a,b,c,d)
268   RttiBaseClassArray,      // ?_R2 # RTTI Base Class Array
269   RttiClassHierarchyDescriptor, // ?_R3 # RTTI Class Hierarchy Descriptor
270   RttiCompleteObjLocator,       // ?_R4 # RTTI Complete Object Locator
271   LocalVftable,                 // ?_S # local vftable
272   LocalVftableCtorClosure,      // ?_T # local vftable constructor closure
273   ArrayNew,                     // ?_U operator new[]
274   ArrayDelete,                  // ?_V operator delete[]
275   ManVectorCtorIter,            // ?__A managed vector ctor iterator
276   ManVectorDtorIter,            // ?__B managed vector dtor iterator
277   EHVectorCopyCtorIter,         // ?__C EH vector copy ctor iterator
278   EHVectorVbaseCopyCtorIter,    // ?__D EH vector vbase copy ctor iterator
279   DynamicInitializer,           // ?__E dynamic initializer for `T'
280   DynamicAtexitDestructor,      // ?__F dynamic atexit destructor for `T'
281   VectorCopyCtorIter,           // ?__G vector copy constructor iterator
282   VectorVbaseCopyCtorIter,      // ?__H vector vbase copy constructor iterator
283   ManVectorVbaseCopyCtorIter,   // ?__I managed vector vbase copy constructor
284                                 // iterator
285   LocalStaticThreadGuard,       // ?__J local static thread guard
286   LiteralOperator,              // ?__K operator ""_name
287   CoAwait,                      // ?__L co_await
288   Spaceship,                    // operator<=>
289 };
290 
291 // A map to translate from operator prefix to operator type.
292 struct OperatorMapEntry {
293   StringView Prefix;
294   StringView Name;
295   OperatorTy Operator;
296 };
297 
298 // The entries here must be in the same order as the enumeration so that it can
299 // be indexed by enum value.
300 OperatorMapEntry OperatorMap[] = {
301     {"0", " <ctor>", OperatorTy::Ctor},
302     {"1", " <dtor>", OperatorTy::Dtor},
303     {"2", "operator new", OperatorTy::New},
304     {"3", "operator delete", OperatorTy::Delete},
305     {"4", "operator=", OperatorTy::Assign},
306     {"5", "operator>>", OperatorTy::RightShift},
307     {"6", "operator<<", OperatorTy::LeftShift},
308     {"7", "operator!", OperatorTy::LogicalNot},
309     {"8", "operator==", OperatorTy::Equals},
310     {"9", "operator!=", OperatorTy::NotEquals},
311     {"A", "operator[]", OperatorTy::ArraySubscript},
312     {"B", "operator <conversion>", OperatorTy::Conversion},
313     {"C", "operator->", OperatorTy::Pointer},
314     {"D", "operator*", OperatorTy::Dereference},
315     {"E", "operator++", OperatorTy::Increment},
316     {"F", "operator--", OperatorTy::Decrement},
317     {"G", "operator-", OperatorTy::Minus},
318     {"H", "operator+", OperatorTy::Plus},
319     {"I", "operator&", OperatorTy::BitwiseAnd},
320     {"J", "operator->*", OperatorTy::MemberPointer},
321     {"K", "operator/", OperatorTy::Divide},
322     {"L", "operator%", OperatorTy::Modulus},
323     {"M", "operator<", OperatorTy::LessThan},
324     {"N", "operator<=", OperatorTy::LessThanEqual},
325     {"O", "operator>", OperatorTy::GreaterThan},
326     {"P", "operator>=", OperatorTy::GreaterThanEqual},
327     {"Q", "operator,", OperatorTy::Comma},
328     {"R", "operator()", OperatorTy::Parens},
329     {"S", "operator~", OperatorTy::BitwiseNot},
330     {"T", "operator^", OperatorTy::BitwiseXor},
331     {"U", "operator|", OperatorTy::BitwiseOr},
332     {"V", "operator&&", OperatorTy::LogicalAnd},
333     {"W", "operator||", OperatorTy::LogicalOr},
334     {"X", "operator*=", OperatorTy::TimesEqual},
335     {"Y", "operator+=", OperatorTy::PlusEqual},
336     {"Z", "operator-=", OperatorTy::MinusEqual},
337     {"_0", "operator/=", OperatorTy::DivEqual},
338     {"_1", "operator%=", OperatorTy::ModEqual},
339     {"_2", "operator>>=", OperatorTy::RshEqual},
340     {"_3", "operator<<=", OperatorTy::LshEqual},
341     {"_4", "operator&=", OperatorTy::BitwiseAndEqual},
342     {"_5", "operator|=", OperatorTy::BitwiseOrEqual},
343     {"_6", "operator^=", OperatorTy::BitwiseXorEqual},
344     {"_7", "`vftable'", OperatorTy::Vftable},
345     {"_8", "`vbtable'", OperatorTy::Vbtable},
346     {"_9", "`vcall'", OperatorTy::Vcall},
347     {"_A", "`typeof'", OperatorTy::Typeof},
348     {"_B", "`local static guard'", OperatorTy::LocalStaticGuard},
349     {"_C", "`string'", OperatorTy::StringLiteral},
350     {"_D", "`vbase dtor'", OperatorTy::VbaseDtor},
351     {"_E", "`vector deleting dtor'", OperatorTy::VecDelDtor},
352     {"_F", "`default ctor closure'", OperatorTy::DefaultCtorClosure},
353     {"_G", "`scalar deleting dtor'", OperatorTy::ScalarDelDtor},
354     {"_H", "`vector ctor iterator'", OperatorTy::VecCtorIter},
355     {"_I", "`vector dtor iterator'", OperatorTy::VecDtorIter},
356     {"_J", "`vector vbase ctor iterator'", OperatorTy::VecVbaseCtorIter},
357     {"_K", "`virtual displacement map'", OperatorTy::VdispMap},
358     {"_L", "`eh vector ctor iterator'", OperatorTy::EHVecCtorIter},
359     {"_M", "`eh vector dtor iterator'", OperatorTy::EHVecDtorIter},
360     {"_N", "`eh vector vbase ctor iterator'", OperatorTy::EHVecVbaseCtorIter},
361     {"_O", "`copy ctor closure'", OperatorTy::CopyCtorClosure},
362     {"_P", "`udt returning'", OperatorTy::UdtReturning},
363     {"_Q", "`unknown'", OperatorTy::Unknown},
364     {"_R0", "`RTTI Type Descriptor'", OperatorTy::RttiTypeDescriptor},
365     {"_R1", "RTTI Base Class Descriptor", OperatorTy::RttiBaseClassDescriptor},
366     {"_R2", "`RTTI Base Class Array'", OperatorTy::RttiBaseClassArray},
367     {"_R3", "`RTTI Class Hierarchy Descriptor'",
368      OperatorTy::RttiClassHierarchyDescriptor},
369     {"_R4", "`RTTI Complete Object Locator'",
370      OperatorTy::RttiCompleteObjLocator},
371     {"_S", "`local vftable'", OperatorTy::LocalVftable},
372     {"_T", "`local vftable ctor closure'", OperatorTy::LocalVftableCtorClosure},
373     {"_U", "operator new[]", OperatorTy::ArrayNew},
374     {"_V", "operator delete[]", OperatorTy::ArrayDelete},
375     {"__A", "managed vector ctor iterator", OperatorTy::ManVectorCtorIter},
376     {"__B", "managed vector dtor iterator", OperatorTy::ManVectorDtorIter},
377     {"__C", "EH vector copy ctor iterator", OperatorTy::EHVectorCopyCtorIter},
378     {"__D", "EH vector vbase copy ctor iterator",
379      OperatorTy::EHVectorVbaseCopyCtorIter},
380     {"__E", "dynamic initializer", OperatorTy::DynamicInitializer},
381     {"__F", "dynamic atexit destructor", OperatorTy::DynamicAtexitDestructor},
382     {"__G", "vector copy ctor iterator", OperatorTy::VectorCopyCtorIter},
383     {"__H", "vector vbase copy constructor iterator",
384      OperatorTy::VectorVbaseCopyCtorIter},
385     {"__I", "managed vector vbase copy constructor iterator",
386      OperatorTy::ManVectorVbaseCopyCtorIter},
387     {"__J", "local static thread guard", OperatorTy::LocalStaticThreadGuard},
388     {"__K", "operator \"\"", OperatorTy::LiteralOperator},
389     {"__L", "co_await", OperatorTy::CoAwait},
390 };
391 
392 // Function classes
393 enum FuncClass : uint16_t {
394   None = 0,
395   Public = 1 << 0,
396   Protected = 1 << 1,
397   Private = 1 << 2,
398   Global = 1 << 3,
399   Static = 1 << 4,
400   Virtual = 1 << 5,
401   Far = 1 << 6,
402   ExternC = 1 << 7,
403   NoPrototype = 1 << 8,
404   VirtualThisAdjust = 1 << 9,
405   VirtualThisAdjustEx = 1 << 10,
406   StaticThisAdjust = 1 << 11
407 };
408 
409 enum NameBackrefBehavior : uint8_t {
410   NBB_None = 0,          // don't save any names as backrefs.
411   NBB_Template = 1 << 0, // save template instanations.
412   NBB_Simple = 1 << 1,   // save simple names.
413 };
414 
415 enum class SymbolCategory {
416   Unknown,
417   NamedFunction,
418   NamedVariable,
419   UnnamedFunction,
420   UnnamedVariable,
421   SpecialOperator
422 };
423 
424 namespace {
425 
426 struct Type;
427 struct Name;
428 
429 struct FunctionParams {
430   bool IsVariadic = false;
431 
432   Type *Current = nullptr;
433 
434   FunctionParams *Next = nullptr;
435 };
436 
437 struct TemplateParams {
438   bool IsTemplateTemplate = false;
439   bool IsAliasTemplate = false;
440   bool IsIntegerLiteral = false;
441   bool IntegerLiteralIsNegative = false;
442   bool IsEmptyParameterPack = false;
443   bool PointerToSymbol = false;
444   bool NullptrLiteral = false;
445   bool DataMemberPointer = false;
446   bool ReferenceToSymbol = false;
447 
448   int ThunkOffsetCount = 0;
449   std::array<int64_t, 3> ThunkOffsets;
450 
451   // If IsIntegerLiteral is true, this is a non-type template parameter
452   // whose value is contained in this field.
453   uint64_t IntegralValue = 0;
454 
455   // Type can be null if this is a template template parameter.  In that case
456   // only Name will be valid.
457   Type *ParamType = nullptr;
458 
459   // Name can be valid if this is a template template parameter (see above) or
460   // this is a function declaration (e.g. foo<&SomeFunc>).  In the latter case
461   // Name contains the name of the function and Type contains the signature.
462   Name *ParamName = nullptr;
463 
464   TemplateParams *Next = nullptr;
465 };
466 
467 // The type class. Mangled symbols are first parsed and converted to
468 // this type and then converted to string.
469 struct Type {
470   virtual ~Type() {}
471 
472   virtual Type *clone(ArenaAllocator &Arena) const;
473 
474   // Write the "first half" of a given type.  This is a static functions to
475   // give the code a chance to do processing that is common to a subset of
476   // subclasses
477   static void outputPre(OutputStream &OS, Type &Ty);
478 
479   // Write the "second half" of a given type.  This is a static functions to
480   // give the code a chance to do processing that is common to a subset of
481   // subclasses
482   static void outputPost(OutputStream &OS, Type &Ty);
483 
484   virtual void outputPre(OutputStream &OS);
485   virtual void outputPost(OutputStream &OS);
486 
487   // Primitive type such as Int.
488   PrimTy Prim = PrimTy::Unknown;
489 
490   Qualifiers Quals = Q_None;
491   StringView Custom;
492   StorageClass Storage = StorageClass::None; // storage class
493 };
494 
495 // Represents an identifier which may be a template.
496 struct Name {
497   virtual ~Name() = default;
498 
499   bool IsTemplateInstantiation = false;
500   bool IsOperator = false;
501   bool IsBackReference = false;
502 
503   bool isStringLiteralOperatorInfo() const;
504 
505   // Name read from an MangledName string.
506   StringView Str;
507 
508   // Template parameters. Only valid if IsTemplateInstantiation is true.
509   TemplateParams *TParams = nullptr;
510 
511   // Nested BackReferences (e.g. "A::B::C") are represented as a linked list.
512   Name *Next = nullptr;
513 };
514 
515 struct OperatorInfo : public Name {
516   explicit OperatorInfo(const OperatorMapEntry &Info) : Info(&Info) {
517     this->IsOperator = true;
518   }
519   explicit OperatorInfo(OperatorTy OpType)
520       : OperatorInfo(OperatorMap[(int)OpType]) {}
521 
522   const OperatorMapEntry *Info = nullptr;
523   bool IsIndirectTable = false;
524 };
525 
526 struct IndirectTable : public OperatorInfo {
527   explicit IndirectTable(const OperatorMapEntry &Info) : OperatorInfo(Info) {
528     this->IsOperator = true;
529     this->IsIndirectTable = true;
530   }
531   explicit IndirectTable(OperatorTy OpType)
532       : IndirectTable(OperatorMap[(int)OpType]) {}
533 
534   const Name *TableLocation = nullptr;
535   const Name *TableTarget = nullptr;
536 };
537 
538 struct StringLiteral : public OperatorInfo {
539   StringLiteral() : OperatorInfo(OperatorTy::StringLiteral) {}
540 
541   PrimTy CharType;
542   bool IsTruncated = false;
543 };
544 
545 struct RttiBaseClassDescriptor : public OperatorInfo {
546   RttiBaseClassDescriptor()
547       : OperatorInfo(OperatorTy::RttiBaseClassDescriptor) {}
548 
549   uint32_t NVOffset = 0;
550   int32_t VBPtrOffset = 0;
551   uint32_t VBTableOffset = 0;
552   uint32_t Flags = 0;
553 };
554 
555 struct LocalStaticGuardVariable : public OperatorInfo {
556   LocalStaticGuardVariable() : OperatorInfo(OperatorTy::LocalStaticGuard) {}
557 
558   uint32_t ScopeIndex = 0;
559   bool IsVisible = false;
560 };
561 
562 struct VirtualMemberPtrThunk : public OperatorInfo {
563   VirtualMemberPtrThunk() : OperatorInfo(OperatorTy::Vcall) {}
564 
565   uint64_t OffsetInVTable = 0;
566   CallingConv CC = CallingConv::Cdecl;
567 };
568 
569 struct PointerType : public Type {
570   Type *clone(ArenaAllocator &Arena) const override;
571   void outputPre(OutputStream &OS) override;
572   void outputPost(OutputStream &OS) override;
573 
574   PointerAffinity Affinity;
575 
576   // Represents a type X in "a pointer to X", "a reference to X",
577   // "an array of X", or "a function returning X".
578   Type *Pointee = nullptr;
579 };
580 
581 struct MemberPointerType : public Type {
582   Type *clone(ArenaAllocator &Arena) const override;
583   void outputPre(OutputStream &OS) override;
584   void outputPost(OutputStream &OS) override;
585 
586   Name *MemberName = nullptr;
587 
588   // Represents a type X in "a pointer to X", "a reference to X",
589   // "an array of X", or "a function returning X".
590   Type *Pointee = nullptr;
591 };
592 
593 struct FunctionType : public Type {
594   struct ThisAdjustor {
595     uint32_t StaticOffset = 0;
596     int32_t VBPtrOffset = 0;
597     int32_t VBOffsetOffset = 0;
598     int32_t VtordispOffset = 0;
599   };
600 
601   Type *clone(ArenaAllocator &Arena) const override;
602   void outputPre(OutputStream &OS) override;
603   void outputPost(OutputStream &OS) override;
604 
605   // True if this FunctionType instance is the Pointee of a PointerType or
606   // MemberPointerType.
607   bool IsFunctionPointer = false;
608   bool IsThunk = false;
609 
610   Type *ReturnType = nullptr;
611   // If this is a reference, the type of reference.
612   ReferenceKind RefKind;
613 
614   CallingConv CallConvention;
615   FuncClass FunctionClass;
616 
617   // Valid if IsThunk is true.
618   ThisAdjustor *ThisAdjust = nullptr;
619 
620   FunctionParams Params;
621 };
622 
623 struct UdtType : public Type {
624   Type *clone(ArenaAllocator &Arena) const override;
625   void outputPre(OutputStream &OS) override;
626 
627   Name *UdtName = nullptr;
628 };
629 
630 struct ArrayDimension {
631   uint64_t Dim = 0;
632   ArrayDimension *Next = nullptr;
633 };
634 
635 struct ArrayType : public Type {
636   Type *clone(ArenaAllocator &Arena) const override;
637   void outputPre(OutputStream &OS) override;
638   void outputPost(OutputStream &OS) override;
639 
640   // Either NextDimension or ElementType will be valid.
641   ArrayDimension *Dims = nullptr;
642 
643   Type *ElementType = nullptr;
644 };
645 
646 } // namespace
647 
648 static bool isMemberPointer(StringView MangledName) {
649   switch (MangledName.popFront()) {
650   case '$':
651     // This is probably an rvalue reference (e.g. $$Q), and you cannot have an
652     // rvalue reference to a member.
653     return false;
654   case 'A':
655     // 'A' indicates a reference, and you cannot have a reference to a member
656     // function or member.
657     return false;
658   case 'P':
659   case 'Q':
660   case 'R':
661   case 'S':
662     // These 4 values indicate some kind of pointer, but we still don't know
663     // what.
664     break;
665   default:
666     assert(false && "Ty is not a pointer type!");
667   }
668 
669   // If it starts with a number, then 6 indicates a non-member function
670   // pointer, and 8 indicates a member function pointer.
671   if (startsWithDigit(MangledName)) {
672     assert(MangledName[0] == '6' || MangledName[0] == '8');
673     return (MangledName[0] == '8');
674   }
675 
676   // Remove ext qualifiers since those can appear on either type and are
677   // therefore not indicative.
678   MangledName.consumeFront('E'); // 64-bit
679   MangledName.consumeFront('I'); // restrict
680   MangledName.consumeFront('F'); // unaligned
681 
682   assert(!MangledName.empty());
683 
684   // The next value should be either ABCD (non-member) or QRST (member).
685   switch (MangledName.front()) {
686   case 'A':
687   case 'B':
688   case 'C':
689   case 'D':
690     return false;
691   case 'Q':
692   case 'R':
693   case 'S':
694   case 'T':
695     return true;
696   default:
697     assert(false);
698   }
699   return false;
700 }
701 
702 static void outputCallingConvention(OutputStream &OS, CallingConv CC) {
703   outputSpaceIfNecessary(OS);
704 
705   switch (CC) {
706   case CallingConv::Cdecl:
707     OS << "__cdecl";
708     break;
709   case CallingConv::Fastcall:
710     OS << "__fastcall";
711     break;
712   case CallingConv::Pascal:
713     OS << "__pascal";
714     break;
715   case CallingConv::Regcall:
716     OS << "__regcall";
717     break;
718   case CallingConv::Stdcall:
719     OS << "__stdcall";
720     break;
721   case CallingConv::Thiscall:
722     OS << "__thiscall";
723     break;
724   case CallingConv::Eabi:
725     OS << "__eabi";
726     break;
727   case CallingConv::Vectorcall:
728     OS << "__vectorcall";
729     break;
730   case CallingConv::Clrcall:
731     OS << "__clrcall";
732     break;
733   default:
734     break;
735   }
736 }
737 
738 static bool startsWithLocalScopePattern(StringView S) {
739   if (!S.consumeFront('?'))
740     return false;
741   if (S.size() < 2)
742     return false;
743 
744   size_t End = S.find('?');
745   if (End == StringView::npos)
746     return false;
747   StringView Candidate = S.substr(0, End);
748   if (Candidate.empty())
749     return false;
750 
751   // \?[0-9]\?
752   // ?@? is the discriminator 0.
753   if (Candidate.size() == 1)
754     return Candidate[0] == '@' || (Candidate[0] >= '0' && Candidate[0] <= '9');
755 
756   // If it's not 0-9, then it's an encoded number terminated with an @
757   if (Candidate.back() != '@')
758     return false;
759   Candidate = Candidate.dropBack();
760 
761   // An encoded number starts with B-P and all subsequent digits are in A-P.
762   // Note that the reason the first digit cannot be A is two fold.  First, it
763   // would create an ambiguity with ?A which delimits the beginning of an
764   // anonymous namespace.  Second, A represents 0, and you don't start a multi
765   // digit number with a leading 0.  Presumably the anonymous namespace
766   // ambiguity is also why single digit encoded numbers use 0-9 rather than A-J.
767   if (Candidate[0] < 'B' || Candidate[0] > 'P')
768     return false;
769   Candidate = Candidate.dropFront();
770   while (!Candidate.empty()) {
771     if (Candidate[0] < 'A' || Candidate[0] > 'P')
772       return false;
773     Candidate = Candidate.dropFront();
774   }
775 
776   return true;
777 }
778 
779 // Write a function or template parameter list.
780 static void outputParameterList(OutputStream &OS,
781                                 const FunctionParams &Params) {
782   if (!Params.Current) {
783     OS << "void";
784     return;
785   }
786 
787   const FunctionParams *Head = &Params;
788   while (Head) {
789     Type::outputPre(OS, *Head->Current);
790     Type::outputPost(OS, *Head->Current);
791 
792     Head = Head->Next;
793 
794     if (Head)
795       OS << ", ";
796   }
797 }
798 
799 static void outputStringLiteral(OutputStream &OS, const StringLiteral &Str) {
800   switch (Str.CharType) {
801   case PrimTy::Wchar:
802     OS << "const wchar_t * {L\"";
803     break;
804   case PrimTy::Char:
805     OS << "const char * {\"";
806     break;
807   case PrimTy::Char16:
808     OS << "const char16_t * {u\"";
809     break;
810   case PrimTy::Char32:
811     OS << "const char32_t * {U\"";
812     break;
813   default:
814     LLVM_BUILTIN_UNREACHABLE;
815   }
816   OS << Str.Str << "\"";
817   if (Str.IsTruncated)
818     OS << "...";
819   OS << "}";
820 }
821 
822 static void outputName(OutputStream &OS, const Name *TheName, const Type *Ty);
823 
824 static void outputParameterList(OutputStream &OS,
825                                 const TemplateParams &Params) {
826   if (Params.IsEmptyParameterPack) {
827     OS << "<>";
828     return;
829   }
830 
831   OS << "<";
832   const TemplateParams *Head = &Params;
833   while (Head) {
834     // Type can be null if this is a template template parameter,
835     // and Name can be null if this is a simple type.
836 
837     if (Head->IsIntegerLiteral) {
838       if (Head->IntegerLiteralIsNegative)
839         OS << '-';
840       OS << Head->IntegralValue;
841     } else if (Head->PointerToSymbol || Head->ReferenceToSymbol) {
842       if (Head->NullptrLiteral)
843         OS << "nullptr";
844       else {
845         if (Head->ThunkOffsetCount > 0)
846           OS << "{";
847         else if (Head->PointerToSymbol)
848           OS << "&";
849         if (Head->ParamType)
850           Type::outputPre(OS, *Head->ParamType);
851         outputName(OS, Head->ParamName, Head->ParamType);
852         if (Head->ParamType)
853           Type::outputPost(OS, *Head->ParamType);
854         if (Head->ThunkOffsetCount > 0) {
855           for (int I = 0; I < Head->ThunkOffsetCount; ++I) {
856             OS << ", " << Head->ThunkOffsets[I];
857           }
858           OS << "}";
859         }
860       }
861     } else if (Head->DataMemberPointer) {
862       OS << "{" << Head->ThunkOffsets[0];
863       for (int I = 1; I < Head->ThunkOffsetCount; ++I)
864         OS << ", " << Head->ThunkOffsets[I];
865       OS << "}";
866     } else if (Head->ParamType) {
867       // simple type.
868       Type::outputPre(OS, *Head->ParamType);
869       Type::outputPost(OS, *Head->ParamType);
870     } else {
871       // Template alias.
872       outputName(OS, Head->ParamName, Head->ParamType);
873     }
874 
875     Head = Head->Next;
876 
877     if (Head)
878       OS << ", ";
879   }
880   OS << ">";
881 }
882 
883 static void outputQualifiers(OutputStream &OS, Qualifiers Q) {
884   if (Q & Q_Const) {
885     outputSpaceIfNecessary(OS);
886     OS << "const";
887   }
888 
889   if (Q & Q_Volatile) {
890     outputSpaceIfNecessary(OS);
891     OS << "volatile";
892   }
893 
894   if (Q & Q_Restrict) {
895     outputSpaceIfNecessary(OS);
896     OS << "__restrict";
897   }
898 }
899 
900 static void outputNameComponent(OutputStream &OS, const Name &N) {
901   OS << N.Str;
902 
903   if (N.IsTemplateInstantiation && N.TParams)
904     outputParameterList(OS, *N.TParams);
905 }
906 
907 static const OperatorInfo *lastComponentAsOperator(const Name *TheName) {
908   if (!TheName)
909     return nullptr;
910   while (TheName->Next)
911     TheName = TheName->Next;
912   if (TheName->IsOperator)
913     return static_cast<const OperatorInfo *>(TheName);
914   return nullptr;
915 }
916 
917 static void outputName(OutputStream &OS, const Name *TheName, const Type *Ty) {
918   if (!TheName)
919     return;
920 
921   outputSpaceIfNecessary(OS);
922 
923   const OperatorInfo *Operator = lastComponentAsOperator(TheName);
924   const VirtualMemberPtrThunk *Thunk = nullptr;
925   bool PrintLastScopeSeparator = true;
926   if (Operator) {
927     if (Operator->IsIndirectTable) {
928       const IndirectTable *Table = static_cast<const IndirectTable *>(Operator);
929       outputName(OS, Table->TableLocation, nullptr);
930       OS << "{for `";
931       outputName(OS, Table->TableTarget, nullptr);
932       OS << "'}";
933       return;
934     }
935     if (Operator->Info->Operator == OperatorTy::Vcall) {
936       Thunk = static_cast<const VirtualMemberPtrThunk *>(Operator);
937       OS << "[thunk]: ";
938       outputCallingConvention(OS, Thunk->CC);
939       OS << " ";
940     } else if (Operator->Info->Operator == OperatorTy::DynamicInitializer) {
941       OS << "`dynamic initializer for '";
942       PrintLastScopeSeparator = false;
943     } else if (Operator->Info->Operator ==
944                OperatorTy::DynamicAtexitDestructor) {
945       OS << "`dynamic atexit destructor for '";
946       PrintLastScopeSeparator = false;
947     }
948   }
949 
950   const Name *Previous = nullptr;
951   // Print out namespaces or outer class BackReferences.
952   for (; TheName->Next; TheName = TheName->Next) {
953     Previous = TheName;
954     outputNameComponent(OS, *TheName);
955     if (TheName->Next != Operator || PrintLastScopeSeparator)
956       OS << "::";
957   }
958 
959   // Print out a regular name.
960   if (!TheName->IsOperator) {
961     outputNameComponent(OS, *TheName);
962     return;
963   }
964 
965 
966   // Print out ctor or dtor.
967   switch (Operator->Info->Operator) {
968   case OperatorTy::Dtor:
969     OS << "~";
970     LLVM_FALLTHROUGH;
971   case OperatorTy::Ctor:
972     // Output the class name with template arguments a second time.
973     outputNameComponent(OS, *Previous);
974 
975     // Structors don't have a name, so outputting the name here actually is a
976     // no-op.  But for template constructors, it needs to output the template
977     // argument list.  e.g.
978     //
979     // template<typename T>
980     // struct Foo {
981     //    template<typename U>
982     //    Foo(U);
983     // };
984     // should demangle as -- for example -- Foo<int><double>(double);
985     outputNameComponent(OS, *TheName);
986     break;
987   case OperatorTy::Conversion:
988     OS << "operator";
989     if (TheName->IsTemplateInstantiation && TheName->TParams)
990       outputParameterList(OS, *TheName->TParams);
991     OS << " ";
992     if (Ty) {
993       const FunctionType *FTy = static_cast<const FunctionType *>(Ty);
994       Type::outputPre(OS, *FTy->ReturnType);
995       Type::outputPost(OS, *FTy->ReturnType);
996     } else {
997       OS << "<conversion>";
998     }
999     break;
1000   case OperatorTy::LiteralOperator:
1001     OS << Operator->Info->Name;
1002     outputNameComponent(OS, *TheName);
1003     break;
1004   case OperatorTy::RttiBaseClassDescriptor: {
1005     const RttiBaseClassDescriptor &BCD =
1006         static_cast<const RttiBaseClassDescriptor &>(*Operator);
1007     OS << "`" << Operator->Info->Name << " at (";
1008     OS << BCD.NVOffset << ", " << BCD.VBPtrOffset << ", " << BCD.VBTableOffset
1009        << ", " << BCD.Flags;
1010     OS << ")'";
1011     break;
1012   }
1013   case OperatorTy::Vcall: {
1014     OS << "`vcall'{";
1015     OS << Thunk->OffsetInVTable << ", {flat}}";
1016     break;
1017   }
1018   case OperatorTy::DynamicInitializer:
1019   case OperatorTy::DynamicAtexitDestructor:
1020     OS << "''";
1021     break;
1022 
1023   case OperatorTy::LocalStaticGuard: {
1024     const LocalStaticGuardVariable &LSG =
1025         static_cast<const LocalStaticGuardVariable &>(*Operator);
1026     OS << Operator->Info->Name;
1027     if (LSG.ScopeIndex > 0)
1028       OS << "{" << LSG.ScopeIndex << "}";
1029     break;
1030   }
1031   default:
1032     OS << Operator->Info->Name;
1033     if (Operator->IsTemplateInstantiation)
1034       outputParameterList(OS, *Operator->TParams);
1035     break;
1036   }
1037 }
1038 
1039 static void outputSpecialOperator(OutputStream &OS, const Name *OuterName) {
1040   assert(OuterName);
1041   // The last component should be an operator.
1042   const OperatorInfo *Operator = lastComponentAsOperator(OuterName);
1043 
1044   assert(Operator->IsOperator);
1045   const OperatorInfo &Oper = static_cast<const OperatorInfo &>(*Operator);
1046   switch (Oper.Info->Operator) {
1047   case OperatorTy::StringLiteral: {
1048     const StringLiteral &SL = static_cast<const StringLiteral &>(Oper);
1049     outputStringLiteral(OS, SL);
1050     break;
1051   }
1052   case OperatorTy::Vcall: {
1053     const VirtualMemberPtrThunk &Thunk =
1054         static_cast<const VirtualMemberPtrThunk &>(Oper);
1055     OS << "[thunk]: ";
1056     outputCallingConvention(OS, Thunk.CC);
1057     OS << " ";
1058     // Print out namespaces or outer class BackReferences.
1059     const Name *N = OuterName;
1060     for (; N->Next; N = N->Next) {
1061       outputNameComponent(OS, *N);
1062       OS << "::";
1063     }
1064     OS << "`vcall'{";
1065     OS << Thunk.OffsetInVTable << ", {flat}}";
1066     break;
1067   }
1068   default:
1069     // There are no other special operator categories.
1070     LLVM_BUILTIN_UNREACHABLE;
1071   }
1072 }
1073 
1074 namespace {
1075 
1076 bool Name::isStringLiteralOperatorInfo() const {
1077   if (!IsOperator)
1078     return false;
1079   const OperatorInfo &O = static_cast<const OperatorInfo &>(*this);
1080   return O.Info->Operator == OperatorTy::StringLiteral;
1081 }
1082 
1083 Type *Type::clone(ArenaAllocator &Arena) const {
1084   return Arena.alloc<Type>(*this);
1085 }
1086 
1087 // Write the "first half" of a given type.
1088 void Type::outputPre(OutputStream &OS, Type &Ty) {
1089   // Function types require custom handling of const and static so we
1090   // handle them separately.  All other types use the same decoration
1091   // for these modifiers, so handle them here in common code.
1092   if (Ty.Prim == PrimTy::Function) {
1093     Ty.outputPre(OS);
1094     return;
1095   }
1096 
1097   switch (Ty.Storage) {
1098   case StorageClass::PrivateStatic:
1099   case StorageClass::PublicStatic:
1100   case StorageClass::ProtectedStatic:
1101     OS << "static ";
1102   default:
1103     break;
1104   }
1105   Ty.outputPre(OS);
1106 
1107   outputQualifiers(OS, Ty.Quals);
1108 }
1109 
1110 // Write the "second half" of a given type.
1111 void Type::outputPost(OutputStream &OS, Type &Ty) { Ty.outputPost(OS); }
1112 
1113 void Type::outputPre(OutputStream &OS) {
1114   switch (Prim) {
1115   case PrimTy::Void:
1116     OS << "void";
1117     break;
1118   case PrimTy::Bool:
1119     OS << "bool";
1120     break;
1121   case PrimTy::Char:
1122     OS << "char";
1123     break;
1124   case PrimTy::Schar:
1125     OS << "signed char";
1126     break;
1127   case PrimTy::Uchar:
1128     OS << "unsigned char";
1129     break;
1130   case PrimTy::Char16:
1131     OS << "char16_t";
1132     break;
1133   case PrimTy::Char32:
1134     OS << "char32_t";
1135     break;
1136   case PrimTy::Short:
1137     OS << "short";
1138     break;
1139   case PrimTy::Ushort:
1140     OS << "unsigned short";
1141     break;
1142   case PrimTy::Int:
1143     OS << "int";
1144     break;
1145   case PrimTy::Uint:
1146     OS << "unsigned int";
1147     break;
1148   case PrimTy::Long:
1149     OS << "long";
1150     break;
1151   case PrimTy::Ulong:
1152     OS << "unsigned long";
1153     break;
1154   case PrimTy::Int64:
1155     OS << "__int64";
1156     break;
1157   case PrimTy::Uint64:
1158     OS << "unsigned __int64";
1159     break;
1160   case PrimTy::Wchar:
1161     OS << "wchar_t";
1162     break;
1163   case PrimTy::Float:
1164     OS << "float";
1165     break;
1166   case PrimTy::Double:
1167     OS << "double";
1168     break;
1169   case PrimTy::Ldouble:
1170     OS << "long double";
1171     break;
1172   case PrimTy::Nullptr:
1173     OS << "std::nullptr_t";
1174     break;
1175   case PrimTy::Custom:
1176     OS << Custom;
1177     break;
1178   case PrimTy::Vbtable:
1179   case PrimTy::Vftable:
1180     break;
1181   default:
1182     assert(false && "Invalid primitive type!");
1183   }
1184 }
1185 void Type::outputPost(OutputStream &OS) {}
1186 
1187 Type *PointerType::clone(ArenaAllocator &Arena) const {
1188   return Arena.alloc<PointerType>(*this);
1189 }
1190 
1191 static void outputPointerIndicator(OutputStream &OS, PointerAffinity Affinity,
1192                                    const Name *MemberName,
1193                                    const Type *Pointee) {
1194   // "[]" and "()" (for function parameters) take precedence over "*",
1195   // so "int *x(int)" means "x is a function returning int *". We need
1196   // parentheses to supercede the default precedence. (e.g. we want to
1197   // emit something like "int (*x)(int)".)
1198   if (Pointee->Prim == PrimTy::Function || Pointee->Prim == PrimTy::Array) {
1199     OS << "(";
1200     if (Pointee->Prim == PrimTy::Function) {
1201       const FunctionType *FTy = static_cast<const FunctionType *>(Pointee);
1202       assert(FTy->IsFunctionPointer);
1203       outputCallingConvention(OS, FTy->CallConvention);
1204       OS << " ";
1205     }
1206   }
1207 
1208   if (MemberName) {
1209     outputName(OS, MemberName, Pointee);
1210     OS << "::";
1211   }
1212 
1213   if (Affinity == PointerAffinity::Pointer)
1214     OS << "*";
1215   else if (Affinity == PointerAffinity::Reference)
1216     OS << "&";
1217   else
1218     OS << "&&";
1219 }
1220 
1221 void PointerType::outputPre(OutputStream &OS) {
1222   Type::outputPre(OS, *Pointee);
1223 
1224   outputSpaceIfNecessary(OS);
1225 
1226   if (Quals & Q_Unaligned)
1227     OS << "__unaligned ";
1228 
1229   outputPointerIndicator(OS, Affinity, nullptr, Pointee);
1230 
1231   // FIXME: We should output this, but it requires updating lots of tests.
1232   // if (Ty.Quals & Q_Pointer64)
1233   //  OS << " __ptr64";
1234 }
1235 
1236 void PointerType::outputPost(OutputStream &OS) {
1237   if (Pointee->Prim == PrimTy::Function || Pointee->Prim == PrimTy::Array)
1238     OS << ")";
1239 
1240   Type::outputPost(OS, *Pointee);
1241 }
1242 
1243 Type *MemberPointerType::clone(ArenaAllocator &Arena) const {
1244   return Arena.alloc<MemberPointerType>(*this);
1245 }
1246 
1247 void MemberPointerType::outputPre(OutputStream &OS) {
1248   Type::outputPre(OS, *Pointee);
1249 
1250   outputSpaceIfNecessary(OS);
1251 
1252   outputPointerIndicator(OS, PointerAffinity::Pointer, MemberName, Pointee);
1253 
1254   // FIXME: We should output this, but it requires updating lots of tests.
1255   // if (Ty.Quals & Q_Pointer64)
1256   //  OS << " __ptr64";
1257 }
1258 
1259 void MemberPointerType::outputPost(OutputStream &OS) {
1260   if (Pointee->Prim == PrimTy::Function || Pointee->Prim == PrimTy::Array)
1261     OS << ")";
1262 
1263   Type::outputPost(OS, *Pointee);
1264 }
1265 
1266 Type *FunctionType::clone(ArenaAllocator &Arena) const {
1267   return Arena.alloc<FunctionType>(*this);
1268 }
1269 
1270 void FunctionType::outputPre(OutputStream &OS) {
1271   if ((FunctionClass & StaticThisAdjust) || (FunctionClass & VirtualThisAdjust))
1272     OS << "[thunk]: ";
1273 
1274   if (!(FunctionClass & Global)) {
1275     if (FunctionClass & Static)
1276       OS << "static ";
1277   }
1278   if (FunctionClass & ExternC)
1279     OS << "extern \"C\" ";
1280 
1281   if (FunctionClass & Virtual)
1282     OS << "virtual ";
1283 
1284   if (ReturnType) {
1285     Type::outputPre(OS, *ReturnType);
1286     OS << " ";
1287   }
1288 
1289   // Function pointers print the calling convention as void (__cdecl *)(params)
1290   // rather than void __cdecl (*)(params).  So we need to let the PointerType
1291   // class handle this.
1292   if (!IsFunctionPointer)
1293     outputCallingConvention(OS, CallConvention);
1294 }
1295 
1296 void FunctionType::outputPost(OutputStream &OS) {
1297   // extern "C" functions don't have a prototype.
1298   if (FunctionClass & NoPrototype)
1299     return;
1300 
1301   if (FunctionClass & StaticThisAdjust) {
1302     OS << "`adjustor{" << ThisAdjust->StaticOffset << "}'";
1303   } else if (FunctionClass & VirtualThisAdjust) {
1304     if (FunctionClass & VirtualThisAdjustEx) {
1305       OS << "`vtordispex{" << ThisAdjust->VBPtrOffset << ", "
1306          << ThisAdjust->VBOffsetOffset << ", " << ThisAdjust->VtordispOffset
1307          << ", " << ThisAdjust->StaticOffset << "}'";
1308     } else {
1309       OS << "`vtordisp{" << ThisAdjust->VtordispOffset << ", "
1310          << ThisAdjust->StaticOffset << "}'";
1311     }
1312   }
1313 
1314   OS << "(";
1315   outputParameterList(OS, Params);
1316   OS << ")";
1317   if (Quals & Q_Const)
1318     OS << " const";
1319   if (Quals & Q_Volatile)
1320     OS << " volatile";
1321   if (Quals & Q_Restrict)
1322     OS << " __restrict";
1323   if (Quals & Q_Unaligned)
1324     OS << " __unaligned";
1325 
1326   if (RefKind == ReferenceKind::LValueRef)
1327     OS << " &";
1328   else if (RefKind == ReferenceKind::RValueRef)
1329     OS << " &&";
1330 
1331   if (ReturnType)
1332     Type::outputPost(OS, *ReturnType);
1333   return;
1334 }
1335 
1336 Type *UdtType::clone(ArenaAllocator &Arena) const {
1337   return Arena.alloc<UdtType>(*this);
1338 }
1339 
1340 void UdtType::outputPre(OutputStream &OS) {
1341   switch (Prim) {
1342   case PrimTy::Class:
1343     OS << "class ";
1344     break;
1345   case PrimTy::Struct:
1346     OS << "struct ";
1347     break;
1348   case PrimTy::Union:
1349     OS << "union ";
1350     break;
1351   case PrimTy::Enum:
1352     OS << "enum ";
1353     break;
1354   default:
1355     assert(false && "Not a udt type!");
1356   }
1357 
1358   outputName(OS, UdtName, this);
1359 }
1360 
1361 Type *ArrayType::clone(ArenaAllocator &Arena) const {
1362   return Arena.alloc<ArrayType>(*this);
1363 }
1364 
1365 void ArrayType::outputPre(OutputStream &OS) {
1366   Type::outputPre(OS, *ElementType);
1367 }
1368 
1369 void ArrayType::outputPost(OutputStream &OS) {
1370   ArrayDimension *D = Dims;
1371   while (D) {
1372     OS << "[";
1373     if (D->Dim > 0)
1374       OS << D->Dim;
1375     OS << "]";
1376     D = D->Next;
1377   }
1378 
1379   Type::outputPost(OS, *ElementType);
1380 }
1381 
1382 struct Symbol {
1383   SymbolCategory Category;
1384 
1385   Qualifiers SymbolQuals = Q_None;
1386   Name *SymbolName = nullptr;
1387   Type *SymbolType = nullptr;
1388 };
1389 
1390 } // namespace
1391 
1392 namespace {
1393 
1394 struct BackrefContext {
1395   static constexpr size_t Max = 10;
1396 
1397   Type *FunctionParams[Max];
1398   size_t FunctionParamCount = 0;
1399 
1400   // The first 10 BackReferences in a mangled name can be back-referenced by
1401   // special name @[0-9]. This is a storage for the first 10 BackReferences.
1402   StringView Names[Max];
1403   size_t NamesCount = 0;
1404 };
1405 
1406 // Demangler class takes the main role in demangling symbols.
1407 // It has a set of functions to parse mangled symbols into Type instances.
1408 // It also has a set of functions to cnovert Type instances to strings.
1409 class Demangler {
1410 public:
1411   Demangler() = default;
1412   virtual ~Demangler() = default;
1413 
1414   // You are supposed to call parse() first and then check if error is true.  If
1415   // it is false, call output() to write the formatted name to the given stream.
1416   Symbol *parse(StringView &MangledName);
1417   Symbol *parseOperator(StringView &MangledName);
1418 
1419   void output(const Symbol *S, OutputStream &OS);
1420 
1421   // True if an error occurred.
1422   bool Error = false;
1423 
1424   void dumpBackReferences();
1425 
1426 private:
1427   std::pair<SymbolCategory, Type *>
1428   demangleSymbolCategoryAndType(StringView &MangledName);
1429 
1430   Type *demangleVariableEncoding(StringView &MangledName, StorageClass SC);
1431   Type *demangleFunctionEncoding(StringView &MangledName);
1432   uint64_t demangleThunkThisAdjust(StringView &MangledName);
1433 
1434   Qualifiers demanglePointerExtQualifiers(StringView &MangledName);
1435 
1436   // Parser functions. This is a recursive-descent parser.
1437   Type *demangleType(StringView &MangledName, QualifierMangleMode QMM);
1438   Type *demangleBasicType(StringView &MangledName);
1439   UdtType *demangleClassType(StringView &MangledName);
1440   PointerType *demanglePointerType(StringView &MangledName);
1441   MemberPointerType *demangleMemberPointerType(StringView &MangledName);
1442   FunctionType *demangleFunctionType(StringView &MangledName, bool HasThisQuals,
1443                                      bool IsFunctionPointer);
1444 
1445   ArrayType *demangleArrayType(StringView &MangledName);
1446 
1447   TemplateParams *demangleTemplateParameterList(StringView &MangledName);
1448   FunctionParams demangleFunctionParameterList(StringView &MangledName);
1449 
1450   std::pair<uint64_t, bool> demangleNumber(StringView &MangledName);
1451   uint64_t demangleUnsigned(StringView &MangledName);
1452   int64_t demangleSigned(StringView &MangledName);
1453 
1454   void memorizeString(StringView s);
1455 
1456   /// Allocate a copy of \p Borrowed into memory that we own.
1457   StringView copyString(StringView Borrowed);
1458 
1459   Name *demangleFullyQualifiedTypeName(StringView &MangledName);
1460   Name *demangleFullyQualifiedSymbolName(StringView &MangledName);
1461 
1462   Name *demangleUnqualifiedTypeName(StringView &MangledName, bool Memorize);
1463   Name *demangleUnqualifiedSymbolName(StringView &MangledName,
1464                                       NameBackrefBehavior NBB);
1465 
1466   Name *demangleNameScopeChain(StringView &MangledName, Name *UnqualifiedName);
1467   Name *demangleNameScopePiece(StringView &MangledName);
1468 
1469   Name *demangleBackRefName(StringView &MangledName);
1470   Name *demangleTemplateInstantiationName(StringView &MangledName,
1471                                           NameBackrefBehavior NBB);
1472   std::pair<OperatorTy, Name *> demangleOperatorName(StringView &MangledName,
1473                                                      bool FullyQualified);
1474   Name *demangleSimpleName(StringView &MangledName, bool Memorize);
1475   Name *demangleAnonymousNamespaceName(StringView &MangledName);
1476   Name *demangleLocallyScopedNamePiece(StringView &MangledName);
1477   StringLiteral *demangleStringLiteral(StringView &MangledName);
1478 
1479   StringView demangleSimpleString(StringView &MangledName, bool Memorize);
1480 
1481   FuncClass demangleFunctionClass(StringView &MangledName);
1482   CallingConv demangleCallingConvention(StringView &MangledName);
1483   StorageClass demangleVariableStorageClass(StringView &MangledName);
1484   ReferenceKind demangleReferenceKind(StringView &MangledName);
1485   void demangleThrowSpecification(StringView &MangledName);
1486   wchar_t demangleWcharLiteral(StringView &MangledName);
1487   uint8_t demangleCharLiteral(StringView &MangledName);
1488 
1489   std::pair<Qualifiers, bool> demangleQualifiers(StringView &MangledName);
1490 
1491   // Memory allocator.
1492   ArenaAllocator Arena;
1493 
1494   // A single type uses one global back-ref table for all function params.
1495   // This means back-refs can even go "into" other types.  Examples:
1496   //
1497   //  // Second int* is a back-ref to first.
1498   //  void foo(int *, int*);
1499   //
1500   //  // Second int* is not a back-ref to first (first is not a function param).
1501   //  int* foo(int*);
1502   //
1503   //  // Second int* is a back-ref to first (ALL function types share the same
1504   //  // back-ref map.
1505   //  using F = void(*)(int*);
1506   //  F G(int *);
1507   BackrefContext Backrefs;
1508 };
1509 } // namespace
1510 
1511 StringView Demangler::copyString(StringView Borrowed) {
1512   char *Stable = Arena.allocUnalignedBuffer(Borrowed.size() + 1);
1513   std::strcpy(Stable, Borrowed.begin());
1514 
1515   return {Stable, Borrowed.size()};
1516 }
1517 
1518 Symbol *Demangler::parseOperator(StringView &MangledName) {
1519   Symbol *S = Arena.alloc<Symbol>();
1520 
1521   bool IsMember = false;
1522   OperatorTy OTy;
1523   std::tie(OTy, S->SymbolName) = demangleOperatorName(MangledName, true);
1524   switch (OTy) {
1525   case OperatorTy::StringLiteral:
1526     S->Category = SymbolCategory::SpecialOperator;
1527     break;
1528   case OperatorTy::Vcall:
1529     S->Category = SymbolCategory::UnnamedFunction;
1530     break;
1531   case OperatorTy::LocalStaticGuard:
1532     S->Category = SymbolCategory::UnnamedVariable;
1533     break;
1534   case OperatorTy::Vftable:                // Foo@@6B@
1535   case OperatorTy::LocalVftable:           // Foo@@6B@
1536   case OperatorTy::RttiCompleteObjLocator: // Foo@@6B@
1537   case OperatorTy::Vbtable:                // Foo@@7B@
1538     S->Category = SymbolCategory::UnnamedVariable;
1539     switch (MangledName.popFront()) {
1540     case '6':
1541     case '7': {
1542       std::tie(S->SymbolQuals, IsMember) = demangleQualifiers(MangledName);
1543       if (!MangledName.consumeFront('@')) {
1544         IndirectTable *Table = Arena.alloc<IndirectTable>(OTy);
1545         Table->TableTarget = demangleFullyQualifiedTypeName(MangledName);
1546         Table->TableLocation = S->SymbolName;
1547         S->SymbolName = Table;
1548       }
1549       break;
1550     }
1551     default:
1552       Error = true;
1553       break;
1554     }
1555     break;
1556   case OperatorTy::RttiTypeDescriptor: // <type>@@8
1557     S->Category = SymbolCategory::UnnamedVariable;
1558     S->SymbolType = demangleType(MangledName, QualifierMangleMode::Result);
1559     if (Error)
1560       break;
1561     if (!MangledName.consumeFront("@8"))
1562       Error = true;
1563     if (!MangledName.empty())
1564       Error = true;
1565     break;
1566   default:
1567     if (!Error)
1568       std::tie(S->Category, S->SymbolType) =
1569           demangleSymbolCategoryAndType(MangledName);
1570     break;
1571   }
1572 
1573   return (Error) ? nullptr : S;
1574 }
1575 
1576 std::pair<SymbolCategory, Type *>
1577 Demangler::demangleSymbolCategoryAndType(StringView &MangledName) {
1578   // Read a variable.
1579   switch (MangledName.front()) {
1580   case '0':
1581   case '1':
1582   case '2':
1583   case '3':
1584   case '4':
1585     return std::make_pair(
1586         SymbolCategory::NamedVariable,
1587         demangleVariableEncoding(MangledName,
1588                                  demangleVariableStorageClass(MangledName)));
1589   case '8':
1590     MangledName.consumeFront('8');
1591     return std::pair<SymbolCategory, Type *>(SymbolCategory::UnnamedVariable,
1592                                              nullptr);
1593   }
1594   return std::make_pair(SymbolCategory::NamedFunction,
1595                         demangleFunctionEncoding(MangledName));
1596 }
1597 
1598 // Parser entry point.
1599 Symbol *Demangler::parse(StringView &MangledName) {
1600   // We can't demangle MD5 names, just output them as-is.
1601   // Also, MSVC-style mangled symbols must start with '?'.
1602   if (MangledName.startsWith("??@") || !MangledName.startsWith('?')) {
1603     Symbol *S = Arena.alloc<Symbol>();
1604     S->Category = SymbolCategory::Unknown;
1605     S->SymbolName = Arena.alloc<Name>();
1606     S->SymbolName->Str = MangledName;
1607     S->SymbolType = nullptr;
1608     MangledName = StringView();
1609     return S;
1610   }
1611 
1612   MangledName.consumeFront('?');
1613 
1614   // ?$ is a template instantiation, but all other names that start with ? are
1615   // operators / special names.
1616   if (MangledName.startsWith('?') && !MangledName.startsWith("?$"))
1617     return parseOperator(MangledName);
1618 
1619   Symbol *S = Arena.alloc<Symbol>();
1620   // What follows is a main symbol name. This may include namespaces or class
1621   // back references.
1622   S->SymbolName = demangleFullyQualifiedSymbolName(MangledName);
1623   if (Error)
1624     return nullptr;
1625 
1626   std::tie(S->Category, S->SymbolType) =
1627       demangleSymbolCategoryAndType(MangledName);
1628 
1629   if (Error)
1630     return nullptr;
1631 
1632   return S;
1633 }
1634 
1635 // <type-encoding> ::= <storage-class> <variable-type>
1636 // <storage-class> ::= 0  # private static member
1637 //                 ::= 1  # protected static member
1638 //                 ::= 2  # public static member
1639 //                 ::= 3  # global
1640 //                 ::= 4  # static local
1641 
1642 Type *Demangler::demangleVariableEncoding(StringView &MangledName,
1643                                           StorageClass SC) {
1644   Type *Ty = demangleType(MangledName, QualifierMangleMode::Drop);
1645 
1646   Ty->Storage = SC;
1647 
1648   // <variable-type> ::= <type> <cvr-qualifiers>
1649   //                 ::= <type> <pointee-cvr-qualifiers> # pointers, references
1650   switch (Ty->Prim) {
1651   case PrimTy::Ptr:
1652   case PrimTy::MemberPtr: {
1653     Qualifiers ExtraChildQuals = Q_None;
1654     Ty->Quals =
1655         Qualifiers(Ty->Quals | demanglePointerExtQualifiers(MangledName));
1656 
1657     bool IsMember = false;
1658     std::tie(ExtraChildQuals, IsMember) = demangleQualifiers(MangledName);
1659 
1660     if (Ty->Prim == PrimTy::MemberPtr) {
1661       assert(IsMember);
1662       Name *BackRefName = demangleFullyQualifiedTypeName(MangledName);
1663       (void)BackRefName;
1664       MemberPointerType *MPTy = static_cast<MemberPointerType *>(Ty);
1665       MPTy->Pointee->Quals = Qualifiers(MPTy->Pointee->Quals | ExtraChildQuals);
1666     } else {
1667       PointerType *PTy = static_cast<PointerType *>(Ty);
1668       PTy->Pointee->Quals = Qualifiers(PTy->Pointee->Quals | ExtraChildQuals);
1669     }
1670 
1671     break;
1672   }
1673   default:
1674     Ty->Quals = demangleQualifiers(MangledName).first;
1675     break;
1676   }
1677 
1678   return Ty;
1679 }
1680 
1681 // Sometimes numbers are encoded in mangled symbols. For example,
1682 // "int (*x)[20]" is a valid C type (x is a pointer to an array of
1683 // length 20), so we need some way to embed numbers as part of symbols.
1684 // This function parses it.
1685 //
1686 // <number>               ::= [?] <non-negative integer>
1687 //
1688 // <non-negative integer> ::= <decimal digit> # when 1 <= Number <= 10
1689 //                        ::= <hex digit>+ @  # when Numbrer == 0 or >= 10
1690 //
1691 // <hex-digit>            ::= [A-P]           # A = 0, B = 1, ...
1692 std::pair<uint64_t, bool> Demangler::demangleNumber(StringView &MangledName) {
1693   bool IsNegative = MangledName.consumeFront('?');
1694 
1695   if (startsWithDigit(MangledName)) {
1696     uint64_t Ret = MangledName[0] - '0' + 1;
1697     MangledName = MangledName.dropFront(1);
1698     return {Ret, IsNegative};
1699   }
1700 
1701   uint64_t Ret = 0;
1702   for (size_t i = 0; i < MangledName.size(); ++i) {
1703     char C = MangledName[i];
1704     if (C == '@') {
1705       MangledName = MangledName.dropFront(i + 1);
1706       return {Ret, IsNegative};
1707     }
1708     if ('A' <= C && C <= 'P') {
1709       Ret = (Ret << 4) + (C - 'A');
1710       continue;
1711     }
1712     break;
1713   }
1714 
1715   Error = true;
1716   return {0ULL, false};
1717 }
1718 
1719 uint64_t Demangler::demangleUnsigned(StringView &MangledName) {
1720   bool IsNegative = false;
1721   uint64_t Number = 0;
1722   std::tie(Number, IsNegative) = demangleNumber(MangledName);
1723   if (IsNegative)
1724     Error = true;
1725   return Number;
1726 }
1727 
1728 int64_t Demangler::demangleSigned(StringView &MangledName) {
1729   bool IsNegative = false;
1730   uint64_t Number = 0;
1731   std::tie(Number, IsNegative) = demangleNumber(MangledName);
1732   if (Number > INT64_MAX)
1733     Error = true;
1734   int64_t I = static_cast<int64_t>(Number);
1735   return IsNegative ? -I : I;
1736 }
1737 
1738 // First 10 strings can be referenced by special BackReferences ?0, ?1, ..., ?9.
1739 // Memorize it.
1740 void Demangler::memorizeString(StringView S) {
1741   if (Backrefs.NamesCount >= BackrefContext::Max)
1742     return;
1743   for (size_t i = 0; i < Backrefs.NamesCount; ++i)
1744     if (S == Backrefs.Names[i])
1745       return;
1746   Backrefs.Names[Backrefs.NamesCount++] = S;
1747 }
1748 
1749 Name *Demangler::demangleBackRefName(StringView &MangledName) {
1750   assert(startsWithDigit(MangledName));
1751 
1752   size_t I = MangledName[0] - '0';
1753   if (I >= Backrefs.NamesCount) {
1754     Error = true;
1755     return nullptr;
1756   }
1757 
1758   MangledName = MangledName.dropFront();
1759   Name *Node = Arena.alloc<Name>();
1760   Node->Str = Backrefs.Names[I];
1761   return Node;
1762 }
1763 
1764 Name *Demangler::demangleTemplateInstantiationName(StringView &MangledName,
1765                                                    NameBackrefBehavior NBB) {
1766   assert(MangledName.startsWith("?$"));
1767   MangledName.consumeFront("?$");
1768 
1769   BackrefContext OuterContext;
1770   std::swap(OuterContext, Backrefs);
1771 
1772   Name *Node = demangleUnqualifiedSymbolName(MangledName, NBB_Simple);
1773   if (!Error)
1774     Node->TParams = demangleTemplateParameterList(MangledName);
1775 
1776   std::swap(OuterContext, Backrefs);
1777   if (Error)
1778     return nullptr;
1779 
1780   Node->IsTemplateInstantiation = true;
1781 
1782   if (NBB & NBB_Template) {
1783     // Render this class template name into a string buffer so that we can
1784     // memorize it for the purpose of back-referencing.
1785     OutputStream OS = OutputStream::create(nullptr, nullptr, 1024);
1786     outputName(OS, Node, nullptr);
1787     OS << '\0';
1788     char *Name = OS.getBuffer();
1789 
1790     StringView Owned = copyString(Name);
1791     memorizeString(Owned);
1792     std::free(Name);
1793   }
1794 
1795   return Node;
1796 }
1797 
1798 std::pair<OperatorTy, Name *>
1799 Demangler::demangleOperatorName(StringView &MangledName, bool FullyQualified) {
1800   assert(MangledName.startsWith('?'));
1801   MangledName.consumeFront('?');
1802 
1803   const OperatorMapEntry *Entry = nullptr;
1804   for (const auto &MapEntry : OperatorMap) {
1805     if (!MangledName.consumeFront(MapEntry.Prefix))
1806       continue;
1807     Entry = &MapEntry;
1808     break;
1809   }
1810   if (!Entry) {
1811     Error = true;
1812     return std::make_pair(OperatorTy::Unknown, nullptr);
1813   }
1814 
1815   Name *N = nullptr;
1816   switch (Entry->Operator) {
1817   case OperatorTy::Vftable:                // Foo@@6B@
1818   case OperatorTy::LocalVftable:           // Foo@@6B@
1819   case OperatorTy::RttiCompleteObjLocator: // Foo@@6B@
1820   case OperatorTy::Vbtable: {              // Foo@@7B@
1821     N = Arena.alloc<OperatorInfo>(*Entry);
1822     if (FullyQualified)
1823       N = demangleNameScopeChain(MangledName, N);
1824     break;
1825   }
1826 
1827   case OperatorTy::StringLiteral:
1828     N = demangleStringLiteral(MangledName);
1829     break;
1830   case OperatorTy::LiteralOperator:
1831     N = Arena.alloc<OperatorInfo>(*Entry);
1832     N->Str = demangleSimpleString(MangledName, false);
1833     if (!MangledName.consumeFront('@'))
1834       Error = true;
1835     break;
1836   case OperatorTy::RttiBaseClassDescriptor: {
1837     RttiBaseClassDescriptor *Temp = Arena.alloc<RttiBaseClassDescriptor>();
1838     Temp->NVOffset = demangleUnsigned(MangledName);
1839     Temp->VBPtrOffset = demangleSigned(MangledName);
1840     Temp->VBTableOffset = demangleUnsigned(MangledName);
1841     Temp->Flags = demangleUnsigned(MangledName);
1842     N = (FullyQualified) ? demangleNameScopeChain(MangledName, Temp) : Temp;
1843     break;
1844   }
1845   case OperatorTy::Vcall: {
1846     VirtualMemberPtrThunk *Temp = Arena.alloc<VirtualMemberPtrThunk>();
1847     N = demangleNameScopeChain(MangledName, Temp);
1848     if (Error)
1849       break;
1850     if (!MangledName.consumeFront("$B"))
1851       Error = true;
1852     Temp->OffsetInVTable = demangleUnsigned(MangledName);
1853     if (!MangledName.consumeFront('A'))
1854       Error = true;
1855     Temp->CC = demangleCallingConvention(MangledName);
1856     break;
1857   }
1858   case OperatorTy::RttiTypeDescriptor:
1859     // This one is just followed by a type, not a name scope.
1860     N = Arena.alloc<OperatorInfo>(*Entry);
1861     break;
1862   case OperatorTy::LocalStaticGuard: {
1863     LocalStaticGuardVariable *Temp = Arena.alloc<LocalStaticGuardVariable>();
1864     N = (FullyQualified) ? demangleNameScopeChain(MangledName, Temp) : Temp;
1865     if (MangledName.consumeFront("4IA"))
1866       Temp->IsVisible = false;
1867     else if (MangledName.consumeFront("5"))
1868       Temp->IsVisible = true;
1869     else
1870       Error = true;
1871     if (!MangledName.empty())
1872       Temp->ScopeIndex = demangleUnsigned(MangledName);
1873     break;
1874   }
1875   default:
1876     N = Arena.alloc<OperatorInfo>(*Entry);
1877     N = (FullyQualified) ? demangleNameScopeChain(MangledName, N) : N;
1878     break;
1879   }
1880   if (Error)
1881     return std::make_pair(OperatorTy::Unknown, nullptr);
1882 
1883   return std::make_pair(Entry->Operator, N);
1884 }
1885 
1886 Name *Demangler::demangleSimpleName(StringView &MangledName, bool Memorize) {
1887   StringView S = demangleSimpleString(MangledName, Memorize);
1888   if (Error)
1889     return nullptr;
1890 
1891   Name *Node = Arena.alloc<Name>();
1892   Node->Str = S;
1893   return Node;
1894 }
1895 
1896 static bool isRebasedHexDigit(char C) { return (C >= 'A' && C <= 'P'); }
1897 
1898 static uint8_t rebasedHexDigitToNumber(char C) {
1899   assert(isRebasedHexDigit(C));
1900   return (C <= 'J') ? (C - 'A') : (10 + C - 'K');
1901 }
1902 
1903 uint8_t Demangler::demangleCharLiteral(StringView &MangledName) {
1904   if (!MangledName.startsWith('?'))
1905     return MangledName.popFront();
1906 
1907   MangledName = MangledName.dropFront();
1908   if (MangledName.empty())
1909     goto CharLiteralError;
1910 
1911   if (MangledName.consumeFront('$')) {
1912     // Two hex digits
1913     if (MangledName.size() < 2)
1914       goto CharLiteralError;
1915     StringView Nibbles = MangledName.substr(0, 2);
1916     if (!isRebasedHexDigit(Nibbles[0]) || !isRebasedHexDigit(Nibbles[1]))
1917       goto CharLiteralError;
1918     // Don't append the null terminator.
1919     uint8_t C1 = rebasedHexDigitToNumber(Nibbles[0]);
1920     uint8_t C2 = rebasedHexDigitToNumber(Nibbles[1]);
1921     MangledName = MangledName.dropFront(2);
1922     return (C1 << 4) | C2;
1923   }
1924 
1925   if (startsWithDigit(MangledName)) {
1926     const char *Lookup = ",/\\:. \n\t'-";
1927     char C = Lookup[MangledName[0] - '0'];
1928     MangledName = MangledName.dropFront();
1929     return C;
1930   }
1931 
1932   if (MangledName[0] >= 'a' && MangledName[0] <= 'z') {
1933     char Lookup[26] = {'\xE1', '\xE2', '\xE3', '\xE4', '\xE5', '\xE6', '\xE7',
1934                        '\xE8', '\xE9', '\xEA', '\xEB', '\xEC', '\xED', '\xEE',
1935                        '\xEF', '\xF0', '\xF1', '\xF2', '\xF3', '\xF4', '\xF5',
1936                        '\xF6', '\xF7', '\xF8', '\xF9', '\xFA'};
1937     char C = Lookup[MangledName[0] - 'a'];
1938     MangledName = MangledName.dropFront();
1939     return C;
1940   }
1941 
1942   if (MangledName[0] >= 'A' && MangledName[0] <= 'Z') {
1943     char Lookup[26] = {'\xC1', '\xC2', '\xC3', '\xC4', '\xC5', '\xC6', '\xC7',
1944                        '\xC8', '\xC9', '\xCA', '\xCB', '\xCC', '\xCD', '\xCE',
1945                        '\xCF', '\xD0', '\xD1', '\xD2', '\xD3', '\xD4', '\xD5',
1946                        '\xD6', '\xD7', '\xD8', '\xD9', '\xDA'};
1947     char C = Lookup[MangledName[0] - 'A'];
1948     MangledName = MangledName.dropFront();
1949     return C;
1950   }
1951 
1952 CharLiteralError:
1953   Error = true;
1954   return '\0';
1955 }
1956 
1957 wchar_t Demangler::demangleWcharLiteral(StringView &MangledName) {
1958   uint8_t C1, C2;
1959 
1960   C1 = demangleCharLiteral(MangledName);
1961   if (Error)
1962     goto WCharLiteralError;
1963   C2 = demangleCharLiteral(MangledName);
1964   if (Error)
1965     goto WCharLiteralError;
1966 
1967   return ((wchar_t)C1 << 8) | (wchar_t)C2;
1968 
1969 WCharLiteralError:
1970   Error = true;
1971   return L'\0';
1972 }
1973 
1974 static void writeHexDigit(char *Buffer, uint8_t Digit) {
1975   assert(Digit <= 15);
1976   *Buffer = (Digit < 10) ? ('0' + Digit) : ('A' + Digit - 10);
1977 }
1978 
1979 static void outputHex(OutputStream &OS, unsigned C) {
1980   if (C == 0) {
1981     OS << "\\x00";
1982     return;
1983   }
1984   // It's easier to do the math if we can work from right to left, but we need
1985   // to print the numbers from left to right.  So render this into a temporary
1986   // buffer first, then output the temporary buffer.  Each byte is of the form
1987   // \xAB, which means that each byte needs 4 characters.  Since there are at
1988   // most 4 bytes, we need a 4*4+1 = 17 character temporary buffer.
1989   char TempBuffer[17];
1990 
1991   ::memset(TempBuffer, 0, sizeof(TempBuffer));
1992   constexpr int MaxPos = 15;
1993 
1994   int Pos = MaxPos - 1;
1995   while (C != 0) {
1996     for (int I = 0; I < 2; ++I) {
1997       writeHexDigit(&TempBuffer[Pos--], C % 16);
1998       C /= 16;
1999     }
2000     TempBuffer[Pos--] = 'x';
2001     TempBuffer[Pos--] = '\\';
2002     assert(Pos >= 0);
2003   }
2004   OS << StringView(&TempBuffer[Pos + 1]);
2005 }
2006 
2007 static void outputEscapedChar(OutputStream &OS, unsigned C) {
2008   switch (C) {
2009   case '\'': // single quote
2010     OS << "\\\'";
2011     return;
2012   case '\"': // double quote
2013     OS << "\\\"";
2014     return;
2015   case '\\': // backslash
2016     OS << "\\\\";
2017     return;
2018   case '\a': // bell
2019     OS << "\\a";
2020     return;
2021   case '\b': // backspace
2022     OS << "\\b";
2023     return;
2024   case '\f': // form feed
2025     OS << "\\f";
2026     return;
2027   case '\n': // new line
2028     OS << "\\n";
2029     return;
2030   case '\r': // carriage return
2031     OS << "\\r";
2032     return;
2033   case '\t': // tab
2034     OS << "\\t";
2035     return;
2036   case '\v': // vertical tab
2037     OS << "\\v";
2038     return;
2039   default:
2040     break;
2041   }
2042 
2043   if (C > 0x1F && C < 0x7F) {
2044     // Standard ascii char.
2045     OS << (char)C;
2046     return;
2047   }
2048 
2049   outputHex(OS, C);
2050 }
2051 
2052 unsigned countTrailingNullBytes(const uint8_t *StringBytes, int Length) {
2053   const uint8_t *End = StringBytes + Length - 1;
2054   unsigned Count = 0;
2055   while (Length > 0 && *End == 0) {
2056     --Length;
2057     --End;
2058     ++Count;
2059   }
2060   return Count;
2061 }
2062 
2063 unsigned countEmbeddedNulls(const uint8_t *StringBytes, unsigned Length) {
2064   unsigned Result = 0;
2065   for (unsigned I = 0; I < Length; ++I) {
2066     if (*StringBytes++ == 0)
2067       ++Result;
2068   }
2069   return Result;
2070 }
2071 
2072 unsigned guessCharByteSize(const uint8_t *StringBytes, unsigned NumChars,
2073                            unsigned NumBytes) {
2074   assert(NumBytes > 0);
2075 
2076   // If the number of bytes is odd, this is guaranteed to be a char string.
2077   if (NumBytes % 2 == 1)
2078     return 1;
2079 
2080   // All strings can encode at most 32 bytes of data.  If it's less than that,
2081   // then we encoded the entire string.  In this case we check for a 1-byte,
2082   // 2-byte, or 4-byte null terminator.
2083   if (NumBytes < 32) {
2084     unsigned TrailingNulls = countTrailingNullBytes(StringBytes, NumChars);
2085     if (TrailingNulls >= 4)
2086       return 4;
2087     if (TrailingNulls >= 2)
2088       return 2;
2089     return 1;
2090   }
2091 
2092   // The whole string was not able to be encoded.  Try to look at embedded null
2093   // terminators to guess.  The heuristic is that we count all embedded null
2094   // terminators.  If more than 2/3 are null, it's a char32.  If more than 1/3
2095   // are null, it's a char16.  Otherwise it's a char8.  This obviously isn't
2096   // perfect and is biased towards languages that have ascii alphabets, but this
2097   // was always going to be best effort since the encoding is lossy.
2098   unsigned Nulls = countEmbeddedNulls(StringBytes, NumChars);
2099   if (Nulls >= 2 * NumChars / 3)
2100     return 4;
2101   if (Nulls >= NumChars / 3)
2102     return 2;
2103   return 1;
2104 }
2105 
2106 static unsigned decodeMultiByteChar(const uint8_t *StringBytes,
2107                                     unsigned CharIndex, unsigned CharBytes) {
2108   assert(CharBytes == 1 || CharBytes == 2 || CharBytes == 4);
2109   unsigned Offset = CharIndex * CharBytes;
2110   unsigned Result = 0;
2111   StringBytes = StringBytes + Offset;
2112   for (unsigned I = 0; I < CharBytes; ++I) {
2113     unsigned C = static_cast<unsigned>(StringBytes[I]);
2114     Result |= C << (8 * I);
2115   }
2116   return Result;
2117 }
2118 
2119 StringLiteral *Demangler::demangleStringLiteral(StringView &MangledName) {
2120   // This function uses goto, so declare all variables up front.
2121   OutputStream OS;
2122   StringView CRC;
2123   uint64_t StringByteSize;
2124   bool IsWcharT = false;
2125   bool IsNegative = false;
2126   size_t CrcEndPos = 0;
2127   char *ResultBuffer = nullptr;
2128 
2129   StringLiteral *Result = Arena.alloc<StringLiteral>();
2130 
2131   // Prefix indicating the beginning of a string literal
2132   if (!MangledName.consumeFront("@_"))
2133     goto StringLiteralError;
2134   if (MangledName.empty())
2135     goto StringLiteralError;
2136 
2137   // Char Type (regular or wchar_t)
2138   switch (MangledName.popFront()) {
2139   case '1':
2140     IsWcharT = true;
2141     LLVM_FALLTHROUGH;
2142   case '0':
2143     break;
2144   default:
2145     goto StringLiteralError;
2146   }
2147 
2148   // Encoded Length
2149   std::tie(StringByteSize, IsNegative) = demangleNumber(MangledName);
2150   if (Error || IsNegative)
2151     goto StringLiteralError;
2152 
2153   // CRC 32 (always 8 characters plus a terminator)
2154   CrcEndPos = MangledName.find('@');
2155   if (CrcEndPos == StringView::npos)
2156     goto StringLiteralError;
2157   CRC = MangledName.substr(0, CrcEndPos);
2158   MangledName = MangledName.dropFront(CrcEndPos + 1);
2159   if (MangledName.empty())
2160     goto StringLiteralError;
2161 
2162   OS = OutputStream::create(nullptr, nullptr, 1024);
2163   if (IsWcharT) {
2164     Result->CharType = PrimTy::Wchar;
2165     if (StringByteSize > 64)
2166       Result->IsTruncated = true;
2167 
2168     while (!MangledName.consumeFront('@')) {
2169       assert(StringByteSize >= 2);
2170       wchar_t W = demangleWcharLiteral(MangledName);
2171       if (StringByteSize != 2 || Result->IsTruncated)
2172         outputEscapedChar(OS, W);
2173       StringByteSize -= 2;
2174       if (Error)
2175         goto StringLiteralError;
2176     }
2177   } else {
2178     if (StringByteSize > 32)
2179       Result->IsTruncated = true;
2180 
2181     constexpr unsigned MaxStringByteLength = 32;
2182     uint8_t StringBytes[MaxStringByteLength];
2183 
2184     unsigned BytesDecoded = 0;
2185     while (!MangledName.consumeFront('@')) {
2186       assert(StringByteSize >= 1);
2187       StringBytes[BytesDecoded++] = demangleCharLiteral(MangledName);
2188     }
2189 
2190     unsigned CharBytes =
2191         guessCharByteSize(StringBytes, BytesDecoded, StringByteSize);
2192     assert(StringByteSize % CharBytes == 0);
2193     switch (CharBytes) {
2194     case 1:
2195       Result->CharType = PrimTy::Char;
2196       break;
2197     case 2:
2198       Result->CharType = PrimTy::Char16;
2199       break;
2200     case 4:
2201       Result->CharType = PrimTy::Char32;
2202       break;
2203     default:
2204       LLVM_BUILTIN_UNREACHABLE;
2205     }
2206     const unsigned NumChars = BytesDecoded / CharBytes;
2207     for (unsigned CharIndex = 0; CharIndex < NumChars; ++CharIndex) {
2208       unsigned NextChar =
2209           decodeMultiByteChar(StringBytes, CharIndex, CharBytes);
2210       if (CharIndex + 1 < NumChars || Result->IsTruncated)
2211         outputEscapedChar(OS, NextChar);
2212     }
2213   }
2214 
2215   OS << '\0';
2216   ResultBuffer = OS.getBuffer();
2217   Result->Str = copyString(ResultBuffer);
2218   std::free(ResultBuffer);
2219   return Result;
2220 
2221 StringLiteralError:
2222   Error = true;
2223   return nullptr;
2224 }
2225 
2226 StringView Demangler::demangleSimpleString(StringView &MangledName,
2227                                            bool Memorize) {
2228   StringView S;
2229   for (size_t i = 0; i < MangledName.size(); ++i) {
2230     if (MangledName[i] != '@')
2231       continue;
2232     S = MangledName.substr(0, i);
2233     MangledName = MangledName.dropFront(i + 1);
2234 
2235     if (Memorize)
2236       memorizeString(S);
2237     return S;
2238   }
2239 
2240   Error = true;
2241   return {};
2242 }
2243 
2244 Name *Demangler::demangleAnonymousNamespaceName(StringView &MangledName) {
2245   assert(MangledName.startsWith("?A"));
2246   MangledName.consumeFront("?A");
2247 
2248   Name *Node = Arena.alloc<Name>();
2249   Node->Str = "`anonymous namespace'";
2250   size_t EndPos = MangledName.find('@');
2251   if (EndPos == StringView::npos) {
2252     Error = true;
2253     return nullptr;
2254   }
2255   StringView NamespaceKey = MangledName.substr(0, EndPos);
2256   memorizeString(NamespaceKey);
2257   MangledName = MangledName.substr(EndPos + 1);
2258   return Node;
2259 }
2260 
2261 Name *Demangler::demangleLocallyScopedNamePiece(StringView &MangledName) {
2262   assert(startsWithLocalScopePattern(MangledName));
2263 
2264   Name *Node = Arena.alloc<Name>();
2265   MangledName.consumeFront('?');
2266   auto Number = demangleNumber(MangledName);
2267   assert(!Number.second);
2268 
2269   // One ? to terminate the number
2270   MangledName.consumeFront('?');
2271 
2272   assert(!Error);
2273   Symbol *Scope = parse(MangledName);
2274   if (Error)
2275     return nullptr;
2276 
2277   // Render the parent symbol's name into a buffer.
2278   OutputStream OS = OutputStream::create(nullptr, nullptr, 1024);
2279   OS << '`';
2280   output(Scope, OS);
2281   OS << '\'';
2282   OS << "::`" << Number.first << "'";
2283   OS << '\0';
2284   char *Result = OS.getBuffer();
2285   Node->Str = copyString(Result);
2286   std::free(Result);
2287   return Node;
2288 }
2289 
2290 // Parses a type name in the form of A@B@C@@ which represents C::B::A.
2291 Name *Demangler::demangleFullyQualifiedTypeName(StringView &MangledName) {
2292   Name *TypeName = demangleUnqualifiedTypeName(MangledName, true);
2293   if (Error)
2294     return nullptr;
2295   assert(TypeName);
2296 
2297   Name *QualName = demangleNameScopeChain(MangledName, TypeName);
2298   if (Error)
2299     return nullptr;
2300   assert(QualName);
2301   return QualName;
2302 }
2303 
2304 // Parses a symbol name in the form of A@B@C@@ which represents C::B::A.
2305 // Symbol names have slightly different rules regarding what can appear
2306 // so we separate out the implementations for flexibility.
2307 Name *Demangler::demangleFullyQualifiedSymbolName(StringView &MangledName) {
2308   // This is the final component of a symbol name (i.e. the leftmost component
2309   // of a mangled name.  Since the only possible template instantiation that
2310   // can appear in this context is a function template, and since those are
2311   // not saved for the purposes of name backreferences, only backref simple
2312   // names.
2313   Name *SymbolName = demangleUnqualifiedSymbolName(MangledName, NBB_Simple);
2314   if (Error)
2315     return nullptr;
2316 
2317   Name *QualName = demangleNameScopeChain(MangledName, SymbolName);
2318   if (Error)
2319     return nullptr;
2320   assert(QualName);
2321   return QualName;
2322 }
2323 
2324 Name *Demangler::demangleUnqualifiedTypeName(StringView &MangledName,
2325                                              bool Memorize) {
2326   // An inner-most name can be a back-reference, because a fully-qualified name
2327   // (e.g. Scope + Inner) can contain other fully qualified names inside of
2328   // them (for example template parameters), and these nested parameters can
2329   // refer to previously mangled types.
2330   if (startsWithDigit(MangledName))
2331     return demangleBackRefName(MangledName);
2332 
2333   if (MangledName.startsWith("?$"))
2334     return demangleTemplateInstantiationName(MangledName, NBB_Template);
2335 
2336   return demangleSimpleName(MangledName, Memorize);
2337 }
2338 
2339 Name *Demangler::demangleUnqualifiedSymbolName(StringView &MangledName,
2340                                                NameBackrefBehavior NBB) {
2341   if (startsWithDigit(MangledName))
2342     return demangleBackRefName(MangledName);
2343   if (MangledName.startsWith("?$"))
2344     return demangleTemplateInstantiationName(MangledName, NBB);
2345   if (MangledName.startsWith('?'))
2346     return demangleOperatorName(MangledName, false).second;
2347   return demangleSimpleName(MangledName, (NBB & NBB_Simple) != 0);
2348 }
2349 
2350 Name *Demangler::demangleNameScopePiece(StringView &MangledName) {
2351   if (startsWithDigit(MangledName))
2352     return demangleBackRefName(MangledName);
2353 
2354   if (MangledName.startsWith("?$"))
2355     return demangleTemplateInstantiationName(MangledName, NBB_Template);
2356 
2357   if (MangledName.startsWith("?A"))
2358     return demangleAnonymousNamespaceName(MangledName);
2359 
2360   if (startsWithLocalScopePattern(MangledName))
2361     return demangleLocallyScopedNamePiece(MangledName);
2362 
2363   return demangleSimpleName(MangledName, true);
2364 }
2365 
2366 Name *Demangler::demangleNameScopeChain(StringView &MangledName,
2367                                         Name *UnqualifiedName) {
2368   Name *Head = UnqualifiedName;
2369 
2370   while (!MangledName.consumeFront("@")) {
2371     if (MangledName.empty()) {
2372       Error = true;
2373       return nullptr;
2374     }
2375 
2376     assert(!Error);
2377     Name *Elem = demangleNameScopePiece(MangledName);
2378     if (Error)
2379       return nullptr;
2380 
2381     Elem->Next = Head;
2382     Head = Elem;
2383   }
2384   return Head;
2385 }
2386 
2387 FuncClass Demangler::demangleFunctionClass(StringView &MangledName) {
2388   SwapAndRestore<StringView> RestoreOnError(MangledName, MangledName);
2389   RestoreOnError.shouldRestore(false);
2390 
2391   switch (MangledName.popFront()) {
2392   case '9':
2393     return FuncClass(ExternC | NoPrototype);
2394   case 'A':
2395     return Private;
2396   case 'B':
2397     return FuncClass(Private | Far);
2398   case 'C':
2399     return FuncClass(Private | Static);
2400   case 'D':
2401     return FuncClass(Private | Static);
2402   case 'E':
2403     return FuncClass(Private | Virtual);
2404   case 'F':
2405     return FuncClass(Private | Virtual);
2406   case 'I':
2407     return FuncClass(Protected);
2408   case 'J':
2409     return FuncClass(Protected | Far);
2410   case 'K':
2411     return FuncClass(Protected | Static);
2412   case 'L':
2413     return FuncClass(Protected | Static | Far);
2414   case 'M':
2415     return FuncClass(Protected | Virtual);
2416   case 'N':
2417     return FuncClass(Protected | Virtual | Far);
2418   case 'O':
2419     return FuncClass(Protected | Virtual | StaticThisAdjust);
2420   case 'P':
2421     return FuncClass(Protected | Virtual | StaticThisAdjust | Far);
2422   case 'Q':
2423     return FuncClass(Public);
2424   case 'R':
2425     return FuncClass(Public | Far);
2426   case 'S':
2427     return FuncClass(Public | Static);
2428   case 'T':
2429     return FuncClass(Public | Static | Far);
2430   case 'U':
2431     return FuncClass(Public | Virtual);
2432   case 'V':
2433     return FuncClass(Public | Virtual | Far);
2434   case 'W':
2435     return FuncClass(Public | Virtual | StaticThisAdjust);
2436   case 'X':
2437     return FuncClass(Public | Virtual | StaticThisAdjust | Far);
2438   case 'Y':
2439     return FuncClass(Global);
2440   case 'Z':
2441     return FuncClass(Global | Far);
2442   case '$': {
2443     FuncClass VFlag = VirtualThisAdjust;
2444     if (MangledName.consumeFront('R'))
2445       VFlag = FuncClass(VFlag | VirtualThisAdjustEx);
2446 
2447     switch (MangledName.popFront()) {
2448     case '0':
2449       return FuncClass(Private | Virtual | VFlag);
2450     case '1':
2451       return FuncClass(Private | Virtual | VFlag | Far);
2452     case '2':
2453       return FuncClass(Protected | Virtual | VFlag);
2454     case '3':
2455       return FuncClass(Protected | Virtual | VFlag | Far);
2456     case '4':
2457       return FuncClass(Public | Virtual | VFlag);
2458     case '5':
2459       return FuncClass(Public | Virtual | VFlag | Far);
2460     }
2461   }
2462   }
2463 
2464   Error = true;
2465   RestoreOnError.shouldRestore(true);
2466   return Public;
2467 }
2468 
2469 CallingConv Demangler::demangleCallingConvention(StringView &MangledName) {
2470   switch (MangledName.popFront()) {
2471   case 'A':
2472   case 'B':
2473     return CallingConv::Cdecl;
2474   case 'C':
2475   case 'D':
2476     return CallingConv::Pascal;
2477   case 'E':
2478   case 'F':
2479     return CallingConv::Thiscall;
2480   case 'G':
2481   case 'H':
2482     return CallingConv::Stdcall;
2483   case 'I':
2484   case 'J':
2485     return CallingConv::Fastcall;
2486   case 'M':
2487   case 'N':
2488     return CallingConv::Clrcall;
2489   case 'O':
2490   case 'P':
2491     return CallingConv::Eabi;
2492   case 'Q':
2493     return CallingConv::Vectorcall;
2494   }
2495 
2496   return CallingConv::None;
2497 }
2498 
2499 StorageClass Demangler::demangleVariableStorageClass(StringView &MangledName) {
2500   assert(std::isdigit(MangledName.front()));
2501 
2502   switch (MangledName.popFront()) {
2503   case '0':
2504     return StorageClass::PrivateStatic;
2505   case '1':
2506     return StorageClass::ProtectedStatic;
2507   case '2':
2508     return StorageClass::PublicStatic;
2509   case '3':
2510     return StorageClass::Global;
2511   case '4':
2512     return StorageClass::FunctionLocalStatic;
2513   }
2514   Error = true;
2515   return StorageClass::None;
2516 }
2517 
2518 std::pair<Qualifiers, bool>
2519 Demangler::demangleQualifiers(StringView &MangledName) {
2520 
2521   switch (MangledName.popFront()) {
2522   // Member qualifiers
2523   case 'Q':
2524     return std::make_pair(Q_None, true);
2525   case 'R':
2526     return std::make_pair(Q_Const, true);
2527   case 'S':
2528     return std::make_pair(Q_Volatile, true);
2529   case 'T':
2530     return std::make_pair(Qualifiers(Q_Const | Q_Volatile), true);
2531   // Non-Member qualifiers
2532   case 'A':
2533     return std::make_pair(Q_None, false);
2534   case 'B':
2535     return std::make_pair(Q_Const, false);
2536   case 'C':
2537     return std::make_pair(Q_Volatile, false);
2538   case 'D':
2539     return std::make_pair(Qualifiers(Q_Const | Q_Volatile), false);
2540   }
2541   Error = true;
2542   return std::make_pair(Q_None, false);
2543 }
2544 
2545 static bool isTagType(StringView S) {
2546   switch (S.front()) {
2547   case 'T': // union
2548   case 'U': // struct
2549   case 'V': // class
2550   case 'W': // enum
2551     return true;
2552   }
2553   return false;
2554 }
2555 
2556 static bool isPointerType(StringView S) {
2557   if (S.startsWith("$$Q")) // foo &&
2558     return true;
2559 
2560   switch (S.front()) {
2561   case 'A': // foo &
2562   case 'P': // foo *
2563   case 'Q': // foo *const
2564   case 'R': // foo *volatile
2565   case 'S': // foo *const volatile
2566     return true;
2567   }
2568   return false;
2569 }
2570 
2571 static bool isArrayType(StringView S) { return S[0] == 'Y'; }
2572 
2573 static bool isFunctionType(StringView S) {
2574   return S.startsWith("$$A8@@") || S.startsWith("$$A6");
2575 }
2576 
2577 // <variable-type> ::= <type> <cvr-qualifiers>
2578 //                 ::= <type> <pointee-cvr-qualifiers> # pointers, references
2579 Type *Demangler::demangleType(StringView &MangledName,
2580                               QualifierMangleMode QMM) {
2581   Qualifiers Quals = Q_None;
2582   bool IsMember = false;
2583   if (QMM == QualifierMangleMode::Mangle) {
2584     std::tie(Quals, IsMember) = demangleQualifiers(MangledName);
2585   } else if (QMM == QualifierMangleMode::Result) {
2586     if (MangledName.consumeFront('?'))
2587       std::tie(Quals, IsMember) = demangleQualifiers(MangledName);
2588   }
2589 
2590   Type *Ty = nullptr;
2591   if (isTagType(MangledName))
2592     Ty = demangleClassType(MangledName);
2593   else if (isPointerType(MangledName)) {
2594     if (isMemberPointer(MangledName))
2595       Ty = demangleMemberPointerType(MangledName);
2596     else
2597       Ty = demanglePointerType(MangledName);
2598   } else if (isArrayType(MangledName))
2599     Ty = demangleArrayType(MangledName);
2600   else if (isFunctionType(MangledName)) {
2601     if (MangledName.consumeFront("$$A8@@"))
2602       Ty = demangleFunctionType(MangledName, true, false);
2603     else {
2604       assert(MangledName.startsWith("$$A6"));
2605       MangledName.consumeFront("$$A6");
2606       Ty = demangleFunctionType(MangledName, false, false);
2607     }
2608   } else {
2609     Ty = demangleBasicType(MangledName);
2610     assert(Ty && !Error);
2611     if (!Ty || Error)
2612       return Ty;
2613   }
2614 
2615   Ty->Quals = Qualifiers(Ty->Quals | Quals);
2616   return Ty;
2617 }
2618 
2619 ReferenceKind Demangler::demangleReferenceKind(StringView &MangledName) {
2620   if (MangledName.consumeFront('G'))
2621     return ReferenceKind::LValueRef;
2622   else if (MangledName.consumeFront('H'))
2623     return ReferenceKind::RValueRef;
2624   return ReferenceKind::None;
2625 }
2626 
2627 void Demangler::demangleThrowSpecification(StringView &MangledName) {
2628   if (MangledName.consumeFront('Z'))
2629     return;
2630 
2631   Error = true;
2632 }
2633 
2634 FunctionType *Demangler::demangleFunctionType(StringView &MangledName,
2635                                               bool HasThisQuals,
2636                                               bool IsFunctionPointer) {
2637   FunctionType *FTy = Arena.alloc<FunctionType>();
2638   FTy->Prim = PrimTy::Function;
2639   FTy->IsFunctionPointer = IsFunctionPointer;
2640 
2641   if (HasThisQuals) {
2642     FTy->Quals = demanglePointerExtQualifiers(MangledName);
2643     FTy->RefKind = demangleReferenceKind(MangledName);
2644     FTy->Quals = Qualifiers(FTy->Quals | demangleQualifiers(MangledName).first);
2645   }
2646 
2647   // Fields that appear on both member and non-member functions.
2648   FTy->CallConvention = demangleCallingConvention(MangledName);
2649 
2650   // <return-type> ::= <type>
2651   //               ::= @ # structors (they have no declared return type)
2652   bool IsStructor = MangledName.consumeFront('@');
2653   if (!IsStructor)
2654     FTy->ReturnType = demangleType(MangledName, QualifierMangleMode::Result);
2655 
2656   FTy->Params = demangleFunctionParameterList(MangledName);
2657 
2658   demangleThrowSpecification(MangledName);
2659 
2660   return FTy;
2661 }
2662 
2663 Type *Demangler::demangleFunctionEncoding(StringView &MangledName) {
2664   FuncClass ExtraFlags = FuncClass::None;
2665   if (MangledName.consumeFront("$$J0"))
2666     ExtraFlags = FuncClass::ExternC;
2667 
2668   FuncClass FC = demangleFunctionClass(MangledName);
2669   FC = FuncClass(ExtraFlags | FC);
2670 
2671   FunctionType::ThisAdjustor *Adjustor = nullptr;
2672   if (FC & FuncClass::StaticThisAdjust) {
2673     Adjustor = Arena.alloc<FunctionType::ThisAdjustor>();
2674     Adjustor->StaticOffset = demangleSigned(MangledName);
2675   } else if (FC & FuncClass::VirtualThisAdjust) {
2676     Adjustor = Arena.alloc<FunctionType::ThisAdjustor>();
2677     if (FC & FuncClass::VirtualThisAdjustEx) {
2678       Adjustor->VBPtrOffset = demangleSigned(MangledName);
2679       Adjustor->VBOffsetOffset = demangleSigned(MangledName);
2680     }
2681     Adjustor->VtordispOffset = demangleSigned(MangledName);
2682     Adjustor->StaticOffset = demangleSigned(MangledName);
2683   }
2684 
2685   FunctionType *FTy = nullptr;
2686   if (FC & NoPrototype) {
2687     // This is an extern "C" function whose full signature hasn't been mangled.
2688     // This happens when we need to mangle a local symbol inside of an extern
2689     // "C" function.
2690     FTy = Arena.alloc<FunctionType>();
2691   } else {
2692     bool HasThisQuals = !(FC & (Global | Static));
2693     FTy = demangleFunctionType(MangledName, HasThisQuals, false);
2694   }
2695   FTy->ThisAdjust = Adjustor;
2696   FTy->FunctionClass = FC;
2697 
2698   return FTy;
2699 }
2700 
2701 // Reads a primitive type.
2702 Type *Demangler::demangleBasicType(StringView &MangledName) {
2703   Type *Ty = Arena.alloc<Type>();
2704 
2705   if (MangledName.consumeFront("$$T")) {
2706     Ty->Prim = PrimTy::Nullptr;
2707     return Ty;
2708   }
2709   if (MangledName.consumeFront("?")) {
2710     Ty->Prim = PrimTy::Custom;
2711     Ty->Custom = demangleSimpleString(MangledName, false);
2712     if (!MangledName.consumeFront('@')) {
2713       Error = true;
2714       return nullptr;
2715     }
2716     return Ty;
2717   }
2718 
2719   switch (MangledName.popFront()) {
2720   case 'X':
2721     Ty->Prim = PrimTy::Void;
2722     break;
2723   case 'D':
2724     Ty->Prim = PrimTy::Char;
2725     break;
2726   case 'C':
2727     Ty->Prim = PrimTy::Schar;
2728     break;
2729   case 'E':
2730     Ty->Prim = PrimTy::Uchar;
2731     break;
2732   case 'F':
2733     Ty->Prim = PrimTy::Short;
2734     break;
2735   case 'G':
2736     Ty->Prim = PrimTy::Ushort;
2737     break;
2738   case 'H':
2739     Ty->Prim = PrimTy::Int;
2740     break;
2741   case 'I':
2742     Ty->Prim = PrimTy::Uint;
2743     break;
2744   case 'J':
2745     Ty->Prim = PrimTy::Long;
2746     break;
2747   case 'K':
2748     Ty->Prim = PrimTy::Ulong;
2749     break;
2750   case 'M':
2751     Ty->Prim = PrimTy::Float;
2752     break;
2753   case 'N':
2754     Ty->Prim = PrimTy::Double;
2755     break;
2756   case 'O':
2757     Ty->Prim = PrimTy::Ldouble;
2758     break;
2759   case '_': {
2760     if (MangledName.empty()) {
2761       Error = true;
2762       return nullptr;
2763     }
2764     switch (MangledName.popFront()) {
2765     case 'N':
2766       Ty->Prim = PrimTy::Bool;
2767       break;
2768     case 'J':
2769       Ty->Prim = PrimTy::Int64;
2770       break;
2771     case 'K':
2772       Ty->Prim = PrimTy::Uint64;
2773       break;
2774     case 'W':
2775       Ty->Prim = PrimTy::Wchar;
2776       break;
2777     case 'S':
2778       Ty->Prim = PrimTy::Char16;
2779       break;
2780     case 'U':
2781       Ty->Prim = PrimTy::Char32;
2782       break;
2783     default:
2784       Error = true;
2785       return nullptr;
2786     }
2787     break;
2788   }
2789   default:
2790     Error = true;
2791     return nullptr;
2792   }
2793   return Ty;
2794 }
2795 
2796 UdtType *Demangler::demangleClassType(StringView &MangledName) {
2797   UdtType *UTy = Arena.alloc<UdtType>();
2798 
2799   switch (MangledName.popFront()) {
2800   case 'T':
2801     UTy->Prim = PrimTy::Union;
2802     break;
2803   case 'U':
2804     UTy->Prim = PrimTy::Struct;
2805     break;
2806   case 'V':
2807     UTy->Prim = PrimTy::Class;
2808     break;
2809   case 'W':
2810     if (MangledName.popFront() != '4') {
2811       Error = true;
2812       return nullptr;
2813     }
2814     UTy->Prim = PrimTy::Enum;
2815     break;
2816   default:
2817     assert(false);
2818   }
2819 
2820   UTy->UdtName = demangleFullyQualifiedTypeName(MangledName);
2821   return UTy;
2822 }
2823 
2824 static std::pair<Qualifiers, PointerAffinity>
2825 demanglePointerCVQualifiers(StringView &MangledName) {
2826   if (MangledName.consumeFront("$$Q"))
2827     return std::make_pair(Q_None, PointerAffinity::RValueReference);
2828 
2829   switch (MangledName.popFront()) {
2830   case 'A':
2831     return std::make_pair(Q_None, PointerAffinity::Reference);
2832   case 'P':
2833     return std::make_pair(Q_None, PointerAffinity::Pointer);
2834   case 'Q':
2835     return std::make_pair(Q_Const, PointerAffinity::Pointer);
2836   case 'R':
2837     return std::make_pair(Q_Volatile, PointerAffinity::Pointer);
2838   case 'S':
2839     return std::make_pair(Qualifiers(Q_Const | Q_Volatile),
2840                           PointerAffinity::Pointer);
2841   default:
2842     assert(false && "Ty is not a pointer type!");
2843   }
2844   return std::make_pair(Q_None, PointerAffinity::Pointer);
2845 }
2846 
2847 // <pointer-type> ::= E? <pointer-cvr-qualifiers> <ext-qualifiers> <type>
2848 //                       # the E is required for 64-bit non-static pointers
2849 PointerType *Demangler::demanglePointerType(StringView &MangledName) {
2850   PointerType *Pointer = Arena.alloc<PointerType>();
2851 
2852   std::tie(Pointer->Quals, Pointer->Affinity) =
2853       demanglePointerCVQualifiers(MangledName);
2854 
2855   Pointer->Prim = PrimTy::Ptr;
2856   if (MangledName.consumeFront("6")) {
2857     Pointer->Pointee = demangleFunctionType(MangledName, false, true);
2858     return Pointer;
2859   }
2860 
2861   Qualifiers ExtQuals = demanglePointerExtQualifiers(MangledName);
2862   Pointer->Quals = Qualifiers(Pointer->Quals | ExtQuals);
2863 
2864   Pointer->Pointee = demangleType(MangledName, QualifierMangleMode::Mangle);
2865   return Pointer;
2866 }
2867 
2868 MemberPointerType *
2869 Demangler::demangleMemberPointerType(StringView &MangledName) {
2870   MemberPointerType *Pointer = Arena.alloc<MemberPointerType>();
2871   Pointer->Prim = PrimTy::MemberPtr;
2872 
2873   PointerAffinity Affinity;
2874   std::tie(Pointer->Quals, Affinity) = demanglePointerCVQualifiers(MangledName);
2875   assert(Affinity == PointerAffinity::Pointer);
2876 
2877   Qualifiers ExtQuals = demanglePointerExtQualifiers(MangledName);
2878   Pointer->Quals = Qualifiers(Pointer->Quals | ExtQuals);
2879 
2880   if (MangledName.consumeFront("8")) {
2881     Pointer->MemberName = demangleFullyQualifiedSymbolName(MangledName);
2882     Pointer->Pointee = demangleFunctionType(MangledName, true, true);
2883   } else {
2884     Qualifiers PointeeQuals = Q_None;
2885     bool IsMember = false;
2886     std::tie(PointeeQuals, IsMember) = demangleQualifiers(MangledName);
2887     assert(IsMember);
2888     Pointer->MemberName = demangleFullyQualifiedSymbolName(MangledName);
2889 
2890     Pointer->Pointee = demangleType(MangledName, QualifierMangleMode::Drop);
2891     Pointer->Pointee->Quals = PointeeQuals;
2892   }
2893 
2894   return Pointer;
2895 }
2896 
2897 Qualifiers Demangler::demanglePointerExtQualifiers(StringView &MangledName) {
2898   Qualifiers Quals = Q_None;
2899   if (MangledName.consumeFront('E'))
2900     Quals = Qualifiers(Quals | Q_Pointer64);
2901   if (MangledName.consumeFront('I'))
2902     Quals = Qualifiers(Quals | Q_Restrict);
2903   if (MangledName.consumeFront('F'))
2904     Quals = Qualifiers(Quals | Q_Unaligned);
2905 
2906   return Quals;
2907 }
2908 
2909 ArrayType *Demangler::demangleArrayType(StringView &MangledName) {
2910   assert(MangledName.front() == 'Y');
2911   MangledName.popFront();
2912 
2913   uint64_t Rank = 0;
2914   bool IsNegative = false;
2915   std::tie(Rank, IsNegative) = demangleNumber(MangledName);
2916   if (IsNegative || Rank == 0) {
2917     Error = true;
2918     return nullptr;
2919   }
2920 
2921   ArrayType *ATy = Arena.alloc<ArrayType>();
2922   ATy->Prim = PrimTy::Array;
2923   ATy->Dims = Arena.alloc<ArrayDimension>();
2924   ArrayDimension *Dim = ATy->Dims;
2925   for (uint64_t I = 0; I < Rank; ++I) {
2926     std::tie(Dim->Dim, IsNegative) = demangleNumber(MangledName);
2927     if (IsNegative) {
2928       Error = true;
2929       return nullptr;
2930     }
2931     if (I + 1 < Rank) {
2932       Dim->Next = Arena.alloc<ArrayDimension>();
2933       Dim = Dim->Next;
2934     }
2935   }
2936 
2937   if (MangledName.consumeFront("$$C")) {
2938     bool IsMember = false;
2939     std::tie(ATy->Quals, IsMember) = demangleQualifiers(MangledName);
2940     if (IsMember) {
2941       Error = true;
2942       return nullptr;
2943     }
2944   }
2945 
2946   ATy->ElementType = demangleType(MangledName, QualifierMangleMode::Drop);
2947   return ATy;
2948 }
2949 
2950 // Reads a function or a template parameters.
2951 FunctionParams
2952 Demangler::demangleFunctionParameterList(StringView &MangledName) {
2953   // Empty parameter list.
2954   if (MangledName.consumeFront('X'))
2955     return {};
2956 
2957   FunctionParams *Head;
2958   FunctionParams **Current = &Head;
2959   while (!Error && !MangledName.startsWith('@') &&
2960          !MangledName.startsWith('Z')) {
2961 
2962     if (startsWithDigit(MangledName)) {
2963       size_t N = MangledName[0] - '0';
2964       if (N >= Backrefs.FunctionParamCount) {
2965         Error = true;
2966         return {};
2967       }
2968       MangledName = MangledName.dropFront();
2969 
2970       *Current = Arena.alloc<FunctionParams>();
2971       (*Current)->Current = Backrefs.FunctionParams[N]->clone(Arena);
2972       Current = &(*Current)->Next;
2973       continue;
2974     }
2975 
2976     size_t OldSize = MangledName.size();
2977 
2978     *Current = Arena.alloc<FunctionParams>();
2979     (*Current)->Current = demangleType(MangledName, QualifierMangleMode::Drop);
2980 
2981     size_t CharsConsumed = OldSize - MangledName.size();
2982     assert(CharsConsumed != 0);
2983 
2984     // Single-letter types are ignored for backreferences because memorizing
2985     // them doesn't save anything.
2986     if (Backrefs.FunctionParamCount <= 9 && CharsConsumed > 1)
2987       Backrefs.FunctionParams[Backrefs.FunctionParamCount++] =
2988           (*Current)->Current;
2989 
2990     Current = &(*Current)->Next;
2991   }
2992 
2993   if (Error)
2994     return {};
2995 
2996   // A non-empty parameter list is terminated by either 'Z' (variadic) parameter
2997   // list or '@' (non variadic).  Careful not to consume "@Z", as in that case
2998   // the following Z could be a throw specifier.
2999   if (MangledName.consumeFront('@'))
3000     return *Head;
3001 
3002   if (MangledName.consumeFront('Z')) {
3003     Head->IsVariadic = true;
3004     return *Head;
3005   }
3006 
3007   Error = true;
3008   return {};
3009 }
3010 
3011 TemplateParams *
3012 Demangler::demangleTemplateParameterList(StringView &MangledName) {
3013   TemplateParams *Head;
3014   TemplateParams **Current = &Head;
3015   while (!Error && !MangledName.startsWith('@')) {
3016     // Template parameter lists don't participate in back-referencing.
3017     *Current = Arena.alloc<TemplateParams>();
3018 
3019     TemplateParams &TP = **Current;
3020 
3021     // Empty parameter pack.
3022     if (MangledName.consumeFront("$S") || MangledName.consumeFront("$$V") ||
3023         MangledName.consumeFront("$$$V")) {
3024       TP.IsEmptyParameterPack = true;
3025     } else if (MangledName.consumeFront("$$Y")) {
3026       // Template alias
3027       TP.IsTemplateTemplate = true;
3028       TP.IsAliasTemplate = true;
3029       TP.ParamName = demangleFullyQualifiedTypeName(MangledName);
3030     } else if (MangledName.consumeFront("$$B")) {
3031       // Array
3032       TP.ParamType = demangleType(MangledName, QualifierMangleMode::Drop);
3033     } else if (MangledName.consumeFront("$$C")) {
3034       // Type has qualifiers.
3035       TP.ParamType = demangleType(MangledName, QualifierMangleMode::Mangle);
3036     } else if (MangledName.startsWith("$1") || MangledName.startsWith("$H") ||
3037                MangledName.startsWith("$I") || MangledName.startsWith("$J")) {
3038       MangledName = MangledName.dropFront();
3039       // 1 - single inheritance       <name>
3040       // H - multiple inheritance     <name> <number>
3041       // I - virtual inheritance      <name> <number> <number> <number>
3042       // J - unspecified inheritance  <name> <number> <number> <number>
3043       char InheritanceSpecifier = MangledName.popFront();
3044       // Pointer to member
3045       Symbol *S = MangledName.startsWith('?') ? parse(MangledName) : nullptr;
3046       switch (InheritanceSpecifier) {
3047       case 'J':
3048         TP.ThunkOffsets[TP.ThunkOffsetCount++] = demangleSigned(MangledName);
3049         LLVM_FALLTHROUGH;
3050       case 'I':
3051         TP.ThunkOffsets[TP.ThunkOffsetCount++] = demangleSigned(MangledName);
3052         LLVM_FALLTHROUGH;
3053       case 'H':
3054         TP.ThunkOffsets[TP.ThunkOffsetCount++] = demangleSigned(MangledName);
3055         LLVM_FALLTHROUGH;
3056       case '1':
3057         break;
3058       default:
3059         Error = true;
3060         break;
3061       }
3062       TP.PointerToSymbol = true;
3063       if (S) {
3064         TP.ParamName = S->SymbolName;
3065         TP.ParamType = S->SymbolType;
3066       } else
3067         TP.NullptrLiteral = true;
3068     } else if (MangledName.startsWith("$E?")) {
3069       MangledName.consumeFront("$E");
3070       // Reference to symbol
3071       Symbol *S = parse(MangledName);
3072       TP.ParamName = S->SymbolName;
3073       TP.ParamType = S->SymbolType;
3074       TP.ReferenceToSymbol = true;
3075     } else if (MangledName.startsWith("$F") || MangledName.startsWith("$G")) {
3076       // Data member pointer.
3077       MangledName = MangledName.dropFront();
3078       char InheritanceSpecifier = MangledName.popFront();
3079 
3080       switch (InheritanceSpecifier) {
3081       case 'G':
3082         TP.ThunkOffsets[TP.ThunkOffsetCount++] = demangleSigned(MangledName);
3083         LLVM_FALLTHROUGH;
3084       case 'F':
3085         TP.ThunkOffsets[TP.ThunkOffsetCount++] = demangleSigned(MangledName);
3086         TP.ThunkOffsets[TP.ThunkOffsetCount++] = demangleSigned(MangledName);
3087         LLVM_FALLTHROUGH;
3088       case '0':
3089         break;
3090       default:
3091         Error = true;
3092         break;
3093       }
3094       TP.DataMemberPointer = true;
3095 
3096     } else if (MangledName.consumeFront("$0")) {
3097       // Integral non-type template parameter
3098       bool IsNegative = false;
3099       uint64_t Value = 0;
3100       std::tie(Value, IsNegative) = demangleNumber(MangledName);
3101 
3102       TP.IsIntegerLiteral = true;
3103       TP.IntegerLiteralIsNegative = IsNegative;
3104       TP.IntegralValue = Value;
3105     } else {
3106       TP.ParamType = demangleType(MangledName, QualifierMangleMode::Drop);
3107     }
3108     if (Error)
3109       return nullptr;
3110 
3111     Current = &TP.Next;
3112   }
3113 
3114   if (Error)
3115     return nullptr;
3116 
3117   // Template parameter lists cannot be variadic, so it can only be terminated
3118   // by @.
3119   if (MangledName.consumeFront('@'))
3120     return Head;
3121   Error = true;
3122   return nullptr;
3123 }
3124 
3125 void Demangler::output(const Symbol *S, OutputStream &OS) {
3126   if (S->Category == SymbolCategory::Unknown) {
3127     outputName(OS, S->SymbolName, S->SymbolType);
3128     return;
3129   }
3130 
3131   if (S->Category == SymbolCategory::SpecialOperator) {
3132     outputSpecialOperator(OS, S->SymbolName);
3133     return;
3134   }
3135 
3136   // Converts an AST to a string.
3137   //
3138   // Converting an AST representing a C++ type to a string is tricky due
3139   // to the bad grammar of the C++ declaration inherited from C. You have
3140   // to construct a string from inside to outside. For example, if a type
3141   // X is a pointer to a function returning int, the order you create a
3142   // string becomes something like this:
3143   //
3144   //   (1) X is a pointer: *X
3145   //   (2) (1) is a function returning int: int (*X)()
3146   //
3147   // So you cannot construct a result just by appending strings to a result.
3148   //
3149   // To deal with this, we split the function into two. outputPre() writes
3150   // the "first half" of type declaration, and outputPost() writes the
3151   // "second half". For example, outputPre() writes a return type for a
3152   // function and outputPost() writes an parameter list.
3153   if (S->SymbolType) {
3154     Type::outputPre(OS, *S->SymbolType);
3155     outputName(OS, S->SymbolName, S->SymbolType);
3156     Type::outputPost(OS, *S->SymbolType);
3157   } else {
3158     outputQualifiers(OS, S->SymbolQuals);
3159     outputName(OS, S->SymbolName, nullptr);
3160   }
3161 }
3162 
3163 void Demangler::dumpBackReferences() {
3164   std::printf("%d function parameter backreferences\n",
3165               (int)Backrefs.FunctionParamCount);
3166 
3167   // Create an output stream so we can render each type.
3168   OutputStream OS = OutputStream::create(nullptr, 0, 1024);
3169   for (size_t I = 0; I < Backrefs.FunctionParamCount; ++I) {
3170     OS.setCurrentPosition(0);
3171 
3172     Type *T = Backrefs.FunctionParams[I];
3173     Type::outputPre(OS, *T);
3174     Type::outputPost(OS, *T);
3175 
3176     std::printf("  [%d] - %.*s\n", (int)I, (int)OS.getCurrentPosition(),
3177                 OS.getBuffer());
3178   }
3179   std::free(OS.getBuffer());
3180 
3181   if (Backrefs.FunctionParamCount > 0)
3182     std::printf("\n");
3183   std::printf("%d name backreferences\n", (int)Backrefs.NamesCount);
3184   for (size_t I = 0; I < Backrefs.NamesCount; ++I) {
3185     std::printf("  [%d] - %.*s\n", (int)I, (int)Backrefs.Names[I].size(),
3186                 Backrefs.Names[I].begin());
3187   }
3188   if (Backrefs.NamesCount > 0)
3189     std::printf("\n");
3190 }
3191 
3192 char *llvm::microsoftDemangle(const char *MangledName, char *Buf, size_t *N,
3193                               int *Status, MSDemangleFlags Flags) {
3194   Demangler D;
3195   StringView Name{MangledName};
3196   Symbol *S = D.parse(Name);
3197 
3198   if (Flags & MSDF_DumpBackrefs)
3199     D.dumpBackReferences();
3200   OutputStream OS = OutputStream::create(Buf, N, 1024);
3201   if (D.Error) {
3202     OS << MangledName;
3203     *Status = llvm::demangle_invalid_mangled_name;
3204   } else {
3205     D.output(S, OS);
3206     *Status = llvm::demangle_success;
3207   }
3208 
3209   OS << '\0';
3210   return OS.getBuffer();
3211 }
3212