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