aboutsummaryrefslogtreecommitdiff
path: root/lib/Demangle/MicrosoftDemangleNodes.cpp
blob: 63ca475ec1fed6fe23182d61e955c48b92d73d85 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//===- MicrosoftDemangle.cpp ----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines a demangler for MSVC-style mangled symbols.
//
//===----------------------------------------------------------------------===//

#include "llvm/Demangle/MicrosoftDemangleNodes.h"
#include "llvm/Demangle/DemangleConfig.h"
#include "llvm/Demangle/Utility.h"
#include <cctype>
#include <string>

using namespace llvm;
using namespace ms_demangle;

#define OUTPUT_ENUM_CLASS_VALUE(Enum, Value, Desc)                             \
  case Enum::Value:                                                            \
    OS << Desc;                                                                \
    break;

// Writes a space if the last token does not end with a punctuation.
static void outputSpaceIfNecessary(OutputStream &OS) {
  if (OS.empty())
    return;

  char C = OS.back();
  if (std::isalnum(C) || C == '>')
    OS << " ";
}

static void outputSingleQualifier(OutputStream &OS, Qualifiers Q) {
  switch (Q) {
  case Q_Const:
    OS << "const";
    break;
  case Q_Volatile:
    OS << "volatile";
    break;
  case Q_Restrict:
    OS << "__restrict";
    break;
  default:
    break;
  }
}

static bool outputQualifierIfPresent(OutputStream &OS, Qualifiers Q,
                                     Qualifiers Mask, bool NeedSpace) {
  if (!(Q & Mask))
    return NeedSpace;

  if (NeedSpace)
    OS << " ";

  outputSingleQualifier(OS, Mask);
  return true;
}

static void outputQualifiers(OutputStream &OS, Qualifiers Q, bool SpaceBefore,
                             bool SpaceAfter) {
  if (Q == Q_None)
    return;

  size_t Pos1 = OS.getCurrentPosition();
  SpaceBefore = outputQualifierIfPresent(OS, Q, Q_Const, SpaceBefore);
  SpaceBefore = outputQualifierIfPresent(OS, Q, Q_Volatile, SpaceBefore);
  SpaceBefore = outputQualifierIfPresent(OS, Q, Q_Restrict, SpaceBefore);
  size_t Pos2 = OS.getCurrentPosition();
  if (SpaceAfter && Pos2 > Pos1)
    OS << " ";
}

static void outputCallingConvention(OutputStream &OS, CallingConv CC) {
  outputSpaceIfNecessary(OS);

  switch (CC) {
  case CallingConv::Cdecl:
    OS << "__cdecl";
    break;
  case CallingConv::Fastcall:
    OS << "__fastcall";
    break;
  case CallingConv::Pascal:
    OS << "__pascal";
    break;
  case CallingConv::Regcall:
    OS << "__regcall";
    break;
  case CallingConv::Stdcall:
    OS << "__stdcall";
    break;
  case CallingConv::Thiscall:
    OS << "__thiscall";
    break;
  case CallingConv::Eabi:
    OS << "__eabi";
    break;
  case CallingConv::Vectorcall:
    OS << "__vectorcall";
    break;
  case CallingConv::Clrcall:
    OS << "__clrcall";
    break;
  default:
    break;
  }
}

std::string Node::toString(OutputFlags Flags) const {
  OutputStream OS;
  initializeOutputStream(nullptr, nullptr, OS, 1024);
  this->output(OS, Flags);
  OS << '\0';
  return {OS.getBuffer()};
}

void TypeNode::outputQuals(bool SpaceBefore, bool SpaceAfter) const {}

