1 //===--- ASTDumper.cpp - Dumping implementation for ASTs ------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the AST dump methods, which dump out the
11 // AST in a form that exposes type details and other fields.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/Attr.h"
17 #include "clang/AST/CommentVisitor.h"
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/DeclLookups.h"
20 #include "clang/AST/DeclObjC.h"
21 #include "clang/AST/DeclOpenMP.h"
22 #include "clang/AST/DeclVisitor.h"
23 #include "clang/AST/LocInfoType.h"
24 #include "clang/AST/StmtVisitor.h"
25 #include "clang/AST/TypeVisitor.h"
26 #include "clang/Basic/Builtins.h"
27 #include "clang/Basic/Module.h"
28 #include "clang/Basic/SourceManager.h"
29 #include "llvm/Support/raw_ostream.h"
30 using namespace clang;
31 using namespace clang::comments;
32 
33 //===----------------------------------------------------------------------===//
34 // ASTDumper Visitor
35 //===----------------------------------------------------------------------===//
36 
37 namespace  {
38   // Colors used for various parts of the AST dump
39   // Do not use bold yellow for any text.  It is hard to read on white screens.
40 
41   struct TerminalColor {
42     raw_ostream::Colors Color;
43     bool Bold;
44   };
45 
46   // Red           - CastColor
47   // Green         - TypeColor
48   // Bold Green    - DeclKindNameColor, UndeserializedColor
49   // Yellow        - AddressColor, LocationColor
50   // Blue          - CommentColor, NullColor, IndentColor
51   // Bold Blue     - AttrColor
52   // Bold Magenta  - StmtColor
53   // Cyan          - ValueKindColor, ObjectKindColor
54   // Bold Cyan     - ValueColor, DeclNameColor
55 
56   // Decl kind names (VarDecl, FunctionDecl, etc)
57   static const TerminalColor DeclKindNameColor = { raw_ostream::GREEN, true };
58   // Attr names (CleanupAttr, GuardedByAttr, etc)
59   static const TerminalColor AttrColor = { raw_ostream::BLUE, true };
60   // Statement names (DeclStmt, ImplicitCastExpr, etc)
61   static const TerminalColor StmtColor = { raw_ostream::MAGENTA, true };
62   // Comment names (FullComment, ParagraphComment, TextComment, etc)
63   static const TerminalColor CommentColor = { raw_ostream::BLUE, false };
64 
65   // Type names (int, float, etc, plus user defined types)
66   static const TerminalColor TypeColor = { raw_ostream::GREEN, false };
67 
68   // Pointer address
69   static const TerminalColor AddressColor = { raw_ostream::YELLOW, false };
70   // Source locations
71   static const TerminalColor LocationColor = { raw_ostream::YELLOW, false };
72 
73   // lvalue/xvalue
74   static const TerminalColor ValueKindColor = { raw_ostream::CYAN, false };
75   // bitfield/objcproperty/objcsubscript/vectorcomponent
76   static const TerminalColor ObjectKindColor = { raw_ostream::CYAN, false };
77 
78   // Null statements
79   static const TerminalColor NullColor = { raw_ostream::BLUE, false };
80 
81   // Undeserialized entities
82   static const TerminalColor UndeserializedColor = { raw_ostream::GREEN, true };
83 
84   // CastKind from CastExpr's
85   static const TerminalColor CastColor = { raw_ostream::RED, false };
86 
87   // Value of the statement
88   static const TerminalColor ValueColor = { raw_ostream::CYAN, true };
89   // Decl names
90   static const TerminalColor DeclNameColor = { raw_ostream::CYAN, true };
91 
92   // Indents ( `, -. | )
93   static const TerminalColor IndentColor = { raw_ostream::BLUE, false };
94 
95   class ASTDumper
96       : public ConstDeclVisitor<ASTDumper>, public ConstStmtVisitor<ASTDumper>,
97         public ConstCommentVisitor<ASTDumper>, public TypeVisitor<ASTDumper> {
98     raw_ostream &OS;
99     const CommandTraits *Traits;
100     const SourceManager *SM;
101 
102     /// The policy to use for printing; can be defaulted.
103     PrintingPolicy PrintPolicy;
104 
105     /// Pending[i] is an action to dump an entity at level i.
106     llvm::SmallVector<std::function<void(bool isLastChild)>, 32> Pending;
107 
108     /// Indicates whether we should trigger deserialization of nodes that had
109     /// not already been loaded.
110     bool Deserialize = false;
111 
112     /// Indicates whether we're at the top level.
113     bool TopLevel = true;
114 
115     /// Indicates if we're handling the first child after entering a new depth.
116     bool FirstChild = true;
117 
118     /// Prefix for currently-being-dumped entity.
119     std::string Prefix;
120 
121     /// Keep track of the last location we print out so that we can
122     /// print out deltas from then on out.
123     const char *LastLocFilename = "";
124     unsigned LastLocLine = ~0U;
125 
126     /// The \c FullComment parent of the comment being dumped.
127     const FullComment *FC = nullptr;
128 
129     bool ShowColors;
130 
131     /// Dump a child of the current node.
132     template<typename Fn> void dumpChild(Fn doDumpChild) {
133       // If we're at the top level, there's nothing interesting to do; just
134       // run the dumper.
135       if (TopLevel) {
136         TopLevel = false;
137         doDumpChild();
138         while (!Pending.empty()) {
139           Pending.back()(true);
140           Pending.pop_back();
141         }
142         Prefix.clear();
143         OS << "\n";
144         TopLevel = true;
145         return;
146       }
147 
148       const FullComment *OrigFC = FC;
149       auto dumpWithIndent = [this, doDumpChild, OrigFC](bool isLastChild) {
150         // Print out the appropriate tree structure and work out the prefix for
151         // children of this node. For instance:
152         //
153         //   A        Prefix = ""
154         //   |-B      Prefix = "| "
155         //   | `-C    Prefix = "|   "
156         //   `-D      Prefix = "  "
157         //     |-E    Prefix = "  | "
158         //     `-F    Prefix = "    "
159         //   G        Prefix = ""
160         //
161         // Note that the first level gets no prefix.
162         {
163           OS << '\n';
164           ColorScope Color(*this, IndentColor);
165           OS << Prefix << (isLastChild ? '`' : '|') << '-';
166           this->Prefix.push_back(isLastChild ? ' ' : '|');
167           this->Prefix.push_back(' ');
168         }
169 
170         FirstChild = true;
171         unsigned Depth = Pending.size();
172 
173         FC = OrigFC;
174         doDumpChild();
175 
176         // If any children are left, they're the last at their nesting level.
177         // Dump those ones out now.
178         while (Depth < Pending.size()) {
179           Pending.back()(true);
180           this->Pending.pop_back();
181         }
182 
183         // Restore the old prefix.
184         this->Prefix.resize(Prefix.size() - 2);
185       };
186 
187       if (FirstChild) {
188         Pending.push_back(std::move(dumpWithIndent));
189       } else {
190         Pending.back()(false);
191         Pending.back() = std::move(dumpWithIndent);
192       }
193       FirstChild = false;
194     }
195 
196     class ColorScope {
197       ASTDumper &Dumper;
198     public:
199       ColorScope(ASTDumper &Dumper, TerminalColor Color)
200         : Dumper(Dumper) {
201         if (Dumper.ShowColors)
202           Dumper.OS.changeColor(Color.Color, Color.Bold);
203       }
204       ~ColorScope() {
205         if (Dumper.ShowColors)
206           Dumper.OS.resetColor();
207       }
208     };
209 
210   public:
211     ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
212               const SourceManager *SM)
213         : ASTDumper(OS, Traits, SM,
214                     SM && SM->getDiagnostics().getShowColors()) {}
215 
216     ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
217               const SourceManager *SM, bool ShowColors)
218         : ASTDumper(OS, Traits, SM, ShowColors, LangOptions()) {}
219     ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
220               const SourceManager *SM, bool ShowColors,
221               const PrintingPolicy &PrintPolicy)
222         : OS(OS), Traits(Traits), SM(SM), PrintPolicy(PrintPolicy),
223           ShowColors(ShowColors) {}
224 
225     void setDeserialize(bool D) { Deserialize = D; }
226 
227     void dumpDecl(const Decl *D);
228     void dumpStmt(const Stmt *S);
229     void dumpFullComment(const FullComment *C);
230 
231     // Utilities
232     void dumpPointer(const void *Ptr);
233     void dumpSourceRange(SourceRange R);
234     void dumpLocation(SourceLocation Loc);
235     void dumpBareType(QualType T, bool Desugar = true);
236     void dumpType(QualType T);
237     void dumpTypeAsChild(QualType T);
238     void dumpTypeAsChild(const Type *T);
239     void dumpBareDeclRef(const Decl *Node);
240     void dumpDeclRef(const Decl *Node, const char *Label = nullptr);
241     void dumpName(const NamedDecl *D);
242     bool hasNodes(const DeclContext *DC);
243     void dumpDeclContext(const DeclContext *DC);
244     void dumpLookups(const DeclContext *DC, bool DumpDecls);
245     void dumpAttr(const Attr *A);
246 
247     // C++ Utilities
248     void dumpAccessSpecifier(AccessSpecifier AS);
249     void dumpCXXCtorInitializer(const CXXCtorInitializer *Init);
250     void dumpTemplateParameters(const TemplateParameterList *TPL);
251     void dumpTemplateArgumentListInfo(const TemplateArgumentListInfo &TALI);
252     void dumpTemplateArgumentLoc(const TemplateArgumentLoc &A);
253     void dumpTemplateArgumentList(const TemplateArgumentList &TAL);
254     void dumpTemplateArgument(const TemplateArgument &A,
255                               SourceRange R = SourceRange());
256 
257     // Objective-C utilities.
258     void dumpObjCTypeParamList(const ObjCTypeParamList *typeParams);
259 
260     // Types
261     void VisitComplexType(const ComplexType *T) {
262       dumpTypeAsChild(T->getElementType());
263     }
264     void VisitPointerType(const PointerType *T) {
265       dumpTypeAsChild(T->getPointeeType());
266     }
267     void VisitBlockPointerType(const BlockPointerType *T) {
268       dumpTypeAsChild(T->getPointeeType());
269     }
270     void VisitReferenceType(const ReferenceType *T) {
271       dumpTypeAsChild(T->getPointeeType());
272     }
273     void VisitRValueReferenceType(const ReferenceType *T) {
274       if (T->isSpelledAsLValue())
275         OS << " written as lvalue reference";
276       VisitReferenceType(T);
277     }
278     void VisitMemberPointerType(const MemberPointerType *T) {
279       dumpTypeAsChild(T->getClass());
280       dumpTypeAsChild(T->getPointeeType());
281     }
282     void VisitArrayType(const ArrayType *T) {
283       switch (T->getSizeModifier()) {
284         case ArrayType::Normal: break;
285         case ArrayType::Static: OS << " static"; break;
286         case ArrayType::Star: OS << " *"; break;
287       }
288       OS << " " << T->getIndexTypeQualifiers().getAsString();
289       dumpTypeAsChild(T->getElementType());
290     }
291     void VisitConstantArrayType(const ConstantArrayType *T) {
292       OS << " " << T->getSize();
293       VisitArrayType(T);
294     }
295     void VisitVariableArrayType(const VariableArrayType *T) {
296       OS << " ";
297       dumpSourceRange(T->getBracketsRange());
298       VisitArrayType(T);
299       dumpStmt(T->getSizeExpr());
300     }
301     void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
302       VisitArrayType(T);
303       OS << " ";
304       dumpSourceRange(T->getBracketsRange());
305       dumpStmt(T->getSizeExpr());
306     }
307     void VisitDependentSizedExtVectorType(
308         const DependentSizedExtVectorType *T) {
309       OS << " ";
310       dumpLocation(T->getAttributeLoc());
311       dumpTypeAsChild(T->getElementType());
312       dumpStmt(T->getSizeExpr());
313     }
314     void VisitVectorType(const VectorType *T) {
315       switch (T->getVectorKind()) {
316         case VectorType::GenericVector: break;
317         case VectorType::AltiVecVector: OS << " altivec"; break;
318         case VectorType::AltiVecPixel: OS << " altivec pixel"; break;
319         case VectorType::AltiVecBool: OS << " altivec bool"; break;
320         case VectorType::NeonVector: OS << " neon"; break;
321         case VectorType::NeonPolyVector: OS << " neon poly"; break;
322       }
323       OS << " " << T->getNumElements();
324       dumpTypeAsChild(T->getElementType());
325     }
326     void VisitFunctionType(const FunctionType *T) {
327       auto EI = T->getExtInfo();
328       if (EI.getNoReturn()) OS << " noreturn";
329       if (EI.getProducesResult()) OS << " produces_result";
330       if (EI.getHasRegParm()) OS << " regparm " << EI.getRegParm();
331       OS << " " << FunctionType::getNameForCallConv(EI.getCC());
332       dumpTypeAsChild(T->getReturnType());
333     }
334     void VisitFunctionProtoType(const FunctionProtoType *T) {
335       auto EPI = T->getExtProtoInfo();
336       if (EPI.HasTrailingReturn) OS << " trailing_return";
337       if (T->isConst()) OS << " const";
338       if (T->isVolatile()) OS << " volatile";
339       if (T->isRestrict()) OS << " restrict";
340       switch (EPI.RefQualifier) {
341         case RQ_None: break;
342         case RQ_LValue: OS << " &"; break;
343         case RQ_RValue: OS << " &&"; break;
344       }
345       // FIXME: Exception specification.
346       // FIXME: Consumed parameters.
347       VisitFunctionType(T);
348       for (QualType PT : T->getParamTypes())
349         dumpTypeAsChild(PT);
350       if (EPI.Variadic)
351         dumpChild([=] { OS << "..."; });
352     }
353     void VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
354       dumpDeclRef(T->getDecl());
355     }
356     void VisitTypedefType(const TypedefType *T) {
357       dumpDeclRef(T->getDecl());
358     }
359     void VisitTypeOfExprType(const TypeOfExprType *T) {
360       dumpStmt(T->getUnderlyingExpr());
361     }
362     void VisitDecltypeType(const DecltypeType *T) {
363       dumpStmt(T->getUnderlyingExpr());
364     }
365     void VisitUnaryTransformType(const UnaryTransformType *T) {
366       switch (T->getUTTKind()) {
367       case UnaryTransformType::EnumUnderlyingType:
368         OS << " underlying_type";
369         break;
370       }
371       dumpTypeAsChild(T->getBaseType());
372     }
373     void VisitTagType(const TagType *T) {
374       dumpDeclRef(T->getDecl());
375     }
376     void VisitAttributedType(const AttributedType *T) {
377       // FIXME: AttrKind
378       dumpTypeAsChild(T->getModifiedType());
379     }
380     void VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
381       OS << " depth " << T->getDepth() << " index " << T->getIndex();
382       if (T->isParameterPack()) OS << " pack";
383       dumpDeclRef(T->getDecl());
384     }
385     void VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) {
386       dumpTypeAsChild(T->getReplacedParameter());
387     }
388     void VisitSubstTemplateTypeParmPackType(
389         const SubstTemplateTypeParmPackType *T) {
390       dumpTypeAsChild(T->getReplacedParameter());
391       dumpTemplateArgument(T->getArgumentPack());
392     }
393     void VisitAutoType(const AutoType *T) {
394       if (T->isDecltypeAuto()) OS << " decltype(auto)";
395       if (!T->isDeduced())
396         OS << " undeduced";
397     }
398     void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
399       if (T->isTypeAlias()) OS << " alias";
400       OS << " "; T->getTemplateName().dump(OS);
401       for (auto &Arg : *T)
402         dumpTemplateArgument(Arg);
403       if (T->isTypeAlias())
404         dumpTypeAsChild(T->getAliasedType());
405     }
406     void VisitInjectedClassNameType(const InjectedClassNameType *T) {
407       dumpDeclRef(T->getDecl());
408     }
409     void VisitObjCInterfaceType(const ObjCInterfaceType *T) {
410       dumpDeclRef(T->getDecl());
411     }
412     void VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
413       dumpTypeAsChild(T->getPointeeType());
414     }
415     void VisitAtomicType(const AtomicType *T) {
416       dumpTypeAsChild(T->getValueType());
417     }
418     void VisitPipeType(const PipeType *T) {
419       dumpTypeAsChild(T->getElementType());
420     }
421     void VisitAdjustedType(const AdjustedType *T) {
422       dumpTypeAsChild(T->getOriginalType());
423     }
424     void VisitPackExpansionType(const PackExpansionType *T) {
425       if (auto N = T->getNumExpansions()) OS << " expansions " << *N;
426       if (!T->isSugared())
427         dumpTypeAsChild(T->getPattern());
428     }
429     // FIXME: ElaboratedType, DependentNameType,
430     // DependentTemplateSpecializationType, ObjCObjectType
431 
432     // Decls
433     void VisitLabelDecl(const LabelDecl *D);
434     void VisitTypedefDecl(const TypedefDecl *D);
435     void VisitEnumDecl(const EnumDecl *D);
436     void VisitRecordDecl(const RecordDecl *D);
437     void VisitEnumConstantDecl(const EnumConstantDecl *D);
438     void VisitIndirectFieldDecl(const IndirectFieldDecl *D);
439     void VisitFunctionDecl(const FunctionDecl *D);
440     void VisitFieldDecl(const FieldDecl *D);
441     void VisitVarDecl(const VarDecl *D);
442     void VisitDecompositionDecl(const DecompositionDecl *D);
443     void VisitBindingDecl(const BindingDecl *D);
444     void VisitFileScopeAsmDecl(const FileScopeAsmDecl *D);
445     void VisitImportDecl(const ImportDecl *D);
446     void VisitPragmaCommentDecl(const PragmaCommentDecl *D);
447     void VisitPragmaDetectMismatchDecl(const PragmaDetectMismatchDecl *D);
448     void VisitCapturedDecl(const CapturedDecl *D);
449 
450     // OpenMP decls
451     void VisitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D);
452     void VisitOMPDeclareReductionDecl(const OMPDeclareReductionDecl *D);
453     void VisitOMPCapturedExprDecl(const OMPCapturedExprDecl *D);
454 
455     // C++ Decls
456     void VisitNamespaceDecl(const NamespaceDecl *D);
457     void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D);
458     void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D);
459     void VisitTypeAliasDecl(const TypeAliasDecl *D);
460     void VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D);
461     void VisitCXXRecordDecl(const CXXRecordDecl *D);
462     void VisitStaticAssertDecl(const StaticAssertDecl *D);
463     template<typename SpecializationDecl>
464     void VisitTemplateDeclSpecialization(const SpecializationDecl *D,
465                                          bool DumpExplicitInst,
466                                          bool DumpRefOnly);
467     template<typename TemplateDecl>
468     void VisitTemplateDecl(const TemplateDecl *D, bool DumpExplicitInst);
469     void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D);
470     void VisitClassTemplateDecl(const ClassTemplateDecl *D);
471     void VisitClassTemplateSpecializationDecl(
472         const ClassTemplateSpecializationDecl *D);
473     void VisitClassTemplatePartialSpecializationDecl(
474         const ClassTemplatePartialSpecializationDecl *D);
475     void VisitClassScopeFunctionSpecializationDecl(
476         const ClassScopeFunctionSpecializationDecl *D);
477     void VisitBuiltinTemplateDecl(const BuiltinTemplateDecl *D);
478     void VisitVarTemplateDecl(const VarTemplateDecl *D);
479     void VisitVarTemplateSpecializationDecl(
480         const VarTemplateSpecializationDecl *D);
481     void VisitVarTemplatePartialSpecializationDecl(
482         const VarTemplatePartialSpecializationDecl *D);
483     void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D);
484     void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D);
485     void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D);
486     void VisitUsingDecl(const UsingDecl *D);
487     void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D);
488     void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D);
489     void VisitUsingShadowDecl(const UsingShadowDecl *D);
490     void VisitConstructorUsingShadowDecl(const ConstructorUsingShadowDecl *D);
491     void VisitLinkageSpecDecl(const LinkageSpecDecl *D);
492     void VisitAccessSpecDecl(const AccessSpecDecl *D);
493     void VisitFriendDecl(const FriendDecl *D);
494 
495     // ObjC Decls
496     void VisitObjCIvarDecl(const ObjCIvarDecl *D);
497     void VisitObjCMethodDecl(const ObjCMethodDecl *D);
498     void VisitObjCTypeParamDecl(const ObjCTypeParamDecl *D);
499     void VisitObjCCategoryDecl(const ObjCCategoryDecl *D);
500     void VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D);
501     void VisitObjCProtocolDecl(const ObjCProtocolDecl *D);
502     void VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D);
503     void VisitObjCImplementationDecl(const ObjCImplementationDecl *D);
504     void VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D);
505     void VisitObjCPropertyDecl(const ObjCPropertyDecl *D);
506     void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D);
507     void VisitBlockDecl(const BlockDecl *D);
508 
509     // Stmts.
510     void VisitStmt(const Stmt *Node);
511     void VisitDeclStmt(const DeclStmt *Node);
512     void VisitAttributedStmt(const AttributedStmt *Node);
513     void VisitLabelStmt(const LabelStmt *Node);
514     void VisitGotoStmt(const GotoStmt *Node);
515     void VisitCXXCatchStmt(const CXXCatchStmt *Node);
516     void VisitCapturedStmt(const CapturedStmt *Node);
517 
518     // OpenMP
519     void VisitOMPExecutableDirective(const OMPExecutableDirective *Node);
520 
521     // Exprs
522     void VisitExpr(const Expr *Node);
523     void VisitCastExpr(const CastExpr *Node);
524     void VisitDeclRefExpr(const DeclRefExpr *Node);
525     void VisitPredefinedExpr(const PredefinedExpr *Node);
526     void VisitCharacterLiteral(const CharacterLiteral *Node);
527     void VisitIntegerLiteral(const IntegerLiteral *Node);
528     void VisitFixedPointLiteral(const FixedPointLiteral *Node);
529     void VisitFloatingLiteral(const FloatingLiteral *Node);
530     void VisitStringLiteral(const StringLiteral *Str);
531     void VisitInitListExpr(const InitListExpr *ILE);
532     void VisitArrayInitLoopExpr(const ArrayInitLoopExpr *ILE);
533     void VisitArrayInitIndexExpr(const ArrayInitIndexExpr *ILE);
534     void VisitUnaryOperator(const UnaryOperator *Node);
535     void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Node);
536     void VisitMemberExpr(const MemberExpr *Node);
537     void VisitExtVectorElementExpr(const ExtVectorElementExpr *Node);
538     void VisitBinaryOperator(const BinaryOperator *Node);
539     void VisitCompoundAssignOperator(const CompoundAssignOperator *Node);
540     void VisitAddrLabelExpr(const AddrLabelExpr *Node);
541     void VisitBlockExpr(const BlockExpr *Node);
542     void VisitOpaqueValueExpr(const OpaqueValueExpr *Node);
543     void VisitGenericSelectionExpr(const GenericSelectionExpr *E);
544 
545     // C++
546     void VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node);
547     void VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node);
548     void VisitCXXThisExpr(const CXXThisExpr *Node);
549     void VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node);
550     void VisitCXXUnresolvedConstructExpr(const CXXUnresolvedConstructExpr *Node);
551     void VisitCXXConstructExpr(const CXXConstructExpr *Node);
552     void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node);
553     void VisitCXXNewExpr(const CXXNewExpr *Node);
554     void VisitCXXDeleteExpr(const CXXDeleteExpr *Node);
555     void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node);
556     void VisitExprWithCleanups(const ExprWithCleanups *Node);
557     void VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node);
558     void dumpCXXTemporary(const CXXTemporary *Temporary);
559     void VisitLambdaExpr(const LambdaExpr *Node) {
560       VisitExpr(Node);
561       dumpDecl(Node->getLambdaClass());
562     }
563     void VisitSizeOfPackExpr(const SizeOfPackExpr *Node);
564     void
565     VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *Node);
566 
567     // ObjC
568     void VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node);
569     void VisitObjCEncodeExpr(const ObjCEncodeExpr *Node);
570     void VisitObjCMessageExpr(const ObjCMessageExpr *Node);
571     void VisitObjCBoxedExpr(const ObjCBoxedExpr *Node);
572     void VisitObjCSelectorExpr(const ObjCSelectorExpr *Node);
573     void VisitObjCProtocolExpr(const ObjCProtocolExpr *Node);
574     void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node);
575     void VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node);
576     void VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node);
577     void VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node);
578 
579     // Comments.
580     const char *getCommandName(unsigned CommandID);
581     void dumpComment(const Comment *C);
582 
583     // Inline comments.
584     void visitTextComment(const TextComment *C);
585     void visitInlineCommandComment(const InlineCommandComment *C);
586     void visitHTMLStartTagComment(const HTMLStartTagComment *C);
587     void visitHTMLEndTagComment(const HTMLEndTagComment *C);
588 
589     // Block comments.
590     void visitBlockCommandComment(const BlockCommandComment *C);
591     void visitParamCommandComment(const ParamCommandComment *C);
592     void visitTParamCommandComment(const TParamCommandComment *C);
593     void visitVerbatimBlockComment(const VerbatimBlockComment *C);
594     void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C);
595     void visitVerbatimLineComment(const VerbatimLineComment *C);
596   };
597 }
598 
599 //===----------------------------------------------------------------------===//
600 //  Utilities
601 //===----------------------------------------------------------------------===//
602 
603 void ASTDumper::dumpPointer(const void *Ptr) {
604   ColorScope Color(*this, AddressColor);
605   OS << ' ' << Ptr;
606 }
607 
608 void ASTDumper::dumpLocation(SourceLocation Loc) {
609   if (!SM)
610     return;
611 
612   ColorScope Color(*this, LocationColor);
613   SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
614 
615   // The general format we print out is filename:line:col, but we drop pieces
616   // that haven't changed since the last loc printed.
617   PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
618 
619   if (PLoc.isInvalid()) {
620     OS << "<invalid sloc>";
621     return;
622   }
623 
624   if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
625     OS << PLoc.getFilename() << ':' << PLoc.getLine()
626        << ':' << PLoc.getColumn();
627     LastLocFilename = PLoc.getFilename();
628     LastLocLine = PLoc.getLine();
629   } else if (PLoc.getLine() != LastLocLine) {
630     OS << "line" << ':' << PLoc.getLine()
631        << ':' << PLoc.getColumn();
632     LastLocLine = PLoc.getLine();
633   } else {
634     OS << "col" << ':' << PLoc.getColumn();
635   }
636 }
637 
638 void ASTDumper::dumpSourceRange(SourceRange R) {
639   // Can't translate locations if a SourceManager isn't available.
640   if (!SM)
641     return;
642 
643   OS << " <";
644   dumpLocation(R.getBegin());
645   if (R.getBegin() != R.getEnd()) {
646     OS << ", ";
647     dumpLocation(R.getEnd());
648   }
649   OS << ">";
650 
651   // <t2.c:123:421[blah], t2.c:412:321>
652 
653 }
654 
655 void ASTDumper::dumpBareType(QualType T, bool Desugar) {
656   ColorScope Color(*this, TypeColor);
657 
658   SplitQualType T_split = T.split();
659   OS << "'" << QualType::getAsString(T_split, PrintPolicy) << "'";
660 
661   if (Desugar && !T.isNull()) {
662     // If the type is sugared, also dump a (shallow) desugared type.
663     SplitQualType D_split = T.getSplitDesugaredType();
664     if (T_split != D_split)
665       OS << ":'" << QualType::getAsString(D_split, PrintPolicy) << "'";
666   }
667 }
668 
669 void ASTDumper::dumpType(QualType T) {
670   OS << ' ';
671   dumpBareType(T);
672 }
673 
674 void ASTDumper::dumpTypeAsChild(QualType T) {
675   SplitQualType SQT = T.split();
676   if (!SQT.Quals.hasQualifiers())
677     return dumpTypeAsChild(SQT.Ty);
678 
679   dumpChild([=] {
680     OS << "QualType";
681     dumpPointer(T.getAsOpaquePtr());
682     OS << " ";
683     dumpBareType(T, false);
684     OS << " " << T.split().Quals.getAsString();
685     dumpTypeAsChild(T.split().Ty);
686   });
687 }
688 
689 void ASTDumper::dumpTypeAsChild(const Type *T) {
690   dumpChild([=] {
691     if (!T) {
692       ColorScope Color(*this, NullColor);
693       OS << "<<<NULL>>>";
694       return;
695     }
696     if (const LocInfoType *LIT = llvm::dyn_cast<LocInfoType>(T)) {
697       {
698         ColorScope Color(*this, TypeColor);
699         OS << "LocInfo Type";
700       }
701       dumpPointer(T);
702       dumpTypeAsChild(LIT->getTypeSourceInfo()->getType());
703       return;
704     }
705 
706     {
707       ColorScope Color(*this, TypeColor);
708       OS << T->getTypeClassName() << "Type";
709     }
710     dumpPointer(T);
711     OS << " ";
712     dumpBareType(QualType(T, 0), false);
713 
714     QualType SingleStepDesugar =
715         T->getLocallyUnqualifiedSingleStepDesugaredType();
716     if (SingleStepDesugar != QualType(T, 0))
717       OS << " sugar";
718     if (T->isDependentType())
719       OS << " dependent";
720     else if (T->isInstantiationDependentType())
721       OS << " instantiation_dependent";
722     if (T->isVariablyModifiedType())
723       OS << " variably_modified";
724     if (T->containsUnexpandedParameterPack())
725       OS << " contains_unexpanded_pack";
726     if (T->isFromAST())
727       OS << " imported";
728 
729     TypeVisitor<ASTDumper>::Visit(T);
730 
731     if (SingleStepDesugar != QualType(T, 0))
732       dumpTypeAsChild(SingleStepDesugar);
733   });
734 }
735 
736 void ASTDumper::dumpBareDeclRef(const Decl *D) {
737   if (!D) {
738     ColorScope Color(*this, NullColor);
739     OS << "<<<NULL>>>";
740     return;
741   }
742 
743   {
744     ColorScope Color(*this, DeclKindNameColor);
745     OS << D->getDeclKindName();
746   }
747   dumpPointer(D);
748 
749   if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
750     ColorScope Color(*this, DeclNameColor);
751     OS << " '" << ND->getDeclName() << '\'';
752   }
753 
754   if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
755     dumpType(VD->getType());
756 }
757 
758 void ASTDumper::dumpDeclRef(const Decl *D, const char *Label) {
759   if (!D)
760     return;
761 
762   dumpChild([=]{
763     if (Label)
764       OS << Label << ' ';
765     dumpBareDeclRef(D);
766   });
767 }
768 
769 void ASTDumper::dumpName(const NamedDecl *ND) {
770   if (ND->getDeclName()) {
771     ColorScope Color(*this, DeclNameColor);
772     OS << ' ' << ND->getNameAsString();
773   }
774 }
775 
776 bool ASTDumper::hasNodes(const DeclContext *DC) {
777   if (!DC)
778     return false;
779 
780   return DC->hasExternalLexicalStorage() ||
781          (Deserialize ? DC->decls_begin() != DC->decls_end()
782                       : DC->noload_decls_begin() != DC->noload_decls_end());
783 }
784 
785 void ASTDumper::dumpDeclContext(const DeclContext *DC) {
786   if (!DC)
787     return;
788 
789   for (auto *D : (Deserialize ? DC->decls() : DC->noload_decls()))
790     dumpDecl(D);
791 
792   if (DC->hasExternalLexicalStorage()) {
793     dumpChild([=]{
794       ColorScope Color(*this, UndeserializedColor);
795       OS << "<undeserialized declarations>";
796     });
797   }
798 }
799 
800 void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) {
801   dumpChild([=] {
802     OS << "StoredDeclsMap ";
803     dumpBareDeclRef(cast<Decl>(DC));
804 
805     const DeclContext *Primary = DC->getPrimaryContext();
806     if (Primary != DC) {
807       OS << " primary";
808       dumpPointer(cast<Decl>(Primary));
809     }
810 
811     bool HasUndeserializedLookups = Primary->hasExternalVisibleStorage();
812 
813     auto Range = Deserialize
814                      ? Primary->lookups()
815                      : Primary->noload_lookups(/*PreserveInternalState=*/true);
816     for (auto I = Range.begin(), E = Range.end(); I != E; ++I) {
817       DeclarationName Name = I.getLookupName();
818       DeclContextLookupResult R = *I;
819 
820       dumpChild([=] {
821         OS << "DeclarationName ";
822         {
823           ColorScope Color(*this, DeclNameColor);
824           OS << '\'' << Name << '\'';
825         }
826 
827         for (DeclContextLookupResult::iterator RI = R.begin(), RE = R.end();
828              RI != RE; ++RI) {
829           dumpChild([=] {
830             dumpBareDeclRef(*RI);
831 
832             if ((*RI)->isHidden())
833               OS << " hidden";
834 
835             // If requested, dump the redecl chain for this lookup.
836             if (DumpDecls) {
837               // Dump earliest decl first.
838               std::function<void(Decl *)> DumpWithPrev = [&](Decl *D) {
839                 if (Decl *Prev = D->getPreviousDecl())
840                   DumpWithPrev(Prev);
841                 dumpDecl(D);
842               };
843               DumpWithPrev(*RI);
844             }
845           });
846         }
847       });
848     }
849 
850     if (HasUndeserializedLookups) {
851       dumpChild([=] {
852         ColorScope Color(*this, UndeserializedColor);
853         OS << "<undeserialized lookups>";
854       });
855     }
856   });
857 }
858 
859 void ASTDumper::dumpAttr(const Attr *A) {
860   dumpChild([=] {
861     {
862       ColorScope Color(*this, AttrColor);
863 
864       switch (A->getKind()) {
865 #define ATTR(X) case attr::X: OS << #X; break;
866 #include "clang/Basic/AttrList.inc"
867       }
868       OS << "Attr";
869     }
870     dumpPointer(A);
871     dumpSourceRange(A->getRange());
872     if (A->isInherited())
873       OS << " Inherited";
874     if (A->isImplicit())
875       OS << " Implicit";
876 #include "clang/AST/AttrDump.inc"
877   });
878 }
879 
880 static void dumpPreviousDeclImpl(raw_ostream &OS, ...) {}
881 
882 template<typename T>
883 static void dumpPreviousDeclImpl(raw_ostream &OS, const Mergeable<T> *D) {
884   const T *First = D->getFirstDecl();
885   if (First != D)
886     OS << " first " << First;
887 }
888 
889 template<typename T>
890 static void dumpPreviousDeclImpl(raw_ostream &OS, const Redeclarable<T> *D) {
891   const T *Prev = D->getPreviousDecl();
892   if (Prev)
893     OS << " prev " << Prev;
894 }
895 
896 /// Dump the previous declaration in the redeclaration chain for a declaration,
897 /// if any.
898 static void dumpPreviousDecl(raw_ostream &OS, const Decl *D) {
899   switch (D->getKind()) {
900 #define DECL(DERIVED, BASE) \
901   case Decl::DERIVED: \
902     return dumpPreviousDeclImpl(OS, cast<DERIVED##Decl>(D));
903 #define ABSTRACT_DECL(DECL)
904 #include "clang/AST/DeclNodes.inc"
905   }
906   llvm_unreachable("Decl that isn't part of DeclNodes.inc!");
907 }
908 
909 //===----------------------------------------------------------------------===//
910 //  C++ Utilities
911 //===----------------------------------------------------------------------===//
912 
913 void ASTDumper::dumpAccessSpecifier(AccessSpecifier AS) {
914   switch (AS) {
915   case AS_none:
916     break;
917   case AS_public:
918     OS << "public";
919     break;
920   case AS_protected:
921     OS << "protected";
922     break;
923   case AS_private:
924     OS << "private";
925     break;
926   }
927 }
928 
929 void ASTDumper::dumpCXXCtorInitializer(const CXXCtorInitializer *Init) {
930   dumpChild([=] {
931     OS << "CXXCtorInitializer";
932     if (Init->isAnyMemberInitializer()) {
933       OS << ' ';
934       dumpBareDeclRef(Init->getAnyMember());
935     } else if (Init->isBaseInitializer()) {
936       dumpType(QualType(Init->getBaseClass(), 0));
937     } else if (Init->isDelegatingInitializer()) {
938       dumpType(Init->getTypeSourceInfo()->getType());
939     } else {
940       llvm_unreachable("Unknown initializer type");
941     }
942     dumpStmt(Init->getInit());
943   });
944 }
945 
946 void ASTDumper::dumpTemplateParameters(const TemplateParameterList *TPL) {
947   if (!TPL)
948     return;
949 
950   for (TemplateParameterList::const_iterator I = TPL->begin(), E = TPL->end();
951        I != E; ++I)
952     dumpDecl(*I);
953 }
954 
955 void ASTDumper::dumpTemplateArgumentListInfo(
956     const TemplateArgumentListInfo &TALI) {
957   for (unsigned i = 0, e = TALI.size(); i < e; ++i)
958     dumpTemplateArgumentLoc(TALI[i]);
959 }
960 
961 void ASTDumper::dumpTemplateArgumentLoc(const TemplateArgumentLoc &A) {
962   dumpTemplateArgument(A.getArgument(), A.getSourceRange());
963 }
964 
965 void ASTDumper::dumpTemplateArgumentList(const TemplateArgumentList &TAL) {
966   for (unsigned i = 0, e = TAL.size(); i < e; ++i)
967     dumpTemplateArgument(TAL[i]);
968 }
969 
970 void ASTDumper::dumpTemplateArgument(const TemplateArgument &A, SourceRange R) {
971   dumpChild([=] {
972     OS << "TemplateArgument";
973     if (R.isValid())
974       dumpSourceRange(R);
975 
976     switch (A.getKind()) {
977     case TemplateArgument::Null:
978       OS << " null";
979       break;
980     case TemplateArgument::Type:
981       OS << " type";
982       dumpType(A.getAsType());
983       break;
984     case TemplateArgument::Declaration:
985       OS << " decl";
986       dumpDeclRef(A.getAsDecl());
987       break;
988     case TemplateArgument::NullPtr:
989       OS << " nullptr";
990       break;
991     case TemplateArgument::Integral:
992       OS << " integral " << A.getAsIntegral();
993       break;
994     case TemplateArgument::Template:
995       OS << " template ";
996       A.getAsTemplate().dump(OS);
997       break;
998     case TemplateArgument::TemplateExpansion:
999       OS << " template expansion";
1000       A.getAsTemplateOrTemplatePattern().dump(OS);
1001       break;
1002     case TemplateArgument::Expression:
1003       OS << " expr";
1004       dumpStmt(A.getAsExpr());
1005       break;
1006     case TemplateArgument::Pack:
1007       OS << " pack";
1008       for (TemplateArgument::pack_iterator I = A.pack_begin(), E = A.pack_end();
1009            I != E; ++I)
1010         dumpTemplateArgument(*I);
1011       break;
1012     }
1013   });
1014 }
1015 
1016 //===----------------------------------------------------------------------===//
1017 //  Objective-C Utilities
1018 //===----------------------------------------------------------------------===//
1019 void ASTDumper::dumpObjCTypeParamList(const ObjCTypeParamList *typeParams) {
1020   if (!typeParams)
1021     return;
1022 
1023   for (auto typeParam : *typeParams) {
1024     dumpDecl(typeParam);
1025   }
1026 }
1027 
1028 //===----------------------------------------------------------------------===//
1029 //  Decl dumping methods.
1030 //===----------------------------------------------------------------------===//
1031 
1032 void ASTDumper::dumpDecl(const Decl *D) {
1033   dumpChild([=] {
1034     if (!D) {
1035       ColorScope Color(*this, NullColor);
1036       OS << "<<<NULL>>>";
1037       return;
1038     }
1039 
1040     {
1041       ColorScope Color(*this, DeclKindNameColor);
1042       OS << D->getDeclKindName() << "Decl";
1043     }
1044     dumpPointer(D);
1045     if (D->getLexicalDeclContext() != D->getDeclContext())
1046       OS << " parent " << cast<Decl>(D->getDeclContext());
1047     dumpPreviousDecl(OS, D);
1048     dumpSourceRange(D->getSourceRange());
1049     OS << ' ';
1050     dumpLocation(D->getLocation());
1051     if (D->isFromASTFile())
1052       OS << " imported";
1053     if (Module *M = D->getOwningModule())
1054       OS << " in " << M->getFullModuleName();
1055     if (auto *ND = dyn_cast<NamedDecl>(D))
1056       for (Module *M : D->getASTContext().getModulesWithMergedDefinition(
1057                const_cast<NamedDecl *>(ND)))
1058         dumpChild([=] { OS << "also in " << M->getFullModuleName(); });
1059     if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
1060       if (ND->isHidden())
1061         OS << " hidden";
1062     if (D->isImplicit())
1063       OS << " implicit";
1064     if (D->isUsed())
1065       OS << " used";
1066     else if (D->isThisDeclarationReferenced())
1067       OS << " referenced";
1068     if (D->isInvalidDecl())
1069       OS << " invalid";
1070     if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1071       if (FD->isConstexpr())
1072         OS << " constexpr";
1073 
1074 
1075     ConstDeclVisitor<ASTDumper>::Visit(D);
1076 
1077     for (Decl::attr_iterator I = D->attr_begin(), E = D->attr_end(); I != E;
1078          ++I)
1079       dumpAttr(*I);
1080 
1081     if (const FullComment *Comment =
1082             D->getASTContext().getLocalCommentForDeclUncached(D))
1083       dumpFullComment(Comment);
1084 
1085     // Decls within functions are visited by the body.
1086     if (!isa<FunctionDecl>(*D) && !isa<ObjCMethodDecl>(*D) &&
1087         hasNodes(dyn_cast<DeclContext>(D)))
1088       dumpDeclContext(cast<DeclContext>(D));
1089   });
1090 }
1091 
1092 void ASTDumper::VisitLabelDecl(const LabelDecl *D) {
1093   dumpName(D);
1094 }
1095 
1096 void ASTDumper::VisitTypedefDecl(const TypedefDecl *D) {
1097   dumpName(D);
1098   dumpType(D->getUnderlyingType());
1099   if (D->isModulePrivate())
1100     OS << " __module_private__";
1101   dumpTypeAsChild(D->getUnderlyingType());
1102 }
1103 
1104 void ASTDumper::VisitEnumDecl(const EnumDecl *D) {
1105   if (D->isScoped()) {
1106     if (D->isScopedUsingClassTag())
1107       OS << " class";
1108     else
1109       OS << " struct";
1110   }
1111   dumpName(D);
1112   if (D->isModulePrivate())
1113     OS << " __module_private__";
1114   if (D->isFixed())
1115     dumpType(D->getIntegerType());
1116 }
1117 
1118 void ASTDumper::VisitRecordDecl(const RecordDecl *D) {
1119   OS << ' ' << D->getKindName();
1120   dumpName(D);
1121   if (D->isModulePrivate())
1122     OS << " __module_private__";
1123   if (D->isCompleteDefinition())
1124     OS << " definition";
1125 }
1126 
1127 void ASTDumper::VisitEnumConstantDecl(const EnumConstantDecl *D) {
1128   dumpName(D);
1129   dumpType(D->getType());
1130   if (const Expr *Init = D->getInitExpr())
1131     dumpStmt(Init);
1132 }
1133 
1134 void ASTDumper::VisitIndirectFieldDecl(const IndirectFieldDecl *D) {
1135   dumpName(D);
1136   dumpType(D->getType());
1137 
1138   for (auto *Child : D->chain())
1139     dumpDeclRef(Child);
1140 }
1141 
1142 void ASTDumper::VisitFunctionDecl(const FunctionDecl *D) {
1143   dumpName(D);
1144   dumpType(D->getType());
1145 
1146   StorageClass SC = D->getStorageClass();
1147   if (SC != SC_None)
1148     OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
1149   if (D->isInlineSpecified())
1150     OS << " inline";
1151   if (D->isVirtualAsWritten())
1152     OS << " virtual";
1153   if (D->isModulePrivate())
1154     OS << " __module_private__";
1155 
1156   if (D->isPure())
1157     OS << " pure";
1158   if (D->isDefaulted()) {
1159     OS << " default";
1160     if (D->isDeleted())
1161       OS << "_delete";
1162   }
1163   if (D->isDeletedAsWritten())
1164     OS << " delete";
1165   if (D->isTrivial())
1166     OS << " trivial";
1167 
1168   if (const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>()) {
1169     FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
1170     switch (EPI.ExceptionSpec.Type) {
1171     default: break;
1172     case EST_Unevaluated:
1173       OS << " noexcept-unevaluated " << EPI.ExceptionSpec.SourceDecl;
1174       break;
1175     case EST_Uninstantiated:
1176       OS << " noexcept-uninstantiated " << EPI.ExceptionSpec.SourceTemplate;
1177       break;
1178     }
1179   }
1180 
1181   if (const FunctionTemplateSpecializationInfo *FTSI =
1182           D->getTemplateSpecializationInfo())
1183     dumpTemplateArgumentList(*FTSI->TemplateArguments);
1184 
1185   if (!D->param_begin() && D->getNumParams())
1186     dumpChild([=] { OS << "<<NULL params x " << D->getNumParams() << ">>"; });
1187   else
1188     for (const ParmVarDecl *Parameter : D->parameters())
1189       dumpDecl(Parameter);
1190 
1191   if (const CXXConstructorDecl *C = dyn_cast<CXXConstructorDecl>(D))
1192     for (CXXConstructorDecl::init_const_iterator I = C->init_begin(),
1193                                                  E = C->init_end();
1194          I != E; ++I)
1195       dumpCXXCtorInitializer(*I);
1196 
1197   if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
1198     if (MD->size_overridden_methods() != 0) {
1199       auto dumpOverride = [=](const CXXMethodDecl *D) {
1200         SplitQualType T_split = D->getType().split();
1201         OS << D << " " << D->getParent()->getName()
1202            << "::" << D->getNameAsString() << " '"
1203            << QualType::getAsString(T_split, PrintPolicy) << "'";
1204       };
1205 
1206       dumpChild([=] {
1207         auto Overrides = MD->overridden_methods();
1208         OS << "Overrides: [ ";
1209         dumpOverride(*Overrides.begin());
1210         for (const auto *Override :
1211              llvm::make_range(Overrides.begin() + 1, Overrides.end())) {
1212           OS << ", ";
1213           dumpOverride(Override);
1214         }
1215         OS << " ]";
1216       });
1217     }
1218   }
1219 
1220   if (D->doesThisDeclarationHaveABody())
1221     dumpStmt(D->getBody());
1222 }
1223 
1224 void ASTDumper::VisitFieldDecl(const FieldDecl *D) {
1225   dumpName(D);
1226   dumpType(D->getType());
1227   if (D->isMutable())
1228     OS << " mutable";
1229   if (D->isModulePrivate())
1230     OS << " __module_private__";
1231 
1232   if (D->isBitField())
1233     dumpStmt(D->getBitWidth());
1234   if (Expr *Init = D->getInClassInitializer())
1235     dumpStmt(Init);
1236 }
1237 
1238 void ASTDumper::VisitVarDecl(const VarDecl *D) {
1239   dumpName(D);
1240   dumpType(D->getType());
1241   StorageClass SC = D->getStorageClass();
1242   if (SC != SC_None)
1243     OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
1244   switch (D->getTLSKind()) {
1245   case VarDecl::TLS_None: break;
1246   case VarDecl::TLS_Static: OS << " tls"; break;
1247   case VarDecl::TLS_Dynamic: OS << " tls_dynamic"; break;
1248   }
1249   if (D->isModulePrivate())
1250     OS << " __module_private__";
1251   if (D->isNRVOVariable())
1252     OS << " nrvo";
1253   if (D->isInline())
1254     OS << " inline";
1255   if (D->isConstexpr())
1256     OS << " constexpr";
1257   if (D->hasInit()) {
1258     switch (D->getInitStyle()) {
1259     case VarDecl::CInit: OS << " cinit"; break;
1260     case VarDecl::CallInit: OS << " callinit"; break;
1261     case VarDecl::ListInit: OS << " listinit"; break;
1262     }
1263     dumpStmt(D->getInit());
1264   }
1265 }
1266 
1267 void ASTDumper::VisitDecompositionDecl(const DecompositionDecl *D) {
1268   VisitVarDecl(D);
1269   for (auto *B : D->bindings())
1270     dumpDecl(B);
1271 }
1272 
1273 void ASTDumper::VisitBindingDecl(const BindingDecl *D) {
1274   dumpName(D);
1275   dumpType(D->getType());
1276   if (auto *E = D->getBinding())
1277     dumpStmt(E);
1278 }
1279 
1280 void ASTDumper::VisitFileScopeAsmDecl(const FileScopeAsmDecl *D) {
1281   dumpStmt(D->getAsmString());
1282 }
1283 
1284 void ASTDumper::VisitImportDecl(const ImportDecl *D) {
1285   OS << ' ' << D->getImportedModule()->getFullModuleName();
1286 }
1287 
1288 void ASTDumper::VisitPragmaCommentDecl(const PragmaCommentDecl *D) {
1289   OS << ' ';
1290   switch (D->getCommentKind()) {
1291   case PCK_Unknown:  llvm_unreachable("unexpected pragma comment kind");
1292   case PCK_Compiler: OS << "compiler"; break;
1293   case PCK_ExeStr:   OS << "exestr"; break;
1294   case PCK_Lib:      OS << "lib"; break;
1295   case PCK_Linker:   OS << "linker"; break;
1296   case PCK_User:     OS << "user"; break;
1297   }
1298   StringRef Arg = D->getArg();
1299   if (!Arg.empty())
1300     OS << " \"" << Arg << "\"";
1301 }
1302 
1303 void ASTDumper::VisitPragmaDetectMismatchDecl(
1304     const PragmaDetectMismatchDecl *D) {
1305   OS << " \"" << D->getName() << "\" \"" << D->getValue() << "\"";
1306 }
1307 
1308 void ASTDumper::VisitCapturedDecl(const CapturedDecl *D) {
1309   dumpStmt(D->getBody());
1310 }
1311 
1312 //===----------------------------------------------------------------------===//
1313 // OpenMP Declarations
1314 //===----------------------------------------------------------------------===//
1315 
1316 void ASTDumper::VisitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D) {
1317   for (auto *E : D->varlists())
1318     dumpStmt(E);
1319 }
1320 
1321 void ASTDumper::VisitOMPDeclareReductionDecl(const OMPDeclareReductionDecl *D) {
1322   dumpName(D);
1323   dumpType(D->getType());
1324   OS << " combiner";
1325   dumpStmt(D->getCombiner());
1326   if (auto *Initializer = D->getInitializer()) {
1327     OS << " initializer";
1328     switch (D->getInitializerKind()) {
1329     case OMPDeclareReductionDecl::DirectInit:
1330       OS << " omp_priv = ";
1331       break;
1332     case OMPDeclareReductionDecl::CopyInit:
1333       OS << " omp_priv ()";
1334       break;
1335     case OMPDeclareReductionDecl::CallInit:
1336       break;
1337     }
1338     dumpStmt(Initializer);
1339   }
1340 }
1341 
1342 void ASTDumper::VisitOMPCapturedExprDecl(const OMPCapturedExprDecl *D) {
1343   dumpName(D);
1344   dumpType(D->getType());
1345   dumpStmt(D->getInit());
1346 }
1347 
1348 //===----------------------------------------------------------------------===//
1349 // C++ Declarations
1350 //===----------------------------------------------------------------------===//
1351 
1352 void ASTDumper::VisitNamespaceDecl(const NamespaceDecl *D) {
1353   dumpName(D);
1354   if (D->isInline())
1355     OS << " inline";
1356   if (!D->isOriginalNamespace())
1357     dumpDeclRef(D->getOriginalNamespace(), "original");
1358 }
1359 
1360 void ASTDumper::VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
1361   OS << ' ';
1362   dumpBareDeclRef(D->getNominatedNamespace());
1363 }
1364 
1365 void ASTDumper::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
1366   dumpName(D);
1367   dumpDeclRef(D->getAliasedNamespace());
1368 }
1369 
1370 void ASTDumper::VisitTypeAliasDecl(const TypeAliasDecl *D) {
1371   dumpName(D);
1372   dumpType(D->getUnderlyingType());
1373   dumpTypeAsChild(D->getUnderlyingType());
1374 }
1375 
1376 void ASTDumper::VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D) {
1377   dumpName(D);
1378   dumpTemplateParameters(D->getTemplateParameters());
1379   dumpDecl(D->getTemplatedDecl());
1380 }
1381 
1382 void ASTDumper::VisitCXXRecordDecl(const CXXRecordDecl *D) {
1383   VisitRecordDecl(D);
1384   if (!D->isCompleteDefinition())
1385     return;
1386 
1387   dumpChild([=] {
1388     {
1389       ColorScope Color(*this, DeclKindNameColor);
1390       OS << "DefinitionData";
1391     }
1392 #define FLAG(fn, name) if (D->fn()) OS << " " #name;
1393     FLAG(isParsingBaseSpecifiers, parsing_base_specifiers);
1394 
1395     FLAG(isGenericLambda, generic);
1396     FLAG(isLambda, lambda);
1397 
1398     FLAG(canPassInRegisters, pass_in_registers);
1399     FLAG(isEmpty, empty);
1400     FLAG(isAggregate, aggregate);
1401     FLAG(isStandardLayout, standard_layout);
1402     FLAG(isTriviallyCopyable, trivially_copyable);
1403     FLAG(isPOD, pod);
1404     FLAG(isTrivial, trivial);
1405     FLAG(isPolymorphic, polymorphic);
1406     FLAG(isAbstract, abstract);
1407     FLAG(isLiteral, literal);
1408 
1409     FLAG(hasUserDeclaredConstructor, has_user_declared_ctor);
1410     FLAG(hasConstexprNonCopyMoveConstructor, has_constexpr_non_copy_move_ctor);
1411     FLAG(hasMutableFields, has_mutable_fields);
1412     FLAG(hasVariantMembers, has_variant_members);
1413     FLAG(allowConstDefaultInit, can_const_default_init);
1414 
1415     dumpChild([=] {
1416       {
1417         ColorScope Color(*this, DeclKindNameColor);
1418         OS << "DefaultConstructor";
1419       }
1420       FLAG(hasDefaultConstructor, exists);
1421       FLAG(hasTrivialDefaultConstructor, trivial);
1422       FLAG(hasNonTrivialDefaultConstructor, non_trivial);
1423       FLAG(hasUserProvidedDefaultConstructor, user_provided);
1424       FLAG(hasConstexprDefaultConstructor, constexpr);
1425       FLAG(needsImplicitDefaultConstructor, needs_implicit);
1426       FLAG(defaultedDefaultConstructorIsConstexpr, defaulted_is_constexpr);
1427     });
1428 
1429     dumpChild([=] {
1430       {
1431         ColorScope Color(*this, DeclKindNameColor);
1432         OS << "CopyConstructor";
1433       }
1434       FLAG(hasSimpleCopyConstructor, simple);
1435       FLAG(hasTrivialCopyConstructor, trivial);
1436       FLAG(hasNonTrivialCopyConstructor, non_trivial);
1437       FLAG(hasUserDeclaredCopyConstructor, user_declared);
1438       FLAG(hasCopyConstructorWithConstParam, has_const_param);
1439       FLAG(needsImplicitCopyConstructor, needs_implicit);
1440       FLAG(needsOverloadResolutionForCopyConstructor,
1441            needs_overload_resolution);
1442       if (!D->needsOverloadResolutionForCopyConstructor())
1443         FLAG(defaultedCopyConstructorIsDeleted, defaulted_is_deleted);
1444       FLAG(implicitCopyConstructorHasConstParam, implicit_has_const_param);
1445     });
1446 
1447     dumpChild([=] {
1448       {
1449         ColorScope Color(*this, DeclKindNameColor);
1450         OS << "MoveConstructor";
1451       }
1452       FLAG(hasMoveConstructor, exists);
1453       FLAG(hasSimpleMoveConstructor, simple);
1454       FLAG(hasTrivialMoveConstructor, trivial);
1455       FLAG(hasNonTrivialMoveConstructor, non_trivial);
1456       FLAG(hasUserDeclaredMoveConstructor, user_declared);
1457       FLAG(needsImplicitMoveConstructor, needs_implicit);
1458       FLAG(needsOverloadResolutionForMoveConstructor,
1459            needs_overload_resolution);
1460       if (!D->needsOverloadResolutionForMoveConstructor())
1461         FLAG(defaultedMoveConstructorIsDeleted, defaulted_is_deleted);
1462     });
1463 
1464     dumpChild([=] {
1465       {
1466         ColorScope Color(*this, DeclKindNameColor);
1467         OS << "CopyAssignment";
1468       }
1469       FLAG(hasTrivialCopyAssignment, trivial);
1470       FLAG(hasNonTrivialCopyAssignment, non_trivial);
1471       FLAG(hasCopyAssignmentWithConstParam, has_const_param);
1472       FLAG(hasUserDeclaredCopyAssignment, user_declared);
1473       FLAG(needsImplicitCopyAssignment, needs_implicit);
1474       FLAG(needsOverloadResolutionForCopyAssignment, needs_overload_resolution);
1475       FLAG(implicitCopyAssignmentHasConstParam, implicit_has_const_param);
1476     });
1477 
1478     dumpChild([=] {
1479       {
1480         ColorScope Color(*this, DeclKindNameColor);
1481         OS << "MoveAssignment";
1482       }
1483       FLAG(hasMoveAssignment, exists);
1484       FLAG(hasSimpleMoveAssignment, simple);
1485       FLAG(hasTrivialMoveAssignment, trivial);
1486       FLAG(hasNonTrivialMoveAssignment, non_trivial);
1487       FLAG(hasUserDeclaredMoveAssignment, user_declared);
1488       FLAG(needsImplicitMoveAssignment, needs_implicit);
1489       FLAG(needsOverloadResolutionForMoveAssignment, needs_overload_resolution);
1490     });
1491 
1492     dumpChild([=] {
1493       {
1494         ColorScope Color(*this, DeclKindNameColor);
1495         OS << "Destructor";
1496       }
1497       FLAG(hasSimpleDestructor, simple);
1498       FLAG(hasIrrelevantDestructor, irrelevant);
1499       FLAG(hasTrivialDestructor, trivial);
1500       FLAG(hasNonTrivialDestructor, non_trivial);
1501       FLAG(hasUserDeclaredDestructor, user_declared);
1502       FLAG(needsImplicitDestructor, needs_implicit);
1503       FLAG(needsOverloadResolutionForDestructor, needs_overload_resolution);
1504       if (!D->needsOverloadResolutionForDestructor())
1505         FLAG(defaultedDestructorIsDeleted, defaulted_is_deleted);
1506     });
1507   });
1508 
1509   for (const auto &I : D->bases()) {
1510     dumpChild([=] {
1511       if (I.isVirtual())
1512         OS << "virtual ";
1513       dumpAccessSpecifier(I.getAccessSpecifier());
1514       dumpType(I.getType());
1515       if (I.isPackExpansion())
1516         OS << "...";
1517     });
1518   }
1519 }
1520 
1521 void ASTDumper::VisitStaticAssertDecl(const StaticAssertDecl *D) {
1522   dumpStmt(D->getAssertExpr());
1523   dumpStmt(D->getMessage());
1524 }
1525 
1526 template<typename SpecializationDecl>
1527 void ASTDumper::VisitTemplateDeclSpecialization(const SpecializationDecl *D,
1528                                                 bool DumpExplicitInst,
1529                                                 bool DumpRefOnly) {
1530   bool DumpedAny = false;
1531   for (auto *RedeclWithBadType : D->redecls()) {
1532     // FIXME: The redecls() range sometimes has elements of a less-specific
1533     // type. (In particular, ClassTemplateSpecializationDecl::redecls() gives
1534     // us TagDecls, and should give CXXRecordDecls).
1535     auto *Redecl = dyn_cast<SpecializationDecl>(RedeclWithBadType);
1536     if (!Redecl) {
1537       // Found the injected-class-name for a class template. This will be dumped
1538       // as part of its surrounding class so we don't need to dump it here.
1539       assert(isa<CXXRecordDecl>(RedeclWithBadType) &&
1540              "expected an injected-class-name");
1541       continue;
1542     }
1543 
1544     switch (Redecl->getTemplateSpecializationKind()) {
1545     case TSK_ExplicitInstantiationDeclaration:
1546     case TSK_ExplicitInstantiationDefinition:
1547       if (!DumpExplicitInst)
1548         break;
1549       LLVM_FALLTHROUGH;
1550     case TSK_Undeclared:
1551     case TSK_ImplicitInstantiation:
1552       if (DumpRefOnly)
1553         dumpDeclRef(Redecl);
1554       else
1555         dumpDecl(Redecl);
1556       DumpedAny = true;
1557       break;
1558     case TSK_ExplicitSpecialization:
1559       break;
1560     }
1561   }
1562 
1563   // Ensure we dump at least one decl for each specialization.
1564   if (!DumpedAny)
1565     dumpDeclRef(D);
1566 }
1567 
1568 template<typename TemplateDecl>
1569 void ASTDumper::VisitTemplateDecl(const TemplateDecl *D,
1570                                   bool DumpExplicitInst) {
1571   dumpName(D);
1572   dumpTemplateParameters(D->getTemplateParameters());
1573 
1574   dumpDecl(D->getTemplatedDecl());
1575 
1576   for (auto *Child : D->specializations())
1577     VisitTemplateDeclSpecialization(Child, DumpExplicitInst,
1578                                     !D->isCanonicalDecl());
1579 }
1580 
1581 void ASTDumper::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
1582   // FIXME: We don't add a declaration of a function template specialization
1583   // to its context when it's explicitly instantiated, so dump explicit
1584   // instantiations when we dump the template itself.
1585   VisitTemplateDecl(D, true);
1586 }
1587 
1588 void ASTDumper::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
1589   VisitTemplateDecl(D, false);
1590 }
1591 
1592 void ASTDumper::VisitClassTemplateSpecializationDecl(
1593     const ClassTemplateSpecializationDecl *D) {
1594   VisitCXXRecordDecl(D);
1595   dumpTemplateArgumentList(D->getTemplateArgs());
1596 }
1597 
1598 void ASTDumper::VisitClassTemplatePartialSpecializationDecl(
1599     const ClassTemplatePartialSpecializationDecl *D) {
1600   VisitClassTemplateSpecializationDecl(D);
1601   dumpTemplateParameters(D->getTemplateParameters());
1602 }
1603 
1604 void ASTDumper::VisitClassScopeFunctionSpecializationDecl(
1605     const ClassScopeFunctionSpecializationDecl *D) {
1606   dumpDecl(D->getSpecialization());
1607   if (D->hasExplicitTemplateArgs())
1608     dumpTemplateArgumentListInfo(D->templateArgs());
1609 }
1610 
1611 void ASTDumper::VisitVarTemplateDecl(const VarTemplateDecl *D) {
1612   VisitTemplateDecl(D, false);
1613 }
1614 
1615 void ASTDumper::VisitBuiltinTemplateDecl(const BuiltinTemplateDecl *D) {
1616   dumpName(D);
1617   dumpTemplateParameters(D->getTemplateParameters());
1618 }
1619 
1620 void ASTDumper::VisitVarTemplateSpecializationDecl(
1621     const VarTemplateSpecializationDecl *D) {
1622   dumpTemplateArgumentList(D->getTemplateArgs());
1623   VisitVarDecl(D);
1624 }
1625 
1626 void ASTDumper::VisitVarTemplatePartialSpecializationDecl(
1627     const VarTemplatePartialSpecializationDecl *D) {
1628   dumpTemplateParameters(D->getTemplateParameters());
1629   VisitVarTemplateSpecializationDecl(D);
1630 }
1631 
1632 void ASTDumper::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
1633   if (D->wasDeclaredWithTypename())
1634     OS << " typename";
1635   else
1636     OS << " class";
1637   OS << " depth " << D->getDepth() << " index " << D->getIndex();
1638   if (D->isParameterPack())
1639     OS << " ...";
1640   dumpName(D);
1641   if (D->hasDefaultArgument())
1642     dumpTemplateArgument(D->getDefaultArgument());
1643 }
1644 
1645 void ASTDumper::VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D) {
1646   dumpType(D->getType());
1647   OS << " depth " << D->getDepth() << " index " << D->getIndex();
1648   if (D->isParameterPack())
1649     OS << " ...";
1650   dumpName(D);
1651   if (D->hasDefaultArgument())
1652     dumpTemplateArgument(D->getDefaultArgument());
1653 }
1654 
1655 void ASTDumper::VisitTemplateTemplateParmDecl(
1656     const TemplateTemplateParmDecl *D) {
1657   OS << " depth " << D->getDepth() << " index " << D->getIndex();
1658   if (D->isParameterPack())
1659     OS << " ...";
1660   dumpName(D);
1661   dumpTemplateParameters(D->getTemplateParameters());
1662   if (D->hasDefaultArgument())
1663     dumpTemplateArgumentLoc(D->getDefaultArgument());
1664 }
1665 
1666 void ASTDumper::VisitUsingDecl(const UsingDecl *D) {
1667   OS << ' ';
1668   if (D->getQualifier())
1669     D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1670   OS << D->getNameAsString();
1671 }
1672 
1673 void ASTDumper::VisitUnresolvedUsingTypenameDecl(
1674     const UnresolvedUsingTypenameDecl *D) {
1675   OS << ' ';
1676   if (D->getQualifier())
1677     D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1678   OS << D->getNameAsString();
1679 }
1680 
1681 void ASTDumper::VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
1682   OS << ' ';
1683   if (D->getQualifier())
1684     D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1685   OS << D->getNameAsString();
1686   dumpType(D->getType());
1687 }
1688 
1689 void ASTDumper::VisitUsingShadowDecl(const UsingShadowDecl *D) {
1690   OS << ' ';
1691   dumpBareDeclRef(D->getTargetDecl());
1692   if (auto *TD = dyn_cast<TypeDecl>(D->getUnderlyingDecl()))
1693     dumpTypeAsChild(TD->getTypeForDecl());
1694 }
1695 
1696 void ASTDumper::VisitConstructorUsingShadowDecl(
1697     const ConstructorUsingShadowDecl *D) {
1698   if (D->constructsVirtualBase())
1699     OS << " virtual";
1700 
1701   dumpChild([=] {
1702     OS << "target ";
1703     dumpBareDeclRef(D->getTargetDecl());
1704   });
1705 
1706   dumpChild([=] {
1707     OS << "nominated ";
1708     dumpBareDeclRef(D->getNominatedBaseClass());
1709     OS << ' ';
1710     dumpBareDeclRef(D->getNominatedBaseClassShadowDecl());
1711   });
1712 
1713   dumpChild([=] {
1714     OS << "constructed ";
1715     dumpBareDeclRef(D->getConstructedBaseClass());
1716     OS << ' ';
1717     dumpBareDeclRef(D->getConstructedBaseClassShadowDecl());
1718   });
1719 }
1720 
1721 void ASTDumper::VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
1722   switch (D->getLanguage()) {
1723   case LinkageSpecDecl::lang_c: OS << " C"; break;
1724   case LinkageSpecDecl::lang_cxx: OS << " C++"; break;
1725   }
1726 }
1727 
1728 void ASTDumper::VisitAccessSpecDecl(const AccessSpecDecl *D) {
1729   OS << ' ';
1730   dumpAccessSpecifier(D->getAccess());
1731 }
1732 
1733 void ASTDumper::VisitFriendDecl(const FriendDecl *D) {
1734   if (TypeSourceInfo *T = D->getFriendType())
1735     dumpType(T->getType());
1736   else
1737     dumpDecl(D->getFriendDecl());
1738 }
1739 
1740 //===----------------------------------------------------------------------===//
1741 // Obj-C Declarations
1742 //===----------------------------------------------------------------------===//
1743 
1744 void ASTDumper::VisitObjCIvarDecl(const ObjCIvarDecl *D) {
1745   dumpName(D);
1746   dumpType(D->getType());
1747   if (D->getSynthesize())
1748     OS << " synthesize";
1749 
1750   switch (D->getAccessControl()) {
1751   case ObjCIvarDecl::None:
1752     OS << " none";
1753     break;
1754   case ObjCIvarDecl::Private:
1755     OS << " private";
1756     break;
1757   case ObjCIvarDecl::Protected:
1758     OS << " protected";
1759     break;
1760   case ObjCIvarDecl::Public:
1761     OS << " public";
1762     break;
1763   case ObjCIvarDecl::Package:
1764     OS << " package";
1765     break;
1766   }
1767 }
1768 
1769 void ASTDumper::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
1770   if (D->isInstanceMethod())
1771     OS << " -";
1772   else
1773     OS << " +";
1774   dumpName(D);
1775   dumpType(D->getReturnType());
1776 
1777   if (D->isThisDeclarationADefinition()) {
1778     dumpDeclContext(D);
1779   } else {
1780     for (const ParmVarDecl *Parameter : D->parameters())
1781       dumpDecl(Parameter);
1782   }
1783 
1784   if (D->isVariadic())
1785     dumpChild([=] { OS << "..."; });
1786 
1787   if (D->hasBody())
1788     dumpStmt(D->getBody());
1789 }
1790 
1791 void ASTDumper::VisitObjCTypeParamDecl(const ObjCTypeParamDecl *D) {
1792   dumpName(D);
1793   switch (D->getVariance()) {
1794   case ObjCTypeParamVariance::Invariant:
1795     break;
1796 
1797   case ObjCTypeParamVariance::Covariant:
1798     OS << " covariant";
1799     break;
1800 
1801   case ObjCTypeParamVariance::Contravariant:
1802     OS << " contravariant";
1803     break;
1804   }
1805 
1806   if (D->hasExplicitBound())
1807     OS << " bounded";
1808   dumpType(D->getUnderlyingType());
1809 }
1810 
1811 void ASTDumper::VisitObjCCategoryDecl(const ObjCCategoryDecl *D) {
1812   dumpName(D);
1813   dumpDeclRef(D->getClassInterface());
1814   dumpObjCTypeParamList(D->getTypeParamList());
1815   dumpDeclRef(D->getImplementation());
1816   for (ObjCCategoryDecl::protocol_iterator I = D->protocol_begin(),
1817                                            E = D->protocol_end();
1818        I != E; ++I)
1819     dumpDeclRef(*I);
1820 }
1821 
1822 void ASTDumper::VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) {
1823   dumpName(D);
1824   dumpDeclRef(D->getClassInterface());
1825   dumpDeclRef(D->getCategoryDecl());
1826 }
1827 
1828 void ASTDumper::VisitObjCProtocolDecl(const ObjCProtocolDecl *D) {
1829   dumpName(D);
1830 
1831   for (auto *Child : D->protocols())
1832     dumpDeclRef(Child);
1833 }
1834 
1835 void ASTDumper::VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) {
1836   dumpName(D);
1837   dumpObjCTypeParamList(D->getTypeParamListAsWritten());
1838   dumpDeclRef(D->getSuperClass(), "super");
1839 
1840   dumpDeclRef(D->getImplementation());
1841   for (auto *Child : D->protocols())
1842     dumpDeclRef(Child);
1843 }
1844 
1845 void ASTDumper::VisitObjCImplementationDecl(const ObjCImplementationDecl *D) {
1846   dumpName(D);
1847   dumpDeclRef(D->getSuperClass(), "super");
1848   dumpDeclRef(D->getClassInterface());
1849   for (ObjCImplementationDecl::init_const_iterator I = D->init_begin(),
1850                                                    E = D->init_end();
1851        I != E; ++I)
1852     dumpCXXCtorInitializer(*I);
1853 }
1854 
1855 void ASTDumper::VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D) {
1856   dumpName(D);
1857   dumpDeclRef(D->getClassInterface());
1858 }
1859 
1860 void ASTDumper::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
1861   dumpName(D);
1862   dumpType(D->getType());
1863 
1864   if (D->getPropertyImplementation() == ObjCPropertyDecl::Required)
1865     OS << " required";
1866   else if (D->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1867     OS << " optional";
1868 
1869   ObjCPropertyDecl::PropertyAttributeKind Attrs = D->getPropertyAttributes();
1870   if (Attrs != ObjCPropertyDecl::OBJC_PR_noattr) {
1871     if (Attrs & ObjCPropertyDecl::OBJC_PR_readonly)
1872       OS << " readonly";
1873     if (Attrs & ObjCPropertyDecl::OBJC_PR_assign)
1874       OS << " assign";
1875     if (Attrs & ObjCPropertyDecl::OBJC_PR_readwrite)
1876       OS << " readwrite";
1877     if (Attrs & ObjCPropertyDecl::OBJC_PR_retain)
1878       OS << " retain";
1879     if (Attrs & ObjCPropertyDecl::OBJC_PR_copy)
1880       OS << " copy";
1881     if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic)
1882       OS << " nonatomic";
1883     if (Attrs & ObjCPropertyDecl::OBJC_PR_atomic)
1884       OS << " atomic";
1885     if (Attrs & ObjCPropertyDecl::OBJC_PR_weak)
1886       OS << " weak";
1887     if (Attrs & ObjCPropertyDecl::OBJC_PR_strong)
1888       OS << " strong";
1889     if (Attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained)
1890       OS << " unsafe_unretained";
1891     if (Attrs & ObjCPropertyDecl::OBJC_PR_class)
1892       OS << " class";
1893     if (Attrs & ObjCPropertyDecl::OBJC_PR_getter)
1894       dumpDeclRef(D->getGetterMethodDecl(), "getter");
1895     if (Attrs & ObjCPropertyDecl::OBJC_PR_setter)
1896       dumpDeclRef(D->getSetterMethodDecl(), "setter");
1897   }
1898 }
1899 
1900 void ASTDumper::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
1901   dumpName(D->getPropertyDecl());
1902   if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
1903     OS << " synthesize";
1904   else
1905     OS << " dynamic";
1906   dumpDeclRef(D->getPropertyDecl());
1907   dumpDeclRef(D->getPropertyIvarDecl());
1908 }
1909 
1910 void ASTDumper::VisitBlockDecl(const BlockDecl *D) {
1911   for (auto I : D->parameters())
1912     dumpDecl(I);
1913 
1914   if (D->isVariadic())
1915     dumpChild([=]{ OS << "..."; });
1916 
1917   if (D->capturesCXXThis())
1918     dumpChild([=]{ OS << "capture this"; });
1919 
1920   for (const auto &I : D->captures()) {
1921     dumpChild([=] {
1922       OS << "capture";
1923       if (I.isByRef())
1924         OS << " byref";
1925       if (I.isNested())
1926         OS << " nested";
1927       if (I.getVariable()) {
1928         OS << ' ';
1929         dumpBareDeclRef(I.getVariable());
1930       }
1931       if (I.hasCopyExpr())
1932         dumpStmt(I.getCopyExpr());
1933     });
1934   }
1935   dumpStmt(D->getBody());
1936 }
1937 
1938 //===----------------------------------------------------------------------===//
1939 //  Stmt dumping methods.
1940 //===----------------------------------------------------------------------===//
1941 
1942 void ASTDumper::dumpStmt(const Stmt *S) {
1943   dumpChild([=] {
1944     if (!S) {
1945       ColorScope Color(*this, NullColor);
1946       OS << "<<<NULL>>>";
1947       return;
1948     }
1949 
1950     // Some statements have custom mechanisms for dumping their children.
1951     if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
1952       VisitDeclStmt(DS);
1953       return;
1954     }
1955     if (const GenericSelectionExpr *GSE = dyn_cast<GenericSelectionExpr>(S)) {
1956       VisitGenericSelectionExpr(GSE);
1957       return;
1958     }
1959 
1960     ConstStmtVisitor<ASTDumper>::Visit(S);
1961 
1962     for (const Stmt *SubStmt : S->children())
1963       dumpStmt(SubStmt);
1964   });
1965 }
1966 
1967 void ASTDumper::VisitStmt(const Stmt *Node) {
1968   {
1969     ColorScope Color(*this, StmtColor);
1970     OS << Node->getStmtClassName();
1971   }
1972   dumpPointer(Node);
1973   dumpSourceRange(Node->getSourceRange());
1974 }
1975 
1976 void ASTDumper::VisitDeclStmt(const DeclStmt *Node) {
1977   VisitStmt(Node);
1978   for (DeclStmt::const_decl_iterator I = Node->decl_begin(),
1979                                      E = Node->decl_end();
1980        I != E; ++I)
1981     dumpDecl(*I);
1982 }
1983 
1984 void ASTDumper::VisitAttributedStmt(const AttributedStmt *Node) {
1985   VisitStmt(Node);
1986   for (ArrayRef<const Attr *>::iterator I = Node->getAttrs().begin(),
1987                                         E = Node->getAttrs().end();
1988        I != E; ++I)
1989     dumpAttr(*I);
1990 }
1991 
1992 void ASTDumper::VisitLabelStmt(const LabelStmt *Node) {
1993   VisitStmt(Node);
1994   OS << " '" << Node->getName() << "'";
1995 }
1996 
1997 void ASTDumper::VisitGotoStmt(const GotoStmt *Node) {
1998   VisitStmt(Node);
1999   OS << " '" << Node->getLabel()->getName() << "'";
2000   dumpPointer(Node->getLabel());
2001 }
2002 
2003 void ASTDumper::VisitCXXCatchStmt(const CXXCatchStmt *Node) {
2004   VisitStmt(Node);
2005   dumpDecl(Node->getExceptionDecl());
2006 }
2007 
2008 void ASTDumper::VisitCapturedStmt(const CapturedStmt *Node) {
2009   VisitStmt(Node);
2010   dumpDecl(Node->getCapturedDecl());
2011 }
2012 
2013 //===----------------------------------------------------------------------===//
2014 //  OpenMP dumping methods.
2015 //===----------------------------------------------------------------------===//
2016 
2017 void ASTDumper::VisitOMPExecutableDirective(
2018     const OMPExecutableDirective *Node) {
2019   VisitStmt(Node);
2020   for (auto *C : Node->clauses()) {
2021     dumpChild([=] {
2022       if (!C) {
2023         ColorScope Color(*this, NullColor);
2024         OS << "<<<NULL>>> OMPClause";
2025         return;
2026       }
2027       {
2028         ColorScope Color(*this, AttrColor);
2029         StringRef ClauseName(getOpenMPClauseName(C->getClauseKind()));
2030         OS << "OMP" << ClauseName.substr(/*Start=*/0, /*N=*/1).upper()
2031            << ClauseName.drop_front() << "Clause";
2032       }
2033       dumpPointer(C);
2034       dumpSourceRange(SourceRange(C->getLocStart(), C->getLocEnd()));
2035       if (C->isImplicit())
2036         OS << " <implicit>";
2037       for (auto *S : C->children())
2038         dumpStmt(S);
2039     });
2040   }
2041 }
2042 
2043 //===----------------------------------------------------------------------===//
2044 //  Expr dumping methods.
2045 //===----------------------------------------------------------------------===//
2046 
2047 void ASTDumper::VisitExpr(const Expr *Node) {
2048   VisitStmt(Node);
2049   dumpType(Node->getType());
2050 
2051   {
2052     ColorScope Color(*this, ValueKindColor);
2053     switch (Node->getValueKind()) {
2054     case VK_RValue:
2055       break;
2056     case VK_LValue:
2057       OS << " lvalue";
2058       break;
2059     case VK_XValue:
2060       OS << " xvalue";
2061       break;
2062     }
2063   }
2064 
2065   {
2066     ColorScope Color(*this, ObjectKindColor);
2067     switch (Node->getObjectKind()) {
2068     case OK_Ordinary:
2069       break;
2070     case OK_BitField:
2071       OS << " bitfield";
2072       break;
2073     case OK_ObjCProperty:
2074       OS << " objcproperty";
2075       break;
2076     case OK_ObjCSubscript:
2077       OS << " objcsubscript";
2078       break;
2079     case OK_VectorComponent:
2080       OS << " vectorcomponent";
2081       break;
2082     }
2083   }
2084 }
2085 
2086 static void dumpBasePath(raw_ostream &OS, const CastExpr *Node) {
2087   if (Node->path_empty())
2088     return;
2089 
2090   OS << " (";
2091   bool First = true;
2092   for (CastExpr::path_const_iterator I = Node->path_begin(),
2093                                      E = Node->path_end();
2094        I != E; ++I) {
2095     const CXXBaseSpecifier *Base = *I;
2096     if (!First)
2097       OS << " -> ";
2098 
2099     const CXXRecordDecl *RD =
2100     cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
2101 
2102     if (Base->isVirtual())
2103       OS << "virtual ";
2104     OS << RD->getName();
2105     First = false;
2106   }
2107 
2108   OS << ')';
2109 }
2110 
2111 void ASTDumper::VisitCastExpr(const CastExpr *Node) {
2112   VisitExpr(Node);
2113   OS << " <";
2114   {
2115     ColorScope Color(*this, CastColor);
2116     OS << Node->getCastKindName();
2117   }
2118   dumpBasePath(OS, Node);
2119   OS << ">";
2120 }
2121 
2122 void ASTDumper::VisitDeclRefExpr(const DeclRefExpr *Node) {
2123   VisitExpr(Node);
2124 
2125   OS << " ";
2126   dumpBareDeclRef(Node->getDecl());
2127   if (Node->getDecl() != Node->getFoundDecl()) {
2128     OS << " (";
2129     dumpBareDeclRef(Node->getFoundDecl());
2130     OS << ")";
2131   }
2132 }
2133 
2134 void ASTDumper::VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node) {
2135   VisitExpr(Node);
2136   OS << " (";
2137   if (!Node->requiresADL())
2138     OS << "no ";
2139   OS << "ADL) = '" << Node->getName() << '\'';
2140 
2141   UnresolvedLookupExpr::decls_iterator
2142     I = Node->decls_begin(), E = Node->decls_end();
2143   if (I == E)
2144     OS << " empty";
2145   for (; I != E; ++I)
2146     dumpPointer(*I);
2147 }
2148 
2149 void ASTDumper::VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node) {
2150   VisitExpr(Node);
2151 
2152   {
2153     ColorScope Color(*this, DeclKindNameColor);
2154     OS << " " << Node->getDecl()->getDeclKindName() << "Decl";
2155   }
2156   OS << "='" << *Node->getDecl() << "'";
2157   dumpPointer(Node->getDecl());
2158   if (Node->isFreeIvar())
2159     OS << " isFreeIvar";
2160 }
2161 
2162 void ASTDumper::VisitPredefinedExpr(const PredefinedExpr *Node) {
2163   VisitExpr(Node);
2164   OS << " " << PredefinedExpr::getIdentTypeName(Node->getIdentType());
2165 }
2166 
2167 void ASTDumper::VisitCharacterLiteral(const CharacterLiteral *Node) {
2168   VisitExpr(Node);
2169   ColorScope Color(*this, ValueColor);
2170   OS << " " << Node->getValue();
2171 }
2172 
2173 void ASTDumper::VisitIntegerLiteral(const IntegerLiteral *Node) {
2174   VisitExpr(Node);
2175 
2176   bool isSigned = Node->getType()->isSignedIntegerType();
2177   ColorScope Color(*this, ValueColor);
2178   OS << " " << Node->getValue().toString(10, isSigned);
2179 }
2180 
2181 void ASTDumper::VisitFixedPointLiteral(const FixedPointLiteral *Node) {
2182   VisitExpr(Node);
2183 
2184   ColorScope Color(*this, ValueColor);
2185   OS << " " << Node->getValueAsString(/*Radix=*/10);
2186 }
2187 
2188 void ASTDumper::VisitFloatingLiteral(const FloatingLiteral *Node) {
2189   VisitExpr(Node);
2190   ColorScope Color(*this, ValueColor);
2191   OS << " " << Node->getValueAsApproximateDouble();
2192 }
2193 
2194 void ASTDumper::VisitStringLiteral(const StringLiteral *Str) {
2195   VisitExpr(Str);
2196   ColorScope Color(*this, ValueColor);
2197   OS << " ";
2198   Str->outputString(OS);
2199 }
2200 
2201 void ASTDumper::VisitInitListExpr(const InitListExpr *ILE) {
2202   VisitExpr(ILE);
2203   if (auto *Filler = ILE->getArrayFiller()) {
2204     dumpChild([=] {
2205       OS << "array filler";
2206       dumpStmt(Filler);
2207     });
2208   }
2209   if (auto *Field = ILE->getInitializedFieldInUnion()) {
2210     OS << " field ";
2211     dumpBareDeclRef(Field);
2212   }
2213 }
2214 
2215 void ASTDumper::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) {
2216   VisitExpr(E);
2217 }
2218 
2219 void ASTDumper::VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E) {
2220   VisitExpr(E);
2221 }
2222 
2223 void ASTDumper::VisitUnaryOperator(const UnaryOperator *Node) {
2224   VisitExpr(Node);
2225   OS << " " << (Node->isPostfix() ? "postfix" : "prefix")
2226      << " '" << UnaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
2227   if (!Node->canOverflow())
2228     OS << " cannot overflow";
2229 }
2230 
2231 void ASTDumper::VisitUnaryExprOrTypeTraitExpr(
2232     const UnaryExprOrTypeTraitExpr *Node) {
2233   VisitExpr(Node);
2234   switch(Node->getKind()) {
2235   case UETT_SizeOf:
2236     OS << " sizeof";
2237     break;
2238   case UETT_AlignOf:
2239     OS << " alignof";
2240     break;
2241   case UETT_VecStep:
2242     OS << " vec_step";
2243     break;
2244   case UETT_OpenMPRequiredSimdAlign:
2245     OS << " __builtin_omp_required_simd_align";
2246     break;
2247   }
2248   if (Node->isArgumentType())
2249     dumpType(Node->getArgumentType());
2250 }
2251 
2252 void ASTDumper::VisitMemberExpr(const MemberExpr *Node) {
2253   VisitExpr(Node);
2254   OS << " " << (Node->isArrow() ? "->" : ".") << *Node->getMemberDecl();
2255   dumpPointer(Node->getMemberDecl());
2256 }
2257 
2258 void ASTDumper::VisitExtVectorElementExpr(const ExtVectorElementExpr *Node) {
2259   VisitExpr(Node);
2260   OS << " " << Node->getAccessor().getNameStart();
2261 }
2262 
2263 void ASTDumper::VisitBinaryOperator(const BinaryOperator *Node) {
2264   VisitExpr(Node);
2265   OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
2266 }
2267 
2268 void ASTDumper::VisitCompoundAssignOperator(
2269     const CompoundAssignOperator *Node) {
2270   VisitExpr(Node);
2271   OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode())
2272      << "' ComputeLHSTy=";
2273   dumpBareType(Node->getComputationLHSType());
2274   OS << " ComputeResultTy=";
2275   dumpBareType(Node->getComputationResultType());
2276 }
2277 
2278 void ASTDumper::VisitBlockExpr(const BlockExpr *Node) {
2279   VisitExpr(Node);
2280   dumpDecl(Node->getBlockDecl());
2281 }
2282 
2283 void ASTDumper::VisitOpaqueValueExpr(const OpaqueValueExpr *Node) {
2284   VisitExpr(Node);
2285 
2286   if (Expr *Source = Node->getSourceExpr())
2287     dumpStmt(Source);
2288 }
2289 
2290 void ASTDumper::VisitGenericSelectionExpr(const GenericSelectionExpr *E) {
2291   VisitExpr(E);
2292   if (E->isResultDependent())
2293     OS << " result_dependent";
2294   dumpStmt(E->getControllingExpr());
2295   dumpTypeAsChild(E->getControllingExpr()->getType()); // FIXME: remove
2296 
2297   for (unsigned I = 0, N = E->getNumAssocs(); I != N; ++I) {
2298     dumpChild([=] {
2299       if (const TypeSourceInfo *TSI = E->getAssocTypeSourceInfo(I)) {
2300         OS << "case ";
2301         dumpType(TSI->getType());
2302       } else {
2303         OS << "default";
2304       }
2305 
2306       if (!E->isResultDependent() && E->getResultIndex() == I)
2307         OS << " selected";
2308 
2309       if (const TypeSourceInfo *TSI = E->getAssocTypeSourceInfo(I))
2310         dumpTypeAsChild(TSI->getType());
2311       dumpStmt(E->getAssocExpr(I));
2312     });
2313   }
2314 }
2315 
2316 // GNU extensions.
2317 
2318 void ASTDumper::VisitAddrLabelExpr(const AddrLabelExpr *Node) {
2319   VisitExpr(Node);
2320   OS << " " << Node->getLabel()->getName();
2321   dumpPointer(Node->getLabel());
2322 }
2323 
2324 //===----------------------------------------------------------------------===//
2325 // C++ Expressions
2326 //===----------------------------------------------------------------------===//
2327 
2328 void ASTDumper::VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node) {
2329   VisitExpr(Node);
2330   OS << " " << Node->getCastName()
2331      << "<" << Node->getTypeAsWritten().getAsString() << ">"
2332      << " <" << Node->getCastKindName();
2333   dumpBasePath(OS, Node);
2334   OS << ">";
2335 }
2336 
2337 void ASTDumper::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node) {
2338   VisitExpr(Node);
2339   OS << " " << (Node->getValue() ? "true" : "false");
2340 }
2341 
2342 void ASTDumper::VisitCXXThisExpr(const CXXThisExpr *Node) {
2343   VisitExpr(Node);
2344   OS << " this";
2345 }
2346 
2347 void ASTDumper::VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node) {
2348   VisitExpr(Node);
2349   OS << " functional cast to " << Node->getTypeAsWritten().getAsString()
2350      << " <" << Node->getCastKindName() << ">";
2351 }
2352 
2353 void ASTDumper::VisitCXXUnresolvedConstructExpr(
2354     const CXXUnresolvedConstructExpr *Node) {
2355   VisitExpr(Node);
2356   dumpType(Node->getTypeAsWritten());
2357   if (Node->isListInitialization())
2358     OS << " list";
2359 }
2360 
2361 void ASTDumper::VisitCXXConstructExpr(const CXXConstructExpr *Node) {
2362   VisitExpr(Node);
2363   CXXConstructorDecl *Ctor = Node->getConstructor();
2364   dumpType(Ctor->getType());
2365   if (Node->isElidable())
2366     OS << " elidable";
2367   if (Node->isListInitialization())
2368     OS << " list";
2369   if (Node->isStdInitListInitialization())
2370     OS << " std::initializer_list";
2371   if (Node->requiresZeroInitialization())
2372     OS << " zeroing";
2373 }
2374 
2375 void ASTDumper::VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node) {
2376   VisitExpr(Node);
2377   OS << " ";
2378   dumpCXXTemporary(Node->getTemporary());
2379 }
2380 
2381 void ASTDumper::VisitCXXNewExpr(const CXXNewExpr *Node) {
2382   VisitExpr(Node);
2383   if (Node->isGlobalNew())
2384     OS << " global";
2385   if (Node->isArray())
2386     OS << " array";
2387   if (Node->getOperatorNew()) {
2388     OS << ' ';
2389     dumpBareDeclRef(Node->getOperatorNew());
2390   }
2391   // We could dump the deallocation function used in case of error, but it's
2392   // usually not that interesting.
2393 }
2394 
2395 void ASTDumper::VisitCXXDeleteExpr(const CXXDeleteExpr *Node) {
2396   VisitExpr(Node);
2397   if (Node->isGlobalDelete())
2398     OS << " global";
2399   if (Node->isArrayForm())
2400     OS << " array";
2401   if (Node->getOperatorDelete()) {
2402     OS << ' ';
2403     dumpBareDeclRef(Node->getOperatorDelete());
2404   }
2405 }
2406 
2407 void
2408 ASTDumper::VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node) {
2409   VisitExpr(Node);
2410   if (const ValueDecl *VD = Node->getExtendingDecl()) {
2411     OS << " extended by ";
2412     dumpBareDeclRef(VD);
2413   }
2414 }
2415 
2416 void ASTDumper::VisitExprWithCleanups(const ExprWithCleanups *Node) {
2417   VisitExpr(Node);
2418   for (unsigned i = 0, e = Node->getNumObjects(); i != e; ++i)
2419     dumpDeclRef(Node->getObject(i), "cleanup");
2420 }
2421 
2422 void ASTDumper::dumpCXXTemporary(const CXXTemporary *Temporary) {
2423   OS << "(CXXTemporary";
2424   dumpPointer(Temporary);
2425   OS << ")";
2426 }
2427 
2428 void ASTDumper::VisitSizeOfPackExpr(const SizeOfPackExpr *Node) {
2429   VisitExpr(Node);
2430   dumpPointer(Node->getPack());
2431   dumpName(Node->getPack());
2432   if (Node->isPartiallySubstituted())
2433     for (const auto &A : Node->getPartialArguments())
2434       dumpTemplateArgument(A);
2435 }
2436 
2437 void ASTDumper::VisitCXXDependentScopeMemberExpr(
2438     const CXXDependentScopeMemberExpr *Node) {
2439   VisitExpr(Node);
2440   OS << " " << (Node->isArrow() ? "->" : ".") << Node->getMember();
2441 }
2442 
2443 //===----------------------------------------------------------------------===//
2444 // Obj-C Expressions
2445 //===----------------------------------------------------------------------===//
2446 
2447 void ASTDumper::VisitObjCMessageExpr(const ObjCMessageExpr *Node) {
2448   VisitExpr(Node);
2449   OS << " selector=";
2450   Node->getSelector().print(OS);
2451   switch (Node->getReceiverKind()) {
2452   case ObjCMessageExpr::Instance:
2453     break;
2454 
2455   case ObjCMessageExpr::Class:
2456     OS << " class=";
2457     dumpBareType(Node->getClassReceiver());
2458     break;
2459 
2460   case ObjCMessageExpr::SuperInstance:
2461     OS << " super (instance)";
2462     break;
2463 
2464   case ObjCMessageExpr::SuperClass:
2465     OS << " super (class)";
2466     break;
2467   }
2468 }
2469 
2470 void ASTDumper::VisitObjCBoxedExpr(const ObjCBoxedExpr *Node) {
2471   VisitExpr(Node);
2472   if (auto *BoxingMethod = Node->getBoxingMethod()) {
2473     OS << " selector=";
2474     BoxingMethod->getSelector().print(OS);
2475   }
2476 }
2477 
2478 void ASTDumper::VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node) {
2479   VisitStmt(Node);
2480   if (const VarDecl *CatchParam = Node->getCatchParamDecl())
2481     dumpDecl(CatchParam);
2482   else
2483     OS << " catch all";
2484 }
2485 
2486 void ASTDumper::VisitObjCEncodeExpr(const ObjCEncodeExpr *Node) {
2487   VisitExpr(Node);
2488   dumpType(Node->getEncodedType());
2489 }
2490 
2491 void ASTDumper::VisitObjCSelectorExpr(const ObjCSelectorExpr *Node) {
2492   VisitExpr(Node);
2493 
2494   OS << " ";
2495   Node->getSelector().print(OS);
2496 }
2497 
2498 void ASTDumper::VisitObjCProtocolExpr(const ObjCProtocolExpr *Node) {
2499   VisitExpr(Node);
2500 
2501   OS << ' ' << *Node->getProtocol();
2502 }
2503 
2504 void ASTDumper::VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node) {
2505   VisitExpr(Node);
2506   if (Node->isImplicitProperty()) {
2507     OS << " Kind=MethodRef Getter=\"";
2508     if (Node->getImplicitPropertyGetter())
2509       Node->getImplicitPropertyGetter()->getSelector().print(OS);
2510     else
2511       OS << "(null)";
2512 
2513     OS << "\" Setter=\"";
2514     if (ObjCMethodDecl *Setter = Node->getImplicitPropertySetter())
2515       Setter->getSelector().print(OS);
2516     else
2517       OS << "(null)";
2518     OS << "\"";
2519   } else {
2520     OS << " Kind=PropertyRef Property=\"" << *Node->getExplicitProperty() <<'"';
2521   }
2522 
2523   if (Node->isSuperReceiver())
2524     OS << " super";
2525 
2526   OS << " Messaging=";
2527   if (Node->isMessagingGetter() && Node->isMessagingSetter())
2528     OS << "Getter&Setter";
2529   else if (Node->isMessagingGetter())
2530     OS << "Getter";
2531   else if (Node->isMessagingSetter())
2532     OS << "Setter";
2533 }
2534 
2535 void ASTDumper::VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node) {
2536   VisitExpr(Node);
2537   if (Node->isArraySubscriptRefExpr())
2538     OS << " Kind=ArraySubscript GetterForArray=\"";
2539   else
2540     OS << " Kind=DictionarySubscript GetterForDictionary=\"";
2541   if (Node->getAtIndexMethodDecl())
2542     Node->getAtIndexMethodDecl()->getSelector().print(OS);
2543   else
2544     OS << "(null)";
2545 
2546   if (Node->isArraySubscriptRefExpr())
2547     OS << "\" SetterForArray=\"";
2548   else
2549     OS << "\" SetterForDictionary=\"";
2550   if (Node->setAtIndexMethodDecl())
2551     Node->setAtIndexMethodDecl()->getSelector().print(OS);
2552   else
2553     OS << "(null)";
2554 }
2555 
2556 void ASTDumper::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node) {
2557   VisitExpr(Node);
2558   OS << " " << (Node->getValue() ? "__objc_yes" : "__objc_no");
2559 }
2560 
2561 //===----------------------------------------------------------------------===//
2562 // Comments
2563 //===----------------------------------------------------------------------===//
2564 
2565 const char *ASTDumper::getCommandName(unsigned CommandID) {
2566   if (Traits)
2567     return Traits->getCommandInfo(CommandID)->Name;
2568   const CommandInfo *Info = CommandTraits::getBuiltinCommandInfo(CommandID);
2569   if (Info)
2570     return Info->Name;
2571   return "<not a builtin command>";
2572 }
2573 
2574 void ASTDumper::dumpFullComment(const FullComment *C) {
2575   if (!C)
2576     return;
2577 
2578   FC = C;
2579   dumpComment(C);
2580   FC = nullptr;
2581 }
2582 
2583 void ASTDumper::dumpComment(const Comment *C) {
2584   dumpChild([=] {
2585     if (!C) {
2586       ColorScope Color(*this, NullColor);
2587       OS << "<<<NULL>>>";
2588       return;
2589     }
2590 
2591     {
2592       ColorScope Color(*this, CommentColor);
2593       OS << C->getCommentKindName();
2594     }
2595     dumpPointer(C);
2596     dumpSourceRange(C->getSourceRange());
2597     ConstCommentVisitor<ASTDumper>::visit(C);
2598     for (Comment::child_iterator I = C->child_begin(), E = C->child_end();
2599          I != E; ++I)
2600       dumpComment(*I);
2601   });
2602 }
2603 
2604 void ASTDumper::visitTextComment(const TextComment *C) {
2605   OS << " Text=\"" << C->getText() << "\"";
2606 }
2607 
2608 void ASTDumper::visitInlineCommandComment(const InlineCommandComment *C) {
2609   OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
2610   switch (C->getRenderKind()) {
2611   case InlineCommandComment::RenderNormal:
2612     OS << " RenderNormal";
2613     break;
2614   case InlineCommandComment::RenderBold:
2615     OS << " RenderBold";
2616     break;
2617   case InlineCommandComment::RenderMonospaced:
2618     OS << " RenderMonospaced";
2619     break;
2620   case InlineCommandComment::RenderEmphasized:
2621     OS << " RenderEmphasized";
2622     break;
2623   }
2624 
2625   for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
2626     OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
2627 }
2628 
2629 void ASTDumper::visitHTMLStartTagComment(const HTMLStartTagComment *C) {
2630   OS << " Name=\"" << C->getTagName() << "\"";
2631   if (C->getNumAttrs() != 0) {
2632     OS << " Attrs: ";
2633     for (unsigned i = 0, e = C->getNumAttrs(); i != e; ++i) {
2634       const HTMLStartTagComment::Attribute &Attr = C->getAttr(i);
2635       OS << " \"" << Attr.Name << "=\"" << Attr.Value << "\"";
2636     }
2637   }
2638   if (C->isSelfClosing())
2639     OS << " SelfClosing";
2640 }
2641 
2642 void ASTDumper::visitHTMLEndTagComment(const HTMLEndTagComment *C) {
2643   OS << " Name=\"" << C->getTagName() << "\"";
2644 }
2645 
2646 void ASTDumper::visitBlockCommandComment(const BlockCommandComment *C) {
2647   OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
2648   for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
2649     OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
2650 }
2651 
2652 void ASTDumper::visitParamCommandComment(const ParamCommandComment *C) {
2653   OS << " " << ParamCommandComment::getDirectionAsString(C->getDirection());
2654 
2655   if (C->isDirectionExplicit())
2656     OS << " explicitly";
2657   else
2658     OS << " implicitly";
2659 
2660   if (C->hasParamName()) {
2661     if (C->isParamIndexValid())
2662       OS << " Param=\"" << C->getParamName(FC) << "\"";
2663     else
2664       OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
2665   }
2666 
2667   if (C->isParamIndexValid() && !C->isVarArgParam())
2668     OS << " ParamIndex=" << C->getParamIndex();
2669 }
2670 
2671 void ASTDumper::visitTParamCommandComment(const TParamCommandComment *C) {
2672   if (C->hasParamName()) {
2673     if (C->isPositionValid())
2674       OS << " Param=\"" << C->getParamName(FC) << "\"";
2675     else
2676       OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
2677   }
2678 
2679   if (C->isPositionValid()) {
2680     OS << " Position=<";
2681     for (unsigned i = 0, e = C->getDepth(); i != e; ++i) {
2682       OS << C->getIndex(i);
2683       if (i != e - 1)
2684         OS << ", ";
2685     }
2686     OS << ">";
2687   }
2688 }
2689 
2690 void ASTDumper::visitVerbatimBlockComment(const VerbatimBlockComment *C) {
2691   OS << " Name=\"" << getCommandName(C->getCommandID()) << "\""
2692         " CloseName=\"" << C->getCloseName() << "\"";
2693 }
2694 
2695 void ASTDumper::visitVerbatimBlockLineComment(
2696     const VerbatimBlockLineComment *C) {
2697   OS << " Text=\"" << C->getText() << "\"";
2698 }
2699 
2700 void ASTDumper::visitVerbatimLineComment(const VerbatimLineComment *C) {
2701   OS << " Text=\"" << C->getText() << "\"";
2702 }
2703 
2704 //===----------------------------------------------------------------------===//
2705 // Type method implementations
2706 //===----------------------------------------------------------------------===//
2707 
2708 void QualType::dump(const char *msg) const {
2709   if (msg)
2710     llvm::errs() << msg << ": ";
2711   dump();
2712 }
2713 
2714 LLVM_DUMP_METHOD void QualType::dump() const { dump(llvm::errs()); }
2715 
2716 LLVM_DUMP_METHOD void QualType::dump(llvm::raw_ostream &OS) const {
2717   ASTDumper Dumper(OS, nullptr, nullptr);
2718   Dumper.dumpTypeAsChild(*this);
2719 }
2720 
2721 LLVM_DUMP_METHOD void Type::dump() const { dump(llvm::errs()); }
2722 
2723 LLVM_DUMP_METHOD void Type::dump(llvm::raw_ostream &OS) const {
2724   QualType(this, 0).dump(OS);
2725 }
2726 
2727 //===----------------------------------------------------------------------===//
2728 // Decl method implementations
2729 //===----------------------------------------------------------------------===//
2730 
2731 LLVM_DUMP_METHOD void Decl::dump() const { dump(llvm::errs()); }
2732 
2733 LLVM_DUMP_METHOD void Decl::dump(raw_ostream &OS, bool Deserialize) const {
2734   const ASTContext &Ctx = getASTContext();
2735   const SourceManager &SM = Ctx.getSourceManager();
2736   ASTDumper P(OS, &Ctx.getCommentCommandTraits(), &SM,
2737               SM.getDiagnostics().getShowColors(), Ctx.getPrintingPolicy());
2738   P.setDeserialize(Deserialize);
2739   P.dumpDecl(this);
2740 }
2741 
2742 LLVM_DUMP_METHOD void Decl::dumpColor() const {
2743   const ASTContext &Ctx = getASTContext();
2744   ASTDumper P(llvm::errs(), &Ctx.getCommentCommandTraits(),
2745               &Ctx.getSourceManager(), /*ShowColors*/ true,
2746               Ctx.getPrintingPolicy());
2747   P.dumpDecl(this);
2748 }
2749 
2750 LLVM_DUMP_METHOD void DeclContext::dumpLookups() const {
2751   dumpLookups(llvm::errs());
2752 }
2753 
2754 LLVM_DUMP_METHOD void DeclContext::dumpLookups(raw_ostream &OS,
2755                                                bool DumpDecls,
2756                                                bool Deserialize) const {
2757   const DeclContext *DC = this;
2758   while (!DC->isTranslationUnit())
2759     DC = DC->getParent();
2760   ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
2761   const SourceManager &SM = Ctx.getSourceManager();
2762   ASTDumper P(OS, &Ctx.getCommentCommandTraits(), &Ctx.getSourceManager(),
2763               SM.getDiagnostics().getShowColors(), Ctx.getPrintingPolicy());
2764   P.setDeserialize(Deserialize);
2765   P.dumpLookups(this, DumpDecls);
2766 }
2767 
2768 //===----------------------------------------------------------------------===//
2769 // Stmt method implementations
2770 //===----------------------------------------------------------------------===//
2771 
2772 LLVM_DUMP_METHOD void Stmt::dump(SourceManager &SM) const {
2773   dump(llvm::errs(), SM);
2774 }
2775 
2776 LLVM_DUMP_METHOD void Stmt::dump(raw_ostream &OS, SourceManager &SM) const {
2777   ASTDumper P(OS, nullptr, &SM);
2778   P.dumpStmt(this);
2779 }
2780 
2781 LLVM_DUMP_METHOD void Stmt::dump(raw_ostream &OS) const {
2782   ASTDumper P(OS, nullptr, nullptr);
2783   P.dumpStmt(this);
2784 }
2785 
2786 LLVM_DUMP_METHOD void Stmt::dump() const {
2787   ASTDumper P(llvm::errs(), nullptr, nullptr);
2788   P.dumpStmt(this);
2789 }
2790 
2791 LLVM_DUMP_METHOD void Stmt::dumpColor() const {
2792   ASTDumper P(llvm::errs(), nullptr, nullptr, /*ShowColors*/true);
2793   P.dumpStmt(this);
2794 }
2795 
2796 //===----------------------------------------------------------------------===//
2797 // Comment method implementations
2798 //===----------------------------------------------------------------------===//
2799 
2800 LLVM_DUMP_METHOD void Comment::dump() const {
2801   dump(llvm::errs(), nullptr, nullptr);
2802 }
2803 
2804 LLVM_DUMP_METHOD void Comment::dump(const ASTContext &Context) const {
2805   dump(llvm::errs(), &Context.getCommentCommandTraits(),
2806        &Context.getSourceManager());
2807 }
2808 
2809 void Comment::dump(raw_ostream &OS, const CommandTraits *Traits,
2810                    const SourceManager *SM) const {
2811   const FullComment *FC = dyn_cast<FullComment>(this);
2812   ASTDumper D(OS, Traits, SM);
2813   D.dumpFullComment(FC);
2814 }
2815 
2816 LLVM_DUMP_METHOD void Comment::dumpColor() const {
2817   const FullComment *FC = dyn_cast<FullComment>(this);
2818   ASTDumper D(llvm::errs(), nullptr, nullptr, /*ShowColors*/true);
2819   D.dumpFullComment(FC);
2820 }
2821