LLVM 20.0.0git
MicrosoftDemangleNodes.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//===----------------------------------------------------------------------===//
12
15#include <cctype>
16#include <string>
17
18using namespace llvm;
19using namespace ms_demangle;
20
21#define OUTPUT_ENUM_CLASS_VALUE(Enum, Value, Desc) \
22 case Enum::Value: \
23 OB << Desc; \
24 break;
25
26// Writes a space if the last token does not end with a punctuation.
28 if (OB.empty())
29 return;
30
31 char C = OB.back();
32 if (std::isalnum(C) || C == '>')
33 OB << " ";
34}
35
37 switch (Q) {
38 case Q_Const:
39 OB << "const";
40 break;
41 case Q_Volatile:
42 OB << "volatile";
43 break;
44 case Q_Restrict:
45 OB << "__restrict";
46 break;
47 default:
48 break;
49 }
50}
51
53 Qualifiers Mask, bool NeedSpace) {
54 if (!(Q & Mask))
55 return NeedSpace;
56
57 if (NeedSpace)
58 OB << " ";
59
60 outputSingleQualifier(OB, Mask);
61 return true;
62}
63
64static void outputQualifiers(OutputBuffer &OB, Qualifiers Q, bool SpaceBefore,
65 bool SpaceAfter) {
66 if (Q == Q_None)
67 return;
68
69 size_t Pos1 = OB.getCurrentPosition();
70 SpaceBefore = outputQualifierIfPresent(OB, Q, Q_Const, SpaceBefore);
71 SpaceBefore = outputQualifierIfPresent(OB, Q, Q_Volatile, SpaceBefore);
72 SpaceBefore = outputQualifierIfPresent(OB, Q, Q_Restrict, SpaceBefore);
73 size_t Pos2 = OB.getCurrentPosition();
74 if (SpaceAfter && Pos2 > Pos1)
75 OB << " ";
76}
77
80
81 switch (CC) {
82 case CallingConv::Cdecl:
83 OB << "__cdecl";
84 break;
85 case CallingConv::Fastcall:
86 OB << "__fastcall";
87 break;
88 case CallingConv::Pascal:
89 OB << "__pascal";
90 break;
91 case CallingConv::Regcall:
92 OB << "__regcall";
93 break;
94 case CallingConv::Stdcall:
95 OB << "__stdcall";
96 break;
97 case CallingConv::Thiscall:
98 OB << "__thiscall";
99 break;
100 case CallingConv::Eabi:
101 OB << "__eabi";
102 break;
103 case CallingConv::Vectorcall:
104 OB << "__vectorcall";
105 break;
106 case CallingConv::Clrcall:
107 OB << "__clrcall";
108 break;
110 OB << "__attribute__((__swiftcall__)) ";
111 break;
112 case CallingConv::SwiftAsync:
113 OB << "__attribute__((__swiftasynccall__)) ";
114 break;
115 default:
116 break;
117 }
118}
119
120std::string Node::toString(OutputFlags Flags) const {
121 OutputBuffer OB;
122 this->output(OB, Flags);
123 std::string_view SV = OB;
124 std::string Owned(SV.begin(), SV.end());
125 std::free(OB.getBuffer());
126 return Owned;
127}
128
129void PrimitiveTypeNode::outputPre(OutputBuffer &OB, OutputFlags Flags) const {
130 switch (PrimKind) {
140 OUTPUT_ENUM_CLASS_VALUE(PrimitiveKind, Ushort, "unsigned short");
146 OUTPUT_ENUM_CLASS_VALUE(PrimitiveKind, Uint64, "unsigned __int64");
154 }
155 outputQualifiers(OB, Quals, true, false);
156}
157
158void NodeArrayNode::output(OutputBuffer &OB, OutputFlags Flags) const {
159 output(OB, Flags, ", ");
160}
161
162void NodeArrayNode::output(OutputBuffer &OB, OutputFlags Flags,
163 std::string_view Separator) const {
164 if (Count == 0)
165 return;
166 if (Nodes[0])
167 Nodes[0]->output(OB, Flags);
168 for (size_t I = 1; I < Count; ++I) {
169 OB << Separator;
170 Nodes[I]->output(OB, Flags);
171 }
172}
173
175 OutputFlags Flags) const {
176 switch (Char) {
177 case CharKind::Wchar:
178 OB << "L\"";
179 break;
180 case CharKind::Char:
181 OB << "\"";
182 break;
183 case CharKind::Char16:
184 OB << "u\"";
185 break;
186 case CharKind::Char32:
187 OB << "U\"";
188 break;
189 }
190 OB << DecodedString << "\"";
191 if (IsTruncated)
192 OB << "...";
193}
194
195void IntegerLiteralNode::output(OutputBuffer &OB, OutputFlags Flags) const {
196 if (IsNegative)
197 OB << '-';
198 OB << Value;
199}
200
202 OutputFlags Flags) const {
203 if (ThunkOffsetCount > 0)
204 OB << "{";
206 OB << "&";
207
208 if (Symbol) {
209 Symbol->output(OB, Flags);
210 if (ThunkOffsetCount > 0)
211 OB << ", ";
212 }
213
214 if (ThunkOffsetCount > 0)
215 OB << ThunkOffsets[0];
216 for (int I = 1; I < ThunkOffsetCount; ++I) {
217 OB << ", " << ThunkOffsets[I];
218 }
219 if (ThunkOffsetCount > 0)
220 OB << "}";
221}
222
224 OutputFlags Flags) const {
225 if (!TemplateParams)
226 return;
227 OB << "<";
228 TemplateParams->output(OB, Flags);
229 OB << ">";
230}
231
233 OutputFlags Flags) const {
234 if (IsDestructor)
235 OB << "`dynamic atexit destructor for ";
236 else
237 OB << "`dynamic initializer for ";
238
239 if (Variable) {
240 OB << "`";
241 Variable->output(OB, Flags);
242 OB << "''";
243 } else {
244 OB << "'";
245 Name->output(OB, Flags);
246 OB << "''";
247 }
248}
249
250void NamedIdentifierNode::output(OutputBuffer &OB, OutputFlags Flags) const {
251 OB << Name;
252 outputTemplateParameters(OB, Flags);
253}
254
256 OutputFlags Flags) const {
257 switch (Operator) {
267 "operator[]");
276 "operator->*");
283 "operator>=");
299 "operator&=");
301 "operator|=");
303 "operator^=");
306 "`vector deleting dtor'");
308 "`default ctor closure'");
310 "`scalar deleting dtor'");
312 "`vector ctor iterator'");
314 "`vector dtor iterator'");
316 "`vector vbase ctor iterator'");
318 "`virtual displacement map'");
320 "`eh vector ctor iterator'");
322 "`eh vector dtor iterator'");
324 "`eh vector vbase ctor iterator'");
326 "`copy ctor closure'");
328 "`local vftable ctor closure'");
331 "operator delete[]");
333 "`managed vector ctor iterator'");
335 "`managed vector dtor iterator'");
337 "`EH vector copy ctor iterator'");
339 "`EH vector vbase copy ctor iterator'");
341 "`vector copy ctor iterator'");
343 "`vector vbase copy constructor iterator'");
345 "`managed vector vbase copy constructor iterator'");
347 "operator co_await");
351 break;
352 }
353 outputTemplateParameters(OB, Flags);
354}
355
357 OutputFlags Flags) const {
358 if (IsThread)
359 OB << "`local static thread guard'";
360 else
361 OB << "`local static guard'";
362 if (ScopeIndex > 0)
363 OB << "{" << ScopeIndex << "}";
364}
365
367 OutputFlags Flags) const {
368 OB << "operator";
369 outputTemplateParameters(OB, Flags);
370 OB << " ";
371 TargetType->output(OB, Flags);
372}
373
374void StructorIdentifierNode::output(OutputBuffer &OB, OutputFlags Flags) const {
375 if (IsDestructor)
376 OB << "~";
377 Class->output(OB, Flags);
378 outputTemplateParameters(OB, Flags);
379}
380
382 OutputFlags Flags) const {
383 OB << "operator \"\"" << Name;
384 outputTemplateParameters(OB, Flags);
385}
386
388 OutputFlags Flags) const {
389 if (!(Flags & OF_NoAccessSpecifier)) {
391 OB << "public: ";
393 OB << "protected: ";
395 OB << "private: ";
396 }
397
398 if (!(Flags & OF_NoMemberType)) {
399 if (!(FunctionClass & FC_Global)) {
401 OB << "static ";
402 }
404 OB << "virtual ";
405
407 OB << "extern \"C\" ";
408 }
409
410 if (!(Flags & OF_NoReturnType) && ReturnType) {
411 ReturnType->outputPre(OB, Flags);
412 OB << " ";
413 }
414
415 if (!(Flags & OF_NoCallingConvention))
417}
418
420 OutputFlags Flags) const {
422 OB << "(";
423 if (Params)
424 Params->output(OB, Flags);
425 else
426 OB << "void";
427
428 if (IsVariadic) {
429 if (OB.back() != '(')
430 OB << ", ";
431 OB << "...";
432 }
433 OB << ")";
434 }
435
436 if (Quals & Q_Const)
437 OB << " const";
438 if (Quals & Q_Volatile)
439 OB << " volatile";
440 if (Quals & Q_Restrict)
441 OB << " __restrict";
442 if (Quals & Q_Unaligned)
443 OB << " __unaligned";
444
445 if (IsNoexcept)
446 OB << " noexcept";
447
449 OB << " &";
451 OB << " &&";
452
453 if (!(Flags & OF_NoReturnType) && ReturnType)
454 ReturnType->outputPost(OB, Flags);
455}
456
457void ThunkSignatureNode::outputPre(OutputBuffer &OB, OutputFlags Flags) const {
458 OB << "[thunk]: ";
459
461}
462
463void ThunkSignatureNode::outputPost(OutputBuffer &OB, OutputFlags Flags) const {
465 OB << "`adjustor{" << ThisAdjust.StaticOffset << "}'";
466 } else if (FunctionClass & FC_VirtualThisAdjust) {
468 OB << "`vtordispex{" << ThisAdjust.VBPtrOffset << ", "
470 << ", " << ThisAdjust.StaticOffset << "}'";
471 } else {
472 OB << "`vtordisp{" << ThisAdjust.VtordispOffset << ", "
473 << ThisAdjust.StaticOffset << "}'";
474 }
475 }
476
478}
479
480void PointerTypeNode::outputPre(OutputBuffer &OB, OutputFlags Flags) const {
482 // If this is a pointer to a function, don't output the calling convention.
483 // It needs to go inside the parentheses.
484 const FunctionSignatureNode *Sig =
485 static_cast<const FunctionSignatureNode *>(Pointee);
487 } else
488 Pointee->outputPre(OB, Flags);
489
491
492 if (Quals & Q_Unaligned)
493 OB << "__unaligned ";
494
495 if (Pointee->kind() == NodeKind::ArrayType) {
496 OB << "(";
497 } else if (Pointee->kind() == NodeKind::FunctionSignature) {
498 OB << "(";
499 const FunctionSignatureNode *Sig =
500 static_cast<const FunctionSignatureNode *>(Pointee);
502 OB << " ";
503 }
504
505 if (ClassParent) {
506 ClassParent->output(OB, Flags);
507 OB << "::";
508 }
509
510 switch (Affinity) {
512 OB << "*";
513 break;
515 OB << "&";
516 break;
518 OB << "&&";
519 break;
520 default:
521 assert(false);
522 }
523 outputQualifiers(OB, Quals, false, false);
524}
525
526void PointerTypeNode::outputPost(OutputBuffer &OB, OutputFlags Flags) const {
527 if (Pointee->kind() == NodeKind::ArrayType ||
529 OB << ")";
530
531 Pointee->outputPost(OB, Flags);
532}
533
534void TagTypeNode::outputPre(OutputBuffer &OB, OutputFlags Flags) const {
535 if (!(Flags & OF_NoTagSpecifier)) {
536 switch (Tag) {
541 }
542 OB << " ";
543 }
544 QualifiedName->output(OB, Flags);
545 outputQualifiers(OB, Quals, true, false);
546}
547
548void TagTypeNode::outputPost(OutputBuffer &OB, OutputFlags Flags) const {}
549
550void ArrayTypeNode::outputPre(OutputBuffer &OB, OutputFlags Flags) const {
551 ElementType->outputPre(OB, Flags);
552 outputQualifiers(OB, Quals, true, false);
553}
554
556 Node *N) const {
558 IntegerLiteralNode *ILN = static_cast<IntegerLiteralNode *>(N);
559 if (ILN->Value != 0)
560 ILN->output(OB, Flags);
561}
562
564 OutputFlags Flags) const {
565 if (Dimensions->Count == 0)
566 return;
567
568 outputOneDimension(OB, Flags, Dimensions->Nodes[0]);
569 for (size_t I = 1; I < Dimensions->Count; ++I) {
570 OB << "][";
571 outputOneDimension(OB, Flags, Dimensions->Nodes[I]);
572 }
573}
574
575void ArrayTypeNode::outputPost(OutputBuffer &OB, OutputFlags Flags) const {
576 OB << "[";
577 outputDimensionsImpl(OB, Flags);
578 OB << "]";
579
580 ElementType->outputPost(OB, Flags);
581}
582
583void SymbolNode::output(OutputBuffer &OB, OutputFlags Flags) const {
584 Name->output(OB, Flags);
585}
586
587void FunctionSymbolNode::output(OutputBuffer &OB, OutputFlags Flags) const {
588 Signature->outputPre(OB, Flags);
590 Name->output(OB, Flags);
591 Signature->outputPost(OB, Flags);
592}
593
594void VariableSymbolNode::output(OutputBuffer &OB, OutputFlags Flags) const {
595 const char *AccessSpec = nullptr;
596 bool IsStatic = true;
597 switch (SC) {
599 AccessSpec = "private";
600 break;
602 AccessSpec = "public";
603 break;
605 AccessSpec = "protected";
606 break;
607 default:
608 IsStatic = false;
609 break;
610 }
611 if (!(Flags & OF_NoAccessSpecifier) && AccessSpec)
612 OB << AccessSpec << ": ";
613 if (!(Flags & OF_NoMemberType) && IsStatic)
614 OB << "static ";
615
616 if (!(Flags & OF_NoVariableType) && Type) {
617 Type->outputPre(OB, Flags);
619 }
620 Name->output(OB, Flags);
621 if (!(Flags & OF_NoVariableType) && Type)
622 Type->outputPost(OB, Flags);
623}
624
625void CustomTypeNode::outputPre(OutputBuffer &OB, OutputFlags Flags) const {
626 Identifier->output(OB, Flags);
627}
628void CustomTypeNode::outputPost(OutputBuffer &OB, OutputFlags Flags) const {}
629
630void QualifiedNameNode::output(OutputBuffer &OB, OutputFlags Flags) const {
631 Components->output(OB, Flags, "::");
632}
633
635 OutputFlags Flags) const {
636 OB << "`RTTI Base Class Descriptor at (";
637 OB << NVOffset << ", " << VBPtrOffset << ", " << VBTableOffset << ", "
638 << this->Flags;
639 OB << ")'";
640}
641
643 OutputFlags Flags) const {
644 Name->output(OB, Flags);
645}
646
648 OutputFlags Flags) const {
649 OB << "`vcall'{" << OffsetInVTable << ", {flat}}";
650}
651
652void SpecialTableSymbolNode::output(OutputBuffer &OB, OutputFlags Flags) const {
653 outputQualifiers(OB, Quals, false, true);
654 Name->output(OB, Flags);
655 if (TargetName) {
656 OB << "{for `";
657 TargetName->output(OB, Flags);
658 OB << "'}";
659 }
660}
Qualifiers
#define I(x, y, z)
Definition: MD5.cpp:58
static void outputCallingConvention(OutputBuffer &OB, CallingConv CC)
#define OUTPUT_ENUM_CLASS_VALUE(Enum, Value, Desc)
static void outputSingleQualifier(OutputBuffer &OB, Qualifiers Q)
static bool outputQualifierIfPresent(OutputBuffer &OB, Qualifiers Q, Qualifiers Mask, bool NeedSpace)
static void outputQualifiers(OutputBuffer &OB, Qualifiers Q, bool SpaceBefore, bool SpaceAfter)
static void outputSpaceIfNecessary(OutputBuffer &OB)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This is a utility class that provides an abstraction for the common functionality between Instruction...
Definition: Operator.h:32
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
@ Swift
Calling convention for Swift.
Definition: CallingConv.h:69
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
#define N
void outputPost(OutputBuffer &OB, OutputFlags Flags) const override
void outputOneDimension(OutputBuffer &OB, OutputFlags Flags, Node *N) const
void outputPre(OutputBuffer &OB, OutputFlags Flags) const override
void outputDimensionsImpl(OutputBuffer &OB, OutputFlags Flags) const
void output(OutputBuffer &OB, OutputFlags Flags) const override
void outputPost(OutputBuffer &OB, OutputFlags Flags) const override
void outputPre(OutputBuffer &OB, OutputFlags Flags) const override
void output(OutputBuffer &OB, OutputFlags Flags) const override
void output(OutputBuffer &OB, OutputFlags Flags) const override
void outputPre(OutputBuffer &OB, OutputFlags Flags) const override
void outputPost(OutputBuffer &OB, OutputFlags Flags) const override
void output(OutputBuffer &OB, OutputFlags Flags) const override
void outputTemplateParameters(OutputBuffer &OB, OutputFlags Flags) const
void output(OutputBuffer &OB, OutputFlags Flags) const override
void output(OutputBuffer &OB, OutputFlags Flags) const override
void output(OutputBuffer &OB, OutputFlags Flags) const override
void output(OutputBuffer &OB, OutputFlags Flags) const override
void output(OutputBuffer &OB, OutputFlags Flags) const override
void output(OutputBuffer &OB, OutputFlags Flags) const override
void output(OutputBuffer &OB, OutputFlags Flags) const override
virtual void output(OutputBuffer &OB, OutputFlags Flags) const =0
void outputPost(OutputBuffer &OB, OutputFlags Flags) const override
void outputPre(OutputBuffer &OB, OutputFlags Flags) const override
void outputPre(OutputBuffer &OB, OutputFlags Flags) const override
void output(OutputBuffer &OB, OutputFlags Flags) const override
void output(OutputBuffer &OB, OutputFlags Flags) const override
void output(OutputBuffer &OB, OutputFlags Flags) const override
void output(OutputBuffer &OB, OutputFlags Flags) const override
void output(OutputBuffer &OB, OutputFlags Flags) const override
void outputPost(OutputBuffer &OB, OutputFlags Flags) const override
void outputPre(OutputBuffer &OB, OutputFlags Flags) const override
void output(OutputBuffer &OB, OutputFlags Flags) const override
void outputPost(OutputBuffer &OB, OutputFlags Flags) const override
void outputPre(OutputBuffer &OB, OutputFlags Flags) const override
void output(OutputBuffer &OB, OutputFlags Flags) const override
virtual void outputPre(OutputBuffer &OB, OutputFlags Flags) const =0
virtual void outputPost(OutputBuffer &OB, OutputFlags Flags) const =0
void output(OutputBuffer &OB, OutputFlags Flags) const override
void output(OutputBuffer &OB, OutputFlags Flags) const override