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