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