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