blob: 3a9aee6bdd283a4a20d62818a66d4d780a149cc2 [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"
Richard Smithd5e7ff82014-10-31 01:17:45 +000023#include "clang/AST/TypeVisitor.h"
Alexander Kornienko90ff6072012-12-20 02:09:13 +000024#include "clang/Basic/Module.h"
Chris Lattner11e30d32007-08-30 06:17:34 +000025#include "clang/Basic/SourceManager.h"
Daniel Dunbar34a96c82009-12-03 09:13:13 +000026#include "llvm/Support/raw_ostream.h"
Chris Lattnercbe4f772007-08-08 22:51:59 +000027using namespace clang;
Alexander Kornienkoebc17b52013-01-14 14:07:11 +000028using namespace clang::comments;
Chris Lattnercbe4f772007-08-08 22:51:59 +000029
30//===----------------------------------------------------------------------===//
Alexander Kornienko18ec81b2012-12-13 13:59:55 +000031// ASTDumper Visitor
Chris Lattnercbe4f772007-08-08 22:51:59 +000032//===----------------------------------------------------------------------===//
33
34namespace {
Richard Trieud215b8d2013-01-26 01:31:20 +000035 // Colors used for various parts of the AST dump
Richard Trieu532018f2014-03-06 01:09:03 +000036 // Do not use bold yellow for any text. It is hard to read on white screens.
Richard Trieud215b8d2013-01-26 01:31:20 +000037
38 struct TerminalColor {
39 raw_ostream::Colors Color;
40 bool Bold;
41 };
42
Richard Trieu532018f2014-03-06 01:09:03 +000043 // Red - CastColor
44 // Green - TypeColor
45 // Bold Green - DeclKindNameColor, UndeserializedColor
46 // Yellow - AddressColor, LocationColor
47 // Blue - CommentColor, NullColor, IndentColor
48 // Bold Blue - AttrColor
49 // Bold Magenta - StmtColor
50 // Cyan - ValueKindColor, ObjectKindColor
51 // Bold Cyan - ValueColor, DeclNameColor
52
Richard Trieud215b8d2013-01-26 01:31:20 +000053 // Decl kind names (VarDecl, FunctionDecl, etc)
54 static const TerminalColor DeclKindNameColor = { raw_ostream::GREEN, true };
55 // Attr names (CleanupAttr, GuardedByAttr, etc)
56 static const TerminalColor AttrColor = { raw_ostream::BLUE, true };
57 // Statement names (DeclStmt, ImplicitCastExpr, etc)
58 static const TerminalColor StmtColor = { raw_ostream::MAGENTA, true };
59 // Comment names (FullComment, ParagraphComment, TextComment, etc)
Richard Trieu532018f2014-03-06 01:09:03 +000060 static const TerminalColor CommentColor = { raw_ostream::BLUE, false };
Richard Trieud215b8d2013-01-26 01:31:20 +000061
62 // Type names (int, float, etc, plus user defined types)
63 static const TerminalColor TypeColor = { raw_ostream::GREEN, false };
64
65 // Pointer address
66 static const TerminalColor AddressColor = { raw_ostream::YELLOW, false };
67 // Source locations
68 static const TerminalColor LocationColor = { raw_ostream::YELLOW, false };
69
70 // lvalue/xvalue
71 static const TerminalColor ValueKindColor = { raw_ostream::CYAN, false };
72 // bitfield/objcproperty/objcsubscript/vectorcomponent
73 static const TerminalColor ObjectKindColor = { raw_ostream::CYAN, false };
74
75 // Null statements
76 static const TerminalColor NullColor = { raw_ostream::BLUE, false };
77
Richard Smith1d209d02013-05-23 01:49:11 +000078 // Undeserialized entities
79 static const TerminalColor UndeserializedColor = { raw_ostream::GREEN, true };
80
Richard Trieud215b8d2013-01-26 01:31:20 +000081 // CastKind from CastExpr's
82 static const TerminalColor CastColor = { raw_ostream::RED, false };
83
84 // Value of the statement
85 static const TerminalColor ValueColor = { raw_ostream::CYAN, true };
86 // Decl names
87 static const TerminalColor DeclNameColor = { raw_ostream::CYAN, true };
88
Richard Trieude5cc7d2013-01-31 01:44:26 +000089 // Indents ( `, -. | )
90 static const TerminalColor IndentColor = { raw_ostream::BLUE, false };
91
Alexander Kornienko90ff6072012-12-20 02:09:13 +000092 class ASTDumper
Alexander Kornienko540bacb2013-02-01 12:35:51 +000093 : public ConstDeclVisitor<ASTDumper>, public ConstStmtVisitor<ASTDumper>,
Richard Smithd5e7ff82014-10-31 01:17:45 +000094 public ConstCommentVisitor<ASTDumper>, public TypeVisitor<ASTDumper> {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000095 raw_ostream &OS;
Alexander Kornienkoebc17b52013-01-14 14:07:11 +000096 const CommandTraits *Traits;
97 const SourceManager *SM;
Mike Stump11289f42009-09-09 15:08:12 +000098
Richard Smithf7514452014-10-30 21:02:37 +000099 /// Pending[i] is an action to dump an entity at level i.
100 llvm::SmallVector<std::function<void(bool isLastChild)>, 32> Pending;
Richard Trieude5cc7d2013-01-31 01:44:26 +0000101
Richard Smithf7514452014-10-30 21:02:37 +0000102 /// Indicates whether we're at the top level.
103 bool TopLevel;
Richard Trieude5cc7d2013-01-31 01:44:26 +0000104
Richard Smithf7514452014-10-30 21:02:37 +0000105 /// Indicates if we're handling the first child after entering a new depth.
106 bool FirstChild;
107
108 /// Prefix for currently-being-dumped entity.
109 std::string Prefix;
Richard Trieude5cc7d2013-01-31 01:44:26 +0000110
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000111 /// Keep track of the last location we print out so that we can
112 /// print out deltas from then on out.
Chris Lattner11e30d32007-08-30 06:17:34 +0000113 const char *LastLocFilename;
114 unsigned LastLocLine;
Douglas Gregor7de59662009-05-29 20:38:28 +0000115
Alexander Kornienkoebc17b52013-01-14 14:07:11 +0000116 /// The \c FullComment parent of the comment being dumped.
117 const FullComment *FC;
118
Richard Trieud215b8d2013-01-26 01:31:20 +0000119 bool ShowColors;
120
Richard Smithf7514452014-10-30 21:02:37 +0000121 /// Dump a child of the current node.
122 template<typename Fn> void dumpChild(Fn doDumpChild) {
123 // If we're at the top level, there's nothing interesting to do; just
124 // run the dumper.
125 if (TopLevel) {
126 TopLevel = false;
127 doDumpChild();
128 while (!Pending.empty()) {
129 Pending.back()(true);
130 Pending.pop_back();
131 }
132 Prefix.clear();
133 OS << "\n";
134 TopLevel = true;
135 return;
Manuel Klimek874030e2012-11-07 00:33:12 +0000136 }
Richard Smithf7514452014-10-30 21:02:37 +0000137
138 const FullComment *OrigFC = FC;
139 auto dumpWithIndent = [this, doDumpChild, OrigFC](bool isLastChild) {
140 // Print out the appropriate tree structure and work out the prefix for
141 // children of this node. For instance:
142 //
143 // A Prefix = ""
144 // |-B Prefix = "| "
145 // | `-C Prefix = "| "
146 // `-D Prefix = " "
147 // |-E Prefix = " | "
148 // `-F Prefix = " "
149 // G Prefix = ""
150 //
151 // Note that the first level gets no prefix.
152 {
153 OS << '\n';
154 ColorScope Color(*this, IndentColor);
155 OS << Prefix << (isLastChild ? '`' : '|') << '-';
NAKAMURA Takumic73e4022014-10-31 00:30:37 +0000156 this->Prefix.push_back(isLastChild ? ' ' : '|');
157 this->Prefix.push_back(' ');
Richard Smithf7514452014-10-30 21:02:37 +0000158 }
159
160 FirstChild = true;
161 unsigned Depth = Pending.size();
162
163 FC = OrigFC;
164 doDumpChild();
165
166 // If any children are left, they're the last at their nesting level.
167 // Dump those ones out now.
168 while (Depth < Pending.size()) {
169 Pending.back()(true);
NAKAMURA Takumic73e4022014-10-31 00:30:37 +0000170 this->Pending.pop_back();
Richard Smithf7514452014-10-30 21:02:37 +0000171 }
172
173 // Restore the old prefix.
NAKAMURA Takumic73e4022014-10-31 00:30:37 +0000174 this->Prefix.resize(Prefix.size() - 2);
Richard Smithf7514452014-10-30 21:02:37 +0000175 };
176
177 if (FirstChild) {
178 Pending.push_back(std::move(dumpWithIndent));
179 } else {
180 Pending.back()(false);
181 Pending.back() = std::move(dumpWithIndent);
Manuel Klimek874030e2012-11-07 00:33:12 +0000182 }
Richard Smithf7514452014-10-30 21:02:37 +0000183 FirstChild = false;
184 }
Manuel Klimek874030e2012-11-07 00:33:12 +0000185
Richard Trieud215b8d2013-01-26 01:31:20 +0000186 class ColorScope {
187 ASTDumper &Dumper;
188 public:
189 ColorScope(ASTDumper &Dumper, TerminalColor Color)
190 : Dumper(Dumper) {
191 if (Dumper.ShowColors)
192 Dumper.OS.changeColor(Color.Color, Color.Bold);
193 }
194 ~ColorScope() {
195 if (Dumper.ShowColors)
196 Dumper.OS.resetColor();
197 }
198 };
199
Chris Lattnercbe4f772007-08-08 22:51:59 +0000200 public:
Alexander Kornienkoebc17b52013-01-14 14:07:11 +0000201 ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
202 const SourceManager *SM)
Richard Smithf7514452014-10-30 21:02:37 +0000203 : OS(OS), Traits(Traits), SM(SM), TopLevel(true), FirstChild(true),
Craig Topper36250ad2014-05-12 05:36:57 +0000204 LastLocFilename(""), LastLocLine(~0U), FC(nullptr),
Richard Trieud215b8d2013-01-26 01:31:20 +0000205 ShowColors(SM && SM->getDiagnostics().getShowColors()) { }
206
207 ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
208 const SourceManager *SM, bool ShowColors)
Richard Smithf7514452014-10-30 21:02:37 +0000209 : OS(OS), Traits(Traits), SM(SM), TopLevel(true), FirstChild(true),
Richard Smith56d12152013-01-31 02:04:38 +0000210 LastLocFilename(""), LastLocLine(~0U),
Richard Trieude5cc7d2013-01-31 01:44:26 +0000211 ShowColors(ShowColors) { }
Mike Stump11289f42009-09-09 15:08:12 +0000212
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000213 void dumpDecl(const Decl *D);
214 void dumpStmt(const Stmt *S);
Alexander Kornienkoebc17b52013-01-14 14:07:11 +0000215 void dumpFullComment(const FullComment *C);
Mike Stump11289f42009-09-09 15:08:12 +0000216
Richard Trieude5cc7d2013-01-31 01:44:26 +0000217 // Utilities
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000218 void dumpPointer(const void *Ptr);
219 void dumpSourceRange(SourceRange R);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000220 void dumpLocation(SourceLocation Loc);
Richard Smithd5e7ff82014-10-31 01:17:45 +0000221 void dumpBareType(QualType T, bool Desugar = true);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000222 void dumpType(QualType T);
Richard Smithd5e7ff82014-10-31 01:17:45 +0000223 void dumpTypeAsChild(QualType T);
224 void dumpTypeAsChild(const Type *T);
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000225 void dumpBareDeclRef(const Decl *Node);
Craig Topper36250ad2014-05-12 05:36:57 +0000226 void dumpDeclRef(const Decl *Node, const char *Label = nullptr);
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000227 void dumpName(const NamedDecl *D);
Richard Trieude5cc7d2013-01-31 01:44:26 +0000228 bool hasNodes(const DeclContext *DC);
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000229 void dumpDeclContext(const DeclContext *DC);
Richard Smith35f986d2014-08-11 22:11:07 +0000230 void dumpLookups(const DeclContext *DC, bool DumpDecls);
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000231 void dumpAttr(const Attr *A);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000232
233 // C++ Utilities
234 void dumpAccessSpecifier(AccessSpecifier AS);
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000235 void dumpCXXCtorInitializer(const CXXCtorInitializer *Init);
236 void dumpTemplateParameters(const TemplateParameterList *TPL);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000237 void dumpTemplateArgumentListInfo(const TemplateArgumentListInfo &TALI);
238 void dumpTemplateArgumentLoc(const TemplateArgumentLoc &A);
239 void dumpTemplateArgumentList(const TemplateArgumentList &TAL);
240 void dumpTemplateArgument(const TemplateArgument &A,
241 SourceRange R = SourceRange());
242
Richard Smithd5e7ff82014-10-31 01:17:45 +0000243 // Types
244 void VisitComplexType(const ComplexType *T) {
245 dumpTypeAsChild(T->getElementType());
246 }
247 void VisitPointerType(const PointerType *T) {
248 dumpTypeAsChild(T->getPointeeType());
249 }
250 void VisitBlockPointerType(const BlockPointerType *T) {
251 dumpTypeAsChild(T->getPointeeType());
252 }
253 void VisitReferenceType(const ReferenceType *T) {
254 dumpTypeAsChild(T->getPointeeType());
255 }
256 void VisitRValueReferenceType(const ReferenceType *T) {
257 if (T->isSpelledAsLValue())
258 OS << " written as lvalue reference";
259 VisitReferenceType(T);
260 }
261 void VisitMemberPointerType(const MemberPointerType *T) {
262 dumpTypeAsChild(T->getClass());
263 dumpTypeAsChild(T->getPointeeType());
264 }
265 void VisitArrayType(const ArrayType *T) {
266 switch (T->getSizeModifier()) {
267 case ArrayType::Normal: break;
268 case ArrayType::Static: OS << " static"; break;
269 case ArrayType::Star: OS << " *"; break;
270 }
271 OS << " " << T->getIndexTypeQualifiers().getAsString();
272 dumpTypeAsChild(T->getElementType());
273 }
274 void VisitConstantArrayType(const ConstantArrayType *T) {
275 OS << " " << T->getSize();
276 VisitArrayType(T);
277 }
278 void VisitVariableArrayType(const VariableArrayType *T) {
279 OS << " ";
280 dumpSourceRange(T->getBracketsRange());
281 VisitArrayType(T);
282 dumpStmt(T->getSizeExpr());
283 }
284 void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
285 VisitArrayType(T);
286 OS << " ";
287 dumpSourceRange(T->getBracketsRange());
288 dumpStmt(T->getSizeExpr());
289 }
290 void VisitDependentSizedExtVectorType(
291 const DependentSizedExtVectorType *T) {
292 OS << " ";
293 dumpLocation(T->getAttributeLoc());
294 dumpTypeAsChild(T->getElementType());
295 dumpStmt(T->getSizeExpr());
296 }
297 void VisitVectorType(const VectorType *T) {
298 switch (T->getVectorKind()) {
299 case VectorType::GenericVector: break;
300 case VectorType::AltiVecVector: OS << " altivec"; break;
301 case VectorType::AltiVecPixel: OS << " altivec pixel"; break;
302 case VectorType::AltiVecBool: OS << " altivec bool"; break;
303 case VectorType::NeonVector: OS << " neon"; break;
304 case VectorType::NeonPolyVector: OS << " neon poly"; break;
305 }
306 OS << " " << T->getNumElements();
307 dumpTypeAsChild(T->getElementType());
308 }
309 void VisitFunctionType(const FunctionType *T) {
310 auto EI = T->getExtInfo();
311 if (EI.getNoReturn()) OS << " noreturn";
312 if (EI.getProducesResult()) OS << " produces_result";
313 if (EI.getHasRegParm()) OS << " regparm " << EI.getRegParm();
314 OS << " " << FunctionType::getNameForCallConv(EI.getCC());
315 dumpTypeAsChild(T->getReturnType());
316 }
317 void VisitFunctionProtoType(const FunctionProtoType *T) {
318 auto EPI = T->getExtProtoInfo();
319 if (EPI.HasTrailingReturn) OS << " trailing_return";
320 if (T->isConst()) OS << " const";
321 if (T->isVolatile()) OS << " volatile";
322 if (T->isRestrict()) OS << " restrict";
323 switch (EPI.RefQualifier) {
324 case RQ_None: break;
325 case RQ_LValue: OS << " &"; break;
326 case RQ_RValue: OS << " &&"; break;
327 }
328 // FIXME: Exception specification.
329 // FIXME: Consumed parameters.
330 VisitFunctionType(T);
331 for (QualType PT : T->getParamTypes())
332 dumpTypeAsChild(PT);
333 if (EPI.Variadic)
334 dumpChild([=] { OS << "..."; });
335 }
336 void VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
337 dumpDeclRef(T->getDecl());
338 }
339 void VisitTypedefType(const TypedefType *T) {
340 dumpDeclRef(T->getDecl());
341 }
342 void VisitTypeOfExprType(const TypeOfExprType *T) {
343 dumpStmt(T->getUnderlyingExpr());
344 }
345 void VisitDecltypeType(const DecltypeType *T) {
346 dumpStmt(T->getUnderlyingExpr());
347 }
348 void VisitUnaryTransformType(const UnaryTransformType *T) {
349 switch (T->getUTTKind()) {
350 case UnaryTransformType::EnumUnderlyingType:
351 OS << " underlying_type";
352 break;
353 }
354 dumpTypeAsChild(T->getBaseType());
355 }
356 void VisitTagType(const TagType *T) {
357 dumpDeclRef(T->getDecl());
358 }
359 void VisitAttributedType(const AttributedType *T) {
360 // FIXME: AttrKind
361 dumpTypeAsChild(T->getModifiedType());
362 }
363 void VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
364 OS << " depth " << T->getDepth() << " index " << T->getIndex();
365 if (T->isParameterPack()) OS << " pack";
366 dumpDeclRef(T->getDecl());
367 }
368 void VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) {
369 dumpTypeAsChild(T->getReplacedParameter());
370 }
371 void VisitSubstTemplateTypeParmPackType(
372 const SubstTemplateTypeParmPackType *T) {
373 dumpTypeAsChild(T->getReplacedParameter());
374 dumpTemplateArgument(T->getArgumentPack());
375 }
376 void VisitAutoType(const AutoType *T) {
377 if (T->isDecltypeAuto()) OS << " decltype(auto)";
378 if (!T->isDeduced())
379 OS << " undeduced";
380 }
381 void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
382 if (T->isTypeAlias()) OS << " alias";
383 OS << " "; T->getTemplateName().dump(OS);
384 for (auto &Arg : *T)
385 dumpTemplateArgument(Arg);
386 if (T->isTypeAlias())
387 dumpTypeAsChild(T->getAliasedType());
388 }
389 void VisitInjectedClassNameType(const InjectedClassNameType *T) {
390 dumpDeclRef(T->getDecl());
391 }
392 void VisitObjCInterfaceType(const ObjCInterfaceType *T) {
393 dumpDeclRef(T->getDecl());
394 }
395 void VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
396 dumpTypeAsChild(T->getPointeeType());
397 }
398 void VisitAtomicType(const AtomicType *T) {
399 dumpTypeAsChild(T->getValueType());
400 }
401 void VisitAdjustedType(const AdjustedType *T) {
402 dumpTypeAsChild(T->getOriginalType());
403 }
404 void VisitPackExpansionType(const PackExpansionType *T) {
405 if (auto N = T->getNumExpansions()) OS << " expansions " << *N;
406 if (!T->isSugared())
407 dumpTypeAsChild(T->getPattern());
408 }
409 // FIXME: ElaboratedType, DependentNameType,
410 // DependentTemplateSpecializationType, ObjCObjectType
411
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000412 // Decls
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000413 void VisitLabelDecl(const LabelDecl *D);
414 void VisitTypedefDecl(const TypedefDecl *D);
415 void VisitEnumDecl(const EnumDecl *D);
416 void VisitRecordDecl(const RecordDecl *D);
417 void VisitEnumConstantDecl(const EnumConstantDecl *D);
418 void VisitIndirectFieldDecl(const IndirectFieldDecl *D);
419 void VisitFunctionDecl(const FunctionDecl *D);
420 void VisitFieldDecl(const FieldDecl *D);
421 void VisitVarDecl(const VarDecl *D);
422 void VisitFileScopeAsmDecl(const FileScopeAsmDecl *D);
423 void VisitImportDecl(const ImportDecl *D);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000424
425 // C++ Decls
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000426 void VisitNamespaceDecl(const NamespaceDecl *D);
427 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D);
428 void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D);
429 void VisitTypeAliasDecl(const TypeAliasDecl *D);
430 void VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D);
431 void VisitCXXRecordDecl(const CXXRecordDecl *D);
432 void VisitStaticAssertDecl(const StaticAssertDecl *D);
Richard Smithcbdf7332014-03-18 02:07:28 +0000433 template<typename SpecializationDecl>
Richard Smithf7514452014-10-30 21:02:37 +0000434 void VisitTemplateDeclSpecialization(const SpecializationDecl *D,
Richard Smithcbdf7332014-03-18 02:07:28 +0000435 bool DumpExplicitInst,
436 bool DumpRefOnly);
Richard Smith20ade552014-03-17 23:34:53 +0000437 template<typename TemplateDecl>
438 void VisitTemplateDecl(const TemplateDecl *D, bool DumpExplicitInst);
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000439 void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D);
440 void VisitClassTemplateDecl(const ClassTemplateDecl *D);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000441 void VisitClassTemplateSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000442 const ClassTemplateSpecializationDecl *D);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000443 void VisitClassTemplatePartialSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000444 const ClassTemplatePartialSpecializationDecl *D);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000445 void VisitClassScopeFunctionSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000446 const ClassScopeFunctionSpecializationDecl *D);
Richard Smithd25789a2013-09-18 01:36:02 +0000447 void VisitVarTemplateDecl(const VarTemplateDecl *D);
448 void VisitVarTemplateSpecializationDecl(
449 const VarTemplateSpecializationDecl *D);
450 void VisitVarTemplatePartialSpecializationDecl(
451 const VarTemplatePartialSpecializationDecl *D);
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000452 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D);
453 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D);
454 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D);
455 void VisitUsingDecl(const UsingDecl *D);
456 void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D);
457 void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D);
458 void VisitUsingShadowDecl(const UsingShadowDecl *D);
459 void VisitLinkageSpecDecl(const LinkageSpecDecl *D);
460 void VisitAccessSpecDecl(const AccessSpecDecl *D);
461 void VisitFriendDecl(const FriendDecl *D);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000462
463 // ObjC Decls
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000464 void VisitObjCIvarDecl(const ObjCIvarDecl *D);
465 void VisitObjCMethodDecl(const ObjCMethodDecl *D);
466 void VisitObjCCategoryDecl(const ObjCCategoryDecl *D);
467 void VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D);
468 void VisitObjCProtocolDecl(const ObjCProtocolDecl *D);
469 void VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D);
470 void VisitObjCImplementationDecl(const ObjCImplementationDecl *D);
471 void VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D);
472 void VisitObjCPropertyDecl(const ObjCPropertyDecl *D);
473 void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D);
474 void VisitBlockDecl(const BlockDecl *D);
Mike Stump11289f42009-09-09 15:08:12 +0000475
Chris Lattner84ca3762007-08-30 01:00:35 +0000476 // Stmts.
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000477 void VisitStmt(const Stmt *Node);
478 void VisitDeclStmt(const DeclStmt *Node);
479 void VisitAttributedStmt(const AttributedStmt *Node);
480 void VisitLabelStmt(const LabelStmt *Node);
481 void VisitGotoStmt(const GotoStmt *Node);
Pavel Labath1ef83422013-09-04 14:35:00 +0000482 void VisitCXXCatchStmt(const CXXCatchStmt *Node);
Mike Stump11289f42009-09-09 15:08:12 +0000483
Chris Lattner84ca3762007-08-30 01:00:35 +0000484 // Exprs
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000485 void VisitExpr(const Expr *Node);
486 void VisitCastExpr(const CastExpr *Node);
487 void VisitDeclRefExpr(const DeclRefExpr *Node);
488 void VisitPredefinedExpr(const PredefinedExpr *Node);
489 void VisitCharacterLiteral(const CharacterLiteral *Node);
490 void VisitIntegerLiteral(const IntegerLiteral *Node);
491 void VisitFloatingLiteral(const FloatingLiteral *Node);
492 void VisitStringLiteral(const StringLiteral *Str);
Richard Smithf0514962014-06-03 08:24:28 +0000493 void VisitInitListExpr(const InitListExpr *ILE);
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000494 void VisitUnaryOperator(const UnaryOperator *Node);
495 void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Node);
496 void VisitMemberExpr(const MemberExpr *Node);
497 void VisitExtVectorElementExpr(const ExtVectorElementExpr *Node);
498 void VisitBinaryOperator(const BinaryOperator *Node);
499 void VisitCompoundAssignOperator(const CompoundAssignOperator *Node);
500 void VisitAddrLabelExpr(const AddrLabelExpr *Node);
501 void VisitBlockExpr(const BlockExpr *Node);
502 void VisitOpaqueValueExpr(const OpaqueValueExpr *Node);
Chris Lattner84ca3762007-08-30 01:00:35 +0000503
504 // C++
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000505 void VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node);
506 void VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node);
507 void VisitCXXThisExpr(const CXXThisExpr *Node);
508 void VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node);
509 void VisitCXXConstructExpr(const CXXConstructExpr *Node);
510 void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node);
Richard Smithe6c01442013-06-05 00:46:14 +0000511 void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node);
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000512 void VisitExprWithCleanups(const ExprWithCleanups *Node);
513 void VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node);
514 void dumpCXXTemporary(const CXXTemporary *Temporary);
Faisal Vali2b391ab2013-09-26 19:54:12 +0000515 void VisitLambdaExpr(const LambdaExpr *Node) {
516 VisitExpr(Node);
517 dumpDecl(Node->getLambdaClass());
518 }
Serge Pavlov6b926032015-02-16 19:58:41 +0000519 void VisitSizeOfPackExpr(const SizeOfPackExpr *Node);
Mike Stump11289f42009-09-09 15:08:12 +0000520
Chris Lattner84ca3762007-08-30 01:00:35 +0000521 // ObjC
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000522 void VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node);
523 void VisitObjCEncodeExpr(const ObjCEncodeExpr *Node);
524 void VisitObjCMessageExpr(const ObjCMessageExpr *Node);
525 void VisitObjCBoxedExpr(const ObjCBoxedExpr *Node);
526 void VisitObjCSelectorExpr(const ObjCSelectorExpr *Node);
527 void VisitObjCProtocolExpr(const ObjCProtocolExpr *Node);
528 void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node);
529 void VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node);
530 void VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node);
531 void VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node);
Alexander Kornienkoebc17b52013-01-14 14:07:11 +0000532
533 // Comments.
534 const char *getCommandName(unsigned CommandID);
535 void dumpComment(const Comment *C);
536
537 // Inline comments.
538 void visitTextComment(const TextComment *C);
539 void visitInlineCommandComment(const InlineCommandComment *C);
540 void visitHTMLStartTagComment(const HTMLStartTagComment *C);
541 void visitHTMLEndTagComment(const HTMLEndTagComment *C);
542
543 // Block comments.
544 void visitBlockCommandComment(const BlockCommandComment *C);
545 void visitParamCommandComment(const ParamCommandComment *C);
546 void visitTParamCommandComment(const TParamCommandComment *C);
547 void visitVerbatimBlockComment(const VerbatimBlockComment *C);
548 void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C);
549 void visitVerbatimLineComment(const VerbatimLineComment *C);
Chris Lattnercbe4f772007-08-08 22:51:59 +0000550 };
551}
552
553//===----------------------------------------------------------------------===//
Chris Lattner11e30d32007-08-30 06:17:34 +0000554// Utilities
555//===----------------------------------------------------------------------===//
556
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000557void ASTDumper::dumpPointer(const void *Ptr) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000558 ColorScope Color(*this, AddressColor);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000559 OS << ' ' << Ptr;
560}
561
Alexander Kornienko18ec81b2012-12-13 13:59:55 +0000562void ASTDumper::dumpLocation(SourceLocation Loc) {
Alex McCarthye14ddef2014-05-02 20:24:11 +0000563 if (!SM)
564 return;
565
Richard Trieud215b8d2013-01-26 01:31:20 +0000566 ColorScope Color(*this, LocationColor);
Chris Lattner53e384f2009-01-16 07:00:02 +0000567 SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000568
Chris Lattner11e30d32007-08-30 06:17:34 +0000569 // The general format we print out is filename:line:col, but we drop pieces
570 // that haven't changed since the last loc printed.
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000571 PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
572
Douglas Gregor453b0122010-11-12 07:15:47 +0000573 if (PLoc.isInvalid()) {
574 OS << "<invalid sloc>";
575 return;
576 }
577
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000578 if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000579 OS << PLoc.getFilename() << ':' << PLoc.getLine()
580 << ':' << PLoc.getColumn();
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000581 LastLocFilename = PLoc.getFilename();
582 LastLocLine = PLoc.getLine();
583 } else if (PLoc.getLine() != LastLocLine) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000584 OS << "line" << ':' << PLoc.getLine()
585 << ':' << PLoc.getColumn();
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000586 LastLocLine = PLoc.getLine();
Chris Lattner11e30d32007-08-30 06:17:34 +0000587 } else {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000588 OS << "col" << ':' << PLoc.getColumn();
Chris Lattner11e30d32007-08-30 06:17:34 +0000589 }
590}
591
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000592void ASTDumper::dumpSourceRange(SourceRange R) {
Chris Lattner11e30d32007-08-30 06:17:34 +0000593 // Can't translate locations if a SourceManager isn't available.
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000594 if (!SM)
595 return;
Mike Stump11289f42009-09-09 15:08:12 +0000596
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000597 OS << " <";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000598 dumpLocation(R.getBegin());
Chris Lattnera7c19fe2007-10-16 22:36:42 +0000599 if (R.getBegin() != R.getEnd()) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000600 OS << ", ";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000601 dumpLocation(R.getEnd());
Chris Lattner11e30d32007-08-30 06:17:34 +0000602 }
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000603 OS << ">";
Mike Stump11289f42009-09-09 15:08:12 +0000604
Chris Lattner11e30d32007-08-30 06:17:34 +0000605 // <t2.c:123:421[blah], t2.c:412:321>
606
607}
608
Richard Smithd5e7ff82014-10-31 01:17:45 +0000609void ASTDumper::dumpBareType(QualType T, bool Desugar) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000610 ColorScope Color(*this, TypeColor);
Richard Smithd5e7ff82014-10-31 01:17:45 +0000611
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000612 SplitQualType T_split = T.split();
613 OS << "'" << QualType::getAsString(T_split) << "'";
614
Richard Smithd5e7ff82014-10-31 01:17:45 +0000615 if (Desugar && !T.isNull()) {
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000616 // If the type is sugared, also dump a (shallow) desugared type.
617 SplitQualType D_split = T.getSplitDesugaredType();
618 if (T_split != D_split)
619 OS << ":'" << QualType::getAsString(D_split) << "'";
620 }
621}
622
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000623void ASTDumper::dumpType(QualType T) {
624 OS << ' ';
625 dumpBareType(T);
626}
627
Richard Smithd5e7ff82014-10-31 01:17:45 +0000628void ASTDumper::dumpTypeAsChild(QualType T) {
629 SplitQualType SQT = T.split();
630 if (!SQT.Quals.hasQualifiers())
631 return dumpTypeAsChild(SQT.Ty);
632
633 dumpChild([=] {
634 OS << "QualType";
635 dumpPointer(T.getAsOpaquePtr());
636 OS << " ";
637 dumpBareType(T, false);
638 OS << " " << T.split().Quals.getAsString();
639 dumpTypeAsChild(T.split().Ty);
640 });
641}
642
643void ASTDumper::dumpTypeAsChild(const Type *T) {
644 dumpChild([=] {
645 if (!T) {
646 ColorScope Color(*this, NullColor);
647 OS << "<<<NULL>>>";
648 return;
649 }
650
651 {
652 ColorScope Color(*this, TypeColor);
653 OS << T->getTypeClassName() << "Type";
654 }
655 dumpPointer(T);
656 OS << " ";
657 dumpBareType(QualType(T, 0), false);
658
659 QualType SingleStepDesugar =
660 T->getLocallyUnqualifiedSingleStepDesugaredType();
661 if (SingleStepDesugar != QualType(T, 0))
662 OS << " sugar";
663 if (T->isDependentType())
664 OS << " dependent";
665 else if (T->isInstantiationDependentType())
666 OS << " instantiation_dependent";
667 if (T->isVariablyModifiedType())
668 OS << " variably_modified";
669 if (T->containsUnexpandedParameterPack())
670 OS << " contains_unexpanded_pack";
671 if (T->isFromAST())
672 OS << " imported";
673
674 TypeVisitor<ASTDumper>::Visit(T);
675
676 if (SingleStepDesugar != QualType(T, 0))
677 dumpTypeAsChild(SingleStepDesugar);
678 });
679}
680
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000681void ASTDumper::dumpBareDeclRef(const Decl *D) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000682 {
683 ColorScope Color(*this, DeclKindNameColor);
684 OS << D->getDeclKindName();
685 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000686 dumpPointer(D);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000687
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000688 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000689 ColorScope Color(*this, DeclNameColor);
David Blaikied4da8722013-05-14 21:04:00 +0000690 OS << " '" << ND->getDeclName() << '\'';
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000691 }
692
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000693 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000694 dumpType(VD->getType());
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000695}
696
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000697void ASTDumper::dumpDeclRef(const Decl *D, const char *Label) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000698 if (!D)
699 return;
700
Richard Smithf7514452014-10-30 21:02:37 +0000701 dumpChild([=]{
702 if (Label)
703 OS << Label << ' ';
704 dumpBareDeclRef(D);
705 });
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000706}
707
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000708void ASTDumper::dumpName(const NamedDecl *ND) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000709 if (ND->getDeclName()) {
710 ColorScope Color(*this, DeclNameColor);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000711 OS << ' ' << ND->getNameAsString();
Richard Trieud215b8d2013-01-26 01:31:20 +0000712 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000713}
714
Richard Trieude5cc7d2013-01-31 01:44:26 +0000715bool ASTDumper::hasNodes(const DeclContext *DC) {
716 if (!DC)
717 return false;
718
Richard Smith1d209d02013-05-23 01:49:11 +0000719 return DC->hasExternalLexicalStorage() ||
720 DC->noload_decls_begin() != DC->noload_decls_end();
Richard Trieude5cc7d2013-01-31 01:44:26 +0000721}
722
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000723void ASTDumper::dumpDeclContext(const DeclContext *DC) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000724 if (!DC)
725 return;
Richard Smithdcc2c452014-03-17 23:00:06 +0000726
Richard Smithdcc2c452014-03-17 23:00:06 +0000727 for (auto *D : DC->noload_decls())
Richard Smithf7514452014-10-30 21:02:37 +0000728 dumpDecl(D);
Richard Smithdcc2c452014-03-17 23:00:06 +0000729
730 if (DC->hasExternalLexicalStorage()) {
Richard Smithf7514452014-10-30 21:02:37 +0000731 dumpChild([=]{
732 ColorScope Color(*this, UndeserializedColor);
733 OS << "<undeserialized declarations>";
734 });
Richard Smith1d209d02013-05-23 01:49:11 +0000735 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000736}
737
Richard Smith35f986d2014-08-11 22:11:07 +0000738void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) {
Richard Smithf7514452014-10-30 21:02:37 +0000739 dumpChild([=] {
740 OS << "StoredDeclsMap ";
741 dumpBareDeclRef(cast<Decl>(DC));
Richard Smith33937e72013-06-22 21:49:40 +0000742
Richard Smithf7514452014-10-30 21:02:37 +0000743 const DeclContext *Primary = DC->getPrimaryContext();
744 if (Primary != DC) {
745 OS << " primary";
746 dumpPointer(cast<Decl>(Primary));
Richard Smith33937e72013-06-22 21:49:40 +0000747 }
748
Richard Smithf7514452014-10-30 21:02:37 +0000749 bool HasUndeserializedLookups = Primary->hasExternalVisibleStorage();
Richard Smith35f986d2014-08-11 22:11:07 +0000750
Richard Smithf7514452014-10-30 21:02:37 +0000751 DeclContext::all_lookups_iterator I = Primary->noload_lookups_begin(),
752 E = Primary->noload_lookups_end();
753 while (I != E) {
754 DeclarationName Name = I.getLookupName();
755 DeclContextLookupResult R = *I++;
Richard Smith35f986d2014-08-11 22:11:07 +0000756
Richard Smithf7514452014-10-30 21:02:37 +0000757 dumpChild([=] {
758 OS << "DeclarationName ";
759 {
760 ColorScope Color(*this, DeclNameColor);
761 OS << '\'' << Name << '\'';
762 }
Richard Smith35f986d2014-08-11 22:11:07 +0000763
Richard Smithf7514452014-10-30 21:02:37 +0000764 for (DeclContextLookupResult::iterator RI = R.begin(), RE = R.end();
765 RI != RE; ++RI) {
766 dumpChild([=] {
767 dumpBareDeclRef(*RI);
768
769 if ((*RI)->isHidden())
770 OS << " hidden";
771
772 // If requested, dump the redecl chain for this lookup.
773 if (DumpDecls) {
774 // Dump earliest decl first.
775 std::function<void(Decl *)> DumpWithPrev = [&](Decl *D) {
776 if (Decl *Prev = D->getPreviousDecl())
777 DumpWithPrev(Prev);
778 dumpDecl(D);
779 };
780 DumpWithPrev(*RI);
781 }
782 });
783 }
784 });
Richard Smith33937e72013-06-22 21:49:40 +0000785 }
Richard Smith33937e72013-06-22 21:49:40 +0000786
Richard Smithf7514452014-10-30 21:02:37 +0000787 if (HasUndeserializedLookups) {
788 dumpChild([=] {
789 ColorScope Color(*this, UndeserializedColor);
790 OS << "<undeserialized lookups>";
791 });
792 }
793 });
Richard Smith33937e72013-06-22 21:49:40 +0000794}
795
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000796void ASTDumper::dumpAttr(const Attr *A) {
Richard Smithf7514452014-10-30 21:02:37 +0000797 dumpChild([=] {
798 {
799 ColorScope Color(*this, AttrColor);
Aaron Ballman36a53502014-01-16 13:03:14 +0000800
Richard Smithf7514452014-10-30 21:02:37 +0000801 switch (A->getKind()) {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000802#define ATTR(X) case attr::X: OS << #X; break;
803#include "clang/Basic/AttrList.inc"
Richard Smithf7514452014-10-30 21:02:37 +0000804 default:
805 llvm_unreachable("unexpected attribute kind");
806 }
807 OS << "Attr";
Richard Trieud215b8d2013-01-26 01:31:20 +0000808 }
Richard Smithf7514452014-10-30 21:02:37 +0000809 dumpPointer(A);
810 dumpSourceRange(A->getRange());
811 if (A->isInherited())
812 OS << " Inherited";
813 if (A->isImplicit())
814 OS << " Implicit";
Hans Wennborgb0a8b4a2014-05-31 04:05:57 +0000815#include "clang/AST/AttrDump.inc"
Richard Smithf7514452014-10-30 21:02:37 +0000816 });
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000817}
818
Richard Smith71bec062013-10-15 21:58:30 +0000819static void dumpPreviousDeclImpl(raw_ostream &OS, ...) {}
820
821template<typename T>
822static void dumpPreviousDeclImpl(raw_ostream &OS, const Mergeable<T> *D) {
Rafael Espindola8db352d2013-10-17 15:37:26 +0000823 const T *First = D->getFirstDecl();
Richard Smith71bec062013-10-15 21:58:30 +0000824 if (First != D)
825 OS << " first " << First;
Richard Smithf5f43542013-02-07 01:35:44 +0000826}
827
828template<typename T>
Richard Smith71bec062013-10-15 21:58:30 +0000829static void dumpPreviousDeclImpl(raw_ostream &OS, const Redeclarable<T> *D) {
830 const T *Prev = D->getPreviousDecl();
831 if (Prev)
832 OS << " prev " << Prev;
Richard Smithf5f43542013-02-07 01:35:44 +0000833}
834
Richard Smith71bec062013-10-15 21:58:30 +0000835/// Dump the previous declaration in the redeclaration chain for a declaration,
836/// if any.
837static void dumpPreviousDecl(raw_ostream &OS, const Decl *D) {
Richard Smithf5f43542013-02-07 01:35:44 +0000838 switch (D->getKind()) {
839#define DECL(DERIVED, BASE) \
840 case Decl::DERIVED: \
Richard Smith71bec062013-10-15 21:58:30 +0000841 return dumpPreviousDeclImpl(OS, cast<DERIVED##Decl>(D));
Richard Smithf5f43542013-02-07 01:35:44 +0000842#define ABSTRACT_DECL(DECL)
843#include "clang/AST/DeclNodes.inc"
844 }
845 llvm_unreachable("Decl that isn't part of DeclNodes.inc!");
846}
847
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000848//===----------------------------------------------------------------------===//
849// C++ Utilities
850//===----------------------------------------------------------------------===//
851
852void ASTDumper::dumpAccessSpecifier(AccessSpecifier AS) {
853 switch (AS) {
854 case AS_none:
855 break;
856 case AS_public:
857 OS << "public";
858 break;
859 case AS_protected:
860 OS << "protected";
861 break;
862 case AS_private:
863 OS << "private";
864 break;
865 }
866}
867
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000868void ASTDumper::dumpCXXCtorInitializer(const CXXCtorInitializer *Init) {
Richard Smithf7514452014-10-30 21:02:37 +0000869 dumpChild([=] {
870 OS << "CXXCtorInitializer";
871 if (Init->isAnyMemberInitializer()) {
872 OS << ' ';
873 dumpBareDeclRef(Init->getAnyMember());
874 } else if (Init->isBaseInitializer()) {
875 dumpType(QualType(Init->getBaseClass(), 0));
876 } else if (Init->isDelegatingInitializer()) {
877 dumpType(Init->getTypeSourceInfo()->getType());
878 } else {
879 llvm_unreachable("Unknown initializer type");
880 }
881 dumpStmt(Init->getInit());
882 });
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000883}
884
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000885void ASTDumper::dumpTemplateParameters(const TemplateParameterList *TPL) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000886 if (!TPL)
887 return;
888
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000889 for (TemplateParameterList::const_iterator I = TPL->begin(), E = TPL->end();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000890 I != E; ++I)
891 dumpDecl(*I);
892}
893
894void ASTDumper::dumpTemplateArgumentListInfo(
895 const TemplateArgumentListInfo &TALI) {
Richard Smithf7514452014-10-30 21:02:37 +0000896 for (unsigned i = 0, e = TALI.size(); i < e; ++i)
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000897 dumpTemplateArgumentLoc(TALI[i]);
898}
899
900void ASTDumper::dumpTemplateArgumentLoc(const TemplateArgumentLoc &A) {
901 dumpTemplateArgument(A.getArgument(), A.getSourceRange());
902}
903
904void ASTDumper::dumpTemplateArgumentList(const TemplateArgumentList &TAL) {
905 for (unsigned i = 0, e = TAL.size(); i < e; ++i)
906 dumpTemplateArgument(TAL[i]);
907}
908
909void ASTDumper::dumpTemplateArgument(const TemplateArgument &A, SourceRange R) {
Richard Smithf7514452014-10-30 21:02:37 +0000910 dumpChild([=] {
911 OS << "TemplateArgument";
912 if (R.isValid())
913 dumpSourceRange(R);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000914
Richard Smithf7514452014-10-30 21:02:37 +0000915 switch (A.getKind()) {
916 case TemplateArgument::Null:
917 OS << " null";
918 break;
919 case TemplateArgument::Type:
920 OS << " type";
921 dumpType(A.getAsType());
922 break;
923 case TemplateArgument::Declaration:
924 OS << " decl";
925 dumpDeclRef(A.getAsDecl());
926 break;
927 case TemplateArgument::NullPtr:
928 OS << " nullptr";
929 break;
930 case TemplateArgument::Integral:
931 OS << " integral " << A.getAsIntegral();
932 break;
933 case TemplateArgument::Template:
934 OS << " template ";
935 A.getAsTemplate().dump(OS);
936 break;
937 case TemplateArgument::TemplateExpansion:
938 OS << " template expansion";
939 A.getAsTemplateOrTemplatePattern().dump(OS);
940 break;
941 case TemplateArgument::Expression:
942 OS << " expr";
943 dumpStmt(A.getAsExpr());
944 break;
945 case TemplateArgument::Pack:
946 OS << " pack";
947 for (TemplateArgument::pack_iterator I = A.pack_begin(), E = A.pack_end();
948 I != E; ++I)
949 dumpTemplateArgument(*I);
950 break;
Richard Trieude5cc7d2013-01-31 01:44:26 +0000951 }
Richard Smithf7514452014-10-30 21:02:37 +0000952 });
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000953}
954
Chris Lattner11e30d32007-08-30 06:17:34 +0000955//===----------------------------------------------------------------------===//
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000956// Decl dumping methods.
Chris Lattnercbe4f772007-08-08 22:51:59 +0000957//===----------------------------------------------------------------------===//
958
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000959void ASTDumper::dumpDecl(const Decl *D) {
Richard Smithf7514452014-10-30 21:02:37 +0000960 dumpChild([=] {
961 if (!D) {
962 ColorScope Color(*this, NullColor);
963 OS << "<<<NULL>>>";
964 return;
965 }
Mike Stump11289f42009-09-09 15:08:12 +0000966
Richard Smithf7514452014-10-30 21:02:37 +0000967 {
968 ColorScope Color(*this, DeclKindNameColor);
969 OS << D->getDeclKindName() << "Decl";
970 }
971 dumpPointer(D);
972 if (D->getLexicalDeclContext() != D->getDeclContext())
973 OS << " parent " << cast<Decl>(D->getDeclContext());
974 dumpPreviousDecl(OS, D);
975 dumpSourceRange(D->getSourceRange());
976 OS << ' ';
977 dumpLocation(D->getLocation());
978 if (Module *M = D->getOwningModule())
979 OS << " in " << M->getFullModuleName();
980 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
981 if (ND->isHidden())
982 OS << " hidden";
983 if (D->isImplicit())
984 OS << " implicit";
985 if (D->isUsed())
986 OS << " used";
987 else if (D->isThisDeclarationReferenced())
988 OS << " referenced";
989 if (D->isInvalidDecl())
990 OS << " invalid";
Hans Wennborg76b00532014-12-05 22:38:57 +0000991 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
992 if (FD->isConstexpr())
993 OS << " constexpr";
994
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000995
Richard Smithf7514452014-10-30 21:02:37 +0000996 ConstDeclVisitor<ASTDumper>::Visit(D);
Richard Trieude5cc7d2013-01-31 01:44:26 +0000997
Richard Smithf7514452014-10-30 21:02:37 +0000998 for (Decl::attr_iterator I = D->attr_begin(), E = D->attr_end(); I != E;
999 ++I)
1000 dumpAttr(*I);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001001
Richard Smithf7514452014-10-30 21:02:37 +00001002 if (const FullComment *Comment =
1003 D->getASTContext().getLocalCommentForDeclUncached(D))
1004 dumpFullComment(Comment);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001005
Richard Smithf7514452014-10-30 21:02:37 +00001006 // Decls within functions are visited by the body.
1007 if (!isa<FunctionDecl>(*D) && !isa<ObjCMethodDecl>(*D) &&
1008 hasNodes(dyn_cast<DeclContext>(D)))
1009 dumpDeclContext(cast<DeclContext>(D));
1010 });
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001011}
1012
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001013void ASTDumper::VisitLabelDecl(const LabelDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001014 dumpName(D);
1015}
1016
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001017void ASTDumper::VisitTypedefDecl(const TypedefDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001018 dumpName(D);
1019 dumpType(D->getUnderlyingType());
1020 if (D->isModulePrivate())
1021 OS << " __module_private__";
1022}
1023
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001024void ASTDumper::VisitEnumDecl(const EnumDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001025 if (D->isScoped()) {
1026 if (D->isScopedUsingClassTag())
1027 OS << " class";
1028 else
1029 OS << " struct";
1030 }
1031 dumpName(D);
1032 if (D->isModulePrivate())
1033 OS << " __module_private__";
1034 if (D->isFixed())
1035 dumpType(D->getIntegerType());
1036}
1037
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001038void ASTDumper::VisitRecordDecl(const RecordDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001039 OS << ' ' << D->getKindName();
1040 dumpName(D);
1041 if (D->isModulePrivate())
1042 OS << " __module_private__";
Richard Smith99bc1b92013-08-30 05:32:29 +00001043 if (D->isCompleteDefinition())
1044 OS << " definition";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001045}
1046
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001047void ASTDumper::VisitEnumConstantDecl(const EnumConstantDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001048 dumpName(D);
1049 dumpType(D->getType());
Richard Smithf7514452014-10-30 21:02:37 +00001050 if (const Expr *Init = D->getInitExpr())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001051 dumpStmt(Init);
1052}
1053
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001054void ASTDumper::VisitIndirectFieldDecl(const IndirectFieldDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001055 dumpName(D);
1056 dumpType(D->getType());
Richard Smithdcc2c452014-03-17 23:00:06 +00001057
Richard Smith8aa49222014-03-18 00:35:12 +00001058 for (auto *Child : D->chain())
Richard Smithf7514452014-10-30 21:02:37 +00001059 dumpDeclRef(Child);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001060}
1061
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001062void ASTDumper::VisitFunctionDecl(const FunctionDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001063 dumpName(D);
1064 dumpType(D->getType());
1065
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001066 StorageClass SC = D->getStorageClass();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001067 if (SC != SC_None)
1068 OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
1069 if (D->isInlineSpecified())
1070 OS << " inline";
1071 if (D->isVirtualAsWritten())
1072 OS << " virtual";
1073 if (D->isModulePrivate())
1074 OS << " __module_private__";
1075
1076 if (D->isPure())
1077 OS << " pure";
1078 else if (D->isDeletedAsWritten())
1079 OS << " delete";
1080
Richard Smithadaa0152013-05-17 02:09:46 +00001081 if (const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>()) {
1082 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
Richard Smith8acb4282014-07-31 21:57:55 +00001083 switch (EPI.ExceptionSpec.Type) {
Richard Smithadaa0152013-05-17 02:09:46 +00001084 default: break;
1085 case EST_Unevaluated:
Richard Smith8acb4282014-07-31 21:57:55 +00001086 OS << " noexcept-unevaluated " << EPI.ExceptionSpec.SourceDecl;
Richard Smithadaa0152013-05-17 02:09:46 +00001087 break;
1088 case EST_Uninstantiated:
Richard Smith8acb4282014-07-31 21:57:55 +00001089 OS << " noexcept-uninstantiated " << EPI.ExceptionSpec.SourceTemplate;
Richard Smithadaa0152013-05-17 02:09:46 +00001090 break;
1091 }
1092 }
1093
Richard Smithf7514452014-10-30 21:02:37 +00001094 if (const FunctionTemplateSpecializationInfo *FTSI =
1095 D->getTemplateSpecializationInfo())
Richard Trieude5cc7d2013-01-31 01:44:26 +00001096 dumpTemplateArgumentList(*FTSI->TemplateArguments);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001097
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001098 for (ArrayRef<NamedDecl *>::iterator
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001099 I = D->getDeclsInPrototypeScope().begin(),
Richard Smithf7514452014-10-30 21:02:37 +00001100 E = D->getDeclsInPrototypeScope().end(); I != E; ++I)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001101 dumpDecl(*I);
1102
Richard Smith8a639892015-01-24 01:07:20 +00001103 if (!D->param_begin() && D->getNumParams())
1104 dumpChild([=] { OS << "<<NULL params x " << D->getNumParams() << ">>"; });
1105 else
1106 for (FunctionDecl::param_const_iterator I = D->param_begin(),
1107 E = D->param_end();
1108 I != E; ++I)
1109 dumpDecl(*I);
Richard Smithf7514452014-10-30 21:02:37 +00001110
1111 if (const CXXConstructorDecl *C = dyn_cast<CXXConstructorDecl>(D))
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001112 for (CXXConstructorDecl::init_const_iterator I = C->init_begin(),
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001113 E = C->init_end();
Richard Smithf7514452014-10-30 21:02:37 +00001114 I != E; ++I)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001115 dumpCXXCtorInitializer(*I);
1116
Richard Smithf7514452014-10-30 21:02:37 +00001117 if (D->doesThisDeclarationHaveABody())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001118 dumpStmt(D->getBody());
1119}
1120
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001121void ASTDumper::VisitFieldDecl(const FieldDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001122 dumpName(D);
1123 dumpType(D->getType());
1124 if (D->isMutable())
1125 OS << " mutable";
1126 if (D->isModulePrivate())
1127 OS << " __module_private__";
Richard Trieude5cc7d2013-01-31 01:44:26 +00001128
Richard Smithf7514452014-10-30 21:02:37 +00001129 if (D->isBitField())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001130 dumpStmt(D->getBitWidth());
Richard Smithf7514452014-10-30 21:02:37 +00001131 if (Expr *Init = D->getInClassInitializer())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001132 dumpStmt(Init);
1133}
1134
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001135void ASTDumper::VisitVarDecl(const VarDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001136 dumpName(D);
1137 dumpType(D->getType());
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001138 StorageClass SC = D->getStorageClass();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001139 if (SC != SC_None)
1140 OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
Richard Smithfd3834f2013-04-13 02:43:54 +00001141 switch (D->getTLSKind()) {
1142 case VarDecl::TLS_None: break;
1143 case VarDecl::TLS_Static: OS << " tls"; break;
1144 case VarDecl::TLS_Dynamic: OS << " tls_dynamic"; break;
1145 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001146 if (D->isModulePrivate())
1147 OS << " __module_private__";
1148 if (D->isNRVOVariable())
1149 OS << " nrvo";
Richard Trieude5cc7d2013-01-31 01:44:26 +00001150 if (D->hasInit()) {
Richard Smithd9967cc2014-07-10 22:54:03 +00001151 switch (D->getInitStyle()) {
1152 case VarDecl::CInit: OS << " cinit"; break;
1153 case VarDecl::CallInit: OS << " callinit"; break;
1154 case VarDecl::ListInit: OS << " listinit"; break;
1155 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001156 dumpStmt(D->getInit());
Richard Trieude5cc7d2013-01-31 01:44:26 +00001157 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001158}
1159
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001160void ASTDumper::VisitFileScopeAsmDecl(const FileScopeAsmDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001161 dumpStmt(D->getAsmString());
1162}
1163
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001164void ASTDumper::VisitImportDecl(const ImportDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001165 OS << ' ' << D->getImportedModule()->getFullModuleName();
1166}
1167
1168//===----------------------------------------------------------------------===//
1169// C++ Declarations
1170//===----------------------------------------------------------------------===//
1171
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001172void ASTDumper::VisitNamespaceDecl(const NamespaceDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001173 dumpName(D);
1174 if (D->isInline())
1175 OS << " inline";
1176 if (!D->isOriginalNamespace())
1177 dumpDeclRef(D->getOriginalNamespace(), "original");
1178}
1179
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001180void ASTDumper::VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001181 OS << ' ';
1182 dumpBareDeclRef(D->getNominatedNamespace());
1183}
1184
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001185void ASTDumper::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001186 dumpName(D);
1187 dumpDeclRef(D->getAliasedNamespace());
1188}
1189
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001190void ASTDumper::VisitTypeAliasDecl(const TypeAliasDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001191 dumpName(D);
1192 dumpType(D->getUnderlyingType());
1193}
1194
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001195void ASTDumper::VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001196 dumpName(D);
1197 dumpTemplateParameters(D->getTemplateParameters());
1198 dumpDecl(D->getTemplatedDecl());
1199}
1200
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001201void ASTDumper::VisitCXXRecordDecl(const CXXRecordDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001202 VisitRecordDecl(D);
1203 if (!D->isCompleteDefinition())
1204 return;
1205
Aaron Ballman574705e2014-03-13 15:41:46 +00001206 for (const auto &I : D->bases()) {
Richard Smithf7514452014-10-30 21:02:37 +00001207 dumpChild([=] {
1208 if (I.isVirtual())
1209 OS << "virtual ";
1210 dumpAccessSpecifier(I.getAccessSpecifier());
1211 dumpType(I.getType());
1212 if (I.isPackExpansion())
1213 OS << "...";
1214 });
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001215 }
1216}
1217
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001218void ASTDumper::VisitStaticAssertDecl(const StaticAssertDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001219 dumpStmt(D->getAssertExpr());
1220 dumpStmt(D->getMessage());
1221}
1222
Richard Smithcbdf7332014-03-18 02:07:28 +00001223template<typename SpecializationDecl>
Richard Smithf7514452014-10-30 21:02:37 +00001224void ASTDumper::VisitTemplateDeclSpecialization(const SpecializationDecl *D,
Richard Smithcbdf7332014-03-18 02:07:28 +00001225 bool DumpExplicitInst,
1226 bool DumpRefOnly) {
1227 bool DumpedAny = false;
1228 for (auto *RedeclWithBadType : D->redecls()) {
1229 // FIXME: The redecls() range sometimes has elements of a less-specific
1230 // type. (In particular, ClassTemplateSpecializationDecl::redecls() gives
1231 // us TagDecls, and should give CXXRecordDecls).
1232 auto *Redecl = dyn_cast<SpecializationDecl>(RedeclWithBadType);
1233 if (!Redecl) {
1234 // Found the injected-class-name for a class template. This will be dumped
1235 // as part of its surrounding class so we don't need to dump it here.
1236 assert(isa<CXXRecordDecl>(RedeclWithBadType) &&
1237 "expected an injected-class-name");
1238 continue;
1239 }
1240
1241 switch (Redecl->getTemplateSpecializationKind()) {
1242 case TSK_ExplicitInstantiationDeclaration:
1243 case TSK_ExplicitInstantiationDefinition:
1244 if (!DumpExplicitInst)
1245 break;
1246 // Fall through.
1247 case TSK_Undeclared:
1248 case TSK_ImplicitInstantiation:
Richard Smithf7514452014-10-30 21:02:37 +00001249 if (DumpRefOnly)
1250 dumpDeclRef(Redecl);
1251 else
1252 dumpDecl(Redecl);
Richard Smithcbdf7332014-03-18 02:07:28 +00001253 DumpedAny = true;
1254 break;
1255 case TSK_ExplicitSpecialization:
1256 break;
1257 }
1258 }
1259
1260 // Ensure we dump at least one decl for each specialization.
1261 if (!DumpedAny)
Richard Smithf7514452014-10-30 21:02:37 +00001262 dumpDeclRef(D);
Richard Smithcbdf7332014-03-18 02:07:28 +00001263}
1264
Richard Smith20ade552014-03-17 23:34:53 +00001265template<typename TemplateDecl>
1266void ASTDumper::VisitTemplateDecl(const TemplateDecl *D,
1267 bool DumpExplicitInst) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001268 dumpName(D);
1269 dumpTemplateParameters(D->getTemplateParameters());
Richard Smithdcc2c452014-03-17 23:00:06 +00001270
Richard Smithf7514452014-10-30 21:02:37 +00001271 dumpDecl(D->getTemplatedDecl());
Richard Smithdcc2c452014-03-17 23:00:06 +00001272
Richard Smithcbdf7332014-03-18 02:07:28 +00001273 for (auto *Child : D->specializations())
Richard Smithf7514452014-10-30 21:02:37 +00001274 VisitTemplateDeclSpecialization(Child, DumpExplicitInst,
Richard Smithcbdf7332014-03-18 02:07:28 +00001275 !D->isCanonicalDecl());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001276}
1277
Richard Smith20ade552014-03-17 23:34:53 +00001278void ASTDumper::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
1279 // FIXME: We don't add a declaration of a function template specialization
1280 // to its context when it's explicitly instantiated, so dump explicit
1281 // instantiations when we dump the template itself.
1282 VisitTemplateDecl(D, true);
1283}
1284
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001285void ASTDumper::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
Richard Smith20ade552014-03-17 23:34:53 +00001286 VisitTemplateDecl(D, false);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001287}
1288
1289void ASTDumper::VisitClassTemplateSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001290 const ClassTemplateSpecializationDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001291 VisitCXXRecordDecl(D);
1292 dumpTemplateArgumentList(D->getTemplateArgs());
1293}
1294
1295void ASTDumper::VisitClassTemplatePartialSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001296 const ClassTemplatePartialSpecializationDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001297 VisitClassTemplateSpecializationDecl(D);
1298 dumpTemplateParameters(D->getTemplateParameters());
1299}
1300
1301void ASTDumper::VisitClassScopeFunctionSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001302 const ClassScopeFunctionSpecializationDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001303 dumpDeclRef(D->getSpecialization());
1304 if (D->hasExplicitTemplateArgs())
1305 dumpTemplateArgumentListInfo(D->templateArgs());
1306}
1307
Richard Smithd25789a2013-09-18 01:36:02 +00001308void ASTDumper::VisitVarTemplateDecl(const VarTemplateDecl *D) {
Richard Smith20ade552014-03-17 23:34:53 +00001309 VisitTemplateDecl(D, false);
Richard Smithd25789a2013-09-18 01:36:02 +00001310}
1311
1312void ASTDumper::VisitVarTemplateSpecializationDecl(
1313 const VarTemplateSpecializationDecl *D) {
1314 dumpTemplateArgumentList(D->getTemplateArgs());
1315 VisitVarDecl(D);
1316}
1317
1318void ASTDumper::VisitVarTemplatePartialSpecializationDecl(
1319 const VarTemplatePartialSpecializationDecl *D) {
1320 dumpTemplateParameters(D->getTemplateParameters());
1321 VisitVarTemplateSpecializationDecl(D);
1322}
1323
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001324void ASTDumper::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001325 if (D->wasDeclaredWithTypename())
1326 OS << " typename";
1327 else
1328 OS << " class";
1329 if (D->isParameterPack())
1330 OS << " ...";
1331 dumpName(D);
Richard Smithf7514452014-10-30 21:02:37 +00001332 if (D->hasDefaultArgument())
Richard Smithecf74ff2014-03-23 20:50:39 +00001333 dumpTemplateArgument(D->getDefaultArgument());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001334}
1335
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001336void ASTDumper::VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001337 dumpType(D->getType());
1338 if (D->isParameterPack())
1339 OS << " ...";
1340 dumpName(D);
Richard Smithf7514452014-10-30 21:02:37 +00001341 if (D->hasDefaultArgument())
Richard Smithecf74ff2014-03-23 20:50:39 +00001342 dumpTemplateArgument(D->getDefaultArgument());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001343}
1344
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001345void ASTDumper::VisitTemplateTemplateParmDecl(
1346 const TemplateTemplateParmDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001347 if (D->isParameterPack())
1348 OS << " ...";
1349 dumpName(D);
1350 dumpTemplateParameters(D->getTemplateParameters());
Richard Smithf7514452014-10-30 21:02:37 +00001351 if (D->hasDefaultArgument())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001352 dumpTemplateArgumentLoc(D->getDefaultArgument());
1353}
1354
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001355void ASTDumper::VisitUsingDecl(const UsingDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001356 OS << ' ';
1357 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1358 OS << D->getNameAsString();
1359}
1360
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001361void ASTDumper::VisitUnresolvedUsingTypenameDecl(
1362 const UnresolvedUsingTypenameDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001363 OS << ' ';
1364 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1365 OS << D->getNameAsString();
1366}
1367
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001368void ASTDumper::VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001369 OS << ' ';
1370 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1371 OS << D->getNameAsString();
1372 dumpType(D->getType());
1373}
1374
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001375void ASTDumper::VisitUsingShadowDecl(const UsingShadowDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001376 OS << ' ';
1377 dumpBareDeclRef(D->getTargetDecl());
1378}
1379
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001380void ASTDumper::VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001381 switch (D->getLanguage()) {
1382 case LinkageSpecDecl::lang_c: OS << " C"; break;
1383 case LinkageSpecDecl::lang_cxx: OS << " C++"; break;
1384 }
1385}
1386
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001387void ASTDumper::VisitAccessSpecDecl(const AccessSpecDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001388 OS << ' ';
1389 dumpAccessSpecifier(D->getAccess());
1390}
1391
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001392void ASTDumper::VisitFriendDecl(const FriendDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001393 if (TypeSourceInfo *T = D->getFriendType())
1394 dumpType(T->getType());
1395 else
1396 dumpDecl(D->getFriendDecl());
1397}
1398
1399//===----------------------------------------------------------------------===//
1400// Obj-C Declarations
1401//===----------------------------------------------------------------------===//
1402
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001403void ASTDumper::VisitObjCIvarDecl(const ObjCIvarDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001404 dumpName(D);
1405 dumpType(D->getType());
1406 if (D->getSynthesize())
1407 OS << " synthesize";
1408
1409 switch (D->getAccessControl()) {
1410 case ObjCIvarDecl::None:
1411 OS << " none";
1412 break;
1413 case ObjCIvarDecl::Private:
1414 OS << " private";
1415 break;
1416 case ObjCIvarDecl::Protected:
1417 OS << " protected";
1418 break;
1419 case ObjCIvarDecl::Public:
1420 OS << " public";
1421 break;
1422 case ObjCIvarDecl::Package:
1423 OS << " package";
1424 break;
1425 }
1426}
1427
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001428void ASTDumper::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001429 if (D->isInstanceMethod())
1430 OS << " -";
1431 else
1432 OS << " +";
1433 dumpName(D);
Alp Toker314cc812014-01-25 16:55:45 +00001434 dumpType(D->getReturnType());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001435
Richard Trieude5cc7d2013-01-31 01:44:26 +00001436 if (D->isThisDeclarationADefinition()) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001437 dumpDeclContext(D);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001438 } else {
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001439 for (ObjCMethodDecl::param_const_iterator I = D->param_begin(),
1440 E = D->param_end();
Richard Smithf7514452014-10-30 21:02:37 +00001441 I != E; ++I)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001442 dumpDecl(*I);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001443 }
1444
Richard Smithf7514452014-10-30 21:02:37 +00001445 if (D->isVariadic())
1446 dumpChild([=] { OS << "..."; });
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001447
Richard Smithf7514452014-10-30 21:02:37 +00001448 if (D->hasBody())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001449 dumpStmt(D->getBody());
1450}
1451
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001452void ASTDumper::VisitObjCCategoryDecl(const ObjCCategoryDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001453 dumpName(D);
1454 dumpDeclRef(D->getClassInterface());
1455 dumpDeclRef(D->getImplementation());
1456 for (ObjCCategoryDecl::protocol_iterator I = D->protocol_begin(),
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001457 E = D->protocol_end();
Richard Smithf7514452014-10-30 21:02:37 +00001458 I != E; ++I)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001459 dumpDeclRef(*I);
1460}
1461
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001462void ASTDumper::VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001463 dumpName(D);
1464 dumpDeclRef(D->getClassInterface());
1465 dumpDeclRef(D->getCategoryDecl());
1466}
1467
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001468void ASTDumper::VisitObjCProtocolDecl(const ObjCProtocolDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001469 dumpName(D);
Richard Smithdcc2c452014-03-17 23:00:06 +00001470
Richard Smith7fcb35f2014-03-18 02:37:59 +00001471 for (auto *Child : D->protocols())
Richard Smithf7514452014-10-30 21:02:37 +00001472 dumpDeclRef(Child);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001473}
1474
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001475void ASTDumper::VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001476 dumpName(D);
1477 dumpDeclRef(D->getSuperClass(), "super");
Richard Smithdcc2c452014-03-17 23:00:06 +00001478
Richard Smithf7514452014-10-30 21:02:37 +00001479 dumpDeclRef(D->getImplementation());
Richard Smith7fcb35f2014-03-18 02:37:59 +00001480 for (auto *Child : D->protocols())
Richard Smithf7514452014-10-30 21:02:37 +00001481 dumpDeclRef(Child);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001482}
1483
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001484void ASTDumper::VisitObjCImplementationDecl(const ObjCImplementationDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001485 dumpName(D);
1486 dumpDeclRef(D->getSuperClass(), "super");
1487 dumpDeclRef(D->getClassInterface());
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001488 for (ObjCImplementationDecl::init_const_iterator I = D->init_begin(),
1489 E = D->init_end();
Richard Smithf7514452014-10-30 21:02:37 +00001490 I != E; ++I)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001491 dumpCXXCtorInitializer(*I);
1492}
1493
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001494void ASTDumper::VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001495 dumpName(D);
1496 dumpDeclRef(D->getClassInterface());
1497}
1498
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001499void ASTDumper::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001500 dumpName(D);
1501 dumpType(D->getType());
1502
1503 if (D->getPropertyImplementation() == ObjCPropertyDecl::Required)
1504 OS << " required";
1505 else if (D->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1506 OS << " optional";
1507
1508 ObjCPropertyDecl::PropertyAttributeKind Attrs = D->getPropertyAttributes();
1509 if (Attrs != ObjCPropertyDecl::OBJC_PR_noattr) {
1510 if (Attrs & ObjCPropertyDecl::OBJC_PR_readonly)
1511 OS << " readonly";
1512 if (Attrs & ObjCPropertyDecl::OBJC_PR_assign)
1513 OS << " assign";
1514 if (Attrs & ObjCPropertyDecl::OBJC_PR_readwrite)
1515 OS << " readwrite";
1516 if (Attrs & ObjCPropertyDecl::OBJC_PR_retain)
1517 OS << " retain";
1518 if (Attrs & ObjCPropertyDecl::OBJC_PR_copy)
1519 OS << " copy";
1520 if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic)
1521 OS << " nonatomic";
1522 if (Attrs & ObjCPropertyDecl::OBJC_PR_atomic)
1523 OS << " atomic";
1524 if (Attrs & ObjCPropertyDecl::OBJC_PR_weak)
1525 OS << " weak";
1526 if (Attrs & ObjCPropertyDecl::OBJC_PR_strong)
1527 OS << " strong";
1528 if (Attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained)
1529 OS << " unsafe_unretained";
Richard Smithf7514452014-10-30 21:02:37 +00001530 if (Attrs & ObjCPropertyDecl::OBJC_PR_getter)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001531 dumpDeclRef(D->getGetterMethodDecl(), "getter");
Richard Smithf7514452014-10-30 21:02:37 +00001532 if (Attrs & ObjCPropertyDecl::OBJC_PR_setter)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001533 dumpDeclRef(D->getSetterMethodDecl(), "setter");
1534 }
1535}
1536
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001537void ASTDumper::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001538 dumpName(D->getPropertyDecl());
1539 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
1540 OS << " synthesize";
1541 else
1542 OS << " dynamic";
1543 dumpDeclRef(D->getPropertyDecl());
1544 dumpDeclRef(D->getPropertyIvarDecl());
1545}
1546
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001547void ASTDumper::VisitBlockDecl(const BlockDecl *D) {
Aaron Ballmanb2b8b1d2014-03-07 16:09:59 +00001548 for (auto I : D->params())
1549 dumpDecl(I);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001550
Richard Smithf7514452014-10-30 21:02:37 +00001551 if (D->isVariadic())
1552 dumpChild([=]{ OS << "..."; });
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001553
Richard Smithf7514452014-10-30 21:02:37 +00001554 if (D->capturesCXXThis())
1555 dumpChild([=]{ OS << "capture this"; });
1556
Aaron Ballman9371dd22014-03-14 18:34:04 +00001557 for (const auto &I : D->captures()) {
Richard Smithf7514452014-10-30 21:02:37 +00001558 dumpChild([=] {
1559 OS << "capture";
1560 if (I.isByRef())
1561 OS << " byref";
1562 if (I.isNested())
1563 OS << " nested";
1564 if (I.getVariable()) {
1565 OS << ' ';
1566 dumpBareDeclRef(I.getVariable());
1567 }
1568 if (I.hasCopyExpr())
1569 dumpStmt(I.getCopyExpr());
1570 });
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001571 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001572 dumpStmt(D->getBody());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001573}
1574
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001575//===----------------------------------------------------------------------===//
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001576// Stmt dumping methods.
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001577//===----------------------------------------------------------------------===//
1578
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001579void ASTDumper::dumpStmt(const Stmt *S) {
Richard Smithf7514452014-10-30 21:02:37 +00001580 dumpChild([=] {
1581 if (!S) {
1582 ColorScope Color(*this, NullColor);
1583 OS << "<<<NULL>>>";
1584 return;
1585 }
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001586
Richard Smithf7514452014-10-30 21:02:37 +00001587 if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
1588 VisitDeclStmt(DS);
1589 return;
1590 }
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001591
Richard Smithf7514452014-10-30 21:02:37 +00001592 ConstStmtVisitor<ASTDumper>::Visit(S);
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001593
Richard Smithf7514452014-10-30 21:02:37 +00001594 for (Stmt::const_child_range CI = S->children(); CI; ++CI)
1595 dumpStmt(*CI);
1596 });
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001597}
1598
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001599void ASTDumper::VisitStmt(const Stmt *Node) {
Richard Trieud215b8d2013-01-26 01:31:20 +00001600 {
1601 ColorScope Color(*this, StmtColor);
1602 OS << Node->getStmtClassName();
1603 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001604 dumpPointer(Node);
1605 dumpSourceRange(Node->getSourceRange());
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001606}
1607
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001608void ASTDumper::VisitDeclStmt(const DeclStmt *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001609 VisitStmt(Node);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001610 for (DeclStmt::const_decl_iterator I = Node->decl_begin(),
1611 E = Node->decl_end();
Richard Smithf7514452014-10-30 21:02:37 +00001612 I != E; ++I)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001613 dumpDecl(*I);
Ted Kremenek433a4922007-12-12 06:59:42 +00001614}
1615
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001616void ASTDumper::VisitAttributedStmt(const AttributedStmt *Node) {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001617 VisitStmt(Node);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001618 for (ArrayRef<const Attr *>::iterator I = Node->getAttrs().begin(),
1619 E = Node->getAttrs().end();
Richard Smithf7514452014-10-30 21:02:37 +00001620 I != E; ++I)
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001621 dumpAttr(*I);
1622}
1623
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001624void ASTDumper::VisitLabelStmt(const LabelStmt *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001625 VisitStmt(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001626 OS << " '" << Node->getName() << "'";
Chris Lattnercbe4f772007-08-08 22:51:59 +00001627}
1628
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001629void ASTDumper::VisitGotoStmt(const GotoStmt *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001630 VisitStmt(Node);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001631 OS << " '" << Node->getLabel()->getName() << "'";
1632 dumpPointer(Node->getLabel());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001633}
1634
Pavel Labath1ef83422013-09-04 14:35:00 +00001635void ASTDumper::VisitCXXCatchStmt(const CXXCatchStmt *Node) {
1636 VisitStmt(Node);
1637 dumpDecl(Node->getExceptionDecl());
1638}
1639
Chris Lattnercbe4f772007-08-08 22:51:59 +00001640//===----------------------------------------------------------------------===//
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001641// Expr dumping methods.
Chris Lattnercbe4f772007-08-08 22:51:59 +00001642//===----------------------------------------------------------------------===//
1643
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001644void ASTDumper::VisitExpr(const Expr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001645 VisitStmt(Node);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001646 dumpType(Node->getType());
1647
Richard Trieud215b8d2013-01-26 01:31:20 +00001648 {
1649 ColorScope Color(*this, ValueKindColor);
1650 switch (Node->getValueKind()) {
1651 case VK_RValue:
1652 break;
1653 case VK_LValue:
1654 OS << " lvalue";
1655 break;
1656 case VK_XValue:
1657 OS << " xvalue";
1658 break;
1659 }
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001660 }
1661
Richard Trieud215b8d2013-01-26 01:31:20 +00001662 {
1663 ColorScope Color(*this, ObjectKindColor);
1664 switch (Node->getObjectKind()) {
1665 case OK_Ordinary:
1666 break;
1667 case OK_BitField:
1668 OS << " bitfield";
1669 break;
1670 case OK_ObjCProperty:
1671 OS << " objcproperty";
1672 break;
1673 case OK_ObjCSubscript:
1674 OS << " objcsubscript";
1675 break;
1676 case OK_VectorComponent:
1677 OS << " vectorcomponent";
1678 break;
1679 }
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001680 }
Chris Lattnercbe4f772007-08-08 22:51:59 +00001681}
1682
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001683static void dumpBasePath(raw_ostream &OS, const CastExpr *Node) {
John McCallcf142162010-08-07 06:22:56 +00001684 if (Node->path_empty())
Anders Carlssona70cff62010-04-24 19:06:50 +00001685 return;
1686
1687 OS << " (";
1688 bool First = true;
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001689 for (CastExpr::path_const_iterator I = Node->path_begin(),
1690 E = Node->path_end();
1691 I != E; ++I) {
Anders Carlssona70cff62010-04-24 19:06:50 +00001692 const CXXBaseSpecifier *Base = *I;
1693 if (!First)
1694 OS << " -> ";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001695
Anders Carlssona70cff62010-04-24 19:06:50 +00001696 const CXXRecordDecl *RD =
1697 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001698
Anders Carlssona70cff62010-04-24 19:06:50 +00001699 if (Base->isVirtual())
1700 OS << "virtual ";
1701 OS << RD->getName();
1702 First = false;
1703 }
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001704
Anders Carlssona70cff62010-04-24 19:06:50 +00001705 OS << ')';
1706}
1707
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001708void ASTDumper::VisitCastExpr(const CastExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001709 VisitExpr(Node);
Richard Trieud215b8d2013-01-26 01:31:20 +00001710 OS << " <";
1711 {
1712 ColorScope Color(*this, CastColor);
1713 OS << Node->getCastKindName();
1714 }
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001715 dumpBasePath(OS, Node);
Anders Carlssona70cff62010-04-24 19:06:50 +00001716 OS << ">";
Anders Carlssond7923c62009-08-22 23:33:40 +00001717}
1718
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001719void ASTDumper::VisitDeclRefExpr(const DeclRefExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001720 VisitExpr(Node);
Ted Kremenek5f64ca82007-09-10 17:32:55 +00001721
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001722 OS << " ";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001723 dumpBareDeclRef(Node->getDecl());
Chandler Carruth8d26bb02011-05-01 23:48:14 +00001724 if (Node->getDecl() != Node->getFoundDecl()) {
1725 OS << " (";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001726 dumpBareDeclRef(Node->getFoundDecl());
Chandler Carruth8d26bb02011-05-01 23:48:14 +00001727 OS << ")";
1728 }
John McCall351762c2011-02-07 10:33:21 +00001729}
1730
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001731void ASTDumper::VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001732 VisitExpr(Node);
John McCall76d09942009-12-11 21:50:11 +00001733 OS << " (";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001734 if (!Node->requiresADL())
1735 OS << "no ";
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001736 OS << "ADL) = '" << Node->getName() << '\'';
John McCall76d09942009-12-11 21:50:11 +00001737
1738 UnresolvedLookupExpr::decls_iterator
1739 I = Node->decls_begin(), E = Node->decls_end();
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001740 if (I == E)
1741 OS << " empty";
John McCall76d09942009-12-11 21:50:11 +00001742 for (; I != E; ++I)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001743 dumpPointer(*I);
John McCall76d09942009-12-11 21:50:11 +00001744}
1745
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001746void ASTDumper::VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001747 VisitExpr(Node);
Steve Naroff5d5efca2008-03-12 13:19:12 +00001748
Richard Trieud215b8d2013-01-26 01:31:20 +00001749 {
1750 ColorScope Color(*this, DeclKindNameColor);
1751 OS << " " << Node->getDecl()->getDeclKindName() << "Decl";
1752 }
1753 OS << "='" << *Node->getDecl() << "'";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001754 dumpPointer(Node->getDecl());
Steve Naroffb3424a92008-05-23 22:01:24 +00001755 if (Node->isFreeIvar())
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001756 OS << " isFreeIvar";
Steve Naroff5d5efca2008-03-12 13:19:12 +00001757}
1758
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001759void ASTDumper::VisitPredefinedExpr(const PredefinedExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001760 VisitExpr(Node);
Alexey Bataevec474782014-10-09 08:45:04 +00001761 OS << " " << PredefinedExpr::getIdentTypeName(Node->getIdentType());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001762}
1763
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001764void ASTDumper::VisitCharacterLiteral(const CharacterLiteral *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001765 VisitExpr(Node);
Richard Trieud215b8d2013-01-26 01:31:20 +00001766 ColorScope Color(*this, ValueColor);
Richard Trieu364ee422011-11-03 23:56:23 +00001767 OS << " " << Node->getValue();
Chris Lattnercbe4f772007-08-08 22:51:59 +00001768}
1769
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001770void ASTDumper::VisitIntegerLiteral(const IntegerLiteral *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001771 VisitExpr(Node);
Chris Lattnercbe4f772007-08-08 22:51:59 +00001772
1773 bool isSigned = Node->getType()->isSignedIntegerType();
Richard Trieud215b8d2013-01-26 01:31:20 +00001774 ColorScope Color(*this, ValueColor);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001775 OS << " " << Node->getValue().toString(10, isSigned);
Chris Lattnercbe4f772007-08-08 22:51:59 +00001776}
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001777
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001778void ASTDumper::VisitFloatingLiteral(const FloatingLiteral *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001779 VisitExpr(Node);
Richard Trieud215b8d2013-01-26 01:31:20 +00001780 ColorScope Color(*this, ValueColor);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001781 OS << " " << Node->getValueAsApproximateDouble();
Chris Lattnercbe4f772007-08-08 22:51:59 +00001782}
Chris Lattner1c20a172007-08-26 03:42:43 +00001783
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001784void ASTDumper::VisitStringLiteral(const StringLiteral *Str) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001785 VisitExpr(Str);
Richard Trieud215b8d2013-01-26 01:31:20 +00001786 ColorScope Color(*this, ValueColor);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001787 OS << " ";
Richard Trieudc355912012-06-13 20:25:24 +00001788 Str->outputString(OS);
Chris Lattnercbe4f772007-08-08 22:51:59 +00001789}
Chris Lattner84ca3762007-08-30 01:00:35 +00001790
Richard Smithf0514962014-06-03 08:24:28 +00001791void ASTDumper::VisitInitListExpr(const InitListExpr *ILE) {
1792 VisitExpr(ILE);
1793 if (auto *Filler = ILE->getArrayFiller()) {
Richard Smithf7514452014-10-30 21:02:37 +00001794 dumpChild([=] {
1795 OS << "array filler";
1796 dumpStmt(Filler);
1797 });
Richard Smithf0514962014-06-03 08:24:28 +00001798 }
1799 if (auto *Field = ILE->getInitializedFieldInUnion()) {
1800 OS << " field ";
1801 dumpBareDeclRef(Field);
1802 }
1803}
1804
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001805void ASTDumper::VisitUnaryOperator(const UnaryOperator *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001806 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001807 OS << " " << (Node->isPostfix() ? "postfix" : "prefix")
1808 << " '" << UnaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
Chris Lattnercbe4f772007-08-08 22:51:59 +00001809}
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001810
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001811void ASTDumper::VisitUnaryExprOrTypeTraitExpr(
1812 const UnaryExprOrTypeTraitExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001813 VisitExpr(Node);
Peter Collingbournee190dee2011-03-11 19:24:49 +00001814 switch(Node->getKind()) {
1815 case UETT_SizeOf:
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001816 OS << " sizeof";
Peter Collingbournee190dee2011-03-11 19:24:49 +00001817 break;
1818 case UETT_AlignOf:
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001819 OS << " alignof";
Peter Collingbournee190dee2011-03-11 19:24:49 +00001820 break;
1821 case UETT_VecStep:
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001822 OS << " vec_step";
Peter Collingbournee190dee2011-03-11 19:24:49 +00001823 break;
1824 }
Sebastian Redl6f282892008-11-11 17:56:53 +00001825 if (Node->isArgumentType())
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001826 dumpType(Node->getArgumentType());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001827}
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +00001828
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001829void ASTDumper::VisitMemberExpr(const MemberExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001830 VisitExpr(Node);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001831 OS << " " << (Node->isArrow() ? "->" : ".") << *Node->getMemberDecl();
1832 dumpPointer(Node->getMemberDecl());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001833}
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001834
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001835void ASTDumper::VisitExtVectorElementExpr(const ExtVectorElementExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001836 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001837 OS << " " << Node->getAccessor().getNameStart();
Chris Lattnercbe4f772007-08-08 22:51:59 +00001838}
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001839
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001840void ASTDumper::VisitBinaryOperator(const BinaryOperator *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001841 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001842 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
Chris Lattner86928112007-08-25 02:00:02 +00001843}
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001844
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001845void ASTDumper::VisitCompoundAssignOperator(
1846 const CompoundAssignOperator *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001847 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001848 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode())
1849 << "' ComputeLHSTy=";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001850 dumpBareType(Node->getComputationLHSType());
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001851 OS << " ComputeResultTy=";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001852 dumpBareType(Node->getComputationResultType());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001853}
Chris Lattnercbe4f772007-08-08 22:51:59 +00001854
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001855void ASTDumper::VisitBlockExpr(const BlockExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001856 VisitExpr(Node);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001857 dumpDecl(Node->getBlockDecl());
John McCall351762c2011-02-07 10:33:21 +00001858}
1859
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001860void ASTDumper::VisitOpaqueValueExpr(const OpaqueValueExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001861 VisitExpr(Node);
John McCallfe96e0b2011-11-06 09:01:30 +00001862
Richard Smithf7514452014-10-30 21:02:37 +00001863 if (Expr *Source = Node->getSourceExpr())
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001864 dumpStmt(Source);
John McCallfe96e0b2011-11-06 09:01:30 +00001865}
1866
Chris Lattnercbe4f772007-08-08 22:51:59 +00001867// GNU extensions.
1868
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001869void ASTDumper::VisitAddrLabelExpr(const AddrLabelExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001870 VisitExpr(Node);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001871 OS << " " << Node->getLabel()->getName();
1872 dumpPointer(Node->getLabel());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001873}
1874
Chris Lattner8f184b12007-08-09 18:03:18 +00001875//===----------------------------------------------------------------------===//
1876// C++ Expressions
1877//===----------------------------------------------------------------------===//
Chris Lattnercbe4f772007-08-08 22:51:59 +00001878
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001879void ASTDumper::VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001880 VisitExpr(Node);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001881 OS << " " << Node->getCastName()
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001882 << "<" << Node->getTypeAsWritten().getAsString() << ">"
Anders Carlssona70cff62010-04-24 19:06:50 +00001883 << " <" << Node->getCastKindName();
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001884 dumpBasePath(OS, Node);
Anders Carlssona70cff62010-04-24 19:06:50 +00001885 OS << ">";
Chris Lattnercbe4f772007-08-08 22:51:59 +00001886}
1887
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001888void ASTDumper::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001889 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001890 OS << " " << (Node->getValue() ? "true" : "false");
Chris Lattnercbe4f772007-08-08 22:51:59 +00001891}
1892
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001893void ASTDumper::VisitCXXThisExpr(const CXXThisExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001894 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001895 OS << " this";
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001896}
1897
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001898void ASTDumper::VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001899 VisitExpr(Node);
Eli Friedman29538892011-09-02 17:38:59 +00001900 OS << " functional cast to " << Node->getTypeAsWritten().getAsString()
1901 << " <" << Node->getCastKindName() << ">";
Douglas Gregore200adc2008-10-27 19:41:14 +00001902}
1903
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001904void ASTDumper::VisitCXXConstructExpr(const CXXConstructExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001905 VisitExpr(Node);
John McCalleba90cd2010-02-02 19:03:45 +00001906 CXXConstructorDecl *Ctor = Node->getConstructor();
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001907 dumpType(Ctor->getType());
Anders Carlsson073846832009-08-12 00:21:52 +00001908 if (Node->isElidable())
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001909 OS << " elidable";
John McCall85370042010-08-07 06:38:55 +00001910 if (Node->requiresZeroInitialization())
1911 OS << " zeroing";
Anders Carlsson073846832009-08-12 00:21:52 +00001912}
1913
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001914void ASTDumper::VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001915 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001916 OS << " ";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001917 dumpCXXTemporary(Node->getTemporary());
Anders Carlsson073846832009-08-12 00:21:52 +00001918}
1919
Richard Smithe6c01442013-06-05 00:46:14 +00001920void
1921ASTDumper::VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node) {
1922 VisitExpr(Node);
1923 if (const ValueDecl *VD = Node->getExtendingDecl()) {
1924 OS << " extended by ";
1925 dumpBareDeclRef(VD);
1926 }
1927}
1928
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001929void ASTDumper::VisitExprWithCleanups(const ExprWithCleanups *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001930 VisitExpr(Node);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001931 for (unsigned i = 0, e = Node->getNumObjects(); i != e; ++i)
1932 dumpDeclRef(Node->getObject(i), "cleanup");
Anders Carlsson073846832009-08-12 00:21:52 +00001933}
1934
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001935void ASTDumper::dumpCXXTemporary(const CXXTemporary *Temporary) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001936 OS << "(CXXTemporary";
1937 dumpPointer(Temporary);
1938 OS << ")";
Anders Carlsson073846832009-08-12 00:21:52 +00001939}
1940
Serge Pavlov6b926032015-02-16 19:58:41 +00001941void ASTDumper::VisitSizeOfPackExpr(const SizeOfPackExpr *Node) {
1942 VisitExpr(Node);
1943 dumpPointer(Node->getPack());
1944 dumpName(Node->getPack());
1945}
1946
1947
Anders Carlsson76f4a902007-08-21 17:43:55 +00001948//===----------------------------------------------------------------------===//
1949// Obj-C Expressions
1950//===----------------------------------------------------------------------===//
1951
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001952void ASTDumper::VisitObjCMessageExpr(const ObjCMessageExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001953 VisitExpr(Node);
Aaron Ballmanb190f972014-01-03 17:59:55 +00001954 OS << " selector=";
1955 Node->getSelector().print(OS);
Douglas Gregor9a129192010-04-21 00:45:42 +00001956 switch (Node->getReceiverKind()) {
1957 case ObjCMessageExpr::Instance:
1958 break;
1959
1960 case ObjCMessageExpr::Class:
1961 OS << " class=";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001962 dumpBareType(Node->getClassReceiver());
Douglas Gregor9a129192010-04-21 00:45:42 +00001963 break;
1964
1965 case ObjCMessageExpr::SuperInstance:
1966 OS << " super (instance)";
1967 break;
1968
1969 case ObjCMessageExpr::SuperClass:
1970 OS << " super (class)";
1971 break;
1972 }
Ted Kremenek36748da2008-02-29 22:04:05 +00001973}
1974
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001975void ASTDumper::VisitObjCBoxedExpr(const ObjCBoxedExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001976 VisitExpr(Node);
Aaron Ballmanb190f972014-01-03 17:59:55 +00001977 OS << " selector=";
1978 Node->getBoxingMethod()->getSelector().print(OS);
Argyrios Kyrtzidis9dd40892012-05-10 20:02:31 +00001979}
1980
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001981void ASTDumper::VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001982 VisitStmt(Node);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001983 if (const VarDecl *CatchParam = Node->getCatchParamDecl())
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001984 dumpDecl(CatchParam);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001985 else
Douglas Gregor96c79492010-04-23 22:50:49 +00001986 OS << " catch all";
Douglas Gregor96c79492010-04-23 22:50:49 +00001987}
1988
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001989void ASTDumper::VisitObjCEncodeExpr(const ObjCEncodeExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001990 VisitExpr(Node);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001991 dumpType(Node->getEncodedType());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001992}
1993
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001994void ASTDumper::VisitObjCSelectorExpr(const ObjCSelectorExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001995 VisitExpr(Node);
Mike Stump11289f42009-09-09 15:08:12 +00001996
Aaron Ballmanb190f972014-01-03 17:59:55 +00001997 OS << " ";
1998 Node->getSelector().print(OS);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001999}
2000
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002001void ASTDumper::VisitObjCProtocolExpr(const ObjCProtocolExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002002 VisitExpr(Node);
Mike Stump11289f42009-09-09 15:08:12 +00002003
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00002004 OS << ' ' << *Node->getProtocol();
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002005}
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00002006
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002007void ASTDumper::VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002008 VisitExpr(Node);
John McCallb7bd14f2010-12-02 01:19:52 +00002009 if (Node->isImplicitProperty()) {
Fariborz Jahanian0f0b3022010-12-22 19:46:35 +00002010 OS << " Kind=MethodRef Getter=\"";
2011 if (Node->getImplicitPropertyGetter())
Aaron Ballmanb190f972014-01-03 17:59:55 +00002012 Node->getImplicitPropertyGetter()->getSelector().print(OS);
Fariborz Jahanian0f0b3022010-12-22 19:46:35 +00002013 else
2014 OS << "(null)";
2015
2016 OS << "\" Setter=\"";
John McCallb7bd14f2010-12-02 01:19:52 +00002017 if (ObjCMethodDecl *Setter = Node->getImplicitPropertySetter())
Aaron Ballmanb190f972014-01-03 17:59:55 +00002018 Setter->getSelector().print(OS);
John McCallb7bd14f2010-12-02 01:19:52 +00002019 else
2020 OS << "(null)";
2021 OS << "\"";
2022 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00002023 OS << " Kind=PropertyRef Property=\"" << *Node->getExplicitProperty() <<'"';
John McCallb7bd14f2010-12-02 01:19:52 +00002024 }
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00002025
Fariborz Jahanian681c0752010-10-14 16:04:05 +00002026 if (Node->isSuperReceiver())
2027 OS << " super";
Argyrios Kyrtzidisab468b02012-03-30 00:19:18 +00002028
2029 OS << " Messaging=";
2030 if (Node->isMessagingGetter() && Node->isMessagingSetter())
2031 OS << "Getter&Setter";
2032 else if (Node->isMessagingGetter())
2033 OS << "Getter";
2034 else if (Node->isMessagingSetter())
2035 OS << "Setter";
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002036}
2037
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002038void ASTDumper::VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002039 VisitExpr(Node);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002040 if (Node->isArraySubscriptRefExpr())
2041 OS << " Kind=ArraySubscript GetterForArray=\"";
2042 else
2043 OS << " Kind=DictionarySubscript GetterForDictionary=\"";
2044 if (Node->getAtIndexMethodDecl())
Aaron Ballmanb190f972014-01-03 17:59:55 +00002045 Node->getAtIndexMethodDecl()->getSelector().print(OS);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002046 else
2047 OS << "(null)";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00002048
Ted Kremeneke65b0862012-03-06 20:05:56 +00002049 if (Node->isArraySubscriptRefExpr())
2050 OS << "\" SetterForArray=\"";
2051 else
2052 OS << "\" SetterForDictionary=\"";
2053 if (Node->setAtIndexMethodDecl())
Aaron Ballmanb190f972014-01-03 17:59:55 +00002054 Node->setAtIndexMethodDecl()->getSelector().print(OS);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002055 else
2056 OS << "(null)";
2057}
2058
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002059void ASTDumper::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002060 VisitExpr(Node);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002061 OS << " " << (Node->getValue() ? "__objc_yes" : "__objc_no");
2062}
2063
Chris Lattnercbe4f772007-08-08 22:51:59 +00002064//===----------------------------------------------------------------------===//
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002065// Comments
2066//===----------------------------------------------------------------------===//
2067
2068const char *ASTDumper::getCommandName(unsigned CommandID) {
2069 if (Traits)
2070 return Traits->getCommandInfo(CommandID)->Name;
2071 const CommandInfo *Info = CommandTraits::getBuiltinCommandInfo(CommandID);
2072 if (Info)
2073 return Info->Name;
2074 return "<not a builtin command>";
2075}
2076
2077void ASTDumper::dumpFullComment(const FullComment *C) {
2078 if (!C)
2079 return;
2080
2081 FC = C;
2082 dumpComment(C);
Craig Topper36250ad2014-05-12 05:36:57 +00002083 FC = nullptr;
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002084}
2085
2086void ASTDumper::dumpComment(const Comment *C) {
Richard Smithf7514452014-10-30 21:02:37 +00002087 dumpChild([=] {
2088 if (!C) {
2089 ColorScope Color(*this, NullColor);
2090 OS << "<<<NULL>>>";
2091 return;
2092 }
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002093
Richard Smithf7514452014-10-30 21:02:37 +00002094 {
2095 ColorScope Color(*this, CommentColor);
2096 OS << C->getCommentKindName();
2097 }
2098 dumpPointer(C);
2099 dumpSourceRange(C->getSourceRange());
2100 ConstCommentVisitor<ASTDumper>::visit(C);
2101 for (Comment::child_iterator I = C->child_begin(), E = C->child_end();
2102 I != E; ++I)
2103 dumpComment(*I);
2104 });
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002105}
2106
2107void ASTDumper::visitTextComment(const TextComment *C) {
2108 OS << " Text=\"" << C->getText() << "\"";
2109}
2110
2111void ASTDumper::visitInlineCommandComment(const InlineCommandComment *C) {
2112 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
2113 switch (C->getRenderKind()) {
2114 case InlineCommandComment::RenderNormal:
2115 OS << " RenderNormal";
2116 break;
2117 case InlineCommandComment::RenderBold:
2118 OS << " RenderBold";
2119 break;
2120 case InlineCommandComment::RenderMonospaced:
2121 OS << " RenderMonospaced";
2122 break;
2123 case InlineCommandComment::RenderEmphasized:
2124 OS << " RenderEmphasized";
2125 break;
2126 }
2127
2128 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
2129 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
2130}
2131
2132void ASTDumper::visitHTMLStartTagComment(const HTMLStartTagComment *C) {
2133 OS << " Name=\"" << C->getTagName() << "\"";
2134 if (C->getNumAttrs() != 0) {
2135 OS << " Attrs: ";
2136 for (unsigned i = 0, e = C->getNumAttrs(); i != e; ++i) {
2137 const HTMLStartTagComment::Attribute &Attr = C->getAttr(i);
2138 OS << " \"" << Attr.Name << "=\"" << Attr.Value << "\"";
2139 }
2140 }
2141 if (C->isSelfClosing())
2142 OS << " SelfClosing";
2143}
2144
2145void ASTDumper::visitHTMLEndTagComment(const HTMLEndTagComment *C) {
2146 OS << " Name=\"" << C->getTagName() << "\"";
2147}
2148
2149void ASTDumper::visitBlockCommandComment(const BlockCommandComment *C) {
2150 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
2151 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
2152 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
2153}
2154
2155void ASTDumper::visitParamCommandComment(const ParamCommandComment *C) {
2156 OS << " " << ParamCommandComment::getDirectionAsString(C->getDirection());
2157
2158 if (C->isDirectionExplicit())
2159 OS << " explicitly";
2160 else
2161 OS << " implicitly";
2162
2163 if (C->hasParamName()) {
2164 if (C->isParamIndexValid())
2165 OS << " Param=\"" << C->getParamName(FC) << "\"";
2166 else
2167 OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
2168 }
2169
Dmitri Gribenkodbff5c72014-03-19 14:03:47 +00002170 if (C->isParamIndexValid() && !C->isVarArgParam())
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002171 OS << " ParamIndex=" << C->getParamIndex();
2172}
2173
2174void ASTDumper::visitTParamCommandComment(const TParamCommandComment *C) {
2175 if (C->hasParamName()) {
2176 if (C->isPositionValid())
2177 OS << " Param=\"" << C->getParamName(FC) << "\"";
2178 else
2179 OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
2180 }
2181
2182 if (C->isPositionValid()) {
2183 OS << " Position=<";
2184 for (unsigned i = 0, e = C->getDepth(); i != e; ++i) {
2185 OS << C->getIndex(i);
2186 if (i != e - 1)
2187 OS << ", ";
2188 }
2189 OS << ">";
2190 }
2191}
2192
2193void ASTDumper::visitVerbatimBlockComment(const VerbatimBlockComment *C) {
2194 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\""
2195 " CloseName=\"" << C->getCloseName() << "\"";
2196}
2197
2198void ASTDumper::visitVerbatimBlockLineComment(
2199 const VerbatimBlockLineComment *C) {
2200 OS << " Text=\"" << C->getText() << "\"";
2201}
2202
2203void ASTDumper::visitVerbatimLineComment(const VerbatimLineComment *C) {
2204 OS << " Text=\"" << C->getText() << "\"";
2205}
2206
2207//===----------------------------------------------------------------------===//
Richard Smithd5e7ff82014-10-31 01:17:45 +00002208// Type method implementations
2209//===----------------------------------------------------------------------===//
2210
2211void QualType::dump(const char *msg) const {
2212 if (msg)
2213 llvm::errs() << msg << ": ";
2214 dump();
2215}
2216
2217LLVM_DUMP_METHOD void QualType::dump() const {
2218 ASTDumper Dumper(llvm::errs(), nullptr, nullptr);
2219 Dumper.dumpTypeAsChild(*this);
2220}
2221
2222LLVM_DUMP_METHOD void Type::dump() const { QualType(this, 0).dump(); }
2223
2224//===----------------------------------------------------------------------===//
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002225// Decl method implementations
2226//===----------------------------------------------------------------------===//
2227
Alp Tokeref6b0072014-01-04 13:47:14 +00002228LLVM_DUMP_METHOD void Decl::dump() const { dump(llvm::errs()); }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002229
Alp Tokeref6b0072014-01-04 13:47:14 +00002230LLVM_DUMP_METHOD void Decl::dump(raw_ostream &OS) const {
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002231 ASTDumper P(OS, &getASTContext().getCommentCommandTraits(),
2232 &getASTContext().getSourceManager());
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002233 P.dumpDecl(this);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002234}
2235
Alp Tokeref6b0072014-01-04 13:47:14 +00002236LLVM_DUMP_METHOD void Decl::dumpColor() const {
Richard Trieud215b8d2013-01-26 01:31:20 +00002237 ASTDumper P(llvm::errs(), &getASTContext().getCommentCommandTraits(),
2238 &getASTContext().getSourceManager(), /*ShowColors*/true);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002239 P.dumpDecl(this);
Richard Trieud215b8d2013-01-26 01:31:20 +00002240}
Richard Smith33937e72013-06-22 21:49:40 +00002241
Alp Tokeref6b0072014-01-04 13:47:14 +00002242LLVM_DUMP_METHOD void DeclContext::dumpLookups() const {
Richard Smith6ea05822013-06-24 01:45:33 +00002243 dumpLookups(llvm::errs());
2244}
2245
Richard Smith35f986d2014-08-11 22:11:07 +00002246LLVM_DUMP_METHOD void DeclContext::dumpLookups(raw_ostream &OS,
2247 bool DumpDecls) const {
Richard Smith33937e72013-06-22 21:49:40 +00002248 const DeclContext *DC = this;
2249 while (!DC->isTranslationUnit())
2250 DC = DC->getParent();
2251 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
Richard Smith6ea05822013-06-24 01:45:33 +00002252 ASTDumper P(OS, &Ctx.getCommentCommandTraits(), &Ctx.getSourceManager());
Richard Smith35f986d2014-08-11 22:11:07 +00002253 P.dumpLookups(this, DumpDecls);
Richard Smith33937e72013-06-22 21:49:40 +00002254}
2255
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002256//===----------------------------------------------------------------------===//
Chris Lattnercbe4f772007-08-08 22:51:59 +00002257// Stmt method implementations
2258//===----------------------------------------------------------------------===//
2259
Alp Tokeref6b0072014-01-04 13:47:14 +00002260LLVM_DUMP_METHOD void Stmt::dump(SourceManager &SM) const {
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +00002261 dump(llvm::errs(), SM);
2262}
2263
Alp Tokeref6b0072014-01-04 13:47:14 +00002264LLVM_DUMP_METHOD void Stmt::dump(raw_ostream &OS, SourceManager &SM) const {
Craig Topper36250ad2014-05-12 05:36:57 +00002265 ASTDumper P(OS, nullptr, &SM);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002266 P.dumpStmt(this);
Chris Lattner779d5d92007-08-30 00:40:08 +00002267}
2268
Alp Tokeref6b0072014-01-04 13:47:14 +00002269LLVM_DUMP_METHOD void Stmt::dump() const {
Craig Topper36250ad2014-05-12 05:36:57 +00002270 ASTDumper P(llvm::errs(), nullptr, nullptr);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002271 P.dumpStmt(this);
Chris Lattnercbe4f772007-08-08 22:51:59 +00002272}
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002273
Alp Tokeref6b0072014-01-04 13:47:14 +00002274LLVM_DUMP_METHOD void Stmt::dumpColor() const {
Craig Topper36250ad2014-05-12 05:36:57 +00002275 ASTDumper P(llvm::errs(), nullptr, nullptr, /*ShowColors*/true);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002276 P.dumpStmt(this);
Richard Trieud215b8d2013-01-26 01:31:20 +00002277}
2278
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002279//===----------------------------------------------------------------------===//
2280// Comment method implementations
2281//===----------------------------------------------------------------------===//
2282
Craig Topper36250ad2014-05-12 05:36:57 +00002283LLVM_DUMP_METHOD void Comment::dump() const {
2284 dump(llvm::errs(), nullptr, nullptr);
2285}
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002286
Alp Tokeref6b0072014-01-04 13:47:14 +00002287LLVM_DUMP_METHOD void Comment::dump(const ASTContext &Context) const {
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002288 dump(llvm::errs(), &Context.getCommentCommandTraits(),
2289 &Context.getSourceManager());
2290}
2291
Alexander Kornienko00911f12013-01-15 12:20:21 +00002292void Comment::dump(raw_ostream &OS, const CommandTraits *Traits,
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002293 const SourceManager *SM) const {
2294 const FullComment *FC = dyn_cast<FullComment>(this);
2295 ASTDumper D(OS, Traits, SM);
2296 D.dumpFullComment(FC);
2297}
Richard Trieud215b8d2013-01-26 01:31:20 +00002298
Alp Tokeref6b0072014-01-04 13:47:14 +00002299LLVM_DUMP_METHOD void Comment::dumpColor() const {
Richard Trieud215b8d2013-01-26 01:31:20 +00002300 const FullComment *FC = dyn_cast<FullComment>(this);
Craig Topper36250ad2014-05-12 05:36:57 +00002301 ASTDumper D(llvm::errs(), nullptr, nullptr, /*ShowColors*/true);
Richard Trieud215b8d2013-01-26 01:31:20 +00002302 D.dumpFullComment(FC);
2303}