xref: /freebsd/contrib/llvm-project/clang/lib/AST/TextNodeDumper.cpp (revision f7c32ed617858bcd22f8d1b03199099d50125721)
1 //===--- TextNodeDumper.cpp - Printing of AST nodes -----------------------===//
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 implements AST dumping of components of individual AST nodes.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "clang/AST/TextNodeDumper.h"
14 #include "clang/AST/APValue.h"
15 #include "clang/AST/DeclFriend.h"
16 #include "clang/AST/DeclOpenMP.h"
17 #include "clang/AST/DeclTemplate.h"
18 #include "clang/AST/LocInfoType.h"
19 #include "clang/AST/Type.h"
20 #include "clang/Basic/Module.h"
21 #include "clang/Basic/SourceManager.h"
22 #include "clang/Basic/Specifiers.h"
23 #include "clang/Basic/TypeTraits.h"
24 #include "llvm/ADT/StringExtras.h"
25 
26 #include <algorithm>
27 #include <utility>
28 
29 using namespace clang;
30 
31 static void dumpPreviousDeclImpl(raw_ostream &OS, ...) {}
32 
33 template <typename T>
34 static void dumpPreviousDeclImpl(raw_ostream &OS, const Mergeable<T> *D) {
35   const T *First = D->getFirstDecl();
36   if (First != D)
37     OS << " first " << First;
38 }
39 
40 template <typename T>
41 static void dumpPreviousDeclImpl(raw_ostream &OS, const Redeclarable<T> *D) {
42   const T *Prev = D->getPreviousDecl();
43   if (Prev)
44     OS << " prev " << Prev;
45 }
46 
47 /// Dump the previous declaration in the redeclaration chain for a declaration,
48 /// if any.
49 static void dumpPreviousDecl(raw_ostream &OS, const Decl *D) {
50   switch (D->getKind()) {
51 #define DECL(DERIVED, BASE)                                                    \
52   case Decl::DERIVED:                                                          \
53     return dumpPreviousDeclImpl(OS, cast<DERIVED##Decl>(D));
54 #define ABSTRACT_DECL(DECL)
55 #include "clang/AST/DeclNodes.inc"
56   }
57   llvm_unreachable("Decl that isn't part of DeclNodes.inc!");
58 }
59 
60 TextNodeDumper::TextNodeDumper(raw_ostream &OS, const ASTContext &Context,
61                                bool ShowColors)
62     : TextTreeStructure(OS, ShowColors), OS(OS), ShowColors(ShowColors),
63       Context(&Context), SM(&Context.getSourceManager()),
64       PrintPolicy(Context.getPrintingPolicy()),
65       Traits(&Context.getCommentCommandTraits()) {}
66 
67 TextNodeDumper::TextNodeDumper(raw_ostream &OS, bool ShowColors)
68     : TextTreeStructure(OS, ShowColors), OS(OS), ShowColors(ShowColors) {}
69 
70 void TextNodeDumper::Visit(const comments::Comment *C,
71                            const comments::FullComment *FC) {
72   if (!C) {
73     ColorScope Color(OS, ShowColors, NullColor);
74     OS << "<<<NULL>>>";
75     return;
76   }
77 
78   {
79     ColorScope Color(OS, ShowColors, CommentColor);
80     OS << C->getCommentKindName();
81   }
82   dumpPointer(C);
83   dumpSourceRange(C->getSourceRange());
84 
85   ConstCommentVisitor<TextNodeDumper, void,
86                       const comments::FullComment *>::visit(C, FC);
87 }
88 
89 void TextNodeDumper::Visit(const Attr *A) {
90   {
91     ColorScope Color(OS, ShowColors, AttrColor);
92 
93     switch (A->getKind()) {
94 #define ATTR(X)                                                                \
95   case attr::X:                                                                \
96     OS << #X;                                                                  \
97     break;
98 #include "clang/Basic/AttrList.inc"
99     }
100     OS << "Attr";
101   }
102   dumpPointer(A);
103   dumpSourceRange(A->getRange());
104   if (A->isInherited())
105     OS << " Inherited";
106   if (A->isImplicit())
107     OS << " Implicit";
108 
109   ConstAttrVisitor<TextNodeDumper>::Visit(A);
110 }
111 
112 void TextNodeDumper::Visit(const TemplateArgument &TA, SourceRange R,
113                            const Decl *From, StringRef Label) {
114   OS << "TemplateArgument";
115   if (R.isValid())
116     dumpSourceRange(R);
117 
118   if (From)
119     dumpDeclRef(From, Label);
120 
121   ConstTemplateArgumentVisitor<TextNodeDumper>::Visit(TA);
122 }
123 
124 void TextNodeDumper::Visit(const Stmt *Node) {
125   if (!Node) {
126     ColorScope Color(OS, ShowColors, NullColor);
127     OS << "<<<NULL>>>";
128     return;
129   }
130   {
131     ColorScope Color(OS, ShowColors, StmtColor);
132     OS << Node->getStmtClassName();
133   }
134   dumpPointer(Node);
135   dumpSourceRange(Node->getSourceRange());
136 
137   if (const auto *E = dyn_cast<Expr>(Node)) {
138     dumpType(E->getType());
139 
140     if (E->containsErrors()) {
141       ColorScope Color(OS, ShowColors, ErrorsColor);
142       OS << " contains-errors";
143     }
144 
145     {
146       ColorScope Color(OS, ShowColors, ValueKindColor);
147       switch (E->getValueKind()) {
148       case VK_PRValue:
149         break;
150       case VK_LValue:
151         OS << " lvalue";
152         break;
153       case VK_XValue:
154         OS << " xvalue";
155         break;
156       }
157     }
158 
159     {
160       ColorScope Color(OS, ShowColors, ObjectKindColor);
161       switch (E->getObjectKind()) {
162       case OK_Ordinary:
163         break;
164       case OK_BitField:
165         OS << " bitfield";
166         break;
167       case OK_ObjCProperty:
168         OS << " objcproperty";
169         break;
170       case OK_ObjCSubscript:
171         OS << " objcsubscript";
172         break;
173       case OK_VectorComponent:
174         OS << " vectorcomponent";
175         break;
176       case OK_MatrixComponent:
177         OS << " matrixcomponent";
178         break;
179       }
180     }
181   }
182 
183   ConstStmtVisitor<TextNodeDumper>::Visit(Node);
184 }
185 
186 void TextNodeDumper::Visit(const Type *T) {
187   if (!T) {
188     ColorScope Color(OS, ShowColors, NullColor);
189     OS << "<<<NULL>>>";
190     return;
191   }
192   if (isa<LocInfoType>(T)) {
193     {
194       ColorScope Color(OS, ShowColors, TypeColor);
195       OS << "LocInfo Type";
196     }
197     dumpPointer(T);
198     return;
199   }
200 
201   {
202     ColorScope Color(OS, ShowColors, TypeColor);
203     OS << T->getTypeClassName() << "Type";
204   }
205   dumpPointer(T);
206   OS << " ";
207   dumpBareType(QualType(T, 0), false);
208 
209   QualType SingleStepDesugar =
210       T->getLocallyUnqualifiedSingleStepDesugaredType();
211   if (SingleStepDesugar != QualType(T, 0))
212     OS << " sugar";
213 
214   if (T->containsErrors()) {
215     ColorScope Color(OS, ShowColors, ErrorsColor);
216     OS << " contains-errors";
217   }
218 
219   if (T->isDependentType())
220     OS << " dependent";
221   else if (T->isInstantiationDependentType())
222     OS << " instantiation_dependent";
223 
224   if (T->isVariablyModifiedType())
225     OS << " variably_modified";
226   if (T->containsUnexpandedParameterPack())
227     OS << " contains_unexpanded_pack";
228   if (T->isFromAST())
229     OS << " imported";
230 
231   TypeVisitor<TextNodeDumper>::Visit(T);
232 }
233 
234 void TextNodeDumper::Visit(QualType T) {
235   OS << "QualType";
236   dumpPointer(T.getAsOpaquePtr());
237   OS << " ";
238   dumpBareType(T, false);
239   OS << " " << T.split().Quals.getAsString();
240 }
241 
242 void TextNodeDumper::Visit(const Decl *D) {
243   if (!D) {
244     ColorScope Color(OS, ShowColors, NullColor);
245     OS << "<<<NULL>>>";
246     return;
247   }
248 
249   {
250     ColorScope Color(OS, ShowColors, DeclKindNameColor);
251     OS << D->getDeclKindName() << "Decl";
252   }
253   dumpPointer(D);
254   if (D->getLexicalDeclContext() != D->getDeclContext())
255     OS << " parent " << cast<Decl>(D->getDeclContext());
256   dumpPreviousDecl(OS, D);
257   dumpSourceRange(D->getSourceRange());
258   OS << ' ';
259   dumpLocation(D->getLocation());
260   if (D->isFromASTFile())
261     OS << " imported";
262   if (Module *M = D->getOwningModule())
263     OS << " in " << M->getFullModuleName();
264   if (auto *ND = dyn_cast<NamedDecl>(D))
265     for (Module *M : D->getASTContext().getModulesWithMergedDefinition(
266              const_cast<NamedDecl *>(ND)))
267       AddChild([=] { OS << "also in " << M->getFullModuleName(); });
268   if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
269     if (!ND->isUnconditionallyVisible())
270       OS << " hidden";
271   if (D->isImplicit())
272     OS << " implicit";
273 
274   if (D->isUsed())
275     OS << " used";
276   else if (D->isThisDeclarationReferenced())
277     OS << " referenced";
278 
279   if (D->isInvalidDecl())
280     OS << " invalid";
281   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
282     if (FD->isConstexprSpecified())
283       OS << " constexpr";
284     if (FD->isConsteval())
285       OS << " consteval";
286   }
287 
288   if (!isa<FunctionDecl>(*D)) {
289     const auto *MD = dyn_cast<ObjCMethodDecl>(D);
290     if (!MD || !MD->isThisDeclarationADefinition()) {
291       const auto *DC = dyn_cast<DeclContext>(D);
292       if (DC && DC->hasExternalLexicalStorage()) {
293         ColorScope Color(OS, ShowColors, UndeserializedColor);
294         OS << " <undeserialized declarations>";
295       }
296     }
297   }
298 
299   ConstDeclVisitor<TextNodeDumper>::Visit(D);
300 }
301 
302 void TextNodeDumper::Visit(const CXXCtorInitializer *Init) {
303   OS << "CXXCtorInitializer";
304   if (Init->isAnyMemberInitializer()) {
305     OS << ' ';
306     dumpBareDeclRef(Init->getAnyMember());
307   } else if (Init->isBaseInitializer()) {
308     dumpType(QualType(Init->getBaseClass(), 0));
309   } else if (Init->isDelegatingInitializer()) {
310     dumpType(Init->getTypeSourceInfo()->getType());
311   } else {
312     llvm_unreachable("Unknown initializer type");
313   }
314 }
315 
316 void TextNodeDumper::Visit(const BlockDecl::Capture &C) {
317   OS << "capture";
318   if (C.isByRef())
319     OS << " byref";
320   if (C.isNested())
321     OS << " nested";
322   if (C.getVariable()) {
323     OS << ' ';
324     dumpBareDeclRef(C.getVariable());
325   }
326 }
327 
328 void TextNodeDumper::Visit(const OMPClause *C) {
329   if (!C) {
330     ColorScope Color(OS, ShowColors, NullColor);
331     OS << "<<<NULL>>> OMPClause";
332     return;
333   }
334   {
335     ColorScope Color(OS, ShowColors, AttrColor);
336     StringRef ClauseName(llvm::omp::getOpenMPClauseName(C->getClauseKind()));
337     OS << "OMP" << ClauseName.substr(/*Start=*/0, /*N=*/1).upper()
338        << ClauseName.drop_front() << "Clause";
339   }
340   dumpPointer(C);
341   dumpSourceRange(SourceRange(C->getBeginLoc(), C->getEndLoc()));
342   if (C->isImplicit())
343     OS << " <implicit>";
344 }
345 
346 void TextNodeDumper::Visit(const GenericSelectionExpr::ConstAssociation &A) {
347   const TypeSourceInfo *TSI = A.getTypeSourceInfo();
348   if (TSI) {
349     OS << "case ";
350     dumpType(TSI->getType());
351   } else {
352     OS << "default";
353   }
354 
355   if (A.isSelected())
356     OS << " selected";
357 }
358 
359 void TextNodeDumper::Visit(const concepts::Requirement *R) {
360   if (!R) {
361     ColorScope Color(OS, ShowColors, NullColor);
362     OS << "<<<NULL>>> Requirement";
363     return;
364   }
365 
366   {
367     ColorScope Color(OS, ShowColors, StmtColor);
368     switch (R->getKind()) {
369     case concepts::Requirement::RK_Type:
370       OS << "TypeRequirement";
371       break;
372     case concepts::Requirement::RK_Simple:
373       OS << "SimpleRequirement";
374       break;
375     case concepts::Requirement::RK_Compound:
376       OS << "CompoundRequirement";
377       break;
378     case concepts::Requirement::RK_Nested:
379       OS << "NestedRequirement";
380       break;
381     }
382   }
383 
384   dumpPointer(R);
385 
386   if (auto *ER = dyn_cast<concepts::ExprRequirement>(R)) {
387     if (ER->hasNoexceptRequirement())
388       OS << " noexcept";
389   }
390 
391   if (R->isDependent())
392     OS << " dependent";
393   else
394     OS << (R->isSatisfied() ? " satisfied" : " unsatisfied");
395   if (R->containsUnexpandedParameterPack())
396     OS << " contains_unexpanded_pack";
397 }
398 
399 static double GetApproxValue(const llvm::APFloat &F) {
400   llvm::APFloat V = F;
401   bool ignored;
402   V.convert(llvm::APFloat::IEEEdouble(), llvm::APFloat::rmNearestTiesToEven,
403             &ignored);
404   return V.convertToDouble();
405 }
406 
407 /// True if the \p APValue \p Value can be folded onto the current line.
408 static bool isSimpleAPValue(const APValue &Value) {
409   switch (Value.getKind()) {
410   case APValue::None:
411   case APValue::Indeterminate:
412   case APValue::Int:
413   case APValue::Float:
414   case APValue::FixedPoint:
415   case APValue::ComplexInt:
416   case APValue::ComplexFloat:
417   case APValue::LValue:
418   case APValue::MemberPointer:
419   case APValue::AddrLabelDiff:
420     return true;
421   case APValue::Vector:
422   case APValue::Array:
423   case APValue::Struct:
424     return false;
425   case APValue::Union:
426     return isSimpleAPValue(Value.getUnionValue());
427   }
428   llvm_unreachable("unexpected APValue kind!");
429 }
430 
431 /// Dump the children of the \p APValue \p Value.
432 ///
433 /// \param[in] Value          The \p APValue to visit
434 /// \param[in] Ty             The \p QualType passed to \p Visit
435 ///
436 /// \param[in] IdxToChildFun  A function mapping an \p APValue and an index
437 ///                           to one of the child of the \p APValue
438 ///
439 /// \param[in] NumChildren    \p IdxToChildFun will be called on \p Value with
440 ///                           the indices in the range \p [0,NumChildren(
441 ///
442 /// \param[in] LabelSingular  The label to use on a line with a single child
443 /// \param[in] LabelPlurial   The label to use on a line with multiple children
444 void TextNodeDumper::dumpAPValueChildren(
445     const APValue &Value, QualType Ty,
446     const APValue &(*IdxToChildFun)(const APValue &, unsigned),
447     unsigned NumChildren, StringRef LabelSingular, StringRef LabelPlurial) {
448   // To save some vertical space we print up to MaxChildrenPerLine APValues
449   // considered to be simple (by isSimpleAPValue) on a single line.
450   constexpr unsigned MaxChildrenPerLine = 4;
451   unsigned I = 0;
452   while (I < NumChildren) {
453     unsigned J = I;
454     while (J < NumChildren) {
455       if (isSimpleAPValue(IdxToChildFun(Value, J)) &&
456           (J - I < MaxChildrenPerLine)) {
457         ++J;
458         continue;
459       }
460       break;
461     }
462 
463     J = std::max(I + 1, J);
464 
465     // Print [I,J) on a single line.
466     AddChild(J - I > 1 ? LabelPlurial : LabelSingular, [=]() {
467       for (unsigned X = I; X < J; ++X) {
468         Visit(IdxToChildFun(Value, X), Ty);
469         if (X + 1 != J)
470           OS << ", ";
471       }
472     });
473     I = J;
474   }
475 }
476 
477 void TextNodeDumper::Visit(const APValue &Value, QualType Ty) {
478   ColorScope Color(OS, ShowColors, ValueKindColor);
479   switch (Value.getKind()) {
480   case APValue::None:
481     OS << "None";
482     return;
483   case APValue::Indeterminate:
484     OS << "Indeterminate";
485     return;
486   case APValue::Int:
487     OS << "Int ";
488     {
489       ColorScope Color(OS, ShowColors, ValueColor);
490       OS << Value.getInt();
491     }
492     return;
493   case APValue::Float:
494     OS << "Float ";
495     {
496       ColorScope Color(OS, ShowColors, ValueColor);
497       OS << GetApproxValue(Value.getFloat());
498     }
499     return;
500   case APValue::FixedPoint:
501     OS << "FixedPoint ";
502     {
503       ColorScope Color(OS, ShowColors, ValueColor);
504       OS << Value.getFixedPoint();
505     }
506     return;
507   case APValue::Vector: {
508     unsigned VectorLength = Value.getVectorLength();
509     OS << "Vector length=" << VectorLength;
510 
511     dumpAPValueChildren(
512         Value, Ty,
513         [](const APValue &Value, unsigned Index) -> const APValue & {
514           return Value.getVectorElt(Index);
515         },
516         VectorLength, "element", "elements");
517     return;
518   }
519   case APValue::ComplexInt:
520     OS << "ComplexInt ";
521     {
522       ColorScope Color(OS, ShowColors, ValueColor);
523       OS << Value.getComplexIntReal() << " + " << Value.getComplexIntImag()
524          << 'i';
525     }
526     return;
527   case APValue::ComplexFloat:
528     OS << "ComplexFloat ";
529     {
530       ColorScope Color(OS, ShowColors, ValueColor);
531       OS << GetApproxValue(Value.getComplexFloatReal()) << " + "
532          << GetApproxValue(Value.getComplexFloatImag()) << 'i';
533     }
534     return;
535   case APValue::LValue:
536     (void)Context;
537     OS << "LValue <todo>";
538     return;
539   case APValue::Array: {
540     unsigned ArraySize = Value.getArraySize();
541     unsigned NumInitializedElements = Value.getArrayInitializedElts();
542     OS << "Array size=" << ArraySize;
543 
544     dumpAPValueChildren(
545         Value, Ty,
546         [](const APValue &Value, unsigned Index) -> const APValue & {
547           return Value.getArrayInitializedElt(Index);
548         },
549         NumInitializedElements, "element", "elements");
550 
551     if (Value.hasArrayFiller()) {
552       AddChild("filler", [=] {
553         {
554           ColorScope Color(OS, ShowColors, ValueColor);
555           OS << ArraySize - NumInitializedElements << " x ";
556         }
557         Visit(Value.getArrayFiller(), Ty);
558       });
559     }
560 
561     return;
562   }
563   case APValue::Struct: {
564     OS << "Struct";
565 
566     dumpAPValueChildren(
567         Value, Ty,
568         [](const APValue &Value, unsigned Index) -> const APValue & {
569           return Value.getStructBase(Index);
570         },
571         Value.getStructNumBases(), "base", "bases");
572 
573     dumpAPValueChildren(
574         Value, Ty,
575         [](const APValue &Value, unsigned Index) -> const APValue & {
576           return Value.getStructField(Index);
577         },
578         Value.getStructNumFields(), "field", "fields");
579 
580     return;
581   }
582   case APValue::Union: {
583     OS << "Union";
584     {
585       ColorScope Color(OS, ShowColors, ValueColor);
586       if (const FieldDecl *FD = Value.getUnionField())
587         OS << " ." << *cast<NamedDecl>(FD);
588     }
589     // If the union value is considered to be simple, fold it into the
590     // current line to save some vertical space.
591     const APValue &UnionValue = Value.getUnionValue();
592     if (isSimpleAPValue(UnionValue)) {
593       OS << ' ';
594       Visit(UnionValue, Ty);
595     } else {
596       AddChild([=] { Visit(UnionValue, Ty); });
597     }
598 
599     return;
600   }
601   case APValue::MemberPointer:
602     OS << "MemberPointer <todo>";
603     return;
604   case APValue::AddrLabelDiff:
605     OS << "AddrLabelDiff <todo>";
606     return;
607   }
608   llvm_unreachable("Unknown APValue kind!");
609 }
610 
611 void TextNodeDumper::dumpPointer(const void *Ptr) {
612   ColorScope Color(OS, ShowColors, AddressColor);
613   OS << ' ' << Ptr;
614 }
615 
616 void TextNodeDumper::dumpLocation(SourceLocation Loc) {
617   if (!SM)
618     return;
619 
620   ColorScope Color(OS, ShowColors, LocationColor);
621   SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
622 
623   // The general format we print out is filename:line:col, but we drop pieces
624   // that haven't changed since the last loc printed.
625   PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
626 
627   if (PLoc.isInvalid()) {
628     OS << "<invalid sloc>";
629     return;
630   }
631 
632   if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
633     OS << PLoc.getFilename() << ':' << PLoc.getLine() << ':'
634        << PLoc.getColumn();
635     LastLocFilename = PLoc.getFilename();
636     LastLocLine = PLoc.getLine();
637   } else if (PLoc.getLine() != LastLocLine) {
638     OS << "line" << ':' << PLoc.getLine() << ':' << PLoc.getColumn();
639     LastLocLine = PLoc.getLine();
640   } else {
641     OS << "col" << ':' << PLoc.getColumn();
642   }
643 }
644 
645 void TextNodeDumper::dumpSourceRange(SourceRange R) {
646   // Can't translate locations if a SourceManager isn't available.
647   if (!SM)
648     return;
649 
650   OS << " <";
651   dumpLocation(R.getBegin());
652   if (R.getBegin() != R.getEnd()) {
653     OS << ", ";
654     dumpLocation(R.getEnd());
655   }
656   OS << ">";
657 
658   // <t2.c:123:421[blah], t2.c:412:321>
659 }
660 
661 void TextNodeDumper::dumpBareType(QualType T, bool Desugar) {
662   ColorScope Color(OS, ShowColors, TypeColor);
663 
664   SplitQualType T_split = T.split();
665   OS << "'" << QualType::getAsString(T_split, PrintPolicy) << "'";
666 
667   if (Desugar && !T.isNull()) {
668     // If the type is sugared, also dump a (shallow) desugared type.
669     SplitQualType D_split = T.getSplitDesugaredType();
670     if (T_split != D_split)
671       OS << ":'" << QualType::getAsString(D_split, PrintPolicy) << "'";
672   }
673 }
674 
675 void TextNodeDumper::dumpType(QualType T) {
676   OS << ' ';
677   dumpBareType(T);
678 }
679 
680 void TextNodeDumper::dumpBareDeclRef(const Decl *D) {
681   if (!D) {
682     ColorScope Color(OS, ShowColors, NullColor);
683     OS << "<<<NULL>>>";
684     return;
685   }
686 
687   {
688     ColorScope Color(OS, ShowColors, DeclKindNameColor);
689     OS << D->getDeclKindName();
690   }
691   dumpPointer(D);
692 
693   if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
694     ColorScope Color(OS, ShowColors, DeclNameColor);
695     OS << " '" << ND->getDeclName() << '\'';
696   }
697 
698   if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
699     dumpType(VD->getType());
700 }
701 
702 void TextNodeDumper::dumpName(const NamedDecl *ND) {
703   if (ND->getDeclName()) {
704     ColorScope Color(OS, ShowColors, DeclNameColor);
705     OS << ' ' << ND->getDeclName();
706   }
707 }
708 
709 void TextNodeDumper::dumpAccessSpecifier(AccessSpecifier AS) {
710   const auto AccessSpelling = getAccessSpelling(AS);
711   if (AccessSpelling.empty())
712     return;
713   OS << AccessSpelling;
714 }
715 
716 void TextNodeDumper::dumpCleanupObject(
717     const ExprWithCleanups::CleanupObject &C) {
718   if (auto *BD = C.dyn_cast<BlockDecl *>())
719     dumpDeclRef(BD, "cleanup");
720   else if (auto *CLE = C.dyn_cast<CompoundLiteralExpr *>())
721     AddChild([=] {
722       OS << "cleanup ";
723       {
724         ColorScope Color(OS, ShowColors, StmtColor);
725         OS << CLE->getStmtClassName();
726       }
727       dumpPointer(CLE);
728     });
729   else
730     llvm_unreachable("unexpected cleanup type");
731 }
732 
733 void TextNodeDumper::dumpDeclRef(const Decl *D, StringRef Label) {
734   if (!D)
735     return;
736 
737   AddChild([=] {
738     if (!Label.empty())
739       OS << Label << ' ';
740     dumpBareDeclRef(D);
741   });
742 }
743 
744 const char *TextNodeDumper::getCommandName(unsigned CommandID) {
745   if (Traits)
746     return Traits->getCommandInfo(CommandID)->Name;
747   const comments::CommandInfo *Info =
748       comments::CommandTraits::getBuiltinCommandInfo(CommandID);
749   if (Info)
750     return Info->Name;
751   return "<not a builtin command>";
752 }
753 
754 void TextNodeDumper::printFPOptions(FPOptionsOverride FPO) {
755 #define OPTION(NAME, TYPE, WIDTH, PREVIOUS)                                    \
756   if (FPO.has##NAME##Override())                                               \
757     OS << " " #NAME "=" << FPO.get##NAME##Override();
758 #include "clang/Basic/FPOptions.def"
759 }
760 
761 void TextNodeDumper::visitTextComment(const comments::TextComment *C,
762                                       const comments::FullComment *) {
763   OS << " Text=\"" << C->getText() << "\"";
764 }
765 
766 void TextNodeDumper::visitInlineCommandComment(
767     const comments::InlineCommandComment *C, const comments::FullComment *) {
768   OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
769   switch (C->getRenderKind()) {
770   case comments::InlineCommandComment::RenderNormal:
771     OS << " RenderNormal";
772     break;
773   case comments::InlineCommandComment::RenderBold:
774     OS << " RenderBold";
775     break;
776   case comments::InlineCommandComment::RenderMonospaced:
777     OS << " RenderMonospaced";
778     break;
779   case comments::InlineCommandComment::RenderEmphasized:
780     OS << " RenderEmphasized";
781     break;
782   case comments::InlineCommandComment::RenderAnchor:
783     OS << " RenderAnchor";
784     break;
785   }
786 
787   for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
788     OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
789 }
790 
791 void TextNodeDumper::visitHTMLStartTagComment(
792     const comments::HTMLStartTagComment *C, const comments::FullComment *) {
793   OS << " Name=\"" << C->getTagName() << "\"";
794   if (C->getNumAttrs() != 0) {
795     OS << " Attrs: ";
796     for (unsigned i = 0, e = C->getNumAttrs(); i != e; ++i) {
797       const comments::HTMLStartTagComment::Attribute &Attr = C->getAttr(i);
798       OS << " \"" << Attr.Name << "=\"" << Attr.Value << "\"";
799     }
800   }
801   if (C->isSelfClosing())
802     OS << " SelfClosing";
803 }
804 
805 void TextNodeDumper::visitHTMLEndTagComment(
806     const comments::HTMLEndTagComment *C, const comments::FullComment *) {
807   OS << " Name=\"" << C->getTagName() << "\"";
808 }
809 
810 void TextNodeDumper::visitBlockCommandComment(
811     const comments::BlockCommandComment *C, const comments::FullComment *) {
812   OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
813   for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
814     OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
815 }
816 
817 void TextNodeDumper::visitParamCommandComment(
818     const comments::ParamCommandComment *C, const comments::FullComment *FC) {
819   OS << " "
820      << comments::ParamCommandComment::getDirectionAsString(C->getDirection());
821 
822   if (C->isDirectionExplicit())
823     OS << " explicitly";
824   else
825     OS << " implicitly";
826 
827   if (C->hasParamName()) {
828     if (C->isParamIndexValid())
829       OS << " Param=\"" << C->getParamName(FC) << "\"";
830     else
831       OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
832   }
833 
834   if (C->isParamIndexValid() && !C->isVarArgParam())
835     OS << " ParamIndex=" << C->getParamIndex();
836 }
837 
838 void TextNodeDumper::visitTParamCommandComment(
839     const comments::TParamCommandComment *C, const comments::FullComment *FC) {
840   if (C->hasParamName()) {
841     if (C->isPositionValid())
842       OS << " Param=\"" << C->getParamName(FC) << "\"";
843     else
844       OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
845   }
846 
847   if (C->isPositionValid()) {
848     OS << " Position=<";
849     for (unsigned i = 0, e = C->getDepth(); i != e; ++i) {
850       OS << C->getIndex(i);
851       if (i != e - 1)
852         OS << ", ";
853     }
854     OS << ">";
855   }
856 }
857 
858 void TextNodeDumper::visitVerbatimBlockComment(
859     const comments::VerbatimBlockComment *C, const comments::FullComment *) {
860   OS << " Name=\"" << getCommandName(C->getCommandID())
861      << "\""
862         " CloseName=\""
863      << C->getCloseName() << "\"";
864 }
865 
866 void TextNodeDumper::visitVerbatimBlockLineComment(
867     const comments::VerbatimBlockLineComment *C,
868     const comments::FullComment *) {
869   OS << " Text=\"" << C->getText() << "\"";
870 }
871 
872 void TextNodeDumper::visitVerbatimLineComment(
873     const comments::VerbatimLineComment *C, const comments::FullComment *) {
874   OS << " Text=\"" << C->getText() << "\"";
875 }
876 
877 void TextNodeDumper::VisitNullTemplateArgument(const TemplateArgument &) {
878   OS << " null";
879 }
880 
881 void TextNodeDumper::VisitTypeTemplateArgument(const TemplateArgument &TA) {
882   OS << " type";
883   dumpType(TA.getAsType());
884 }
885 
886 void TextNodeDumper::VisitDeclarationTemplateArgument(
887     const TemplateArgument &TA) {
888   OS << " decl";
889   dumpDeclRef(TA.getAsDecl());
890 }
891 
892 void TextNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &) {
893   OS << " nullptr";
894 }
895 
896 void TextNodeDumper::VisitIntegralTemplateArgument(const TemplateArgument &TA) {
897   OS << " integral " << TA.getAsIntegral();
898 }
899 
900 void TextNodeDumper::VisitTemplateTemplateArgument(const TemplateArgument &TA) {
901   OS << " template ";
902   TA.getAsTemplate().dump(OS);
903 }
904 
905 void TextNodeDumper::VisitTemplateExpansionTemplateArgument(
906     const TemplateArgument &TA) {
907   OS << " template expansion ";
908   TA.getAsTemplateOrTemplatePattern().dump(OS);
909 }
910 
911 void TextNodeDumper::VisitExpressionTemplateArgument(const TemplateArgument &) {
912   OS << " expr";
913 }
914 
915 void TextNodeDumper::VisitPackTemplateArgument(const TemplateArgument &) {
916   OS << " pack";
917 }
918 
919 static void dumpBasePath(raw_ostream &OS, const CastExpr *Node) {
920   if (Node->path_empty())
921     return;
922 
923   OS << " (";
924   bool First = true;
925   for (CastExpr::path_const_iterator I = Node->path_begin(),
926                                      E = Node->path_end();
927        I != E; ++I) {
928     const CXXBaseSpecifier *Base = *I;
929     if (!First)
930       OS << " -> ";
931 
932     const auto *RD =
933         cast<CXXRecordDecl>(Base->getType()->castAs<RecordType>()->getDecl());
934 
935     if (Base->isVirtual())
936       OS << "virtual ";
937     OS << RD->getName();
938     First = false;
939   }
940 
941   OS << ')';
942 }
943 
944 void TextNodeDumper::VisitIfStmt(const IfStmt *Node) {
945   if (Node->hasInitStorage())
946     OS << " has_init";
947   if (Node->hasVarStorage())
948     OS << " has_var";
949   if (Node->hasElseStorage())
950     OS << " has_else";
951 }
952 
953 void TextNodeDumper::VisitSwitchStmt(const SwitchStmt *Node) {
954   if (Node->hasInitStorage())
955     OS << " has_init";
956   if (Node->hasVarStorage())
957     OS << " has_var";
958 }
959 
960 void TextNodeDumper::VisitWhileStmt(const WhileStmt *Node) {
961   if (Node->hasVarStorage())
962     OS << " has_var";
963 }
964 
965 void TextNodeDumper::VisitLabelStmt(const LabelStmt *Node) {
966   OS << " '" << Node->getName() << "'";
967   if (Node->isSideEntry())
968     OS << " side_entry";
969 }
970 
971 void TextNodeDumper::VisitGotoStmt(const GotoStmt *Node) {
972   OS << " '" << Node->getLabel()->getName() << "'";
973   dumpPointer(Node->getLabel());
974 }
975 
976 void TextNodeDumper::VisitCaseStmt(const CaseStmt *Node) {
977   if (Node->caseStmtIsGNURange())
978     OS << " gnu_range";
979 }
980 
981 void TextNodeDumper::VisitConstantExpr(const ConstantExpr *Node) {
982   if (Node->hasAPValueResult())
983     AddChild("value",
984              [=] { Visit(Node->getAPValueResult(), Node->getType()); });
985 }
986 
987 void TextNodeDumper::VisitCallExpr(const CallExpr *Node) {
988   if (Node->usesADL())
989     OS << " adl";
990   if (Node->hasStoredFPFeatures())
991     printFPOptions(Node->getFPFeatures());
992 }
993 
994 void TextNodeDumper::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *Node) {
995   const char *OperatorSpelling = clang::getOperatorSpelling(Node->getOperator());
996   if (OperatorSpelling)
997     OS << " '" << OperatorSpelling << "'";
998 
999   VisitCallExpr(Node);
1000 }
1001 
1002 void TextNodeDumper::VisitCastExpr(const CastExpr *Node) {
1003   OS << " <";
1004   {
1005     ColorScope Color(OS, ShowColors, CastColor);
1006     OS << Node->getCastKindName();
1007   }
1008   dumpBasePath(OS, Node);
1009   OS << ">";
1010   if (Node->hasStoredFPFeatures())
1011     printFPOptions(Node->getFPFeatures());
1012 }
1013 
1014 void TextNodeDumper::VisitImplicitCastExpr(const ImplicitCastExpr *Node) {
1015   VisitCastExpr(Node);
1016   if (Node->isPartOfExplicitCast())
1017     OS << " part_of_explicit_cast";
1018 }
1019 
1020 void TextNodeDumper::VisitDeclRefExpr(const DeclRefExpr *Node) {
1021   OS << " ";
1022   dumpBareDeclRef(Node->getDecl());
1023   if (Node->getDecl() != Node->getFoundDecl()) {
1024     OS << " (";
1025     dumpBareDeclRef(Node->getFoundDecl());
1026     OS << ")";
1027   }
1028   switch (Node->isNonOdrUse()) {
1029   case NOUR_None: break;
1030   case NOUR_Unevaluated: OS << " non_odr_use_unevaluated"; break;
1031   case NOUR_Constant: OS << " non_odr_use_constant"; break;
1032   case NOUR_Discarded: OS << " non_odr_use_discarded"; break;
1033   }
1034 }
1035 
1036 void TextNodeDumper::VisitUnresolvedLookupExpr(
1037     const UnresolvedLookupExpr *Node) {
1038   OS << " (";
1039   if (!Node->requiresADL())
1040     OS << "no ";
1041   OS << "ADL) = '" << Node->getName() << '\'';
1042 
1043   UnresolvedLookupExpr::decls_iterator I = Node->decls_begin(),
1044                                        E = Node->decls_end();
1045   if (I == E)
1046     OS << " empty";
1047   for (; I != E; ++I)
1048     dumpPointer(*I);
1049 }
1050 
1051 void TextNodeDumper::VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node) {
1052   {
1053     ColorScope Color(OS, ShowColors, DeclKindNameColor);
1054     OS << " " << Node->getDecl()->getDeclKindName() << "Decl";
1055   }
1056   OS << "='" << *Node->getDecl() << "'";
1057   dumpPointer(Node->getDecl());
1058   if (Node->isFreeIvar())
1059     OS << " isFreeIvar";
1060 }
1061 
1062 void TextNodeDumper::VisitSYCLUniqueStableNameExpr(
1063     const SYCLUniqueStableNameExpr *Node) {
1064   dumpType(Node->getTypeSourceInfo()->getType());
1065 }
1066 
1067 void TextNodeDumper::VisitPredefinedExpr(const PredefinedExpr *Node) {
1068   OS << " " << PredefinedExpr::getIdentKindName(Node->getIdentKind());
1069 }
1070 
1071 void TextNodeDumper::VisitCharacterLiteral(const CharacterLiteral *Node) {
1072   ColorScope Color(OS, ShowColors, ValueColor);
1073   OS << " " << Node->getValue();
1074 }
1075 
1076 void TextNodeDumper::VisitIntegerLiteral(const IntegerLiteral *Node) {
1077   bool isSigned = Node->getType()->isSignedIntegerType();
1078   ColorScope Color(OS, ShowColors, ValueColor);
1079   OS << " " << toString(Node->getValue(), 10, isSigned);
1080 }
1081 
1082 void TextNodeDumper::VisitFixedPointLiteral(const FixedPointLiteral *Node) {
1083   ColorScope Color(OS, ShowColors, ValueColor);
1084   OS << " " << Node->getValueAsString(/*Radix=*/10);
1085 }
1086 
1087 void TextNodeDumper::VisitFloatingLiteral(const FloatingLiteral *Node) {
1088   ColorScope Color(OS, ShowColors, ValueColor);
1089   OS << " " << Node->getValueAsApproximateDouble();
1090 }
1091 
1092 void TextNodeDumper::VisitStringLiteral(const StringLiteral *Str) {
1093   ColorScope Color(OS, ShowColors, ValueColor);
1094   OS << " ";
1095   Str->outputString(OS);
1096 }
1097 
1098 void TextNodeDumper::VisitInitListExpr(const InitListExpr *ILE) {
1099   if (auto *Field = ILE->getInitializedFieldInUnion()) {
1100     OS << " field ";
1101     dumpBareDeclRef(Field);
1102   }
1103 }
1104 
1105 void TextNodeDumper::VisitGenericSelectionExpr(const GenericSelectionExpr *E) {
1106   if (E->isResultDependent())
1107     OS << " result_dependent";
1108 }
1109 
1110 void TextNodeDumper::VisitUnaryOperator(const UnaryOperator *Node) {
1111   OS << " " << (Node->isPostfix() ? "postfix" : "prefix") << " '"
1112      << UnaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
1113   if (!Node->canOverflow())
1114     OS << " cannot overflow";
1115   if (Node->hasStoredFPFeatures())
1116     printFPOptions(Node->getStoredFPFeatures());
1117 }
1118 
1119 void TextNodeDumper::VisitUnaryExprOrTypeTraitExpr(
1120     const UnaryExprOrTypeTraitExpr *Node) {
1121   OS << " " << getTraitSpelling(Node->getKind());
1122 
1123   if (Node->isArgumentType())
1124     dumpType(Node->getArgumentType());
1125 }
1126 
1127 void TextNodeDumper::VisitMemberExpr(const MemberExpr *Node) {
1128   OS << " " << (Node->isArrow() ? "->" : ".") << *Node->getMemberDecl();
1129   dumpPointer(Node->getMemberDecl());
1130   switch (Node->isNonOdrUse()) {
1131   case NOUR_None: break;
1132   case NOUR_Unevaluated: OS << " non_odr_use_unevaluated"; break;
1133   case NOUR_Constant: OS << " non_odr_use_constant"; break;
1134   case NOUR_Discarded: OS << " non_odr_use_discarded"; break;
1135   }
1136 }
1137 
1138 void TextNodeDumper::VisitExtVectorElementExpr(
1139     const ExtVectorElementExpr *Node) {
1140   OS << " " << Node->getAccessor().getNameStart();
1141 }
1142 
1143 void TextNodeDumper::VisitBinaryOperator(const BinaryOperator *Node) {
1144   OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
1145   if (Node->hasStoredFPFeatures())
1146     printFPOptions(Node->getStoredFPFeatures());
1147 }
1148 
1149 void TextNodeDumper::VisitCompoundAssignOperator(
1150     const CompoundAssignOperator *Node) {
1151   OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode())
1152      << "' ComputeLHSTy=";
1153   dumpBareType(Node->getComputationLHSType());
1154   OS << " ComputeResultTy=";
1155   dumpBareType(Node->getComputationResultType());
1156   if (Node->hasStoredFPFeatures())
1157     printFPOptions(Node->getStoredFPFeatures());
1158 }
1159 
1160 void TextNodeDumper::VisitAddrLabelExpr(const AddrLabelExpr *Node) {
1161   OS << " " << Node->getLabel()->getName();
1162   dumpPointer(Node->getLabel());
1163 }
1164 
1165 void TextNodeDumper::VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node) {
1166   OS << " " << Node->getCastName() << "<"
1167      << Node->getTypeAsWritten().getAsString() << ">"
1168      << " <" << Node->getCastKindName();
1169   dumpBasePath(OS, Node);
1170   OS << ">";
1171 }
1172 
1173 void TextNodeDumper::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node) {
1174   OS << " " << (Node->getValue() ? "true" : "false");
1175 }
1176 
1177 void TextNodeDumper::VisitCXXThisExpr(const CXXThisExpr *Node) {
1178   if (Node->isImplicit())
1179     OS << " implicit";
1180   OS << " this";
1181 }
1182 
1183 void TextNodeDumper::VisitCXXFunctionalCastExpr(
1184     const CXXFunctionalCastExpr *Node) {
1185   OS << " functional cast to " << Node->getTypeAsWritten().getAsString() << " <"
1186      << Node->getCastKindName() << ">";
1187   if (Node->hasStoredFPFeatures())
1188     printFPOptions(Node->getFPFeatures());
1189 }
1190 
1191 void TextNodeDumper::VisitCXXStaticCastExpr(const CXXStaticCastExpr *Node) {
1192   VisitCXXNamedCastExpr(Node);
1193   if (Node->hasStoredFPFeatures())
1194     printFPOptions(Node->getFPFeatures());
1195 }
1196 
1197 void TextNodeDumper::VisitCXXUnresolvedConstructExpr(
1198     const CXXUnresolvedConstructExpr *Node) {
1199   dumpType(Node->getTypeAsWritten());
1200   if (Node->isListInitialization())
1201     OS << " list";
1202 }
1203 
1204 void TextNodeDumper::VisitCXXConstructExpr(const CXXConstructExpr *Node) {
1205   CXXConstructorDecl *Ctor = Node->getConstructor();
1206   dumpType(Ctor->getType());
1207   if (Node->isElidable())
1208     OS << " elidable";
1209   if (Node->isListInitialization())
1210     OS << " list";
1211   if (Node->isStdInitListInitialization())
1212     OS << " std::initializer_list";
1213   if (Node->requiresZeroInitialization())
1214     OS << " zeroing";
1215 }
1216 
1217 void TextNodeDumper::VisitCXXBindTemporaryExpr(
1218     const CXXBindTemporaryExpr *Node) {
1219   OS << " (CXXTemporary";
1220   dumpPointer(Node);
1221   OS << ")";
1222 }
1223 
1224 void TextNodeDumper::VisitCXXNewExpr(const CXXNewExpr *Node) {
1225   if (Node->isGlobalNew())
1226     OS << " global";
1227   if (Node->isArray())
1228     OS << " array";
1229   if (Node->getOperatorNew()) {
1230     OS << ' ';
1231     dumpBareDeclRef(Node->getOperatorNew());
1232   }
1233   // We could dump the deallocation function used in case of error, but it's
1234   // usually not that interesting.
1235 }
1236 
1237 void TextNodeDumper::VisitCXXDeleteExpr(const CXXDeleteExpr *Node) {
1238   if (Node->isGlobalDelete())
1239     OS << " global";
1240   if (Node->isArrayForm())
1241     OS << " array";
1242   if (Node->getOperatorDelete()) {
1243     OS << ' ';
1244     dumpBareDeclRef(Node->getOperatorDelete());
1245   }
1246 }
1247 
1248 void TextNodeDumper::VisitTypeTraitExpr(const TypeTraitExpr *Node) {
1249   OS << " " << getTraitSpelling(Node->getTrait());
1250 }
1251 
1252 void TextNodeDumper::VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *Node) {
1253   OS << " " << getTraitSpelling(Node->getTrait());
1254 }
1255 
1256 void TextNodeDumper::VisitExpressionTraitExpr(const ExpressionTraitExpr *Node) {
1257   OS << " " << getTraitSpelling(Node->getTrait());
1258 }
1259 
1260 void TextNodeDumper::VisitMaterializeTemporaryExpr(
1261     const MaterializeTemporaryExpr *Node) {
1262   if (const ValueDecl *VD = Node->getExtendingDecl()) {
1263     OS << " extended by ";
1264     dumpBareDeclRef(VD);
1265   }
1266 }
1267 
1268 void TextNodeDumper::VisitExprWithCleanups(const ExprWithCleanups *Node) {
1269   for (unsigned i = 0, e = Node->getNumObjects(); i != e; ++i)
1270     dumpCleanupObject(Node->getObject(i));
1271 }
1272 
1273 void TextNodeDumper::VisitSizeOfPackExpr(const SizeOfPackExpr *Node) {
1274   dumpPointer(Node->getPack());
1275   dumpName(Node->getPack());
1276 }
1277 
1278 void TextNodeDumper::VisitCXXDependentScopeMemberExpr(
1279     const CXXDependentScopeMemberExpr *Node) {
1280   OS << " " << (Node->isArrow() ? "->" : ".") << Node->getMember();
1281 }
1282 
1283 void TextNodeDumper::VisitObjCMessageExpr(const ObjCMessageExpr *Node) {
1284   OS << " selector=";
1285   Node->getSelector().print(OS);
1286   switch (Node->getReceiverKind()) {
1287   case ObjCMessageExpr::Instance:
1288     break;
1289 
1290   case ObjCMessageExpr::Class:
1291     OS << " class=";
1292     dumpBareType(Node->getClassReceiver());
1293     break;
1294 
1295   case ObjCMessageExpr::SuperInstance:
1296     OS << " super (instance)";
1297     break;
1298 
1299   case ObjCMessageExpr::SuperClass:
1300     OS << " super (class)";
1301     break;
1302   }
1303 }
1304 
1305 void TextNodeDumper::VisitObjCBoxedExpr(const ObjCBoxedExpr *Node) {
1306   if (auto *BoxingMethod = Node->getBoxingMethod()) {
1307     OS << " selector=";
1308     BoxingMethod->getSelector().print(OS);
1309   }
1310 }
1311 
1312 void TextNodeDumper::VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node) {
1313   if (!Node->getCatchParamDecl())
1314     OS << " catch all";
1315 }
1316 
1317 void TextNodeDumper::VisitObjCEncodeExpr(const ObjCEncodeExpr *Node) {
1318   dumpType(Node->getEncodedType());
1319 }
1320 
1321 void TextNodeDumper::VisitObjCSelectorExpr(const ObjCSelectorExpr *Node) {
1322   OS << " ";
1323   Node->getSelector().print(OS);
1324 }
1325 
1326 void TextNodeDumper::VisitObjCProtocolExpr(const ObjCProtocolExpr *Node) {
1327   OS << ' ' << *Node->getProtocol();
1328 }
1329 
1330 void TextNodeDumper::VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node) {
1331   if (Node->isImplicitProperty()) {
1332     OS << " Kind=MethodRef Getter=\"";
1333     if (Node->getImplicitPropertyGetter())
1334       Node->getImplicitPropertyGetter()->getSelector().print(OS);
1335     else
1336       OS << "(null)";
1337 
1338     OS << "\" Setter=\"";
1339     if (ObjCMethodDecl *Setter = Node->getImplicitPropertySetter())
1340       Setter->getSelector().print(OS);
1341     else
1342       OS << "(null)";
1343     OS << "\"";
1344   } else {
1345     OS << " Kind=PropertyRef Property=\"" << *Node->getExplicitProperty()
1346        << '"';
1347   }
1348 
1349   if (Node->isSuperReceiver())
1350     OS << " super";
1351 
1352   OS << " Messaging=";
1353   if (Node->isMessagingGetter() && Node->isMessagingSetter())
1354     OS << "Getter&Setter";
1355   else if (Node->isMessagingGetter())
1356     OS << "Getter";
1357   else if (Node->isMessagingSetter())
1358     OS << "Setter";
1359 }
1360 
1361 void TextNodeDumper::VisitObjCSubscriptRefExpr(
1362     const ObjCSubscriptRefExpr *Node) {
1363   if (Node->isArraySubscriptRefExpr())
1364     OS << " Kind=ArraySubscript GetterForArray=\"";
1365   else
1366     OS << " Kind=DictionarySubscript GetterForDictionary=\"";
1367   if (Node->getAtIndexMethodDecl())
1368     Node->getAtIndexMethodDecl()->getSelector().print(OS);
1369   else
1370     OS << "(null)";
1371 
1372   if (Node->isArraySubscriptRefExpr())
1373     OS << "\" SetterForArray=\"";
1374   else
1375     OS << "\" SetterForDictionary=\"";
1376   if (Node->setAtIndexMethodDecl())
1377     Node->setAtIndexMethodDecl()->getSelector().print(OS);
1378   else
1379     OS << "(null)";
1380 }
1381 
1382 void TextNodeDumper::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node) {
1383   OS << " " << (Node->getValue() ? "__objc_yes" : "__objc_no");
1384 }
1385 
1386 void TextNodeDumper::VisitOMPIteratorExpr(const OMPIteratorExpr *Node) {
1387   OS << " ";
1388   for (unsigned I = 0, E = Node->numOfIterators(); I < E; ++I) {
1389     Visit(Node->getIteratorDecl(I));
1390     OS << " = ";
1391     const OMPIteratorExpr::IteratorRange Range = Node->getIteratorRange(I);
1392     OS << " begin ";
1393     Visit(Range.Begin);
1394     OS << " end ";
1395     Visit(Range.End);
1396     if (Range.Step) {
1397       OS << " step ";
1398       Visit(Range.Step);
1399     }
1400   }
1401 }
1402 
1403 void TextNodeDumper::VisitConceptSpecializationExpr(
1404     const ConceptSpecializationExpr *Node) {
1405   OS << " ";
1406   dumpBareDeclRef(Node->getFoundDecl());
1407 }
1408 
1409 void TextNodeDumper::VisitRequiresExpr(
1410     const RequiresExpr *Node) {
1411   if (!Node->isValueDependent())
1412     OS << (Node->isSatisfied() ? " satisfied" : " unsatisfied");
1413 }
1414 
1415 void TextNodeDumper::VisitRValueReferenceType(const ReferenceType *T) {
1416   if (T->isSpelledAsLValue())
1417     OS << " written as lvalue reference";
1418 }
1419 
1420 void TextNodeDumper::VisitArrayType(const ArrayType *T) {
1421   switch (T->getSizeModifier()) {
1422   case ArrayType::Normal:
1423     break;
1424   case ArrayType::Static:
1425     OS << " static";
1426     break;
1427   case ArrayType::Star:
1428     OS << " *";
1429     break;
1430   }
1431   OS << " " << T->getIndexTypeQualifiers().getAsString();
1432 }
1433 
1434 void TextNodeDumper::VisitConstantArrayType(const ConstantArrayType *T) {
1435   OS << " " << T->getSize();
1436   VisitArrayType(T);
1437 }
1438 
1439 void TextNodeDumper::VisitVariableArrayType(const VariableArrayType *T) {
1440   OS << " ";
1441   dumpSourceRange(T->getBracketsRange());
1442   VisitArrayType(T);
1443 }
1444 
1445 void TextNodeDumper::VisitDependentSizedArrayType(
1446     const DependentSizedArrayType *T) {
1447   VisitArrayType(T);
1448   OS << " ";
1449   dumpSourceRange(T->getBracketsRange());
1450 }
1451 
1452 void TextNodeDumper::VisitDependentSizedExtVectorType(
1453     const DependentSizedExtVectorType *T) {
1454   OS << " ";
1455   dumpLocation(T->getAttributeLoc());
1456 }
1457 
1458 void TextNodeDumper::VisitVectorType(const VectorType *T) {
1459   switch (T->getVectorKind()) {
1460   case VectorType::GenericVector:
1461     break;
1462   case VectorType::AltiVecVector:
1463     OS << " altivec";
1464     break;
1465   case VectorType::AltiVecPixel:
1466     OS << " altivec pixel";
1467     break;
1468   case VectorType::AltiVecBool:
1469     OS << " altivec bool";
1470     break;
1471   case VectorType::NeonVector:
1472     OS << " neon";
1473     break;
1474   case VectorType::NeonPolyVector:
1475     OS << " neon poly";
1476     break;
1477   case VectorType::SveFixedLengthDataVector:
1478     OS << " fixed-length sve data vector";
1479     break;
1480   case VectorType::SveFixedLengthPredicateVector:
1481     OS << " fixed-length sve predicate vector";
1482     break;
1483   }
1484   OS << " " << T->getNumElements();
1485 }
1486 
1487 void TextNodeDumper::VisitFunctionType(const FunctionType *T) {
1488   auto EI = T->getExtInfo();
1489   if (EI.getNoReturn())
1490     OS << " noreturn";
1491   if (EI.getProducesResult())
1492     OS << " produces_result";
1493   if (EI.getHasRegParm())
1494     OS << " regparm " << EI.getRegParm();
1495   OS << " " << FunctionType::getNameForCallConv(EI.getCC());
1496 }
1497 
1498 void TextNodeDumper::VisitFunctionProtoType(const FunctionProtoType *T) {
1499   auto EPI = T->getExtProtoInfo();
1500   if (EPI.HasTrailingReturn)
1501     OS << " trailing_return";
1502   if (T->isConst())
1503     OS << " const";
1504   if (T->isVolatile())
1505     OS << " volatile";
1506   if (T->isRestrict())
1507     OS << " restrict";
1508   if (T->getExtProtoInfo().Variadic)
1509     OS << " variadic";
1510   switch (EPI.RefQualifier) {
1511   case RQ_None:
1512     break;
1513   case RQ_LValue:
1514     OS << " &";
1515     break;
1516   case RQ_RValue:
1517     OS << " &&";
1518     break;
1519   }
1520   // FIXME: Exception specification.
1521   // FIXME: Consumed parameters.
1522   VisitFunctionType(T);
1523 }
1524 
1525 void TextNodeDumper::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
1526   dumpDeclRef(T->getDecl());
1527 }
1528 
1529 void TextNodeDumper::VisitTypedefType(const TypedefType *T) {
1530   dumpDeclRef(T->getDecl());
1531 }
1532 
1533 void TextNodeDumper::VisitUnaryTransformType(const UnaryTransformType *T) {
1534   switch (T->getUTTKind()) {
1535   case UnaryTransformType::EnumUnderlyingType:
1536     OS << " underlying_type";
1537     break;
1538   }
1539 }
1540 
1541 void TextNodeDumper::VisitTagType(const TagType *T) {
1542   dumpDeclRef(T->getDecl());
1543 }
1544 
1545 void TextNodeDumper::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1546   OS << " depth " << T->getDepth() << " index " << T->getIndex();
1547   if (T->isParameterPack())
1548     OS << " pack";
1549   dumpDeclRef(T->getDecl());
1550 }
1551 
1552 void TextNodeDumper::VisitAutoType(const AutoType *T) {
1553   if (T->isDecltypeAuto())
1554     OS << " decltype(auto)";
1555   if (!T->isDeduced())
1556     OS << " undeduced";
1557   if (T->isConstrained()) {
1558     dumpDeclRef(T->getTypeConstraintConcept());
1559     for (const auto &Arg : T->getTypeConstraintArguments())
1560       VisitTemplateArgument(Arg);
1561   }
1562 }
1563 
1564 void TextNodeDumper::VisitTemplateSpecializationType(
1565     const TemplateSpecializationType *T) {
1566   if (T->isTypeAlias())
1567     OS << " alias";
1568   OS << " ";
1569   T->getTemplateName().dump(OS);
1570 }
1571 
1572 void TextNodeDumper::VisitInjectedClassNameType(
1573     const InjectedClassNameType *T) {
1574   dumpDeclRef(T->getDecl());
1575 }
1576 
1577 void TextNodeDumper::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
1578   dumpDeclRef(T->getDecl());
1579 }
1580 
1581 void TextNodeDumper::VisitPackExpansionType(const PackExpansionType *T) {
1582   if (auto N = T->getNumExpansions())
1583     OS << " expansions " << *N;
1584 }
1585 
1586 void TextNodeDumper::VisitLabelDecl(const LabelDecl *D) { dumpName(D); }
1587 
1588 void TextNodeDumper::VisitTypedefDecl(const TypedefDecl *D) {
1589   dumpName(D);
1590   dumpType(D->getUnderlyingType());
1591   if (D->isModulePrivate())
1592     OS << " __module_private__";
1593 }
1594 
1595 void TextNodeDumper::VisitEnumDecl(const EnumDecl *D) {
1596   if (D->isScoped()) {
1597     if (D->isScopedUsingClassTag())
1598       OS << " class";
1599     else
1600       OS << " struct";
1601   }
1602   dumpName(D);
1603   if (D->isModulePrivate())
1604     OS << " __module_private__";
1605   if (D->isFixed())
1606     dumpType(D->getIntegerType());
1607 }
1608 
1609 void TextNodeDumper::VisitRecordDecl(const RecordDecl *D) {
1610   OS << ' ' << D->getKindName();
1611   dumpName(D);
1612   if (D->isModulePrivate())
1613     OS << " __module_private__";
1614   if (D->isCompleteDefinition())
1615     OS << " definition";
1616 }
1617 
1618 void TextNodeDumper::VisitEnumConstantDecl(const EnumConstantDecl *D) {
1619   dumpName(D);
1620   dumpType(D->getType());
1621 }
1622 
1623 void TextNodeDumper::VisitIndirectFieldDecl(const IndirectFieldDecl *D) {
1624   dumpName(D);
1625   dumpType(D->getType());
1626 
1627   for (const auto *Child : D->chain())
1628     dumpDeclRef(Child);
1629 }
1630 
1631 void TextNodeDumper::VisitFunctionDecl(const FunctionDecl *D) {
1632   dumpName(D);
1633   dumpType(D->getType());
1634 
1635   StorageClass SC = D->getStorageClass();
1636   if (SC != SC_None)
1637     OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
1638   if (D->isInlineSpecified())
1639     OS << " inline";
1640   if (D->isVirtualAsWritten())
1641     OS << " virtual";
1642   if (D->isModulePrivate())
1643     OS << " __module_private__";
1644 
1645   if (D->isPure())
1646     OS << " pure";
1647   if (D->isDefaulted()) {
1648     OS << " default";
1649     if (D->isDeleted())
1650       OS << "_delete";
1651   }
1652   if (D->isDeletedAsWritten())
1653     OS << " delete";
1654   if (D->isTrivial())
1655     OS << " trivial";
1656 
1657   if (const auto *FPT = D->getType()->getAs<FunctionProtoType>()) {
1658     FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
1659     switch (EPI.ExceptionSpec.Type) {
1660     default:
1661       break;
1662     case EST_Unevaluated:
1663       OS << " noexcept-unevaluated " << EPI.ExceptionSpec.SourceDecl;
1664       break;
1665     case EST_Uninstantiated:
1666       OS << " noexcept-uninstantiated " << EPI.ExceptionSpec.SourceTemplate;
1667       break;
1668     }
1669   }
1670 
1671   if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
1672     if (MD->size_overridden_methods() != 0) {
1673       auto dumpOverride = [=](const CXXMethodDecl *D) {
1674         SplitQualType T_split = D->getType().split();
1675         OS << D << " " << D->getParent()->getName() << "::" << D->getDeclName()
1676            << " '" << QualType::getAsString(T_split, PrintPolicy) << "'";
1677       };
1678 
1679       AddChild([=] {
1680         auto Overrides = MD->overridden_methods();
1681         OS << "Overrides: [ ";
1682         dumpOverride(*Overrides.begin());
1683         for (const auto *Override :
1684              llvm::make_range(Overrides.begin() + 1, Overrides.end())) {
1685           OS << ", ";
1686           dumpOverride(Override);
1687         }
1688         OS << " ]";
1689       });
1690     }
1691   }
1692 
1693   // Since NumParams comes from the FunctionProtoType of the FunctionDecl and
1694   // the Params are set later, it is possible for a dump during debugging to
1695   // encounter a FunctionDecl that has been created but hasn't been assigned
1696   // ParmVarDecls yet.
1697   if (!D->param_empty() && !D->param_begin())
1698     OS << " <<<NULL params x " << D->getNumParams() << ">>>";
1699 }
1700 
1701 void TextNodeDumper::VisitLifetimeExtendedTemporaryDecl(
1702     const LifetimeExtendedTemporaryDecl *D) {
1703   OS << " extended by ";
1704   dumpBareDeclRef(D->getExtendingDecl());
1705   OS << " mangling ";
1706   {
1707     ColorScope Color(OS, ShowColors, ValueColor);
1708     OS << D->getManglingNumber();
1709   }
1710 }
1711 
1712 void TextNodeDumper::VisitFieldDecl(const FieldDecl *D) {
1713   dumpName(D);
1714   dumpType(D->getType());
1715   if (D->isMutable())
1716     OS << " mutable";
1717   if (D->isModulePrivate())
1718     OS << " __module_private__";
1719 }
1720 
1721 void TextNodeDumper::VisitVarDecl(const VarDecl *D) {
1722   dumpName(D);
1723   dumpType(D->getType());
1724   StorageClass SC = D->getStorageClass();
1725   if (SC != SC_None)
1726     OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
1727   switch (D->getTLSKind()) {
1728   case VarDecl::TLS_None:
1729     break;
1730   case VarDecl::TLS_Static:
1731     OS << " tls";
1732     break;
1733   case VarDecl::TLS_Dynamic:
1734     OS << " tls_dynamic";
1735     break;
1736   }
1737   if (D->isModulePrivate())
1738     OS << " __module_private__";
1739   if (D->isNRVOVariable())
1740     OS << " nrvo";
1741   if (D->isInline())
1742     OS << " inline";
1743   if (D->isConstexpr())
1744     OS << " constexpr";
1745   if (D->hasInit()) {
1746     switch (D->getInitStyle()) {
1747     case VarDecl::CInit:
1748       OS << " cinit";
1749       break;
1750     case VarDecl::CallInit:
1751       OS << " callinit";
1752       break;
1753     case VarDecl::ListInit:
1754       OS << " listinit";
1755       break;
1756     }
1757   }
1758   if (D->needsDestruction(D->getASTContext()))
1759     OS << " destroyed";
1760   if (D->isParameterPack())
1761     OS << " pack";
1762 
1763   if (D->hasInit()) {
1764     const Expr *E = D->getInit();
1765     // Only dump the value of constexpr VarDecls for now.
1766     if (E && !E->isValueDependent() && D->isConstexpr()) {
1767       const APValue *Value = D->evaluateValue();
1768       if (Value)
1769         AddChild("value", [=] { Visit(*Value, E->getType()); });
1770     }
1771   }
1772 }
1773 
1774 void TextNodeDumper::VisitBindingDecl(const BindingDecl *D) {
1775   dumpName(D);
1776   dumpType(D->getType());
1777 }
1778 
1779 void TextNodeDumper::VisitCapturedDecl(const CapturedDecl *D) {
1780   if (D->isNothrow())
1781     OS << " nothrow";
1782 }
1783 
1784 void TextNodeDumper::VisitImportDecl(const ImportDecl *D) {
1785   OS << ' ' << D->getImportedModule()->getFullModuleName();
1786 
1787   for (Decl *InitD :
1788        D->getASTContext().getModuleInitializers(D->getImportedModule()))
1789     dumpDeclRef(InitD, "initializer");
1790 }
1791 
1792 void TextNodeDumper::VisitPragmaCommentDecl(const PragmaCommentDecl *D) {
1793   OS << ' ';
1794   switch (D->getCommentKind()) {
1795   case PCK_Unknown:
1796     llvm_unreachable("unexpected pragma comment kind");
1797   case PCK_Compiler:
1798     OS << "compiler";
1799     break;
1800   case PCK_ExeStr:
1801     OS << "exestr";
1802     break;
1803   case PCK_Lib:
1804     OS << "lib";
1805     break;
1806   case PCK_Linker:
1807     OS << "linker";
1808     break;
1809   case PCK_User:
1810     OS << "user";
1811     break;
1812   }
1813   StringRef Arg = D->getArg();
1814   if (!Arg.empty())
1815     OS << " \"" << Arg << "\"";
1816 }
1817 
1818 void TextNodeDumper::VisitPragmaDetectMismatchDecl(
1819     const PragmaDetectMismatchDecl *D) {
1820   OS << " \"" << D->getName() << "\" \"" << D->getValue() << "\"";
1821 }
1822 
1823 void TextNodeDumper::VisitOMPExecutableDirective(
1824     const OMPExecutableDirective *D) {
1825   if (D->isStandaloneDirective())
1826     OS << " openmp_standalone_directive";
1827 }
1828 
1829 void TextNodeDumper::VisitOMPDeclareReductionDecl(
1830     const OMPDeclareReductionDecl *D) {
1831   dumpName(D);
1832   dumpType(D->getType());
1833   OS << " combiner";
1834   dumpPointer(D->getCombiner());
1835   if (const auto *Initializer = D->getInitializer()) {
1836     OS << " initializer";
1837     dumpPointer(Initializer);
1838     switch (D->getInitializerKind()) {
1839     case OMPDeclareReductionDecl::DirectInit:
1840       OS << " omp_priv = ";
1841       break;
1842     case OMPDeclareReductionDecl::CopyInit:
1843       OS << " omp_priv ()";
1844       break;
1845     case OMPDeclareReductionDecl::CallInit:
1846       break;
1847     }
1848   }
1849 }
1850 
1851 void TextNodeDumper::VisitOMPRequiresDecl(const OMPRequiresDecl *D) {
1852   for (const auto *C : D->clauselists()) {
1853     AddChild([=] {
1854       if (!C) {
1855         ColorScope Color(OS, ShowColors, NullColor);
1856         OS << "<<<NULL>>> OMPClause";
1857         return;
1858       }
1859       {
1860         ColorScope Color(OS, ShowColors, AttrColor);
1861         StringRef ClauseName(
1862             llvm::omp::getOpenMPClauseName(C->getClauseKind()));
1863         OS << "OMP" << ClauseName.substr(/*Start=*/0, /*N=*/1).upper()
1864            << ClauseName.drop_front() << "Clause";
1865       }
1866       dumpPointer(C);
1867       dumpSourceRange(SourceRange(C->getBeginLoc(), C->getEndLoc()));
1868     });
1869   }
1870 }
1871 
1872 void TextNodeDumper::VisitOMPCapturedExprDecl(const OMPCapturedExprDecl *D) {
1873   dumpName(D);
1874   dumpType(D->getType());
1875 }
1876 
1877 void TextNodeDumper::VisitNamespaceDecl(const NamespaceDecl *D) {
1878   dumpName(D);
1879   if (D->isInline())
1880     OS << " inline";
1881   if (!D->isOriginalNamespace())
1882     dumpDeclRef(D->getOriginalNamespace(), "original");
1883 }
1884 
1885 void TextNodeDumper::VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
1886   OS << ' ';
1887   dumpBareDeclRef(D->getNominatedNamespace());
1888 }
1889 
1890 void TextNodeDumper::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
1891   dumpName(D);
1892   dumpDeclRef(D->getAliasedNamespace());
1893 }
1894 
1895 void TextNodeDumper::VisitTypeAliasDecl(const TypeAliasDecl *D) {
1896   dumpName(D);
1897   dumpType(D->getUnderlyingType());
1898 }
1899 
1900 void TextNodeDumper::VisitTypeAliasTemplateDecl(
1901     const TypeAliasTemplateDecl *D) {
1902   dumpName(D);
1903 }
1904 
1905 void TextNodeDumper::VisitCXXRecordDecl(const CXXRecordDecl *D) {
1906   VisitRecordDecl(D);
1907   if (!D->isCompleteDefinition())
1908     return;
1909 
1910   AddChild([=] {
1911     {
1912       ColorScope Color(OS, ShowColors, DeclKindNameColor);
1913       OS << "DefinitionData";
1914     }
1915 #define FLAG(fn, name)                                                         \
1916   if (D->fn())                                                                 \
1917     OS << " " #name;
1918     FLAG(isParsingBaseSpecifiers, parsing_base_specifiers);
1919 
1920     FLAG(isGenericLambda, generic);
1921     FLAG(isLambda, lambda);
1922 
1923     FLAG(isAnonymousStructOrUnion, is_anonymous);
1924     FLAG(canPassInRegisters, pass_in_registers);
1925     FLAG(isEmpty, empty);
1926     FLAG(isAggregate, aggregate);
1927     FLAG(isStandardLayout, standard_layout);
1928     FLAG(isTriviallyCopyable, trivially_copyable);
1929     FLAG(isPOD, pod);
1930     FLAG(isTrivial, trivial);
1931     FLAG(isPolymorphic, polymorphic);
1932     FLAG(isAbstract, abstract);
1933     FLAG(isLiteral, literal);
1934 
1935     FLAG(hasUserDeclaredConstructor, has_user_declared_ctor);
1936     FLAG(hasConstexprNonCopyMoveConstructor, has_constexpr_non_copy_move_ctor);
1937     FLAG(hasMutableFields, has_mutable_fields);
1938     FLAG(hasVariantMembers, has_variant_members);
1939     FLAG(allowConstDefaultInit, can_const_default_init);
1940 
1941     AddChild([=] {
1942       {
1943         ColorScope Color(OS, ShowColors, DeclKindNameColor);
1944         OS << "DefaultConstructor";
1945       }
1946       FLAG(hasDefaultConstructor, exists);
1947       FLAG(hasTrivialDefaultConstructor, trivial);
1948       FLAG(hasNonTrivialDefaultConstructor, non_trivial);
1949       FLAG(hasUserProvidedDefaultConstructor, user_provided);
1950       FLAG(hasConstexprDefaultConstructor, constexpr);
1951       FLAG(needsImplicitDefaultConstructor, needs_implicit);
1952       FLAG(defaultedDefaultConstructorIsConstexpr, defaulted_is_constexpr);
1953     });
1954 
1955     AddChild([=] {
1956       {
1957         ColorScope Color(OS, ShowColors, DeclKindNameColor);
1958         OS << "CopyConstructor";
1959       }
1960       FLAG(hasSimpleCopyConstructor, simple);
1961       FLAG(hasTrivialCopyConstructor, trivial);
1962       FLAG(hasNonTrivialCopyConstructor, non_trivial);
1963       FLAG(hasUserDeclaredCopyConstructor, user_declared);
1964       FLAG(hasCopyConstructorWithConstParam, has_const_param);
1965       FLAG(needsImplicitCopyConstructor, needs_implicit);
1966       FLAG(needsOverloadResolutionForCopyConstructor,
1967            needs_overload_resolution);
1968       if (!D->needsOverloadResolutionForCopyConstructor())
1969         FLAG(defaultedCopyConstructorIsDeleted, defaulted_is_deleted);
1970       FLAG(implicitCopyConstructorHasConstParam, implicit_has_const_param);
1971     });
1972 
1973     AddChild([=] {
1974       {
1975         ColorScope Color(OS, ShowColors, DeclKindNameColor);
1976         OS << "MoveConstructor";
1977       }
1978       FLAG(hasMoveConstructor, exists);
1979       FLAG(hasSimpleMoveConstructor, simple);
1980       FLAG(hasTrivialMoveConstructor, trivial);
1981       FLAG(hasNonTrivialMoveConstructor, non_trivial);
1982       FLAG(hasUserDeclaredMoveConstructor, user_declared);
1983       FLAG(needsImplicitMoveConstructor, needs_implicit);
1984       FLAG(needsOverloadResolutionForMoveConstructor,
1985            needs_overload_resolution);
1986       if (!D->needsOverloadResolutionForMoveConstructor())
1987         FLAG(defaultedMoveConstructorIsDeleted, defaulted_is_deleted);
1988     });
1989 
1990     AddChild([=] {
1991       {
1992         ColorScope Color(OS, ShowColors, DeclKindNameColor);
1993         OS << "CopyAssignment";
1994       }
1995       FLAG(hasSimpleCopyAssignment, simple);
1996       FLAG(hasTrivialCopyAssignment, trivial);
1997       FLAG(hasNonTrivialCopyAssignment, non_trivial);
1998       FLAG(hasCopyAssignmentWithConstParam, has_const_param);
1999       FLAG(hasUserDeclaredCopyAssignment, user_declared);
2000       FLAG(needsImplicitCopyAssignment, needs_implicit);
2001       FLAG(needsOverloadResolutionForCopyAssignment, needs_overload_resolution);
2002       FLAG(implicitCopyAssignmentHasConstParam, implicit_has_const_param);
2003     });
2004 
2005     AddChild([=] {
2006       {
2007         ColorScope Color(OS, ShowColors, DeclKindNameColor);
2008         OS << "MoveAssignment";
2009       }
2010       FLAG(hasMoveAssignment, exists);
2011       FLAG(hasSimpleMoveAssignment, simple);
2012       FLAG(hasTrivialMoveAssignment, trivial);
2013       FLAG(hasNonTrivialMoveAssignment, non_trivial);
2014       FLAG(hasUserDeclaredMoveAssignment, user_declared);
2015       FLAG(needsImplicitMoveAssignment, needs_implicit);
2016       FLAG(needsOverloadResolutionForMoveAssignment, needs_overload_resolution);
2017     });
2018 
2019     AddChild([=] {
2020       {
2021         ColorScope Color(OS, ShowColors, DeclKindNameColor);
2022         OS << "Destructor";
2023       }
2024       FLAG(hasSimpleDestructor, simple);
2025       FLAG(hasIrrelevantDestructor, irrelevant);
2026       FLAG(hasTrivialDestructor, trivial);
2027       FLAG(hasNonTrivialDestructor, non_trivial);
2028       FLAG(hasUserDeclaredDestructor, user_declared);
2029       FLAG(hasConstexprDestructor, constexpr);
2030       FLAG(needsImplicitDestructor, needs_implicit);
2031       FLAG(needsOverloadResolutionForDestructor, needs_overload_resolution);
2032       if (!D->needsOverloadResolutionForDestructor())
2033         FLAG(defaultedDestructorIsDeleted, defaulted_is_deleted);
2034     });
2035   });
2036 
2037   for (const auto &I : D->bases()) {
2038     AddChild([=] {
2039       if (I.isVirtual())
2040         OS << "virtual ";
2041       dumpAccessSpecifier(I.getAccessSpecifier());
2042       dumpType(I.getType());
2043       if (I.isPackExpansion())
2044         OS << "...";
2045     });
2046   }
2047 }
2048 
2049 void TextNodeDumper::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
2050   dumpName(D);
2051 }
2052 
2053 void TextNodeDumper::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
2054   dumpName(D);
2055 }
2056 
2057 void TextNodeDumper::VisitVarTemplateDecl(const VarTemplateDecl *D) {
2058   dumpName(D);
2059 }
2060 
2061 void TextNodeDumper::VisitBuiltinTemplateDecl(const BuiltinTemplateDecl *D) {
2062   dumpName(D);
2063 }
2064 
2065 void TextNodeDumper::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
2066   if (const auto *TC = D->getTypeConstraint()) {
2067     OS << " ";
2068     dumpBareDeclRef(TC->getNamedConcept());
2069     if (TC->getNamedConcept() != TC->getFoundDecl()) {
2070       OS << " (";
2071       dumpBareDeclRef(TC->getFoundDecl());
2072       OS << ")";
2073     }
2074   } else if (D->wasDeclaredWithTypename())
2075     OS << " typename";
2076   else
2077     OS << " class";
2078   OS << " depth " << D->getDepth() << " index " << D->getIndex();
2079   if (D->isParameterPack())
2080     OS << " ...";
2081   dumpName(D);
2082 }
2083 
2084 void TextNodeDumper::VisitNonTypeTemplateParmDecl(
2085     const NonTypeTemplateParmDecl *D) {
2086   dumpType(D->getType());
2087   OS << " depth " << D->getDepth() << " index " << D->getIndex();
2088   if (D->isParameterPack())
2089     OS << " ...";
2090   dumpName(D);
2091 }
2092 
2093 void TextNodeDumper::VisitTemplateTemplateParmDecl(
2094     const TemplateTemplateParmDecl *D) {
2095   OS << " depth " << D->getDepth() << " index " << D->getIndex();
2096   if (D->isParameterPack())
2097     OS << " ...";
2098   dumpName(D);
2099 }
2100 
2101 void TextNodeDumper::VisitUsingDecl(const UsingDecl *D) {
2102   OS << ' ';
2103   if (D->getQualifier())
2104     D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
2105   OS << D->getDeclName();
2106 }
2107 
2108 void TextNodeDumper::VisitUsingEnumDecl(const UsingEnumDecl *D) {
2109   OS << ' ';
2110   dumpBareDeclRef(D->getEnumDecl());
2111 }
2112 
2113 void TextNodeDumper::VisitUnresolvedUsingTypenameDecl(
2114     const UnresolvedUsingTypenameDecl *D) {
2115   OS << ' ';
2116   if (D->getQualifier())
2117     D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
2118   OS << D->getDeclName();
2119 }
2120 
2121 void TextNodeDumper::VisitUnresolvedUsingValueDecl(
2122     const UnresolvedUsingValueDecl *D) {
2123   OS << ' ';
2124   if (D->getQualifier())
2125     D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
2126   OS << D->getDeclName();
2127   dumpType(D->getType());
2128 }
2129 
2130 void TextNodeDumper::VisitUsingShadowDecl(const UsingShadowDecl *D) {
2131   OS << ' ';
2132   dumpBareDeclRef(D->getTargetDecl());
2133 }
2134 
2135 void TextNodeDumper::VisitConstructorUsingShadowDecl(
2136     const ConstructorUsingShadowDecl *D) {
2137   if (D->constructsVirtualBase())
2138     OS << " virtual";
2139 
2140   AddChild([=] {
2141     OS << "target ";
2142     dumpBareDeclRef(D->getTargetDecl());
2143   });
2144 
2145   AddChild([=] {
2146     OS << "nominated ";
2147     dumpBareDeclRef(D->getNominatedBaseClass());
2148     OS << ' ';
2149     dumpBareDeclRef(D->getNominatedBaseClassShadowDecl());
2150   });
2151 
2152   AddChild([=] {
2153     OS << "constructed ";
2154     dumpBareDeclRef(D->getConstructedBaseClass());
2155     OS << ' ';
2156     dumpBareDeclRef(D->getConstructedBaseClassShadowDecl());
2157   });
2158 }
2159 
2160 void TextNodeDumper::VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
2161   switch (D->getLanguage()) {
2162   case LinkageSpecDecl::lang_c:
2163     OS << " C";
2164     break;
2165   case LinkageSpecDecl::lang_cxx:
2166     OS << " C++";
2167     break;
2168   }
2169 }
2170 
2171 void TextNodeDumper::VisitAccessSpecDecl(const AccessSpecDecl *D) {
2172   OS << ' ';
2173   dumpAccessSpecifier(D->getAccess());
2174 }
2175 
2176 void TextNodeDumper::VisitFriendDecl(const FriendDecl *D) {
2177   if (TypeSourceInfo *T = D->getFriendType())
2178     dumpType(T->getType());
2179 }
2180 
2181 void TextNodeDumper::VisitObjCIvarDecl(const ObjCIvarDecl *D) {
2182   dumpName(D);
2183   dumpType(D->getType());
2184   if (D->getSynthesize())
2185     OS << " synthesize";
2186 
2187   switch (D->getAccessControl()) {
2188   case ObjCIvarDecl::None:
2189     OS << " none";
2190     break;
2191   case ObjCIvarDecl::Private:
2192     OS << " private";
2193     break;
2194   case ObjCIvarDecl::Protected:
2195     OS << " protected";
2196     break;
2197   case ObjCIvarDecl::Public:
2198     OS << " public";
2199     break;
2200   case ObjCIvarDecl::Package:
2201     OS << " package";
2202     break;
2203   }
2204 }
2205 
2206 void TextNodeDumper::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
2207   if (D->isInstanceMethod())
2208     OS << " -";
2209   else
2210     OS << " +";
2211   dumpName(D);
2212   dumpType(D->getReturnType());
2213 
2214   if (D->isVariadic())
2215     OS << " variadic";
2216 }
2217 
2218 void TextNodeDumper::VisitObjCTypeParamDecl(const ObjCTypeParamDecl *D) {
2219   dumpName(D);
2220   switch (D->getVariance()) {
2221   case ObjCTypeParamVariance::Invariant:
2222     break;
2223 
2224   case ObjCTypeParamVariance::Covariant:
2225     OS << " covariant";
2226     break;
2227 
2228   case ObjCTypeParamVariance::Contravariant:
2229     OS << " contravariant";
2230     break;
2231   }
2232 
2233   if (D->hasExplicitBound())
2234     OS << " bounded";
2235   dumpType(D->getUnderlyingType());
2236 }
2237 
2238 void TextNodeDumper::VisitObjCCategoryDecl(const ObjCCategoryDecl *D) {
2239   dumpName(D);
2240   dumpDeclRef(D->getClassInterface());
2241   dumpDeclRef(D->getImplementation());
2242   for (const auto *P : D->protocols())
2243     dumpDeclRef(P);
2244 }
2245 
2246 void TextNodeDumper::VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) {
2247   dumpName(D);
2248   dumpDeclRef(D->getClassInterface());
2249   dumpDeclRef(D->getCategoryDecl());
2250 }
2251 
2252 void TextNodeDumper::VisitObjCProtocolDecl(const ObjCProtocolDecl *D) {
2253   dumpName(D);
2254 
2255   for (const auto *Child : D->protocols())
2256     dumpDeclRef(Child);
2257 }
2258 
2259 void TextNodeDumper::VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) {
2260   dumpName(D);
2261   dumpDeclRef(D->getSuperClass(), "super");
2262 
2263   dumpDeclRef(D->getImplementation());
2264   for (const auto *Child : D->protocols())
2265     dumpDeclRef(Child);
2266 }
2267 
2268 void TextNodeDumper::VisitObjCImplementationDecl(
2269     const ObjCImplementationDecl *D) {
2270   dumpName(D);
2271   dumpDeclRef(D->getSuperClass(), "super");
2272   dumpDeclRef(D->getClassInterface());
2273 }
2274 
2275 void TextNodeDumper::VisitObjCCompatibleAliasDecl(
2276     const ObjCCompatibleAliasDecl *D) {
2277   dumpName(D);
2278   dumpDeclRef(D->getClassInterface());
2279 }
2280 
2281 void TextNodeDumper::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
2282   dumpName(D);
2283   dumpType(D->getType());
2284 
2285   if (D->getPropertyImplementation() == ObjCPropertyDecl::Required)
2286     OS << " required";
2287   else if (D->getPropertyImplementation() == ObjCPropertyDecl::Optional)
2288     OS << " optional";
2289 
2290   ObjCPropertyAttribute::Kind Attrs = D->getPropertyAttributes();
2291   if (Attrs != ObjCPropertyAttribute::kind_noattr) {
2292     if (Attrs & ObjCPropertyAttribute::kind_readonly)
2293       OS << " readonly";
2294     if (Attrs & ObjCPropertyAttribute::kind_assign)
2295       OS << " assign";
2296     if (Attrs & ObjCPropertyAttribute::kind_readwrite)
2297       OS << " readwrite";
2298     if (Attrs & ObjCPropertyAttribute::kind_retain)
2299       OS << " retain";
2300     if (Attrs & ObjCPropertyAttribute::kind_copy)
2301       OS << " copy";
2302     if (Attrs & ObjCPropertyAttribute::kind_nonatomic)
2303       OS << " nonatomic";
2304     if (Attrs & ObjCPropertyAttribute::kind_atomic)
2305       OS << " atomic";
2306     if (Attrs & ObjCPropertyAttribute::kind_weak)
2307       OS << " weak";
2308     if (Attrs & ObjCPropertyAttribute::kind_strong)
2309       OS << " strong";
2310     if (Attrs & ObjCPropertyAttribute::kind_unsafe_unretained)
2311       OS << " unsafe_unretained";
2312     if (Attrs & ObjCPropertyAttribute::kind_class)
2313       OS << " class";
2314     if (Attrs & ObjCPropertyAttribute::kind_direct)
2315       OS << " direct";
2316     if (Attrs & ObjCPropertyAttribute::kind_getter)
2317       dumpDeclRef(D->getGetterMethodDecl(), "getter");
2318     if (Attrs & ObjCPropertyAttribute::kind_setter)
2319       dumpDeclRef(D->getSetterMethodDecl(), "setter");
2320   }
2321 }
2322 
2323 void TextNodeDumper::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
2324   dumpName(D->getPropertyDecl());
2325   if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
2326     OS << " synthesize";
2327   else
2328     OS << " dynamic";
2329   dumpDeclRef(D->getPropertyDecl());
2330   dumpDeclRef(D->getPropertyIvarDecl());
2331 }
2332 
2333 void TextNodeDumper::VisitBlockDecl(const BlockDecl *D) {
2334   if (D->isVariadic())
2335     OS << " variadic";
2336 
2337   if (D->capturesCXXThis())
2338     OS << " captures_this";
2339 }
2340 
2341 void TextNodeDumper::VisitConceptDecl(const ConceptDecl *D) {
2342   dumpName(D);
2343 }
2344