LLVM  9.0.0svn
MicrosoftDemangle.cpp
Go to the documentation of this file.
1 //===- MicrosoftDemangle.cpp ----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines a demangler for MSVC-style mangled symbols.
10 //
11 // This file has no dependencies on the rest of LLVM so that it can be
12 // easily reused in other programs such as libcxxabi.
13 //
14 //===----------------------------------------------------------------------===//
15 
17 #include "llvm/Demangle/Demangle.h"
19 
22 #include "llvm/Demangle/Utility.h"
23 
24 #include <array>
25 #include <cctype>
26 #include <cstdio>
27 #include <tuple>
28 
29 using namespace llvm;
30 using namespace ms_demangle;
31 
32 static bool startsWithDigit(StringView S) {
33  return !S.empty() && std::isdigit(S.front());
34 }
35 
36 
37 struct NodeList {
38  Node *N = nullptr;
39  NodeList *Next = nullptr;
40 };
41 
42 static bool isMemberPointer(StringView MangledName, bool &Error) {
43  Error = false;
44  switch (MangledName.popFront()) {
45  case '$':
46  // This is probably an rvalue reference (e.g. $$Q), and you cannot have an
47  // rvalue reference to a member.
48  return false;
49  case 'A':
50  // 'A' indicates a reference, and you cannot have a reference to a member
51  // function or member.
52  return false;
53  case 'P':
54  case 'Q':
55  case 'R':
56  case 'S':
57  // These 4 values indicate some kind of pointer, but we still don't know
58  // what.
59  break;
60  default:
61  Error = true;
62  return false;
63  }
64 
65  // If it starts with a number, then 6 indicates a non-member function
66  // pointer, and 8 indicates a member function pointer.
67  if (startsWithDigit(MangledName)) {
68  if (MangledName[0] != '6' && MangledName[0] != '8') {
69  Error = true;
70  return false;
71  }
72  return (MangledName[0] == '8');
73  }
74 
75  // Remove ext qualifiers since those can appear on either type and are
76  // therefore not indicative.
77  MangledName.consumeFront('E'); // 64-bit
78  MangledName.consumeFront('I'); // restrict
79  MangledName.consumeFront('F'); // unaligned
80 
81  if (MangledName.empty()) {
82  Error = true;
83  return false;
84  }
85 
86  // The next value should be either ABCD (non-member) or QRST (member).
87  switch (MangledName.front()) {
88  case 'A':
89  case 'B':
90  case 'C':
91  case 'D':
92  return false;
93  case 'Q':
94  case 'R':
95  case 'S':
96  case 'T':
97  return true;
98  default:
99  Error = true;
100  return false;
101  }
102 }
103 
105 consumeSpecialIntrinsicKind(StringView &MangledName) {
106  if (MangledName.consumeFront("?_7"))
108  if (MangledName.consumeFront("?_8"))
110  if (MangledName.consumeFront("?_9"))
112  if (MangledName.consumeFront("?_A"))
114  if (MangledName.consumeFront("?_B"))
116  if (MangledName.consumeFront("?_C"))
118  if (MangledName.consumeFront("?_P"))
120  if (MangledName.consumeFront("?_R0"))
122  if (MangledName.consumeFront("?_R1"))
124  if (MangledName.consumeFront("?_R2"))
126  if (MangledName.consumeFront("?_R3"))
128  if (MangledName.consumeFront("?_R4"))
130  if (MangledName.consumeFront("?_S"))
132  if (MangledName.consumeFront("?__E"))
134  if (MangledName.consumeFront("?__F"))
136  if (MangledName.consumeFront("?__J"))
139 }
140 
141 static bool startsWithLocalScopePattern(StringView S) {
142  if (!S.consumeFront('?'))
143  return false;
144  if (S.size() < 2)
145  return false;
146 
147  size_t End = S.find('?');
148  if (End == StringView::npos)
149  return false;
150  StringView Candidate = S.substr(0, End);
151  if (Candidate.empty())
152  return false;
153 
154  // \?[0-9]\?
155  // ?@? is the discriminator 0.
156  if (Candidate.size() == 1)
157  return Candidate[0] == '@' || (Candidate[0] >= '0' && Candidate[0] <= '9');
158 
159  // If it's not 0-9, then it's an encoded number terminated with an @
160  if (Candidate.back() != '@')
161  return false;
162  Candidate = Candidate.dropBack();
163 
164  // An encoded number starts with B-P and all subsequent digits are in A-P.
165  // Note that the reason the first digit cannot be A is two fold. First, it
166  // would create an ambiguity with ?A which delimits the beginning of an
167  // anonymous namespace. Second, A represents 0, and you don't start a multi
168  // digit number with a leading 0. Presumably the anonymous namespace
169  // ambiguity is also why single digit encoded numbers use 0-9 rather than A-J.
170  if (Candidate[0] < 'B' || Candidate[0] > 'P')
171  return false;
172  Candidate = Candidate.dropFront();
173  while (!Candidate.empty()) {
174  if (Candidate[0] < 'A' || Candidate[0] > 'P')
175  return false;
176  Candidate = Candidate.dropFront();
177  }
178 
179  return true;
180 }
181 
182 static bool isTagType(StringView S) {
183  switch (S.front()) {
184  case 'T': // union
185  case 'U': // struct
186  case 'V': // class
187  case 'W': // enum
188  return true;
189  }
190  return false;
191 }
192 
193 static bool isCustomType(StringView S) { return S[0] == '?'; }
194 
195 static bool isPointerType(StringView S) {
196  if (S.startsWith("$$Q")) // foo &&
197  return true;
198 
199  switch (S.front()) {
200  case 'A': // foo &
201  case 'P': // foo *
202  case 'Q': // foo *const
203  case 'R': // foo *volatile
204  case 'S': // foo *const volatile
205  return true;
206  }
207  return false;
208 }
209 
210 static bool isArrayType(StringView S) { return S[0] == 'Y'; }
211 
212 static bool isFunctionType(StringView S) {
213  return S.startsWith("$$A8@@") || S.startsWith("$$A6");
214 }
215 
217 demangleFunctionRefQualifier(StringView &MangledName) {
218  if (MangledName.consumeFront('G'))
220  else if (MangledName.consumeFront('H'))
223 }
224 
225 static std::pair<Qualifiers, PointerAffinity>
226 demanglePointerCVQualifiers(StringView &MangledName) {
227  if (MangledName.consumeFront("$$Q"))
228  return std::make_pair(Q_None, PointerAffinity::RValueReference);
229 
230  switch (MangledName.popFront()) {
231  case 'A':
232  return std::make_pair(Q_None, PointerAffinity::Reference);
233  case 'P':
234  return std::make_pair(Q_None, PointerAffinity::Pointer);
235  case 'Q':
236  return std::make_pair(Q_Const, PointerAffinity::Pointer);
237  case 'R':
238  return std::make_pair(Q_Volatile, PointerAffinity::Pointer);
239  case 'S':
240  return std::make_pair(Qualifiers(Q_Const | Q_Volatile),
242  default:
243  assert(false && "Ty is not a pointer type!");
244  }
245  return std::make_pair(Q_None, PointerAffinity::Pointer);
246 }
247 
248 StringView Demangler::copyString(StringView Borrowed) {
249  char *Stable = Arena.allocUnalignedBuffer(Borrowed.size() + 1);
250  std::strcpy(Stable, Borrowed.begin());
251 
252  return {Stable, Borrowed.size()};
253 }
254 
256 Demangler::demangleSpecialTableSymbolNode(StringView &MangledName,
258  NamedIdentifierNode *NI = Arena.alloc<NamedIdentifierNode>();
259  switch (K) {
261  NI->Name = "`vftable'";
262  break;
264  NI->Name = "`vbtable'";
265  break;
267  NI->Name = "`local vftable'";
268  break;
270  NI->Name = "`RTTI Complete Object Locator'";
271  break;
272  default:
274  }
275  QualifiedNameNode *QN = demangleNameScopeChain(MangledName, NI);
276  SpecialTableSymbolNode *STSN = Arena.alloc<SpecialTableSymbolNode>();
277  STSN->Name = QN;
278  bool IsMember = false;
279  if (MangledName.empty()) {
280  Error = true;
281  return nullptr;
282  }
283  char Front = MangledName.popFront();
284  if (Front != '6' && Front != '7') {
285  Error = true;
286  return nullptr;
287  }
288 
289  std::tie(STSN->Quals, IsMember) = demangleQualifiers(MangledName);
290  if (!MangledName.consumeFront('@'))
291  STSN->TargetName = demangleFullyQualifiedTypeName(MangledName);
292  return STSN;
293 }
294 
296 Demangler::demangleLocalStaticGuard(StringView &MangledName) {
298  Arena.alloc<LocalStaticGuardIdentifierNode>();
299  QualifiedNameNode *QN = demangleNameScopeChain(MangledName, LSGI);
301  Arena.alloc<LocalStaticGuardVariableNode>();
302  LSGVN->Name = QN;
303 
304  if (MangledName.consumeFront("4IA"))
305  LSGVN->IsVisible = false;
306  else if (MangledName.consumeFront("5"))
307  LSGVN->IsVisible = true;
308  else {
309  Error = true;
310  return nullptr;
311  }
312 
313  if (!MangledName.empty())
314  LSGI->ScopeIndex = demangleUnsigned(MangledName);
315  return LSGVN;
316 }
317 
319  StringView Name) {
321  Id->Name = Name;
322  return Id;
323 }
324 
328  QN->Components = Arena.alloc<NodeArrayNode>();
329  QN->Components->Count = 1;
330  QN->Components->Nodes = Arena.allocArray<Node *>(1);
331  QN->Components->Nodes[0] = Identifier;
332  return QN;
333 }
334 
336  StringView Name) {
338  return synthesizeQualifiedName(Arena, Id);
339 }
340 
342  TypeNode *Type,
343  StringView VariableName) {
345  VSN->Type = Type;
346  VSN->Name = synthesizeQualifiedName(Arena, VariableName);
347  return VSN;
348 }
349 
350 VariableSymbolNode *Demangler::demangleUntypedVariable(
351  ArenaAllocator &Arena, StringView &MangledName, StringView VariableName) {
352  NamedIdentifierNode *NI = synthesizeNamedIdentifier(Arena, VariableName);
353  QualifiedNameNode *QN = demangleNameScopeChain(MangledName, NI);
355  VSN->Name = QN;
356  if (MangledName.consumeFront("8"))
357  return VSN;
358 
359  Error = true;
360  return nullptr;
361 }
362 
364 Demangler::demangleRttiBaseClassDescriptorNode(ArenaAllocator &Arena,
365  StringView &MangledName) {
368  RBCDN->NVOffset = demangleUnsigned(MangledName);
369  RBCDN->VBPtrOffset = demangleSigned(MangledName);
370  RBCDN->VBTableOffset = demangleUnsigned(MangledName);
371  RBCDN->Flags = demangleUnsigned(MangledName);
372  if (Error)
373  return nullptr;
374 
376  VSN->Name = demangleNameScopeChain(MangledName, RBCDN);
377  MangledName.consumeFront('8');
378  return VSN;
379 }
380 
381 FunctionSymbolNode *Demangler::demangleInitFiniStub(StringView &MangledName,
382  bool IsDestructor) {
385  DSIN->IsDestructor = IsDestructor;
386 
387  bool IsKnownStaticDataMember = false;
388  if (MangledName.consumeFront('?'))
389  IsKnownStaticDataMember = true;
390 
391  SymbolNode *Symbol = demangleDeclarator(MangledName);
392  if (Error)
393  return nullptr;
394 
395  FunctionSymbolNode *FSN = nullptr;
396 
397  if (Symbol->kind() == NodeKind::VariableSymbol) {
398  DSIN->Variable = static_cast<VariableSymbolNode *>(Symbol);
399 
400  // Older versions of clang mangled this type of symbol incorrectly. They
401  // would omit the leading ? and they would only emit a single @ at the end.
402  // The correct mangling is a leading ? and 2 trailing @ signs. Handle
403  // both cases.
404  int AtCount = IsKnownStaticDataMember ? 2 : 1;
405  for (int I = 0; I < AtCount; ++I) {
406  if (MangledName.consumeFront('@'))
407  continue;
408  Error = true;
409  return nullptr;
410  }
411 
412  FSN = demangleFunctionEncoding(MangledName);
413  if (FSN)
414  FSN->Name = synthesizeQualifiedName(Arena, DSIN);
415  } else {
416  if (IsKnownStaticDataMember) {
417  // This was supposed to be a static data member, but we got a function.
418  Error = true;
419  return nullptr;
420  }
421 
422  FSN = static_cast<FunctionSymbolNode *>(Symbol);
423  DSIN->Name = Symbol->Name;
424  FSN->Name = synthesizeQualifiedName(Arena, DSIN);
425  }
426 
427  return FSN;
428 }
429 
430 SymbolNode *Demangler::demangleSpecialIntrinsic(StringView &MangledName) {
432  if (SIK == SpecialIntrinsicKind::None)
433  return nullptr;
434 
435  switch (SIK) {
437  return demangleStringLiteral(MangledName);
442  return demangleSpecialTableSymbolNode(MangledName, SIK);
444  return demangleVcallThunkNode(MangledName);
446  return demangleLocalStaticGuard(MangledName);
448  TypeNode *T = demangleType(MangledName, QualifierMangleMode::Result);
449  if (Error)
450  break;
451  if (!MangledName.consumeFront("@8"))
452  break;
453  if (!MangledName.empty())
454  break;
455  return synthesizeVariable(Arena, T, "`RTTI Type Descriptor'");
456  }
458  return demangleUntypedVariable(Arena, MangledName,
459  "`RTTI Base Class Array'");
461  return demangleUntypedVariable(Arena, MangledName,
462  "`RTTI Class Hierarchy Descriptor'");
464  return demangleRttiBaseClassDescriptorNode(Arena, MangledName);
466  return demangleInitFiniStub(MangledName, false);
468  return demangleInitFiniStub(MangledName, true);
469  default:
470  break;
471  }
472  Error = true;
473  return nullptr;
474 }
475 
477 Demangler::demangleFunctionIdentifierCode(StringView &MangledName) {
478  assert(MangledName.startsWith('?'));
479  MangledName = MangledName.dropFront();
480  if (MangledName.empty()) {
481  Error = true;
482  return nullptr;
483  }
484 
485  if (MangledName.consumeFront("__"))
486  return demangleFunctionIdentifierCode(
488  if (MangledName.consumeFront("_"))
489  return demangleFunctionIdentifierCode(MangledName,
491  return demangleFunctionIdentifierCode(MangledName,
493 }
494 
496 Demangler::demangleStructorIdentifier(StringView &MangledName,
497  bool IsDestructor) {
499  N->IsDestructor = IsDestructor;
500  return N;
501 }
502 
504 Demangler::demangleConversionOperatorIdentifier(StringView &MangledName) {
507  return N;
508 }
509 
511 Demangler::demangleLiteralOperatorIdentifier(StringView &MangledName) {
514  N->Name = demangleSimpleString(MangledName, /*Memorize=*/false);
515  return N;
516 }
517 
519 Demangler::translateIntrinsicFunctionCode(char CH,
521  using IFK = IntrinsicFunctionKind;
522  if (!(CH >= '0' && CH <= '9') && !(CH >= 'A' && CH <= 'Z')) {
523  Error = true;
524  return IFK::None;
525  }
526 
527  // Not all ? identifiers are intrinsics *functions*. This function only maps
528  // operator codes for the special functions, all others are handled elsewhere,
529  // hence the IFK::None entries in the table.
530  static IFK Basic[36] = {
531  IFK::None, // ?0 # Foo::Foo()
532  IFK::None, // ?1 # Foo::~Foo()
533  IFK::New, // ?2 # operator new
534  IFK::Delete, // ?3 # operator delete
535  IFK::Assign, // ?4 # operator=
536  IFK::RightShift, // ?5 # operator>>
537  IFK::LeftShift, // ?6 # operator<<
538  IFK::LogicalNot, // ?7 # operator!
539  IFK::Equals, // ?8 # operator==
540  IFK::NotEquals, // ?9 # operator!=
541  IFK::ArraySubscript, // ?A # operator[]
542  IFK::None, // ?B # Foo::operator <type>()
543  IFK::Pointer, // ?C # operator->
544  IFK::Dereference, // ?D # operator*
545  IFK::Increment, // ?E # operator++
546  IFK::Decrement, // ?F # operator--
547  IFK::Minus, // ?G # operator-
548  IFK::Plus, // ?H # operator+
549  IFK::BitwiseAnd, // ?I # operator&
550  IFK::MemberPointer, // ?J # operator->*
551  IFK::Divide, // ?K # operator/
552  IFK::Modulus, // ?L # operator%
553  IFK::LessThan, // ?M operator<
554  IFK::LessThanEqual, // ?N operator<=
555  IFK::GreaterThan, // ?O operator>
556  IFK::GreaterThanEqual, // ?P operator>=
557  IFK::Comma, // ?Q operator,
558  IFK::Parens, // ?R operator()
559  IFK::BitwiseNot, // ?S operator~
560  IFK::BitwiseXor, // ?T operator^
561  IFK::BitwiseOr, // ?U operator|
562  IFK::LogicalAnd, // ?V operator&&
563  IFK::LogicalOr, // ?W operator||
564  IFK::TimesEqual, // ?X operator*=
565  IFK::PlusEqual, // ?Y operator+=
566  IFK::MinusEqual, // ?Z operator-=
567  };
568  static IFK Under[36] = {
569  IFK::DivEqual, // ?_0 operator/=
570  IFK::ModEqual, // ?_1 operator%=
571  IFK::RshEqual, // ?_2 operator>>=
572  IFK::LshEqual, // ?_3 operator<<=
573  IFK::BitwiseAndEqual, // ?_4 operator&=
574  IFK::BitwiseOrEqual, // ?_5 operator|=
575  IFK::BitwiseXorEqual, // ?_6 operator^=
576  IFK::None, // ?_7 # vftable
577  IFK::None, // ?_8 # vbtable
578  IFK::None, // ?_9 # vcall
579  IFK::None, // ?_A # typeof
580  IFK::None, // ?_B # local static guard
581  IFK::None, // ?_C # string literal
582  IFK::VbaseDtor, // ?_D # vbase destructor
583  IFK::VecDelDtor, // ?_E # vector deleting destructor
584  IFK::DefaultCtorClosure, // ?_F # default constructor closure
585  IFK::ScalarDelDtor, // ?_G # scalar deleting destructor
586  IFK::VecCtorIter, // ?_H # vector constructor iterator
587  IFK::VecDtorIter, // ?_I # vector destructor iterator
588  IFK::VecVbaseCtorIter, // ?_J # vector vbase constructor iterator
589  IFK::VdispMap, // ?_K # virtual displacement map
590  IFK::EHVecCtorIter, // ?_L # eh vector constructor iterator
591  IFK::EHVecDtorIter, // ?_M # eh vector destructor iterator
592  IFK::EHVecVbaseCtorIter, // ?_N # eh vector vbase constructor iterator
593  IFK::CopyCtorClosure, // ?_O # copy constructor closure
594  IFK::None, // ?_P<name> # udt returning <name>
595  IFK::None, // ?_Q # <unknown>
596  IFK::None, // ?_R0 - ?_R4 # RTTI Codes
597  IFK::None, // ?_S # local vftable
598  IFK::LocalVftableCtorClosure, // ?_T # local vftable constructor closure
599  IFK::ArrayNew, // ?_U operator new[]
600  IFK::ArrayDelete, // ?_V operator delete[]
601  IFK::None, // ?_W <unused>
602  IFK::None, // ?_X <unused>
603  IFK::None, // ?_Y <unused>
604  IFK::None, // ?_Z <unused>
605  };
606  static IFK DoubleUnder[36] = {
607  IFK::None, // ?__0 <unused>
608  IFK::None, // ?__1 <unused>
609  IFK::None, // ?__2 <unused>
610  IFK::None, // ?__3 <unused>
611  IFK::None, // ?__4 <unused>
612  IFK::None, // ?__5 <unused>
613  IFK::None, // ?__6 <unused>
614  IFK::None, // ?__7 <unused>
615  IFK::None, // ?__8 <unused>
616  IFK::None, // ?__9 <unused>
617  IFK::ManVectorCtorIter, // ?__A managed vector ctor iterator
618  IFK::ManVectorDtorIter, // ?__B managed vector dtor iterator
619  IFK::EHVectorCopyCtorIter, // ?__C EH vector copy ctor iterator
620  IFK::EHVectorVbaseCopyCtorIter, // ?__D EH vector vbase copy ctor iter
621  IFK::None, // ?__E dynamic initializer for `T'
622  IFK::None, // ?__F dynamic atexit destructor for `T'
623  IFK::VectorCopyCtorIter, // ?__G vector copy constructor iter
624  IFK::VectorVbaseCopyCtorIter, // ?__H vector vbase copy ctor iter
625  IFK::ManVectorVbaseCopyCtorIter, // ?__I managed vector vbase copy ctor
626  // iter
627  IFK::None, // ?__J local static thread guard
628  IFK::None, // ?__K operator ""_name
629  IFK::CoAwait, // ?__L operator co_await
630  IFK::Spaceship, // ?__M operator<=>
631  IFK::None, // ?__N <unused>
632  IFK::None, // ?__O <unused>
633  IFK::None, // ?__P <unused>
634  IFK::None, // ?__Q <unused>
635  IFK::None, // ?__R <unused>
636  IFK::None, // ?__S <unused>
637  IFK::None, // ?__T <unused>
638  IFK::None, // ?__U <unused>
639  IFK::None, // ?__V <unused>
640  IFK::None, // ?__W <unused>
641  IFK::None, // ?__X <unused>
642  IFK::None, // ?__Y <unused>
643  IFK::None, // ?__Z <unused>
644  };
645 
646  int Index = (CH >= '0' && CH <= '9') ? (CH - '0') : (CH - 'A' + 10);
647  switch (Group) {
649  return Basic[Index];
651  return Under[Index];
653  return DoubleUnder[Index];
654  }
656 }
657 
659 Demangler::demangleFunctionIdentifierCode(StringView &MangledName,
661  if (MangledName.empty()) {
662  Error = true;
663  return nullptr;
664  }
665  switch (Group) {
667  switch (char CH = MangledName.popFront()) {
668  case '0':
669  case '1':
670  return demangleStructorIdentifier(MangledName, CH == '1');
671  case 'B':
672  return demangleConversionOperatorIdentifier(MangledName);
673  default:
675  translateIntrinsicFunctionCode(CH, Group));
676  }
677  break;
680  translateIntrinsicFunctionCode(MangledName.popFront(), Group));
682  switch (char CH = MangledName.popFront()) {
683  case 'K':
684  return demangleLiteralOperatorIdentifier(MangledName);
685  default:
687  translateIntrinsicFunctionCode(CH, Group));
688  }
689  }
690 
692 }
693 
694 SymbolNode *Demangler::demangleEncodedSymbol(StringView &MangledName,
696  if (MangledName.empty()) {
697  Error = true;
698  return nullptr;
699  }
700 
701  // Read a variable.
702  switch (MangledName.front()) {
703  case '0':
704  case '1':
705  case '2':
706  case '3':
707  case '4': {
708  StorageClass SC = demangleVariableStorageClass(MangledName);
709  return demangleVariableEncoding(MangledName, SC);
710  }
711  }
712  FunctionSymbolNode *FSN = demangleFunctionEncoding(MangledName);
713 
717  static_cast<ConversionOperatorIdentifierNode *>(UQN);
718  if (FSN)
719  COIN->TargetType = FSN->Signature->ReturnType;
720  }
721  return FSN;
722 }
723 
724 SymbolNode *Demangler::demangleDeclarator(StringView &MangledName) {
725  // What follows is a main symbol name. This may include namespaces or class
726  // back references.
727  QualifiedNameNode *QN = demangleFullyQualifiedSymbolName(MangledName);
728  if (Error)
729  return nullptr;
730 
731  SymbolNode *Symbol = demangleEncodedSymbol(MangledName, QN);
732  if (Error)
733  return nullptr;
734  Symbol->Name = QN;
735 
739  static_cast<ConversionOperatorIdentifierNode *>(UQN);
740  if (!COIN->TargetType) {
741  Error = true;
742  return nullptr;
743  }
744  }
745  return Symbol;
746 }
747 
748 // Parser entry point.
749 SymbolNode *Demangler::parse(StringView &MangledName) {
750  // We can't demangle MD5 names, just output them as-is.
751  // Also, MSVC-style mangled symbols must start with '?'.
752  if (MangledName.startsWith("??@")) {
753  // This is an MD5 mangled name. We can't demangle it, just return the
754  // mangled name.
756  S->Name = synthesizeQualifiedName(Arena, MangledName);
757  return S;
758  }
759 
760  if (!MangledName.startsWith('?')) {
761  Error = true;
762  return nullptr;
763  }
764 
765  MangledName.consumeFront('?');
766 
767  // ?$ is a template instantiation, but all other names that start with ? are
768  // operators / special names.
769  if (SymbolNode *SI = demangleSpecialIntrinsic(MangledName))
770  return SI;
771 
772  return demangleDeclarator(MangledName);
773 }
774 
775 TagTypeNode *Demangler::parseTagUniqueName(StringView &MangledName) {
776  if (!MangledName.consumeFront(".?A"))
777  return nullptr;
778  MangledName.consumeFront(".?A");
779  if (MangledName.empty())
780  return nullptr;
781 
782  return demangleClassType(MangledName);
783 }
784 
785 // <type-encoding> ::= <storage-class> <variable-type>
786 // <storage-class> ::= 0 # private static member
787 // ::= 1 # protected static member
788 // ::= 2 # public static member
789 // ::= 3 # global
790 // ::= 4 # static local
791 
792 VariableSymbolNode *Demangler::demangleVariableEncoding(StringView &MangledName,
793  StorageClass SC) {
795 
796  VSN->Type = demangleType(MangledName, QualifierMangleMode::Drop);
797  VSN->SC = SC;
798 
799  if (Error)
800  return nullptr;
801 
802  // <variable-type> ::= <type> <cvr-qualifiers>
803  // ::= <type> <pointee-cvr-qualifiers> # pointers, references
804  switch (VSN->Type->kind()) {
805  case NodeKind::PointerType: {
806  PointerTypeNode *PTN = static_cast<PointerTypeNode *>(VSN->Type);
807 
808  Qualifiers ExtraChildQuals = Q_None;
809  PTN->Quals = Qualifiers(VSN->Type->Quals |
810  demanglePointerExtQualifiers(MangledName));
811 
812  bool IsMember = false;
813  std::tie(ExtraChildQuals, IsMember) = demangleQualifiers(MangledName);
814 
815  if (PTN->ClassParent) {
816  QualifiedNameNode *BackRefName =
817  demangleFullyQualifiedTypeName(MangledName);
818  (void)BackRefName;
819  }
820  PTN->Pointee->Quals = Qualifiers(PTN->Pointee->Quals | ExtraChildQuals);
821 
822  break;
823  }
824  default:
825  VSN->Type->Quals = demangleQualifiers(MangledName).first;
826  break;
827  }
828 
829  return VSN;
830 }
831 
832 // Sometimes numbers are encoded in mangled symbols. For example,
833 // "int (*x)[20]" is a valid C type (x is a pointer to an array of
834 // length 20), so we need some way to embed numbers as part of symbols.
835 // This function parses it.
836 //
837 // <number> ::= [?] <non-negative integer>
838 //
839 // <non-negative integer> ::= <decimal digit> # when 1 <= Number <= 10
840 // ::= <hex digit>+ @ # when Number == 0 or >= 10
841 //
842 // <hex-digit> ::= [A-P] # A = 0, B = 1, ...
843 std::pair<uint64_t, bool> Demangler::demangleNumber(StringView &MangledName) {
844  bool IsNegative = MangledName.consumeFront('?');
845 
846  if (startsWithDigit(MangledName)) {
847  uint64_t Ret = MangledName[0] - '0' + 1;
848  MangledName = MangledName.dropFront(1);
849  return {Ret, IsNegative};
850  }
851 
852  uint64_t Ret = 0;
853  for (size_t i = 0; i < MangledName.size(); ++i) {
854  char C = MangledName[i];
855  if (C == '@') {
856  MangledName = MangledName.dropFront(i + 1);
857  return {Ret, IsNegative};
858  }
859  if ('A' <= C && C <= 'P') {
860  Ret = (Ret << 4) + (C - 'A');
861  continue;
862  }
863  break;
864  }
865 
866  Error = true;
867  return {0ULL, false};
868 }
869 
870 uint64_t Demangler::demangleUnsigned(StringView &MangledName) {
871  bool IsNegative = false;
872  uint64_t Number = 0;
873  std::tie(Number, IsNegative) = demangleNumber(MangledName);
874  if (IsNegative)
875  Error = true;
876  return Number;
877 }
878 
879 int64_t Demangler::demangleSigned(StringView &MangledName) {
880  bool IsNegative = false;
881  uint64_t Number = 0;
882  std::tie(Number, IsNegative) = demangleNumber(MangledName);
883  if (Number > INT64_MAX)
884  Error = true;
885  int64_t I = static_cast<int64_t>(Number);
886  return IsNegative ? -I : I;
887 }
888 
889 // First 10 strings can be referenced by special BackReferences ?0, ?1, ..., ?9.
890 // Memorize it.
891 void Demangler::memorizeString(StringView S) {
892  if (Backrefs.NamesCount >= BackrefContext::Max)
893  return;
894  for (size_t i = 0; i < Backrefs.NamesCount; ++i)
895  if (S == Backrefs.Names[i]->Name)
896  return;
898  N->Name = S;
899  Backrefs.Names[Backrefs.NamesCount++] = N;
900 }
901 
902 NamedIdentifierNode *Demangler::demangleBackRefName(StringView &MangledName) {
903  assert(startsWithDigit(MangledName));
904 
905  size_t I = MangledName[0] - '0';
906  if (I >= Backrefs.NamesCount) {
907  Error = true;
908  return nullptr;
909  }
910 
911  MangledName = MangledName.dropFront();
912  return Backrefs.Names[I];
913 }
914 
915 void Demangler::memorizeIdentifier(IdentifierNode *Identifier) {
916  // Render this class template name into a string buffer so that we can
917  // memorize it for the purpose of back-referencing.
918  OutputStream OS;
919  if (!initializeOutputStream(nullptr, nullptr, OS, 1024))
920  // FIXME: Propagate out-of-memory as an error?
921  std::terminate();
922  Identifier->output(OS, OF_Default);
923  OS << '\0';
924  char *Name = OS.getBuffer();
925 
926  StringView Owned = copyString(Name);
927  memorizeString(Owned);
928  std::free(Name);
929 }
930 
932 Demangler::demangleTemplateInstantiationName(StringView &MangledName,
933  NameBackrefBehavior NBB) {
934  assert(MangledName.startsWith("?$"));
935  MangledName.consumeFront("?$");
936 
937  BackrefContext OuterContext;
938  std::swap(OuterContext, Backrefs);
939 
940  IdentifierNode *Identifier =
941  demangleUnqualifiedSymbolName(MangledName, NBB_Simple);
942  if (!Error)
943  Identifier->TemplateParams = demangleTemplateParameterList(MangledName);
944 
945  std::swap(OuterContext, Backrefs);
946  if (Error)
947  return nullptr;
948 
949  if (NBB & NBB_Template) {
950  // NBB_Template is only set for types and non-leaf names ("a::" in "a::b").
951  // Structors and conversion operators only makes sense in a leaf name, so
952  // reject them in NBB_Template contexts.
953  if (Identifier->kind() == NodeKind::ConversionOperatorIdentifier ||
954  Identifier->kind() == NodeKind::StructorIdentifier) {
955  Error = true;
956  return nullptr;
957  }
958 
959  memorizeIdentifier(Identifier);
960  }
961 
962  return Identifier;
963 }
964 
965 NamedIdentifierNode *Demangler::demangleSimpleName(StringView &MangledName,
966  bool Memorize) {
967  StringView S = demangleSimpleString(MangledName, Memorize);
968  if (Error)
969  return nullptr;
970 
972  Name->Name = S;
973  return Name;
974 }
975 
976 static bool isRebasedHexDigit(char C) { return (C >= 'A' && C <= 'P'); }
977 
978 static uint8_t rebasedHexDigitToNumber(char C) {
980  return (C <= 'J') ? (C - 'A') : (10 + C - 'K');
981 }
982 
983 uint8_t Demangler::demangleCharLiteral(StringView &MangledName) {
984  assert(!MangledName.empty());
985  if (!MangledName.startsWith('?'))
986  return MangledName.popFront();
987 
988  MangledName = MangledName.dropFront();
989  if (MangledName.empty())
990  goto CharLiteralError;
991 
992  if (MangledName.consumeFront('$')) {
993  // Two hex digits
994  if (MangledName.size() < 2)
995  goto CharLiteralError;
996  StringView Nibbles = MangledName.substr(0, 2);
997  if (!isRebasedHexDigit(Nibbles[0]) || !isRebasedHexDigit(Nibbles[1]))
998  goto CharLiteralError;
999  // Don't append the null terminator.
1000  uint8_t C1 = rebasedHexDigitToNumber(Nibbles[0]);
1001  uint8_t C2 = rebasedHexDigitToNumber(Nibbles[1]);
1002  MangledName = MangledName.dropFront(2);
1003  return (C1 << 4) | C2;
1004  }
1005 
1006  if (startsWithDigit(MangledName)) {
1007  const char *Lookup = ",/\\:. \n\t'-";
1008  char C = Lookup[MangledName[0] - '0'];
1009  MangledName = MangledName.dropFront();
1010  return C;
1011  }
1012 
1013  if (MangledName[0] >= 'a' && MangledName[0] <= 'z') {
1014  char Lookup[26] = {'\xE1', '\xE2', '\xE3', '\xE4', '\xE5', '\xE6', '\xE7',
1015  '\xE8', '\xE9', '\xEA', '\xEB', '\xEC', '\xED', '\xEE',
1016  '\xEF', '\xF0', '\xF1', '\xF2', '\xF3', '\xF4', '\xF5',
1017  '\xF6', '\xF7', '\xF8', '\xF9', '\xFA'};
1018  char C = Lookup[MangledName[0] - 'a'];
1019  MangledName = MangledName.dropFront();
1020  return C;
1021  }
1022 
1023  if (MangledName[0] >= 'A' && MangledName[0] <= 'Z') {
1024  char Lookup[26] = {'\xC1', '\xC2', '\xC3', '\xC4', '\xC5', '\xC6', '\xC7',
1025  '\xC8', '\xC9', '\xCA', '\xCB', '\xCC', '\xCD', '\xCE',
1026  '\xCF', '\xD0', '\xD1', '\xD2', '\xD3', '\xD4', '\xD5',
1027  '\xD6', '\xD7', '\xD8', '\xD9', '\xDA'};
1028  char C = Lookup[MangledName[0] - 'A'];
1029  MangledName = MangledName.dropFront();
1030  return C;
1031  }
1032 
1033 CharLiteralError:
1034  Error = true;
1035  return '\0';
1036 }
1037 
1038 wchar_t Demangler::demangleWcharLiteral(StringView &MangledName) {
1039  uint8_t C1, C2;
1040 
1041  C1 = demangleCharLiteral(MangledName);
1042  if (Error || MangledName.empty())
1043  goto WCharLiteralError;
1044  C2 = demangleCharLiteral(MangledName);
1045  if (Error)
1046  goto WCharLiteralError;
1047 
1048  return ((wchar_t)C1 << 8) | (wchar_t)C2;
1049 
1050 WCharLiteralError:
1051  Error = true;
1052  return L'\0';
1053 }
1054 
1055 static void writeHexDigit(char *Buffer, uint8_t Digit) {
1056  assert(Digit <= 15);
1057  *Buffer = (Digit < 10) ? ('0' + Digit) : ('A' + Digit - 10);
1058 }
1059 
1060 static void outputHex(OutputStream &OS, unsigned C) {
1061  if (C == 0) {
1062  OS << "\\x00";
1063  return;
1064  }
1065  // It's easier to do the math if we can work from right to left, but we need
1066  // to print the numbers from left to right. So render this into a temporary
1067  // buffer first, then output the temporary buffer. Each byte is of the form
1068  // \xAB, which means that each byte needs 4 characters. Since there are at
1069  // most 4 bytes, we need a 4*4+1 = 17 character temporary buffer.
1070  char TempBuffer[17];
1071 
1072  ::memset(TempBuffer, 0, sizeof(TempBuffer));
1073  constexpr int MaxPos = sizeof(TempBuffer) - 1;
1074 
1075  int Pos = MaxPos - 1; // TempBuffer[MaxPos] is the terminating \0.
1076  while (C != 0) {
1077  for (int I = 0; I < 2; ++I) {
1078  writeHexDigit(&TempBuffer[Pos--], C % 16);
1079  C /= 16;
1080  }
1081  }
1082  TempBuffer[Pos--] = 'x';
1083  assert(Pos >= 0);
1084  TempBuffer[Pos--] = '\\';
1085  OS << StringView(&TempBuffer[Pos + 1]);
1086 }
1087 
1088 static void outputEscapedChar(OutputStream &OS, unsigned C) {
1089  switch (C) {
1090  case '\0': // nul
1091  OS << "\\0";
1092  return;
1093  case '\'': // single quote
1094  OS << "\\\'";
1095  return;
1096  case '\"': // double quote
1097  OS << "\\\"";
1098  return;
1099  case '\\': // backslash
1100  OS << "\\\\";
1101  return;
1102  case '\a': // bell
1103  OS << "\\a";
1104  return;
1105  case '\b': // backspace
1106  OS << "\\b";
1107  return;
1108  case '\f': // form feed
1109  OS << "\\f";
1110  return;
1111  case '\n': // new line
1112  OS << "\\n";
1113  return;
1114  case '\r': // carriage return
1115  OS << "\\r";
1116  return;
1117  case '\t': // tab
1118  OS << "\\t";
1119  return;
1120  case '\v': // vertical tab
1121  OS << "\\v";
1122  return;
1123  default:
1124  break;
1125  }
1126 
1127  if (C > 0x1F && C < 0x7F) {
1128  // Standard ascii char.
1129  OS << (char)C;
1130  return;
1131  }
1132 
1133  outputHex(OS, C);
1134 }
1135 
1136 static unsigned countTrailingNullBytes(const uint8_t *StringBytes, int Length) {
1137  const uint8_t *End = StringBytes + Length - 1;
1138  unsigned Count = 0;
1139  while (Length > 0 && *End == 0) {
1140  --Length;
1141  --End;
1142  ++Count;
1143  }
1144  return Count;
1145 }
1146 
1147 static unsigned countEmbeddedNulls(const uint8_t *StringBytes,
1148  unsigned Length) {
1149  unsigned Result = 0;
1150  for (unsigned I = 0; I < Length; ++I) {
1151  if (*StringBytes++ == 0)
1152  ++Result;
1153  }
1154  return Result;
1155 }
1156 
1157 // A mangled (non-wide) string literal stores the total length of the string it
1158 // refers to (passed in NumBytes), and it contains up to 32 bytes of actual text
1159 // (passed in StringBytes, NumChars).
1160 static unsigned guessCharByteSize(const uint8_t *StringBytes, unsigned NumChars,
1161  uint64_t NumBytes) {
1162  assert(NumBytes > 0);
1163 
1164  // If the number of bytes is odd, this is guaranteed to be a char string.
1165  if (NumBytes % 2 == 1)
1166  return 1;
1167 
1168  // All strings can encode at most 32 bytes of data. If it's less than that,
1169  // then we encoded the entire string. In this case we check for a 1-byte,
1170  // 2-byte, or 4-byte null terminator.
1171  if (NumBytes < 32) {
1172  unsigned TrailingNulls = countTrailingNullBytes(StringBytes, NumChars);
1173  if (TrailingNulls >= 4 && NumBytes % 4 == 0)
1174  return 4;
1175  if (TrailingNulls >= 2)
1176  return 2;
1177  return 1;
1178  }
1179 
1180  // The whole string was not able to be encoded. Try to look at embedded null
1181  // terminators to guess. The heuristic is that we count all embedded null
1182  // terminators. If more than 2/3 are null, it's a char32. If more than 1/3
1183  // are null, it's a char16. Otherwise it's a char8. This obviously isn't
1184  // perfect and is biased towards languages that have ascii alphabets, but this
1185  // was always going to be best effort since the encoding is lossy.
1186  unsigned Nulls = countEmbeddedNulls(StringBytes, NumChars);
1187  if (Nulls >= 2 * NumChars / 3 && NumBytes % 4 == 0)
1188  return 4;
1189  if (Nulls >= NumChars / 3)
1190  return 2;
1191  return 1;
1192 }
1193 
1194 static unsigned decodeMultiByteChar(const uint8_t *StringBytes,
1195  unsigned CharIndex, unsigned CharBytes) {
1196  assert(CharBytes == 1 || CharBytes == 2 || CharBytes == 4);
1197  unsigned Offset = CharIndex * CharBytes;
1198  unsigned Result = 0;
1199  StringBytes = StringBytes + Offset;
1200  for (unsigned I = 0; I < CharBytes; ++I) {
1201  unsigned C = static_cast<unsigned>(StringBytes[I]);
1202  Result |= C << (8 * I);
1203  }
1204  return Result;
1205 }
1206 
1207 FunctionSymbolNode *Demangler::demangleVcallThunkNode(StringView &MangledName) {
1210  FSN->Signature = Arena.alloc<ThunkSignatureNode>();
1212 
1213  FSN->Name = demangleNameScopeChain(MangledName, VTIN);
1214  if (!Error)
1215  Error = !MangledName.consumeFront("$B");
1216  if (!Error)
1217  VTIN->OffsetInVTable = demangleUnsigned(MangledName);
1218  if (!Error)
1219  Error = !MangledName.consumeFront('A');
1220  if (!Error)
1221  FSN->Signature->CallConvention = demangleCallingConvention(MangledName);
1222  return (Error) ? nullptr : FSN;
1223 }
1224 
1226 Demangler::demangleStringLiteral(StringView &MangledName) {
1227  // This function uses goto, so declare all variables up front.
1228  OutputStream OS;
1229  StringView CRC;
1230  uint64_t StringByteSize;
1231  bool IsWcharT = false;
1232  bool IsNegative = false;
1233  size_t CrcEndPos = 0;
1234  char *ResultBuffer = nullptr;
1235 
1237 
1238  // Must happen before the first `goto StringLiteralError`.
1239  if (!initializeOutputStream(nullptr, nullptr, OS, 1024))
1240  // FIXME: Propagate out-of-memory as an error?
1241  std::terminate();
1242 
1243  // Prefix indicating the beginning of a string literal
1244  if (!MangledName.consumeFront("@_"))
1245  goto StringLiteralError;
1246  if (MangledName.empty())
1247  goto StringLiteralError;
1248 
1249  // Char Type (regular or wchar_t)
1250  switch (MangledName.popFront()) {
1251  case '1':
1252  IsWcharT = true;
1254  case '0':
1255  break;
1256  default:
1257  goto StringLiteralError;
1258  }
1259 
1260  // Encoded Length
1261  std::tie(StringByteSize, IsNegative) = demangleNumber(MangledName);
1262  if (Error || IsNegative || StringByteSize < (IsWcharT ? 2 : 1))
1263  goto StringLiteralError;
1264 
1265  // CRC 32 (always 8 characters plus a terminator)
1266  CrcEndPos = MangledName.find('@');
1267  if (CrcEndPos == StringView::npos)
1268  goto StringLiteralError;
1269  CRC = MangledName.substr(0, CrcEndPos);
1270  MangledName = MangledName.dropFront(CrcEndPos + 1);
1271  if (MangledName.empty())
1272  goto StringLiteralError;
1273 
1274  if (IsWcharT) {
1275  Result->Char = CharKind::Wchar;
1276  if (StringByteSize > 64)
1277  Result->IsTruncated = true;
1278 
1279  while (!MangledName.consumeFront('@')) {
1280  if (MangledName.size() < 2)
1281  goto StringLiteralError;
1282  wchar_t W = demangleWcharLiteral(MangledName);
1283  if (StringByteSize != 2 || Result->IsTruncated)
1284  outputEscapedChar(OS, W);
1285  StringByteSize -= 2;
1286  if (Error)
1287  goto StringLiteralError;
1288  }
1289  } else {
1290  // The max byte length is actually 32, but some compilers mangled strings
1291  // incorrectly, so we have to assume it can go higher.
1292  constexpr unsigned MaxStringByteLength = 32 * 4;
1293  uint8_t StringBytes[MaxStringByteLength];
1294 
1295  unsigned BytesDecoded = 0;
1296  while (!MangledName.consumeFront('@')) {
1297  if (MangledName.size() < 1 || BytesDecoded >= MaxStringByteLength)
1298  goto StringLiteralError;
1299  StringBytes[BytesDecoded++] = demangleCharLiteral(MangledName);
1300  }
1301 
1302  if (StringByteSize > BytesDecoded)
1303  Result->IsTruncated = true;
1304 
1305  unsigned CharBytes =
1306  guessCharByteSize(StringBytes, BytesDecoded, StringByteSize);
1307  assert(StringByteSize % CharBytes == 0);
1308  switch (CharBytes) {
1309  case 1:
1310  Result->Char = CharKind::Char;
1311  break;
1312  case 2:
1313  Result->Char = CharKind::Char16;
1314  break;
1315  case 4:
1316  Result->Char = CharKind::Char32;
1317  break;
1318  default:
1320  }
1321  const unsigned NumChars = BytesDecoded / CharBytes;
1322  for (unsigned CharIndex = 0; CharIndex < NumChars; ++CharIndex) {
1323  unsigned NextChar =
1324  decodeMultiByteChar(StringBytes, CharIndex, CharBytes);
1325  if (CharIndex + 1 < NumChars || Result->IsTruncated)
1326  outputEscapedChar(OS, NextChar);
1327  }
1328  }
1329 
1330  OS << '\0';
1331  ResultBuffer = OS.getBuffer();
1332  Result->DecodedString = copyString(ResultBuffer);
1333  std::free(ResultBuffer);
1334  return Result;
1335 
1336 StringLiteralError:
1337  Error = true;
1338  std::free(OS.getBuffer());
1339  return nullptr;
1340 }
1341 
1342 // Returns MangledName's prefix before the first '@', or an error if
1343 // MangledName contains no '@' or the prefix has length 0.
1344 StringView Demangler::demangleSimpleString(StringView &MangledName,
1345  bool Memorize) {
1346  StringView S;
1347  for (size_t i = 0; i < MangledName.size(); ++i) {
1348  if (MangledName[i] != '@')
1349  continue;
1350  if (i == 0)
1351  break;
1352  S = MangledName.substr(0, i);
1353  MangledName = MangledName.dropFront(i + 1);
1354 
1355  if (Memorize)
1356  memorizeString(S);
1357  return S;
1358  }
1359 
1360  Error = true;
1361  return {};
1362 }
1363 
1365 Demangler::demangleAnonymousNamespaceName(StringView &MangledName) {
1366  assert(MangledName.startsWith("?A"));
1367  MangledName.consumeFront("?A");
1368 
1370  Node->Name = "`anonymous namespace'";
1371  size_t EndPos = MangledName.find('@');
1372  if (EndPos == StringView::npos) {
1373  Error = true;
1374  return nullptr;
1375  }
1376  StringView NamespaceKey = MangledName.substr(0, EndPos);
1377  memorizeString(NamespaceKey);
1378  MangledName = MangledName.substr(EndPos + 1);
1379  return Node;
1380 }
1381 
1383 Demangler::demangleLocallyScopedNamePiece(StringView &MangledName) {
1384  assert(startsWithLocalScopePattern(MangledName));
1385 
1386  NamedIdentifierNode *Identifier = Arena.alloc<NamedIdentifierNode>();
1387  MangledName.consumeFront('?');
1388  uint64_t Number = 0;
1389  bool IsNegative = false;
1390  std::tie(Number, IsNegative) = demangleNumber(MangledName);
1391  assert(!IsNegative);
1392 
1393  // One ? to terminate the number
1394  MangledName.consumeFront('?');
1395 
1396  assert(!Error);
1397  Node *Scope = parse(MangledName);
1398  if (Error)
1399  return nullptr;
1400 
1401  // Render the parent symbol's name into a buffer.
1402  OutputStream OS;
1403  if (!initializeOutputStream(nullptr, nullptr, OS, 1024))
1404  // FIXME: Propagate out-of-memory as an error?
1405  std::terminate();
1406  OS << '`';
1407  Scope->output(OS, OF_Default);
1408  OS << '\'';
1409  OS << "::`" << Number << "'";
1410  OS << '\0';
1411  char *Result = OS.getBuffer();
1412  Identifier->Name = copyString(Result);
1413  std::free(Result);
1414  return Identifier;
1415 }
1416 
1417 // Parses a type name in the form of A@B@C@@ which represents C::B::A.
1419 Demangler::demangleFullyQualifiedTypeName(StringView &MangledName) {
1420  IdentifierNode *Identifier =
1421  demangleUnqualifiedTypeName(MangledName, /*Memorize=*/true);
1422  if (Error)
1423  return nullptr;
1424  assert(Identifier);
1425 
1426  QualifiedNameNode *QN = demangleNameScopeChain(MangledName, Identifier);
1427  if (Error)
1428  return nullptr;
1429  assert(QN);
1430  return QN;
1431 }
1432 
1433 // Parses a symbol name in the form of A@B@C@@ which represents C::B::A.
1434 // Symbol names have slightly different rules regarding what can appear
1435 // so we separate out the implementations for flexibility.
1437 Demangler::demangleFullyQualifiedSymbolName(StringView &MangledName) {
1438  // This is the final component of a symbol name (i.e. the leftmost component
1439  // of a mangled name. Since the only possible template instantiation that
1440  // can appear in this context is a function template, and since those are
1441  // not saved for the purposes of name backreferences, only backref simple
1442  // names.
1443  IdentifierNode *Identifier =
1444  demangleUnqualifiedSymbolName(MangledName, NBB_Simple);
1445  if (Error)
1446  return nullptr;
1447 
1448  QualifiedNameNode *QN = demangleNameScopeChain(MangledName, Identifier);
1449  if (Error)
1450  return nullptr;
1451 
1452  if (Identifier->kind() == NodeKind::StructorIdentifier) {
1453  if (QN->Components->Count < 2) {
1454  Error = true;
1455  return nullptr;
1456  }
1457  StructorIdentifierNode *SIN =
1458  static_cast<StructorIdentifierNode *>(Identifier);
1459  Node *ClassNode = QN->Components->Nodes[QN->Components->Count - 2];
1460  SIN->Class = static_cast<IdentifierNode *>(ClassNode);
1461  }
1462  assert(QN);
1463  return QN;
1464 }
1465 
1466 IdentifierNode *Demangler::demangleUnqualifiedTypeName(StringView &MangledName,
1467  bool Memorize) {
1468  // An inner-most name can be a back-reference, because a fully-qualified name
1469  // (e.g. Scope + Inner) can contain other fully qualified names inside of
1470  // them (for example template parameters), and these nested parameters can
1471  // refer to previously mangled types.
1472  if (startsWithDigit(MangledName))
1473  return demangleBackRefName(MangledName);
1474 
1475  if (MangledName.startsWith("?$"))
1476  return demangleTemplateInstantiationName(MangledName, NBB_Template);
1477 
1478  return demangleSimpleName(MangledName, Memorize);
1479 }
1480 
1482 Demangler::demangleUnqualifiedSymbolName(StringView &MangledName,
1483  NameBackrefBehavior NBB) {
1484  if (startsWithDigit(MangledName))
1485  return demangleBackRefName(MangledName);
1486  if (MangledName.startsWith("?$"))
1487  return demangleTemplateInstantiationName(MangledName, NBB);
1488  if (MangledName.startsWith('?'))
1489  return demangleFunctionIdentifierCode(MangledName);
1490  return demangleSimpleName(MangledName, /*Memorize=*/(NBB & NBB_Simple) != 0);
1491 }
1492 
1493 IdentifierNode *Demangler::demangleNameScopePiece(StringView &MangledName) {
1494  if (startsWithDigit(MangledName))
1495  return demangleBackRefName(MangledName);
1496 
1497  if (MangledName.startsWith("?$"))
1498  return demangleTemplateInstantiationName(MangledName, NBB_Template);
1499 
1500  if (MangledName.startsWith("?A"))
1501  return demangleAnonymousNamespaceName(MangledName);
1502 
1503  if (startsWithLocalScopePattern(MangledName))
1504  return demangleLocallyScopedNamePiece(MangledName);
1505 
1506  return demangleSimpleName(MangledName, /*Memorize=*/true);
1507 }
1508 
1510  size_t Count) {
1511  NodeArrayNode *N = Arena.alloc<NodeArrayNode>();
1512  N->Count = Count;
1513  N->Nodes = Arena.allocArray<Node *>(Count);
1514  for (size_t I = 0; I < Count; ++I) {
1515  N->Nodes[I] = Head->N;
1516  Head = Head->Next;
1517  }
1518  return N;
1519 }
1520 
1522 Demangler::demangleNameScopeChain(StringView &MangledName,
1523  IdentifierNode *UnqualifiedName) {
1524  NodeList *Head = Arena.alloc<NodeList>();
1525 
1526  Head->N = UnqualifiedName;
1527 
1528  size_t Count = 1;
1529  while (!MangledName.consumeFront("@")) {
1530  ++Count;
1531  NodeList *NewHead = Arena.alloc<NodeList>();
1532  NewHead->Next = Head;
1533  Head = NewHead;
1534 
1535  if (MangledName.empty()) {
1536  Error = true;
1537  return nullptr;
1538  }
1539 
1540  assert(!Error);
1541  IdentifierNode *Elem = demangleNameScopePiece(MangledName);
1542  if (Error)
1543  return nullptr;
1544 
1545  Head->N = Elem;
1546  }
1547 
1548  QualifiedNameNode *QN = Arena.alloc<QualifiedNameNode>();
1549  QN->Components = nodeListToNodeArray(Arena, Head, Count);
1550  return QN;
1551 }
1552 
1553 FuncClass Demangler::demangleFunctionClass(StringView &MangledName) {
1554  switch (MangledName.popFront()) {
1555  case '9':
1557  case 'A':
1558  return FC_Private;
1559  case 'B':
1560  return FuncClass(FC_Private | FC_Far);
1561  case 'C':
1562  return FuncClass(FC_Private | FC_Static);
1563  case 'D':
1564  return FuncClass(FC_Private | FC_Static);
1565  case 'E':
1566  return FuncClass(FC_Private | FC_Virtual);
1567  case 'F':
1568  return FuncClass(FC_Private | FC_Virtual);
1569  case 'G':
1571  case 'H':
1573  case 'I':
1574  return FuncClass(FC_Protected);
1575  case 'J':
1576  return FuncClass(FC_Protected | FC_Far);
1577  case 'K':
1578  return FuncClass(FC_Protected | FC_Static);
1579  case 'L':
1580  return FuncClass(FC_Protected | FC_Static | FC_Far);
1581  case 'M':
1582  return FuncClass(FC_Protected | FC_Virtual);
1583  case 'N':
1585  case 'O':
1587  case 'P':
1589  case 'Q':
1590  return FuncClass(FC_Public);
1591  case 'R':
1592  return FuncClass(FC_Public | FC_Far);
1593  case 'S':
1594  return FuncClass(FC_Public | FC_Static);
1595  case 'T':
1596  return FuncClass(FC_Public | FC_Static | FC_Far);
1597  case 'U':
1598  return FuncClass(FC_Public | FC_Virtual);
1599  case 'V':
1600  return FuncClass(FC_Public | FC_Virtual | FC_Far);
1601  case 'W':
1603  case 'X':
1605  case 'Y':
1606  return FuncClass(FC_Global);
1607  case 'Z':
1608  return FuncClass(FC_Global | FC_Far);
1609  case '$': {
1611  if (MangledName.consumeFront('R'))
1612  VFlag = FuncClass(VFlag | FC_VirtualThisAdjustEx);
1613  if (MangledName.empty())
1614  break;
1615  switch (MangledName.popFront()) {
1616  case '0':
1617  return FuncClass(FC_Private | FC_Virtual | VFlag);
1618  case '1':
1619  return FuncClass(FC_Private | FC_Virtual | VFlag | FC_Far);
1620  case '2':
1621  return FuncClass(FC_Protected | FC_Virtual | VFlag);
1622  case '3':
1623  return FuncClass(FC_Protected | FC_Virtual | VFlag | FC_Far);
1624  case '4':
1625  return FuncClass(FC_Public | FC_Virtual | VFlag);
1626  case '5':
1627  return FuncClass(FC_Public | FC_Virtual | VFlag | FC_Far);
1628  }
1629  }
1630  }
1631 
1632  Error = true;
1633  return FC_Public;
1634 }
1635 
1636 CallingConv Demangler::demangleCallingConvention(StringView &MangledName) {
1637  if (MangledName.empty()) {
1638  Error = true;
1639  return CallingConv::None;
1640  }
1641 
1642  switch (MangledName.popFront()) {
1643  case 'A':
1644  case 'B':
1645  return CallingConv::Cdecl;
1646  case 'C':
1647  case 'D':
1648  return CallingConv::Pascal;
1649  case 'E':
1650  case 'F':
1651  return CallingConv::Thiscall;
1652  case 'G':
1653  case 'H':
1654  return CallingConv::Stdcall;
1655  case 'I':
1656  case 'J':
1657  return CallingConv::Fastcall;
1658  case 'M':
1659  case 'N':
1660  return CallingConv::Clrcall;
1661  case 'O':
1662  case 'P':
1663  return CallingConv::Eabi;
1664  case 'Q':
1665  return CallingConv::Vectorcall;
1666  }
1667 
1668  return CallingConv::None;
1669 }
1670 
1671 StorageClass Demangler::demangleVariableStorageClass(StringView &MangledName) {
1672  assert(std::isdigit(MangledName.front()));
1673 
1674  switch (MangledName.popFront()) {
1675  case '0':
1677  case '1':
1679  case '2':
1681  case '3':
1682  return StorageClass::Global;
1683  case '4':
1685  }
1686  Error = true;
1687  return StorageClass::None;
1688 }
1689 
1690 std::pair<Qualifiers, bool>
1691 Demangler::demangleQualifiers(StringView &MangledName) {
1692  if (MangledName.empty()) {
1693  Error = true;
1694  return std::make_pair(Q_None, false);
1695  }
1696 
1697  switch (MangledName.popFront()) {
1698  // Member qualifiers
1699  case 'Q':
1700  return std::make_pair(Q_None, true);
1701  case 'R':
1702  return std::make_pair(Q_Const, true);
1703  case 'S':
1704  return std::make_pair(Q_Volatile, true);
1705  case 'T':
1706  return std::make_pair(Qualifiers(Q_Const | Q_Volatile), true);
1707  // Non-Member qualifiers
1708  case 'A':
1709  return std::make_pair(Q_None, false);
1710  case 'B':
1711  return std::make_pair(Q_Const, false);
1712  case 'C':
1713  return std::make_pair(Q_Volatile, false);
1714  case 'D':
1715  return std::make_pair(Qualifiers(Q_Const | Q_Volatile), false);
1716  }
1717  Error = true;
1718  return std::make_pair(Q_None, false);
1719 }
1720 
1721 // <variable-type> ::= <type> <cvr-qualifiers>
1722 // ::= <type> <pointee-cvr-qualifiers> # pointers, references
1723 TypeNode *Demangler::demangleType(StringView &MangledName,
1724  QualifierMangleMode QMM) {
1725  Qualifiers Quals = Q_None;
1726  bool IsMember = false;
1727  if (QMM == QualifierMangleMode::Mangle) {
1728  std::tie(Quals, IsMember) = demangleQualifiers(MangledName);
1729  } else if (QMM == QualifierMangleMode::Result) {
1730  if (MangledName.consumeFront('?'))
1731  std::tie(Quals, IsMember) = demangleQualifiers(MangledName);
1732  }
1733 
1734  if (MangledName.empty()) {
1735  Error = true;
1736  return nullptr;
1737  }
1738 
1739  TypeNode *Ty = nullptr;
1740  if (isTagType(MangledName))
1741  Ty = demangleClassType(MangledName);
1742  else if (isPointerType(MangledName)) {
1743  if (isMemberPointer(MangledName, Error))
1744  Ty = demangleMemberPointerType(MangledName);
1745  else if (!Error)
1746  Ty = demanglePointerType(MangledName);
1747  else
1748  return nullptr;
1749  } else if (isArrayType(MangledName))
1750  Ty = demangleArrayType(MangledName);
1751  else if (isFunctionType(MangledName)) {
1752  if (MangledName.consumeFront("$$A8@@"))
1753  Ty = demangleFunctionType(MangledName, true);
1754  else {
1755  assert(MangledName.startsWith("$$A6"));
1756  MangledName.consumeFront("$$A6");
1757  Ty = demangleFunctionType(MangledName, false);
1758  }
1759  } else if (isCustomType(MangledName)) {
1760  Ty = demangleCustomType(MangledName);
1761  } else {
1762  Ty = demanglePrimitiveType(MangledName);
1763  }
1764 
1765  if (!Ty || Error)
1766  return Ty;
1767  Ty->Quals = Qualifiers(Ty->Quals | Quals);
1768  return Ty;
1769 }
1770 
1771 bool Demangler::demangleThrowSpecification(StringView &MangledName) {
1772  if (MangledName.consumeFront("_E"))
1773  return true;
1774  if (MangledName.consumeFront('Z'))
1775  return false;
1776 
1777  Error = true;
1778  return false;
1779 }
1780 
1781 FunctionSignatureNode *Demangler::demangleFunctionType(StringView &MangledName,
1782  bool HasThisQuals) {
1784 
1785  if (HasThisQuals) {
1786  FTy->Quals = demanglePointerExtQualifiers(MangledName);
1787  FTy->RefQualifier = demangleFunctionRefQualifier(MangledName);
1788  FTy->Quals = Qualifiers(FTy->Quals | demangleQualifiers(MangledName).first);
1789  }
1790 
1791  // Fields that appear on both member and non-member functions.
1792  FTy->CallConvention = demangleCallingConvention(MangledName);
1793 
1794  // <return-type> ::= <type>
1795  // ::= @ # structors (they have no declared return type)
1796  bool IsStructor = MangledName.consumeFront('@');
1797  if (!IsStructor)
1798  FTy->ReturnType = demangleType(MangledName, QualifierMangleMode::Result);
1799 
1800  FTy->Params = demangleFunctionParameterList(MangledName);
1801 
1802  FTy->IsNoexcept = demangleThrowSpecification(MangledName);
1803 
1804  return FTy;
1805 }
1806 
1808 Demangler::demangleFunctionEncoding(StringView &MangledName) {
1809  FuncClass ExtraFlags = FC_None;
1810  if (MangledName.consumeFront("$$J0"))
1811  ExtraFlags = FC_ExternC;
1812 
1813  if (MangledName.empty()) {
1814  Error = true;
1815  return nullptr;
1816  }
1817 
1818  FuncClass FC = demangleFunctionClass(MangledName);
1819  FC = FuncClass(ExtraFlags | FC);
1820 
1821  FunctionSignatureNode *FSN = nullptr;
1822  ThunkSignatureNode *TTN = nullptr;
1823  if (FC & FC_StaticThisAdjust) {
1824  TTN = Arena.alloc<ThunkSignatureNode>();
1825  TTN->ThisAdjust.StaticOffset = demangleSigned(MangledName);
1826  } else if (FC & FC_VirtualThisAdjust) {
1827  TTN = Arena.alloc<ThunkSignatureNode>();
1828  if (FC & FC_VirtualThisAdjustEx) {
1829  TTN->ThisAdjust.VBPtrOffset = demangleSigned(MangledName);
1830  TTN->ThisAdjust.VBOffsetOffset = demangleSigned(MangledName);
1831  }
1832  TTN->ThisAdjust.VtordispOffset = demangleSigned(MangledName);
1833  TTN->ThisAdjust.StaticOffset = demangleSigned(MangledName);
1834  }
1835 
1836  if (FC & FC_NoParameterList) {
1837  // This is an extern "C" function whose full signature hasn't been mangled.
1838  // This happens when we need to mangle a local symbol inside of an extern
1839  // "C" function.
1840  FSN = Arena.alloc<FunctionSignatureNode>();
1841  } else {
1842  bool HasThisQuals = !(FC & (FC_Global | FC_Static));
1843  FSN = demangleFunctionType(MangledName, HasThisQuals);
1844  }
1845 
1846  if (Error)
1847  return nullptr;
1848 
1849  if (TTN) {
1850  *static_cast<FunctionSignatureNode *>(TTN) = *FSN;
1851  FSN = TTN;
1852  }
1853  FSN->FunctionClass = FC;
1854 
1856  Symbol->Signature = FSN;
1857  return Symbol;
1858 }
1859 
1860 CustomTypeNode *Demangler::demangleCustomType(StringView &MangledName) {
1861  assert(MangledName.startsWith('?'));
1862  MangledName.popFront();
1863 
1864  CustomTypeNode *CTN = Arena.alloc<CustomTypeNode>();
1865  CTN->Identifier = demangleUnqualifiedTypeName(MangledName, /*Memorize=*/true);
1866  if (!MangledName.consumeFront('@'))
1867  Error = true;
1868  if (Error)
1869  return nullptr;
1870  return CTN;
1871 }
1872 
1873 // Reads a primitive type.
1874 PrimitiveTypeNode *Demangler::demanglePrimitiveType(StringView &MangledName) {
1875  if (MangledName.consumeFront("$$T"))
1877 
1878  switch (MangledName.popFront()) {
1879  case 'X':
1881  case 'D':
1883  case 'C':
1885  case 'E':
1887  case 'F':
1889  case 'G':
1891  case 'H':
1893  case 'I':
1895  case 'J':
1897  case 'K':
1899  case 'M':
1901  case 'N':
1903  case 'O':
1905  case '_': {
1906  if (MangledName.empty()) {
1907  Error = true;
1908  return nullptr;
1909  }
1910  switch (MangledName.popFront()) {
1911  case 'N':
1913  case 'J':
1915  case 'K':
1917  case 'W':
1919  case 'S':
1921  case 'U':
1923  }
1924  break;
1925  }
1926  }
1927  Error = true;
1928  return nullptr;
1929 }
1930 
1931 TagTypeNode *Demangler::demangleClassType(StringView &MangledName) {
1932  TagTypeNode *TT = nullptr;
1933 
1934  switch (MangledName.popFront()) {
1935  case 'T':
1936  TT = Arena.alloc<TagTypeNode>(TagKind::Union);
1937  break;
1938  case 'U':
1939  TT = Arena.alloc<TagTypeNode>(TagKind::Struct);
1940  break;
1941  case 'V':
1942  TT = Arena.alloc<TagTypeNode>(TagKind::Class);
1943  break;
1944  case 'W':
1945  if (!MangledName.consumeFront('4')) {
1946  Error = true;
1947  return nullptr;
1948  }
1949  TT = Arena.alloc<TagTypeNode>(TagKind::Enum);
1950  break;
1951  default:
1952  assert(false);
1953  }
1954 
1955  TT->QualifiedName = demangleFullyQualifiedTypeName(MangledName);
1956  return TT;
1957 }
1958 
1959 // <pointer-type> ::= E? <pointer-cvr-qualifiers> <ext-qualifiers> <type>
1960 // # the E is required for 64-bit non-static pointers
1961 PointerTypeNode *Demangler::demanglePointerType(StringView &MangledName) {
1963 
1964  std::tie(Pointer->Quals, Pointer->Affinity) =
1965  demanglePointerCVQualifiers(MangledName);
1966 
1967  if (MangledName.consumeFront("6")) {
1968  Pointer->Pointee = demangleFunctionType(MangledName, false);
1969  return Pointer;
1970  }
1971 
1972  Qualifiers ExtQuals = demanglePointerExtQualifiers(MangledName);
1973  Pointer->Quals = Qualifiers(Pointer->Quals | ExtQuals);
1974 
1975  Pointer->Pointee = demangleType(MangledName, QualifierMangleMode::Mangle);
1976  return Pointer;
1977 }
1978 
1979 PointerTypeNode *Demangler::demangleMemberPointerType(StringView &MangledName) {
1981 
1982  std::tie(Pointer->Quals, Pointer->Affinity) =
1983  demanglePointerCVQualifiers(MangledName);
1985 
1986  Qualifiers ExtQuals = demanglePointerExtQualifiers(MangledName);
1987  Pointer->Quals = Qualifiers(Pointer->Quals | ExtQuals);
1988 
1989  // isMemberPointer() only returns true if there is at least one character
1990  // after the qualifiers.
1991  if (MangledName.consumeFront("8")) {
1992  Pointer->ClassParent = demangleFullyQualifiedTypeName(MangledName);
1993  Pointer->Pointee = demangleFunctionType(MangledName, true);
1994  } else {
1995  Qualifiers PointeeQuals = Q_None;
1996  bool IsMember = false;
1997  std::tie(PointeeQuals, IsMember) = demangleQualifiers(MangledName);
1998  assert(IsMember || Error);
1999  Pointer->ClassParent = demangleFullyQualifiedTypeName(MangledName);
2000 
2001  Pointer->Pointee = demangleType(MangledName, QualifierMangleMode::Drop);
2002  if (Pointer->Pointee)
2003  Pointer->Pointee->Quals = PointeeQuals;
2004  }
2005 
2006  return Pointer;
2007 }
2008 
2009 Qualifiers Demangler::demanglePointerExtQualifiers(StringView &MangledName) {
2010  Qualifiers Quals = Q_None;
2011  if (MangledName.consumeFront('E'))
2012  Quals = Qualifiers(Quals | Q_Pointer64);
2013  if (MangledName.consumeFront('I'))
2014  Quals = Qualifiers(Quals | Q_Restrict);
2015  if (MangledName.consumeFront('F'))
2016  Quals = Qualifiers(Quals | Q_Unaligned);
2017 
2018  return Quals;
2019 }
2020 
2021 ArrayTypeNode *Demangler::demangleArrayType(StringView &MangledName) {
2022  assert(MangledName.front() == 'Y');
2023  MangledName.popFront();
2024 
2025  uint64_t Rank = 0;
2026  bool IsNegative = false;
2027  std::tie(Rank, IsNegative) = demangleNumber(MangledName);
2028  if (IsNegative || Rank == 0) {
2029  Error = true;
2030  return nullptr;
2031  }
2032 
2033  ArrayTypeNode *ATy = Arena.alloc<ArrayTypeNode>();
2034  NodeList *Head = Arena.alloc<NodeList>();
2035  NodeList *Tail = Head;
2036 
2037  for (uint64_t I = 0; I < Rank; ++I) {
2038  uint64_t D = 0;
2039  std::tie(D, IsNegative) = demangleNumber(MangledName);
2040  if (Error || IsNegative) {
2041  Error = true;
2042  return nullptr;
2043  }
2044  Tail->N = Arena.alloc<IntegerLiteralNode>(D, IsNegative);
2045  if (I + 1 < Rank) {
2046  Tail->Next = Arena.alloc<NodeList>();
2047  Tail = Tail->Next;
2048  }
2049  }
2050  ATy->Dimensions = nodeListToNodeArray(Arena, Head, Rank);
2051 
2052  if (MangledName.consumeFront("$$C")) {
2053  bool IsMember = false;
2054  std::tie(ATy->Quals, IsMember) = demangleQualifiers(MangledName);
2055  if (IsMember) {
2056  Error = true;
2057  return nullptr;
2058  }
2059  }
2060 
2061  ATy->ElementType = demangleType(MangledName, QualifierMangleMode::Drop);
2062  return ATy;
2063 }
2064 
2065 // Reads a function or a template parameters.
2066 NodeArrayNode *
2067 Demangler::demangleFunctionParameterList(StringView &MangledName) {
2068  // Empty parameter list.
2069  if (MangledName.consumeFront('X'))
2070  return nullptr;
2071 
2072  NodeList *Head = Arena.alloc<NodeList>();
2073  NodeList **Current = &Head;
2074  size_t Count = 0;
2075  while (!Error && !MangledName.startsWith('@') &&
2076  !MangledName.startsWith('Z')) {
2077  ++Count;
2078 
2079  if (startsWithDigit(MangledName)) {
2080  size_t N = MangledName[0] - '0';
2081  if (N >= Backrefs.FunctionParamCount) {
2082  Error = true;
2083  return nullptr;
2084  }
2085  MangledName = MangledName.dropFront();
2086 
2087  *Current = Arena.alloc<NodeList>();
2088  (*Current)->N = Backrefs.FunctionParams[N];
2089  Current = &(*Current)->Next;
2090  continue;
2091  }
2092 
2093  size_t OldSize = MangledName.size();
2094 
2095  *Current = Arena.alloc<NodeList>();
2096  TypeNode *TN = demangleType(MangledName, QualifierMangleMode::Drop);
2097  if (!TN || Error)
2098  return nullptr;
2099 
2100  (*Current)->N = TN;
2101 
2102  size_t CharsConsumed = OldSize - MangledName.size();
2103  assert(CharsConsumed != 0);
2104 
2105  // Single-letter types are ignored for backreferences because memorizing
2106  // them doesn't save anything.
2107  if (Backrefs.FunctionParamCount <= 9 && CharsConsumed > 1)
2108  Backrefs.FunctionParams[Backrefs.FunctionParamCount++] = TN;
2109 
2110  Current = &(*Current)->Next;
2111  }
2112 
2113  if (Error)
2114  return nullptr;
2115 
2116  NodeArrayNode *NA = nodeListToNodeArray(Arena, Head, Count);
2117  // A non-empty parameter list is terminated by either 'Z' (variadic) parameter
2118  // list or '@' (non variadic). Careful not to consume "@Z", as in that case
2119  // the following Z could be a throw specifier.
2120  if (MangledName.consumeFront('@'))
2121  return NA;
2122 
2123  if (MangledName.consumeFront('Z')) {
2124  // This is a variadic parameter list. We probably need a variadic node to
2125  // append to the end.
2126  return NA;
2127  }
2128 
2129  Error = true;
2130  return nullptr;
2131 }
2132 
2133 NodeArrayNode *
2134 Demangler::demangleTemplateParameterList(StringView &MangledName) {
2135  NodeList *Head;
2136  NodeList **Current = &Head;
2137  size_t Count = 0;
2138 
2139  while (!Error && !MangledName.startsWith('@')) {
2140  if (MangledName.consumeFront("$S") || MangledName.consumeFront("$$V") ||
2141  MangledName.consumeFront("$$$V") || MangledName.consumeFront("$$Z")) {
2142  // parameter pack separator
2143  continue;
2144  }
2145 
2146  ++Count;
2147 
2148  // Template parameter lists don't participate in back-referencing.
2149  *Current = Arena.alloc<NodeList>();
2150 
2151  NodeList &TP = **Current;
2152 
2153  TemplateParameterReferenceNode *TPRN = nullptr;
2154  if (MangledName.consumeFront("$$Y")) {
2155  // Template alias
2156  TP.N = demangleFullyQualifiedTypeName(MangledName);
2157  } else if (MangledName.consumeFront("$$B")) {
2158  // Array
2159  TP.N = demangleType(MangledName, QualifierMangleMode::Drop);
2160  } else if (MangledName.consumeFront("$$C")) {
2161  // Type has qualifiers.
2162  TP.N = demangleType(MangledName, QualifierMangleMode::Mangle);
2163  } else if (MangledName.startsWith("$1") || MangledName.startsWith("$H") ||
2164  MangledName.startsWith("$I") || MangledName.startsWith("$J")) {
2165  // Pointer to member
2166  TP.N = TPRN = Arena.alloc<TemplateParameterReferenceNode>();
2167  TPRN->IsMemberPointer = true;
2168 
2169  MangledName = MangledName.dropFront();
2170  // 1 - single inheritance <name>
2171  // H - multiple inheritance <name> <number>
2172  // I - virtual inheritance <name> <number> <number> <number>
2173  // J - unspecified inheritance <name> <number> <number> <number>
2174  char InheritanceSpecifier = MangledName.popFront();
2175  SymbolNode *S = nullptr;
2176  if (MangledName.startsWith('?')) {
2177  S = parse(MangledName);
2178  if (Error || !S->Name) {
2179  Error = true;
2180  return nullptr;
2181  }
2182  memorizeIdentifier(S->Name->getUnqualifiedIdentifier());
2183  }
2184 
2185  switch (InheritanceSpecifier) {
2186  case 'J':
2187  TPRN->ThunkOffsets[TPRN->ThunkOffsetCount++] =
2188  demangleSigned(MangledName);
2190  case 'I':
2191  TPRN->ThunkOffsets[TPRN->ThunkOffsetCount++] =
2192  demangleSigned(MangledName);
2194  case 'H':
2195  TPRN->ThunkOffsets[TPRN->ThunkOffsetCount++] =
2196  demangleSigned(MangledName);
2198  case '1':
2199  break;
2200  default:
2201  Error = true;
2202  break;
2203  }
2205  TPRN->Symbol = S;
2206  } else if (MangledName.startsWith("$E?")) {
2207  MangledName.consumeFront("$E");
2208  // Reference to symbol
2209  TP.N = TPRN = Arena.alloc<TemplateParameterReferenceNode>();
2210  TPRN->Symbol = parse(MangledName);
2212  } else if (MangledName.startsWith("$F") || MangledName.startsWith("$G")) {
2213  TP.N = TPRN = Arena.alloc<TemplateParameterReferenceNode>();
2214 
2215  // Data member pointer.
2216  MangledName = MangledName.dropFront();
2217  char InheritanceSpecifier = MangledName.popFront();
2218 
2219  switch (InheritanceSpecifier) {
2220  case 'G':
2221  TPRN->ThunkOffsets[TPRN->ThunkOffsetCount++] =
2222  demangleSigned(MangledName);
2224  case 'F':
2225  TPRN->ThunkOffsets[TPRN->ThunkOffsetCount++] =
2226  demangleSigned(MangledName);
2227  TPRN->ThunkOffsets[TPRN->ThunkOffsetCount++] =
2228  demangleSigned(MangledName);
2230  case '0':
2231  break;
2232  default:
2233  Error = true;
2234  break;
2235  }
2236  TPRN->IsMemberPointer = true;
2237 
2238  } else if (MangledName.consumeFront("$0")) {
2239  // Integral non-type template parameter
2240  bool IsNegative = false;
2241  uint64_t Value = 0;
2242  std::tie(Value, IsNegative) = demangleNumber(MangledName);
2243 
2244  TP.N = Arena.alloc<IntegerLiteralNode>(Value, IsNegative);
2245  } else {
2246  TP.N = demangleType(MangledName, QualifierMangleMode::Drop);
2247  }
2248  if (Error)
2249  return nullptr;
2250 
2251  Current = &TP.Next;
2252  }
2253 
2254  if (Error)
2255  return nullptr;
2256 
2257  // Template parameter lists cannot be variadic, so it can only be terminated
2258  // by @.
2259  if (MangledName.consumeFront('@'))
2260  return nodeListToNodeArray(Arena, Head, Count);
2261  Error = true;
2262  return nullptr;
2263 }
2264 
2266  std::printf("%d function parameter backreferences\n",
2267  (int)Backrefs.FunctionParamCount);
2268 
2269  // Create an output stream so we can render each type.
2270  OutputStream OS;
2271  if (!initializeOutputStream(nullptr, nullptr, OS, 1024))
2272  std::terminate();
2273  for (size_t I = 0; I < Backrefs.FunctionParamCount; ++I) {
2274  OS.setCurrentPosition(0);
2275 
2276  TypeNode *T = Backrefs.FunctionParams[I];
2277  T->output(OS, OF_Default);
2278 
2279  std::printf(" [%d] - %.*s\n", (int)I, (int)OS.getCurrentPosition(),
2280  OS.getBuffer());
2281  }
2282  std::free(OS.getBuffer());
2283 
2284  if (Backrefs.FunctionParamCount > 0)
2285  std::printf("\n");
2286  std::printf("%d name backreferences\n", (int)Backrefs.NamesCount);
2287  for (size_t I = 0; I < Backrefs.NamesCount; ++I) {
2288  std::printf(" [%d] - %.*s\n", (int)I, (int)Backrefs.Names[I]->Name.size(),
2289  Backrefs.Names[I]->Name.begin());
2290  }
2291  if (Backrefs.NamesCount > 0)
2292  std::printf("\n");
2293 }
2294 
2295 char *llvm::microsoftDemangle(const char *MangledName, char *Buf, size_t *N,
2296  int *Status, MSDemangleFlags Flags) {
2297  int InternalStatus = demangle_success;
2298  Demangler D;
2299  OutputStream S;
2300 
2301  StringView Name{MangledName};
2302  SymbolNode *AST = D.parse(Name);
2303 
2304  if (Flags & MSDF_DumpBackrefs)
2305  D.dumpBackReferences();
2306 
2307  if (D.Error)
2308  InternalStatus = demangle_invalid_mangled_name;
2309  else if (!initializeOutputStream(Buf, N, S, 1024))
2310  InternalStatus = demangle_memory_alloc_failure;
2311  else {
2312  AST->output(S, OF_Default);
2313  S += '\0';
2314  if (N != nullptr)
2315  *N = S.getCurrentPosition();
2316  Buf = S.getBuffer();
2317  }
2318 
2319  if (Status)
2320  *Status = InternalStatus;
2321  return InternalStatus == demangle_success ? Buf : nullptr;
2322 }
static VariableSymbolNode * synthesizeVariable(ArenaAllocator &Arena, TypeNode *Type, StringView VariableName)
static bool isFunctionType(StringView S)
uint64_t CallInst * C
static void outputEscapedChar(OutputStream &OS, unsigned C)
void output(OutputStream &OS, OutputFlags Flags) const override
static uint8_t rebasedHexDigitToNumber(char C)
static bool isArrayType(StringView S)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static unsigned countTrailingNullBytes(const uint8_t *StringBytes, int Length)
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
static SpecialIntrinsicKind consumeSpecialIntrinsicKind(StringView &MangledName)
void output(OutputStream &OS, OutputFlags Flags) const override
static bool isMemberPointer(StringView MangledName, bool &Error)
SymbolNode * parse(StringView &MangledName)
static void outputHex(OutputStream &OS, unsigned C)
static bool isTagType(StringView S)
static int Lookup(ArrayRef< TableEntry > Table, unsigned Opcode)
#define INT64_MAX
Definition: DataTypes.h:77
static unsigned decodeMultiByteChar(const uint8_t *StringBytes, unsigned CharIndex, unsigned CharBytes)
static FunctionRefQualifier demangleFunctionRefQualifier(StringView &MangledName)
T * alloc(Args &&... ConstructorArgs)
static bool isPointerType(StringView S)
TagTypeNode * parseTagUniqueName(StringView &MangledName)
static bool isCustomType(StringView S)
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
static NamedIdentifierNode * synthesizeNamedIdentifier(ArenaAllocator &Arena, StringView Name)
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
static bool startsWithDigit(StringView S)
static bool isRebasedHexDigit(char C)
#define DEMANGLE_UNREACHABLE
static unsigned countEmbeddedNulls(const uint8_t *StringBytes, unsigned Length)
MSDemangleFlags
Definition: Demangle.h:35
static NodeArrayNode * nodeListToNodeArray(ArenaAllocator &Arena, NodeList *Head, size_t Count)
llvm::Expected< Value > parse(llvm::StringRef JSON)
Parses the provided JSON source, or returns a ParseError.
Definition: JSON.cpp:510
NodeList * Next
unsigned first
uint32_t Number
Definition: Profile.cpp:47
#define DEMANGLE_FALLTHROUGH
static unsigned guessCharByteSize(const uint8_t *StringBytes, unsigned NumChars, uint64_t NumBytes)
CHAIN = SC CHAIN, Imm128 - System call.
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
Qualifiers
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:940
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
static bool startsWithLocalScopePattern(StringView S)
static const size_t npos
Definition: StringView.h:28
static QualifiedNameNode * synthesizeQualifiedName(ArenaAllocator &Arena, IdentifierNode *Identifier)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:72
bool initializeOutputStream(char *Buf, size_t *N, OutputStream &S, size_t InitSize)
Definition: Utility.h:174
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
static std::pair< Qualifiers, PointerAffinity > demanglePointerCVQualifiers(StringView &MangledName)
virtual void output(OutputStream &OS, OutputFlags Flags) const =0
static void writeHexDigit(char *Buffer, uint8_t Digit)
char * microsoftDemangle(const char *mangled_name, char *buf, size_t *n, int *status, MSDemangleFlags Flags=MSDF_None)