blob: 76699bd56880fa75650ebec1edd9d6b081690c8d [file] [log] [blame]
Alexander Kornienko18ec81b2012-12-13 13:59:55 +00001//===--- ASTDumper.cpp - Dumping implementation for ASTs ------------------===//
Chris Lattnercbe4f772007-08-08 22:51:59 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnercbe4f772007-08-08 22:51:59 +00007//
8//===----------------------------------------------------------------------===//
9//
Alexander Kornienko18ec81b2012-12-13 13:59:55 +000010// This file implements the AST dump methods, which dump out the
Chris Lattnercbe4f772007-08-08 22:51:59 +000011// AST in a form that exposes type details and other fields.
12//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "clang/AST/ASTContext.h"
Alexander Kornienko5bc364e2013-01-07 17:53:08 +000016#include "clang/AST/Attr.h"
Alexander Kornienkoebc17b52013-01-14 14:07:11 +000017#include "clang/AST/CommentVisitor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/AST/DeclCXX.h"
Richard Smith33937e72013-06-22 21:49:40 +000019#include "clang/AST/DeclLookups.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/AST/DeclObjC.h"
Alexander Kornienko90ff6072012-12-20 02:09:13 +000021#include "clang/AST/DeclVisitor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/AST/StmtVisitor.h"
Alexander Kornienko90ff6072012-12-20 02:09:13 +000023#include "clang/Basic/Module.h"
Chris Lattner11e30d32007-08-30 06:17:34 +000024#include "clang/Basic/SourceManager.h"
Daniel Dunbar34a96c82009-12-03 09:13:13 +000025#include "llvm/Support/raw_ostream.h"
Chris Lattnercbe4f772007-08-08 22:51:59 +000026using namespace clang;
Alexander Kornienkoebc17b52013-01-14 14:07:11 +000027using namespace clang::comments;
Chris Lattnercbe4f772007-08-08 22:51:59 +000028
29//===----------------------------------------------------------------------===//
Alexander Kornienko18ec81b2012-12-13 13:59:55 +000030// ASTDumper Visitor
Chris Lattnercbe4f772007-08-08 22:51:59 +000031//===----------------------------------------------------------------------===//
32
33namespace {
Richard Trieud215b8d2013-01-26 01:31:20 +000034 // Colors used for various parts of the AST dump
Richard Trieu532018f2014-03-06 01:09:03 +000035 // Do not use bold yellow for any text. It is hard to read on white screens.
Richard Trieud215b8d2013-01-26 01:31:20 +000036
37 struct TerminalColor {
38 raw_ostream::Colors Color;
39 bool Bold;
40 };
41
Richard Trieu532018f2014-03-06 01:09:03 +000042 // Red - CastColor
43 // Green - TypeColor
44 // Bold Green - DeclKindNameColor, UndeserializedColor
45 // Yellow - AddressColor, LocationColor
46 // Blue - CommentColor, NullColor, IndentColor
47 // Bold Blue - AttrColor
48 // Bold Magenta - StmtColor
49 // Cyan - ValueKindColor, ObjectKindColor
50 // Bold Cyan - ValueColor, DeclNameColor
51
Richard Trieud215b8d2013-01-26 01:31:20 +000052 // Decl kind names (VarDecl, FunctionDecl, etc)
53 static const TerminalColor DeclKindNameColor = { raw_ostream::GREEN, true };
54 // Attr names (CleanupAttr, GuardedByAttr, etc)
55 static const TerminalColor AttrColor = { raw_ostream::BLUE, true };
56 // Statement names (DeclStmt, ImplicitCastExpr, etc)
57 static const TerminalColor StmtColor = { raw_ostream::MAGENTA, true };
58 // Comment names (FullComment, ParagraphComment, TextComment, etc)
Richard Trieu532018f2014-03-06 01:09:03 +000059 static const TerminalColor CommentColor = { raw_ostream::BLUE, false };
Richard Trieud215b8d2013-01-26 01:31:20 +000060
61 // Type names (int, float, etc, plus user defined types)
62 static const TerminalColor TypeColor = { raw_ostream::GREEN, false };
63
64 // Pointer address
65 static const TerminalColor AddressColor = { raw_ostream::YELLOW, false };
66 // Source locations
67 static const TerminalColor LocationColor = { raw_ostream::YELLOW, false };
68
69 // lvalue/xvalue
70 static const TerminalColor ValueKindColor = { raw_ostream::CYAN, false };
71 // bitfield/objcproperty/objcsubscript/vectorcomponent
72 static const TerminalColor ObjectKindColor = { raw_ostream::CYAN, false };
73
74 // Null statements
75 static const TerminalColor NullColor = { raw_ostream::BLUE, false };
76
Richard Smith1d209d02013-05-23 01:49:11 +000077 // Undeserialized entities
78 static const TerminalColor UndeserializedColor = { raw_ostream::GREEN, true };
79
Richard Trieud215b8d2013-01-26 01:31:20 +000080 // CastKind from CastExpr's
81 static const TerminalColor CastColor = { raw_ostream::RED, false };
82
83 // Value of the statement
84 static const TerminalColor ValueColor = { raw_ostream::CYAN, true };
85 // Decl names
86 static const TerminalColor DeclNameColor = { raw_ostream::CYAN, true };
87
Richard Trieude5cc7d2013-01-31 01:44:26 +000088 // Indents ( `, -. | )
89 static const TerminalColor IndentColor = { raw_ostream::BLUE, false };
90
Alexander Kornienko90ff6072012-12-20 02:09:13 +000091 class ASTDumper
Alexander Kornienko540bacb2013-02-01 12:35:51 +000092 : public ConstDeclVisitor<ASTDumper>, public ConstStmtVisitor<ASTDumper>,
Alexander Kornienkoebc17b52013-01-14 14:07:11 +000093 public ConstCommentVisitor<ASTDumper> {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000094 raw_ostream &OS;
Alexander Kornienkoebc17b52013-01-14 14:07:11 +000095 const CommandTraits *Traits;
96 const SourceManager *SM;
Manuel Klimek874030e2012-11-07 00:33:12 +000097 bool IsFirstLine;
Mike Stump11289f42009-09-09 15:08:12 +000098
Richard Trieude5cc7d2013-01-31 01:44:26 +000099 // Indicates whether more child are expected at the current tree depth
100 enum IndentType { IT_Child, IT_LastChild };
101
102 /// Indents[i] indicates if another child exists at level i.
103 /// Used by Indent() to print the tree structure.
104 llvm::SmallVector<IndentType, 32> Indents;
105
106 /// Indicates that more children will be needed at this indent level.
107 /// If true, prevents lastChild() from marking the node as the last child.
108 /// This is used when there are multiple collections of children to be
109 /// dumped as well as during conditional node dumping.
110 bool MoreChildren;
111
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000112 /// Keep track of the last location we print out so that we can
113 /// print out deltas from then on out.
Chris Lattner11e30d32007-08-30 06:17:34 +0000114 const char *LastLocFilename;
115 unsigned LastLocLine;
Douglas Gregor7de59662009-05-29 20:38:28 +0000116
Alexander Kornienkoebc17b52013-01-14 14:07:11 +0000117 /// The \c FullComment parent of the comment being dumped.
118 const FullComment *FC;
119
Richard Trieud215b8d2013-01-26 01:31:20 +0000120 bool ShowColors;
121
Manuel Klimek874030e2012-11-07 00:33:12 +0000122 class IndentScope {
Alexander Kornienko18ec81b2012-12-13 13:59:55 +0000123 ASTDumper &Dumper;
Richard Trieude5cc7d2013-01-31 01:44:26 +0000124 // Preserve the Dumper's MoreChildren value from the previous IndentScope
125 bool MoreChildren;
Manuel Klimek874030e2012-11-07 00:33:12 +0000126 public:
Alexander Kornienko18ec81b2012-12-13 13:59:55 +0000127 IndentScope(ASTDumper &Dumper) : Dumper(Dumper) {
Richard Trieude5cc7d2013-01-31 01:44:26 +0000128 MoreChildren = Dumper.hasMoreChildren();
129 Dumper.setMoreChildren(false);
Manuel Klimek874030e2012-11-07 00:33:12 +0000130 Dumper.indent();
131 }
132 ~IndentScope() {
Richard Trieude5cc7d2013-01-31 01:44:26 +0000133 Dumper.setMoreChildren(MoreChildren);
Manuel Klimek874030e2012-11-07 00:33:12 +0000134 Dumper.unindent();
135 }
136 };
137
Richard Trieud215b8d2013-01-26 01:31:20 +0000138 class ColorScope {
139 ASTDumper &Dumper;
140 public:
141 ColorScope(ASTDumper &Dumper, TerminalColor Color)
142 : Dumper(Dumper) {
143 if (Dumper.ShowColors)
144 Dumper.OS.changeColor(Color.Color, Color.Bold);
145 }
146 ~ColorScope() {
147 if (Dumper.ShowColors)
148 Dumper.OS.resetColor();
149 }
150 };
151
Richard Smithdcc2c452014-03-17 23:00:06 +0000152 class ChildDumper {
153 ASTDumper &Dumper;
154
155 const Decl *Prev;
156 bool PrevRef;
157 public:
158 ChildDumper(ASTDumper &Dumper) : Dumper(Dumper), Prev(0) {}
159 ~ChildDumper() {
160 if (Prev) {
161 Dumper.lastChild();
162 dump(0);
163 }
164 }
165
166 // FIXME: This should take an arbitrary callable as the dumping action.
167 void dump(const Decl *D, bool Ref = false) {
168 if (Prev) {
169 if (PrevRef)
170 Dumper.dumpDeclRef(Prev);
171 else
172 Dumper.dumpDecl(Prev);
173 }
174 Prev = D;
175 PrevRef = Ref;
176 }
177 void dumpRef(const Decl *D) { dump(D, true); }
178
179 // Give up ownership of the children of the node. By calling this,
180 // the caller takes back responsibility for calling lastChild().
181 void release() { dump(0); }
182 };
183
Chris Lattnercbe4f772007-08-08 22:51:59 +0000184 public:
Alexander Kornienkoebc17b52013-01-14 14:07:11 +0000185 ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
186 const SourceManager *SM)
Richard Smith56d12152013-01-31 02:04:38 +0000187 : OS(OS), Traits(Traits), SM(SM), IsFirstLine(true), MoreChildren(false),
188 LastLocFilename(""), LastLocLine(~0U), FC(0),
Richard Trieud215b8d2013-01-26 01:31:20 +0000189 ShowColors(SM && SM->getDiagnostics().getShowColors()) { }
190
191 ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
192 const SourceManager *SM, bool ShowColors)
Richard Smith56d12152013-01-31 02:04:38 +0000193 : OS(OS), Traits(Traits), SM(SM), IsFirstLine(true), MoreChildren(false),
194 LastLocFilename(""), LastLocLine(~0U),
Richard Trieude5cc7d2013-01-31 01:44:26 +0000195 ShowColors(ShowColors) { }
Mike Stump11289f42009-09-09 15:08:12 +0000196
Alexander Kornienko18ec81b2012-12-13 13:59:55 +0000197 ~ASTDumper() {
Manuel Klimek874030e2012-11-07 00:33:12 +0000198 OS << "\n";
199 }
200
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000201 void dumpDecl(const Decl *D);
202 void dumpStmt(const Stmt *S);
Alexander Kornienkoebc17b52013-01-14 14:07:11 +0000203 void dumpFullComment(const FullComment *C);
Mike Stump11289f42009-09-09 15:08:12 +0000204
Richard Trieude5cc7d2013-01-31 01:44:26 +0000205 // Formatting
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000206 void indent();
207 void unindent();
Richard Trieude5cc7d2013-01-31 01:44:26 +0000208 void lastChild();
209 bool hasMoreChildren();
210 void setMoreChildren(bool Value);
211
212 // Utilities
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000213 void dumpPointer(const void *Ptr);
214 void dumpSourceRange(SourceRange R);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000215 void dumpLocation(SourceLocation Loc);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000216 void dumpBareType(QualType T);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000217 void dumpType(QualType T);
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000218 void dumpBareDeclRef(const Decl *Node);
Alexander Kornienko9bdeb112012-12-20 12:23:54 +0000219 void dumpDeclRef(const Decl *Node, const char *Label = 0);
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000220 void dumpName(const NamedDecl *D);
Richard Trieude5cc7d2013-01-31 01:44:26 +0000221 bool hasNodes(const DeclContext *DC);
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000222 void dumpDeclContext(const DeclContext *DC);
Richard Smith33937e72013-06-22 21:49:40 +0000223 void dumpLookups(const DeclContext *DC);
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000224 void dumpAttr(const Attr *A);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000225
226 // C++ Utilities
227 void dumpAccessSpecifier(AccessSpecifier AS);
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000228 void dumpCXXCtorInitializer(const CXXCtorInitializer *Init);
229 void dumpTemplateParameters(const TemplateParameterList *TPL);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000230 void dumpTemplateArgumentListInfo(const TemplateArgumentListInfo &TALI);
231 void dumpTemplateArgumentLoc(const TemplateArgumentLoc &A);
232 void dumpTemplateArgumentList(const TemplateArgumentList &TAL);
233 void dumpTemplateArgument(const TemplateArgument &A,
234 SourceRange R = SourceRange());
235
236 // Decls
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000237 void VisitLabelDecl(const LabelDecl *D);
238 void VisitTypedefDecl(const TypedefDecl *D);
239 void VisitEnumDecl(const EnumDecl *D);
240 void VisitRecordDecl(const RecordDecl *D);
241 void VisitEnumConstantDecl(const EnumConstantDecl *D);
242 void VisitIndirectFieldDecl(const IndirectFieldDecl *D);
243 void VisitFunctionDecl(const FunctionDecl *D);
244 void VisitFieldDecl(const FieldDecl *D);
245 void VisitVarDecl(const VarDecl *D);
246 void VisitFileScopeAsmDecl(const FileScopeAsmDecl *D);
247 void VisitImportDecl(const ImportDecl *D);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000248
249 // C++ Decls
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000250 void VisitNamespaceDecl(const NamespaceDecl *D);
251 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D);
252 void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D);
253 void VisitTypeAliasDecl(const TypeAliasDecl *D);
254 void VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D);
255 void VisitCXXRecordDecl(const CXXRecordDecl *D);
256 void VisitStaticAssertDecl(const StaticAssertDecl *D);
Richard Smithcbdf7332014-03-18 02:07:28 +0000257 template<typename SpecializationDecl>
258 void VisitTemplateDeclSpecialization(ChildDumper &Children,
259 const SpecializationDecl *D,
260 bool DumpExplicitInst,
261 bool DumpRefOnly);
Richard Smith20ade552014-03-17 23:34:53 +0000262 template<typename TemplateDecl>
263 void VisitTemplateDecl(const TemplateDecl *D, bool DumpExplicitInst);
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000264 void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D);
265 void VisitClassTemplateDecl(const ClassTemplateDecl *D);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000266 void VisitClassTemplateSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000267 const ClassTemplateSpecializationDecl *D);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000268 void VisitClassTemplatePartialSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000269 const ClassTemplatePartialSpecializationDecl *D);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000270 void VisitClassScopeFunctionSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000271 const ClassScopeFunctionSpecializationDecl *D);
Richard Smithd25789a2013-09-18 01:36:02 +0000272 void VisitVarTemplateDecl(const VarTemplateDecl *D);
273 void VisitVarTemplateSpecializationDecl(
274 const VarTemplateSpecializationDecl *D);
275 void VisitVarTemplatePartialSpecializationDecl(
276 const VarTemplatePartialSpecializationDecl *D);
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000277 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D);
278 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D);
279 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D);
280 void VisitUsingDecl(const UsingDecl *D);
281 void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D);
282 void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D);
283 void VisitUsingShadowDecl(const UsingShadowDecl *D);
284 void VisitLinkageSpecDecl(const LinkageSpecDecl *D);
285 void VisitAccessSpecDecl(const AccessSpecDecl *D);
286 void VisitFriendDecl(const FriendDecl *D);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000287
288 // ObjC Decls
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000289 void VisitObjCIvarDecl(const ObjCIvarDecl *D);
290 void VisitObjCMethodDecl(const ObjCMethodDecl *D);
291 void VisitObjCCategoryDecl(const ObjCCategoryDecl *D);
292 void VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D);
293 void VisitObjCProtocolDecl(const ObjCProtocolDecl *D);
294 void VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D);
295 void VisitObjCImplementationDecl(const ObjCImplementationDecl *D);
296 void VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D);
297 void VisitObjCPropertyDecl(const ObjCPropertyDecl *D);
298 void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D);
299 void VisitBlockDecl(const BlockDecl *D);
Mike Stump11289f42009-09-09 15:08:12 +0000300
Chris Lattner84ca3762007-08-30 01:00:35 +0000301 // Stmts.
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000302 void VisitStmt(const Stmt *Node);
303 void VisitDeclStmt(const DeclStmt *Node);
304 void VisitAttributedStmt(const AttributedStmt *Node);
305 void VisitLabelStmt(const LabelStmt *Node);
306 void VisitGotoStmt(const GotoStmt *Node);
Pavel Labath1ef83422013-09-04 14:35:00 +0000307 void VisitCXXCatchStmt(const CXXCatchStmt *Node);
Mike Stump11289f42009-09-09 15:08:12 +0000308
Chris Lattner84ca3762007-08-30 01:00:35 +0000309 // Exprs
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000310 void VisitExpr(const Expr *Node);
311 void VisitCastExpr(const CastExpr *Node);
312 void VisitDeclRefExpr(const DeclRefExpr *Node);
313 void VisitPredefinedExpr(const PredefinedExpr *Node);
314 void VisitCharacterLiteral(const CharacterLiteral *Node);
315 void VisitIntegerLiteral(const IntegerLiteral *Node);
316 void VisitFloatingLiteral(const FloatingLiteral *Node);
317 void VisitStringLiteral(const StringLiteral *Str);
318 void VisitUnaryOperator(const UnaryOperator *Node);
319 void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Node);
320 void VisitMemberExpr(const MemberExpr *Node);
321 void VisitExtVectorElementExpr(const ExtVectorElementExpr *Node);
322 void VisitBinaryOperator(const BinaryOperator *Node);
323 void VisitCompoundAssignOperator(const CompoundAssignOperator *Node);
324 void VisitAddrLabelExpr(const AddrLabelExpr *Node);
325 void VisitBlockExpr(const BlockExpr *Node);
326 void VisitOpaqueValueExpr(const OpaqueValueExpr *Node);
Chris Lattner84ca3762007-08-30 01:00:35 +0000327
328 // C++
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000329 void VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node);
330 void VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node);
331 void VisitCXXThisExpr(const CXXThisExpr *Node);
332 void VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node);
333 void VisitCXXConstructExpr(const CXXConstructExpr *Node);
334 void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node);
Richard Smithe6c01442013-06-05 00:46:14 +0000335 void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node);
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000336 void VisitExprWithCleanups(const ExprWithCleanups *Node);
337 void VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node);
338 void dumpCXXTemporary(const CXXTemporary *Temporary);
Faisal Vali2b391ab2013-09-26 19:54:12 +0000339 void VisitLambdaExpr(const LambdaExpr *Node) {
340 VisitExpr(Node);
341 dumpDecl(Node->getLambdaClass());
342 }
Mike Stump11289f42009-09-09 15:08:12 +0000343
Chris Lattner84ca3762007-08-30 01:00:35 +0000344 // ObjC
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000345 void VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node);
346 void VisitObjCEncodeExpr(const ObjCEncodeExpr *Node);
347 void VisitObjCMessageExpr(const ObjCMessageExpr *Node);
348 void VisitObjCBoxedExpr(const ObjCBoxedExpr *Node);
349 void VisitObjCSelectorExpr(const ObjCSelectorExpr *Node);
350 void VisitObjCProtocolExpr(const ObjCProtocolExpr *Node);
351 void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node);
352 void VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node);
353 void VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node);
354 void VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node);
Alexander Kornienkoebc17b52013-01-14 14:07:11 +0000355
356 // Comments.
357 const char *getCommandName(unsigned CommandID);
358 void dumpComment(const Comment *C);
359
360 // Inline comments.
361 void visitTextComment(const TextComment *C);
362 void visitInlineCommandComment(const InlineCommandComment *C);
363 void visitHTMLStartTagComment(const HTMLStartTagComment *C);
364 void visitHTMLEndTagComment(const HTMLEndTagComment *C);
365
366 // Block comments.
367 void visitBlockCommandComment(const BlockCommandComment *C);
368 void visitParamCommandComment(const ParamCommandComment *C);
369 void visitTParamCommandComment(const TParamCommandComment *C);
370 void visitVerbatimBlockComment(const VerbatimBlockComment *C);
371 void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C);
372 void visitVerbatimLineComment(const VerbatimLineComment *C);
Chris Lattnercbe4f772007-08-08 22:51:59 +0000373 };
374}
375
376//===----------------------------------------------------------------------===//
Chris Lattner11e30d32007-08-30 06:17:34 +0000377// Utilities
378//===----------------------------------------------------------------------===//
379
Richard Trieude5cc7d2013-01-31 01:44:26 +0000380// Print out the appropriate tree structure using the Indents vector.
381// Example of tree and the Indents vector at each level.
382// A { }
383// |-B { IT_Child }
384// | `-C { IT_Child, IT_LastChild }
385// `-D { IT_LastChild }
386// |-E { IT_LastChild, IT_Child }
387// `-F { IT_LastChild, IT_LastChild }
388// Type non-last element, last element
389// IT_Child "| " "|-"
390// IT_LastChild " " "`-"
Alexander Kornienko18ec81b2012-12-13 13:59:55 +0000391void ASTDumper::indent() {
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000392 if (IsFirstLine)
393 IsFirstLine = false;
394 else
395 OS << "\n";
Richard Trieude5cc7d2013-01-31 01:44:26 +0000396
397 ColorScope Color(*this, IndentColor);
Craig Topper2341c0d2013-07-04 03:08:24 +0000398 for (SmallVectorImpl<IndentType>::const_iterator I = Indents.begin(),
399 E = Indents.end();
Richard Trieude5cc7d2013-01-31 01:44:26 +0000400 I != E; ++I) {
401 switch (*I) {
Richard Smith56d12152013-01-31 02:04:38 +0000402 case IT_Child:
403 if (I == E - 1)
404 OS << "|-";
405 else
406 OS << "| ";
407 continue;
408 case IT_LastChild:
409 if (I == E - 1)
410 OS << "`-";
411 else
412 OS << " ";
413 continue;
Richard Trieude5cc7d2013-01-31 01:44:26 +0000414 }
Richard Smith56d12152013-01-31 02:04:38 +0000415 llvm_unreachable("Invalid IndentType");
Richard Trieude5cc7d2013-01-31 01:44:26 +0000416 }
417 Indents.push_back(IT_Child);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000418}
419
Alexander Kornienko18ec81b2012-12-13 13:59:55 +0000420void ASTDumper::unindent() {
Richard Trieude5cc7d2013-01-31 01:44:26 +0000421 Indents.pop_back();
422}
423
424// Call before each potential last child node is to be dumped. If MoreChildren
425// is false, then this is the last child, otherwise treat as a regular node.
426void ASTDumper::lastChild() {
427 if (!hasMoreChildren())
428 Indents.back() = IT_LastChild;
429}
430
431// MoreChildren should be set before calling another function that may print
432// additional nodes to prevent conflicting final child nodes.
433bool ASTDumper::hasMoreChildren() {
434 return MoreChildren;
435}
436
437void ASTDumper::setMoreChildren(bool Value) {
438 MoreChildren = Value;
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000439}
440
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000441void ASTDumper::dumpPointer(const void *Ptr) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000442 ColorScope Color(*this, AddressColor);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000443 OS << ' ' << Ptr;
444}
445
Alexander Kornienko18ec81b2012-12-13 13:59:55 +0000446void ASTDumper::dumpLocation(SourceLocation Loc) {
Alex McCarthye14ddef2014-05-02 20:24:11 +0000447 if (!SM)
448 return;
449
Richard Trieud215b8d2013-01-26 01:31:20 +0000450 ColorScope Color(*this, LocationColor);
Chris Lattner53e384f2009-01-16 07:00:02 +0000451 SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000452
Chris Lattner11e30d32007-08-30 06:17:34 +0000453 // The general format we print out is filename:line:col, but we drop pieces
454 // that haven't changed since the last loc printed.
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000455 PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
456
Douglas Gregor453b0122010-11-12 07:15:47 +0000457 if (PLoc.isInvalid()) {
458 OS << "<invalid sloc>";
459 return;
460 }
461
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000462 if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000463 OS << PLoc.getFilename() << ':' << PLoc.getLine()
464 << ':' << PLoc.getColumn();
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000465 LastLocFilename = PLoc.getFilename();
466 LastLocLine = PLoc.getLine();
467 } else if (PLoc.getLine() != LastLocLine) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000468 OS << "line" << ':' << PLoc.getLine()
469 << ':' << PLoc.getColumn();
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000470 LastLocLine = PLoc.getLine();
Chris Lattner11e30d32007-08-30 06:17:34 +0000471 } else {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000472 OS << "col" << ':' << PLoc.getColumn();
Chris Lattner11e30d32007-08-30 06:17:34 +0000473 }
474}
475
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000476void ASTDumper::dumpSourceRange(SourceRange R) {
Chris Lattner11e30d32007-08-30 06:17:34 +0000477 // Can't translate locations if a SourceManager isn't available.
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000478 if (!SM)
479 return;
Mike Stump11289f42009-09-09 15:08:12 +0000480
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000481 OS << " <";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000482 dumpLocation(R.getBegin());
Chris Lattnera7c19fe2007-10-16 22:36:42 +0000483 if (R.getBegin() != R.getEnd()) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000484 OS << ", ";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000485 dumpLocation(R.getEnd());
Chris Lattner11e30d32007-08-30 06:17:34 +0000486 }
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000487 OS << ">";
Mike Stump11289f42009-09-09 15:08:12 +0000488
Chris Lattner11e30d32007-08-30 06:17:34 +0000489 // <t2.c:123:421[blah], t2.c:412:321>
490
491}
492
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000493void ASTDumper::dumpBareType(QualType T) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000494 ColorScope Color(*this, TypeColor);
495
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000496 SplitQualType T_split = T.split();
497 OS << "'" << QualType::getAsString(T_split) << "'";
498
499 if (!T.isNull()) {
500 // If the type is sugared, also dump a (shallow) desugared type.
501 SplitQualType D_split = T.getSplitDesugaredType();
502 if (T_split != D_split)
503 OS << ":'" << QualType::getAsString(D_split) << "'";
504 }
505}
506
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000507void ASTDumper::dumpType(QualType T) {
508 OS << ' ';
509 dumpBareType(T);
510}
511
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000512void ASTDumper::dumpBareDeclRef(const Decl *D) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000513 {
514 ColorScope Color(*this, DeclKindNameColor);
515 OS << D->getDeclKindName();
516 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000517 dumpPointer(D);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000518
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000519 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000520 ColorScope Color(*this, DeclNameColor);
David Blaikied4da8722013-05-14 21:04:00 +0000521 OS << " '" << ND->getDeclName() << '\'';
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000522 }
523
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000524 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000525 dumpType(VD->getType());
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000526}
527
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000528void ASTDumper::dumpDeclRef(const Decl *D, const char *Label) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000529 if (!D)
530 return;
531
532 IndentScope Indent(*this);
533 if (Label)
534 OS << Label << ' ';
535 dumpBareDeclRef(D);
536}
537
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000538void ASTDumper::dumpName(const NamedDecl *ND) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000539 if (ND->getDeclName()) {
540 ColorScope Color(*this, DeclNameColor);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000541 OS << ' ' << ND->getNameAsString();
Richard Trieud215b8d2013-01-26 01:31:20 +0000542 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000543}
544
Richard Trieude5cc7d2013-01-31 01:44:26 +0000545bool ASTDumper::hasNodes(const DeclContext *DC) {
546 if (!DC)
547 return false;
548
Richard Smith1d209d02013-05-23 01:49:11 +0000549 return DC->hasExternalLexicalStorage() ||
550 DC->noload_decls_begin() != DC->noload_decls_end();
Richard Trieude5cc7d2013-01-31 01:44:26 +0000551}
552
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000553void ASTDumper::dumpDeclContext(const DeclContext *DC) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000554 if (!DC)
555 return;
Richard Smithdcc2c452014-03-17 23:00:06 +0000556
557 ChildDumper Children(*this);
558 for (auto *D : DC->noload_decls())
559 Children.dump(D);
560
561 if (DC->hasExternalLexicalStorage()) {
562 Children.release();
563
Richard Smith1d209d02013-05-23 01:49:11 +0000564 lastChild();
565 IndentScope Indent(*this);
566 ColorScope Color(*this, UndeserializedColor);
567 OS << "<undeserialized declarations>";
568 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000569}
570
Richard Smith33937e72013-06-22 21:49:40 +0000571void ASTDumper::dumpLookups(const DeclContext *DC) {
572 IndentScope Indent(*this);
573
574 OS << "StoredDeclsMap ";
575 dumpBareDeclRef(cast<Decl>(DC));
576
577 const DeclContext *Primary = DC->getPrimaryContext();
578 if (Primary != DC) {
579 OS << " primary";
580 dumpPointer(cast<Decl>(Primary));
581 }
582
583 bool HasUndeserializedLookups = Primary->hasExternalVisibleStorage();
584
585 DeclContext::all_lookups_iterator I = Primary->noload_lookups_begin(),
586 E = Primary->noload_lookups_end();
587 while (I != E) {
588 DeclarationName Name = I.getLookupName();
589 DeclContextLookupResult R = *I++;
590 if (I == E && !HasUndeserializedLookups)
591 lastChild();
592
593 IndentScope Indent(*this);
594 OS << "DeclarationName ";
595 {
596 ColorScope Color(*this, DeclNameColor);
597 OS << '\'' << Name << '\'';
598 }
599
600 for (DeclContextLookupResult::iterator RI = R.begin(), RE = R.end();
601 RI != RE; ++RI) {
602 if (RI + 1 == RE)
603 lastChild();
604 dumpDeclRef(*RI);
Richard Smith15fc7df2013-10-22 23:50:38 +0000605 if ((*RI)->isHidden())
606 OS << " hidden";
Richard Smith33937e72013-06-22 21:49:40 +0000607 }
608 }
609
610 if (HasUndeserializedLookups) {
611 lastChild();
612 IndentScope Indent(*this);
613 ColorScope Color(*this, UndeserializedColor);
614 OS << "<undeserialized lookups>";
615 }
616}
617
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000618void ASTDumper::dumpAttr(const Attr *A) {
619 IndentScope Indent(*this);
Richard Trieud215b8d2013-01-26 01:31:20 +0000620 {
621 ColorScope Color(*this, AttrColor);
Aaron Ballman36a53502014-01-16 13:03:14 +0000622
Richard Trieud215b8d2013-01-26 01:31:20 +0000623 switch (A->getKind()) {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000624#define ATTR(X) case attr::X: OS << #X; break;
625#include "clang/Basic/AttrList.inc"
Richard Trieud215b8d2013-01-26 01:31:20 +0000626 default: llvm_unreachable("unexpected attribute kind");
627 }
628 OS << "Attr";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000629 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000630 dumpPointer(A);
631 dumpSourceRange(A->getRange());
632#include "clang/AST/AttrDump.inc"
Aaron Ballman36a53502014-01-16 13:03:14 +0000633 if (A->isImplicit())
634 OS << " Implicit";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000635}
636
Richard Smith71bec062013-10-15 21:58:30 +0000637static void dumpPreviousDeclImpl(raw_ostream &OS, ...) {}
638
639template<typename T>
640static void dumpPreviousDeclImpl(raw_ostream &OS, const Mergeable<T> *D) {
Rafael Espindola8db352d2013-10-17 15:37:26 +0000641 const T *First = D->getFirstDecl();
Richard Smith71bec062013-10-15 21:58:30 +0000642 if (First != D)
643 OS << " first " << First;
Richard Smithf5f43542013-02-07 01:35:44 +0000644}
645
646template<typename T>
Richard Smith71bec062013-10-15 21:58:30 +0000647static void dumpPreviousDeclImpl(raw_ostream &OS, const Redeclarable<T> *D) {
648 const T *Prev = D->getPreviousDecl();
649 if (Prev)
650 OS << " prev " << Prev;
Richard Smithf5f43542013-02-07 01:35:44 +0000651}
652
Richard Smith71bec062013-10-15 21:58:30 +0000653/// Dump the previous declaration in the redeclaration chain for a declaration,
654/// if any.
655static void dumpPreviousDecl(raw_ostream &OS, const Decl *D) {
Richard Smithf5f43542013-02-07 01:35:44 +0000656 switch (D->getKind()) {
657#define DECL(DERIVED, BASE) \
658 case Decl::DERIVED: \
Richard Smith71bec062013-10-15 21:58:30 +0000659 return dumpPreviousDeclImpl(OS, cast<DERIVED##Decl>(D));
Richard Smithf5f43542013-02-07 01:35:44 +0000660#define ABSTRACT_DECL(DECL)
661#include "clang/AST/DeclNodes.inc"
662 }
663 llvm_unreachable("Decl that isn't part of DeclNodes.inc!");
664}
665
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000666//===----------------------------------------------------------------------===//
667// C++ Utilities
668//===----------------------------------------------------------------------===//
669
670void ASTDumper::dumpAccessSpecifier(AccessSpecifier AS) {
671 switch (AS) {
672 case AS_none:
673 break;
674 case AS_public:
675 OS << "public";
676 break;
677 case AS_protected:
678 OS << "protected";
679 break;
680 case AS_private:
681 OS << "private";
682 break;
683 }
684}
685
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000686void ASTDumper::dumpCXXCtorInitializer(const CXXCtorInitializer *Init) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000687 IndentScope Indent(*this);
688 OS << "CXXCtorInitializer";
689 if (Init->isAnyMemberInitializer()) {
690 OS << ' ';
691 dumpBareDeclRef(Init->getAnyMember());
692 } else {
693 dumpType(QualType(Init->getBaseClass(), 0));
694 }
695 dumpStmt(Init->getInit());
696}
697
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000698void ASTDumper::dumpTemplateParameters(const TemplateParameterList *TPL) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000699 if (!TPL)
700 return;
701
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000702 for (TemplateParameterList::const_iterator I = TPL->begin(), E = TPL->end();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000703 I != E; ++I)
704 dumpDecl(*I);
705}
706
707void ASTDumper::dumpTemplateArgumentListInfo(
708 const TemplateArgumentListInfo &TALI) {
Richard Trieude5cc7d2013-01-31 01:44:26 +0000709 for (unsigned i = 0, e = TALI.size(); i < e; ++i) {
710 if (i + 1 == e)
711 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000712 dumpTemplateArgumentLoc(TALI[i]);
Richard Trieude5cc7d2013-01-31 01:44:26 +0000713 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000714}
715
716void ASTDumper::dumpTemplateArgumentLoc(const TemplateArgumentLoc &A) {
717 dumpTemplateArgument(A.getArgument(), A.getSourceRange());
718}
719
720void ASTDumper::dumpTemplateArgumentList(const TemplateArgumentList &TAL) {
721 for (unsigned i = 0, e = TAL.size(); i < e; ++i)
722 dumpTemplateArgument(TAL[i]);
723}
724
725void ASTDumper::dumpTemplateArgument(const TemplateArgument &A, SourceRange R) {
726 IndentScope Indent(*this);
727 OS << "TemplateArgument";
728 if (R.isValid())
729 dumpSourceRange(R);
730
731 switch (A.getKind()) {
732 case TemplateArgument::Null:
733 OS << " null";
734 break;
735 case TemplateArgument::Type:
736 OS << " type";
Richard Trieude5cc7d2013-01-31 01:44:26 +0000737 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000738 dumpType(A.getAsType());
739 break;
740 case TemplateArgument::Declaration:
741 OS << " decl";
Richard Trieude5cc7d2013-01-31 01:44:26 +0000742 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000743 dumpDeclRef(A.getAsDecl());
744 break;
745 case TemplateArgument::NullPtr:
746 OS << " nullptr";
747 break;
748 case TemplateArgument::Integral:
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000749 OS << " integral " << A.getAsIntegral();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000750 break;
751 case TemplateArgument::Template:
752 OS << " template ";
753 A.getAsTemplate().dump(OS);
754 break;
755 case TemplateArgument::TemplateExpansion:
756 OS << " template expansion";
757 A.getAsTemplateOrTemplatePattern().dump(OS);
758 break;
759 case TemplateArgument::Expression:
760 OS << " expr";
Richard Trieude5cc7d2013-01-31 01:44:26 +0000761 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000762 dumpStmt(A.getAsExpr());
763 break;
764 case TemplateArgument::Pack:
765 OS << " pack";
766 for (TemplateArgument::pack_iterator I = A.pack_begin(), E = A.pack_end();
Richard Trieude5cc7d2013-01-31 01:44:26 +0000767 I != E; ++I) {
768 if (I + 1 == E)
769 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000770 dumpTemplateArgument(*I);
Richard Trieude5cc7d2013-01-31 01:44:26 +0000771 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000772 break;
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000773 }
774}
775
Chris Lattner11e30d32007-08-30 06:17:34 +0000776//===----------------------------------------------------------------------===//
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000777// Decl dumping methods.
Chris Lattnercbe4f772007-08-08 22:51:59 +0000778//===----------------------------------------------------------------------===//
779
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000780void ASTDumper::dumpDecl(const Decl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000781 IndentScope Indent(*this);
Mike Stump11289f42009-09-09 15:08:12 +0000782
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000783 if (!D) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000784 ColorScope Color(*this, NullColor);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000785 OS << "<<<NULL>>>";
786 return;
Chris Lattnercbe4f772007-08-08 22:51:59 +0000787 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000788
Richard Trieud215b8d2013-01-26 01:31:20 +0000789 {
790 ColorScope Color(*this, DeclKindNameColor);
791 OS << D->getDeclKindName() << "Decl";
792 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000793 dumpPointer(D);
Richard Smithf5f43542013-02-07 01:35:44 +0000794 if (D->getLexicalDeclContext() != D->getDeclContext())
795 OS << " parent " << cast<Decl>(D->getDeclContext());
Richard Smith71bec062013-10-15 21:58:30 +0000796 dumpPreviousDecl(OS, D);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000797 dumpSourceRange(D->getSourceRange());
David Blaikie5ee3d002014-04-02 05:48:29 +0000798 OS << ' ';
799 dumpLocation(D->getLocation());
Richard Smith15fc7df2013-10-22 23:50:38 +0000800 if (Module *M = D->getOwningModule())
801 OS << " in " << M->getFullModuleName();
802 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
803 if (ND->isHidden())
804 OS << " hidden";
Richard Trieude5cc7d2013-01-31 01:44:26 +0000805
Aaron Ballmanb97112e2014-03-08 22:19:01 +0000806 bool HasAttrs = D->hasAttrs();
Richard Smithb39b9d52013-05-21 05:24:00 +0000807 const FullComment *Comment =
808 D->getASTContext().getLocalCommentForDeclUncached(D);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000809 // Decls within functions are visited by the body
Richard Trieude5cc7d2013-01-31 01:44:26 +0000810 bool HasDeclContext = !isa<FunctionDecl>(*D) && !isa<ObjCMethodDecl>(*D) &&
811 hasNodes(dyn_cast<DeclContext>(D));
812
Richard Smithb39b9d52013-05-21 05:24:00 +0000813 setMoreChildren(HasAttrs || Comment || HasDeclContext);
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000814 ConstDeclVisitor<ASTDumper>::Visit(D);
Richard Trieude5cc7d2013-01-31 01:44:26 +0000815
Richard Smithb39b9d52013-05-21 05:24:00 +0000816 setMoreChildren(Comment || HasDeclContext);
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000817 for (Decl::attr_iterator I = D->attr_begin(), E = D->attr_end();
818 I != E; ++I) {
819 if (I + 1 == E)
820 lastChild();
821 dumpAttr(*I);
Richard Trieude5cc7d2013-01-31 01:44:26 +0000822 }
823
824 setMoreChildren(HasDeclContext);
825 lastChild();
Richard Smithb39b9d52013-05-21 05:24:00 +0000826 dumpFullComment(Comment);
Richard Trieude5cc7d2013-01-31 01:44:26 +0000827
Nick Lewyckya382db02013-08-27 03:15:56 +0000828 if (D->isInvalidDecl())
829 OS << " invalid";
830
Richard Trieude5cc7d2013-01-31 01:44:26 +0000831 setMoreChildren(false);
832 if (HasDeclContext)
Richard Smithf5f43542013-02-07 01:35:44 +0000833 dumpDeclContext(cast<DeclContext>(D));
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000834}
835
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000836void ASTDumper::VisitLabelDecl(const LabelDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000837 dumpName(D);
838}
839
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000840void ASTDumper::VisitTypedefDecl(const TypedefDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000841 dumpName(D);
842 dumpType(D->getUnderlyingType());
843 if (D->isModulePrivate())
844 OS << " __module_private__";
845}
846
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000847void ASTDumper::VisitEnumDecl(const EnumDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000848 if (D->isScoped()) {
849 if (D->isScopedUsingClassTag())
850 OS << " class";
851 else
852 OS << " struct";
853 }
854 dumpName(D);
855 if (D->isModulePrivate())
856 OS << " __module_private__";
857 if (D->isFixed())
858 dumpType(D->getIntegerType());
859}
860
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000861void ASTDumper::VisitRecordDecl(const RecordDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000862 OS << ' ' << D->getKindName();
863 dumpName(D);
864 if (D->isModulePrivate())
865 OS << " __module_private__";
Richard Smith99bc1b92013-08-30 05:32:29 +0000866 if (D->isCompleteDefinition())
867 OS << " definition";
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000868}
869
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000870void ASTDumper::VisitEnumConstantDecl(const EnumConstantDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000871 dumpName(D);
872 dumpType(D->getType());
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000873 if (const Expr *Init = D->getInitExpr()) {
Richard Trieude5cc7d2013-01-31 01:44:26 +0000874 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000875 dumpStmt(Init);
Richard Trieude5cc7d2013-01-31 01:44:26 +0000876 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000877}
878
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000879void ASTDumper::VisitIndirectFieldDecl(const IndirectFieldDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000880 dumpName(D);
881 dumpType(D->getType());
Richard Smithdcc2c452014-03-17 23:00:06 +0000882
883 ChildDumper Children(*this);
Richard Smith8aa49222014-03-18 00:35:12 +0000884 for (auto *Child : D->chain())
885 Children.dumpRef(Child);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000886}
887
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000888void ASTDumper::VisitFunctionDecl(const FunctionDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000889 dumpName(D);
890 dumpType(D->getType());
891
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000892 StorageClass SC = D->getStorageClass();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000893 if (SC != SC_None)
894 OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
895 if (D->isInlineSpecified())
896 OS << " inline";
897 if (D->isVirtualAsWritten())
898 OS << " virtual";
899 if (D->isModulePrivate())
900 OS << " __module_private__";
901
902 if (D->isPure())
903 OS << " pure";
904 else if (D->isDeletedAsWritten())
905 OS << " delete";
906
Richard Smithadaa0152013-05-17 02:09:46 +0000907 if (const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>()) {
908 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
909 switch (EPI.ExceptionSpecType) {
910 default: break;
911 case EST_Unevaluated:
912 OS << " noexcept-unevaluated " << EPI.ExceptionSpecDecl;
913 break;
914 case EST_Uninstantiated:
915 OS << " noexcept-uninstantiated " << EPI.ExceptionSpecTemplate;
916 break;
917 }
918 }
919
Richard Trieude5cc7d2013-01-31 01:44:26 +0000920 bool OldMoreChildren = hasMoreChildren();
921 const FunctionTemplateSpecializationInfo *FTSI =
922 D->getTemplateSpecializationInfo();
923 bool HasTemplateSpecialization = FTSI;
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000924
Richard Trieude5cc7d2013-01-31 01:44:26 +0000925 bool HasNamedDecls = D->getDeclsInPrototypeScope().begin() !=
926 D->getDeclsInPrototypeScope().end();
927
928 bool HasFunctionDecls = D->param_begin() != D->param_end();
929
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000930 const CXXConstructorDecl *C = dyn_cast<CXXConstructorDecl>(D);
Richard Trieude5cc7d2013-01-31 01:44:26 +0000931 bool HasCtorInitializers = C && C->init_begin() != C->init_end();
932
933 bool HasDeclarationBody = D->doesThisDeclarationHaveABody();
934
935 setMoreChildren(OldMoreChildren || HasNamedDecls || HasFunctionDecls ||
936 HasCtorInitializers || HasDeclarationBody);
937 if (HasTemplateSpecialization) {
938 lastChild();
939 dumpTemplateArgumentList(*FTSI->TemplateArguments);
940 }
941
942 setMoreChildren(OldMoreChildren || HasFunctionDecls ||
943 HasCtorInitializers || HasDeclarationBody);
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000944 for (ArrayRef<NamedDecl *>::iterator
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000945 I = D->getDeclsInPrototypeScope().begin(),
Richard Trieude5cc7d2013-01-31 01:44:26 +0000946 E = D->getDeclsInPrototypeScope().end(); I != E; ++I) {
947 if (I + 1 == E)
948 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000949 dumpDecl(*I);
Richard Trieude5cc7d2013-01-31 01:44:26 +0000950 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000951
Richard Trieude5cc7d2013-01-31 01:44:26 +0000952 setMoreChildren(OldMoreChildren || HasCtorInitializers || HasDeclarationBody);
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000953 for (FunctionDecl::param_const_iterator I = D->param_begin(),
954 E = D->param_end();
Richard Trieude5cc7d2013-01-31 01:44:26 +0000955 I != E; ++I) {
956 if (I + 1 == E)
957 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000958 dumpDecl(*I);
Richard Trieude5cc7d2013-01-31 01:44:26 +0000959 }
960
961 setMoreChildren(OldMoreChildren || HasDeclarationBody);
962 if (HasCtorInitializers)
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000963 for (CXXConstructorDecl::init_const_iterator I = C->init_begin(),
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000964 E = C->init_end();
965 I != E; ++I) {
Richard Trieude5cc7d2013-01-31 01:44:26 +0000966 if (I + 1 == E)
967 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000968 dumpCXXCtorInitializer(*I);
Richard Trieude5cc7d2013-01-31 01:44:26 +0000969 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000970
Richard Trieude5cc7d2013-01-31 01:44:26 +0000971 setMoreChildren(OldMoreChildren);
972 if (HasDeclarationBody) {
973 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000974 dumpStmt(D->getBody());
Richard Trieude5cc7d2013-01-31 01:44:26 +0000975 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000976}
977
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000978void ASTDumper::VisitFieldDecl(const FieldDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000979 dumpName(D);
980 dumpType(D->getType());
981 if (D->isMutable())
982 OS << " mutable";
983 if (D->isModulePrivate())
984 OS << " __module_private__";
Richard Trieude5cc7d2013-01-31 01:44:26 +0000985
986 bool OldMoreChildren = hasMoreChildren();
987 bool IsBitField = D->isBitField();
988 Expr *Init = D->getInClassInitializer();
989 bool HasInit = Init;
990
991 setMoreChildren(OldMoreChildren || HasInit);
992 if (IsBitField) {
993 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000994 dumpStmt(D->getBitWidth());
Richard Trieude5cc7d2013-01-31 01:44:26 +0000995 }
996 setMoreChildren(OldMoreChildren);
997 if (HasInit) {
998 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000999 dumpStmt(Init);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001000 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001001}
1002
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001003void ASTDumper::VisitVarDecl(const VarDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001004 dumpName(D);
1005 dumpType(D->getType());
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001006 StorageClass SC = D->getStorageClass();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001007 if (SC != SC_None)
1008 OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
Richard Smithfd3834f2013-04-13 02:43:54 +00001009 switch (D->getTLSKind()) {
1010 case VarDecl::TLS_None: break;
1011 case VarDecl::TLS_Static: OS << " tls"; break;
1012 case VarDecl::TLS_Dynamic: OS << " tls_dynamic"; break;
1013 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001014 if (D->isModulePrivate())
1015 OS << " __module_private__";
1016 if (D->isNRVOVariable())
1017 OS << " nrvo";
Richard Trieude5cc7d2013-01-31 01:44:26 +00001018 if (D->hasInit()) {
1019 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001020 dumpStmt(D->getInit());
Richard Trieude5cc7d2013-01-31 01:44:26 +00001021 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001022}
1023
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001024void ASTDumper::VisitFileScopeAsmDecl(const FileScopeAsmDecl *D) {
Richard Trieude5cc7d2013-01-31 01:44:26 +00001025 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001026 dumpStmt(D->getAsmString());
1027}
1028
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001029void ASTDumper::VisitImportDecl(const ImportDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001030 OS << ' ' << D->getImportedModule()->getFullModuleName();
1031}
1032
1033//===----------------------------------------------------------------------===//
1034// C++ Declarations
1035//===----------------------------------------------------------------------===//
1036
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001037void ASTDumper::VisitNamespaceDecl(const NamespaceDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001038 dumpName(D);
1039 if (D->isInline())
1040 OS << " inline";
1041 if (!D->isOriginalNamespace())
1042 dumpDeclRef(D->getOriginalNamespace(), "original");
1043}
1044
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001045void ASTDumper::VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001046 OS << ' ';
1047 dumpBareDeclRef(D->getNominatedNamespace());
1048}
1049
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001050void ASTDumper::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001051 dumpName(D);
1052 dumpDeclRef(D->getAliasedNamespace());
1053}
1054
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001055void ASTDumper::VisitTypeAliasDecl(const TypeAliasDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001056 dumpName(D);
1057 dumpType(D->getUnderlyingType());
1058}
1059
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001060void ASTDumper::VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001061 dumpName(D);
1062 dumpTemplateParameters(D->getTemplateParameters());
1063 dumpDecl(D->getTemplatedDecl());
1064}
1065
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001066void ASTDumper::VisitCXXRecordDecl(const CXXRecordDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001067 VisitRecordDecl(D);
1068 if (!D->isCompleteDefinition())
1069 return;
1070
Aaron Ballman574705e2014-03-13 15:41:46 +00001071 for (const auto &I : D->bases()) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001072 IndentScope Indent(*this);
Aaron Ballman574705e2014-03-13 15:41:46 +00001073 if (I.isVirtual())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001074 OS << "virtual ";
Aaron Ballman574705e2014-03-13 15:41:46 +00001075 dumpAccessSpecifier(I.getAccessSpecifier());
1076 dumpType(I.getType());
1077 if (I.isPackExpansion())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001078 OS << "...";
1079 }
1080}
1081
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001082void ASTDumper::VisitStaticAssertDecl(const StaticAssertDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001083 dumpStmt(D->getAssertExpr());
Richard Trieude5cc7d2013-01-31 01:44:26 +00001084 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001085 dumpStmt(D->getMessage());
1086}
1087
Richard Smithcbdf7332014-03-18 02:07:28 +00001088template<typename SpecializationDecl>
1089void ASTDumper::VisitTemplateDeclSpecialization(ChildDumper &Children,
1090 const SpecializationDecl *D,
1091 bool DumpExplicitInst,
1092 bool DumpRefOnly) {
1093 bool DumpedAny = false;
1094 for (auto *RedeclWithBadType : D->redecls()) {
1095 // FIXME: The redecls() range sometimes has elements of a less-specific
1096 // type. (In particular, ClassTemplateSpecializationDecl::redecls() gives
1097 // us TagDecls, and should give CXXRecordDecls).
1098 auto *Redecl = dyn_cast<SpecializationDecl>(RedeclWithBadType);
1099 if (!Redecl) {
1100 // Found the injected-class-name for a class template. This will be dumped
1101 // as part of its surrounding class so we don't need to dump it here.
1102 assert(isa<CXXRecordDecl>(RedeclWithBadType) &&
1103 "expected an injected-class-name");
1104 continue;
1105 }
1106
1107 switch (Redecl->getTemplateSpecializationKind()) {
1108 case TSK_ExplicitInstantiationDeclaration:
1109 case TSK_ExplicitInstantiationDefinition:
1110 if (!DumpExplicitInst)
1111 break;
1112 // Fall through.
1113 case TSK_Undeclared:
1114 case TSK_ImplicitInstantiation:
1115 Children.dump(Redecl, DumpRefOnly);
1116 DumpedAny = true;
1117 break;
1118 case TSK_ExplicitSpecialization:
1119 break;
1120 }
1121 }
1122
1123 // Ensure we dump at least one decl for each specialization.
1124 if (!DumpedAny)
1125 Children.dumpRef(D);
1126}
1127
Richard Smith20ade552014-03-17 23:34:53 +00001128template<typename TemplateDecl>
1129void ASTDumper::VisitTemplateDecl(const TemplateDecl *D,
1130 bool DumpExplicitInst) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001131 dumpName(D);
1132 dumpTemplateParameters(D->getTemplateParameters());
Richard Smithdcc2c452014-03-17 23:00:06 +00001133
1134 ChildDumper Children(*this);
1135 Children.dump(D->getTemplatedDecl());
1136
Richard Smithcbdf7332014-03-18 02:07:28 +00001137 for (auto *Child : D->specializations())
1138 VisitTemplateDeclSpecialization(Children, Child, DumpExplicitInst,
1139 !D->isCanonicalDecl());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001140}
1141
Richard Smith20ade552014-03-17 23:34:53 +00001142void ASTDumper::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
1143 // FIXME: We don't add a declaration of a function template specialization
1144 // to its context when it's explicitly instantiated, so dump explicit
1145 // instantiations when we dump the template itself.
1146 VisitTemplateDecl(D, true);
1147}
1148
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001149void ASTDumper::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
Richard Smith20ade552014-03-17 23:34:53 +00001150 VisitTemplateDecl(D, false);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001151}
1152
1153void ASTDumper::VisitClassTemplateSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001154 const ClassTemplateSpecializationDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001155 VisitCXXRecordDecl(D);
1156 dumpTemplateArgumentList(D->getTemplateArgs());
1157}
1158
1159void ASTDumper::VisitClassTemplatePartialSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001160 const ClassTemplatePartialSpecializationDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001161 VisitClassTemplateSpecializationDecl(D);
1162 dumpTemplateParameters(D->getTemplateParameters());
1163}
1164
1165void ASTDumper::VisitClassScopeFunctionSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001166 const ClassScopeFunctionSpecializationDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001167 dumpDeclRef(D->getSpecialization());
1168 if (D->hasExplicitTemplateArgs())
1169 dumpTemplateArgumentListInfo(D->templateArgs());
1170}
1171
Richard Smithd25789a2013-09-18 01:36:02 +00001172void ASTDumper::VisitVarTemplateDecl(const VarTemplateDecl *D) {
Richard Smith20ade552014-03-17 23:34:53 +00001173 VisitTemplateDecl(D, false);
Richard Smithd25789a2013-09-18 01:36:02 +00001174}
1175
1176void ASTDumper::VisitVarTemplateSpecializationDecl(
1177 const VarTemplateSpecializationDecl *D) {
1178 dumpTemplateArgumentList(D->getTemplateArgs());
1179 VisitVarDecl(D);
1180}
1181
1182void ASTDumper::VisitVarTemplatePartialSpecializationDecl(
1183 const VarTemplatePartialSpecializationDecl *D) {
1184 dumpTemplateParameters(D->getTemplateParameters());
1185 VisitVarTemplateSpecializationDecl(D);
1186}
1187
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001188void ASTDumper::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001189 if (D->wasDeclaredWithTypename())
1190 OS << " typename";
1191 else
1192 OS << " class";
1193 if (D->isParameterPack())
1194 OS << " ...";
1195 dumpName(D);
Richard Smithecf74ff2014-03-23 20:50:39 +00001196 if (D->hasDefaultArgument()) {
1197 lastChild();
1198 dumpTemplateArgument(D->getDefaultArgument());
1199 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001200}
1201
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001202void ASTDumper::VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001203 dumpType(D->getType());
1204 if (D->isParameterPack())
1205 OS << " ...";
1206 dumpName(D);
Richard Smithecf74ff2014-03-23 20:50:39 +00001207 if (D->hasDefaultArgument()) {
1208 lastChild();
1209 dumpTemplateArgument(D->getDefaultArgument());
1210 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001211}
1212
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001213void ASTDumper::VisitTemplateTemplateParmDecl(
1214 const TemplateTemplateParmDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001215 if (D->isParameterPack())
1216 OS << " ...";
1217 dumpName(D);
1218 dumpTemplateParameters(D->getTemplateParameters());
Richard Smithecf74ff2014-03-23 20:50:39 +00001219 if (D->hasDefaultArgument()) {
1220 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001221 dumpTemplateArgumentLoc(D->getDefaultArgument());
Richard Smithecf74ff2014-03-23 20:50:39 +00001222 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001223}
1224
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001225void ASTDumper::VisitUsingDecl(const UsingDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001226 OS << ' ';
1227 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1228 OS << D->getNameAsString();
1229}
1230
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001231void ASTDumper::VisitUnresolvedUsingTypenameDecl(
1232 const UnresolvedUsingTypenameDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001233 OS << ' ';
1234 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1235 OS << D->getNameAsString();
1236}
1237
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001238void ASTDumper::VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001239 OS << ' ';
1240 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1241 OS << D->getNameAsString();
1242 dumpType(D->getType());
1243}
1244
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001245void ASTDumper::VisitUsingShadowDecl(const UsingShadowDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001246 OS << ' ';
1247 dumpBareDeclRef(D->getTargetDecl());
1248}
1249
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001250void ASTDumper::VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001251 switch (D->getLanguage()) {
1252 case LinkageSpecDecl::lang_c: OS << " C"; break;
1253 case LinkageSpecDecl::lang_cxx: OS << " C++"; break;
1254 }
1255}
1256
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001257void ASTDumper::VisitAccessSpecDecl(const AccessSpecDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001258 OS << ' ';
1259 dumpAccessSpecifier(D->getAccess());
1260}
1261
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001262void ASTDumper::VisitFriendDecl(const FriendDecl *D) {
Richard Smithf5f43542013-02-07 01:35:44 +00001263 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001264 if (TypeSourceInfo *T = D->getFriendType())
1265 dumpType(T->getType());
1266 else
1267 dumpDecl(D->getFriendDecl());
1268}
1269
1270//===----------------------------------------------------------------------===//
1271// Obj-C Declarations
1272//===----------------------------------------------------------------------===//
1273
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001274void ASTDumper::VisitObjCIvarDecl(const ObjCIvarDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001275 dumpName(D);
1276 dumpType(D->getType());
1277 if (D->getSynthesize())
1278 OS << " synthesize";
1279
1280 switch (D->getAccessControl()) {
1281 case ObjCIvarDecl::None:
1282 OS << " none";
1283 break;
1284 case ObjCIvarDecl::Private:
1285 OS << " private";
1286 break;
1287 case ObjCIvarDecl::Protected:
1288 OS << " protected";
1289 break;
1290 case ObjCIvarDecl::Public:
1291 OS << " public";
1292 break;
1293 case ObjCIvarDecl::Package:
1294 OS << " package";
1295 break;
1296 }
1297}
1298
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001299void ASTDumper::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001300 if (D->isInstanceMethod())
1301 OS << " -";
1302 else
1303 OS << " +";
1304 dumpName(D);
Alp Toker314cc812014-01-25 16:55:45 +00001305 dumpType(D->getReturnType());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001306
Richard Trieude5cc7d2013-01-31 01:44:26 +00001307 bool OldMoreChildren = hasMoreChildren();
1308 bool IsVariadic = D->isVariadic();
1309 bool HasBody = D->hasBody();
1310
1311 setMoreChildren(OldMoreChildren || IsVariadic || HasBody);
1312 if (D->isThisDeclarationADefinition()) {
1313 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001314 dumpDeclContext(D);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001315 } else {
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001316 for (ObjCMethodDecl::param_const_iterator I = D->param_begin(),
1317 E = D->param_end();
1318 I != E; ++I) {
Richard Trieude5cc7d2013-01-31 01:44:26 +00001319 if (I + 1 == E)
1320 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001321 dumpDecl(*I);
1322 }
1323 }
1324
Richard Trieude5cc7d2013-01-31 01:44:26 +00001325 setMoreChildren(OldMoreChildren || HasBody);
1326 if (IsVariadic) {
1327 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001328 IndentScope Indent(*this);
1329 OS << "...";
1330 }
1331
Richard Trieude5cc7d2013-01-31 01:44:26 +00001332 setMoreChildren(OldMoreChildren);
1333 if (HasBody) {
1334 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001335 dumpStmt(D->getBody());
Richard Trieude5cc7d2013-01-31 01:44:26 +00001336 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001337}
1338
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001339void ASTDumper::VisitObjCCategoryDecl(const ObjCCategoryDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001340 dumpName(D);
1341 dumpDeclRef(D->getClassInterface());
Richard Trieude5cc7d2013-01-31 01:44:26 +00001342 if (D->protocol_begin() == D->protocol_end())
1343 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001344 dumpDeclRef(D->getImplementation());
1345 for (ObjCCategoryDecl::protocol_iterator I = D->protocol_begin(),
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001346 E = D->protocol_end();
1347 I != E; ++I) {
Richard Trieude5cc7d2013-01-31 01:44:26 +00001348 if (I + 1 == E)
1349 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001350 dumpDeclRef(*I);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001351 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001352}
1353
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001354void ASTDumper::VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001355 dumpName(D);
1356 dumpDeclRef(D->getClassInterface());
Richard Trieude5cc7d2013-01-31 01:44:26 +00001357 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001358 dumpDeclRef(D->getCategoryDecl());
1359}
1360
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001361void ASTDumper::VisitObjCProtocolDecl(const ObjCProtocolDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001362 dumpName(D);
Richard Smithdcc2c452014-03-17 23:00:06 +00001363
1364 ChildDumper Children(*this);
Richard Smith7fcb35f2014-03-18 02:37:59 +00001365 for (auto *Child : D->protocols())
1366 Children.dumpRef(Child);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001367}
1368
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001369void ASTDumper::VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001370 dumpName(D);
1371 dumpDeclRef(D->getSuperClass(), "super");
Richard Smithdcc2c452014-03-17 23:00:06 +00001372
1373 ChildDumper Children(*this);
1374 Children.dumpRef(D->getImplementation());
Richard Smith7fcb35f2014-03-18 02:37:59 +00001375 for (auto *Child : D->protocols())
1376 Children.dumpRef(Child);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001377}
1378
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001379void ASTDumper::VisitObjCImplementationDecl(const ObjCImplementationDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001380 dumpName(D);
1381 dumpDeclRef(D->getSuperClass(), "super");
Richard Trieude5cc7d2013-01-31 01:44:26 +00001382 if (D->init_begin() == D->init_end())
1383 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001384 dumpDeclRef(D->getClassInterface());
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001385 for (ObjCImplementationDecl::init_const_iterator I = D->init_begin(),
1386 E = D->init_end();
1387 I != E; ++I) {
Richard Trieude5cc7d2013-01-31 01:44:26 +00001388 if (I + 1 == E)
1389 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001390 dumpCXXCtorInitializer(*I);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001391 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001392}
1393
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001394void ASTDumper::VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001395 dumpName(D);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001396 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001397 dumpDeclRef(D->getClassInterface());
1398}
1399
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001400void ASTDumper::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001401 dumpName(D);
1402 dumpType(D->getType());
1403
1404 if (D->getPropertyImplementation() == ObjCPropertyDecl::Required)
1405 OS << " required";
1406 else if (D->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1407 OS << " optional";
1408
1409 ObjCPropertyDecl::PropertyAttributeKind Attrs = D->getPropertyAttributes();
1410 if (Attrs != ObjCPropertyDecl::OBJC_PR_noattr) {
1411 if (Attrs & ObjCPropertyDecl::OBJC_PR_readonly)
1412 OS << " readonly";
1413 if (Attrs & ObjCPropertyDecl::OBJC_PR_assign)
1414 OS << " assign";
1415 if (Attrs & ObjCPropertyDecl::OBJC_PR_readwrite)
1416 OS << " readwrite";
1417 if (Attrs & ObjCPropertyDecl::OBJC_PR_retain)
1418 OS << " retain";
1419 if (Attrs & ObjCPropertyDecl::OBJC_PR_copy)
1420 OS << " copy";
1421 if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic)
1422 OS << " nonatomic";
1423 if (Attrs & ObjCPropertyDecl::OBJC_PR_atomic)
1424 OS << " atomic";
1425 if (Attrs & ObjCPropertyDecl::OBJC_PR_weak)
1426 OS << " weak";
1427 if (Attrs & ObjCPropertyDecl::OBJC_PR_strong)
1428 OS << " strong";
1429 if (Attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained)
1430 OS << " unsafe_unretained";
Richard Trieude5cc7d2013-01-31 01:44:26 +00001431 if (Attrs & ObjCPropertyDecl::OBJC_PR_getter) {
1432 if (!(Attrs & ObjCPropertyDecl::OBJC_PR_setter))
1433 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001434 dumpDeclRef(D->getGetterMethodDecl(), "getter");
Richard Trieude5cc7d2013-01-31 01:44:26 +00001435 }
1436 if (Attrs & ObjCPropertyDecl::OBJC_PR_setter) {
1437 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001438 dumpDeclRef(D->getSetterMethodDecl(), "setter");
Richard Trieude5cc7d2013-01-31 01:44:26 +00001439 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001440 }
1441}
1442
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001443void ASTDumper::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001444 dumpName(D->getPropertyDecl());
1445 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
1446 OS << " synthesize";
1447 else
1448 OS << " dynamic";
1449 dumpDeclRef(D->getPropertyDecl());
Richard Trieude5cc7d2013-01-31 01:44:26 +00001450 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001451 dumpDeclRef(D->getPropertyIvarDecl());
1452}
1453
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001454void ASTDumper::VisitBlockDecl(const BlockDecl *D) {
Aaron Ballmanb2b8b1d2014-03-07 16:09:59 +00001455 for (auto I : D->params())
1456 dumpDecl(I);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001457
1458 if (D->isVariadic()) {
1459 IndentScope Indent(*this);
1460 OS << "...";
1461 }
1462
1463 if (D->capturesCXXThis()) {
1464 IndentScope Indent(*this);
1465 OS << "capture this";
1466 }
Aaron Ballman9371dd22014-03-14 18:34:04 +00001467 for (const auto &I : D->captures()) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001468 IndentScope Indent(*this);
1469 OS << "capture";
Aaron Ballman9371dd22014-03-14 18:34:04 +00001470 if (I.isByRef())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001471 OS << " byref";
Aaron Ballman9371dd22014-03-14 18:34:04 +00001472 if (I.isNested())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001473 OS << " nested";
Aaron Ballman9371dd22014-03-14 18:34:04 +00001474 if (I.getVariable()) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001475 OS << ' ';
Aaron Ballman9371dd22014-03-14 18:34:04 +00001476 dumpBareDeclRef(I.getVariable());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001477 }
Aaron Ballman9371dd22014-03-14 18:34:04 +00001478 if (I.hasCopyExpr())
1479 dumpStmt(I.getCopyExpr());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001480 }
Richard Trieude5cc7d2013-01-31 01:44:26 +00001481 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001482 dumpStmt(D->getBody());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001483}
1484
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001485//===----------------------------------------------------------------------===//
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001486// Stmt dumping methods.
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001487//===----------------------------------------------------------------------===//
1488
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001489void ASTDumper::dumpStmt(const Stmt *S) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001490 IndentScope Indent(*this);
1491
1492 if (!S) {
Richard Trieud215b8d2013-01-26 01:31:20 +00001493 ColorScope Color(*this, NullColor);
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001494 OS << "<<<NULL>>>";
1495 return;
1496 }
1497
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001498 if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001499 VisitDeclStmt(DS);
1500 return;
1501 }
1502
David Blaikie7d170102013-05-15 07:37:26 +00001503 setMoreChildren(!S->children().empty());
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001504 ConstStmtVisitor<ASTDumper>::Visit(S);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001505 setMoreChildren(false);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001506 for (Stmt::const_child_range CI = S->children(); CI; ++CI) {
1507 Stmt::const_child_range Next = CI;
Richard Trieude5cc7d2013-01-31 01:44:26 +00001508 ++Next;
1509 if (!Next)
1510 lastChild();
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001511 dumpStmt(*CI);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001512 }
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001513}
1514
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001515void ASTDumper::VisitStmt(const Stmt *Node) {
Richard Trieud215b8d2013-01-26 01:31:20 +00001516 {
1517 ColorScope Color(*this, StmtColor);
1518 OS << Node->getStmtClassName();
1519 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001520 dumpPointer(Node);
1521 dumpSourceRange(Node->getSourceRange());
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001522}
1523
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001524void ASTDumper::VisitDeclStmt(const DeclStmt *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001525 VisitStmt(Node);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001526 for (DeclStmt::const_decl_iterator I = Node->decl_begin(),
1527 E = Node->decl_end();
Richard Trieude5cc7d2013-01-31 01:44:26 +00001528 I != E; ++I) {
1529 if (I + 1 == E)
1530 lastChild();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001531 dumpDecl(*I);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001532 }
Ted Kremenek433a4922007-12-12 06:59:42 +00001533}
1534
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001535void ASTDumper::VisitAttributedStmt(const AttributedStmt *Node) {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001536 VisitStmt(Node);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001537 for (ArrayRef<const Attr *>::iterator I = Node->getAttrs().begin(),
1538 E = Node->getAttrs().end();
1539 I != E; ++I) {
Richard Trieude5cc7d2013-01-31 01:44:26 +00001540 if (I + 1 == E)
1541 lastChild();
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001542 dumpAttr(*I);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001543 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001544}
1545
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001546void ASTDumper::VisitLabelStmt(const LabelStmt *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001547 VisitStmt(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001548 OS << " '" << Node->getName() << "'";
Chris Lattnercbe4f772007-08-08 22:51:59 +00001549}
1550
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001551void ASTDumper::VisitGotoStmt(const GotoStmt *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001552 VisitStmt(Node);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001553 OS << " '" << Node->getLabel()->getName() << "'";
1554 dumpPointer(Node->getLabel());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001555}
1556
Pavel Labath1ef83422013-09-04 14:35:00 +00001557void ASTDumper::VisitCXXCatchStmt(const CXXCatchStmt *Node) {
1558 VisitStmt(Node);
1559 dumpDecl(Node->getExceptionDecl());
1560}
1561
Chris Lattnercbe4f772007-08-08 22:51:59 +00001562//===----------------------------------------------------------------------===//
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001563// Expr dumping methods.
Chris Lattnercbe4f772007-08-08 22:51:59 +00001564//===----------------------------------------------------------------------===//
1565
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001566void ASTDumper::VisitExpr(const Expr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001567 VisitStmt(Node);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001568 dumpType(Node->getType());
1569
Richard Trieud215b8d2013-01-26 01:31:20 +00001570 {
1571 ColorScope Color(*this, ValueKindColor);
1572 switch (Node->getValueKind()) {
1573 case VK_RValue:
1574 break;
1575 case VK_LValue:
1576 OS << " lvalue";
1577 break;
1578 case VK_XValue:
1579 OS << " xvalue";
1580 break;
1581 }
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001582 }
1583
Richard Trieud215b8d2013-01-26 01:31:20 +00001584 {
1585 ColorScope Color(*this, ObjectKindColor);
1586 switch (Node->getObjectKind()) {
1587 case OK_Ordinary:
1588 break;
1589 case OK_BitField:
1590 OS << " bitfield";
1591 break;
1592 case OK_ObjCProperty:
1593 OS << " objcproperty";
1594 break;
1595 case OK_ObjCSubscript:
1596 OS << " objcsubscript";
1597 break;
1598 case OK_VectorComponent:
1599 OS << " vectorcomponent";
1600 break;
1601 }
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001602 }
Chris Lattnercbe4f772007-08-08 22:51:59 +00001603}
1604
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001605static void dumpBasePath(raw_ostream &OS, const CastExpr *Node) {
John McCallcf142162010-08-07 06:22:56 +00001606 if (Node->path_empty())
Anders Carlssona70cff62010-04-24 19:06:50 +00001607 return;
1608
1609 OS << " (";
1610 bool First = true;
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001611 for (CastExpr::path_const_iterator I = Node->path_begin(),
1612 E = Node->path_end();
1613 I != E; ++I) {
Anders Carlssona70cff62010-04-24 19:06:50 +00001614 const CXXBaseSpecifier *Base = *I;
1615 if (!First)
1616 OS << " -> ";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001617
Anders Carlssona70cff62010-04-24 19:06:50 +00001618 const CXXRecordDecl *RD =
1619 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001620
Anders Carlssona70cff62010-04-24 19:06:50 +00001621 if (Base->isVirtual())
1622 OS << "virtual ";
1623 OS << RD->getName();
1624 First = false;
1625 }
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001626
Anders Carlssona70cff62010-04-24 19:06:50 +00001627 OS << ')';
1628}
1629
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001630void ASTDumper::VisitCastExpr(const CastExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001631 VisitExpr(Node);
Richard Trieud215b8d2013-01-26 01:31:20 +00001632 OS << " <";
1633 {
1634 ColorScope Color(*this, CastColor);
1635 OS << Node->getCastKindName();
1636 }
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001637 dumpBasePath(OS, Node);
Anders Carlssona70cff62010-04-24 19:06:50 +00001638 OS << ">";
Anders Carlssond7923c62009-08-22 23:33:40 +00001639}
1640
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001641void ASTDumper::VisitDeclRefExpr(const DeclRefExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001642 VisitExpr(Node);
Ted Kremenek5f64ca82007-09-10 17:32:55 +00001643
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001644 OS << " ";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001645 dumpBareDeclRef(Node->getDecl());
Chandler Carruth8d26bb02011-05-01 23:48:14 +00001646 if (Node->getDecl() != Node->getFoundDecl()) {
1647 OS << " (";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001648 dumpBareDeclRef(Node->getFoundDecl());
Chandler Carruth8d26bb02011-05-01 23:48:14 +00001649 OS << ")";
1650 }
John McCall351762c2011-02-07 10:33:21 +00001651}
1652
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001653void ASTDumper::VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001654 VisitExpr(Node);
John McCall76d09942009-12-11 21:50:11 +00001655 OS << " (";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001656 if (!Node->requiresADL())
1657 OS << "no ";
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001658 OS << "ADL) = '" << Node->getName() << '\'';
John McCall76d09942009-12-11 21:50:11 +00001659
1660 UnresolvedLookupExpr::decls_iterator
1661 I = Node->decls_begin(), E = Node->decls_end();
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001662 if (I == E)
1663 OS << " empty";
John McCall76d09942009-12-11 21:50:11 +00001664 for (; I != E; ++I)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001665 dumpPointer(*I);
John McCall76d09942009-12-11 21:50:11 +00001666}
1667
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001668void ASTDumper::VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001669 VisitExpr(Node);
Steve Naroff5d5efca2008-03-12 13:19:12 +00001670
Richard Trieud215b8d2013-01-26 01:31:20 +00001671 {
1672 ColorScope Color(*this, DeclKindNameColor);
1673 OS << " " << Node->getDecl()->getDeclKindName() << "Decl";
1674 }
1675 OS << "='" << *Node->getDecl() << "'";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001676 dumpPointer(Node->getDecl());
Steve Naroffb3424a92008-05-23 22:01:24 +00001677 if (Node->isFreeIvar())
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001678 OS << " isFreeIvar";
Steve Naroff5d5efca2008-03-12 13:19:12 +00001679}
1680
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001681void ASTDumper::VisitPredefinedExpr(const PredefinedExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001682 VisitExpr(Node);
Chris Lattnercbe4f772007-08-08 22:51:59 +00001683 switch (Node->getIdentType()) {
David Blaikie83d382b2011-09-23 05:06:16 +00001684 default: llvm_unreachable("unknown case");
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001685 case PredefinedExpr::Func: OS << " __func__"; break;
1686 case PredefinedExpr::Function: OS << " __FUNCTION__"; break;
David Majnemerbed356a2013-11-06 23:31:56 +00001687 case PredefinedExpr::FuncDName: OS << " __FUNCDNAME__"; break;
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001688 case PredefinedExpr::LFunction: OS << " L__FUNCTION__"; break;
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001689 case PredefinedExpr::PrettyFunction: OS << " __PRETTY_FUNCTION__";break;
Reid Kleckner52eddda2014-04-08 18:13:24 +00001690 case PredefinedExpr::FuncSig: OS << " __FUNCSIG__"; break;
Chris Lattnercbe4f772007-08-08 22:51:59 +00001691 }
1692}
1693
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001694void ASTDumper::VisitCharacterLiteral(const CharacterLiteral *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001695 VisitExpr(Node);
Richard Trieud215b8d2013-01-26 01:31:20 +00001696 ColorScope Color(*this, ValueColor);
Richard Trieu364ee422011-11-03 23:56:23 +00001697 OS << " " << Node->getValue();
Chris Lattnercbe4f772007-08-08 22:51:59 +00001698}
1699
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001700void ASTDumper::VisitIntegerLiteral(const IntegerLiteral *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001701 VisitExpr(Node);
Chris Lattnercbe4f772007-08-08 22:51:59 +00001702
1703 bool isSigned = Node->getType()->isSignedIntegerType();
Richard Trieud215b8d2013-01-26 01:31:20 +00001704 ColorScope Color(*this, ValueColor);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001705 OS << " " << Node->getValue().toString(10, isSigned);
Chris Lattnercbe4f772007-08-08 22:51:59 +00001706}
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001707
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001708void ASTDumper::VisitFloatingLiteral(const FloatingLiteral *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001709 VisitExpr(Node);
Richard Trieud215b8d2013-01-26 01:31:20 +00001710 ColorScope Color(*this, ValueColor);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001711 OS << " " << Node->getValueAsApproximateDouble();
Chris Lattnercbe4f772007-08-08 22:51:59 +00001712}
Chris Lattner1c20a172007-08-26 03:42:43 +00001713
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001714void ASTDumper::VisitStringLiteral(const StringLiteral *Str) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001715 VisitExpr(Str);
Richard Trieud215b8d2013-01-26 01:31:20 +00001716 ColorScope Color(*this, ValueColor);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001717 OS << " ";
Richard Trieudc355912012-06-13 20:25:24 +00001718 Str->outputString(OS);
Chris Lattnercbe4f772007-08-08 22:51:59 +00001719}
Chris Lattner84ca3762007-08-30 01:00:35 +00001720
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001721void ASTDumper::VisitUnaryOperator(const UnaryOperator *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001722 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001723 OS << " " << (Node->isPostfix() ? "postfix" : "prefix")
1724 << " '" << UnaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
Chris Lattnercbe4f772007-08-08 22:51:59 +00001725}
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001726
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001727void ASTDumper::VisitUnaryExprOrTypeTraitExpr(
1728 const UnaryExprOrTypeTraitExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001729 VisitExpr(Node);
Peter Collingbournee190dee2011-03-11 19:24:49 +00001730 switch(Node->getKind()) {
1731 case UETT_SizeOf:
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001732 OS << " sizeof";
Peter Collingbournee190dee2011-03-11 19:24:49 +00001733 break;
1734 case UETT_AlignOf:
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001735 OS << " alignof";
Peter Collingbournee190dee2011-03-11 19:24:49 +00001736 break;
1737 case UETT_VecStep:
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001738 OS << " vec_step";
Peter Collingbournee190dee2011-03-11 19:24:49 +00001739 break;
1740 }
Sebastian Redl6f282892008-11-11 17:56:53 +00001741 if (Node->isArgumentType())
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001742 dumpType(Node->getArgumentType());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001743}
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +00001744
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001745void ASTDumper::VisitMemberExpr(const MemberExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001746 VisitExpr(Node);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001747 OS << " " << (Node->isArrow() ? "->" : ".") << *Node->getMemberDecl();
1748 dumpPointer(Node->getMemberDecl());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001749}
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001750
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001751void ASTDumper::VisitExtVectorElementExpr(const ExtVectorElementExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001752 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001753 OS << " " << Node->getAccessor().getNameStart();
Chris Lattnercbe4f772007-08-08 22:51:59 +00001754}
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001755
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001756void ASTDumper::VisitBinaryOperator(const BinaryOperator *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001757 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001758 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
Chris Lattner86928112007-08-25 02:00:02 +00001759}
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001760
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001761void ASTDumper::VisitCompoundAssignOperator(
1762 const CompoundAssignOperator *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001763 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001764 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode())
1765 << "' ComputeLHSTy=";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001766 dumpBareType(Node->getComputationLHSType());
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001767 OS << " ComputeResultTy=";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001768 dumpBareType(Node->getComputationResultType());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001769}
Chris Lattnercbe4f772007-08-08 22:51:59 +00001770
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001771void ASTDumper::VisitBlockExpr(const BlockExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001772 VisitExpr(Node);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001773 dumpDecl(Node->getBlockDecl());
John McCall351762c2011-02-07 10:33:21 +00001774}
1775
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001776void ASTDumper::VisitOpaqueValueExpr(const OpaqueValueExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001777 VisitExpr(Node);
John McCallfe96e0b2011-11-06 09:01:30 +00001778
Richard Trieude5cc7d2013-01-31 01:44:26 +00001779 if (Expr *Source = Node->getSourceExpr()) {
1780 lastChild();
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001781 dumpStmt(Source);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001782 }
John McCallfe96e0b2011-11-06 09:01:30 +00001783}
1784
Chris Lattnercbe4f772007-08-08 22:51:59 +00001785// GNU extensions.
1786
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001787void ASTDumper::VisitAddrLabelExpr(const AddrLabelExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001788 VisitExpr(Node);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001789 OS << " " << Node->getLabel()->getName();
1790 dumpPointer(Node->getLabel());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001791}
1792
Chris Lattner8f184b12007-08-09 18:03:18 +00001793//===----------------------------------------------------------------------===//
1794// C++ Expressions
1795//===----------------------------------------------------------------------===//
Chris Lattnercbe4f772007-08-08 22:51:59 +00001796
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001797void ASTDumper::VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001798 VisitExpr(Node);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001799 OS << " " << Node->getCastName()
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001800 << "<" << Node->getTypeAsWritten().getAsString() << ">"
Anders Carlssona70cff62010-04-24 19:06:50 +00001801 << " <" << Node->getCastKindName();
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001802 dumpBasePath(OS, Node);
Anders Carlssona70cff62010-04-24 19:06:50 +00001803 OS << ">";
Chris Lattnercbe4f772007-08-08 22:51:59 +00001804}
1805
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001806void ASTDumper::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001807 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001808 OS << " " << (Node->getValue() ? "true" : "false");
Chris Lattnercbe4f772007-08-08 22:51:59 +00001809}
1810
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001811void ASTDumper::VisitCXXThisExpr(const CXXThisExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001812 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001813 OS << " this";
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001814}
1815
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001816void ASTDumper::VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001817 VisitExpr(Node);
Eli Friedman29538892011-09-02 17:38:59 +00001818 OS << " functional cast to " << Node->getTypeAsWritten().getAsString()
1819 << " <" << Node->getCastKindName() << ">";
Douglas Gregore200adc2008-10-27 19:41:14 +00001820}
1821
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001822void ASTDumper::VisitCXXConstructExpr(const CXXConstructExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001823 VisitExpr(Node);
John McCalleba90cd2010-02-02 19:03:45 +00001824 CXXConstructorDecl *Ctor = Node->getConstructor();
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001825 dumpType(Ctor->getType());
Anders Carlsson073846832009-08-12 00:21:52 +00001826 if (Node->isElidable())
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001827 OS << " elidable";
John McCall85370042010-08-07 06:38:55 +00001828 if (Node->requiresZeroInitialization())
1829 OS << " zeroing";
Anders Carlsson073846832009-08-12 00:21:52 +00001830}
1831
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001832void ASTDumper::VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001833 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001834 OS << " ";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001835 dumpCXXTemporary(Node->getTemporary());
Anders Carlsson073846832009-08-12 00:21:52 +00001836}
1837
Richard Smithe6c01442013-06-05 00:46:14 +00001838void
1839ASTDumper::VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node) {
1840 VisitExpr(Node);
1841 if (const ValueDecl *VD = Node->getExtendingDecl()) {
1842 OS << " extended by ";
1843 dumpBareDeclRef(VD);
1844 }
1845}
1846
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001847void ASTDumper::VisitExprWithCleanups(const ExprWithCleanups *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001848 VisitExpr(Node);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001849 for (unsigned i = 0, e = Node->getNumObjects(); i != e; ++i)
1850 dumpDeclRef(Node->getObject(i), "cleanup");
Anders Carlsson073846832009-08-12 00:21:52 +00001851}
1852
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001853void ASTDumper::dumpCXXTemporary(const CXXTemporary *Temporary) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001854 OS << "(CXXTemporary";
1855 dumpPointer(Temporary);
1856 OS << ")";
Anders Carlsson073846832009-08-12 00:21:52 +00001857}
1858
Anders Carlsson76f4a902007-08-21 17:43:55 +00001859//===----------------------------------------------------------------------===//
1860// Obj-C Expressions
1861//===----------------------------------------------------------------------===//
1862
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001863void ASTDumper::VisitObjCMessageExpr(const ObjCMessageExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001864 VisitExpr(Node);
Aaron Ballmanb190f972014-01-03 17:59:55 +00001865 OS << " selector=";
1866 Node->getSelector().print(OS);
Douglas Gregor9a129192010-04-21 00:45:42 +00001867 switch (Node->getReceiverKind()) {
1868 case ObjCMessageExpr::Instance:
1869 break;
1870
1871 case ObjCMessageExpr::Class:
1872 OS << " class=";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001873 dumpBareType(Node->getClassReceiver());
Douglas Gregor9a129192010-04-21 00:45:42 +00001874 break;
1875
1876 case ObjCMessageExpr::SuperInstance:
1877 OS << " super (instance)";
1878 break;
1879
1880 case ObjCMessageExpr::SuperClass:
1881 OS << " super (class)";
1882 break;
1883 }
Ted Kremenek36748da2008-02-29 22:04:05 +00001884}
1885
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001886void ASTDumper::VisitObjCBoxedExpr(const ObjCBoxedExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001887 VisitExpr(Node);
Aaron Ballmanb190f972014-01-03 17:59:55 +00001888 OS << " selector=";
1889 Node->getBoxingMethod()->getSelector().print(OS);
Argyrios Kyrtzidis9dd40892012-05-10 20:02:31 +00001890}
1891
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001892void ASTDumper::VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001893 VisitStmt(Node);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001894 if (const VarDecl *CatchParam = Node->getCatchParamDecl())
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001895 dumpDecl(CatchParam);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001896 else
Douglas Gregor96c79492010-04-23 22:50:49 +00001897 OS << " catch all";
Douglas Gregor96c79492010-04-23 22:50:49 +00001898}
1899
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001900void ASTDumper::VisitObjCEncodeExpr(const ObjCEncodeExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001901 VisitExpr(Node);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001902 dumpType(Node->getEncodedType());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001903}
1904
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001905void ASTDumper::VisitObjCSelectorExpr(const ObjCSelectorExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001906 VisitExpr(Node);
Mike Stump11289f42009-09-09 15:08:12 +00001907
Aaron Ballmanb190f972014-01-03 17:59:55 +00001908 OS << " ";
1909 Node->getSelector().print(OS);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001910}
1911
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001912void ASTDumper::VisitObjCProtocolExpr(const ObjCProtocolExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001913 VisitExpr(Node);
Mike Stump11289f42009-09-09 15:08:12 +00001914
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001915 OS << ' ' << *Node->getProtocol();
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001916}
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001917
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001918void ASTDumper::VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001919 VisitExpr(Node);
John McCallb7bd14f2010-12-02 01:19:52 +00001920 if (Node->isImplicitProperty()) {
Fariborz Jahanian0f0b3022010-12-22 19:46:35 +00001921 OS << " Kind=MethodRef Getter=\"";
1922 if (Node->getImplicitPropertyGetter())
Aaron Ballmanb190f972014-01-03 17:59:55 +00001923 Node->getImplicitPropertyGetter()->getSelector().print(OS);
Fariborz Jahanian0f0b3022010-12-22 19:46:35 +00001924 else
1925 OS << "(null)";
1926
1927 OS << "\" Setter=\"";
John McCallb7bd14f2010-12-02 01:19:52 +00001928 if (ObjCMethodDecl *Setter = Node->getImplicitPropertySetter())
Aaron Ballmanb190f972014-01-03 17:59:55 +00001929 Setter->getSelector().print(OS);
John McCallb7bd14f2010-12-02 01:19:52 +00001930 else
1931 OS << "(null)";
1932 OS << "\"";
1933 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001934 OS << " Kind=PropertyRef Property=\"" << *Node->getExplicitProperty() <<'"';
John McCallb7bd14f2010-12-02 01:19:52 +00001935 }
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00001936
Fariborz Jahanian681c0752010-10-14 16:04:05 +00001937 if (Node->isSuperReceiver())
1938 OS << " super";
Argyrios Kyrtzidisab468b02012-03-30 00:19:18 +00001939
1940 OS << " Messaging=";
1941 if (Node->isMessagingGetter() && Node->isMessagingSetter())
1942 OS << "Getter&Setter";
1943 else if (Node->isMessagingGetter())
1944 OS << "Getter";
1945 else if (Node->isMessagingSetter())
1946 OS << "Setter";
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001947}
1948
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001949void ASTDumper::VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001950 VisitExpr(Node);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001951 if (Node->isArraySubscriptRefExpr())
1952 OS << " Kind=ArraySubscript GetterForArray=\"";
1953 else
1954 OS << " Kind=DictionarySubscript GetterForDictionary=\"";
1955 if (Node->getAtIndexMethodDecl())
Aaron Ballmanb190f972014-01-03 17:59:55 +00001956 Node->getAtIndexMethodDecl()->getSelector().print(OS);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001957 else
1958 OS << "(null)";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001959
Ted Kremeneke65b0862012-03-06 20:05:56 +00001960 if (Node->isArraySubscriptRefExpr())
1961 OS << "\" SetterForArray=\"";
1962 else
1963 OS << "\" SetterForDictionary=\"";
1964 if (Node->setAtIndexMethodDecl())
Aaron Ballmanb190f972014-01-03 17:59:55 +00001965 Node->setAtIndexMethodDecl()->getSelector().print(OS);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001966 else
1967 OS << "(null)";
1968}
1969
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001970void ASTDumper::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001971 VisitExpr(Node);
Ted Kremeneke65b0862012-03-06 20:05:56 +00001972 OS << " " << (Node->getValue() ? "__objc_yes" : "__objc_no");
1973}
1974
Chris Lattnercbe4f772007-08-08 22:51:59 +00001975//===----------------------------------------------------------------------===//
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00001976// Comments
1977//===----------------------------------------------------------------------===//
1978
1979const char *ASTDumper::getCommandName(unsigned CommandID) {
1980 if (Traits)
1981 return Traits->getCommandInfo(CommandID)->Name;
1982 const CommandInfo *Info = CommandTraits::getBuiltinCommandInfo(CommandID);
1983 if (Info)
1984 return Info->Name;
1985 return "<not a builtin command>";
1986}
1987
1988void ASTDumper::dumpFullComment(const FullComment *C) {
1989 if (!C)
1990 return;
1991
1992 FC = C;
1993 dumpComment(C);
1994 FC = 0;
1995}
1996
1997void ASTDumper::dumpComment(const Comment *C) {
1998 IndentScope Indent(*this);
1999
2000 if (!C) {
Richard Trieud215b8d2013-01-26 01:31:20 +00002001 ColorScope Color(*this, NullColor);
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002002 OS << "<<<NULL>>>";
2003 return;
2004 }
2005
Richard Trieud215b8d2013-01-26 01:31:20 +00002006 {
2007 ColorScope Color(*this, CommentColor);
2008 OS << C->getCommentKindName();
2009 }
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002010 dumpPointer(C);
2011 dumpSourceRange(C->getSourceRange());
2012 ConstCommentVisitor<ASTDumper>::visit(C);
2013 for (Comment::child_iterator I = C->child_begin(), E = C->child_end();
Richard Trieude5cc7d2013-01-31 01:44:26 +00002014 I != E; ++I) {
2015 if (I + 1 == E)
2016 lastChild();
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002017 dumpComment(*I);
Richard Trieude5cc7d2013-01-31 01:44:26 +00002018 }
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002019}
2020
2021void ASTDumper::visitTextComment(const TextComment *C) {
2022 OS << " Text=\"" << C->getText() << "\"";
2023}
2024
2025void ASTDumper::visitInlineCommandComment(const InlineCommandComment *C) {
2026 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
2027 switch (C->getRenderKind()) {
2028 case InlineCommandComment::RenderNormal:
2029 OS << " RenderNormal";
2030 break;
2031 case InlineCommandComment::RenderBold:
2032 OS << " RenderBold";
2033 break;
2034 case InlineCommandComment::RenderMonospaced:
2035 OS << " RenderMonospaced";
2036 break;
2037 case InlineCommandComment::RenderEmphasized:
2038 OS << " RenderEmphasized";
2039 break;
2040 }
2041
2042 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
2043 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
2044}
2045
2046void ASTDumper::visitHTMLStartTagComment(const HTMLStartTagComment *C) {
2047 OS << " Name=\"" << C->getTagName() << "\"";
2048 if (C->getNumAttrs() != 0) {
2049 OS << " Attrs: ";
2050 for (unsigned i = 0, e = C->getNumAttrs(); i != e; ++i) {
2051 const HTMLStartTagComment::Attribute &Attr = C->getAttr(i);
2052 OS << " \"" << Attr.Name << "=\"" << Attr.Value << "\"";
2053 }
2054 }
2055 if (C->isSelfClosing())
2056 OS << " SelfClosing";
2057}
2058
2059void ASTDumper::visitHTMLEndTagComment(const HTMLEndTagComment *C) {
2060 OS << " Name=\"" << C->getTagName() << "\"";
2061}
2062
2063void ASTDumper::visitBlockCommandComment(const BlockCommandComment *C) {
2064 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
2065 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
2066 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
2067}
2068
2069void ASTDumper::visitParamCommandComment(const ParamCommandComment *C) {
2070 OS << " " << ParamCommandComment::getDirectionAsString(C->getDirection());
2071
2072 if (C->isDirectionExplicit())
2073 OS << " explicitly";
2074 else
2075 OS << " implicitly";
2076
2077 if (C->hasParamName()) {
2078 if (C->isParamIndexValid())
2079 OS << " Param=\"" << C->getParamName(FC) << "\"";
2080 else
2081 OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
2082 }
2083
Dmitri Gribenkodbff5c72014-03-19 14:03:47 +00002084 if (C->isParamIndexValid() && !C->isVarArgParam())
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002085 OS << " ParamIndex=" << C->getParamIndex();
2086}
2087
2088void ASTDumper::visitTParamCommandComment(const TParamCommandComment *C) {
2089 if (C->hasParamName()) {
2090 if (C->isPositionValid())
2091 OS << " Param=\"" << C->getParamName(FC) << "\"";
2092 else
2093 OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
2094 }
2095
2096 if (C->isPositionValid()) {
2097 OS << " Position=<";
2098 for (unsigned i = 0, e = C->getDepth(); i != e; ++i) {
2099 OS << C->getIndex(i);
2100 if (i != e - 1)
2101 OS << ", ";
2102 }
2103 OS << ">";
2104 }
2105}
2106
2107void ASTDumper::visitVerbatimBlockComment(const VerbatimBlockComment *C) {
2108 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\""
2109 " CloseName=\"" << C->getCloseName() << "\"";
2110}
2111
2112void ASTDumper::visitVerbatimBlockLineComment(
2113 const VerbatimBlockLineComment *C) {
2114 OS << " Text=\"" << C->getText() << "\"";
2115}
2116
2117void ASTDumper::visitVerbatimLineComment(const VerbatimLineComment *C) {
2118 OS << " Text=\"" << C->getText() << "\"";
2119}
2120
2121//===----------------------------------------------------------------------===//
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002122// Decl method implementations
2123//===----------------------------------------------------------------------===//
2124
Alp Tokeref6b0072014-01-04 13:47:14 +00002125LLVM_DUMP_METHOD void Decl::dump() const { dump(llvm::errs()); }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002126
Alp Tokeref6b0072014-01-04 13:47:14 +00002127LLVM_DUMP_METHOD void Decl::dump(raw_ostream &OS) const {
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002128 ASTDumper P(OS, &getASTContext().getCommentCommandTraits(),
2129 &getASTContext().getSourceManager());
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002130 P.dumpDecl(this);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002131}
2132
Alp Tokeref6b0072014-01-04 13:47:14 +00002133LLVM_DUMP_METHOD void Decl::dumpColor() const {
Richard Trieud215b8d2013-01-26 01:31:20 +00002134 ASTDumper P(llvm::errs(), &getASTContext().getCommentCommandTraits(),
2135 &getASTContext().getSourceManager(), /*ShowColors*/true);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002136 P.dumpDecl(this);
Richard Trieud215b8d2013-01-26 01:31:20 +00002137}
Richard Smith33937e72013-06-22 21:49:40 +00002138
Alp Tokeref6b0072014-01-04 13:47:14 +00002139LLVM_DUMP_METHOD void DeclContext::dumpLookups() const {
Richard Smith6ea05822013-06-24 01:45:33 +00002140 dumpLookups(llvm::errs());
2141}
2142
Alp Tokeref6b0072014-01-04 13:47:14 +00002143LLVM_DUMP_METHOD void DeclContext::dumpLookups(raw_ostream &OS) const {
Richard Smith33937e72013-06-22 21:49:40 +00002144 const DeclContext *DC = this;
2145 while (!DC->isTranslationUnit())
2146 DC = DC->getParent();
2147 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
Richard Smith6ea05822013-06-24 01:45:33 +00002148 ASTDumper P(OS, &Ctx.getCommentCommandTraits(), &Ctx.getSourceManager());
Richard Smith33937e72013-06-22 21:49:40 +00002149 P.dumpLookups(this);
2150}
2151
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002152//===----------------------------------------------------------------------===//
Chris Lattnercbe4f772007-08-08 22:51:59 +00002153// Stmt method implementations
2154//===----------------------------------------------------------------------===//
2155
Alp Tokeref6b0072014-01-04 13:47:14 +00002156LLVM_DUMP_METHOD void Stmt::dump(SourceManager &SM) const {
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +00002157 dump(llvm::errs(), SM);
2158}
2159
Alp Tokeref6b0072014-01-04 13:47:14 +00002160LLVM_DUMP_METHOD void Stmt::dump(raw_ostream &OS, SourceManager &SM) const {
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002161 ASTDumper P(OS, 0, &SM);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002162 P.dumpStmt(this);
Chris Lattner779d5d92007-08-30 00:40:08 +00002163}
2164
Alp Tokeref6b0072014-01-04 13:47:14 +00002165LLVM_DUMP_METHOD void Stmt::dump() const {
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002166 ASTDumper P(llvm::errs(), 0, 0);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002167 P.dumpStmt(this);
Chris Lattnercbe4f772007-08-08 22:51:59 +00002168}
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002169
Alp Tokeref6b0072014-01-04 13:47:14 +00002170LLVM_DUMP_METHOD void Stmt::dumpColor() const {
Richard Trieud215b8d2013-01-26 01:31:20 +00002171 ASTDumper P(llvm::errs(), 0, 0, /*ShowColors*/true);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002172 P.dumpStmt(this);
Richard Trieud215b8d2013-01-26 01:31:20 +00002173}
2174
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002175//===----------------------------------------------------------------------===//
2176// Comment method implementations
2177//===----------------------------------------------------------------------===//
2178
Alp Tokeref6b0072014-01-04 13:47:14 +00002179LLVM_DUMP_METHOD void Comment::dump() const { dump(llvm::errs(), 0, 0); }
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002180
Alp Tokeref6b0072014-01-04 13:47:14 +00002181LLVM_DUMP_METHOD void Comment::dump(const ASTContext &Context) const {
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002182 dump(llvm::errs(), &Context.getCommentCommandTraits(),
2183 &Context.getSourceManager());
2184}
2185
Alexander Kornienko00911f12013-01-15 12:20:21 +00002186void Comment::dump(raw_ostream &OS, const CommandTraits *Traits,
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002187 const SourceManager *SM) const {
2188 const FullComment *FC = dyn_cast<FullComment>(this);
2189 ASTDumper D(OS, Traits, SM);
2190 D.dumpFullComment(FC);
2191}
Richard Trieud215b8d2013-01-26 01:31:20 +00002192
Alp Tokeref6b0072014-01-04 13:47:14 +00002193LLVM_DUMP_METHOD void Comment::dumpColor() const {
Richard Trieud215b8d2013-01-26 01:31:20 +00002194 const FullComment *FC = dyn_cast<FullComment>(this);
2195 ASTDumper D(llvm::errs(), 0, 0, /*ShowColors*/true);
2196 D.dumpFullComment(FC);
2197}