void PrimitiveTypeNode::outputPre(OutputStream &OS, OutputFlags Flags) const {
  switch (PrimKind) {
    OUTPUT_ENUM_CLASS_VALUE(PrimitiveKind, Void, "void");
    OUTPUT_ENUM_CLASS_VALUE(PrimitiveKind, Bool, "bool");
    OUTPUT_ENUM_CLASS_VALUE(PrimitiveKind, Char, "char");
    OUTPUT_ENUM_CLASS_VALUE(PrimitiveKind, Schar, "signed char");
    OUTPUT_ENUM_CLASS_VALUE(PrimitiveKind, Uchar, "unsigned char");
    OUTPUT_ENUM_CLASS_VALUE(PrimitiveKind, Char8, "char8_t");
    OUTPUT_ENUM_CLASS_VALUE(PrimitiveKind, Char16, "char16_t");
    OUTPUT_ENUM_CLASS_VALUE(PrimitiveKind, Char32, "char32_t");
    OUTPUT_ENUM_CLASS_VALUE(PrimitiveKind, Short, "short");
    OUTPUT_ENUM_CLASS_VALUE(PrimitiveKind, Ushort, "unsigned short");
    OUTPUT_ENUM_CLASS_VALUE(PrimitiveKind, Int, "int");
    OUTPUT_ENUM_CLASS_VALUE(PrimitiveKind, Uint, "unsigned int");
    OUTPUT_ENUM_CLASS_VALUE(PrimitiveKind, Long, "long");
    OUTPUT_ENUM_CLASS_VALUE(PrimitiveKind, Ulong, "unsigned long");
    OUTPUT_ENUM_CLASS_VALUE(PrimitiveKind, Int64, "__int64");
    OUTPUT_ENUM_CLASS_VALUE(PrimitiveKind, Uint64, "unsigned __int64");
    OUTPUT_ENUM_CLASS_VALUE(PrimitiveKind, Wchar, "wchar_t");
    OUTPUT_ENUM_CLASS_VALUE(PrimitiveKind, Float, "float");
    OUTPUT_ENUM_CLASS_VALUE(PrimitiveKind, Double, "double");
    OUTPUT_ENUM_CLASS_VALUE(PrimitiveKind, Ldouble, "long double");
    OUTPUT_ENUM_CLASS_VALUE(PrimitiveKind, Nullptr, "std::nullptr_t");
  }
  outputQualifiers(OS, Quals, true, false);
}

void NodeArrayNode::output(OutputStream &OS, OutputFlags Flags) const {
  output(OS, Flags, ", ");
}

void NodeArrayNode::output(OutputStream &OS, OutputFlags Flags,
                           StringView Separator) const {
  if (Count == 0)
    return;
  if (Nodes[0])
    Nodes[0]->output(OS, Flags);
  for (size_t I = 1; I < Count; ++I) {
    OS << Separator;
    Nodes[I]->output(OS, Flags);
  }
}

void EncodedStringLiteralNode::output(OutputStream &OS,
                                      OutputFlags Flags) const {
  switch (Char) {
  case CharKind::Wchar:
    OS << "L\"";
    break;
  case CharKind::Char:
    OS << "\"";
    break;
  case CharKind::Char16:
    OS << "u\"";
    break;
  case CharKind::Char32:
    OS << "U\"";
    break;
  }
  OS << DecodedString << "\"";
  if (IsTruncated)
    OS << "...";
}

void IntegerLiteralNode::output(OutputStream &OS, OutputFlags Flags) const {
  if (IsNegative)
    OS << '-';
  OS << Value;
}

void TemplateParameterReferenceNode::output(OutputStream &OS,
                                            OutputFlags Flags) const {
  if (ThunkOffsetCount > 0)
    OS << "{";
  else if (Affinity == PointerAffinity::Pointer)
    OS << "&";

  if (Symbol) {
    Symbol->output(OS, Flags);
    if (ThunkOffsetCount > 0)
      OS << ", ";
  }

  if (ThunkOffsetCount > 0)
    OS << ThunkOffsets[0];
  for (int I = 1; I < ThunkOffsetCount; ++I) {
    OS << ", " << ThunkOffsets[I];
  }
  if (ThunkOffsetCount > 0)
    OS << "}";
}

void IdentifierNode::outputTemplateParameters(OutputStream &OS,
                                              OutputFlags Flags) const {
  if (!TemplateParams)
    return;
  OS << "<";
  TemplateParams->output(OS, Flags);
  OS << ">";
}

void DynamicStructorIdentifierNode::output(OutputStream &OS,
                                           OutputFlags Flags) const {
  if (IsDestructor)
    OS << "`dynamic atexit destructor for ";
  else
    OS << "`dynamic initializer for ";

  if (Variable) {
    OS << "`";
    Variable->output(OS, Flags);
    OS << "''";
  } else {
    OS << "'";
    Name->output(OS, Flags);
    OS << "''";
  }
}

void NamedIdentifierNode::output(OutputStream &OS, OutputFlags Flags) const {
  OS << Name;
  outputTemplateParameters(OS, Flags);
}

