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