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