void IntrinsicFunctionIdentifierNode::output(OutputStream &OS,
                                             OutputFlags Flags) const {
  switch (Operator) {
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, New, "operator new");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, Delete, "operator delete");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, Assign, "operator=");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, RightShift, "operator>>");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, LeftShift, "operator<<");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, LogicalNot, "operator!");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, Equals, "operator==");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, NotEquals, "operator!=");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, ArraySubscript,
                            "operator[]");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, Pointer, "operator->");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, Increment, "operator++");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, Decrement, "operator--");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, Minus, "operator-");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, Plus, "operator+");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, Dereference, "operator*");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, BitwiseAnd, "operator&");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, MemberPointer,
                            "operator->*");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, Divide, "operator/");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, Modulus, "operator%");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, LessThan, "operator<");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, LessThanEqual, "operator<=");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, GreaterThan, "operator>");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, GreaterThanEqual,
                            "operator>=");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, Comma, "operator,");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, Parens, "operator()");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, BitwiseNot, "operator~");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, BitwiseXor, "operator^");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, BitwiseOr, "operator|");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, LogicalAnd, "operator&&");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, LogicalOr, "operator||");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, TimesEqual, "operator*=");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, PlusEqual, "operator+=");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, MinusEqual, "operator-=");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, DivEqual, "operator/=");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, ModEqual, "operator%=");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, RshEqual, "operator>>=");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, LshEqual, "operator<<=");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, BitwiseAndEqual,
                            "operator&=");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, BitwiseOrEqual,
                            "operator|=");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, BitwiseXorEqual,
                            "operator^=");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, VbaseDtor, "`vbase dtor'");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, VecDelDtor,
                            "`vector deleting dtor'");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, DefaultCtorClosure,
                            "`default ctor closure'");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, ScalarDelDtor,
                            "`scalar deleting dtor'");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, VecCtorIter,
                            "`vector ctor iterator'");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, VecDtorIter,
                            "`vector dtor iterator'");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, VecVbaseCtorIter,
                            "`vector vbase ctor iterator'");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, VdispMap,
                            "`virtual displacement map'");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, EHVecCtorIter,
                            "`eh vector ctor iterator'");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, EHVecDtorIter,
                            "`eh vector dtor iterator'");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, EHVecVbaseCtorIter,
                            "`eh vector vbase ctor iterator'");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, CopyCtorClosure,
                            "`copy ctor closure'");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, LocalVftableCtorClosure,
                            "`local vftable ctor closure'");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, ArrayNew, "operator new[]");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, ArrayDelete,
                            "operator delete[]");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, ManVectorCtorIter,
                            "`managed vector ctor iterator'");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, ManVectorDtorIter,
                            "`managed vector dtor iterator'");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, EHVectorCopyCtorIter,
                            "`EH vector copy ctor iterator'");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, EHVectorVbaseCopyCtorIter,
                            "`EH vector vbase copy ctor iterator'");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, VectorCopyCtorIter,
                            "`vector copy ctor iterator'");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, VectorVbaseCopyCtorIter,
                            "`vector vbase copy constructor iterator'");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, ManVectorVbaseCopyCtorIter,
                            "`managed vector vbase copy constructor iterator'");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, CoAwait,
                            "operator co_await");
    OUTPUT_ENUM_CLASS_VALUE(IntrinsicFunctionKind, Spaceship, "operator<=>");
  case IntrinsicFunctionKind::MaxIntrinsic:
  case IntrinsicFunctionKind::None:
    break;
  }
  outputTemplateParameters(OS, Flags);
}

void LocalStaticGuardIdentifierNode::output(OutputStream &OS,
                                            OutputFlags Flags) const {
  if (IsThread)
    OS << "`local static thread guard'";
  else
    OS << "`local static guard'";
  if (ScopeIndex > 0)
    OS << "{" << ScopeIndex << "}";
}

void ConversionOperatorIdentifierNode::output(OutputStream &OS,
                                              OutputFlags Flags) const {
  OS << "operator";
  outputTemplateParameters(OS, Flags);
  OS << " ";
  TargetType->output(OS, Flags);
}

void StructorIdentifierNode::output(OutputStream &OS, OutputFlags Flags) const {
  if (IsDestructor)
    OS << "~";
  Class->output(OS, Flags);
  outputTemplateParameters(OS, Flags);
}

void LiteralOperatorIdentifierNode::output(OutputStream &OS,
                                           OutputFlags Flags) const {
  OS << "operator \"\"" << Name;
  outputTemplateParameters(OS, Flags);
}

