blob: 4da6240f8670e07b11cefd6a02b8fd61dbb84097 [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);
Reid Kleckner5c682bc2015-03-19 18:09:25 +0000511 void VisitCXXNewExpr(const CXXNewExpr *Node);
512 void VisitCXXDeleteExpr(const CXXDeleteExpr *Node);
Richard Smithe6c01442013-06-05 00:46:14 +0000513 void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node);
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000514 void VisitExprWithCleanups(const ExprWithCleanups *Node);
515 void VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node);
516 void dumpCXXTemporary(const CXXTemporary *Temporary);
Faisal Vali2b391ab2013-09-26 19:54:12 +0000517 void VisitLambdaExpr(const LambdaExpr *Node) {
518 VisitExpr(Node);
519 dumpDecl(Node->getLambdaClass());
520 }
Serge Pavlov6b926032015-02-16 19:58:41 +0000521 void VisitSizeOfPackExpr(const SizeOfPackExpr *Node);
Mike Stump11289f42009-09-09 15:08:12 +0000522
Chris Lattner84ca3762007-08-30 01:00:35 +0000523 // ObjC
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000524 void VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node);
525 void VisitObjCEncodeExpr(const ObjCEncodeExpr *Node);
526 void VisitObjCMessageExpr(const ObjCMessageExpr *Node);
527 void VisitObjCBoxedExpr(const ObjCBoxedExpr *Node);
528 void VisitObjCSelectorExpr(const ObjCSelectorExpr *Node);
529 void VisitObjCProtocolExpr(const ObjCProtocolExpr *Node);
530 void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node);
531 void VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node);
532 void VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node);
533 void VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node);
Alexander Kornienkoebc17b52013-01-14 14:07:11 +0000534
535 // Comments.
536 const char *getCommandName(unsigned CommandID);
537 void dumpComment(const Comment *C);
538
539 // Inline comments.
540 void visitTextComment(const TextComment *C);
541 void visitInlineCommandComment(const InlineCommandComment *C);
542 void visitHTMLStartTagComment(const HTMLStartTagComment *C);
543 void visitHTMLEndTagComment(const HTMLEndTagComment *C);
544
545 // Block comments.
546 void visitBlockCommandComment(const BlockCommandComment *C);
547 void visitParamCommandComment(const ParamCommandComment *C);
548 void visitTParamCommandComment(const TParamCommandComment *C);
549 void visitVerbatimBlockComment(const VerbatimBlockComment *C);
550 void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C);
551 void visitVerbatimLineComment(const VerbatimLineComment *C);
Chris Lattnercbe4f772007-08-08 22:51:59 +0000552 };
Alexander Kornienko3d9d9292015-06-22 09:47:44 +0000553} // namespace
Chris Lattnercbe4f772007-08-08 22:51:59 +0000554
555//===----------------------------------------------------------------------===//
Chris Lattner11e30d32007-08-30 06:17:34 +0000556// Utilities
557//===----------------------------------------------------------------------===//
558
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000559void ASTDumper::dumpPointer(const void *Ptr) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000560 ColorScope Color(*this, AddressColor);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000561 OS << ' ' << Ptr;
562}
563
Alexander Kornienko18ec81b2012-12-13 13:59:55 +0000564void ASTDumper::dumpLocation(SourceLocation Loc) {
Alex McCarthye14ddef2014-05-02 20:24:11 +0000565 if (!SM)
566 return;
567
Richard Trieud215b8d2013-01-26 01:31:20 +0000568 ColorScope Color(*this, LocationColor);
Chris Lattner53e384f2009-01-16 07:00:02 +0000569 SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000570
Chris Lattner11e30d32007-08-30 06:17:34 +0000571 // The general format we print out is filename:line:col, but we drop pieces
572 // that haven't changed since the last loc printed.
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000573 PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
574
Douglas Gregor453b0122010-11-12 07:15:47 +0000575 if (PLoc.isInvalid()) {
576 OS << "<invalid sloc>";
577 return;
578 }
579
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000580 if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000581 OS << PLoc.getFilename() << ':' << PLoc.getLine()
582 << ':' << PLoc.getColumn();
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000583 LastLocFilename = PLoc.getFilename();
584 LastLocLine = PLoc.getLine();
585 } else if (PLoc.getLine() != LastLocLine) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000586 OS << "line" << ':' << PLoc.getLine()
587 << ':' << PLoc.getColumn();
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000588 LastLocLine = PLoc.getLine();
Chris Lattner11e30d32007-08-30 06:17:34 +0000589 } else {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000590 OS << "col" << ':' << PLoc.getColumn();
Chris Lattner11e30d32007-08-30 06:17:34 +0000591 }
592}
593
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000594void ASTDumper::dumpSourceRange(SourceRange R) {
Chris Lattner11e30d32007-08-30 06:17:34 +0000595 // Can't translate locations if a SourceManager isn't available.
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000596 if (!SM)
597 return;
Mike Stump11289f42009-09-09 15:08:12 +0000598
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000599 OS << " <";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000600 dumpLocation(R.getBegin());
Chris Lattnera7c19fe2007-10-16 22:36:42 +0000601 if (R.getBegin() != R.getEnd()) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000602 OS << ", ";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000603 dumpLocation(R.getEnd());
Chris Lattner11e30d32007-08-30 06:17:34 +0000604 }
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000605 OS << ">";
Mike Stump11289f42009-09-09 15:08:12 +0000606
Chris Lattner11e30d32007-08-30 06:17:34 +0000607 // <t2.c:123:421[blah], t2.c:412:321>
608
609}
610
Richard Smithd5e7ff82014-10-31 01:17:45 +0000611void ASTDumper::dumpBareType(QualType T, bool Desugar) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000612 ColorScope Color(*this, TypeColor);
Richard Smithd5e7ff82014-10-31 01:17:45 +0000613
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000614 SplitQualType T_split = T.split();
615 OS << "'" << QualType::getAsString(T_split) << "'";
616
Richard Smithd5e7ff82014-10-31 01:17:45 +0000617 if (Desugar && !T.isNull()) {
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000618 // If the type is sugared, also dump a (shallow) desugared type.
619 SplitQualType D_split = T.getSplitDesugaredType();
620 if (T_split != D_split)
621 OS << ":'" << QualType::getAsString(D_split) << "'";
622 }
623}
624
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000625void ASTDumper::dumpType(QualType T) {
626 OS << ' ';
627 dumpBareType(T);
628}
629
Richard Smithd5e7ff82014-10-31 01:17:45 +0000630void ASTDumper::dumpTypeAsChild(QualType T) {
631 SplitQualType SQT = T.split();
632 if (!SQT.Quals.hasQualifiers())
633 return dumpTypeAsChild(SQT.Ty);
634
635 dumpChild([=] {
636 OS << "QualType";
637 dumpPointer(T.getAsOpaquePtr());
638 OS << " ";
639 dumpBareType(T, false);
640 OS << " " << T.split().Quals.getAsString();
641 dumpTypeAsChild(T.split().Ty);
642 });
643}
644
645void ASTDumper::dumpTypeAsChild(const Type *T) {
646 dumpChild([=] {
647 if (!T) {
648 ColorScope Color(*this, NullColor);
649 OS << "<<<NULL>>>";
650 return;
651 }
652
653 {
654 ColorScope Color(*this, TypeColor);
655 OS << T->getTypeClassName() << "Type";
656 }
657 dumpPointer(T);
658 OS << " ";
659 dumpBareType(QualType(T, 0), false);
660
661 QualType SingleStepDesugar =
662 T->getLocallyUnqualifiedSingleStepDesugaredType();
663 if (SingleStepDesugar != QualType(T, 0))
664 OS << " sugar";
665 if (T->isDependentType())
666 OS << " dependent";
667 else if (T->isInstantiationDependentType())
668 OS << " instantiation_dependent";
669 if (T->isVariablyModifiedType())
670 OS << " variably_modified";
671 if (T->containsUnexpandedParameterPack())
672 OS << " contains_unexpanded_pack";
673 if (T->isFromAST())
674 OS << " imported";
675
676 TypeVisitor<ASTDumper>::Visit(T);
677
678 if (SingleStepDesugar != QualType(T, 0))
679 dumpTypeAsChild(SingleStepDesugar);
680 });
681}
682
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000683void ASTDumper::dumpBareDeclRef(const Decl *D) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000684 {
685 ColorScope Color(*this, DeclKindNameColor);
686 OS << D->getDeclKindName();
687 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000688 dumpPointer(D);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000689
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000690 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000691 ColorScope Color(*this, DeclNameColor);
David Blaikied4da8722013-05-14 21:04:00 +0000692 OS << " '" << ND->getDeclName() << '\'';
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000693 }
694
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000695 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000696 dumpType(VD->getType());
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000697}
698
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000699void ASTDumper::dumpDeclRef(const Decl *D, const char *Label) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000700 if (!D)
701 return;
702
Richard Smithf7514452014-10-30 21:02:37 +0000703 dumpChild([=]{
704 if (Label)
705 OS << Label << ' ';
706 dumpBareDeclRef(D);
707 });
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000708}
709
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000710void ASTDumper::dumpName(const NamedDecl *ND) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000711 if (ND->getDeclName()) {
712 ColorScope Color(*this, DeclNameColor);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000713 OS << ' ' << ND->getNameAsString();
Richard Trieud215b8d2013-01-26 01:31:20 +0000714 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000715}
716
Richard Trieude5cc7d2013-01-31 01:44:26 +0000717bool ASTDumper::hasNodes(const DeclContext *DC) {
718 if (!DC)
719 return false;
720
Richard Smith1d209d02013-05-23 01:49:11 +0000721 return DC->hasExternalLexicalStorage() ||
722 DC->noload_decls_begin() != DC->noload_decls_end();
Richard Trieude5cc7d2013-01-31 01:44:26 +0000723}
724
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000725void ASTDumper::dumpDeclContext(const DeclContext *DC) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000726 if (!DC)
727 return;
Richard Smithdcc2c452014-03-17 23:00:06 +0000728
Richard Smithdcc2c452014-03-17 23:00:06 +0000729 for (auto *D : DC->noload_decls())
Richard Smithf7514452014-10-30 21:02:37 +0000730 dumpDecl(D);
Richard Smithdcc2c452014-03-17 23:00:06 +0000731
732 if (DC->hasExternalLexicalStorage()) {
Richard Smithf7514452014-10-30 21:02:37 +0000733 dumpChild([=]{
734 ColorScope Color(*this, UndeserializedColor);
735 OS << "<undeserialized declarations>";
736 });
Richard Smith1d209d02013-05-23 01:49:11 +0000737 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000738}
739
Richard Smith35f986d2014-08-11 22:11:07 +0000740void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) {
Richard Smithf7514452014-10-30 21:02:37 +0000741 dumpChild([=] {
742 OS << "StoredDeclsMap ";
743 dumpBareDeclRef(cast<Decl>(DC));
Richard Smith33937e72013-06-22 21:49:40 +0000744
Richard Smithf7514452014-10-30 21:02:37 +0000745 const DeclContext *Primary = DC->getPrimaryContext();
746 if (Primary != DC) {
747 OS << " primary";
748 dumpPointer(cast<Decl>(Primary));
Richard Smith33937e72013-06-22 21:49:40 +0000749 }
750
Richard Smithf7514452014-10-30 21:02:37 +0000751 bool HasUndeserializedLookups = Primary->hasExternalVisibleStorage();
Richard Smith35f986d2014-08-11 22:11:07 +0000752
Richard Smithf7514452014-10-30 21:02:37 +0000753 DeclContext::all_lookups_iterator I = Primary->noload_lookups_begin(),
754 E = Primary->noload_lookups_end();
755 while (I != E) {
756 DeclarationName Name = I.getLookupName();
757 DeclContextLookupResult R = *I++;
Richard Smith35f986d2014-08-11 22:11:07 +0000758
Richard Smithf7514452014-10-30 21:02:37 +0000759 dumpChild([=] {
760 OS << "DeclarationName ";
761 {
762 ColorScope Color(*this, DeclNameColor);
763 OS << '\'' << Name << '\'';
764 }
Richard Smith35f986d2014-08-11 22:11:07 +0000765
Richard Smithf7514452014-10-30 21:02:37 +0000766 for (DeclContextLookupResult::iterator RI = R.begin(), RE = R.end();
767 RI != RE; ++RI) {
768 dumpChild([=] {
769 dumpBareDeclRef(*RI);
770
771 if ((*RI)->isHidden())
772 OS << " hidden";
773
774 // If requested, dump the redecl chain for this lookup.
775 if (DumpDecls) {
776 // Dump earliest decl first.
777 std::function<void(Decl *)> DumpWithPrev = [&](Decl *D) {
778 if (Decl *Prev = D->getPreviousDecl())
779 DumpWithPrev(Prev);
780 dumpDecl(D);
781 };
782 DumpWithPrev(*RI);
783 }
784 });
785 }
786 });
Richard Smith33937e72013-06-22 21:49:40 +0000787 }
Richard Smith33937e72013-06-22 21:49:40 +0000788
Richard Smithf7514452014-10-30 21:02:37 +0000789 if (HasUndeserializedLookups) {
790 dumpChild([=] {
791 ColorScope Color(*this, UndeserializedColor);
792 OS << "<undeserialized lookups>";
793 });
794 }
795 });
Richard Smith33937e72013-06-22 21:49:40 +0000796}
797
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000798void ASTDumper::dumpAttr(const Attr *A) {
Richard Smithf7514452014-10-30 21:02:37 +0000799 dumpChild([=] {
800 {
801 ColorScope Color(*this, AttrColor);
Aaron Ballman36a53502014-01-16 13:03:14 +0000802
Richard Smithf7514452014-10-30 21:02:37 +0000803 switch (A->getKind()) {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000804#define ATTR(X) case attr::X: OS << #X; break;
805#include "clang/Basic/AttrList.inc"
Richard Smithf7514452014-10-30 21:02:37 +0000806 default:
807 llvm_unreachable("unexpected attribute kind");
808 }
809 OS << "Attr";
Richard Trieud215b8d2013-01-26 01:31:20 +0000810 }
Richard Smithf7514452014-10-30 21:02:37 +0000811 dumpPointer(A);
812 dumpSourceRange(A->getRange());
813 if (A->isInherited())
814 OS << " Inherited";
815 if (A->isImplicit())
816 OS << " Implicit";
Hans Wennborgb0a8b4a2014-05-31 04:05:57 +0000817#include "clang/AST/AttrDump.inc"
Richard Smithf7514452014-10-30 21:02:37 +0000818 });
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000819}
820
Richard Smith71bec062013-10-15 21:58:30 +0000821static void dumpPreviousDeclImpl(raw_ostream &OS, ...) {}
822
823template<typename T>
824static void dumpPreviousDeclImpl(raw_ostream &OS, const Mergeable<T> *D) {
Rafael Espindola8db352d2013-10-17 15:37:26 +0000825 const T *First = D->getFirstDecl();
Richard Smith71bec062013-10-15 21:58:30 +0000826 if (First != D)
827 OS << " first " << First;
Richard Smithf5f43542013-02-07 01:35:44 +0000828}
829
830template<typename T>
Richard Smith71bec062013-10-15 21:58:30 +0000831static void dumpPreviousDeclImpl(raw_ostream &OS, const Redeclarable<T> *D) {
832 const T *Prev = D->getPreviousDecl();
833 if (Prev)
834 OS << " prev " << Prev;
Richard Smithf5f43542013-02-07 01:35:44 +0000835}
836
Richard Smith71bec062013-10-15 21:58:30 +0000837/// Dump the previous declaration in the redeclaration chain for a declaration,
838/// if any.
839static void dumpPreviousDecl(raw_ostream &OS, const Decl *D) {
Richard Smithf5f43542013-02-07 01:35:44 +0000840 switch (D->getKind()) {
841#define DECL(DERIVED, BASE) \
842 case Decl::DERIVED: \
Richard Smith71bec062013-10-15 21:58:30 +0000843 return dumpPreviousDeclImpl(OS, cast<DERIVED##Decl>(D));
Richard Smithf5f43542013-02-07 01:35:44 +0000844#define ABSTRACT_DECL(DECL)
845#include "clang/AST/DeclNodes.inc"
846 }
847 llvm_unreachable("Decl that isn't part of DeclNodes.inc!");
848}
849
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000850//===----------------------------------------------------------------------===//
851// C++ Utilities
852//===----------------------------------------------------------------------===//
853
854void ASTDumper::dumpAccessSpecifier(AccessSpecifier AS) {
855 switch (AS) {
856 case AS_none:
857 break;
858 case AS_public:
859 OS << "public";
860 break;
861 case AS_protected:
862 OS << "protected";
863 break;
864 case AS_private:
865 OS << "private";
866 break;
867 }
868}
869
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000870void ASTDumper::dumpCXXCtorInitializer(const CXXCtorInitializer *Init) {
Richard Smithf7514452014-10-30 21:02:37 +0000871 dumpChild([=] {
872 OS << "CXXCtorInitializer";
873 if (Init->isAnyMemberInitializer()) {
874 OS << ' ';
875 dumpBareDeclRef(Init->getAnyMember());
876 } else if (Init->isBaseInitializer()) {
877 dumpType(QualType(Init->getBaseClass(), 0));
878 } else if (Init->isDelegatingInitializer()) {
879 dumpType(Init->getTypeSourceInfo()->getType());
880 } else {
881 llvm_unreachable("Unknown initializer type");
882 }
883 dumpStmt(Init->getInit());
884 });
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000885}
886
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000887void ASTDumper::dumpTemplateParameters(const TemplateParameterList *TPL) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000888 if (!TPL)
889 return;
890
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000891 for (TemplateParameterList::const_iterator I = TPL->begin(), E = TPL->end();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000892 I != E; ++I)
893 dumpDecl(*I);
894}
895
896void ASTDumper::dumpTemplateArgumentListInfo(
897 const TemplateArgumentListInfo &TALI) {
Richard Smithf7514452014-10-30 21:02:37 +0000898 for (unsigned i = 0, e = TALI.size(); i < e; ++i)
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000899 dumpTemplateArgumentLoc(TALI[i]);
900}
901
902void ASTDumper::dumpTemplateArgumentLoc(const TemplateArgumentLoc &A) {
903 dumpTemplateArgument(A.getArgument(), A.getSourceRange());
904}
905
906void ASTDumper::dumpTemplateArgumentList(const TemplateArgumentList &TAL) {
907 for (unsigned i = 0, e = TAL.size(); i < e; ++i)
908 dumpTemplateArgument(TAL[i]);
909}
910
911void ASTDumper::dumpTemplateArgument(const TemplateArgument &A, SourceRange R) {
Richard Smithf7514452014-10-30 21:02:37 +0000912 dumpChild([=] {
913 OS << "TemplateArgument";
914 if (R.isValid())
915 dumpSourceRange(R);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000916
Richard Smithf7514452014-10-30 21:02:37 +0000917 switch (A.getKind()) {
918 case TemplateArgument::Null:
919 OS << " null";
920 break;
921 case TemplateArgument::Type:
922 OS << " type";
923 dumpType(A.getAsType());
924 break;
925 case TemplateArgument::Declaration:
926 OS << " decl";
927 dumpDeclRef(A.getAsDecl());
928 break;
929 case TemplateArgument::NullPtr:
930 OS << " nullptr";
931 break;
932 case TemplateArgument::Integral:
933 OS << " integral " << A.getAsIntegral();
934 break;
935 case TemplateArgument::Template:
936 OS << " template ";
937 A.getAsTemplate().dump(OS);
938 break;
939 case TemplateArgument::TemplateExpansion:
940 OS << " template expansion";
941 A.getAsTemplateOrTemplatePattern().dump(OS);
942 break;
943 case TemplateArgument::Expression:
944 OS << " expr";
945 dumpStmt(A.getAsExpr());
946 break;
947 case TemplateArgument::Pack:
948 OS << " pack";
949 for (TemplateArgument::pack_iterator I = A.pack_begin(), E = A.pack_end();
950 I != E; ++I)
951 dumpTemplateArgument(*I);
952 break;
Richard Trieude5cc7d2013-01-31 01:44:26 +0000953 }
Richard Smithf7514452014-10-30 21:02:37 +0000954 });
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000955}
956
Chris Lattner11e30d32007-08-30 06:17:34 +0000957//===----------------------------------------------------------------------===//
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000958// Decl dumping methods.
Chris Lattnercbe4f772007-08-08 22:51:59 +0000959//===----------------------------------------------------------------------===//
960
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000961void ASTDumper::dumpDecl(const Decl *D) {
Richard Smithf7514452014-10-30 21:02:37 +0000962 dumpChild([=] {
963 if (!D) {
964 ColorScope Color(*this, NullColor);
965 OS << "<<<NULL>>>";
966 return;
967 }
Mike Stump11289f42009-09-09 15:08:12 +0000968
Richard Smithf7514452014-10-30 21:02:37 +0000969 {
970 ColorScope Color(*this, DeclKindNameColor);
971 OS << D->getDeclKindName() << "Decl";
972 }
973 dumpPointer(D);
974 if (D->getLexicalDeclContext() != D->getDeclContext())
975 OS << " parent " << cast<Decl>(D->getDeclContext());
976 dumpPreviousDecl(OS, D);
977 dumpSourceRange(D->getSourceRange());
978 OS << ' ';
979 dumpLocation(D->getLocation());
Richard Smith42413142015-05-15 20:05:43 +0000980 if (Module *M = D->getImportedOwningModule())
Richard Smithf7514452014-10-30 21:02:37 +0000981 OS << " in " << M->getFullModuleName();
Richard Smith42413142015-05-15 20:05:43 +0000982 else if (Module *M = D->getLocalOwningModule())
983 OS << " in (local) " << M->getFullModuleName();
Richard Smitha2eb4092015-06-22 18:47:01 +0000984 if (auto *ND = dyn_cast<NamedDecl>(D))
985 for (Module *M : D->getASTContext().getModulesWithMergedDefinition(
986 const_cast<NamedDecl *>(ND)))
987 dumpChild([=] { OS << "also in " << M->getFullModuleName(); });
Richard Smithf7514452014-10-30 21:02:37 +0000988 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
989 if (ND->isHidden())
990 OS << " hidden";
991 if (D->isImplicit())
992 OS << " implicit";
993 if (D->isUsed())
994 OS << " used";
995 else if (D->isThisDeclarationReferenced())
996 OS << " referenced";
997 if (D->isInvalidDecl())
998 OS << " invalid";
Hans Wennborg76b00532014-12-05 22:38:57 +0000999 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1000 if (FD->isConstexpr())
1001 OS << " constexpr";
1002
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001003
Richard Smithf7514452014-10-30 21:02:37 +00001004 ConstDeclVisitor<ASTDumper>::Visit(D);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001005
Richard Smithf7514452014-10-30 21:02:37 +00001006 for (Decl::attr_iterator I = D->attr_begin(), E = D->attr_end(); I != E;
1007 ++I)
1008 dumpAttr(*I);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001009
Richard Smithf7514452014-10-30 21:02:37 +00001010 if (const FullComment *Comment =
1011 D->getASTContext().getLocalCommentForDeclUncached(D))
1012 dumpFullComment(Comment);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001013
Richard Smithf7514452014-10-30 21:02:37 +00001014 // Decls within functions are visited by the body.
1015 if (!isa<FunctionDecl>(*D) && !isa<ObjCMethodDecl>(*D) &&
1016 hasNodes(dyn_cast<DeclContext>(D)))
1017 dumpDeclContext(cast<DeclContext>(D));
1018 });
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001019}
1020
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001021void ASTDumper::VisitLabelDecl(const LabelDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001022 dumpName(D);
1023}
1024
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001025void ASTDumper::VisitTypedefDecl(const TypedefDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001026 dumpName(D);
1027 dumpType(D->getUnderlyingType());
1028 if (D->isModulePrivate())
1029 OS << " __module_private__";
1030}
1031
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001032void ASTDumper::VisitEnumDecl(const EnumDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001033 if (D->isScoped()) {
1034 if (D->isScopedUsingClassTag())
1035 OS << " class";
1036 else
1037 OS << " struct";
1038 }
1039 dumpName(D);
1040 if (D->isModulePrivate())
1041 OS << " __module_private__";
1042 if (D->isFixed())
1043 dumpType(D->getIntegerType());
1044}
1045
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001046void ASTDumper::VisitRecordDecl(const RecordDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001047 OS << ' ' << D->getKindName();
1048 dumpName(D);
1049 if (D->isModulePrivate())
1050 OS << " __module_private__";
Richard Smith99bc1b92013-08-30 05:32:29 +00001051 if (D->isCompleteDefinition())
1052 OS << " definition";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001053}
1054
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001055void ASTDumper::VisitEnumConstantDecl(const EnumConstantDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001056 dumpName(D);
1057 dumpType(D->getType());
Richard Smithf7514452014-10-30 21:02:37 +00001058 if (const Expr *Init = D->getInitExpr())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001059 dumpStmt(Init);
1060}
1061
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001062void ASTDumper::VisitIndirectFieldDecl(const IndirectFieldDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001063 dumpName(D);
1064 dumpType(D->getType());
Richard Smithdcc2c452014-03-17 23:00:06 +00001065
Richard Smith8aa49222014-03-18 00:35:12 +00001066 for (auto *Child : D->chain())
Richard Smithf7514452014-10-30 21:02:37 +00001067 dumpDeclRef(Child);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001068}
1069
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001070void ASTDumper::VisitFunctionDecl(const FunctionDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001071 dumpName(D);
1072 dumpType(D->getType());
1073
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001074 StorageClass SC = D->getStorageClass();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001075 if (SC != SC_None)
1076 OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
1077 if (D->isInlineSpecified())
1078 OS << " inline";
1079 if (D->isVirtualAsWritten())
1080 OS << " virtual";
1081 if (D->isModulePrivate())
1082 OS << " __module_private__";
1083
1084 if (D->isPure())
1085 OS << " pure";
1086 else if (D->isDeletedAsWritten())
1087 OS << " delete";
1088
Richard Smithadaa0152013-05-17 02:09:46 +00001089 if (const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>()) {
1090 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
Richard Smith8acb4282014-07-31 21:57:55 +00001091 switch (EPI.ExceptionSpec.Type) {
Richard Smithadaa0152013-05-17 02:09:46 +00001092 default: break;
1093 case EST_Unevaluated:
Richard Smith8acb4282014-07-31 21:57:55 +00001094 OS << " noexcept-unevaluated " << EPI.ExceptionSpec.SourceDecl;
Richard Smithadaa0152013-05-17 02:09:46 +00001095 break;
1096 case EST_Uninstantiated:
Richard Smith8acb4282014-07-31 21:57:55 +00001097 OS << " noexcept-uninstantiated " << EPI.ExceptionSpec.SourceTemplate;
Richard Smithadaa0152013-05-17 02:09:46 +00001098 break;
1099 }
1100 }
1101
Richard Smithf7514452014-10-30 21:02:37 +00001102 if (const FunctionTemplateSpecializationInfo *FTSI =
1103 D->getTemplateSpecializationInfo())
Richard Trieude5cc7d2013-01-31 01:44:26 +00001104 dumpTemplateArgumentList(*FTSI->TemplateArguments);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001105
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001106 for (ArrayRef<NamedDecl *>::iterator
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001107 I = D->getDeclsInPrototypeScope().begin(),
Richard Smithf7514452014-10-30 21:02:37 +00001108 E = D->getDeclsInPrototypeScope().end(); I != E; ++I)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001109 dumpDecl(*I);
1110
Richard Smith8a639892015-01-24 01:07:20 +00001111 if (!D->param_begin() && D->getNumParams())
1112 dumpChild([=] { OS << "<<NULL params x " << D->getNumParams() << ">>"; });
1113 else
1114 for (FunctionDecl::param_const_iterator I = D->param_begin(),
1115 E = D->param_end();
1116 I != E; ++I)
1117 dumpDecl(*I);
Richard Smithf7514452014-10-30 21:02:37 +00001118
1119 if (const CXXConstructorDecl *C = dyn_cast<CXXConstructorDecl>(D))
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001120 for (CXXConstructorDecl::init_const_iterator I = C->init_begin(),
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001121 E = C->init_end();
Richard Smithf7514452014-10-30 21:02:37 +00001122 I != E; ++I)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001123 dumpCXXCtorInitializer(*I);
1124
Richard Smithf7514452014-10-30 21:02:37 +00001125 if (D->doesThisDeclarationHaveABody())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001126 dumpStmt(D->getBody());
1127}
1128
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001129void ASTDumper::VisitFieldDecl(const FieldDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001130 dumpName(D);
1131 dumpType(D->getType());
1132 if (D->isMutable())
1133 OS << " mutable";
1134 if (D->isModulePrivate())
1135 OS << " __module_private__";
Richard Trieude5cc7d2013-01-31 01:44:26 +00001136
Richard Smithf7514452014-10-30 21:02:37 +00001137 if (D->isBitField())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001138 dumpStmt(D->getBitWidth());
Richard Smithf7514452014-10-30 21:02:37 +00001139 if (Expr *Init = D->getInClassInitializer())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001140 dumpStmt(Init);
1141}
1142
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001143void ASTDumper::VisitVarDecl(const VarDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001144 dumpName(D);
1145 dumpType(D->getType());
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001146 StorageClass SC = D->getStorageClass();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001147 if (SC != SC_None)
1148 OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
Richard Smithfd3834f2013-04-13 02:43:54 +00001149 switch (D->getTLSKind()) {
1150 case VarDecl::TLS_None: break;
1151 case VarDecl::TLS_Static: OS << " tls"; break;
1152 case VarDecl::TLS_Dynamic: OS << " tls_dynamic"; break;
1153 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001154 if (D->isModulePrivate())
1155 OS << " __module_private__";
1156 if (D->isNRVOVariable())
1157 OS << " nrvo";
Richard Trieude5cc7d2013-01-31 01:44:26 +00001158 if (D->hasInit()) {
Richard Smithd9967cc2014-07-10 22:54:03 +00001159 switch (D->getInitStyle()) {
1160 case VarDecl::CInit: OS << " cinit"; break;
1161 case VarDecl::CallInit: OS << " callinit"; break;
1162 case VarDecl::ListInit: OS << " listinit"; break;
1163 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001164 dumpStmt(D->getInit());
Richard Trieude5cc7d2013-01-31 01:44:26 +00001165 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001166}
1167
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001168void ASTDumper::VisitFileScopeAsmDecl(const FileScopeAsmDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001169 dumpStmt(D->getAsmString());
1170}
1171
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001172void ASTDumper::VisitImportDecl(const ImportDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001173 OS << ' ' << D->getImportedModule()->getFullModuleName();
1174}
1175
1176//===----------------------------------------------------------------------===//
1177// C++ Declarations
1178//===----------------------------------------------------------------------===//
1179
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001180void ASTDumper::VisitNamespaceDecl(const NamespaceDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001181 dumpName(D);
1182 if (D->isInline())
1183 OS << " inline";
1184 if (!D->isOriginalNamespace())
1185 dumpDeclRef(D->getOriginalNamespace(), "original");
1186}
1187
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001188void ASTDumper::VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001189 OS << ' ';
1190 dumpBareDeclRef(D->getNominatedNamespace());
1191}
1192
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001193void ASTDumper::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001194 dumpName(D);
1195 dumpDeclRef(D->getAliasedNamespace());
1196}
1197
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001198void ASTDumper::VisitTypeAliasDecl(const TypeAliasDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001199 dumpName(D);
1200 dumpType(D->getUnderlyingType());
1201}
1202
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001203void ASTDumper::VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001204 dumpName(D);
1205 dumpTemplateParameters(D->getTemplateParameters());
1206 dumpDecl(D->getTemplatedDecl());
1207}
1208
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001209void ASTDumper::VisitCXXRecordDecl(const CXXRecordDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001210 VisitRecordDecl(D);
1211 if (!D->isCompleteDefinition())
1212 return;
1213
Aaron Ballman574705e2014-03-13 15:41:46 +00001214 for (const auto &I : D->bases()) {
Richard Smithf7514452014-10-30 21:02:37 +00001215 dumpChild([=] {
1216 if (I.isVirtual())
1217 OS << "virtual ";
1218 dumpAccessSpecifier(I.getAccessSpecifier());
1219 dumpType(I.getType());
1220 if (I.isPackExpansion())
1221 OS << "...";
1222 });
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001223 }
1224}
1225
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001226void ASTDumper::VisitStaticAssertDecl(const StaticAssertDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001227 dumpStmt(D->getAssertExpr());
1228 dumpStmt(D->getMessage());
1229}
1230
Richard Smithcbdf7332014-03-18 02:07:28 +00001231template<typename SpecializationDecl>
Richard Smithf7514452014-10-30 21:02:37 +00001232void ASTDumper::VisitTemplateDeclSpecialization(const SpecializationDecl *D,
Richard Smithcbdf7332014-03-18 02:07:28 +00001233 bool DumpExplicitInst,
1234 bool DumpRefOnly) {
1235 bool DumpedAny = false;
1236 for (auto *RedeclWithBadType : D->redecls()) {
1237 // FIXME: The redecls() range sometimes has elements of a less-specific
1238 // type. (In particular, ClassTemplateSpecializationDecl::redecls() gives
1239 // us TagDecls, and should give CXXRecordDecls).
1240 auto *Redecl = dyn_cast<SpecializationDecl>(RedeclWithBadType);
1241 if (!Redecl) {
1242 // Found the injected-class-name for a class template. This will be dumped
1243 // as part of its surrounding class so we don't need to dump it here.
1244 assert(isa<CXXRecordDecl>(RedeclWithBadType) &&
1245 "expected an injected-class-name");
1246 continue;
1247 }
1248
1249 switch (Redecl->getTemplateSpecializationKind()) {
1250 case TSK_ExplicitInstantiationDeclaration:
1251 case TSK_ExplicitInstantiationDefinition:
1252 if (!DumpExplicitInst)
1253 break;
1254 // Fall through.
1255 case TSK_Undeclared:
1256 case TSK_ImplicitInstantiation:
Richard Smithf7514452014-10-30 21:02:37 +00001257 if (DumpRefOnly)
1258 dumpDeclRef(Redecl);
1259 else
1260 dumpDecl(Redecl);
Richard Smithcbdf7332014-03-18 02:07:28 +00001261 DumpedAny = true;
1262 break;
1263 case TSK_ExplicitSpecialization:
1264 break;
1265 }
1266 }
1267
1268 // Ensure we dump at least one decl for each specialization.
1269 if (!DumpedAny)
Richard Smithf7514452014-10-30 21:02:37 +00001270 dumpDeclRef(D);
Richard Smithcbdf7332014-03-18 02:07:28 +00001271}
1272
Richard Smith20ade552014-03-17 23:34:53 +00001273template<typename TemplateDecl>
1274void ASTDumper::VisitTemplateDecl(const TemplateDecl *D,
1275 bool DumpExplicitInst) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001276 dumpName(D);
1277 dumpTemplateParameters(D->getTemplateParameters());
Richard Smithdcc2c452014-03-17 23:00:06 +00001278
Richard Smithf7514452014-10-30 21:02:37 +00001279 dumpDecl(D->getTemplatedDecl());
Richard Smithdcc2c452014-03-17 23:00:06 +00001280
Richard Smithcbdf7332014-03-18 02:07:28 +00001281 for (auto *Child : D->specializations())
Richard Smithf7514452014-10-30 21:02:37 +00001282 VisitTemplateDeclSpecialization(Child, DumpExplicitInst,
Richard Smithcbdf7332014-03-18 02:07:28 +00001283 !D->isCanonicalDecl());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001284}
1285
Richard Smith20ade552014-03-17 23:34:53 +00001286void ASTDumper::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
1287 // FIXME: We don't add a declaration of a function template specialization
1288 // to its context when it's explicitly instantiated, so dump explicit
1289 // instantiations when we dump the template itself.
1290 VisitTemplateDecl(D, true);
1291}
1292
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001293void ASTDumper::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
Richard Smith20ade552014-03-17 23:34:53 +00001294 VisitTemplateDecl(D, false);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001295}
1296
1297void ASTDumper::VisitClassTemplateSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001298 const ClassTemplateSpecializationDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001299 VisitCXXRecordDecl(D);
1300 dumpTemplateArgumentList(D->getTemplateArgs());
1301}
1302
1303void ASTDumper::VisitClassTemplatePartialSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001304 const ClassTemplatePartialSpecializationDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001305 VisitClassTemplateSpecializationDecl(D);
1306 dumpTemplateParameters(D->getTemplateParameters());
1307}
1308
1309void ASTDumper::VisitClassScopeFunctionSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001310 const ClassScopeFunctionSpecializationDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001311 dumpDeclRef(D->getSpecialization());
1312 if (D->hasExplicitTemplateArgs())
1313 dumpTemplateArgumentListInfo(D->templateArgs());
1314}
1315
Richard Smithd25789a2013-09-18 01:36:02 +00001316void ASTDumper::VisitVarTemplateDecl(const VarTemplateDecl *D) {
Richard Smith20ade552014-03-17 23:34:53 +00001317 VisitTemplateDecl(D, false);
Richard Smithd25789a2013-09-18 01:36:02 +00001318}
1319
1320void ASTDumper::VisitVarTemplateSpecializationDecl(
1321 const VarTemplateSpecializationDecl *D) {
1322 dumpTemplateArgumentList(D->getTemplateArgs());
1323 VisitVarDecl(D);
1324}
1325
1326void ASTDumper::VisitVarTemplatePartialSpecializationDecl(
1327 const VarTemplatePartialSpecializationDecl *D) {
1328 dumpTemplateParameters(D->getTemplateParameters());
1329 VisitVarTemplateSpecializationDecl(D);
1330}
1331
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001332void ASTDumper::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001333 if (D->wasDeclaredWithTypename())
1334 OS << " typename";
1335 else
1336 OS << " class";
1337 if (D->isParameterPack())
1338 OS << " ...";
1339 dumpName(D);
Richard Smithf7514452014-10-30 21:02:37 +00001340 if (D->hasDefaultArgument())
Richard Smithecf74ff2014-03-23 20:50:39 +00001341 dumpTemplateArgument(D->getDefaultArgument());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001342}
1343
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001344void ASTDumper::VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001345 dumpType(D->getType());
1346 if (D->isParameterPack())
1347 OS << " ...";
1348 dumpName(D);
Richard Smithf7514452014-10-30 21:02:37 +00001349 if (D->hasDefaultArgument())
Richard Smithecf74ff2014-03-23 20:50:39 +00001350 dumpTemplateArgument(D->getDefaultArgument());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001351}
1352
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001353void ASTDumper::VisitTemplateTemplateParmDecl(
1354 const TemplateTemplateParmDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001355 if (D->isParameterPack())
1356 OS << " ...";
1357 dumpName(D);
1358 dumpTemplateParameters(D->getTemplateParameters());
Richard Smithf7514452014-10-30 21:02:37 +00001359 if (D->hasDefaultArgument())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001360 dumpTemplateArgumentLoc(D->getDefaultArgument());
1361}
1362
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001363void ASTDumper::VisitUsingDecl(const UsingDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001364 OS << ' ';
1365 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1366 OS << D->getNameAsString();
1367}
1368
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001369void ASTDumper::VisitUnresolvedUsingTypenameDecl(
1370 const UnresolvedUsingTypenameDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001371 OS << ' ';
1372 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1373 OS << D->getNameAsString();
1374}
1375
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001376void ASTDumper::VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001377 OS << ' ';
1378 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1379 OS << D->getNameAsString();
1380 dumpType(D->getType());
1381}
1382
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001383void ASTDumper::VisitUsingShadowDecl(const UsingShadowDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001384 OS << ' ';
1385 dumpBareDeclRef(D->getTargetDecl());
1386}
1387
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001388void ASTDumper::VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001389 switch (D->getLanguage()) {
1390 case LinkageSpecDecl::lang_c: OS << " C"; break;
1391 case LinkageSpecDecl::lang_cxx: OS << " C++"; break;
1392 }
1393}
1394
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001395void ASTDumper::VisitAccessSpecDecl(const AccessSpecDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001396 OS << ' ';
1397 dumpAccessSpecifier(D->getAccess());
1398}
1399
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001400void ASTDumper::VisitFriendDecl(const FriendDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001401 if (TypeSourceInfo *T = D->getFriendType())
1402 dumpType(T->getType());
1403 else
1404 dumpDecl(D->getFriendDecl());
1405}
1406
1407//===----------------------------------------------------------------------===//
1408// Obj-C Declarations
1409//===----------------------------------------------------------------------===//
1410
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001411void ASTDumper::VisitObjCIvarDecl(const ObjCIvarDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001412 dumpName(D);
1413 dumpType(D->getType());
1414 if (D->getSynthesize())
1415 OS << " synthesize";
1416
1417 switch (D->getAccessControl()) {
1418 case ObjCIvarDecl::None:
1419 OS << " none";
1420 break;
1421 case ObjCIvarDecl::Private:
1422 OS << " private";
1423 break;
1424 case ObjCIvarDecl::Protected:
1425 OS << " protected";
1426 break;
1427 case ObjCIvarDecl::Public:
1428 OS << " public";
1429 break;
1430 case ObjCIvarDecl::Package:
1431 OS << " package";
1432 break;
1433 }
1434}
1435
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001436void ASTDumper::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001437 if (D->isInstanceMethod())
1438 OS << " -";
1439 else
1440 OS << " +";
1441 dumpName(D);
Alp Toker314cc812014-01-25 16:55:45 +00001442 dumpType(D->getReturnType());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001443
Richard Trieude5cc7d2013-01-31 01:44:26 +00001444 if (D->isThisDeclarationADefinition()) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001445 dumpDeclContext(D);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001446 } else {
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001447 for (ObjCMethodDecl::param_const_iterator I = D->param_begin(),
1448 E = D->param_end();
Richard Smithf7514452014-10-30 21:02:37 +00001449 I != E; ++I)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001450 dumpDecl(*I);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001451 }
1452
Richard Smithf7514452014-10-30 21:02:37 +00001453 if (D->isVariadic())
1454 dumpChild([=] { OS << "..."; });
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001455
Richard Smithf7514452014-10-30 21:02:37 +00001456 if (D->hasBody())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001457 dumpStmt(D->getBody());
1458}
1459
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001460void ASTDumper::VisitObjCCategoryDecl(const ObjCCategoryDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001461 dumpName(D);
1462 dumpDeclRef(D->getClassInterface());
1463 dumpDeclRef(D->getImplementation());
1464 for (ObjCCategoryDecl::protocol_iterator I = D->protocol_begin(),
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001465 E = D->protocol_end();
Richard Smithf7514452014-10-30 21:02:37 +00001466 I != E; ++I)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001467 dumpDeclRef(*I);
1468}
1469
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001470void ASTDumper::VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001471 dumpName(D);
1472 dumpDeclRef(D->getClassInterface());
1473 dumpDeclRef(D->getCategoryDecl());
1474}
1475
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001476void ASTDumper::VisitObjCProtocolDecl(const ObjCProtocolDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001477 dumpName(D);
Richard Smithdcc2c452014-03-17 23:00:06 +00001478
Richard Smith7fcb35f2014-03-18 02:37:59 +00001479 for (auto *Child : D->protocols())
Richard Smithf7514452014-10-30 21:02:37 +00001480 dumpDeclRef(Child);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001481}
1482
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001483void ASTDumper::VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001484 dumpName(D);
1485 dumpDeclRef(D->getSuperClass(), "super");
Richard Smithdcc2c452014-03-17 23:00:06 +00001486
Richard Smithf7514452014-10-30 21:02:37 +00001487 dumpDeclRef(D->getImplementation());
Richard Smith7fcb35f2014-03-18 02:37:59 +00001488 for (auto *Child : D->protocols())
Richard Smithf7514452014-10-30 21:02:37 +00001489 dumpDeclRef(Child);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001490}
1491
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001492void ASTDumper::VisitObjCImplementationDecl(const ObjCImplementationDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001493 dumpName(D);
1494 dumpDeclRef(D->getSuperClass(), "super");
1495 dumpDeclRef(D->getClassInterface());
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001496 for (ObjCImplementationDecl::init_const_iterator I = D->init_begin(),
1497 E = D->init_end();
Richard Smithf7514452014-10-30 21:02:37 +00001498 I != E; ++I)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001499 dumpCXXCtorInitializer(*I);
1500}
1501
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001502void ASTDumper::VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001503 dumpName(D);
1504 dumpDeclRef(D->getClassInterface());
1505}
1506
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001507void ASTDumper::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001508 dumpName(D);
1509 dumpType(D->getType());
1510
1511 if (D->getPropertyImplementation() == ObjCPropertyDecl::Required)
1512 OS << " required";
1513 else if (D->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1514 OS << " optional";
1515
1516 ObjCPropertyDecl::PropertyAttributeKind Attrs = D->getPropertyAttributes();
1517 if (Attrs != ObjCPropertyDecl::OBJC_PR_noattr) {
1518 if (Attrs & ObjCPropertyDecl::OBJC_PR_readonly)
1519 OS << " readonly";
1520 if (Attrs & ObjCPropertyDecl::OBJC_PR_assign)
1521 OS << " assign";
1522 if (Attrs & ObjCPropertyDecl::OBJC_PR_readwrite)
1523 OS << " readwrite";
1524 if (Attrs & ObjCPropertyDecl::OBJC_PR_retain)
1525 OS << " retain";
1526 if (Attrs & ObjCPropertyDecl::OBJC_PR_copy)
1527 OS << " copy";
1528 if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic)
1529 OS << " nonatomic";
1530 if (Attrs & ObjCPropertyDecl::OBJC_PR_atomic)
1531 OS << " atomic";
1532 if (Attrs & ObjCPropertyDecl::OBJC_PR_weak)
1533 OS << " weak";
1534 if (Attrs & ObjCPropertyDecl::OBJC_PR_strong)
1535 OS << " strong";
1536 if (Attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained)
1537 OS << " unsafe_unretained";
Richard Smithf7514452014-10-30 21:02:37 +00001538 if (Attrs & ObjCPropertyDecl::OBJC_PR_getter)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001539 dumpDeclRef(D->getGetterMethodDecl(), "getter");
Richard Smithf7514452014-10-30 21:02:37 +00001540 if (Attrs & ObjCPropertyDecl::OBJC_PR_setter)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001541 dumpDeclRef(D->getSetterMethodDecl(), "setter");
1542 }
1543}
1544
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001545void ASTDumper::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001546 dumpName(D->getPropertyDecl());
1547 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
1548 OS << " synthesize";
1549 else
1550 OS << " dynamic";
1551 dumpDeclRef(D->getPropertyDecl());
1552 dumpDeclRef(D->getPropertyIvarDecl());
1553}
1554
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001555void ASTDumper::VisitBlockDecl(const BlockDecl *D) {
Aaron Ballmanb2b8b1d2014-03-07 16:09:59 +00001556 for (auto I : D->params())
1557 dumpDecl(I);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001558
Richard Smithf7514452014-10-30 21:02:37 +00001559 if (D->isVariadic())
1560 dumpChild([=]{ OS << "..."; });
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001561
Richard Smithf7514452014-10-30 21:02:37 +00001562 if (D->capturesCXXThis())
1563 dumpChild([=]{ OS << "capture this"; });
1564
Aaron Ballman9371dd22014-03-14 18:34:04 +00001565 for (const auto &I : D->captures()) {
Richard Smithf7514452014-10-30 21:02:37 +00001566 dumpChild([=] {
1567 OS << "capture";
1568 if (I.isByRef())
1569 OS << " byref";
1570 if (I.isNested())
1571 OS << " nested";
1572 if (I.getVariable()) {
1573 OS << ' ';
1574 dumpBareDeclRef(I.getVariable());
1575 }
1576 if (I.hasCopyExpr())
1577 dumpStmt(I.getCopyExpr());
1578 });
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001579 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001580 dumpStmt(D->getBody());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001581}
1582
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001583//===----------------------------------------------------------------------===//
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001584// Stmt dumping methods.
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001585//===----------------------------------------------------------------------===//
1586
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001587void ASTDumper::dumpStmt(const Stmt *S) {
Richard Smithf7514452014-10-30 21:02:37 +00001588 dumpChild([=] {
1589 if (!S) {
1590 ColorScope Color(*this, NullColor);
1591 OS << "<<<NULL>>>";
1592 return;
1593 }
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001594
Richard Smithf7514452014-10-30 21:02:37 +00001595 if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
1596 VisitDeclStmt(DS);
1597 return;
1598 }
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001599
Richard Smithf7514452014-10-30 21:02:37 +00001600 ConstStmtVisitor<ASTDumper>::Visit(S);
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001601
Richard Smithf7514452014-10-30 21:02:37 +00001602 for (Stmt::const_child_range CI = S->children(); CI; ++CI)
1603 dumpStmt(*CI);
1604 });
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001605}
1606
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001607void ASTDumper::VisitStmt(const Stmt *Node) {
Richard Trieud215b8d2013-01-26 01:31:20 +00001608 {
1609 ColorScope Color(*this, StmtColor);
1610 OS << Node->getStmtClassName();
1611 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001612 dumpPointer(Node);
1613 dumpSourceRange(Node->getSourceRange());
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001614}
1615
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001616void ASTDumper::VisitDeclStmt(const DeclStmt *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001617 VisitStmt(Node);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001618 for (DeclStmt::const_decl_iterator I = Node->decl_begin(),
1619 E = Node->decl_end();
Richard Smithf7514452014-10-30 21:02:37 +00001620 I != E; ++I)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001621 dumpDecl(*I);
Ted Kremenek433a4922007-12-12 06:59:42 +00001622}
1623
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001624void ASTDumper::VisitAttributedStmt(const AttributedStmt *Node) {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001625 VisitStmt(Node);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001626 for (ArrayRef<const Attr *>::iterator I = Node->getAttrs().begin(),
1627 E = Node->getAttrs().end();
Richard Smithf7514452014-10-30 21:02:37 +00001628 I != E; ++I)
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001629 dumpAttr(*I);
1630}
1631
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001632void ASTDumper::VisitLabelStmt(const LabelStmt *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001633 VisitStmt(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001634 OS << " '" << Node->getName() << "'";
Chris Lattnercbe4f772007-08-08 22:51:59 +00001635}
1636
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001637void ASTDumper::VisitGotoStmt(const GotoStmt *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001638 VisitStmt(Node);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001639 OS << " '" << Node->getLabel()->getName() << "'";
1640 dumpPointer(Node->getLabel());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001641}
1642
Pavel Labath1ef83422013-09-04 14:35:00 +00001643void ASTDumper::VisitCXXCatchStmt(const CXXCatchStmt *Node) {
1644 VisitStmt(Node);
1645 dumpDecl(Node->getExceptionDecl());
1646}
1647
Chris Lattnercbe4f772007-08-08 22:51:59 +00001648//===----------------------------------------------------------------------===//
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001649// Expr dumping methods.
Chris Lattnercbe4f772007-08-08 22:51:59 +00001650//===----------------------------------------------------------------------===//
1651
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001652void ASTDumper::VisitExpr(const Expr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001653 VisitStmt(Node);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001654 dumpType(Node->getType());
1655
Richard Trieud215b8d2013-01-26 01:31:20 +00001656 {
1657 ColorScope Color(*this, ValueKindColor);
1658 switch (Node->getValueKind()) {
1659 case VK_RValue:
1660 break;
1661 case VK_LValue:
1662 OS << " lvalue";
1663 break;
1664 case VK_XValue:
1665 OS << " xvalue";
1666 break;
1667 }
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001668 }
1669
Richard Trieud215b8d2013-01-26 01:31:20 +00001670 {
1671 ColorScope Color(*this, ObjectKindColor);
1672 switch (Node->getObjectKind()) {
1673 case OK_Ordinary:
1674 break;
1675 case OK_BitField:
1676 OS << " bitfield";
1677 break;
1678 case OK_ObjCProperty:
1679 OS << " objcproperty";
1680 break;
1681 case OK_ObjCSubscript:
1682 OS << " objcsubscript";
1683 break;
1684 case OK_VectorComponent:
1685 OS << " vectorcomponent";
1686 break;
1687 }
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001688 }
Chris Lattnercbe4f772007-08-08 22:51:59 +00001689}
1690
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001691static void dumpBasePath(raw_ostream &OS, const CastExpr *Node) {
John McCallcf142162010-08-07 06:22:56 +00001692 if (Node->path_empty())
Anders Carlssona70cff62010-04-24 19:06:50 +00001693 return;
1694
1695 OS << " (";
1696 bool First = true;
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001697 for (CastExpr::path_const_iterator I = Node->path_begin(),
1698 E = Node->path_end();
1699 I != E; ++I) {
Anders Carlssona70cff62010-04-24 19:06:50 +00001700 const CXXBaseSpecifier *Base = *I;
1701 if (!First)
1702 OS << " -> ";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001703
Anders Carlssona70cff62010-04-24 19:06:50 +00001704 const CXXRecordDecl *RD =
1705 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001706
Anders Carlssona70cff62010-04-24 19:06:50 +00001707 if (Base->isVirtual())
1708 OS << "virtual ";
1709 OS << RD->getName();
1710 First = false;
1711 }
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001712
Anders Carlssona70cff62010-04-24 19:06:50 +00001713 OS << ')';
1714}
1715
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001716void ASTDumper::VisitCastExpr(const CastExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001717 VisitExpr(Node);
Richard Trieud215b8d2013-01-26 01:31:20 +00001718 OS << " <";
1719 {
1720 ColorScope Color(*this, CastColor);
1721 OS << Node->getCastKindName();
1722 }
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001723 dumpBasePath(OS, Node);
Anders Carlssona70cff62010-04-24 19:06:50 +00001724 OS << ">";
Anders Carlssond7923c62009-08-22 23:33:40 +00001725}
1726
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001727void ASTDumper::VisitDeclRefExpr(const DeclRefExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001728 VisitExpr(Node);
Ted Kremenek5f64ca82007-09-10 17:32:55 +00001729
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001730 OS << " ";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001731 dumpBareDeclRef(Node->getDecl());
Chandler Carruth8d26bb02011-05-01 23:48:14 +00001732 if (Node->getDecl() != Node->getFoundDecl()) {
1733 OS << " (";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001734 dumpBareDeclRef(Node->getFoundDecl());
Chandler Carruth8d26bb02011-05-01 23:48:14 +00001735 OS << ")";
1736 }
John McCall351762c2011-02-07 10:33:21 +00001737}
1738
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001739void ASTDumper::VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001740 VisitExpr(Node);
John McCall76d09942009-12-11 21:50:11 +00001741 OS << " (";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001742 if (!Node->requiresADL())
1743 OS << "no ";
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001744 OS << "ADL) = '" << Node->getName() << '\'';
John McCall76d09942009-12-11 21:50:11 +00001745
1746 UnresolvedLookupExpr::decls_iterator
1747 I = Node->decls_begin(), E = Node->decls_end();
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001748 if (I == E)
1749 OS << " empty";
John McCall76d09942009-12-11 21:50:11 +00001750 for (; I != E; ++I)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001751 dumpPointer(*I);
John McCall76d09942009-12-11 21:50:11 +00001752}
1753
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001754void ASTDumper::VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001755 VisitExpr(Node);
Steve Naroff5d5efca2008-03-12 13:19:12 +00001756
Richard Trieud215b8d2013-01-26 01:31:20 +00001757 {
1758 ColorScope Color(*this, DeclKindNameColor);
1759 OS << " " << Node->getDecl()->getDeclKindName() << "Decl";
1760 }
1761 OS << "='" << *Node->getDecl() << "'";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001762 dumpPointer(Node->getDecl());
Steve Naroffb3424a92008-05-23 22:01:24 +00001763 if (Node->isFreeIvar())
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001764 OS << " isFreeIvar";
Steve Naroff5d5efca2008-03-12 13:19:12 +00001765}
1766
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001767void ASTDumper::VisitPredefinedExpr(const PredefinedExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001768 VisitExpr(Node);
Alexey Bataevec474782014-10-09 08:45:04 +00001769 OS << " " << PredefinedExpr::getIdentTypeName(Node->getIdentType());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001770}
1771
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001772void ASTDumper::VisitCharacterLiteral(const CharacterLiteral *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001773 VisitExpr(Node);
Richard Trieud215b8d2013-01-26 01:31:20 +00001774 ColorScope Color(*this, ValueColor);
Richard Trieu364ee422011-11-03 23:56:23 +00001775 OS << " " << Node->getValue();
Chris Lattnercbe4f772007-08-08 22:51:59 +00001776}
1777
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001778void ASTDumper::VisitIntegerLiteral(const IntegerLiteral *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001779 VisitExpr(Node);
Chris Lattnercbe4f772007-08-08 22:51:59 +00001780
1781 bool isSigned = Node->getType()->isSignedIntegerType();
Richard Trieud215b8d2013-01-26 01:31:20 +00001782 ColorScope Color(*this, ValueColor);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001783 OS << " " << Node->getValue().toString(10, isSigned);
Chris Lattnercbe4f772007-08-08 22:51:59 +00001784}
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001785
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001786void ASTDumper::VisitFloatingLiteral(const FloatingLiteral *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001787 VisitExpr(Node);
Richard Trieud215b8d2013-01-26 01:31:20 +00001788 ColorScope Color(*this, ValueColor);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001789 OS << " " << Node->getValueAsApproximateDouble();
Chris Lattnercbe4f772007-08-08 22:51:59 +00001790}
Chris Lattner1c20a172007-08-26 03:42:43 +00001791
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001792void ASTDumper::VisitStringLiteral(const StringLiteral *Str) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001793 VisitExpr(Str);
Richard Trieud215b8d2013-01-26 01:31:20 +00001794 ColorScope Color(*this, ValueColor);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001795 OS << " ";
Richard Trieudc355912012-06-13 20:25:24 +00001796 Str->outputString(OS);
Chris Lattnercbe4f772007-08-08 22:51:59 +00001797}
Chris Lattner84ca3762007-08-30 01:00:35 +00001798
Richard Smithf0514962014-06-03 08:24:28 +00001799void ASTDumper::VisitInitListExpr(const InitListExpr *ILE) {
1800 VisitExpr(ILE);
1801 if (auto *Filler = ILE->getArrayFiller()) {
Richard Smithf7514452014-10-30 21:02:37 +00001802 dumpChild([=] {
1803 OS << "array filler";
1804 dumpStmt(Filler);
1805 });
Richard Smithf0514962014-06-03 08:24:28 +00001806 }
1807 if (auto *Field = ILE->getInitializedFieldInUnion()) {
1808 OS << " field ";
1809 dumpBareDeclRef(Field);
1810 }
1811}
1812
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001813void ASTDumper::VisitUnaryOperator(const UnaryOperator *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001814 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001815 OS << " " << (Node->isPostfix() ? "postfix" : "prefix")
1816 << " '" << UnaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
Chris Lattnercbe4f772007-08-08 22:51:59 +00001817}
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001818
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001819void ASTDumper::VisitUnaryExprOrTypeTraitExpr(
1820 const UnaryExprOrTypeTraitExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001821 VisitExpr(Node);
Peter Collingbournee190dee2011-03-11 19:24:49 +00001822 switch(Node->getKind()) {
1823 case UETT_SizeOf:
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001824 OS << " sizeof";
Peter Collingbournee190dee2011-03-11 19:24:49 +00001825 break;
1826 case UETT_AlignOf:
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001827 OS << " alignof";
Peter Collingbournee190dee2011-03-11 19:24:49 +00001828 break;
1829 case UETT_VecStep:
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001830 OS << " vec_step";
Peter Collingbournee190dee2011-03-11 19:24:49 +00001831 break;
1832 }
Sebastian Redl6f282892008-11-11 17:56:53 +00001833 if (Node->isArgumentType())
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001834 dumpType(Node->getArgumentType());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001835}
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +00001836
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001837void ASTDumper::VisitMemberExpr(const MemberExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001838 VisitExpr(Node);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001839 OS << " " << (Node->isArrow() ? "->" : ".") << *Node->getMemberDecl();
1840 dumpPointer(Node->getMemberDecl());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001841}
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001842
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001843void ASTDumper::VisitExtVectorElementExpr(const ExtVectorElementExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001844 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001845 OS << " " << Node->getAccessor().getNameStart();
Chris Lattnercbe4f772007-08-08 22:51:59 +00001846}
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001847
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001848void ASTDumper::VisitBinaryOperator(const BinaryOperator *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001849 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001850 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
Chris Lattner86928112007-08-25 02:00:02 +00001851}
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001852
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001853void ASTDumper::VisitCompoundAssignOperator(
1854 const CompoundAssignOperator *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001855 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001856 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode())
1857 << "' ComputeLHSTy=";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001858 dumpBareType(Node->getComputationLHSType());
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001859 OS << " ComputeResultTy=";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001860 dumpBareType(Node->getComputationResultType());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001861}
Chris Lattnercbe4f772007-08-08 22:51:59 +00001862
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001863void ASTDumper::VisitBlockExpr(const BlockExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001864 VisitExpr(Node);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001865 dumpDecl(Node->getBlockDecl());
John McCall351762c2011-02-07 10:33:21 +00001866}
1867
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001868void ASTDumper::VisitOpaqueValueExpr(const OpaqueValueExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001869 VisitExpr(Node);
John McCallfe96e0b2011-11-06 09:01:30 +00001870
Richard Smithf7514452014-10-30 21:02:37 +00001871 if (Expr *Source = Node->getSourceExpr())
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001872 dumpStmt(Source);
John McCallfe96e0b2011-11-06 09:01:30 +00001873}
1874
Chris Lattnercbe4f772007-08-08 22:51:59 +00001875// GNU extensions.
1876
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001877void ASTDumper::VisitAddrLabelExpr(const AddrLabelExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001878 VisitExpr(Node);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001879 OS << " " << Node->getLabel()->getName();
1880 dumpPointer(Node->getLabel());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001881}
1882
Chris Lattner8f184b12007-08-09 18:03:18 +00001883//===----------------------------------------------------------------------===//
1884// C++ Expressions
1885//===----------------------------------------------------------------------===//
Chris Lattnercbe4f772007-08-08 22:51:59 +00001886
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001887void ASTDumper::VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001888 VisitExpr(Node);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001889 OS << " " << Node->getCastName()
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001890 << "<" << Node->getTypeAsWritten().getAsString() << ">"
Anders Carlssona70cff62010-04-24 19:06:50 +00001891 << " <" << Node->getCastKindName();
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001892 dumpBasePath(OS, Node);
Anders Carlssona70cff62010-04-24 19:06:50 +00001893 OS << ">";
Chris Lattnercbe4f772007-08-08 22:51:59 +00001894}
1895
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001896void ASTDumper::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001897 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001898 OS << " " << (Node->getValue() ? "true" : "false");
Chris Lattnercbe4f772007-08-08 22:51:59 +00001899}
1900
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001901void ASTDumper::VisitCXXThisExpr(const CXXThisExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001902 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001903 OS << " this";
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001904}
1905
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001906void ASTDumper::VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001907 VisitExpr(Node);
Eli Friedman29538892011-09-02 17:38:59 +00001908 OS << " functional cast to " << Node->getTypeAsWritten().getAsString()
1909 << " <" << Node->getCastKindName() << ">";
Douglas Gregore200adc2008-10-27 19:41:14 +00001910}
1911
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001912void ASTDumper::VisitCXXConstructExpr(const CXXConstructExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001913 VisitExpr(Node);
John McCalleba90cd2010-02-02 19:03:45 +00001914 CXXConstructorDecl *Ctor = Node->getConstructor();
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001915 dumpType(Ctor->getType());
Anders Carlsson073846832009-08-12 00:21:52 +00001916 if (Node->isElidable())
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001917 OS << " elidable";
John McCall85370042010-08-07 06:38:55 +00001918 if (Node->requiresZeroInitialization())
1919 OS << " zeroing";
Anders Carlsson073846832009-08-12 00:21:52 +00001920}
1921
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001922void ASTDumper::VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001923 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001924 OS << " ";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001925 dumpCXXTemporary(Node->getTemporary());
Anders Carlsson073846832009-08-12 00:21:52 +00001926}
1927
Reid Kleckner5c682bc2015-03-19 18:09:25 +00001928void ASTDumper::VisitCXXNewExpr(const CXXNewExpr *Node) {
1929 VisitExpr(Node);
Reid Kleckner5c682bc2015-03-19 18:09:25 +00001930 if (Node->isGlobalNew())
Reid Kleckner461c0c62015-03-19 18:47:47 +00001931 OS << " global";
Reid Kleckner5c682bc2015-03-19 18:09:25 +00001932 if (Node->isArray())
Reid Kleckner461c0c62015-03-19 18:47:47 +00001933 OS << " array";
1934 if (Node->getOperatorNew()) {
1935 OS << ' ';
1936 dumpBareDeclRef(Node->getOperatorNew());
1937 }
Reid Kleckner5c682bc2015-03-19 18:09:25 +00001938 // We could dump the deallocation function used in case of error, but it's
1939 // usually not that interesting.
1940}
1941
1942void ASTDumper::VisitCXXDeleteExpr(const CXXDeleteExpr *Node) {
1943 VisitExpr(Node);
Reid Kleckner5c682bc2015-03-19 18:09:25 +00001944 if (Node->isGlobalDelete())
Reid Kleckner461c0c62015-03-19 18:47:47 +00001945 OS << " global";
Reid Kleckner5c682bc2015-03-19 18:09:25 +00001946 if (Node->isArrayForm())
Reid Kleckner461c0c62015-03-19 18:47:47 +00001947 OS << " array";
1948 if (Node->getOperatorDelete()) {
1949 OS << ' ';
1950 dumpBareDeclRef(Node->getOperatorDelete());
1951 }
Reid Kleckner5c682bc2015-03-19 18:09:25 +00001952}
1953
Richard Smithe6c01442013-06-05 00:46:14 +00001954void
1955ASTDumper::VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node) {
1956 VisitExpr(Node);
1957 if (const ValueDecl *VD = Node->getExtendingDecl()) {
1958 OS << " extended by ";
1959 dumpBareDeclRef(VD);
1960 }
1961}
1962
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001963void ASTDumper::VisitExprWithCleanups(const ExprWithCleanups *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001964 VisitExpr(Node);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001965 for (unsigned i = 0, e = Node->getNumObjects(); i != e; ++i)
1966 dumpDeclRef(Node->getObject(i), "cleanup");
Anders Carlsson073846832009-08-12 00:21:52 +00001967}
1968
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001969void ASTDumper::dumpCXXTemporary(const CXXTemporary *Temporary) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001970 OS << "(CXXTemporary";
1971 dumpPointer(Temporary);
1972 OS << ")";
Anders Carlsson073846832009-08-12 00:21:52 +00001973}
1974
Serge Pavlov6b926032015-02-16 19:58:41 +00001975void ASTDumper::VisitSizeOfPackExpr(const SizeOfPackExpr *Node) {
1976 VisitExpr(Node);
1977 dumpPointer(Node->getPack());
1978 dumpName(Node->getPack());
1979}
1980
1981
Anders Carlsson76f4a902007-08-21 17:43:55 +00001982//===----------------------------------------------------------------------===//
1983// Obj-C Expressions
1984//===----------------------------------------------------------------------===//
1985
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001986void ASTDumper::VisitObjCMessageExpr(const ObjCMessageExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001987 VisitExpr(Node);
Aaron Ballmanb190f972014-01-03 17:59:55 +00001988 OS << " selector=";
1989 Node->getSelector().print(OS);
Douglas Gregor9a129192010-04-21 00:45:42 +00001990 switch (Node->getReceiverKind()) {
1991 case ObjCMessageExpr::Instance:
1992 break;
1993
1994 case ObjCMessageExpr::Class:
1995 OS << " class=";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001996 dumpBareType(Node->getClassReceiver());
Douglas Gregor9a129192010-04-21 00:45:42 +00001997 break;
1998
1999 case ObjCMessageExpr::SuperInstance:
2000 OS << " super (instance)";
2001 break;
2002
2003 case ObjCMessageExpr::SuperClass:
2004 OS << " super (class)";
2005 break;
2006 }
Ted Kremenek36748da2008-02-29 22:04:05 +00002007}
2008
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002009void ASTDumper::VisitObjCBoxedExpr(const ObjCBoxedExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002010 VisitExpr(Node);
Aaron Ballmanb190f972014-01-03 17:59:55 +00002011 OS << " selector=";
2012 Node->getBoxingMethod()->getSelector().print(OS);
Argyrios Kyrtzidis9dd40892012-05-10 20:02:31 +00002013}
2014
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002015void ASTDumper::VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002016 VisitStmt(Node);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002017 if (const VarDecl *CatchParam = Node->getCatchParamDecl())
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002018 dumpDecl(CatchParam);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002019 else
Douglas Gregor96c79492010-04-23 22:50:49 +00002020 OS << " catch all";
Douglas Gregor96c79492010-04-23 22:50:49 +00002021}
2022
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002023void ASTDumper::VisitObjCEncodeExpr(const ObjCEncodeExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002024 VisitExpr(Node);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00002025 dumpType(Node->getEncodedType());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002026}
2027
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002028void ASTDumper::VisitObjCSelectorExpr(const ObjCSelectorExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002029 VisitExpr(Node);
Mike Stump11289f42009-09-09 15:08:12 +00002030
Aaron Ballmanb190f972014-01-03 17:59:55 +00002031 OS << " ";
2032 Node->getSelector().print(OS);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002033}
2034
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002035void ASTDumper::VisitObjCProtocolExpr(const ObjCProtocolExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002036 VisitExpr(Node);
Mike Stump11289f42009-09-09 15:08:12 +00002037
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00002038 OS << ' ' << *Node->getProtocol();
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002039}
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00002040
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002041void ASTDumper::VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002042 VisitExpr(Node);
John McCallb7bd14f2010-12-02 01:19:52 +00002043 if (Node->isImplicitProperty()) {
Fariborz Jahanian0f0b3022010-12-22 19:46:35 +00002044 OS << " Kind=MethodRef Getter=\"";
2045 if (Node->getImplicitPropertyGetter())
Aaron Ballmanb190f972014-01-03 17:59:55 +00002046 Node->getImplicitPropertyGetter()->getSelector().print(OS);
Fariborz Jahanian0f0b3022010-12-22 19:46:35 +00002047 else
2048 OS << "(null)";
2049
2050 OS << "\" Setter=\"";
John McCallb7bd14f2010-12-02 01:19:52 +00002051 if (ObjCMethodDecl *Setter = Node->getImplicitPropertySetter())
Aaron Ballmanb190f972014-01-03 17:59:55 +00002052 Setter->getSelector().print(OS);
John McCallb7bd14f2010-12-02 01:19:52 +00002053 else
2054 OS << "(null)";
2055 OS << "\"";
2056 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00002057 OS << " Kind=PropertyRef Property=\"" << *Node->getExplicitProperty() <<'"';
John McCallb7bd14f2010-12-02 01:19:52 +00002058 }
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00002059
Fariborz Jahanian681c0752010-10-14 16:04:05 +00002060 if (Node->isSuperReceiver())
2061 OS << " super";
Argyrios Kyrtzidisab468b02012-03-30 00:19:18 +00002062
2063 OS << " Messaging=";
2064 if (Node->isMessagingGetter() && Node->isMessagingSetter())
2065 OS << "Getter&Setter";
2066 else if (Node->isMessagingGetter())
2067 OS << "Getter";
2068 else if (Node->isMessagingSetter())
2069 OS << "Setter";
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002070}
2071
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002072void ASTDumper::VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002073 VisitExpr(Node);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002074 if (Node->isArraySubscriptRefExpr())
2075 OS << " Kind=ArraySubscript GetterForArray=\"";
2076 else
2077 OS << " Kind=DictionarySubscript GetterForDictionary=\"";
2078 if (Node->getAtIndexMethodDecl())
Aaron Ballmanb190f972014-01-03 17:59:55 +00002079 Node->getAtIndexMethodDecl()->getSelector().print(OS);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002080 else
2081 OS << "(null)";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00002082
Ted Kremeneke65b0862012-03-06 20:05:56 +00002083 if (Node->isArraySubscriptRefExpr())
2084 OS << "\" SetterForArray=\"";
2085 else
2086 OS << "\" SetterForDictionary=\"";
2087 if (Node->setAtIndexMethodDecl())
Aaron Ballmanb190f972014-01-03 17:59:55 +00002088 Node->setAtIndexMethodDecl()->getSelector().print(OS);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002089 else
2090 OS << "(null)";
2091}
2092
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002093void ASTDumper::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002094 VisitExpr(Node);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002095 OS << " " << (Node->getValue() ? "__objc_yes" : "__objc_no");
2096}
2097
Chris Lattnercbe4f772007-08-08 22:51:59 +00002098//===----------------------------------------------------------------------===//
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002099// Comments
2100//===----------------------------------------------------------------------===//
2101
2102const char *ASTDumper::getCommandName(unsigned CommandID) {
2103 if (Traits)
2104 return Traits->getCommandInfo(CommandID)->Name;
2105 const CommandInfo *Info = CommandTraits::getBuiltinCommandInfo(CommandID);
2106 if (Info)
2107 return Info->Name;
2108 return "<not a builtin command>";
2109}
2110
2111void ASTDumper::dumpFullComment(const FullComment *C) {
2112 if (!C)
2113 return;
2114
2115 FC = C;
2116 dumpComment(C);
Craig Topper36250ad2014-05-12 05:36:57 +00002117 FC = nullptr;
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002118}
2119
2120void ASTDumper::dumpComment(const Comment *C) {
Richard Smithf7514452014-10-30 21:02:37 +00002121 dumpChild([=] {
2122 if (!C) {
2123 ColorScope Color(*this, NullColor);
2124 OS << "<<<NULL>>>";
2125 return;
2126 }
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002127
Richard Smithf7514452014-10-30 21:02:37 +00002128 {
2129 ColorScope Color(*this, CommentColor);
2130 OS << C->getCommentKindName();
2131 }
2132 dumpPointer(C);
2133 dumpSourceRange(C->getSourceRange());
2134 ConstCommentVisitor<ASTDumper>::visit(C);
2135 for (Comment::child_iterator I = C->child_begin(), E = C->child_end();
2136 I != E; ++I)
2137 dumpComment(*I);
2138 });
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002139}
2140
2141void ASTDumper::visitTextComment(const TextComment *C) {
2142 OS << " Text=\"" << C->getText() << "\"";
2143}
2144
2145void ASTDumper::visitInlineCommandComment(const InlineCommandComment *C) {
2146 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
2147 switch (C->getRenderKind()) {
2148 case InlineCommandComment::RenderNormal:
2149 OS << " RenderNormal";
2150 break;
2151 case InlineCommandComment::RenderBold:
2152 OS << " RenderBold";
2153 break;
2154 case InlineCommandComment::RenderMonospaced:
2155 OS << " RenderMonospaced";
2156 break;
2157 case InlineCommandComment::RenderEmphasized:
2158 OS << " RenderEmphasized";
2159 break;
2160 }
2161
2162 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
2163 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
2164}
2165
2166void ASTDumper::visitHTMLStartTagComment(const HTMLStartTagComment *C) {
2167 OS << " Name=\"" << C->getTagName() << "\"";
2168 if (C->getNumAttrs() != 0) {
2169 OS << " Attrs: ";
2170 for (unsigned i = 0, e = C->getNumAttrs(); i != e; ++i) {
2171 const HTMLStartTagComment::Attribute &Attr = C->getAttr(i);
2172 OS << " \"" << Attr.Name << "=\"" << Attr.Value << "\"";
2173 }
2174 }
2175 if (C->isSelfClosing())
2176 OS << " SelfClosing";
2177}
2178
2179void ASTDumper::visitHTMLEndTagComment(const HTMLEndTagComment *C) {
2180 OS << " Name=\"" << C->getTagName() << "\"";
2181}
2182
2183void ASTDumper::visitBlockCommandComment(const BlockCommandComment *C) {
2184 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
2185 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
2186 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
2187}
2188
2189void ASTDumper::visitParamCommandComment(const ParamCommandComment *C) {
2190 OS << " " << ParamCommandComment::getDirectionAsString(C->getDirection());
2191
2192 if (C->isDirectionExplicit())
2193 OS << " explicitly";
2194 else
2195 OS << " implicitly";
2196
2197 if (C->hasParamName()) {
2198 if (C->isParamIndexValid())
2199 OS << " Param=\"" << C->getParamName(FC) << "\"";
2200 else
2201 OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
2202 }
2203
Dmitri Gribenkodbff5c72014-03-19 14:03:47 +00002204 if (C->isParamIndexValid() && !C->isVarArgParam())
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002205 OS << " ParamIndex=" << C->getParamIndex();
2206}
2207
2208void ASTDumper::visitTParamCommandComment(const TParamCommandComment *C) {
2209 if (C->hasParamName()) {
2210 if (C->isPositionValid())
2211 OS << " Param=\"" << C->getParamName(FC) << "\"";
2212 else
2213 OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
2214 }
2215
2216 if (C->isPositionValid()) {
2217 OS << " Position=<";
2218 for (unsigned i = 0, e = C->getDepth(); i != e; ++i) {
2219 OS << C->getIndex(i);
2220 if (i != e - 1)
2221 OS << ", ";
2222 }
2223 OS << ">";
2224 }
2225}
2226
2227void ASTDumper::visitVerbatimBlockComment(const VerbatimBlockComment *C) {
2228 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\""
2229 " CloseName=\"" << C->getCloseName() << "\"";
2230}
2231
2232void ASTDumper::visitVerbatimBlockLineComment(
2233 const VerbatimBlockLineComment *C) {
2234 OS << " Text=\"" << C->getText() << "\"";
2235}
2236
2237void ASTDumper::visitVerbatimLineComment(const VerbatimLineComment *C) {
2238 OS << " Text=\"" << C->getText() << "\"";
2239}
2240
2241//===----------------------------------------------------------------------===//
Richard Smithd5e7ff82014-10-31 01:17:45 +00002242// Type method implementations
2243//===----------------------------------------------------------------------===//
2244
2245void QualType::dump(const char *msg) const {
2246 if (msg)
2247 llvm::errs() << msg << ": ";
2248 dump();
2249}
2250
2251LLVM_DUMP_METHOD void QualType::dump() const {
2252 ASTDumper Dumper(llvm::errs(), nullptr, nullptr);
2253 Dumper.dumpTypeAsChild(*this);
2254}
2255
2256LLVM_DUMP_METHOD void Type::dump() const { QualType(this, 0).dump(); }
2257
2258//===----------------------------------------------------------------------===//
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002259// Decl method implementations
2260//===----------------------------------------------------------------------===//
2261
Alp Tokeref6b0072014-01-04 13:47:14 +00002262LLVM_DUMP_METHOD void Decl::dump() const { dump(llvm::errs()); }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002263
Alp Tokeref6b0072014-01-04 13:47:14 +00002264LLVM_DUMP_METHOD void Decl::dump(raw_ostream &OS) const {
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002265 ASTDumper P(OS, &getASTContext().getCommentCommandTraits(),
2266 &getASTContext().getSourceManager());
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002267 P.dumpDecl(this);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002268}
2269
Alp Tokeref6b0072014-01-04 13:47:14 +00002270LLVM_DUMP_METHOD void Decl::dumpColor() const {
Richard Trieud215b8d2013-01-26 01:31:20 +00002271 ASTDumper P(llvm::errs(), &getASTContext().getCommentCommandTraits(),
2272 &getASTContext().getSourceManager(), /*ShowColors*/true);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002273 P.dumpDecl(this);
Richard Trieud215b8d2013-01-26 01:31:20 +00002274}
Richard Smith33937e72013-06-22 21:49:40 +00002275
Alp Tokeref6b0072014-01-04 13:47:14 +00002276LLVM_DUMP_METHOD void DeclContext::dumpLookups() const {
Richard Smith6ea05822013-06-24 01:45:33 +00002277 dumpLookups(llvm::errs());
2278}
2279
Richard Smith35f986d2014-08-11 22:11:07 +00002280LLVM_DUMP_METHOD void DeclContext::dumpLookups(raw_ostream &OS,
2281 bool DumpDecls) const {
Richard Smith33937e72013-06-22 21:49:40 +00002282 const DeclContext *DC = this;
2283 while (!DC->isTranslationUnit())
2284 DC = DC->getParent();
2285 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
Richard Smith6ea05822013-06-24 01:45:33 +00002286 ASTDumper P(OS, &Ctx.getCommentCommandTraits(), &Ctx.getSourceManager());
Richard Smith35f986d2014-08-11 22:11:07 +00002287 P.dumpLookups(this, DumpDecls);
Richard Smith33937e72013-06-22 21:49:40 +00002288}
2289
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002290//===----------------------------------------------------------------------===//
Chris Lattnercbe4f772007-08-08 22:51:59 +00002291// Stmt method implementations
2292//===----------------------------------------------------------------------===//
2293
Alp Tokeref6b0072014-01-04 13:47:14 +00002294LLVM_DUMP_METHOD void Stmt::dump(SourceManager &SM) const {
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +00002295 dump(llvm::errs(), SM);
2296}
2297
Alp Tokeref6b0072014-01-04 13:47:14 +00002298LLVM_DUMP_METHOD void Stmt::dump(raw_ostream &OS, SourceManager &SM) const {
Craig Topper36250ad2014-05-12 05:36:57 +00002299 ASTDumper P(OS, nullptr, &SM);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002300 P.dumpStmt(this);
Chris Lattner779d5d92007-08-30 00:40:08 +00002301}
2302
Faisal Vali2da8ed92015-03-22 13:35:56 +00002303LLVM_DUMP_METHOD void Stmt::dump(raw_ostream &OS) const {
2304 ASTDumper P(OS, nullptr, nullptr);
2305 P.dumpStmt(this);
2306}
2307
Alp Tokeref6b0072014-01-04 13:47:14 +00002308LLVM_DUMP_METHOD void Stmt::dump() const {
Craig Topper36250ad2014-05-12 05:36:57 +00002309 ASTDumper P(llvm::errs(), nullptr, nullptr);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002310 P.dumpStmt(this);
Chris Lattnercbe4f772007-08-08 22:51:59 +00002311}
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002312
Alp Tokeref6b0072014-01-04 13:47:14 +00002313LLVM_DUMP_METHOD void Stmt::dumpColor() const {
Craig Topper36250ad2014-05-12 05:36:57 +00002314 ASTDumper P(llvm::errs(), nullptr, nullptr, /*ShowColors*/true);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002315 P.dumpStmt(this);
Richard Trieud215b8d2013-01-26 01:31:20 +00002316}
2317
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002318//===----------------------------------------------------------------------===//
2319// Comment method implementations
2320//===----------------------------------------------------------------------===//
2321
Craig Topper36250ad2014-05-12 05:36:57 +00002322LLVM_DUMP_METHOD void Comment::dump() const {
2323 dump(llvm::errs(), nullptr, nullptr);
2324}
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002325
Alp Tokeref6b0072014-01-04 13:47:14 +00002326LLVM_DUMP_METHOD void Comment::dump(const ASTContext &Context) const {
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002327 dump(llvm::errs(), &Context.getCommentCommandTraits(),
2328 &Context.getSourceManager());
2329}
2330
Alexander Kornienko00911f12013-01-15 12:20:21 +00002331void Comment::dump(raw_ostream &OS, const CommandTraits *Traits,
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002332 const SourceManager *SM) const {
2333 const FullComment *FC = dyn_cast<FullComment>(this);
2334 ASTDumper D(OS, Traits, SM);
2335 D.dumpFullComment(FC);
2336}
Richard Trieud215b8d2013-01-26 01:31:20 +00002337
Alp Tokeref6b0072014-01-04 13:47:14 +00002338LLVM_DUMP_METHOD void Comment::dumpColor() const {
Richard Trieud215b8d2013-01-26 01:31:20 +00002339 const FullComment *FC = dyn_cast<FullComment>(this);
Craig Topper36250ad2014-05-12 05:36:57 +00002340 ASTDumper D(llvm::errs(), nullptr, nullptr, /*ShowColors*/true);
Richard Trieud215b8d2013-01-26 01:31:20 +00002341 D.dumpFullComment(FC);
2342}