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 "Compiler.h"
20 #include "StringView.h"
21 #include "Utility.h"
22 
23 #include <cctype>
24 #include <cstdio>
25 #include <tuple>
26 
27 // This memory allocator is extremely fast, but it doesn't call dtors
28 // for allocated objects. That means you can't use STL containers
29 // (such as std::vector) with this allocator. But it pays off --
30 // the demangler is 3x faster with this allocator compared to one with
31 // STL containers.
32 namespace {
33   constexpr size_t AllocUnit = 4096;
34 
35 class ArenaAllocator {
36   struct AllocatorNode {
37     uint8_t *Buf = nullptr;
38     size_t Used = 0;
39     size_t Capacity = 0;
40     AllocatorNode *Next = nullptr;
41   };
42 
43   void addNode(size_t Capacity) {
44     AllocatorNode *NewHead = new AllocatorNode;
45     NewHead->Buf = new uint8_t[Capacity];
46     NewHead->Next = Head;
47     NewHead->Capacity = Capacity;
48     Head = NewHead;
49     NewHead->Used = 0;
50   }
51 
52 public:
53   ArenaAllocator() { addNode(AllocUnit); }
54 
55   ~ArenaAllocator() {
56     while (Head) {
57       assert(Head->Buf);
58       delete[] Head->Buf;
59       AllocatorNode *Next = Head->Next;
60       delete Head;
61       Head = Next;
62     }
63   }
64 
65   char *allocUnalignedBuffer(size_t Length) {
66     uint8_t *Buf = Head->Buf + Head->Used;
67 
68     Head->Used += Length;
69     if (Head->Used > Head->Capacity) {
70       // It's possible we need a buffer which is larger than our default unit
71       // size, so we need to be careful to add a node with capacity that is at
72       // least as large as what we need.
73       addNode(std::max(AllocUnit, Length));
74       Head->Used = Length;
75       Buf = Head->Buf;
76     }
77 
78     return reinterpret_cast<char *>(Buf);
79   }
80 
81   template <typename T, typename... Args> T *alloc(Args &&... ConstructorArgs) {
82 
83     size_t Size = sizeof(T);
84     assert(Head && Head->Buf);
85 
86     size_t P = (size_t)Head->Buf + Head->Used;
87     uintptr_t AlignedP =
88         (((size_t)P + alignof(T) - 1) & ~(size_t)(alignof(T) - 1));
89     uint8_t *PP = (uint8_t *)AlignedP;
90     size_t Adjustment = AlignedP - P;
91 
92     Head->Used += Size + Adjustment;
93     if (Head->Used < Head->Capacity)
94       return new (PP) T(std::forward<Args>(ConstructorArgs)...);
95 
96     addNode(AllocUnit);
97     Head->Used = Size;
98     return new (Head->Buf) T(std::forward<Args>(ConstructorArgs)...);
99   }
100 
101 private:
102   AllocatorNode *Head = nullptr;
103 };
104 } // namespace
105 
106 static bool startsWithDigit(StringView S) {
107   return !S.empty() && std::isdigit(S.front());
108 }
109 
110 // Writes a space if the last token does not end with a punctuation.
111 static void outputSpaceIfNecessary(OutputStream &OS) {
112   if (OS.empty())
113     return;
114 
115   char C = OS.back();
116   if (isalnum(C) || C == '>')
117     OS << " ";
118 }
119 
120 // Storage classes
121 enum Qualifiers : uint8_t {
122   Q_None = 0,
123   Q_Const = 1 << 0,
124   Q_Volatile = 1 << 1,
125   Q_Far = 1 << 2,
126   Q_Huge = 1 << 3,
127   Q_Unaligned = 1 << 4,
128   Q_Restrict = 1 << 5,
129   Q_Pointer64 = 1 << 6
130 };
131 
132 enum class StorageClass : uint8_t {
133   None,
134   PrivateStatic,
135   ProtectedStatic,
136   PublicStatic,
137   Global,
138   FunctionLocalStatic
139 };
140 
141 enum class QualifierMangleMode { Drop, Mangle, Result };
142 
143 enum class PointerAffinity { Pointer, Reference, RValueReference };
144 
145 // Calling conventions
146 enum class CallingConv : uint8_t {
147   None,
148   Cdecl,
149   Pascal,
150   Thiscall,
151   Stdcall,
152   Fastcall,
153   Clrcall,
154   Eabi,
155   Vectorcall,
156   Regcall,
157 };
158 
159 enum class ReferenceKind : uint8_t { None, LValueRef, RValueRef };
160 
161 // Types
162 enum class PrimTy : uint8_t {
163   Unknown,
164   None,
165   Function,
166   Ptr,
167   MemberPtr,
168   Array,
169 
170   Struct,
171   Union,
172   Class,
173   Enum,
174 
175   Void,
176   Bool,
177   Char,
178   Schar,
179   Uchar,
180   Char16,
181   Char32,
182   Short,
183   Ushort,
184   Int,
185   Uint,
186   Long,
187   Ulong,
188   Int64,
189   Uint64,
190   Wchar,
191   Float,
192   Double,
193   Ldouble,
194   Nullptr
195 };
196 
197 // Function classes
198 enum FuncClass : uint8_t {
199   Public = 1 << 0,
200   Protected = 1 << 1,
201   Private = 1 << 2,
202   Global = 1 << 3,
203   Static = 1 << 4,
204   Virtual = 1 << 5,
205   Far = 1 << 6,
206 };
207 
208 enum class SymbolCategory { Function, Variable };
209 
210 namespace {
211 
212 struct NameResolver {
213   virtual ~NameResolver() = default;
214   virtual StringView resolve(StringView S) = 0;
215 };
216 
217 struct Type;
218 struct Name;
219 
220 struct FunctionParams {
221   bool IsVariadic = false;
222 
223   Type *Current = nullptr;
224 
225   FunctionParams *Next = nullptr;
226 };
227 
228 struct TemplateParams {
229   bool IsTemplateTemplate = false;
230   bool IsAliasTemplate = false;
231 
232   // Type can be null if this is a template template parameter.  In that case
233   // only Name will be valid.
234   Type *ParamType = nullptr;
235 
236   // Name can be valid if this is a template template parameter (see above) or
237   // this is a function declaration (e.g. foo<&SomeFunc>).  In the latter case
238   // Name contains the name of the function and Type contains the signature.
239   Name *ParamName = nullptr;
240 
241   TemplateParams *Next = nullptr;
242 };
243 
244 // The type class. Mangled symbols are first parsed and converted to
245 // this type and then converted to string.
246 struct Type {
247   virtual ~Type() {}
248 
249   virtual Type *clone(ArenaAllocator &Arena) const;
250 
251   // Write the "first half" of a given type.  This is a static functions to
252   // give the code a chance to do processing that is common to a subset of
253   // subclasses
254   static void outputPre(OutputStream &OS, Type &Ty, NameResolver &Resolver);
255 
256   // Write the "second half" of a given type.  This is a static functions to
257   // give the code a chance to do processing that is common to a subset of
258   // subclasses
259   static void outputPost(OutputStream &OS, Type &Ty, NameResolver &Resolver);
260 
261   virtual void outputPre(OutputStream &OS, NameResolver &Resolver);
262   virtual void outputPost(OutputStream &OS, NameResolver &Resolver);
263 
264   // Primitive type such as Int.
265   PrimTy Prim = PrimTy::Unknown;
266 
267   Qualifiers Quals = Q_None;
268   StorageClass Storage = StorageClass::None; // storage class
269 };
270 
271 // Represents an identifier which may be a template.
272 struct Name {
273   // Name read from an MangledName string.
274   StringView Str;
275 
276   bool IsTemplateInstantiation = false;
277   bool IsOperator = false;
278   bool IsBackReference = false;
279 
280   // Template parameters. Only valid if Flags contains NF_TemplateInstantiation.
281   TemplateParams *TParams = nullptr;
282 
283   // Nested BackReferences (e.g. "A::B::C") are represented as a linked list.
284   Name *Next = nullptr;
285 };
286 
287 struct PointerType : public Type {
288   Type *clone(ArenaAllocator &Arena) const override;
289   void outputPre(OutputStream &OS, NameResolver &Resolver) override;
290   void outputPost(OutputStream &OS, NameResolver &Resolver) override;
291 
292   PointerAffinity Affinity;
293 
294   // Represents a type X in "a pointer to X", "a reference to X",
295   // "an array of X", or "a function returning X".
296   Type *Pointee = nullptr;
297 };
298 
299 struct MemberPointerType : public Type {
300   Type *clone(ArenaAllocator &Arena) const override;
301   void outputPre(OutputStream &OS, NameResolver &Resolver) override;
302   void outputPost(OutputStream &OS, NameResolver &Resolver) override;
303 
304   Name *MemberName = nullptr;
305 
306   // Represents a type X in "a pointer to X", "a reference to X",
307   // "an array of X", or "a function returning X".
308   Type *Pointee = nullptr;
309 };
310 
311 struct FunctionType : public Type {
312   Type *clone(ArenaAllocator &Arena) const override;
313   void outputPre(OutputStream &OS, NameResolver &Resolver) override;
314   void outputPost(OutputStream &OS, NameResolver &Resolver) override;
315 
316   // True if this FunctionType instance is the Pointee of a PointerType or
317   // MemberPointerType.
318   bool IsFunctionPointer = false;
319 
320   Type *ReturnType = nullptr;
321   // If this is a reference, the type of reference.
322   ReferenceKind RefKind;
323 
324   CallingConv CallConvention;
325   FuncClass FunctionClass;
326 
327   FunctionParams Params;
328 };
329 
330 struct UdtType : public Type {
331   Type *clone(ArenaAllocator &Arena) const override;
332   void outputPre(OutputStream &OS, NameResolver &Resolver) override;
333 
334   Name *UdtName = nullptr;
335 };
336 
337 struct ArrayType : public Type {
338   Type *clone(ArenaAllocator &Arena) const override;
339   void outputPre(OutputStream &OS, NameResolver &Resolver) override;
340   void outputPost(OutputStream &OS, NameResolver &Resolver) override;
341 
342   // Either NextDimension or ElementType will be valid.
343   ArrayType *NextDimension = nullptr;
344   uint32_t ArrayDimension = 0;
345 
346   Type *ElementType = nullptr;
347 };
348 
349 } // namespace
350 
351 static bool isMemberPointer(StringView MangledName) {
352   switch (MangledName.popFront()) {
353   case '$':
354     // This is probably an rvalue reference (e.g. $$Q), and you cannot have an
355     // rvalue reference to a member.
356     return false;
357   case 'A':
358     // 'A' indicates a reference, and you cannot have a reference to a member
359     // function or member.
360     return false;
361   case 'P':
362   case 'Q':
363   case 'R':
364   case 'S':
365     // These 4 values indicate some kind of pointer, but we still don't know
366     // what.
367     break;
368   default:
369     assert(false && "Ty is not a pointer type!");
370   }
371 
372   // If it starts with a number, then 6 indicates a non-member function
373   // pointer, and 8 indicates a member function pointer.
374   if (startsWithDigit(MangledName)) {
375     assert(MangledName[0] == '6' || MangledName[0] == '8');
376     return (MangledName[0] == '8');
377   }
378 
379   // Remove ext qualifiers since those can appear on either type and are
380   // therefore not indicative.
381   MangledName.consumeFront('E'); // 64-bit
382   MangledName.consumeFront('I'); // restrict
383   MangledName.consumeFront('F'); // unaligned
384 
385   assert(!MangledName.empty());
386 
387   // The next value should be either ABCD (non-member) or QRST (member).
388   switch (MangledName.front()) {
389   case 'A':
390   case 'B':
391   case 'C':
392   case 'D':
393     return false;
394   case 'Q':
395   case 'R':
396   case 'S':
397   case 'T':
398     return true;
399   default:
400     assert(false);
401   }
402   return false;
403 }
404 
405 static void outputCallingConvention(OutputStream &OS, CallingConv CC) {
406   outputSpaceIfNecessary(OS);
407 
408   switch (CC) {
409   case CallingConv::Cdecl:
410     OS << "__cdecl";
411     break;
412   case CallingConv::Fastcall:
413     OS << "__fastcall";
414     break;
415   case CallingConv::Pascal:
416     OS << "__pascal";
417     break;
418   case CallingConv::Regcall:
419     OS << "__regcall";
420     break;
421   case CallingConv::Stdcall:
422     OS << "__stdcall";
423     break;
424   case CallingConv::Thiscall:
425     OS << "__thiscall";
426     break;
427   case CallingConv::Eabi:
428     OS << "__eabi";
429     break;
430   case CallingConv::Vectorcall:
431     OS << "__vectorcall";
432     break;
433   case CallingConv::Clrcall:
434     OS << "__clrcall";
435     break;
436   default:
437     break;
438   }
439 }
440 
441 static bool startsWithLocalScopePattern(StringView S) {
442   if (!S.consumeFront('?'))
443     return false;
444   if (S.size() < 2)
445     return false;
446 
447   size_t End = S.find('?');
448   if (End == StringView::npos)
449     return false;
450   StringView Candidate = S.substr(0, End);
451   if (Candidate.empty())
452     return false;
453 
454   // \?[0-9]\?
455   // ?@? is the discriminator 0.
456   if (Candidate.size() == 1)
457     return Candidate[0] == '@' || (Candidate[0] >= '0' && Candidate[0] <= '9');
458 
459   // If it's not 0-9, then it's an encoded number terminated with an @
460   if (Candidate.back() != '@')
461     return false;
462   Candidate = Candidate.dropBack();
463 
464   // An encoded number starts with B-P and all subsequent digits are in A-P.
465   // Note that the reason the first digit cannot be A is two fold.  First, it
466   // would create an ambiguity with ?A which delimits the beginning of an
467   // anonymous namespace.  Second, A represents 0, and you don't start a multi
468   // digit number with a leading 0.  Presumably the anonymous namespace
469   // ambiguity is also why single digit encoded numbers use 0-9 rather than A-J.
470   if (Candidate[0] < 'B' || Candidate[0] > 'P')
471     return false;
472   Candidate = Candidate.dropFront();
473   while (!Candidate.empty()) {
474     if (Candidate[0] < 'A' || Candidate[0] > 'P')
475       return false;
476     Candidate = Candidate.dropFront();
477   }
478 
479   return true;
480 }
481 
482 // Write a function or template parameter list.
483 static void outputParameterList(OutputStream &OS, const FunctionParams &Params,
484                                 NameResolver &Resolver) {
485   if (!Params.Current) {
486     OS << "void";
487     return;
488   }
489 
490   const FunctionParams *Head = &Params;
491   while (Head) {
492     Type::outputPre(OS, *Head->Current, Resolver);
493     Type::outputPost(OS, *Head->Current, Resolver);
494 
495     Head = Head->Next;
496 
497     if (Head)
498       OS << ", ";
499   }
500 }
501 
502 static void outputName(OutputStream &OS, const Name *TheName,
503                        NameResolver &Resolver);
504 
505 static void outputParameterList(OutputStream &OS, const TemplateParams &Params,
506                                 NameResolver &Resolver) {
507   if (!Params.ParamType && !Params.ParamName) {
508     OS << "<>";
509     return;
510   }
511 
512   OS << "<";
513   const TemplateParams *Head = &Params;
514   while (Head) {
515     // Type can be null if this is a template template parameter,
516     // and Name can be null if this is a simple type.
517 
518     if (Head->ParamType && Head->ParamName) {
519       // Function pointer.
520       OS << "&";
521       Type::outputPre(OS, *Head->ParamType, Resolver);
522       outputName(OS, Head->ParamName, Resolver);
523       Type::outputPost(OS, *Head->ParamType, Resolver);
524     } else if (Head->ParamType) {
525       // simple type.
526       Type::outputPre(OS, *Head->ParamType, Resolver);
527       Type::outputPost(OS, *Head->ParamType, Resolver);
528     } else {
529       // Template alias.
530       outputName(OS, Head->ParamName, Resolver);
531     }
532 
533     Head = Head->Next;
534 
535     if (Head)
536       OS << ", ";
537   }
538   OS << ">";
539 }
540 
541 static void outputNameComponent(OutputStream &OS, const Name &N,
542                                 NameResolver &Resolver) {
543   StringView S = N.Str;
544 
545   if (N.IsBackReference)
546     S = Resolver.resolve(N.Str);
547   OS << S;
548 
549   if (N.IsTemplateInstantiation)
550     outputParameterList(OS, *N.TParams, Resolver);
551 }
552 
553 static void outputName(OutputStream &OS, const Name *TheName,
554                        NameResolver &Resolver) {
555   if (!TheName)
556     return;
557 
558   outputSpaceIfNecessary(OS);
559 
560   const Name *Previous = nullptr;
561   // Print out namespaces or outer class BackReferences.
562   for (; TheName->Next; TheName = TheName->Next) {
563     Previous = TheName;
564     outputNameComponent(OS, *TheName, Resolver);
565     OS << "::";
566   }
567 
568   // Print out a regular name.
569   if (!TheName->IsOperator) {
570     outputNameComponent(OS, *TheName, Resolver);
571     return;
572   }
573 
574   // Print out ctor or dtor.
575   if (TheName->Str == "dtor")
576     OS << "~";
577 
578   if (TheName->Str == "ctor" || TheName->Str == "dtor") {
579     outputNameComponent(OS, *Previous, Resolver);
580     return;
581   }
582 
583   // Print out an overloaded operator.
584   OS << "operator";
585   outputNameComponent(OS, *TheName, Resolver);
586 }
587 
588 namespace {
589 
590 Type *Type::clone(ArenaAllocator &Arena) const {
591   return Arena.alloc<Type>(*this);
592 }
593 
594 // Write the "first half" of a given type.
595 void Type::outputPre(OutputStream &OS, Type &Ty, NameResolver &Resolver) {
596   // Function types require custom handling of const and static so we
597   // handle them separately.  All other types use the same decoration
598   // for these modifiers, so handle them here in common code.
599   if (Ty.Prim == PrimTy::Function) {
600     Ty.outputPre(OS, Resolver);
601     return;
602   }
603 
604   switch (Ty.Storage) {
605   case StorageClass::PrivateStatic:
606   case StorageClass::PublicStatic:
607   case StorageClass::ProtectedStatic:
608     OS << "static ";
609   default:
610     break;
611   }
612   Ty.outputPre(OS, Resolver);
613 
614   if (Ty.Quals & Q_Const) {
615     outputSpaceIfNecessary(OS);
616     OS << "const";
617   }
618 
619   if (Ty.Quals & Q_Volatile) {
620     outputSpaceIfNecessary(OS);
621     OS << "volatile";
622   }
623 
624   if (Ty.Quals & Q_Restrict) {
625     outputSpaceIfNecessary(OS);
626     OS << "__restrict";
627   }
628 }
629 
630 // Write the "second half" of a given type.
631 void Type::outputPost(OutputStream &OS, Type &Ty, NameResolver &Resolver) {
632   Ty.outputPost(OS, Resolver);
633 }
634 
635 void Type::outputPre(OutputStream &OS, NameResolver &Resolver) {
636   switch (Prim) {
637   case PrimTy::Void:
638     OS << "void";
639     break;
640   case PrimTy::Bool:
641     OS << "bool";
642     break;
643   case PrimTy::Char:
644     OS << "char";
645     break;
646   case PrimTy::Schar:
647     OS << "signed char";
648     break;
649   case PrimTy::Uchar:
650     OS << "unsigned char";
651     break;
652   case PrimTy::Char16:
653     OS << "char16_t";
654     break;
655   case PrimTy::Char32:
656     OS << "char32_t";
657     break;
658   case PrimTy::Short:
659     OS << "short";
660     break;
661   case PrimTy::Ushort:
662     OS << "unsigned short";
663     break;
664   case PrimTy::Int:
665     OS << "int";
666     break;
667   case PrimTy::Uint:
668     OS << "unsigned int";
669     break;
670   case PrimTy::Long:
671     OS << "long";
672     break;
673   case PrimTy::Ulong:
674     OS << "unsigned long";
675     break;
676   case PrimTy::Int64:
677     OS << "__int64";
678     break;
679   case PrimTy::Uint64:
680     OS << "unsigned __int64";
681     break;
682   case PrimTy::Wchar:
683     OS << "wchar_t";
684     break;
685   case PrimTy::Float:
686     OS << "float";
687     break;
688   case PrimTy::Double:
689     OS << "double";
690     break;
691   case PrimTy::Ldouble:
692     OS << "long double";
693     break;
694   case PrimTy::Nullptr:
695     OS << "std::nullptr_t";
696     break;
697   default:
698     assert(false && "Invalid primitive type!");
699   }
700 }
701 void Type::outputPost(OutputStream &OS, NameResolver &Resolver) {}
702 
703 Type *PointerType::clone(ArenaAllocator &Arena) const {
704   return Arena.alloc<PointerType>(*this);
705 }
706 
707 static void outputPointerIndicator(OutputStream &OS, PointerAffinity Affinity,
708                                    const Name *MemberName, const Type *Pointee,
709                                    NameResolver &Resolver) {
710   // "[]" and "()" (for function parameters) take precedence over "*",
711   // so "int *x(int)" means "x is a function returning int *". We need
712   // parentheses to supercede the default precedence. (e.g. we want to
713   // emit something like "int (*x)(int)".)
714   if (Pointee->Prim == PrimTy::Function || Pointee->Prim == PrimTy::Array) {
715     OS << "(";
716     if (Pointee->Prim == PrimTy::Function) {
717       const FunctionType *FTy = static_cast<const FunctionType *>(Pointee);
718       assert(FTy->IsFunctionPointer);
719       outputCallingConvention(OS, FTy->CallConvention);
720       OS << " ";
721     }
722   }
723 
724   if (MemberName) {
725     outputName(OS, MemberName, Resolver);
726     OS << "::";
727   }
728 
729   if (Affinity == PointerAffinity::Pointer)
730     OS << "*";
731   else if (Affinity == PointerAffinity::Reference)
732     OS << "&";
733   else
734     OS << "&&";
735 }
736 
737 void PointerType::outputPre(OutputStream &OS, NameResolver &Resolver) {
738   Type::outputPre(OS, *Pointee, Resolver);
739 
740   outputSpaceIfNecessary(OS);
741 
742   if (Quals & Q_Unaligned)
743     OS << "__unaligned ";
744 
745   outputPointerIndicator(OS, Affinity, nullptr, Pointee, Resolver);
746 
747   // FIXME: We should output this, but it requires updating lots of tests.
748   // if (Ty.Quals & Q_Pointer64)
749   //  OS << " __ptr64";
750 }
751 
752 void PointerType::outputPost(OutputStream &OS, NameResolver &Resolver) {
753   if (Pointee->Prim == PrimTy::Function || Pointee->Prim == PrimTy::Array)
754     OS << ")";
755 
756   Type::outputPost(OS, *Pointee, Resolver);
757 }
758 
759 Type *MemberPointerType::clone(ArenaAllocator &Arena) const {
760   return Arena.alloc<MemberPointerType>(*this);
761 }
762 
763 void MemberPointerType::outputPre(OutputStream &OS, NameResolver &Resolver) {
764   Type::outputPre(OS, *Pointee, Resolver);
765 
766   outputSpaceIfNecessary(OS);
767 
768   outputPointerIndicator(OS, PointerAffinity::Pointer, MemberName, Pointee,
769                          Resolver);
770 
771   // FIXME: We should output this, but it requires updating lots of tests.
772   // if (Ty.Quals & Q_Pointer64)
773   //  OS << " __ptr64";
774   if (Quals & Q_Restrict)
775     OS << " __restrict";
776 }
777 
778 void MemberPointerType::outputPost(OutputStream &OS, NameResolver &Resolver) {
779   if (Pointee->Prim == PrimTy::Function || Pointee->Prim == PrimTy::Array)
780     OS << ")";
781 
782   Type::outputPost(OS, *Pointee, Resolver);
783 }
784 
785 Type *FunctionType::clone(ArenaAllocator &Arena) const {
786   return Arena.alloc<FunctionType>(*this);
787 }
788 
789 void FunctionType::outputPre(OutputStream &OS, NameResolver &Resolver) {
790   if (!(FunctionClass & Global)) {
791     if (FunctionClass & Static)
792       OS << "static ";
793   }
794 
795   if (ReturnType) {
796     Type::outputPre(OS, *ReturnType, Resolver);
797     OS << " ";
798   }
799 
800   // Function pointers print the calling convention as void (__cdecl *)(params)
801   // rather than void __cdecl (*)(params).  So we need to let the PointerType
802   // class handle this.
803   if (!IsFunctionPointer)
804     outputCallingConvention(OS, CallConvention);
805 }
806 
807 void FunctionType::outputPost(OutputStream &OS, NameResolver &Resolver) {
808   OS << "(";
809   outputParameterList(OS, Params, Resolver);
810   OS << ")";
811   if (Quals & Q_Const)
812     OS << " const";
813   if (Quals & Q_Volatile)
814     OS << " volatile";
815   if (Quals & Q_Restrict)
816     OS << " __restrict";
817   if (Quals & Q_Unaligned)
818     OS << " __unaligned";
819 
820   if (RefKind == ReferenceKind::LValueRef)
821     OS << " &";
822   else if (RefKind == ReferenceKind::RValueRef)
823     OS << " &&";
824 
825   if (ReturnType)
826     Type::outputPost(OS, *ReturnType, Resolver);
827   return;
828 }
829 
830 Type *UdtType::clone(ArenaAllocator &Arena) const {
831   return Arena.alloc<UdtType>(*this);
832 }
833 
834 void UdtType::outputPre(OutputStream &OS, NameResolver &Resolver) {
835   switch (Prim) {
836   case PrimTy::Class:
837     OS << "class ";
838     break;
839   case PrimTy::Struct:
840     OS << "struct ";
841     break;
842   case PrimTy::Union:
843     OS << "union ";
844     break;
845   case PrimTy::Enum:
846     OS << "enum ";
847     break;
848   default:
849     assert(false && "Not a udt type!");
850   }
851 
852   outputName(OS, UdtName, Resolver);
853 }
854 
855 Type *ArrayType::clone(ArenaAllocator &Arena) const {
856   return Arena.alloc<ArrayType>(*this);
857 }
858 
859 void ArrayType::outputPre(OutputStream &OS, NameResolver &Resolver) {
860   Type::outputPre(OS, *ElementType, Resolver);
861 }
862 
863 void ArrayType::outputPost(OutputStream &OS, NameResolver &Resolver) {
864   if (ArrayDimension > 0)
865     OS << "[" << ArrayDimension << "]";
866   if (NextDimension)
867     Type::outputPost(OS, *NextDimension, Resolver);
868   else if (ElementType)
869     Type::outputPost(OS, *ElementType, Resolver);
870 }
871 
872 struct Symbol {
873   SymbolCategory Category;
874 
875   Name *SymbolName = nullptr;
876   Type *SymbolType = nullptr;
877 };
878 
879 } // namespace
880 
881 namespace {
882 
883 // Demangler class takes the main role in demangling symbols.
884 // It has a set of functions to parse mangled symbols into Type instances.
885 // It also has a set of functions to cnovert Type instances to strings.
886 class Demangler : public NameResolver {
887 public:
888   Demangler() = default;
889   virtual ~Demangler() = default;
890 
891   // You are supposed to call parse() first and then check if error is true.  If
892   // it is false, call output() to write the formatted name to the given stream.
893   Symbol *parse(StringView &MangledName);
894   void output(const Symbol *S, OutputStream &OS);
895 
896   StringView resolve(StringView N) override;
897 
898   // True if an error occurred.
899   bool Error = false;
900 
901   void dumpBackReferences();
902 
903 private:
904   Type *demangleVariableEncoding(StringView &MangledName);
905   Type *demangleFunctionEncoding(StringView &MangledName);
906 
907   Qualifiers demanglePointerExtQualifiers(StringView &MangledName);
908 
909   // Parser functions. This is a recursive-descent parser.
910   Type *demangleType(StringView &MangledName, QualifierMangleMode QMM);
911   Type *demangleBasicType(StringView &MangledName);
912   UdtType *demangleClassType(StringView &MangledName);
913   PointerType *demanglePointerType(StringView &MangledName);
914   MemberPointerType *demangleMemberPointerType(StringView &MangledName);
915   FunctionType *demangleFunctionType(StringView &MangledName, bool HasThisQuals,
916                                      bool IsFunctionPointer);
917 
918   ArrayType *demangleArrayType(StringView &MangledName);
919 
920   TemplateParams *demangleTemplateParameterList(StringView &MangledName);
921   FunctionParams demangleFunctionParameterList(StringView &MangledName);
922 
923   int demangleNumber(StringView &MangledName);
924 
925   void memorizeString(StringView s);
926 
927   /// Allocate a copy of \p Borrowed into memory that we own.
928   StringView copyString(StringView Borrowed);
929 
930   Name *demangleFullyQualifiedTypeName(StringView &MangledName);
931   Name *demangleFullyQualifiedSymbolName(StringView &MangledName);
932 
933   Name *demangleUnqualifiedTypeName(StringView &MangledName, bool Memorize);
934   Name *demangleUnqualifiedSymbolName(StringView &MangledName, bool Memorize);
935 
936   Name *demangleNameScopeChain(StringView &MangledName, Name *UnqualifiedName);
937   Name *demangleNameScopePiece(StringView &MangledName);
938 
939   Name *demangleBackRefName(StringView &MangledName);
940   Name *demangleTemplateInstantiationName(StringView &MangledName);
941   Name *demangleOperatorName(StringView &MangledName);
942   Name *demangleSimpleName(StringView &MangledName, bool Memorize);
943   Name *demangleAnonymousNamespaceName(StringView &MangledName);
944   Name *demangleLocallyScopedNamePiece(StringView &MangledName);
945 
946   StringView demangleSimpleString(StringView &MangledName, bool Memorize);
947 
948   FuncClass demangleFunctionClass(StringView &MangledName);
949   CallingConv demangleCallingConvention(StringView &MangledName);
950   StorageClass demangleVariableStorageClass(StringView &MangledName);
951   ReferenceKind demangleReferenceKind(StringView &MangledName);
952   void demangleThrowSpecification(StringView &MangledName);
953 
954   std::pair<Qualifiers, bool> demangleQualifiers(StringView &MangledName);
955 
956   // Memory allocator.
957   ArenaAllocator Arena;
958 
959   // A single type uses one global back-ref table for all function params.
960   // This means back-refs can even go "into" other types.  Examples:
961   //
962   //  // Second int* is a back-ref to first.
963   //  void foo(int *, int*);
964   //
965   //  // Second int* is not a back-ref to first (first is not a function param).
966   //  int* foo(int*);
967   //
968   //  // Second int* is a back-ref to first (ALL function types share the same
969   //  // back-ref map.
970   //  using F = void(*)(int*);
971   //  F G(int *);
972   Type *FunctionParamBackRefs[10];
973   size_t FunctionParamBackRefCount = 0;
974 
975   // The first 10 BackReferences in a mangled name can be back-referenced by
976   // special name @[0-9]. This is a storage for the first 10 BackReferences.
977   StringView BackReferences[10];
978   size_t BackRefCount = 0;
979 };
980 } // namespace
981 
982 StringView Demangler::copyString(StringView Borrowed) {
983   char *Stable = Arena.allocUnalignedBuffer(Borrowed.size() + 1);
984   std::strcpy(Stable, Borrowed.begin());
985 
986   return {Stable, Borrowed.size()};
987 }
988 
989 // Parser entry point.
990 Symbol *Demangler::parse(StringView &MangledName) {
991   Symbol *S = Arena.alloc<Symbol>();
992 
993   // MSVC-style mangled symbols must start with '?'.
994   if (!MangledName.consumeFront("?")) {
995     S->SymbolName = Arena.alloc<Name>();
996     S->SymbolName->Str = MangledName;
997     S->SymbolType = Arena.alloc<Type>();
998     S->SymbolType->Prim = PrimTy::Unknown;
999     return S;
1000   }
1001 
1002   // What follows is a main symbol name. This may include
1003   // namespaces or class BackReferences.
1004   S->SymbolName = demangleFullyQualifiedSymbolName(MangledName);
1005   if (Error)
1006     return nullptr;
1007   // Read a variable.
1008   if (startsWithDigit(MangledName)) {
1009     S->Category = SymbolCategory::Variable;
1010     S->SymbolType = demangleVariableEncoding(MangledName);
1011   } else {
1012     S->Category = SymbolCategory::Function;
1013     S->SymbolType = demangleFunctionEncoding(MangledName);
1014   }
1015 
1016   if (Error)
1017     return nullptr;
1018 
1019   return S;
1020 }
1021 
1022 // <type-encoding> ::= <storage-class> <variable-type>
1023 // <storage-class> ::= 0  # private static member
1024 //                 ::= 1  # protected static member
1025 //                 ::= 2  # public static member
1026 //                 ::= 3  # global
1027 //                 ::= 4  # static local
1028 
1029 Type *Demangler::demangleVariableEncoding(StringView &MangledName) {
1030   StorageClass SC = demangleVariableStorageClass(MangledName);
1031 
1032   Type *Ty = demangleType(MangledName, QualifierMangleMode::Drop);
1033 
1034   Ty->Storage = SC;
1035 
1036   // <variable-type> ::= <type> <cvr-qualifiers>
1037   //                 ::= <type> <pointee-cvr-qualifiers> # pointers, references
1038   switch (Ty->Prim) {
1039   case PrimTy::Ptr:
1040   case PrimTy::MemberPtr: {
1041     Qualifiers ExtraChildQuals = Q_None;
1042     Ty->Quals =
1043         Qualifiers(Ty->Quals | demanglePointerExtQualifiers(MangledName));
1044 
1045     bool IsMember = false;
1046     std::tie(ExtraChildQuals, IsMember) = demangleQualifiers(MangledName);
1047 
1048     if (Ty->Prim == PrimTy::MemberPtr) {
1049       assert(IsMember);
1050       Name *BackRefName = demangleFullyQualifiedTypeName(MangledName);
1051       (void)BackRefName;
1052       MemberPointerType *MPTy = static_cast<MemberPointerType *>(Ty);
1053       MPTy->Pointee->Quals = Qualifiers(MPTy->Pointee->Quals | ExtraChildQuals);
1054     } else {
1055       PointerType *PTy = static_cast<PointerType *>(Ty);
1056       PTy->Pointee->Quals = Qualifiers(PTy->Pointee->Quals | ExtraChildQuals);
1057     }
1058 
1059     break;
1060   }
1061   default:
1062     Ty->Quals = demangleQualifiers(MangledName).first;
1063     break;
1064   }
1065 
1066   return Ty;
1067 }
1068 
1069 // Sometimes numbers are encoded in mangled symbols. For example,
1070 // "int (*x)[20]" is a valid C type (x is a pointer to an array of
1071 // length 20), so we need some way to embed numbers as part of symbols.
1072 // This function parses it.
1073 //
1074 // <number>               ::= [?] <non-negative integer>
1075 //
1076 // <non-negative integer> ::= <decimal digit> # when 1 <= Number <= 10
1077 //                        ::= <hex digit>+ @  # when Numbrer == 0 or >= 10
1078 //
1079 // <hex-digit>            ::= [A-P]           # A = 0, B = 1, ...
1080 int Demangler::demangleNumber(StringView &MangledName) {
1081   bool neg = MangledName.consumeFront("?");
1082 
1083   if (startsWithDigit(MangledName)) {
1084     int32_t Ret = MangledName[0] - '0' + 1;
1085     MangledName = MangledName.dropFront(1);
1086     return neg ? -Ret : Ret;
1087   }
1088 
1089   int Ret = 0;
1090   for (size_t i = 0; i < MangledName.size(); ++i) {
1091     char C = MangledName[i];
1092     if (C == '@') {
1093       MangledName = MangledName.dropFront(i + 1);
1094       return neg ? -Ret : Ret;
1095     }
1096     if ('A' <= C && C <= 'P') {
1097       Ret = (Ret << 4) + (C - 'A');
1098       continue;
1099     }
1100     break;
1101   }
1102 
1103   Error = true;
1104   return 0;
1105 }
1106 
1107 // First 10 strings can be referenced by special BackReferences ?0, ?1, ..., ?9.
1108 // Memorize it.
1109 void Demangler::memorizeString(StringView S) {
1110   if (BackRefCount >= sizeof(BackReferences) / sizeof(*BackReferences))
1111     return;
1112   for (size_t i = 0; i < BackRefCount; ++i)
1113     if (S == BackReferences[i])
1114       return;
1115   BackReferences[BackRefCount++] = S;
1116 }
1117 
1118 Name *Demangler::demangleBackRefName(StringView &MangledName) {
1119   assert(startsWithDigit(MangledName));
1120   Name *Node = Arena.alloc<Name>();
1121   Node->IsBackReference = true;
1122   Node->Str = {MangledName.begin(), 1};
1123   MangledName = MangledName.dropFront();
1124   return Node;
1125 }
1126 
1127 Name *Demangler::demangleTemplateInstantiationName(StringView &MangledName) {
1128   assert(MangledName.startsWith("?$"));
1129   MangledName.consumeFront("?$");
1130 
1131   Name *Node = demangleUnqualifiedSymbolName(MangledName, false);
1132   if (Error)
1133     return nullptr;
1134 
1135   Node->TParams = demangleTemplateParameterList(MangledName);
1136   if (Error)
1137     return nullptr;
1138 
1139   Node->IsTemplateInstantiation = true;
1140 
1141   // Render this class template name into a string buffer so that we can
1142   // memorize it for the purpose of back-referencing.
1143   OutputStream OS = OutputStream::create(nullptr, nullptr, 1024);
1144   outputName(OS, Node, *this);
1145   OS << '\0';
1146   char *Name = OS.getBuffer();
1147 
1148   StringView Owned = copyString(Name);
1149   memorizeString(Owned);
1150   std::free(Name);
1151 
1152   return Node;
1153 }
1154 
1155 Name *Demangler::demangleOperatorName(StringView &MangledName) {
1156   assert(MangledName.startsWith('?'));
1157   MangledName.consumeFront('?');
1158 
1159   auto NameString = [this, &MangledName]() -> StringView {
1160     switch (MangledName.popFront()) {
1161     case '0':
1162       return "ctor";
1163     case '1':
1164       return "dtor";
1165     case '2':
1166       return " new";
1167     case '3':
1168       return " delete";
1169     case '4':
1170       return "=";
1171     case '5':
1172       return ">>";
1173     case '6':
1174       return "<<";
1175     case '7':
1176       return "!";
1177     case '8':
1178       return "==";
1179     case '9':
1180       return "!=";
1181     case 'A':
1182       return "[]";
1183     case 'C':
1184       return "->";
1185     case 'D':
1186       return "*";
1187     case 'E':
1188       return "++";
1189     case 'F':
1190       return "--";
1191     case 'G':
1192       return "-";
1193     case 'H':
1194       return "+";
1195     case 'I':
1196       return "&";
1197     case 'J':
1198       return "->*";
1199     case 'K':
1200       return "/";
1201     case 'L':
1202       return "%";
1203     case 'M':
1204       return "<";
1205     case 'N':
1206       return "<=";
1207     case 'O':
1208       return ">";
1209     case 'P':
1210       return ">=";
1211     case 'Q':
1212       return ",";
1213     case 'R':
1214       return "()";
1215     case 'S':
1216       return "~";
1217     case 'T':
1218       return "^";
1219     case 'U':
1220       return "|";
1221     case 'V':
1222       return "&&";
1223     case 'W':
1224       return "||";
1225     case 'X':
1226       return "*=";
1227     case 'Y':
1228       return "+=";
1229     case 'Z':
1230       return "-=";
1231     case '_': {
1232       if (MangledName.empty())
1233         break;
1234 
1235       switch (MangledName.popFront()) {
1236       case '0':
1237         return "/=";
1238       case '1':
1239         return "%=";
1240       case '2':
1241         return ">>=";
1242       case '3':
1243         return "<<=";
1244       case '4':
1245         return "&=";
1246       case '5':
1247         return "|=";
1248       case '6':
1249         return "^=";
1250       case 'U':
1251         return " new[]";
1252       case 'V':
1253         return " delete[]";
1254       case '_':
1255         if (MangledName.consumeFront("L"))
1256           return " co_await";
1257         if (MangledName.consumeFront("K")) {
1258           size_t EndPos = MangledName.find('@');
1259           if (EndPos == StringView::npos)
1260             break;
1261           StringView OpName = demangleSimpleString(MangledName, false);
1262           size_t FullSize = OpName.size() + 3; // <space>""OpName
1263           char *Buffer = Arena.allocUnalignedBuffer(FullSize);
1264           Buffer[0] = ' ';
1265           Buffer[1] = '"';
1266           Buffer[2] = '"';
1267           std::memcpy(Buffer + 3, OpName.begin(), OpName.size());
1268           return {Buffer, FullSize};
1269         }
1270       }
1271     }
1272     }
1273     Error = true;
1274     return "";
1275   };
1276 
1277   Name *Node = Arena.alloc<Name>();
1278   Node->Str = NameString();
1279   Node->IsOperator = true;
1280   return Node;
1281 }
1282 
1283 Name *Demangler::demangleSimpleName(StringView &MangledName, bool Memorize) {
1284   StringView S = demangleSimpleString(MangledName, Memorize);
1285   if (Error)
1286     return nullptr;
1287 
1288   Name *Node = Arena.alloc<Name>();
1289   Node->Str = S;
1290   return Node;
1291 }
1292 
1293 StringView Demangler::demangleSimpleString(StringView &MangledName,
1294                                            bool Memorize) {
1295   StringView S;
1296   for (size_t i = 0; i < MangledName.size(); ++i) {
1297     if (MangledName[i] != '@')
1298       continue;
1299     S = MangledName.substr(0, i);
1300     MangledName = MangledName.dropFront(i + 1);
1301 
1302     if (Memorize)
1303       memorizeString(S);
1304     return S;
1305   }
1306 
1307   Error = true;
1308   return {};
1309 }
1310 
1311 Name *Demangler::demangleAnonymousNamespaceName(StringView &MangledName) {
1312   assert(MangledName.startsWith("?A"));
1313   MangledName.consumeFront("?A");
1314 
1315   Name *Node = Arena.alloc<Name>();
1316   Node->Str = "`anonymous namespace'";
1317   if (MangledName.consumeFront('@'))
1318     return Node;
1319 
1320   Error = true;
1321   return nullptr;
1322 }
1323 
1324 Name *Demangler::demangleLocallyScopedNamePiece(StringView &MangledName) {
1325   assert(startsWithLocalScopePattern(MangledName));
1326 
1327   Name *Node = Arena.alloc<Name>();
1328   MangledName.consumeFront('?');
1329   int ScopeIdentifier = demangleNumber(MangledName);
1330 
1331   // One ? to terminate the number
1332   MangledName.consumeFront('?');
1333 
1334   assert(!Error);
1335   Symbol *Scope = parse(MangledName);
1336   if (Error)
1337     return nullptr;
1338 
1339   // Render the parent symbol's name into a buffer.
1340   OutputStream OS = OutputStream::create(nullptr, nullptr, 1024);
1341   OS << '`';
1342   output(Scope, OS);
1343   OS << '\'';
1344   OS << "::`" << ScopeIdentifier << "'";
1345   OS << '\0';
1346   char *Result = OS.getBuffer();
1347   Node->Str = copyString(Result);
1348   std::free(Result);
1349   return Node;
1350 }
1351 
1352 // Parses a type name in the form of A@B@C@@ which represents C::B::A.
1353 Name *Demangler::demangleFullyQualifiedTypeName(StringView &MangledName) {
1354   Name *TypeName = demangleUnqualifiedTypeName(MangledName, true);
1355   if (Error)
1356     return nullptr;
1357   assert(TypeName);
1358 
1359   Name *QualName = demangleNameScopeChain(MangledName, TypeName);
1360   if (Error)
1361     return nullptr;
1362   assert(QualName);
1363   return QualName;
1364 }
1365 
1366 // Parses a symbol name in the form of A@B@C@@ which represents C::B::A.
1367 // Symbol names have slightly different rules regarding what can appear
1368 // so we separate out the implementations for flexibility.
1369 Name *Demangler::demangleFullyQualifiedSymbolName(StringView &MangledName) {
1370   Name *SymbolName = demangleUnqualifiedSymbolName(MangledName, true);
1371   if (Error)
1372     return nullptr;
1373   assert(SymbolName);
1374 
1375   Name *QualName = demangleNameScopeChain(MangledName, SymbolName);
1376   if (Error)
1377     return nullptr;
1378   assert(QualName);
1379   return QualName;
1380 }
1381 
1382 Name *Demangler::demangleUnqualifiedTypeName(StringView &MangledName,
1383                                              bool Memorize) {
1384   // An inner-most name can be a back-reference, because a fully-qualified name
1385   // (e.g. Scope + Inner) can contain other fully qualified names inside of
1386   // them (for example template parameters), and these nested parameters can
1387   // refer to previously mangled types.
1388   if (startsWithDigit(MangledName))
1389     return demangleBackRefName(MangledName);
1390 
1391   if (MangledName.startsWith("?$"))
1392     return demangleTemplateInstantiationName(MangledName);
1393 
1394   return demangleSimpleName(MangledName, Memorize);
1395 }
1396 
1397 Name *Demangler::demangleUnqualifiedSymbolName(StringView &MangledName,
1398                                                bool Memorize) {
1399   if (startsWithDigit(MangledName))
1400     return demangleBackRefName(MangledName);
1401   if (MangledName.startsWith("?$"))
1402     return demangleTemplateInstantiationName(MangledName);
1403   if (MangledName.startsWith('?'))
1404     return demangleOperatorName(MangledName);
1405   return demangleSimpleName(MangledName, Memorize);
1406 }
1407 
1408 Name *Demangler::demangleNameScopePiece(StringView &MangledName) {
1409   if (startsWithDigit(MangledName))
1410     return demangleBackRefName(MangledName);
1411 
1412   if (MangledName.startsWith("?$"))
1413     return demangleTemplateInstantiationName(MangledName);
1414 
1415   if (MangledName.startsWith("?A"))
1416     return demangleAnonymousNamespaceName(MangledName);
1417 
1418   if (startsWithLocalScopePattern(MangledName))
1419     return demangleLocallyScopedNamePiece(MangledName);
1420 
1421   return demangleSimpleName(MangledName, true);
1422 }
1423 
1424 Name *Demangler::demangleNameScopeChain(StringView &MangledName,
1425                                         Name *UnqualifiedName) {
1426   Name *Head = UnqualifiedName;
1427 
1428   while (!MangledName.consumeFront("@")) {
1429     if (MangledName.empty()) {
1430       Error = true;
1431       return nullptr;
1432     }
1433 
1434     assert(!Error);
1435     Name *Elem = demangleNameScopePiece(MangledName);
1436     if (Error)
1437       return nullptr;
1438 
1439     Elem->Next = Head;
1440     Head = Elem;
1441   }
1442   return Head;
1443 }
1444 
1445 FuncClass Demangler::demangleFunctionClass(StringView &MangledName) {
1446   SwapAndRestore<StringView> RestoreOnError(MangledName, MangledName);
1447   RestoreOnError.shouldRestore(false);
1448 
1449   switch (MangledName.popFront()) {
1450   case 'A':
1451     return Private;
1452   case 'B':
1453     return FuncClass(Private | Far);
1454   case 'C':
1455     return FuncClass(Private | Static);
1456   case 'D':
1457     return FuncClass(Private | Static);
1458   case 'E':
1459     return FuncClass(Private | Virtual);
1460   case 'F':
1461     return FuncClass(Private | Virtual);
1462   case 'I':
1463     return Protected;
1464   case 'J':
1465     return FuncClass(Protected | Far);
1466   case 'K':
1467     return FuncClass(Protected | Static);
1468   case 'L':
1469     return FuncClass(Protected | Static | Far);
1470   case 'M':
1471     return FuncClass(Protected | Virtual);
1472   case 'N':
1473     return FuncClass(Protected | Virtual | Far);
1474   case 'Q':
1475     return Public;
1476   case 'R':
1477     return FuncClass(Public | Far);
1478   case 'S':
1479     return FuncClass(Public | Static);
1480   case 'T':
1481     return FuncClass(Public | Static | Far);
1482   case 'U':
1483     return FuncClass(Public | Virtual);
1484   case 'V':
1485     return FuncClass(Public | Virtual | Far);
1486   case 'Y':
1487     return Global;
1488   case 'Z':
1489     return FuncClass(Global | Far);
1490   }
1491 
1492   Error = true;
1493   RestoreOnError.shouldRestore(true);
1494   return Public;
1495 }
1496 
1497 CallingConv Demangler::demangleCallingConvention(StringView &MangledName) {
1498   switch (MangledName.popFront()) {
1499   case 'A':
1500   case 'B':
1501     return CallingConv::Cdecl;
1502   case 'C':
1503   case 'D':
1504     return CallingConv::Pascal;
1505   case 'E':
1506   case 'F':
1507     return CallingConv::Thiscall;
1508   case 'G':
1509   case 'H':
1510     return CallingConv::Stdcall;
1511   case 'I':
1512   case 'J':
1513     return CallingConv::Fastcall;
1514   case 'M':
1515   case 'N':
1516     return CallingConv::Clrcall;
1517   case 'O':
1518   case 'P':
1519     return CallingConv::Eabi;
1520   case 'Q':
1521     return CallingConv::Vectorcall;
1522   }
1523 
1524   return CallingConv::None;
1525 }
1526 
1527 StorageClass Demangler::demangleVariableStorageClass(StringView &MangledName) {
1528   assert(std::isdigit(MangledName.front()));
1529 
1530   switch (MangledName.popFront()) {
1531   case '0':
1532     return StorageClass::PrivateStatic;
1533   case '1':
1534     return StorageClass::ProtectedStatic;
1535   case '2':
1536     return StorageClass::PublicStatic;
1537   case '3':
1538     return StorageClass::Global;
1539   case '4':
1540     return StorageClass::FunctionLocalStatic;
1541   }
1542   Error = true;
1543   return StorageClass::None;
1544 }
1545 
1546 std::pair<Qualifiers, bool>
1547 Demangler::demangleQualifiers(StringView &MangledName) {
1548 
1549   switch (MangledName.popFront()) {
1550   // Member qualifiers
1551   case 'Q':
1552     return std::make_pair(Q_None, true);
1553   case 'R':
1554     return std::make_pair(Q_Const, true);
1555   case 'S':
1556     return std::make_pair(Q_Volatile, true);
1557   case 'T':
1558     return std::make_pair(Qualifiers(Q_Const | Q_Volatile), true);
1559   // Non-Member qualifiers
1560   case 'A':
1561     return std::make_pair(Q_None, false);
1562   case 'B':
1563     return std::make_pair(Q_Const, false);
1564   case 'C':
1565     return std::make_pair(Q_Volatile, false);
1566   case 'D':
1567     return std::make_pair(Qualifiers(Q_Const | Q_Volatile), false);
1568   }
1569   Error = true;
1570   return std::make_pair(Q_None, false);
1571 }
1572 
1573 static bool isTagType(StringView S) {
1574   switch (S.front()) {
1575   case 'T': // union
1576   case 'U': // struct
1577   case 'V': // class
1578   case 'W': // enum
1579     return true;
1580   }
1581   return false;
1582 }
1583 
1584 static bool isPointerType(StringView S) {
1585   if (S.startsWith("$$Q")) // foo &&
1586     return true;
1587 
1588   switch (S.front()) {
1589   case 'A': // foo &
1590   case 'P': // foo *
1591   case 'Q': // foo *const
1592   case 'R': // foo *volatile
1593   case 'S': // foo *const volatile
1594     return true;
1595   }
1596   return false;
1597 }
1598 
1599 static bool isArrayType(StringView S) { return S[0] == 'Y'; }
1600 
1601 static bool isFunctionType(StringView S) {
1602   return S.startsWith("$$A8@@") || S.startsWith("$$A6");
1603 }
1604 
1605 // <variable-type> ::= <type> <cvr-qualifiers>
1606 //                 ::= <type> <pointee-cvr-qualifiers> # pointers, references
1607 Type *Demangler::demangleType(StringView &MangledName,
1608                               QualifierMangleMode QMM) {
1609   Qualifiers Quals = Q_None;
1610   bool IsMember = false;
1611   bool IsMemberKnown = false;
1612   if (QMM == QualifierMangleMode::Mangle) {
1613     std::tie(Quals, IsMember) = demangleQualifiers(MangledName);
1614     IsMemberKnown = true;
1615   } else if (QMM == QualifierMangleMode::Result) {
1616     if (MangledName.consumeFront('?')) {
1617       std::tie(Quals, IsMember) = demangleQualifiers(MangledName);
1618       IsMemberKnown = true;
1619     }
1620   }
1621 
1622   Type *Ty = nullptr;
1623   if (isTagType(MangledName))
1624     Ty = demangleClassType(MangledName);
1625   else if (isPointerType(MangledName)) {
1626     if (!IsMemberKnown)
1627       IsMember = isMemberPointer(MangledName);
1628 
1629     if (IsMember)
1630       Ty = demangleMemberPointerType(MangledName);
1631     else
1632       Ty = demanglePointerType(MangledName);
1633   } else if (isArrayType(MangledName))
1634     Ty = demangleArrayType(MangledName);
1635   else if (isFunctionType(MangledName)) {
1636     if (MangledName.consumeFront("$$A8@@"))
1637       Ty = demangleFunctionType(MangledName, true, false);
1638     else {
1639       assert(MangledName.startsWith("$$A6"));
1640       MangledName.consumeFront("$$A6");
1641       Ty = demangleFunctionType(MangledName, false, false);
1642     }
1643   } else {
1644     Ty = demangleBasicType(MangledName);
1645     assert(Ty && !Error);
1646     if (!Ty || Error)
1647       return Ty;
1648   }
1649 
1650   Ty->Quals = Qualifiers(Ty->Quals | Quals);
1651   return Ty;
1652 }
1653 
1654 ReferenceKind Demangler::demangleReferenceKind(StringView &MangledName) {
1655   if (MangledName.consumeFront('G'))
1656     return ReferenceKind::LValueRef;
1657   else if (MangledName.consumeFront('H'))
1658     return ReferenceKind::RValueRef;
1659   return ReferenceKind::None;
1660 }
1661 
1662 void Demangler::demangleThrowSpecification(StringView &MangledName) {
1663   if (MangledName.consumeFront('Z'))
1664     return;
1665 
1666   Error = true;
1667 }
1668 
1669 FunctionType *Demangler::demangleFunctionType(StringView &MangledName,
1670                                               bool HasThisQuals,
1671                                               bool IsFunctionPointer) {
1672   FunctionType *FTy = Arena.alloc<FunctionType>();
1673   FTy->Prim = PrimTy::Function;
1674   FTy->IsFunctionPointer = IsFunctionPointer;
1675 
1676   if (HasThisQuals) {
1677     FTy->Quals = demanglePointerExtQualifiers(MangledName);
1678     FTy->RefKind = demangleReferenceKind(MangledName);
1679     FTy->Quals = Qualifiers(FTy->Quals | demangleQualifiers(MangledName).first);
1680   }
1681 
1682   // Fields that appear on both member and non-member functions.
1683   FTy->CallConvention = demangleCallingConvention(MangledName);
1684 
1685   // <return-type> ::= <type>
1686   //               ::= @ # structors (they have no declared return type)
1687   bool IsStructor = MangledName.consumeFront('@');
1688   if (!IsStructor)
1689     FTy->ReturnType = demangleType(MangledName, QualifierMangleMode::Result);
1690 
1691   FTy->Params = demangleFunctionParameterList(MangledName);
1692 
1693   demangleThrowSpecification(MangledName);
1694 
1695   return FTy;
1696 }
1697 
1698 Type *Demangler::demangleFunctionEncoding(StringView &MangledName) {
1699   FuncClass FC = demangleFunctionClass(MangledName);
1700 
1701   bool HasThisQuals = !(FC & (Global | Static));
1702   FunctionType *FTy = demangleFunctionType(MangledName, HasThisQuals, false);
1703   FTy->FunctionClass = FC;
1704 
1705   return FTy;
1706 }
1707 
1708 // Reads a primitive type.
1709 Type *Demangler::demangleBasicType(StringView &MangledName) {
1710   Type *Ty = Arena.alloc<Type>();
1711 
1712   if (MangledName.consumeFront("$$T")) {
1713     Ty->Prim = PrimTy::Nullptr;
1714     return Ty;
1715   }
1716 
1717   switch (MangledName.popFront()) {
1718   case 'X':
1719     Ty->Prim = PrimTy::Void;
1720     break;
1721   case 'D':
1722     Ty->Prim = PrimTy::Char;
1723     break;
1724   case 'C':
1725     Ty->Prim = PrimTy::Schar;
1726     break;
1727   case 'E':
1728     Ty->Prim = PrimTy::Uchar;
1729     break;
1730   case 'F':
1731     Ty->Prim = PrimTy::Short;
1732     break;
1733   case 'G':
1734     Ty->Prim = PrimTy::Ushort;
1735     break;
1736   case 'H':
1737     Ty->Prim = PrimTy::Int;
1738     break;
1739   case 'I':
1740     Ty->Prim = PrimTy::Uint;
1741     break;
1742   case 'J':
1743     Ty->Prim = PrimTy::Long;
1744     break;
1745   case 'K':
1746     Ty->Prim = PrimTy::Ulong;
1747     break;
1748   case 'M':
1749     Ty->Prim = PrimTy::Float;
1750     break;
1751   case 'N':
1752     Ty->Prim = PrimTy::Double;
1753     break;
1754   case 'O':
1755     Ty->Prim = PrimTy::Ldouble;
1756     break;
1757   case '_': {
1758     if (MangledName.empty()) {
1759       Error = true;
1760       return nullptr;
1761     }
1762     switch (MangledName.popFront()) {
1763     case 'N':
1764       Ty->Prim = PrimTy::Bool;
1765       break;
1766     case 'J':
1767       Ty->Prim = PrimTy::Int64;
1768       break;
1769     case 'K':
1770       Ty->Prim = PrimTy::Uint64;
1771       break;
1772     case 'W':
1773       Ty->Prim = PrimTy::Wchar;
1774       break;
1775     case 'S':
1776       Ty->Prim = PrimTy::Char16;
1777       break;
1778     case 'U':
1779       Ty->Prim = PrimTy::Char32;
1780       break;
1781     default:
1782       Error = true;
1783       return nullptr;
1784     }
1785     break;
1786   }
1787   default:
1788     Error = true;
1789     return nullptr;
1790   }
1791   return Ty;
1792 }
1793 
1794 UdtType *Demangler::demangleClassType(StringView &MangledName) {
1795   UdtType *UTy = Arena.alloc<UdtType>();
1796 
1797   switch (MangledName.popFront()) {
1798   case 'T':
1799     UTy->Prim = PrimTy::Union;
1800     break;
1801   case 'U':
1802     UTy->Prim = PrimTy::Struct;
1803     break;
1804   case 'V':
1805     UTy->Prim = PrimTy::Class;
1806     break;
1807   case 'W':
1808     if (MangledName.popFront() != '4') {
1809       Error = true;
1810       return nullptr;
1811     }
1812     UTy->Prim = PrimTy::Enum;
1813     break;
1814   default:
1815     assert(false);
1816   }
1817 
1818   UTy->UdtName = demangleFullyQualifiedTypeName(MangledName);
1819   return UTy;
1820 }
1821 
1822 static std::pair<Qualifiers, PointerAffinity>
1823 demanglePointerCVQualifiers(StringView &MangledName) {
1824   if (MangledName.consumeFront("$$Q"))
1825     return std::make_pair(Q_None, PointerAffinity::RValueReference);
1826 
1827   switch (MangledName.popFront()) {
1828   case 'A':
1829     return std::make_pair(Q_None, PointerAffinity::Reference);
1830   case 'P':
1831     return std::make_pair(Q_None, PointerAffinity::Pointer);
1832   case 'Q':
1833     return std::make_pair(Q_Const, PointerAffinity::Pointer);
1834   case 'R':
1835     return std::make_pair(Q_Volatile, PointerAffinity::Pointer);
1836   case 'S':
1837     return std::make_pair(Qualifiers(Q_Const | Q_Volatile),
1838                           PointerAffinity::Pointer);
1839   default:
1840     assert(false && "Ty is not a pointer type!");
1841   }
1842   return std::make_pair(Q_None, PointerAffinity::Pointer);
1843 }
1844 
1845 // <pointer-type> ::= E? <pointer-cvr-qualifiers> <ext-qualifiers> <type>
1846 //                       # the E is required for 64-bit non-static pointers
1847 PointerType *Demangler::demanglePointerType(StringView &MangledName) {
1848   PointerType *Pointer = Arena.alloc<PointerType>();
1849 
1850   std::tie(Pointer->Quals, Pointer->Affinity) =
1851       demanglePointerCVQualifiers(MangledName);
1852 
1853   Pointer->Prim = PrimTy::Ptr;
1854   if (MangledName.consumeFront("6")) {
1855     Pointer->Pointee = demangleFunctionType(MangledName, false, true);
1856     return Pointer;
1857   }
1858 
1859   Qualifiers ExtQuals = demanglePointerExtQualifiers(MangledName);
1860   Pointer->Quals = Qualifiers(Pointer->Quals | ExtQuals);
1861 
1862   Pointer->Pointee = demangleType(MangledName, QualifierMangleMode::Mangle);
1863   return Pointer;
1864 }
1865 
1866 MemberPointerType *
1867 Demangler::demangleMemberPointerType(StringView &MangledName) {
1868   MemberPointerType *Pointer = Arena.alloc<MemberPointerType>();
1869   Pointer->Prim = PrimTy::MemberPtr;
1870 
1871   PointerAffinity Affinity;
1872   std::tie(Pointer->Quals, Affinity) = demanglePointerCVQualifiers(MangledName);
1873   assert(Affinity == PointerAffinity::Pointer);
1874 
1875   Qualifiers ExtQuals = demanglePointerExtQualifiers(MangledName);
1876   Pointer->Quals = Qualifiers(Pointer->Quals | ExtQuals);
1877 
1878   if (MangledName.consumeFront("8")) {
1879     Pointer->MemberName = demangleFullyQualifiedSymbolName(MangledName);
1880     Pointer->Pointee = demangleFunctionType(MangledName, true, true);
1881   } else {
1882     Qualifiers PointeeQuals = Q_None;
1883     bool IsMember = false;
1884     std::tie(PointeeQuals, IsMember) = demangleQualifiers(MangledName);
1885     assert(IsMember);
1886     Pointer->MemberName = demangleFullyQualifiedSymbolName(MangledName);
1887 
1888     Pointer->Pointee = demangleType(MangledName, QualifierMangleMode::Drop);
1889     Pointer->Pointee->Quals = PointeeQuals;
1890   }
1891 
1892   return Pointer;
1893 }
1894 
1895 Qualifiers Demangler::demanglePointerExtQualifiers(StringView &MangledName) {
1896   Qualifiers Quals = Q_None;
1897   if (MangledName.consumeFront('E'))
1898     Quals = Qualifiers(Quals | Q_Pointer64);
1899   if (MangledName.consumeFront('I'))
1900     Quals = Qualifiers(Quals | Q_Restrict);
1901   if (MangledName.consumeFront('F'))
1902     Quals = Qualifiers(Quals | Q_Unaligned);
1903 
1904   return Quals;
1905 }
1906 
1907 ArrayType *Demangler::demangleArrayType(StringView &MangledName) {
1908   assert(MangledName.front() == 'Y');
1909   MangledName.popFront();
1910 
1911   int Dimension = demangleNumber(MangledName);
1912   if (Dimension <= 0) {
1913     Error = true;
1914     return nullptr;
1915   }
1916 
1917   ArrayType *ATy = Arena.alloc<ArrayType>();
1918   ArrayType *Dim = ATy;
1919   for (int I = 0; I < Dimension; ++I) {
1920     Dim->Prim = PrimTy::Array;
1921     Dim->ArrayDimension = demangleNumber(MangledName);
1922     Dim->NextDimension = Arena.alloc<ArrayType>();
1923     Dim = Dim->NextDimension;
1924   }
1925 
1926   if (MangledName.consumeFront("$$C")) {
1927     if (MangledName.consumeFront("B"))
1928       ATy->Quals = Q_Const;
1929     else if (MangledName.consumeFront("C") || MangledName.consumeFront("D"))
1930       ATy->Quals = Qualifiers(Q_Const | Q_Volatile);
1931     else if (!MangledName.consumeFront("A"))
1932       Error = true;
1933   }
1934 
1935   ATy->ElementType = demangleType(MangledName, QualifierMangleMode::Drop);
1936   Dim->ElementType = ATy->ElementType;
1937   return ATy;
1938 }
1939 
1940 // Reads a function or a template parameters.
1941 FunctionParams
1942 Demangler::demangleFunctionParameterList(StringView &MangledName) {
1943   // Empty parameter list.
1944   if (MangledName.consumeFront('X'))
1945     return {};
1946 
1947   FunctionParams *Head;
1948   FunctionParams **Current = &Head;
1949   while (!Error && !MangledName.startsWith('@') &&
1950          !MangledName.startsWith('Z')) {
1951 
1952     if (startsWithDigit(MangledName)) {
1953       size_t N = MangledName[0] - '0';
1954       if (N >= FunctionParamBackRefCount) {
1955         Error = true;
1956         return {};
1957       }
1958       MangledName = MangledName.dropFront();
1959 
1960       *Current = Arena.alloc<FunctionParams>();
1961       (*Current)->Current = FunctionParamBackRefs[N]->clone(Arena);
1962       Current = &(*Current)->Next;
1963       continue;
1964     }
1965 
1966     size_t OldSize = MangledName.size();
1967 
1968     *Current = Arena.alloc<FunctionParams>();
1969     (*Current)->Current = demangleType(MangledName, QualifierMangleMode::Drop);
1970 
1971     size_t CharsConsumed = OldSize - MangledName.size();
1972     assert(CharsConsumed != 0);
1973 
1974     // Single-letter types are ignored for backreferences because memorizing
1975     // them doesn't save anything.
1976     if (FunctionParamBackRefCount <= 9 && CharsConsumed > 1)
1977       FunctionParamBackRefs[FunctionParamBackRefCount++] = (*Current)->Current;
1978 
1979     Current = &(*Current)->Next;
1980   }
1981 
1982   if (Error)
1983     return {};
1984 
1985   // A non-empty parameter list is terminated by either 'Z' (variadic) parameter
1986   // list or '@' (non variadic).  Careful not to consume "@Z", as in that case
1987   // the following Z could be a throw specifier.
1988   if (MangledName.consumeFront('@'))
1989     return *Head;
1990 
1991   if (MangledName.consumeFront('Z')) {
1992     Head->IsVariadic = true;
1993     return *Head;
1994   }
1995 
1996   Error = true;
1997   return {};
1998 }
1999 
2000 TemplateParams *
2001 Demangler::demangleTemplateParameterList(StringView &MangledName) {
2002   TemplateParams *Head;
2003   TemplateParams **Current = &Head;
2004   while (!Error && !MangledName.startsWith('@')) {
2005     // Template parameter lists don't participate in back-referencing.
2006     *Current = Arena.alloc<TemplateParams>();
2007 
2008     // Empty parameter pack.
2009     if (MangledName.consumeFront("$S") || MangledName.consumeFront("$$V") ||
2010         MangledName.consumeFront("$$$V")) {
2011       break;
2012     }
2013 
2014     if (MangledName.consumeFront("$$Y")) {
2015       (*Current)->IsTemplateTemplate = true;
2016       (*Current)->IsAliasTemplate = true;
2017       (*Current)->ParamName = demangleFullyQualifiedTypeName(MangledName);
2018     } else if (MangledName.consumeFront("$1?")) {
2019       (*Current)->ParamName = demangleFullyQualifiedSymbolName(MangledName);
2020       (*Current)->ParamType = demangleFunctionEncoding(MangledName);
2021     } else {
2022       (*Current)->ParamType =
2023           demangleType(MangledName, QualifierMangleMode::Drop);
2024     }
2025     if (Error)
2026       return nullptr;
2027 
2028     Current = &(*Current)->Next;
2029   }
2030 
2031   if (Error)
2032     return nullptr;
2033 
2034   // Template parameter lists cannot be variadic, so it can only be terminated
2035   // by @.
2036   if (MangledName.consumeFront('@'))
2037     return Head;
2038   Error = true;
2039   return nullptr;
2040 }
2041 
2042 StringView Demangler::resolve(StringView N) {
2043   assert(N.size() == 1 && isdigit(N[0]));
2044   size_t Digit = N[0] - '0';
2045   if (Digit >= BackRefCount)
2046     return N;
2047   return BackReferences[Digit];
2048 }
2049 
2050 void Demangler::output(const Symbol *S, OutputStream &OS) {
2051   // Converts an AST to a string.
2052   //
2053   // Converting an AST representing a C++ type to a string is tricky due
2054   // to the bad grammar of the C++ declaration inherited from C. You have
2055   // to construct a string from inside to outside. For example, if a type
2056   // X is a pointer to a function returning int, the order you create a
2057   // string becomes something like this:
2058   //
2059   //   (1) X is a pointer: *X
2060   //   (2) (1) is a function returning int: int (*X)()
2061   //
2062   // So you cannot construct a result just by appending strings to a result.
2063   //
2064   // To deal with this, we split the function into two. outputPre() writes
2065   // the "first half" of type declaration, and outputPost() writes the
2066   // "second half". For example, outputPre() writes a return type for a
2067   // function and outputPost() writes an parameter list.
2068   Type::outputPre(OS, *S->SymbolType, *this);
2069   outputName(OS, S->SymbolName, *this);
2070   Type::outputPost(OS, *S->SymbolType, *this);
2071 }
2072 
2073 void Demangler::dumpBackReferences() {
2074   std::printf("%d function parameter backreferences\n",
2075               (int)FunctionParamBackRefCount);
2076 
2077   // Create an output stream so we can render each type.
2078   OutputStream OS = OutputStream::create(nullptr, 0, 1024);
2079   for (size_t I = 0; I < FunctionParamBackRefCount; ++I) {
2080     OS.setCurrentPosition(0);
2081 
2082     Type *T = FunctionParamBackRefs[I];
2083     Type::outputPre(OS, *T, *this);
2084     Type::outputPost(OS, *T, *this);
2085 
2086     std::printf("  [%d] - %.*s\n", (int)I, (int)OS.getCurrentPosition(),
2087                 OS.getBuffer());
2088   }
2089   std::free(OS.getBuffer());
2090 
2091   if (FunctionParamBackRefCount > 0)
2092     std::printf("\n");
2093   std::printf("%d name backreferences\n", (int)BackRefCount);
2094   for (size_t I = 0; I < BackRefCount; ++I) {
2095     std::printf("  [%d] - %.*s\n", (int)I, (int)BackReferences[I].size(),
2096                 BackReferences[I].begin());
2097   }
2098   if (BackRefCount > 0)
2099     std::printf("\n");
2100 }
2101 
2102 char *llvm::microsoftDemangle(const char *MangledName, char *Buf, size_t *N,
2103                               int *Status, MSDemangleFlags Flags) {
2104   Demangler D;
2105   StringView Name{MangledName};
2106   Symbol *S = D.parse(Name);
2107 
2108   if (Flags & MSDF_DumpBackrefs)
2109     D.dumpBackReferences();
2110   OutputStream OS = OutputStream::create(Buf, N, 1024);
2111   if (D.Error) {
2112     OS << MangledName;
2113     *Status = llvm::demangle_invalid_mangled_name;
2114   } else {
2115     D.output(S, OS);
2116     *Status = llvm::demangle_success;
2117   }
2118 
2119   OS << '\0';
2120   return OS.getBuffer();
2121 }
2122