void FunctionSignatureNode::outputPre(OutputStream &OS,
                                      OutputFlags Flags) const {
  if (FunctionClass & FC_Public)
    OS << "public: ";
  if (FunctionClass & FC_Protected)
    OS << "protected: ";
  if (FunctionClass & FC_Private)
    OS << "private: ";

  if (!(FunctionClass & FC_Global)) {
    if (FunctionClass & FC_Static)
      OS << "static ";
  }
  if (FunctionClass & FC_Virtual)
    OS << "virtual ";

  if (FunctionClass & FC_ExternC)
    OS << "extern \"C\" ";

  if (ReturnType) {
    ReturnType->outputPre(OS, Flags);
    OS << " ";
  }

  if (!(Flags & OF_NoCallingConvention))
    outputCallingConvention(OS, CallConvention);
}

void FunctionSignatureNode::outputPost(OutputStream &OS,
                                       OutputFlags Flags) const {
  if (!(FunctionClass & FC_NoParameterList)) {
    OS << "(";
    if (Params)
      Params->output(OS, Flags);
    else
      OS << "void";

    if (IsVariadic) {
      if (OS.back() != '(')
        OS << ", ";
      OS << "...";
    }
    OS << ")";
  }

  if (Quals & Q_Const)
    OS << " const";
  if (Quals & Q_Volatile)
    OS << " volatile";
  if (Quals & Q_Restrict)
    OS << " __restrict";
  if (Quals & Q_Unaligned)
    OS << " __unaligned";

  if (IsNoexcept)
    OS << " noexcept";

  if (RefQualifier == FunctionRefQualifier::Reference)
    OS << " &";
  else if (RefQualifier == FunctionRefQualifier::RValueReference)
    OS << " &&";

  if (ReturnType)
    ReturnType->outputPost(OS, Flags);
}

void ThunkSignatureNode::outputPre(OutputStream &OS, OutputFlags Flags) const {
  OS << "[thunk]: ";

  FunctionSignatureNode::outputPre(OS, Flags);
}

void ThunkSignatureNode::outputPost(OutputStream &OS, OutputFlags Flags) const {
  if (FunctionClass & FC_StaticThisAdjust) {
    OS << "`adjustor{" << ThisAdjust.StaticOffset << "}'";
  } else if (FunctionClass & FC_VirtualThisAdjust) {
    if (FunctionClass & FC_VirtualThisAdjustEx) {
      OS << "`vtordispex{" << ThisAdjust.VBPtrOffset << ", "
         << ThisAdjust.VBOffsetOffset << ", " << ThisAdjust.VtordispOffset
         << ", " << ThisAdjust.StaticOffset << "}'";
    } else {
      OS << "`vtordisp{" << ThisAdjust.VtordispOffset << ", "
         << ThisAdjust.StaticOffset << "}'";
    }
  }

  FunctionSignatureNode::outputPost(OS, Flags);
}

void PointerTypeNode::outputPre(OutputStream &OS, OutputFlags Flags) const {
  if (Pointee->kind() == NodeKind::FunctionSignature) {
    // If this is a pointer to a function, don't output the calling convention.
    // It needs to go inside the parentheses.
    const FunctionSignatureNode *Sig =
        static_cast<const FunctionSignatureNode *>(Pointee);
    Sig->outputPre(OS, OF_NoCallingConvention);
  } else
    Pointee->outputPre(OS, Flags);

  outputSpaceIfNecessary(OS);

  if (Quals & Q_Unaligned)
    OS << "__unaligned ";

  if (Pointee->kind() == NodeKind::ArrayType) {
    OS << "(";
  } else if (Pointee->kind() == NodeKind::FunctionSignature) {
    OS << "(";
    const FunctionSignatureNode *Sig =
        static_cast<const FunctionSignatureNode *>(Pointee);
    outputCallingConvention(OS, Sig->CallConvention);
    OS << " ";
  }

  if (ClassParent) {
    ClassParent->output(OS, Flags);
    OS << "::";
  }

  switch (Affinity) {
  case PointerAffinity::Pointer:
    OS << "*";
    break;
  case PointerAffinity::Reference:
    OS << "&";
    break;
  case PointerAffinity::RValueReference:
    OS << "&&";
    break;
  default:
    assert(false);
  }
  outputQualifiers(OS, Quals, false, false);
}

void PointerTypeNode::outputPost(OutputStream &OS, OutputFlags Flags) const {
  if (Pointee->kind() == NodeKind::ArrayType ||
      Pointee->kind() == NodeKind::FunctionSignature)
    OS << ")";

  Pointee->outputPost(OS, Flags);
}

