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