void TagTypeNode::outputPre(OutputStream &OS, OutputFlags Flags) const {
  if (!(Flags & OF_NoTagSpecifier)) {
    switch (Tag) {
      OUTPUT_ENUM_CLASS_VALUE(TagKind, Class, "class");
      OUTPUT_ENUM_CLASS_VALUE(TagKind, Struct, "struct");
      OUTPUT_ENUM_CLASS_VALUE(TagKind, Union, "union");
      OUTPUT_ENUM_CLASS_VALUE(TagKind, Enum, "enum");
    }
    OS << " ";
  }
  QualifiedName->output(OS, Flags);
  outputQualifiers(OS, Quals, true, false);
}

void TagTypeNode::outputPost(OutputStream &OS, OutputFlags Flags) const {}

void ArrayTypeNode::outputPre(OutputStream &OS, OutputFlags Flags) const {
  ElementType->outputPre(OS, Flags);
  outputQualifiers(OS, Quals, true, false);
}

void ArrayTypeNode::outputOneDimension(OutputStream &OS, OutputFlags Flags,
                                       Node *N) const {
  assert(N->kind() == NodeKind::IntegerLiteral);
  IntegerLiteralNode *ILN = static_cast<IntegerLiteralNode *>(N);
  if (ILN->Value != 0)
    ILN->output(OS, Flags);
}

void ArrayTypeNode::outputDimensionsImpl(OutputStream &OS,
                                         OutputFlags Flags) const {
  if (Dimensions->Count == 0)
    return;

  outputOneDimension(OS, Flags, Dimensions->Nodes[0]);
  for (size_t I = 1; I < Dimensions->Count; ++I) {
    OS << "][";
    outputOneDimension(OS, Flags, Dimensions->Nodes[I]);
  }
}

void ArrayTypeNode::outputPost(OutputStream &OS, OutputFlags Flags) const {
  OS << "[";
  outputDimensionsImpl(OS, Flags);
  OS << "]";

  ElementType->outputPost(OS, Flags);
}

void SymbolNode::output(OutputStream &OS, OutputFlags Flags) const {
  Name->output(OS, Flags);
}

void FunctionSymbolNode::output(OutputStream &OS, OutputFlags Flags) const {
  Signature->outputPre(OS, Flags);
  outputSpaceIfNecessary(OS);
  Name->output(OS, Flags);
  Signature->outputPost(OS, Flags);
}

void VariableSymbolNode::output(OutputStream &OS, OutputFlags Flags) const {
  switch (SC) {
  case StorageClass::PrivateStatic:
    OS << "private: static ";
    break;
  case StorageClass::PublicStatic:
    OS << "public: static ";
    break;
  case StorageClass::ProtectedStatic:
    OS << "protected: static ";
    break;
  default:
    break;
  }

  if (Type) {
    Type->outputPre(OS, Flags);
    outputSpaceIfNecessary(OS);
  }
  Name->output(OS, Flags);
  if (Type)
    Type->outputPost(OS, Flags);
}

void CustomTypeNode::outputPre(OutputStream &OS, OutputFlags Flags) const {
  Identifier->output(OS, Flags);
}
void CustomTypeNode::outputPost(OutputStream &OS, OutputFlags Flags) const {}

void QualifiedNameNode::output(OutputStream &OS, OutputFlags Flags) const {
  Components->output(OS, Flags, "::");
}

void RttiBaseClassDescriptorNode::output(OutputStream &OS,
                                         OutputFlags Flags) const {
  OS << "`RTTI Base Class Descriptor at (";
  OS << NVOffset << ", " << VBPtrOffset << ", " << VBTableOffset << ", "
     << this->Flags;
  OS << ")'";
}

void LocalStaticGuardVariableNode::output(OutputStream &OS,
                                          OutputFlags Flags) const {
  Name->output(OS, Flags);
}

void VcallThunkIdentifierNode::output(OutputStream &OS,
                                      OutputFlags Flags) const {
  OS << "`vcall'{" << OffsetInVTable << ", {flat}}";
}

void SpecialTableSymbolNode::output(OutputStream &OS, OutputFlags Flags) const {
  outputQualifiers(OS, Quals, false, true);
  Name->output(OS, Flags);
  if (TargetName) {
    OS << "{for `";
    TargetName->output(OS, Flags);
    OS << "'}";
  }
  return;
}