blob: ce32f5090a7ada27a03734f52528def6efdeee9b [file] [log] [blame]
Alexander Kornienko18ec81b2012-12-13 13:59:55 +00001//===--- ASTDumper.cpp - Dumping implementation for ASTs ------------------===//
Chris Lattnercbe4f772007-08-08 22:51:59 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnercbe4f772007-08-08 22:51:59 +00007//
8//===----------------------------------------------------------------------===//
9//
Alexander Kornienko18ec81b2012-12-13 13:59:55 +000010// This file implements the AST dump methods, which dump out the
Chris Lattnercbe4f772007-08-08 22:51:59 +000011// AST in a form that exposes type details and other fields.
12//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "clang/AST/ASTContext.h"
Alexander Kornienko5bc364e2013-01-07 17:53:08 +000016#include "clang/AST/Attr.h"
Alexander Kornienkoebc17b52013-01-14 14:07:11 +000017#include "clang/AST/CommentVisitor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/AST/DeclCXX.h"
Richard Smith33937e72013-06-22 21:49:40 +000019#include "clang/AST/DeclLookups.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/AST/DeclObjC.h"
Alexander Kornienko90ff6072012-12-20 02:09:13 +000021#include "clang/AST/DeclVisitor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/AST/StmtVisitor.h"
Richard Smithd5e7ff82014-10-31 01:17:45 +000023#include "clang/AST/TypeVisitor.h"
Alexander Kornienko90ff6072012-12-20 02:09:13 +000024#include "clang/Basic/Module.h"
Chris Lattner11e30d32007-08-30 06:17:34 +000025#include "clang/Basic/SourceManager.h"
Daniel Dunbar34a96c82009-12-03 09:13:13 +000026#include "llvm/Support/raw_ostream.h"
Chris Lattnercbe4f772007-08-08 22:51:59 +000027using namespace clang;
Alexander Kornienkoebc17b52013-01-14 14:07:11 +000028using namespace clang::comments;
Chris Lattnercbe4f772007-08-08 22:51:59 +000029
30//===----------------------------------------------------------------------===//
Alexander Kornienko18ec81b2012-12-13 13:59:55 +000031// ASTDumper Visitor
Chris Lattnercbe4f772007-08-08 22:51:59 +000032//===----------------------------------------------------------------------===//
33
34namespace {
Richard Trieud215b8d2013-01-26 01:31:20 +000035 // Colors used for various parts of the AST dump
Richard Trieu532018f2014-03-06 01:09:03 +000036 // Do not use bold yellow for any text. It is hard to read on white screens.
Richard Trieud215b8d2013-01-26 01:31:20 +000037
38 struct TerminalColor {
39 raw_ostream::Colors Color;
40 bool Bold;
41 };
42
Richard Trieu532018f2014-03-06 01:09:03 +000043 // Red - CastColor
44 // Green - TypeColor
45 // Bold Green - DeclKindNameColor, UndeserializedColor
46 // Yellow - AddressColor, LocationColor
47 // Blue - CommentColor, NullColor, IndentColor
48 // Bold Blue - AttrColor
49 // Bold Magenta - StmtColor
50 // Cyan - ValueKindColor, ObjectKindColor
51 // Bold Cyan - ValueColor, DeclNameColor
52
Richard Trieud215b8d2013-01-26 01:31:20 +000053 // Decl kind names (VarDecl, FunctionDecl, etc)
54 static const TerminalColor DeclKindNameColor = { raw_ostream::GREEN, true };
55 // Attr names (CleanupAttr, GuardedByAttr, etc)
56 static const TerminalColor AttrColor = { raw_ostream::BLUE, true };
57 // Statement names (DeclStmt, ImplicitCastExpr, etc)
58 static const TerminalColor StmtColor = { raw_ostream::MAGENTA, true };
59 // Comment names (FullComment, ParagraphComment, TextComment, etc)
Richard Trieu532018f2014-03-06 01:09:03 +000060 static const TerminalColor CommentColor = { raw_ostream::BLUE, false };
Richard Trieud215b8d2013-01-26 01:31:20 +000061
62 // Type names (int, float, etc, plus user defined types)
63 static const TerminalColor TypeColor = { raw_ostream::GREEN, false };
64
65 // Pointer address
66 static const TerminalColor AddressColor = { raw_ostream::YELLOW, false };
67 // Source locations
68 static const TerminalColor LocationColor = { raw_ostream::YELLOW, false };
69
70 // lvalue/xvalue
71 static const TerminalColor ValueKindColor = { raw_ostream::CYAN, false };
72 // bitfield/objcproperty/objcsubscript/vectorcomponent
73 static const TerminalColor ObjectKindColor = { raw_ostream::CYAN, false };
74
75 // Null statements
76 static const TerminalColor NullColor = { raw_ostream::BLUE, false };
77
Richard Smith1d209d02013-05-23 01:49:11 +000078 // Undeserialized entities
79 static const TerminalColor UndeserializedColor = { raw_ostream::GREEN, true };
80
Richard Trieud215b8d2013-01-26 01:31:20 +000081 // CastKind from CastExpr's
82 static const TerminalColor CastColor = { raw_ostream::RED, false };
83
84 // Value of the statement
85 static const TerminalColor ValueColor = { raw_ostream::CYAN, true };
86 // Decl names
87 static const TerminalColor DeclNameColor = { raw_ostream::CYAN, true };
88
Richard Trieude5cc7d2013-01-31 01:44:26 +000089 // Indents ( `, -. | )
90 static const TerminalColor IndentColor = { raw_ostream::BLUE, false };
91
Alexander Kornienko90ff6072012-12-20 02:09:13 +000092 class ASTDumper
Alexander Kornienko540bacb2013-02-01 12:35:51 +000093 : public ConstDeclVisitor<ASTDumper>, public ConstStmtVisitor<ASTDumper>,
Richard Smithd5e7ff82014-10-31 01:17:45 +000094 public ConstCommentVisitor<ASTDumper>, public TypeVisitor<ASTDumper> {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000095 raw_ostream &OS;
Alexander Kornienkoebc17b52013-01-14 14:07:11 +000096 const CommandTraits *Traits;
97 const SourceManager *SM;
Mike Stump11289f42009-09-09 15:08:12 +000098
Richard Smithf7514452014-10-30 21:02:37 +000099 /// Pending[i] is an action to dump an entity at level i.
100 llvm::SmallVector<std::function<void(bool isLastChild)>, 32> Pending;
Richard Trieude5cc7d2013-01-31 01:44:26 +0000101
Richard Smithf7514452014-10-30 21:02:37 +0000102 /// Indicates whether we're at the top level.
103 bool TopLevel;
Richard Trieude5cc7d2013-01-31 01:44:26 +0000104
Richard Smithf7514452014-10-30 21:02:37 +0000105 /// Indicates if we're handling the first child after entering a new depth.
106 bool FirstChild;
107
108 /// Prefix for currently-being-dumped entity.
109 std::string Prefix;
Richard Trieude5cc7d2013-01-31 01:44:26 +0000110
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000111 /// Keep track of the last location we print out so that we can
112 /// print out deltas from then on out.
Chris Lattner11e30d32007-08-30 06:17:34 +0000113 const char *LastLocFilename;
114 unsigned LastLocLine;
Douglas Gregor7de59662009-05-29 20:38:28 +0000115
Alexander Kornienkoebc17b52013-01-14 14:07:11 +0000116 /// The \c FullComment parent of the comment being dumped.
117 const FullComment *FC;
118
Richard Trieud215b8d2013-01-26 01:31:20 +0000119 bool ShowColors;
120
Richard Smithf7514452014-10-30 21:02:37 +0000121 /// Dump a child of the current node.
122 template<typename Fn> void dumpChild(Fn doDumpChild) {
123 // If we're at the top level, there's nothing interesting to do; just
124 // run the dumper.
125 if (TopLevel) {
126 TopLevel = false;
127 doDumpChild();
128 while (!Pending.empty()) {
129 Pending.back()(true);
130 Pending.pop_back();
131 }
132 Prefix.clear();
133 OS << "\n";
134 TopLevel = true;
135 return;
Manuel Klimek874030e2012-11-07 00:33:12 +0000136 }
Richard Smithf7514452014-10-30 21:02:37 +0000137
138 const FullComment *OrigFC = FC;
139 auto dumpWithIndent = [this, doDumpChild, OrigFC](bool isLastChild) {
140 // Print out the appropriate tree structure and work out the prefix for
141 // children of this node. For instance:
142 //
143 // A Prefix = ""
144 // |-B Prefix = "| "
145 // | `-C Prefix = "| "
146 // `-D Prefix = " "
147 // |-E Prefix = " | "
148 // `-F Prefix = " "
149 // G Prefix = ""
150 //
151 // Note that the first level gets no prefix.
152 {
153 OS << '\n';
154 ColorScope Color(*this, IndentColor);
155 OS << Prefix << (isLastChild ? '`' : '|') << '-';
NAKAMURA Takumic73e4022014-10-31 00:30:37 +0000156 this->Prefix.push_back(isLastChild ? ' ' : '|');
157 this->Prefix.push_back(' ');
Richard Smithf7514452014-10-30 21:02:37 +0000158 }
159
160 FirstChild = true;
161 unsigned Depth = Pending.size();
162
163 FC = OrigFC;
164 doDumpChild();
165
166 // If any children are left, they're the last at their nesting level.
167 // Dump those ones out now.
168 while (Depth < Pending.size()) {
169 Pending.back()(true);
NAKAMURA Takumic73e4022014-10-31 00:30:37 +0000170 this->Pending.pop_back();
Richard Smithf7514452014-10-30 21:02:37 +0000171 }
172
173 // Restore the old prefix.
NAKAMURA Takumic73e4022014-10-31 00:30:37 +0000174 this->Prefix.resize(Prefix.size() - 2);
Richard Smithf7514452014-10-30 21:02:37 +0000175 };
176
177 if (FirstChild) {
178 Pending.push_back(std::move(dumpWithIndent));
179 } else {
180 Pending.back()(false);
181 Pending.back() = std::move(dumpWithIndent);
Manuel Klimek874030e2012-11-07 00:33:12 +0000182 }
Richard Smithf7514452014-10-30 21:02:37 +0000183 FirstChild = false;
184 }
Manuel Klimek874030e2012-11-07 00:33:12 +0000185
Richard Trieud215b8d2013-01-26 01:31:20 +0000186 class ColorScope {
187 ASTDumper &Dumper;
188 public:
189 ColorScope(ASTDumper &Dumper, TerminalColor Color)
190 : Dumper(Dumper) {
191 if (Dumper.ShowColors)
192 Dumper.OS.changeColor(Color.Color, Color.Bold);
193 }
194 ~ColorScope() {
195 if (Dumper.ShowColors)
196 Dumper.OS.resetColor();
197 }
198 };
199
Chris Lattnercbe4f772007-08-08 22:51:59 +0000200 public:
Alexander Kornienkoebc17b52013-01-14 14:07:11 +0000201 ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
202 const SourceManager *SM)
Richard Smithf7514452014-10-30 21:02:37 +0000203 : OS(OS), Traits(Traits), SM(SM), TopLevel(true), FirstChild(true),
Craig Topper36250ad2014-05-12 05:36:57 +0000204 LastLocFilename(""), LastLocLine(~0U), FC(nullptr),
Richard Trieud215b8d2013-01-26 01:31:20 +0000205 ShowColors(SM && SM->getDiagnostics().getShowColors()) { }
206
207 ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
208 const SourceManager *SM, bool ShowColors)
Richard Smithf7514452014-10-30 21:02:37 +0000209 : OS(OS), Traits(Traits), SM(SM), TopLevel(true), FirstChild(true),
Richard Smith56d12152013-01-31 02:04:38 +0000210 LastLocFilename(""), LastLocLine(~0U),
Richard Trieude5cc7d2013-01-31 01:44:26 +0000211 ShowColors(ShowColors) { }
Mike Stump11289f42009-09-09 15:08:12 +0000212
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000213 void dumpDecl(const Decl *D);
214 void dumpStmt(const Stmt *S);
Alexander Kornienkoebc17b52013-01-14 14:07:11 +0000215 void dumpFullComment(const FullComment *C);
Mike Stump11289f42009-09-09 15:08:12 +0000216
Richard Trieude5cc7d2013-01-31 01:44:26 +0000217 // Utilities
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000218 void dumpPointer(const void *Ptr);
219 void dumpSourceRange(SourceRange R);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000220 void dumpLocation(SourceLocation Loc);
Richard Smithd5e7ff82014-10-31 01:17:45 +0000221 void dumpBareType(QualType T, bool Desugar = true);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000222 void dumpType(QualType T);
Richard Smithd5e7ff82014-10-31 01:17:45 +0000223 void dumpTypeAsChild(QualType T);
224 void dumpTypeAsChild(const Type *T);
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000225 void dumpBareDeclRef(const Decl *Node);
Craig Topper36250ad2014-05-12 05:36:57 +0000226 void dumpDeclRef(const Decl *Node, const char *Label = nullptr);
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000227 void dumpName(const NamedDecl *D);
Richard Trieude5cc7d2013-01-31 01:44:26 +0000228 bool hasNodes(const DeclContext *DC);
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000229 void dumpDeclContext(const DeclContext *DC);
Richard Smith35f986d2014-08-11 22:11:07 +0000230 void dumpLookups(const DeclContext *DC, bool DumpDecls);
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000231 void dumpAttr(const Attr *A);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000232
233 // C++ Utilities
234 void dumpAccessSpecifier(AccessSpecifier AS);
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000235 void dumpCXXCtorInitializer(const CXXCtorInitializer *Init);
236 void dumpTemplateParameters(const TemplateParameterList *TPL);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000237 void dumpTemplateArgumentListInfo(const TemplateArgumentListInfo &TALI);
238 void dumpTemplateArgumentLoc(const TemplateArgumentLoc &A);
239 void dumpTemplateArgumentList(const TemplateArgumentList &TAL);
240 void dumpTemplateArgument(const TemplateArgument &A,
241 SourceRange R = SourceRange());
242
Richard Smithd5e7ff82014-10-31 01:17:45 +0000243 // Types
244 void VisitComplexType(const ComplexType *T) {
245 dumpTypeAsChild(T->getElementType());
246 }
247 void VisitPointerType(const PointerType *T) {
248 dumpTypeAsChild(T->getPointeeType());
249 }
250 void VisitBlockPointerType(const BlockPointerType *T) {
251 dumpTypeAsChild(T->getPointeeType());
252 }
253 void VisitReferenceType(const ReferenceType *T) {
254 dumpTypeAsChild(T->getPointeeType());
255 }
256 void VisitRValueReferenceType(const ReferenceType *T) {
257 if (T->isSpelledAsLValue())
258 OS << " written as lvalue reference";
259 VisitReferenceType(T);
260 }
261 void VisitMemberPointerType(const MemberPointerType *T) {
262 dumpTypeAsChild(T->getClass());
263 dumpTypeAsChild(T->getPointeeType());
264 }
265 void VisitArrayType(const ArrayType *T) {
266 switch (T->getSizeModifier()) {
267 case ArrayType::Normal: break;
268 case ArrayType::Static: OS << " static"; break;
269 case ArrayType::Star: OS << " *"; break;
270 }
271 OS << " " << T->getIndexTypeQualifiers().getAsString();
272 dumpTypeAsChild(T->getElementType());
273 }
274 void VisitConstantArrayType(const ConstantArrayType *T) {
275 OS << " " << T->getSize();
276 VisitArrayType(T);
277 }
278 void VisitVariableArrayType(const VariableArrayType *T) {
279 OS << " ";
280 dumpSourceRange(T->getBracketsRange());
281 VisitArrayType(T);
282 dumpStmt(T->getSizeExpr());
283 }
284 void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
285 VisitArrayType(T);
286 OS << " ";
287 dumpSourceRange(T->getBracketsRange());
288 dumpStmt(T->getSizeExpr());
289 }
290 void VisitDependentSizedExtVectorType(
291 const DependentSizedExtVectorType *T) {
292 OS << " ";
293 dumpLocation(T->getAttributeLoc());
294 dumpTypeAsChild(T->getElementType());
295 dumpStmt(T->getSizeExpr());
296 }
297 void VisitVectorType(const VectorType *T) {
298 switch (T->getVectorKind()) {
299 case VectorType::GenericVector: break;
300 case VectorType::AltiVecVector: OS << " altivec"; break;
301 case VectorType::AltiVecPixel: OS << " altivec pixel"; break;
302 case VectorType::AltiVecBool: OS << " altivec bool"; break;
303 case VectorType::NeonVector: OS << " neon"; break;
304 case VectorType::NeonPolyVector: OS << " neon poly"; break;
305 }
306 OS << " " << T->getNumElements();
307 dumpTypeAsChild(T->getElementType());
308 }
309 void VisitFunctionType(const FunctionType *T) {
310 auto EI = T->getExtInfo();
311 if (EI.getNoReturn()) OS << " noreturn";
312 if (EI.getProducesResult()) OS << " produces_result";
313 if (EI.getHasRegParm()) OS << " regparm " << EI.getRegParm();
314 OS << " " << FunctionType::getNameForCallConv(EI.getCC());
315 dumpTypeAsChild(T->getReturnType());
316 }
317 void VisitFunctionProtoType(const FunctionProtoType *T) {
318 auto EPI = T->getExtProtoInfo();
319 if (EPI.HasTrailingReturn) OS << " trailing_return";
320 if (T->isConst()) OS << " const";
321 if (T->isVolatile()) OS << " volatile";
322 if (T->isRestrict()) OS << " restrict";
323 switch (EPI.RefQualifier) {
324 case RQ_None: break;
325 case RQ_LValue: OS << " &"; break;
326 case RQ_RValue: OS << " &&"; break;
327 }
328 // FIXME: Exception specification.
329 // FIXME: Consumed parameters.
330 VisitFunctionType(T);
331 for (QualType PT : T->getParamTypes())
332 dumpTypeAsChild(PT);
333 if (EPI.Variadic)
334 dumpChild([=] { OS << "..."; });
335 }
336 void VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
337 dumpDeclRef(T->getDecl());
338 }
339 void VisitTypedefType(const TypedefType *T) {
340 dumpDeclRef(T->getDecl());
341 }
342 void VisitTypeOfExprType(const TypeOfExprType *T) {
343 dumpStmt(T->getUnderlyingExpr());
344 }
345 void VisitDecltypeType(const DecltypeType *T) {
346 dumpStmt(T->getUnderlyingExpr());
347 }
348 void VisitUnaryTransformType(const UnaryTransformType *T) {
349 switch (T->getUTTKind()) {
350 case UnaryTransformType::EnumUnderlyingType:
351 OS << " underlying_type";
352 break;
353 }
354 dumpTypeAsChild(T->getBaseType());
355 }
356 void VisitTagType(const TagType *T) {
357 dumpDeclRef(T->getDecl());
358 }
359 void VisitAttributedType(const AttributedType *T) {
360 // FIXME: AttrKind
361 dumpTypeAsChild(T->getModifiedType());
362 }
363 void VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
364 OS << " depth " << T->getDepth() << " index " << T->getIndex();
365 if (T->isParameterPack()) OS << " pack";
366 dumpDeclRef(T->getDecl());
367 }
368 void VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) {
369 dumpTypeAsChild(T->getReplacedParameter());
370 }
371 void VisitSubstTemplateTypeParmPackType(
372 const SubstTemplateTypeParmPackType *T) {
373 dumpTypeAsChild(T->getReplacedParameter());
374 dumpTemplateArgument(T->getArgumentPack());
375 }
376 void VisitAutoType(const AutoType *T) {
377 if (T->isDecltypeAuto()) OS << " decltype(auto)";
378 if (!T->isDeduced())
379 OS << " undeduced";
380 }
381 void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
382 if (T->isTypeAlias()) OS << " alias";
383 OS << " "; T->getTemplateName().dump(OS);
384 for (auto &Arg : *T)
385 dumpTemplateArgument(Arg);
386 if (T->isTypeAlias())
387 dumpTypeAsChild(T->getAliasedType());
388 }
389 void VisitInjectedClassNameType(const InjectedClassNameType *T) {
390 dumpDeclRef(T->getDecl());
391 }
392 void VisitObjCInterfaceType(const ObjCInterfaceType *T) {
393 dumpDeclRef(T->getDecl());
394 }
395 void VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
396 dumpTypeAsChild(T->getPointeeType());
397 }
398 void VisitAtomicType(const AtomicType *T) {
399 dumpTypeAsChild(T->getValueType());
400 }
401 void VisitAdjustedType(const AdjustedType *T) {
402 dumpTypeAsChild(T->getOriginalType());
403 }
404 void VisitPackExpansionType(const PackExpansionType *T) {
405 if (auto N = T->getNumExpansions()) OS << " expansions " << *N;
406 if (!T->isSugared())
407 dumpTypeAsChild(T->getPattern());
408 }
409 // FIXME: ElaboratedType, DependentNameType,
410 // DependentTemplateSpecializationType, ObjCObjectType
411
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000412 // Decls
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000413 void VisitLabelDecl(const LabelDecl *D);
414 void VisitTypedefDecl(const TypedefDecl *D);
415 void VisitEnumDecl(const EnumDecl *D);
416 void VisitRecordDecl(const RecordDecl *D);
417 void VisitEnumConstantDecl(const EnumConstantDecl *D);
418 void VisitIndirectFieldDecl(const IndirectFieldDecl *D);
419 void VisitFunctionDecl(const FunctionDecl *D);
420 void VisitFieldDecl(const FieldDecl *D);
421 void VisitVarDecl(const VarDecl *D);
422 void VisitFileScopeAsmDecl(const FileScopeAsmDecl *D);
423 void VisitImportDecl(const ImportDecl *D);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000424
425 // C++ Decls
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000426 void VisitNamespaceDecl(const NamespaceDecl *D);
427 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D);
428 void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D);
429 void VisitTypeAliasDecl(const TypeAliasDecl *D);
430 void VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D);
431 void VisitCXXRecordDecl(const CXXRecordDecl *D);
432 void VisitStaticAssertDecl(const StaticAssertDecl *D);
Richard Smithcbdf7332014-03-18 02:07:28 +0000433 template<typename SpecializationDecl>
Richard Smithf7514452014-10-30 21:02:37 +0000434 void VisitTemplateDeclSpecialization(const SpecializationDecl *D,
Richard Smithcbdf7332014-03-18 02:07:28 +0000435 bool DumpExplicitInst,
436 bool DumpRefOnly);
Richard Smith20ade552014-03-17 23:34:53 +0000437 template<typename TemplateDecl>
438 void VisitTemplateDecl(const TemplateDecl *D, bool DumpExplicitInst);
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000439 void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D);
440 void VisitClassTemplateDecl(const ClassTemplateDecl *D);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000441 void VisitClassTemplateSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000442 const ClassTemplateSpecializationDecl *D);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000443 void VisitClassTemplatePartialSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000444 const ClassTemplatePartialSpecializationDecl *D);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000445 void VisitClassScopeFunctionSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000446 const ClassScopeFunctionSpecializationDecl *D);
Richard Smithd25789a2013-09-18 01:36:02 +0000447 void VisitVarTemplateDecl(const VarTemplateDecl *D);
448 void VisitVarTemplateSpecializationDecl(
449 const VarTemplateSpecializationDecl *D);
450 void VisitVarTemplatePartialSpecializationDecl(
451 const VarTemplatePartialSpecializationDecl *D);
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000452 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D);
453 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D);
454 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D);
455 void VisitUsingDecl(const UsingDecl *D);
456 void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D);
457 void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D);
458 void VisitUsingShadowDecl(const UsingShadowDecl *D);
459 void VisitLinkageSpecDecl(const LinkageSpecDecl *D);
460 void VisitAccessSpecDecl(const AccessSpecDecl *D);
461 void VisitFriendDecl(const FriendDecl *D);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000462
463 // ObjC Decls
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000464 void VisitObjCIvarDecl(const ObjCIvarDecl *D);
465 void VisitObjCMethodDecl(const ObjCMethodDecl *D);
466 void VisitObjCCategoryDecl(const ObjCCategoryDecl *D);
467 void VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D);
468 void VisitObjCProtocolDecl(const ObjCProtocolDecl *D);
469 void VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D);
470 void VisitObjCImplementationDecl(const ObjCImplementationDecl *D);
471 void VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D);
472 void VisitObjCPropertyDecl(const ObjCPropertyDecl *D);
473 void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D);
474 void VisitBlockDecl(const BlockDecl *D);
Mike Stump11289f42009-09-09 15:08:12 +0000475
Chris Lattner84ca3762007-08-30 01:00:35 +0000476 // Stmts.
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000477 void VisitStmt(const Stmt *Node);
478 void VisitDeclStmt(const DeclStmt *Node);
479 void VisitAttributedStmt(const AttributedStmt *Node);
480 void VisitLabelStmt(const LabelStmt *Node);
481 void VisitGotoStmt(const GotoStmt *Node);
Pavel Labath1ef83422013-09-04 14:35:00 +0000482 void VisitCXXCatchStmt(const CXXCatchStmt *Node);
Mike Stump11289f42009-09-09 15:08:12 +0000483
Chris Lattner84ca3762007-08-30 01:00:35 +0000484 // Exprs
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000485 void VisitExpr(const Expr *Node);
486 void VisitCastExpr(const CastExpr *Node);
487 void VisitDeclRefExpr(const DeclRefExpr *Node);
488 void VisitPredefinedExpr(const PredefinedExpr *Node);
489 void VisitCharacterLiteral(const CharacterLiteral *Node);
490 void VisitIntegerLiteral(const IntegerLiteral *Node);
491 void VisitFloatingLiteral(const FloatingLiteral *Node);
492 void VisitStringLiteral(const StringLiteral *Str);
Richard Smithf0514962014-06-03 08:24:28 +0000493 void VisitInitListExpr(const InitListExpr *ILE);
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000494 void VisitUnaryOperator(const UnaryOperator *Node);
495 void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Node);
496 void VisitMemberExpr(const MemberExpr *Node);
497 void VisitExtVectorElementExpr(const ExtVectorElementExpr *Node);
498 void VisitBinaryOperator(const BinaryOperator *Node);
499 void VisitCompoundAssignOperator(const CompoundAssignOperator *Node);
500 void VisitAddrLabelExpr(const AddrLabelExpr *Node);
501 void VisitBlockExpr(const BlockExpr *Node);
502 void VisitOpaqueValueExpr(const OpaqueValueExpr *Node);
Chris Lattner84ca3762007-08-30 01:00:35 +0000503
504 // C++
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000505 void VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node);
506 void VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node);
507 void VisitCXXThisExpr(const CXXThisExpr *Node);
508 void VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node);
509 void VisitCXXConstructExpr(const CXXConstructExpr *Node);
510 void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node);
Richard Smithe6c01442013-06-05 00:46:14 +0000511 void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node);
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000512 void VisitExprWithCleanups(const ExprWithCleanups *Node);
513 void VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node);
514 void dumpCXXTemporary(const CXXTemporary *Temporary);
Faisal Vali2b391ab2013-09-26 19:54:12 +0000515 void VisitLambdaExpr(const LambdaExpr *Node) {
516 VisitExpr(Node);
517 dumpDecl(Node->getLambdaClass());
518 }
Mike Stump11289f42009-09-09 15:08:12 +0000519
Chris Lattner84ca3762007-08-30 01:00:35 +0000520 // ObjC
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000521 void VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node);
522 void VisitObjCEncodeExpr(const ObjCEncodeExpr *Node);
523 void VisitObjCMessageExpr(const ObjCMessageExpr *Node);
524 void VisitObjCBoxedExpr(const ObjCBoxedExpr *Node);
525 void VisitObjCSelectorExpr(const ObjCSelectorExpr *Node);
526 void VisitObjCProtocolExpr(const ObjCProtocolExpr *Node);
527 void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node);
528 void VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node);
529 void VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node);
530 void VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node);
Alexander Kornienkoebc17b52013-01-14 14:07:11 +0000531
532 // Comments.
533 const char *getCommandName(unsigned CommandID);
534 void dumpComment(const Comment *C);
535
536 // Inline comments.
537 void visitTextComment(const TextComment *C);
538 void visitInlineCommandComment(const InlineCommandComment *C);
539 void visitHTMLStartTagComment(const HTMLStartTagComment *C);
540 void visitHTMLEndTagComment(const HTMLEndTagComment *C);
541
542 // Block comments.
543 void visitBlockCommandComment(const BlockCommandComment *C);
544 void visitParamCommandComment(const ParamCommandComment *C);
545 void visitTParamCommandComment(const TParamCommandComment *C);
546 void visitVerbatimBlockComment(const VerbatimBlockComment *C);
547 void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C);
548 void visitVerbatimLineComment(const VerbatimLineComment *C);
Chris Lattnercbe4f772007-08-08 22:51:59 +0000549 };
550}
551
552//===----------------------------------------------------------------------===//
Chris Lattner11e30d32007-08-30 06:17:34 +0000553// Utilities
554//===----------------------------------------------------------------------===//
555
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000556void ASTDumper::dumpPointer(const void *Ptr) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000557 ColorScope Color(*this, AddressColor);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000558 OS << ' ' << Ptr;
559}
560
Alexander Kornienko18ec81b2012-12-13 13:59:55 +0000561void ASTDumper::dumpLocation(SourceLocation Loc) {
Alex McCarthye14ddef2014-05-02 20:24:11 +0000562 if (!SM)
563 return;
564
Richard Trieud215b8d2013-01-26 01:31:20 +0000565 ColorScope Color(*this, LocationColor);
Chris Lattner53e384f2009-01-16 07:00:02 +0000566 SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000567
Chris Lattner11e30d32007-08-30 06:17:34 +0000568 // The general format we print out is filename:line:col, but we drop pieces
569 // that haven't changed since the last loc printed.
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000570 PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
571
Douglas Gregor453b0122010-11-12 07:15:47 +0000572 if (PLoc.isInvalid()) {
573 OS << "<invalid sloc>";
574 return;
575 }
576
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000577 if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000578 OS << PLoc.getFilename() << ':' << PLoc.getLine()
579 << ':' << PLoc.getColumn();
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000580 LastLocFilename = PLoc.getFilename();
581 LastLocLine = PLoc.getLine();
582 } else if (PLoc.getLine() != LastLocLine) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000583 OS << "line" << ':' << PLoc.getLine()
584 << ':' << PLoc.getColumn();
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000585 LastLocLine = PLoc.getLine();
Chris Lattner11e30d32007-08-30 06:17:34 +0000586 } else {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000587 OS << "col" << ':' << PLoc.getColumn();
Chris Lattner11e30d32007-08-30 06:17:34 +0000588 }
589}
590
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000591void ASTDumper::dumpSourceRange(SourceRange R) {
Chris Lattner11e30d32007-08-30 06:17:34 +0000592 // Can't translate locations if a SourceManager isn't available.
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000593 if (!SM)
594 return;
Mike Stump11289f42009-09-09 15:08:12 +0000595
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000596 OS << " <";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000597 dumpLocation(R.getBegin());
Chris Lattnera7c19fe2007-10-16 22:36:42 +0000598 if (R.getBegin() != R.getEnd()) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000599 OS << ", ";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000600 dumpLocation(R.getEnd());
Chris Lattner11e30d32007-08-30 06:17:34 +0000601 }
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000602 OS << ">";
Mike Stump11289f42009-09-09 15:08:12 +0000603
Chris Lattner11e30d32007-08-30 06:17:34 +0000604 // <t2.c:123:421[blah], t2.c:412:321>
605
606}
607
Richard Smithd5e7ff82014-10-31 01:17:45 +0000608void ASTDumper::dumpBareType(QualType T, bool Desugar) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000609 ColorScope Color(*this, TypeColor);
Richard Smithd5e7ff82014-10-31 01:17:45 +0000610
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000611 SplitQualType T_split = T.split();
612 OS << "'" << QualType::getAsString(T_split) << "'";
613
Richard Smithd5e7ff82014-10-31 01:17:45 +0000614 if (Desugar && !T.isNull()) {
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000615 // If the type is sugared, also dump a (shallow) desugared type.
616 SplitQualType D_split = T.getSplitDesugaredType();
617 if (T_split != D_split)
618 OS << ":'" << QualType::getAsString(D_split) << "'";
619 }
620}
621
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000622void ASTDumper::dumpType(QualType T) {
623 OS << ' ';
624 dumpBareType(T);
625}
626
Richard Smithd5e7ff82014-10-31 01:17:45 +0000627void ASTDumper::dumpTypeAsChild(QualType T) {
628 SplitQualType SQT = T.split();
629 if (!SQT.Quals.hasQualifiers())
630 return dumpTypeAsChild(SQT.Ty);
631
632 dumpChild([=] {
633 OS << "QualType";
634 dumpPointer(T.getAsOpaquePtr());
635 OS << " ";
636 dumpBareType(T, false);
637 OS << " " << T.split().Quals.getAsString();
638 dumpTypeAsChild(T.split().Ty);
639 });
640}
641
642void ASTDumper::dumpTypeAsChild(const Type *T) {
643 dumpChild([=] {
644 if (!T) {
645 ColorScope Color(*this, NullColor);
646 OS << "<<<NULL>>>";
647 return;
648 }
649
650 {
651 ColorScope Color(*this, TypeColor);
652 OS << T->getTypeClassName() << "Type";
653 }
654 dumpPointer(T);
655 OS << " ";
656 dumpBareType(QualType(T, 0), false);
657
658 QualType SingleStepDesugar =
659 T->getLocallyUnqualifiedSingleStepDesugaredType();
660 if (SingleStepDesugar != QualType(T, 0))
661 OS << " sugar";
662 if (T->isDependentType())
663 OS << " dependent";
664 else if (T->isInstantiationDependentType())
665 OS << " instantiation_dependent";
666 if (T->isVariablyModifiedType())
667 OS << " variably_modified";
668 if (T->containsUnexpandedParameterPack())
669 OS << " contains_unexpanded_pack";
670 if (T->isFromAST())
671 OS << " imported";
672
673 TypeVisitor<ASTDumper>::Visit(T);
674
675 if (SingleStepDesugar != QualType(T, 0))
676 dumpTypeAsChild(SingleStepDesugar);
677 });
678}
679
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000680void ASTDumper::dumpBareDeclRef(const Decl *D) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000681 {
682 ColorScope Color(*this, DeclKindNameColor);
683 OS << D->getDeclKindName();
684 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000685 dumpPointer(D);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000686
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000687 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000688 ColorScope Color(*this, DeclNameColor);
David Blaikied4da8722013-05-14 21:04:00 +0000689 OS << " '" << ND->getDeclName() << '\'';
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000690 }
691
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000692 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000693 dumpType(VD->getType());
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000694}
695
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000696void ASTDumper::dumpDeclRef(const Decl *D, const char *Label) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000697 if (!D)
698 return;
699
Richard Smithf7514452014-10-30 21:02:37 +0000700 dumpChild([=]{
701 if (Label)
702 OS << Label << ' ';
703 dumpBareDeclRef(D);
704 });
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000705}
706
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000707void ASTDumper::dumpName(const NamedDecl *ND) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000708 if (ND->getDeclName()) {
709 ColorScope Color(*this, DeclNameColor);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000710 OS << ' ' << ND->getNameAsString();
Richard Trieud215b8d2013-01-26 01:31:20 +0000711 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000712}
713
Richard Trieude5cc7d2013-01-31 01:44:26 +0000714bool ASTDumper::hasNodes(const DeclContext *DC) {
715 if (!DC)
716 return false;
717
Richard Smith1d209d02013-05-23 01:49:11 +0000718 return DC->hasExternalLexicalStorage() ||
719 DC->noload_decls_begin() != DC->noload_decls_end();
Richard Trieude5cc7d2013-01-31 01:44:26 +0000720}
721
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000722void ASTDumper::dumpDeclContext(const DeclContext *DC) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000723 if (!DC)
724 return;
Richard Smithdcc2c452014-03-17 23:00:06 +0000725
Richard Smithdcc2c452014-03-17 23:00:06 +0000726 for (auto *D : DC->noload_decls())
Richard Smithf7514452014-10-30 21:02:37 +0000727 dumpDecl(D);
Richard Smithdcc2c452014-03-17 23:00:06 +0000728
729 if (DC->hasExternalLexicalStorage()) {
Richard Smithf7514452014-10-30 21:02:37 +0000730 dumpChild([=]{
731 ColorScope Color(*this, UndeserializedColor);
732 OS << "<undeserialized declarations>";
733 });
Richard Smith1d209d02013-05-23 01:49:11 +0000734 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000735}
736
Richard Smith35f986d2014-08-11 22:11:07 +0000737void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) {
Richard Smithf7514452014-10-30 21:02:37 +0000738 dumpChild([=] {
739 OS << "StoredDeclsMap ";
740 dumpBareDeclRef(cast<Decl>(DC));
Richard Smith33937e72013-06-22 21:49:40 +0000741
Richard Smithf7514452014-10-30 21:02:37 +0000742 const DeclContext *Primary = DC->getPrimaryContext();
743 if (Primary != DC) {
744 OS << " primary";
745 dumpPointer(cast<Decl>(Primary));
Richard Smith33937e72013-06-22 21:49:40 +0000746 }
747
Richard Smithf7514452014-10-30 21:02:37 +0000748 bool HasUndeserializedLookups = Primary->hasExternalVisibleStorage();
Richard Smith35f986d2014-08-11 22:11:07 +0000749
Richard Smithf7514452014-10-30 21:02:37 +0000750 DeclContext::all_lookups_iterator I = Primary->noload_lookups_begin(),
751 E = Primary->noload_lookups_end();
752 while (I != E) {
753 DeclarationName Name = I.getLookupName();
754 DeclContextLookupResult R = *I++;
Richard Smith35f986d2014-08-11 22:11:07 +0000755
Richard Smithf7514452014-10-30 21:02:37 +0000756 dumpChild([=] {
757 OS << "DeclarationName ";
758 {
759 ColorScope Color(*this, DeclNameColor);
760 OS << '\'' << Name << '\'';
761 }
Richard Smith35f986d2014-08-11 22:11:07 +0000762
Richard Smithf7514452014-10-30 21:02:37 +0000763 for (DeclContextLookupResult::iterator RI = R.begin(), RE = R.end();
764 RI != RE; ++RI) {
765 dumpChild([=] {
766 dumpBareDeclRef(*RI);
767
768 if ((*RI)->isHidden())
769 OS << " hidden";
770
771 // If requested, dump the redecl chain for this lookup.
772 if (DumpDecls) {
773 // Dump earliest decl first.
774 std::function<void(Decl *)> DumpWithPrev = [&](Decl *D) {
775 if (Decl *Prev = D->getPreviousDecl())
776 DumpWithPrev(Prev);
777 dumpDecl(D);
778 };
779 DumpWithPrev(*RI);
780 }
781 });
782 }
783 });
Richard Smith33937e72013-06-22 21:49:40 +0000784 }
Richard Smith33937e72013-06-22 21:49:40 +0000785
Richard Smithf7514452014-10-30 21:02:37 +0000786 if (HasUndeserializedLookups) {
787 dumpChild([=] {
788 ColorScope Color(*this, UndeserializedColor);
789 OS << "<undeserialized lookups>";
790 });
791 }
792 });
Richard Smith33937e72013-06-22 21:49:40 +0000793}
794
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000795void ASTDumper::dumpAttr(const Attr *A) {
Richard Smithf7514452014-10-30 21:02:37 +0000796 dumpChild([=] {
797 {
798 ColorScope Color(*this, AttrColor);
Aaron Ballman36a53502014-01-16 13:03:14 +0000799
Richard Smithf7514452014-10-30 21:02:37 +0000800 switch (A->getKind()) {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000801#define ATTR(X) case attr::X: OS << #X; break;
802#include "clang/Basic/AttrList.inc"
Richard Smithf7514452014-10-30 21:02:37 +0000803 default:
804 llvm_unreachable("unexpected attribute kind");
805 }
806 OS << "Attr";
Richard Trieud215b8d2013-01-26 01:31:20 +0000807 }
Richard Smithf7514452014-10-30 21:02:37 +0000808 dumpPointer(A);
809 dumpSourceRange(A->getRange());
810 if (A->isInherited())
811 OS << " Inherited";
812 if (A->isImplicit())
813 OS << " Implicit";
Hans Wennborgb0a8b4a2014-05-31 04:05:57 +0000814#include "clang/AST/AttrDump.inc"
Richard Smithf7514452014-10-30 21:02:37 +0000815 });
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000816}
817
Richard Smith71bec062013-10-15 21:58:30 +0000818static void dumpPreviousDeclImpl(raw_ostream &OS, ...) {}
819
820template<typename T>
821static void dumpPreviousDeclImpl(raw_ostream &OS, const Mergeable<T> *D) {
Rafael Espindola8db352d2013-10-17 15:37:26 +0000822 const T *First = D->getFirstDecl();
Richard Smith71bec062013-10-15 21:58:30 +0000823 if (First != D)
824 OS << " first " << First;
Richard Smithf5f43542013-02-07 01:35:44 +0000825}
826
827template<typename T>
Richard Smith71bec062013-10-15 21:58:30 +0000828static void dumpPreviousDeclImpl(raw_ostream &OS, const Redeclarable<T> *D) {
829 const T *Prev = D->getPreviousDecl();
830 if (Prev)
831 OS << " prev " << Prev;
Richard Smithf5f43542013-02-07 01:35:44 +0000832}
833
Richard Smith71bec062013-10-15 21:58:30 +0000834/// Dump the previous declaration in the redeclaration chain for a declaration,
835/// if any.
836static void dumpPreviousDecl(raw_ostream &OS, const Decl *D) {
Richard Smithf5f43542013-02-07 01:35:44 +0000837 switch (D->getKind()) {
838#define DECL(DERIVED, BASE) \
839 case Decl::DERIVED: \
Richard Smith71bec062013-10-15 21:58:30 +0000840 return dumpPreviousDeclImpl(OS, cast<DERIVED##Decl>(D));
Richard Smithf5f43542013-02-07 01:35:44 +0000841#define ABSTRACT_DECL(DECL)
842#include "clang/AST/DeclNodes.inc"
843 }
844 llvm_unreachable("Decl that isn't part of DeclNodes.inc!");
845}
846
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000847//===----------------------------------------------------------------------===//
848// C++ Utilities
849//===----------------------------------------------------------------------===//
850
851void ASTDumper::dumpAccessSpecifier(AccessSpecifier AS) {
852 switch (AS) {
853 case AS_none:
854 break;
855 case AS_public:
856 OS << "public";
857 break;
858 case AS_protected:
859 OS << "protected";
860 break;
861 case AS_private:
862 OS << "private";
863 break;
864 }
865}
866
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000867void ASTDumper::dumpCXXCtorInitializer(const CXXCtorInitializer *Init) {
Richard Smithf7514452014-10-30 21:02:37 +0000868 dumpChild([=] {
869 OS << "CXXCtorInitializer";
870 if (Init->isAnyMemberInitializer()) {
871 OS << ' ';
872 dumpBareDeclRef(Init->getAnyMember());
873 } else if (Init->isBaseInitializer()) {
874 dumpType(QualType(Init->getBaseClass(), 0));
875 } else if (Init->isDelegatingInitializer()) {
876 dumpType(Init->getTypeSourceInfo()->getType());
877 } else {
878 llvm_unreachable("Unknown initializer type");
879 }
880 dumpStmt(Init->getInit());
881 });
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000882}
883
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000884void ASTDumper::dumpTemplateParameters(const TemplateParameterList *TPL) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000885 if (!TPL)
886 return;
887
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000888 for (TemplateParameterList::const_iterator I = TPL->begin(), E = TPL->end();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000889 I != E; ++I)
890 dumpDecl(*I);
891}
892
893void ASTDumper::dumpTemplateArgumentListInfo(
894 const TemplateArgumentListInfo &TALI) {
Richard Smithf7514452014-10-30 21:02:37 +0000895 for (unsigned i = 0, e = TALI.size(); i < e; ++i)
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000896 dumpTemplateArgumentLoc(TALI[i]);
897}
898
899void ASTDumper::dumpTemplateArgumentLoc(const TemplateArgumentLoc &A) {
900 dumpTemplateArgument(A.getArgument(), A.getSourceRange());
901}
902
903void ASTDumper::dumpTemplateArgumentList(const TemplateArgumentList &TAL) {
904 for (unsigned i = 0, e = TAL.size(); i < e; ++i)
905 dumpTemplateArgument(TAL[i]);
906}
907
908void ASTDumper::dumpTemplateArgument(const TemplateArgument &A, SourceRange R) {
Richard Smithf7514452014-10-30 21:02:37 +0000909 dumpChild([=] {
910 OS << "TemplateArgument";
911 if (R.isValid())
912 dumpSourceRange(R);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000913
Richard Smithf7514452014-10-30 21:02:37 +0000914 switch (A.getKind()) {
915 case TemplateArgument::Null:
916 OS << " null";
917 break;
918 case TemplateArgument::Type:
919 OS << " type";
920 dumpType(A.getAsType());
921 break;
922 case TemplateArgument::Declaration:
923 OS << " decl";
924 dumpDeclRef(A.getAsDecl());
925 break;
926 case TemplateArgument::NullPtr:
927 OS << " nullptr";
928 break;
929 case TemplateArgument::Integral:
930 OS << " integral " << A.getAsIntegral();
931 break;
932 case TemplateArgument::Template:
933 OS << " template ";
934 A.getAsTemplate().dump(OS);
935 break;
936 case TemplateArgument::TemplateExpansion:
937 OS << " template expansion";
938 A.getAsTemplateOrTemplatePattern().dump(OS);
939 break;
940 case TemplateArgument::Expression:
941 OS << " expr";
942 dumpStmt(A.getAsExpr());
943 break;
944 case TemplateArgument::Pack:
945 OS << " pack";
946 for (TemplateArgument::pack_iterator I = A.pack_begin(), E = A.pack_end();
947 I != E; ++I)
948 dumpTemplateArgument(*I);
949 break;
Richard Trieude5cc7d2013-01-31 01:44:26 +0000950 }
Richard Smithf7514452014-10-30 21:02:37 +0000951 });
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000952}
953
Chris Lattner11e30d32007-08-30 06:17:34 +0000954//===----------------------------------------------------------------------===//
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000955// Decl dumping methods.
Chris Lattnercbe4f772007-08-08 22:51:59 +0000956//===----------------------------------------------------------------------===//
957
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000958void ASTDumper::dumpDecl(const Decl *D) {
Richard Smithf7514452014-10-30 21:02:37 +0000959 dumpChild([=] {
960 if (!D) {
961 ColorScope Color(*this, NullColor);
962 OS << "<<<NULL>>>";
963 return;
964 }
Mike Stump11289f42009-09-09 15:08:12 +0000965
Richard Smithf7514452014-10-30 21:02:37 +0000966 {
967 ColorScope Color(*this, DeclKindNameColor);
968 OS << D->getDeclKindName() << "Decl";
969 }
970 dumpPointer(D);
971 if (D->getLexicalDeclContext() != D->getDeclContext())
972 OS << " parent " << cast<Decl>(D->getDeclContext());
973 dumpPreviousDecl(OS, D);
974 dumpSourceRange(D->getSourceRange());
975 OS << ' ';
976 dumpLocation(D->getLocation());
977 if (Module *M = D->getOwningModule())
978 OS << " in " << M->getFullModuleName();
979 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
980 if (ND->isHidden())
981 OS << " hidden";
982 if (D->isImplicit())
983 OS << " implicit";
984 if (D->isUsed())
985 OS << " used";
986 else if (D->isThisDeclarationReferenced())
987 OS << " referenced";
988 if (D->isInvalidDecl())
989 OS << " invalid";
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000990
Richard Smithf7514452014-10-30 21:02:37 +0000991 ConstDeclVisitor<ASTDumper>::Visit(D);
Richard Trieude5cc7d2013-01-31 01:44:26 +0000992
Richard Smithf7514452014-10-30 21:02:37 +0000993 for (Decl::attr_iterator I = D->attr_begin(), E = D->attr_end(); I != E;
994 ++I)
995 dumpAttr(*I);
Richard Trieude5cc7d2013-01-31 01:44:26 +0000996
Richard Smithf7514452014-10-30 21:02:37 +0000997 if (const FullComment *Comment =
998 D->getASTContext().getLocalCommentForDeclUncached(D))
999 dumpFullComment(Comment);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001000
Richard Smithf7514452014-10-30 21:02:37 +00001001 // Decls within functions are visited by the body.
1002 if (!isa<FunctionDecl>(*D) && !isa<ObjCMethodDecl>(*D) &&
1003 hasNodes(dyn_cast<DeclContext>(D)))
1004 dumpDeclContext(cast<DeclContext>(D));
1005 });
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001006}
1007
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001008void ASTDumper::VisitLabelDecl(const LabelDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001009 dumpName(D);
1010}
1011
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001012void ASTDumper::VisitTypedefDecl(const TypedefDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001013 dumpName(D);
1014 dumpType(D->getUnderlyingType());
1015 if (D->isModulePrivate())
1016 OS << " __module_private__";
1017}
1018
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001019void ASTDumper::VisitEnumDecl(const EnumDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001020 if (D->isScoped()) {
1021 if (D->isScopedUsingClassTag())
1022 OS << " class";
1023 else
1024 OS << " struct";
1025 }
1026 dumpName(D);
1027 if (D->isModulePrivate())
1028 OS << " __module_private__";
1029 if (D->isFixed())
1030 dumpType(D->getIntegerType());
1031}
1032
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001033void ASTDumper::VisitRecordDecl(const RecordDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001034 OS << ' ' << D->getKindName();
1035 dumpName(D);
1036 if (D->isModulePrivate())
1037 OS << " __module_private__";
Richard Smith99bc1b92013-08-30 05:32:29 +00001038 if (D->isCompleteDefinition())
1039 OS << " definition";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001040}
1041
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001042void ASTDumper::VisitEnumConstantDecl(const EnumConstantDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001043 dumpName(D);
1044 dumpType(D->getType());
Richard Smithf7514452014-10-30 21:02:37 +00001045 if (const Expr *Init = D->getInitExpr())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001046 dumpStmt(Init);
1047}
1048
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001049void ASTDumper::VisitIndirectFieldDecl(const IndirectFieldDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001050 dumpName(D);
1051 dumpType(D->getType());
Richard Smithdcc2c452014-03-17 23:00:06 +00001052
Richard Smith8aa49222014-03-18 00:35:12 +00001053 for (auto *Child : D->chain())
Richard Smithf7514452014-10-30 21:02:37 +00001054 dumpDeclRef(Child);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001055}
1056
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001057void ASTDumper::VisitFunctionDecl(const FunctionDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001058 dumpName(D);
1059 dumpType(D->getType());
1060
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001061 StorageClass SC = D->getStorageClass();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001062 if (SC != SC_None)
1063 OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
1064 if (D->isInlineSpecified())
1065 OS << " inline";
1066 if (D->isVirtualAsWritten())
1067 OS << " virtual";
1068 if (D->isModulePrivate())
1069 OS << " __module_private__";
1070
1071 if (D->isPure())
1072 OS << " pure";
1073 else if (D->isDeletedAsWritten())
1074 OS << " delete";
1075
Richard Smithadaa0152013-05-17 02:09:46 +00001076 if (const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>()) {
1077 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
Richard Smith8acb4282014-07-31 21:57:55 +00001078 switch (EPI.ExceptionSpec.Type) {
Richard Smithadaa0152013-05-17 02:09:46 +00001079 default: break;
1080 case EST_Unevaluated:
Richard Smith8acb4282014-07-31 21:57:55 +00001081 OS << " noexcept-unevaluated " << EPI.ExceptionSpec.SourceDecl;
Richard Smithadaa0152013-05-17 02:09:46 +00001082 break;
1083 case EST_Uninstantiated:
Richard Smith8acb4282014-07-31 21:57:55 +00001084 OS << " noexcept-uninstantiated " << EPI.ExceptionSpec.SourceTemplate;
Richard Smithadaa0152013-05-17 02:09:46 +00001085 break;
1086 }
1087 }
1088
Richard Smithf7514452014-10-30 21:02:37 +00001089 if (const FunctionTemplateSpecializationInfo *FTSI =
1090 D->getTemplateSpecializationInfo())
Richard Trieude5cc7d2013-01-31 01:44:26 +00001091 dumpTemplateArgumentList(*FTSI->TemplateArguments);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001092
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001093 for (ArrayRef<NamedDecl *>::iterator
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001094 I = D->getDeclsInPrototypeScope().begin(),
Richard Smithf7514452014-10-30 21:02:37 +00001095 E = D->getDeclsInPrototypeScope().end(); I != E; ++I)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001096 dumpDecl(*I);
1097
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001098 for (FunctionDecl::param_const_iterator I = D->param_begin(),
1099 E = D->param_end();
Richard Smithf7514452014-10-30 21:02:37 +00001100 I != E; ++I)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001101 dumpDecl(*I);
Richard Smithf7514452014-10-30 21:02:37 +00001102
1103 if (const CXXConstructorDecl *C = dyn_cast<CXXConstructorDecl>(D))
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001104 for (CXXConstructorDecl::init_const_iterator I = C->init_begin(),
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001105 E = C->init_end();
Richard Smithf7514452014-10-30 21:02:37 +00001106 I != E; ++I)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001107 dumpCXXCtorInitializer(*I);
1108
Richard Smithf7514452014-10-30 21:02:37 +00001109 if (D->doesThisDeclarationHaveABody())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001110 dumpStmt(D->getBody());
1111}
1112
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001113void ASTDumper::VisitFieldDecl(const FieldDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001114 dumpName(D);
1115 dumpType(D->getType());
1116 if (D->isMutable())
1117 OS << " mutable";
1118 if (D->isModulePrivate())
1119 OS << " __module_private__";
Richard Trieude5cc7d2013-01-31 01:44:26 +00001120
Richard Smithf7514452014-10-30 21:02:37 +00001121 if (D->isBitField())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001122 dumpStmt(D->getBitWidth());
Richard Smithf7514452014-10-30 21:02:37 +00001123 if (Expr *Init = D->getInClassInitializer())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001124 dumpStmt(Init);
1125}
1126
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001127void ASTDumper::VisitVarDecl(const VarDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001128 dumpName(D);
1129 dumpType(D->getType());
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001130 StorageClass SC = D->getStorageClass();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001131 if (SC != SC_None)
1132 OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
Richard Smithfd3834f2013-04-13 02:43:54 +00001133 switch (D->getTLSKind()) {
1134 case VarDecl::TLS_None: break;
1135 case VarDecl::TLS_Static: OS << " tls"; break;
1136 case VarDecl::TLS_Dynamic: OS << " tls_dynamic"; break;
1137 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001138 if (D->isModulePrivate())
1139 OS << " __module_private__";
1140 if (D->isNRVOVariable())
1141 OS << " nrvo";
Richard Trieude5cc7d2013-01-31 01:44:26 +00001142 if (D->hasInit()) {
Richard Smithd9967cc2014-07-10 22:54:03 +00001143 switch (D->getInitStyle()) {
1144 case VarDecl::CInit: OS << " cinit"; break;
1145 case VarDecl::CallInit: OS << " callinit"; break;
1146 case VarDecl::ListInit: OS << " listinit"; break;
1147 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001148 dumpStmt(D->getInit());
Richard Trieude5cc7d2013-01-31 01:44:26 +00001149 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001150}
1151
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001152void ASTDumper::VisitFileScopeAsmDecl(const FileScopeAsmDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001153 dumpStmt(D->getAsmString());
1154}
1155
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001156void ASTDumper::VisitImportDecl(const ImportDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001157 OS << ' ' << D->getImportedModule()->getFullModuleName();
1158}
1159
1160//===----------------------------------------------------------------------===//
1161// C++ Declarations
1162//===----------------------------------------------------------------------===//
1163
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001164void ASTDumper::VisitNamespaceDecl(const NamespaceDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001165 dumpName(D);
1166 if (D->isInline())
1167 OS << " inline";
1168 if (!D->isOriginalNamespace())
1169 dumpDeclRef(D->getOriginalNamespace(), "original");
1170}
1171
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001172void ASTDumper::VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001173 OS << ' ';
1174 dumpBareDeclRef(D->getNominatedNamespace());
1175}
1176
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001177void ASTDumper::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001178 dumpName(D);
1179 dumpDeclRef(D->getAliasedNamespace());
1180}
1181
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001182void ASTDumper::VisitTypeAliasDecl(const TypeAliasDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001183 dumpName(D);
1184 dumpType(D->getUnderlyingType());
1185}
1186
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001187void ASTDumper::VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001188 dumpName(D);
1189 dumpTemplateParameters(D->getTemplateParameters());
1190 dumpDecl(D->getTemplatedDecl());
1191}
1192
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001193void ASTDumper::VisitCXXRecordDecl(const CXXRecordDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001194 VisitRecordDecl(D);
1195 if (!D->isCompleteDefinition())
1196 return;
1197
Aaron Ballman574705e2014-03-13 15:41:46 +00001198 for (const auto &I : D->bases()) {
Richard Smithf7514452014-10-30 21:02:37 +00001199 dumpChild([=] {
1200 if (I.isVirtual())
1201 OS << "virtual ";
1202 dumpAccessSpecifier(I.getAccessSpecifier());
1203 dumpType(I.getType());
1204 if (I.isPackExpansion())
1205 OS << "...";
1206 });
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001207 }
1208}
1209
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001210void ASTDumper::VisitStaticAssertDecl(const StaticAssertDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001211 dumpStmt(D->getAssertExpr());
1212 dumpStmt(D->getMessage());
1213}
1214
Richard Smithcbdf7332014-03-18 02:07:28 +00001215template<typename SpecializationDecl>
Richard Smithf7514452014-10-30 21:02:37 +00001216void ASTDumper::VisitTemplateDeclSpecialization(const SpecializationDecl *D,
Richard Smithcbdf7332014-03-18 02:07:28 +00001217 bool DumpExplicitInst,
1218 bool DumpRefOnly) {
1219 bool DumpedAny = false;
1220 for (auto *RedeclWithBadType : D->redecls()) {
1221 // FIXME: The redecls() range sometimes has elements of a less-specific
1222 // type. (In particular, ClassTemplateSpecializationDecl::redecls() gives
1223 // us TagDecls, and should give CXXRecordDecls).
1224 auto *Redecl = dyn_cast<SpecializationDecl>(RedeclWithBadType);
1225 if (!Redecl) {
1226 // Found the injected-class-name for a class template. This will be dumped
1227 // as part of its surrounding class so we don't need to dump it here.
1228 assert(isa<CXXRecordDecl>(RedeclWithBadType) &&
1229 "expected an injected-class-name");
1230 continue;
1231 }
1232
1233 switch (Redecl->getTemplateSpecializationKind()) {
1234 case TSK_ExplicitInstantiationDeclaration:
1235 case TSK_ExplicitInstantiationDefinition:
1236 if (!DumpExplicitInst)
1237 break;
1238 // Fall through.
1239 case TSK_Undeclared:
1240 case TSK_ImplicitInstantiation:
Richard Smithf7514452014-10-30 21:02:37 +00001241 if (DumpRefOnly)
1242 dumpDeclRef(Redecl);
1243 else
1244 dumpDecl(Redecl);
Richard Smithcbdf7332014-03-18 02:07:28 +00001245 DumpedAny = true;
1246 break;
1247 case TSK_ExplicitSpecialization:
1248 break;
1249 }
1250 }
1251
1252 // Ensure we dump at least one decl for each specialization.
1253 if (!DumpedAny)
Richard Smithf7514452014-10-30 21:02:37 +00001254 dumpDeclRef(D);
Richard Smithcbdf7332014-03-18 02:07:28 +00001255}
1256
Richard Smith20ade552014-03-17 23:34:53 +00001257template<typename TemplateDecl>
1258void ASTDumper::VisitTemplateDecl(const TemplateDecl *D,
1259 bool DumpExplicitInst) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001260 dumpName(D);
1261 dumpTemplateParameters(D->getTemplateParameters());
Richard Smithdcc2c452014-03-17 23:00:06 +00001262
Richard Smithf7514452014-10-30 21:02:37 +00001263 dumpDecl(D->getTemplatedDecl());
Richard Smithdcc2c452014-03-17 23:00:06 +00001264
Richard Smithcbdf7332014-03-18 02:07:28 +00001265 for (auto *Child : D->specializations())
Richard Smithf7514452014-10-30 21:02:37 +00001266 VisitTemplateDeclSpecialization(Child, DumpExplicitInst,
Richard Smithcbdf7332014-03-18 02:07:28 +00001267 !D->isCanonicalDecl());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001268}
1269
Richard Smith20ade552014-03-17 23:34:53 +00001270void ASTDumper::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
1271 // FIXME: We don't add a declaration of a function template specialization
1272 // to its context when it's explicitly instantiated, so dump explicit
1273 // instantiations when we dump the template itself.
1274 VisitTemplateDecl(D, true);
1275}
1276
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001277void ASTDumper::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
Richard Smith20ade552014-03-17 23:34:53 +00001278 VisitTemplateDecl(D, false);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001279}
1280
1281void ASTDumper::VisitClassTemplateSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001282 const ClassTemplateSpecializationDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001283 VisitCXXRecordDecl(D);
1284 dumpTemplateArgumentList(D->getTemplateArgs());
1285}
1286
1287void ASTDumper::VisitClassTemplatePartialSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001288 const ClassTemplatePartialSpecializationDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001289 VisitClassTemplateSpecializationDecl(D);
1290 dumpTemplateParameters(D->getTemplateParameters());
1291}
1292
1293void ASTDumper::VisitClassScopeFunctionSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001294 const ClassScopeFunctionSpecializationDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001295 dumpDeclRef(D->getSpecialization());
1296 if (D->hasExplicitTemplateArgs())
1297 dumpTemplateArgumentListInfo(D->templateArgs());
1298}
1299
Richard Smithd25789a2013-09-18 01:36:02 +00001300void ASTDumper::VisitVarTemplateDecl(const VarTemplateDecl *D) {
Richard Smith20ade552014-03-17 23:34:53 +00001301 VisitTemplateDecl(D, false);
Richard Smithd25789a2013-09-18 01:36:02 +00001302}
1303
1304void ASTDumper::VisitVarTemplateSpecializationDecl(
1305 const VarTemplateSpecializationDecl *D) {
1306 dumpTemplateArgumentList(D->getTemplateArgs());
1307 VisitVarDecl(D);
1308}
1309
1310void ASTDumper::VisitVarTemplatePartialSpecializationDecl(
1311 const VarTemplatePartialSpecializationDecl *D) {
1312 dumpTemplateParameters(D->getTemplateParameters());
1313 VisitVarTemplateSpecializationDecl(D);
1314}
1315
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001316void ASTDumper::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001317 if (D->wasDeclaredWithTypename())
1318 OS << " typename";
1319 else
1320 OS << " class";
1321 if (D->isParameterPack())
1322 OS << " ...";
1323 dumpName(D);
Richard Smithf7514452014-10-30 21:02:37 +00001324 if (D->hasDefaultArgument())
Richard Smithecf74ff2014-03-23 20:50:39 +00001325 dumpTemplateArgument(D->getDefaultArgument());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001326}
1327
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001328void ASTDumper::VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001329 dumpType(D->getType());
1330 if (D->isParameterPack())
1331 OS << " ...";
1332 dumpName(D);
Richard Smithf7514452014-10-30 21:02:37 +00001333 if (D->hasDefaultArgument())
Richard Smithecf74ff2014-03-23 20:50:39 +00001334 dumpTemplateArgument(D->getDefaultArgument());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001335}
1336
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001337void ASTDumper::VisitTemplateTemplateParmDecl(
1338 const TemplateTemplateParmDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001339 if (D->isParameterPack())
1340 OS << " ...";
1341 dumpName(D);
1342 dumpTemplateParameters(D->getTemplateParameters());
Richard Smithf7514452014-10-30 21:02:37 +00001343 if (D->hasDefaultArgument())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001344 dumpTemplateArgumentLoc(D->getDefaultArgument());
1345}
1346
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001347void ASTDumper::VisitUsingDecl(const UsingDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001348 OS << ' ';
1349 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1350 OS << D->getNameAsString();
1351}
1352
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001353void ASTDumper::VisitUnresolvedUsingTypenameDecl(
1354 const UnresolvedUsingTypenameDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001355 OS << ' ';
1356 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1357 OS << D->getNameAsString();
1358}
1359
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001360void ASTDumper::VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001361 OS << ' ';
1362 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
1363 OS << D->getNameAsString();
1364 dumpType(D->getType());
1365}
1366
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001367void ASTDumper::VisitUsingShadowDecl(const UsingShadowDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001368 OS << ' ';
1369 dumpBareDeclRef(D->getTargetDecl());
1370}
1371
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001372void ASTDumper::VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001373 switch (D->getLanguage()) {
1374 case LinkageSpecDecl::lang_c: OS << " C"; break;
1375 case LinkageSpecDecl::lang_cxx: OS << " C++"; break;
1376 }
1377}
1378
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001379void ASTDumper::VisitAccessSpecDecl(const AccessSpecDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001380 OS << ' ';
1381 dumpAccessSpecifier(D->getAccess());
1382}
1383
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001384void ASTDumper::VisitFriendDecl(const FriendDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001385 if (TypeSourceInfo *T = D->getFriendType())
1386 dumpType(T->getType());
1387 else
1388 dumpDecl(D->getFriendDecl());
1389}
1390
1391//===----------------------------------------------------------------------===//
1392// Obj-C Declarations
1393//===----------------------------------------------------------------------===//
1394
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001395void ASTDumper::VisitObjCIvarDecl(const ObjCIvarDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001396 dumpName(D);
1397 dumpType(D->getType());
1398 if (D->getSynthesize())
1399 OS << " synthesize";
1400
1401 switch (D->getAccessControl()) {
1402 case ObjCIvarDecl::None:
1403 OS << " none";
1404 break;
1405 case ObjCIvarDecl::Private:
1406 OS << " private";
1407 break;
1408 case ObjCIvarDecl::Protected:
1409 OS << " protected";
1410 break;
1411 case ObjCIvarDecl::Public:
1412 OS << " public";
1413 break;
1414 case ObjCIvarDecl::Package:
1415 OS << " package";
1416 break;
1417 }
1418}
1419
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001420void ASTDumper::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001421 if (D->isInstanceMethod())
1422 OS << " -";
1423 else
1424 OS << " +";
1425 dumpName(D);
Alp Toker314cc812014-01-25 16:55:45 +00001426 dumpType(D->getReturnType());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001427
Richard Trieude5cc7d2013-01-31 01:44:26 +00001428 if (D->isThisDeclarationADefinition()) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001429 dumpDeclContext(D);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001430 } else {
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001431 for (ObjCMethodDecl::param_const_iterator I = D->param_begin(),
1432 E = D->param_end();
Richard Smithf7514452014-10-30 21:02:37 +00001433 I != E; ++I)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001434 dumpDecl(*I);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001435 }
1436
Richard Smithf7514452014-10-30 21:02:37 +00001437 if (D->isVariadic())
1438 dumpChild([=] { OS << "..."; });
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001439
Richard Smithf7514452014-10-30 21:02:37 +00001440 if (D->hasBody())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001441 dumpStmt(D->getBody());
1442}
1443
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001444void ASTDumper::VisitObjCCategoryDecl(const ObjCCategoryDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001445 dumpName(D);
1446 dumpDeclRef(D->getClassInterface());
1447 dumpDeclRef(D->getImplementation());
1448 for (ObjCCategoryDecl::protocol_iterator I = D->protocol_begin(),
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001449 E = D->protocol_end();
Richard Smithf7514452014-10-30 21:02:37 +00001450 I != E; ++I)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001451 dumpDeclRef(*I);
1452}
1453
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001454void ASTDumper::VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001455 dumpName(D);
1456 dumpDeclRef(D->getClassInterface());
1457 dumpDeclRef(D->getCategoryDecl());
1458}
1459
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001460void ASTDumper::VisitObjCProtocolDecl(const ObjCProtocolDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001461 dumpName(D);
Richard Smithdcc2c452014-03-17 23:00:06 +00001462
Richard Smith7fcb35f2014-03-18 02:37:59 +00001463 for (auto *Child : D->protocols())
Richard Smithf7514452014-10-30 21:02:37 +00001464 dumpDeclRef(Child);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001465}
1466
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001467void ASTDumper::VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001468 dumpName(D);
1469 dumpDeclRef(D->getSuperClass(), "super");
Richard Smithdcc2c452014-03-17 23:00:06 +00001470
Richard Smithf7514452014-10-30 21:02:37 +00001471 dumpDeclRef(D->getImplementation());
Richard Smith7fcb35f2014-03-18 02:37:59 +00001472 for (auto *Child : D->protocols())
Richard Smithf7514452014-10-30 21:02:37 +00001473 dumpDeclRef(Child);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001474}
1475
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001476void ASTDumper::VisitObjCImplementationDecl(const ObjCImplementationDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001477 dumpName(D);
1478 dumpDeclRef(D->getSuperClass(), "super");
1479 dumpDeclRef(D->getClassInterface());
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001480 for (ObjCImplementationDecl::init_const_iterator I = D->init_begin(),
1481 E = D->init_end();
Richard Smithf7514452014-10-30 21:02:37 +00001482 I != E; ++I)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001483 dumpCXXCtorInitializer(*I);
1484}
1485
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001486void ASTDumper::VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001487 dumpName(D);
1488 dumpDeclRef(D->getClassInterface());
1489}
1490
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001491void ASTDumper::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001492 dumpName(D);
1493 dumpType(D->getType());
1494
1495 if (D->getPropertyImplementation() == ObjCPropertyDecl::Required)
1496 OS << " required";
1497 else if (D->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1498 OS << " optional";
1499
1500 ObjCPropertyDecl::PropertyAttributeKind Attrs = D->getPropertyAttributes();
1501 if (Attrs != ObjCPropertyDecl::OBJC_PR_noattr) {
1502 if (Attrs & ObjCPropertyDecl::OBJC_PR_readonly)
1503 OS << " readonly";
1504 if (Attrs & ObjCPropertyDecl::OBJC_PR_assign)
1505 OS << " assign";
1506 if (Attrs & ObjCPropertyDecl::OBJC_PR_readwrite)
1507 OS << " readwrite";
1508 if (Attrs & ObjCPropertyDecl::OBJC_PR_retain)
1509 OS << " retain";
1510 if (Attrs & ObjCPropertyDecl::OBJC_PR_copy)
1511 OS << " copy";
1512 if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic)
1513 OS << " nonatomic";
1514 if (Attrs & ObjCPropertyDecl::OBJC_PR_atomic)
1515 OS << " atomic";
1516 if (Attrs & ObjCPropertyDecl::OBJC_PR_weak)
1517 OS << " weak";
1518 if (Attrs & ObjCPropertyDecl::OBJC_PR_strong)
1519 OS << " strong";
1520 if (Attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained)
1521 OS << " unsafe_unretained";
Richard Smithf7514452014-10-30 21:02:37 +00001522 if (Attrs & ObjCPropertyDecl::OBJC_PR_getter)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001523 dumpDeclRef(D->getGetterMethodDecl(), "getter");
Richard Smithf7514452014-10-30 21:02:37 +00001524 if (Attrs & ObjCPropertyDecl::OBJC_PR_setter)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001525 dumpDeclRef(D->getSetterMethodDecl(), "setter");
1526 }
1527}
1528
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001529void ASTDumper::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001530 dumpName(D->getPropertyDecl());
1531 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
1532 OS << " synthesize";
1533 else
1534 OS << " dynamic";
1535 dumpDeclRef(D->getPropertyDecl());
1536 dumpDeclRef(D->getPropertyIvarDecl());
1537}
1538
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001539void ASTDumper::VisitBlockDecl(const BlockDecl *D) {
Aaron Ballmanb2b8b1d2014-03-07 16:09:59 +00001540 for (auto I : D->params())
1541 dumpDecl(I);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001542
Richard Smithf7514452014-10-30 21:02:37 +00001543 if (D->isVariadic())
1544 dumpChild([=]{ OS << "..."; });
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001545
Richard Smithf7514452014-10-30 21:02:37 +00001546 if (D->capturesCXXThis())
1547 dumpChild([=]{ OS << "capture this"; });
1548
Aaron Ballman9371dd22014-03-14 18:34:04 +00001549 for (const auto &I : D->captures()) {
Richard Smithf7514452014-10-30 21:02:37 +00001550 dumpChild([=] {
1551 OS << "capture";
1552 if (I.isByRef())
1553 OS << " byref";
1554 if (I.isNested())
1555 OS << " nested";
1556 if (I.getVariable()) {
1557 OS << ' ';
1558 dumpBareDeclRef(I.getVariable());
1559 }
1560 if (I.hasCopyExpr())
1561 dumpStmt(I.getCopyExpr());
1562 });
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001563 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001564 dumpStmt(D->getBody());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001565}
1566
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001567//===----------------------------------------------------------------------===//
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001568// Stmt dumping methods.
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001569//===----------------------------------------------------------------------===//
1570
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001571void ASTDumper::dumpStmt(const Stmt *S) {
Richard Smithf7514452014-10-30 21:02:37 +00001572 dumpChild([=] {
1573 if (!S) {
1574 ColorScope Color(*this, NullColor);
1575 OS << "<<<NULL>>>";
1576 return;
1577 }
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001578
Richard Smithf7514452014-10-30 21:02:37 +00001579 if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
1580 VisitDeclStmt(DS);
1581 return;
1582 }
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001583
Richard Smithf7514452014-10-30 21:02:37 +00001584 ConstStmtVisitor<ASTDumper>::Visit(S);
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001585
Richard Smithf7514452014-10-30 21:02:37 +00001586 for (Stmt::const_child_range CI = S->children(); CI; ++CI)
1587 dumpStmt(*CI);
1588 });
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001589}
1590
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001591void ASTDumper::VisitStmt(const Stmt *Node) {
Richard Trieud215b8d2013-01-26 01:31:20 +00001592 {
1593 ColorScope Color(*this, StmtColor);
1594 OS << Node->getStmtClassName();
1595 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001596 dumpPointer(Node);
1597 dumpSourceRange(Node->getSourceRange());
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001598}
1599
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001600void ASTDumper::VisitDeclStmt(const DeclStmt *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001601 VisitStmt(Node);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001602 for (DeclStmt::const_decl_iterator I = Node->decl_begin(),
1603 E = Node->decl_end();
Richard Smithf7514452014-10-30 21:02:37 +00001604 I != E; ++I)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001605 dumpDecl(*I);
Ted Kremenek433a4922007-12-12 06:59:42 +00001606}
1607
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001608void ASTDumper::VisitAttributedStmt(const AttributedStmt *Node) {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001609 VisitStmt(Node);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001610 for (ArrayRef<const Attr *>::iterator I = Node->getAttrs().begin(),
1611 E = Node->getAttrs().end();
Richard Smithf7514452014-10-30 21:02:37 +00001612 I != E; ++I)
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001613 dumpAttr(*I);
1614}
1615
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001616void ASTDumper::VisitLabelStmt(const LabelStmt *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001617 VisitStmt(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001618 OS << " '" << Node->getName() << "'";
Chris Lattnercbe4f772007-08-08 22:51:59 +00001619}
1620
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001621void ASTDumper::VisitGotoStmt(const GotoStmt *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001622 VisitStmt(Node);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001623 OS << " '" << Node->getLabel()->getName() << "'";
1624 dumpPointer(Node->getLabel());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001625}
1626
Pavel Labath1ef83422013-09-04 14:35:00 +00001627void ASTDumper::VisitCXXCatchStmt(const CXXCatchStmt *Node) {
1628 VisitStmt(Node);
1629 dumpDecl(Node->getExceptionDecl());
1630}
1631
Chris Lattnercbe4f772007-08-08 22:51:59 +00001632//===----------------------------------------------------------------------===//
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001633// Expr dumping methods.
Chris Lattnercbe4f772007-08-08 22:51:59 +00001634//===----------------------------------------------------------------------===//
1635
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001636void ASTDumper::VisitExpr(const Expr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001637 VisitStmt(Node);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001638 dumpType(Node->getType());
1639
Richard Trieud215b8d2013-01-26 01:31:20 +00001640 {
1641 ColorScope Color(*this, ValueKindColor);
1642 switch (Node->getValueKind()) {
1643 case VK_RValue:
1644 break;
1645 case VK_LValue:
1646 OS << " lvalue";
1647 break;
1648 case VK_XValue:
1649 OS << " xvalue";
1650 break;
1651 }
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001652 }
1653
Richard Trieud215b8d2013-01-26 01:31:20 +00001654 {
1655 ColorScope Color(*this, ObjectKindColor);
1656 switch (Node->getObjectKind()) {
1657 case OK_Ordinary:
1658 break;
1659 case OK_BitField:
1660 OS << " bitfield";
1661 break;
1662 case OK_ObjCProperty:
1663 OS << " objcproperty";
1664 break;
1665 case OK_ObjCSubscript:
1666 OS << " objcsubscript";
1667 break;
1668 case OK_VectorComponent:
1669 OS << " vectorcomponent";
1670 break;
1671 }
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001672 }
Chris Lattnercbe4f772007-08-08 22:51:59 +00001673}
1674
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001675static void dumpBasePath(raw_ostream &OS, const CastExpr *Node) {
John McCallcf142162010-08-07 06:22:56 +00001676 if (Node->path_empty())
Anders Carlssona70cff62010-04-24 19:06:50 +00001677 return;
1678
1679 OS << " (";
1680 bool First = true;
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001681 for (CastExpr::path_const_iterator I = Node->path_begin(),
1682 E = Node->path_end();
1683 I != E; ++I) {
Anders Carlssona70cff62010-04-24 19:06:50 +00001684 const CXXBaseSpecifier *Base = *I;
1685 if (!First)
1686 OS << " -> ";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001687
Anders Carlssona70cff62010-04-24 19:06:50 +00001688 const CXXRecordDecl *RD =
1689 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001690
Anders Carlssona70cff62010-04-24 19:06:50 +00001691 if (Base->isVirtual())
1692 OS << "virtual ";
1693 OS << RD->getName();
1694 First = false;
1695 }
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001696
Anders Carlssona70cff62010-04-24 19:06:50 +00001697 OS << ')';
1698}
1699
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001700void ASTDumper::VisitCastExpr(const CastExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001701 VisitExpr(Node);
Richard Trieud215b8d2013-01-26 01:31:20 +00001702 OS << " <";
1703 {
1704 ColorScope Color(*this, CastColor);
1705 OS << Node->getCastKindName();
1706 }
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001707 dumpBasePath(OS, Node);
Anders Carlssona70cff62010-04-24 19:06:50 +00001708 OS << ">";
Anders Carlssond7923c62009-08-22 23:33:40 +00001709}
1710
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001711void ASTDumper::VisitDeclRefExpr(const DeclRefExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001712 VisitExpr(Node);
Ted Kremenek5f64ca82007-09-10 17:32:55 +00001713
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001714 OS << " ";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001715 dumpBareDeclRef(Node->getDecl());
Chandler Carruth8d26bb02011-05-01 23:48:14 +00001716 if (Node->getDecl() != Node->getFoundDecl()) {
1717 OS << " (";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001718 dumpBareDeclRef(Node->getFoundDecl());
Chandler Carruth8d26bb02011-05-01 23:48:14 +00001719 OS << ")";
1720 }
John McCall351762c2011-02-07 10:33:21 +00001721}
1722
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001723void ASTDumper::VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001724 VisitExpr(Node);
John McCall76d09942009-12-11 21:50:11 +00001725 OS << " (";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001726 if (!Node->requiresADL())
1727 OS << "no ";
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001728 OS << "ADL) = '" << Node->getName() << '\'';
John McCall76d09942009-12-11 21:50:11 +00001729
1730 UnresolvedLookupExpr::decls_iterator
1731 I = Node->decls_begin(), E = Node->decls_end();
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001732 if (I == E)
1733 OS << " empty";
John McCall76d09942009-12-11 21:50:11 +00001734 for (; I != E; ++I)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001735 dumpPointer(*I);
John McCall76d09942009-12-11 21:50:11 +00001736}
1737
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001738void ASTDumper::VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001739 VisitExpr(Node);
Steve Naroff5d5efca2008-03-12 13:19:12 +00001740
Richard Trieud215b8d2013-01-26 01:31:20 +00001741 {
1742 ColorScope Color(*this, DeclKindNameColor);
1743 OS << " " << Node->getDecl()->getDeclKindName() << "Decl";
1744 }
1745 OS << "='" << *Node->getDecl() << "'";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001746 dumpPointer(Node->getDecl());
Steve Naroffb3424a92008-05-23 22:01:24 +00001747 if (Node->isFreeIvar())
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001748 OS << " isFreeIvar";
Steve Naroff5d5efca2008-03-12 13:19:12 +00001749}
1750
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001751void ASTDumper::VisitPredefinedExpr(const PredefinedExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001752 VisitExpr(Node);
Alexey Bataevec474782014-10-09 08:45:04 +00001753 OS << " " << PredefinedExpr::getIdentTypeName(Node->getIdentType());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001754}
1755
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001756void ASTDumper::VisitCharacterLiteral(const CharacterLiteral *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001757 VisitExpr(Node);
Richard Trieud215b8d2013-01-26 01:31:20 +00001758 ColorScope Color(*this, ValueColor);
Richard Trieu364ee422011-11-03 23:56:23 +00001759 OS << " " << Node->getValue();
Chris Lattnercbe4f772007-08-08 22:51:59 +00001760}
1761
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001762void ASTDumper::VisitIntegerLiteral(const IntegerLiteral *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001763 VisitExpr(Node);
Chris Lattnercbe4f772007-08-08 22:51:59 +00001764
1765 bool isSigned = Node->getType()->isSignedIntegerType();
Richard Trieud215b8d2013-01-26 01:31:20 +00001766 ColorScope Color(*this, ValueColor);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001767 OS << " " << Node->getValue().toString(10, isSigned);
Chris Lattnercbe4f772007-08-08 22:51:59 +00001768}
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001769
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001770void ASTDumper::VisitFloatingLiteral(const FloatingLiteral *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001771 VisitExpr(Node);
Richard Trieud215b8d2013-01-26 01:31:20 +00001772 ColorScope Color(*this, ValueColor);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001773 OS << " " << Node->getValueAsApproximateDouble();
Chris Lattnercbe4f772007-08-08 22:51:59 +00001774}
Chris Lattner1c20a172007-08-26 03:42:43 +00001775
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001776void ASTDumper::VisitStringLiteral(const StringLiteral *Str) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001777 VisitExpr(Str);
Richard Trieud215b8d2013-01-26 01:31:20 +00001778 ColorScope Color(*this, ValueColor);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001779 OS << " ";
Richard Trieudc355912012-06-13 20:25:24 +00001780 Str->outputString(OS);
Chris Lattnercbe4f772007-08-08 22:51:59 +00001781}
Chris Lattner84ca3762007-08-30 01:00:35 +00001782
Richard Smithf0514962014-06-03 08:24:28 +00001783void ASTDumper::VisitInitListExpr(const InitListExpr *ILE) {
1784 VisitExpr(ILE);
1785 if (auto *Filler = ILE->getArrayFiller()) {
Richard Smithf7514452014-10-30 21:02:37 +00001786 dumpChild([=] {
1787 OS << "array filler";
1788 dumpStmt(Filler);
1789 });
Richard Smithf0514962014-06-03 08:24:28 +00001790 }
1791 if (auto *Field = ILE->getInitializedFieldInUnion()) {
1792 OS << " field ";
1793 dumpBareDeclRef(Field);
1794 }
1795}
1796
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001797void ASTDumper::VisitUnaryOperator(const UnaryOperator *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001798 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001799 OS << " " << (Node->isPostfix() ? "postfix" : "prefix")
1800 << " '" << UnaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
Chris Lattnercbe4f772007-08-08 22:51:59 +00001801}
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001802
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001803void ASTDumper::VisitUnaryExprOrTypeTraitExpr(
1804 const UnaryExprOrTypeTraitExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001805 VisitExpr(Node);
Peter Collingbournee190dee2011-03-11 19:24:49 +00001806 switch(Node->getKind()) {
1807 case UETT_SizeOf:
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001808 OS << " sizeof";
Peter Collingbournee190dee2011-03-11 19:24:49 +00001809 break;
1810 case UETT_AlignOf:
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001811 OS << " alignof";
Peter Collingbournee190dee2011-03-11 19:24:49 +00001812 break;
1813 case UETT_VecStep:
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001814 OS << " vec_step";
Peter Collingbournee190dee2011-03-11 19:24:49 +00001815 break;
1816 }
Sebastian Redl6f282892008-11-11 17:56:53 +00001817 if (Node->isArgumentType())
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001818 dumpType(Node->getArgumentType());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001819}
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +00001820
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001821void ASTDumper::VisitMemberExpr(const MemberExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001822 VisitExpr(Node);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001823 OS << " " << (Node->isArrow() ? "->" : ".") << *Node->getMemberDecl();
1824 dumpPointer(Node->getMemberDecl());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001825}
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001826
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001827void ASTDumper::VisitExtVectorElementExpr(const ExtVectorElementExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001828 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001829 OS << " " << Node->getAccessor().getNameStart();
Chris Lattnercbe4f772007-08-08 22:51:59 +00001830}
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001831
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001832void ASTDumper::VisitBinaryOperator(const BinaryOperator *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001833 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001834 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
Chris Lattner86928112007-08-25 02:00:02 +00001835}
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001836
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001837void ASTDumper::VisitCompoundAssignOperator(
1838 const CompoundAssignOperator *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001839 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001840 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode())
1841 << "' ComputeLHSTy=";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001842 dumpBareType(Node->getComputationLHSType());
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001843 OS << " ComputeResultTy=";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001844 dumpBareType(Node->getComputationResultType());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001845}
Chris Lattnercbe4f772007-08-08 22:51:59 +00001846
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001847void ASTDumper::VisitBlockExpr(const BlockExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001848 VisitExpr(Node);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001849 dumpDecl(Node->getBlockDecl());
John McCall351762c2011-02-07 10:33:21 +00001850}
1851
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001852void ASTDumper::VisitOpaqueValueExpr(const OpaqueValueExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001853 VisitExpr(Node);
John McCallfe96e0b2011-11-06 09:01:30 +00001854
Richard Smithf7514452014-10-30 21:02:37 +00001855 if (Expr *Source = Node->getSourceExpr())
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001856 dumpStmt(Source);
John McCallfe96e0b2011-11-06 09:01:30 +00001857}
1858
Chris Lattnercbe4f772007-08-08 22:51:59 +00001859// GNU extensions.
1860
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001861void ASTDumper::VisitAddrLabelExpr(const AddrLabelExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001862 VisitExpr(Node);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001863 OS << " " << Node->getLabel()->getName();
1864 dumpPointer(Node->getLabel());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001865}
1866
Chris Lattner8f184b12007-08-09 18:03:18 +00001867//===----------------------------------------------------------------------===//
1868// C++ Expressions
1869//===----------------------------------------------------------------------===//
Chris Lattnercbe4f772007-08-08 22:51:59 +00001870
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001871void ASTDumper::VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001872 VisitExpr(Node);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001873 OS << " " << Node->getCastName()
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001874 << "<" << Node->getTypeAsWritten().getAsString() << ">"
Anders Carlssona70cff62010-04-24 19:06:50 +00001875 << " <" << Node->getCastKindName();
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001876 dumpBasePath(OS, Node);
Anders Carlssona70cff62010-04-24 19:06:50 +00001877 OS << ">";
Chris Lattnercbe4f772007-08-08 22:51:59 +00001878}
1879
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001880void ASTDumper::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001881 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001882 OS << " " << (Node->getValue() ? "true" : "false");
Chris Lattnercbe4f772007-08-08 22:51:59 +00001883}
1884
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001885void ASTDumper::VisitCXXThisExpr(const CXXThisExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001886 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001887 OS << " this";
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001888}
1889
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001890void ASTDumper::VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001891 VisitExpr(Node);
Eli Friedman29538892011-09-02 17:38:59 +00001892 OS << " functional cast to " << Node->getTypeAsWritten().getAsString()
1893 << " <" << Node->getCastKindName() << ">";
Douglas Gregore200adc2008-10-27 19:41:14 +00001894}
1895
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001896void ASTDumper::VisitCXXConstructExpr(const CXXConstructExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001897 VisitExpr(Node);
John McCalleba90cd2010-02-02 19:03:45 +00001898 CXXConstructorDecl *Ctor = Node->getConstructor();
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001899 dumpType(Ctor->getType());
Anders Carlsson073846832009-08-12 00:21:52 +00001900 if (Node->isElidable())
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001901 OS << " elidable";
John McCall85370042010-08-07 06:38:55 +00001902 if (Node->requiresZeroInitialization())
1903 OS << " zeroing";
Anders Carlsson073846832009-08-12 00:21:52 +00001904}
1905
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001906void ASTDumper::VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001907 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001908 OS << " ";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001909 dumpCXXTemporary(Node->getTemporary());
Anders Carlsson073846832009-08-12 00:21:52 +00001910}
1911
Richard Smithe6c01442013-06-05 00:46:14 +00001912void
1913ASTDumper::VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node) {
1914 VisitExpr(Node);
1915 if (const ValueDecl *VD = Node->getExtendingDecl()) {
1916 OS << " extended by ";
1917 dumpBareDeclRef(VD);
1918 }
1919}
1920
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001921void ASTDumper::VisitExprWithCleanups(const ExprWithCleanups *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001922 VisitExpr(Node);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001923 for (unsigned i = 0, e = Node->getNumObjects(); i != e; ++i)
1924 dumpDeclRef(Node->getObject(i), "cleanup");
Anders Carlsson073846832009-08-12 00:21:52 +00001925}
1926
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001927void ASTDumper::dumpCXXTemporary(const CXXTemporary *Temporary) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001928 OS << "(CXXTemporary";
1929 dumpPointer(Temporary);
1930 OS << ")";
Anders Carlsson073846832009-08-12 00:21:52 +00001931}
1932
Anders Carlsson76f4a902007-08-21 17:43:55 +00001933//===----------------------------------------------------------------------===//
1934// Obj-C Expressions
1935//===----------------------------------------------------------------------===//
1936
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001937void ASTDumper::VisitObjCMessageExpr(const ObjCMessageExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001938 VisitExpr(Node);
Aaron Ballmanb190f972014-01-03 17:59:55 +00001939 OS << " selector=";
1940 Node->getSelector().print(OS);
Douglas Gregor9a129192010-04-21 00:45:42 +00001941 switch (Node->getReceiverKind()) {
1942 case ObjCMessageExpr::Instance:
1943 break;
1944
1945 case ObjCMessageExpr::Class:
1946 OS << " class=";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001947 dumpBareType(Node->getClassReceiver());
Douglas Gregor9a129192010-04-21 00:45:42 +00001948 break;
1949
1950 case ObjCMessageExpr::SuperInstance:
1951 OS << " super (instance)";
1952 break;
1953
1954 case ObjCMessageExpr::SuperClass:
1955 OS << " super (class)";
1956 break;
1957 }
Ted Kremenek36748da2008-02-29 22:04:05 +00001958}
1959
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001960void ASTDumper::VisitObjCBoxedExpr(const ObjCBoxedExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001961 VisitExpr(Node);
Aaron Ballmanb190f972014-01-03 17:59:55 +00001962 OS << " selector=";
1963 Node->getBoxingMethod()->getSelector().print(OS);
Argyrios Kyrtzidis9dd40892012-05-10 20:02:31 +00001964}
1965
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001966void ASTDumper::VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001967 VisitStmt(Node);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001968 if (const VarDecl *CatchParam = Node->getCatchParamDecl())
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001969 dumpDecl(CatchParam);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001970 else
Douglas Gregor96c79492010-04-23 22:50:49 +00001971 OS << " catch all";
Douglas Gregor96c79492010-04-23 22:50:49 +00001972}
1973
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001974void ASTDumper::VisitObjCEncodeExpr(const ObjCEncodeExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001975 VisitExpr(Node);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001976 dumpType(Node->getEncodedType());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001977}
1978
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001979void ASTDumper::VisitObjCSelectorExpr(const ObjCSelectorExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001980 VisitExpr(Node);
Mike Stump11289f42009-09-09 15:08:12 +00001981
Aaron Ballmanb190f972014-01-03 17:59:55 +00001982 OS << " ";
1983 Node->getSelector().print(OS);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001984}
1985
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001986void ASTDumper::VisitObjCProtocolExpr(const ObjCProtocolExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001987 VisitExpr(Node);
Mike Stump11289f42009-09-09 15:08:12 +00001988
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001989 OS << ' ' << *Node->getProtocol();
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001990}
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001991
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001992void ASTDumper::VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001993 VisitExpr(Node);
John McCallb7bd14f2010-12-02 01:19:52 +00001994 if (Node->isImplicitProperty()) {
Fariborz Jahanian0f0b3022010-12-22 19:46:35 +00001995 OS << " Kind=MethodRef Getter=\"";
1996 if (Node->getImplicitPropertyGetter())
Aaron Ballmanb190f972014-01-03 17:59:55 +00001997 Node->getImplicitPropertyGetter()->getSelector().print(OS);
Fariborz Jahanian0f0b3022010-12-22 19:46:35 +00001998 else
1999 OS << "(null)";
2000
2001 OS << "\" Setter=\"";
John McCallb7bd14f2010-12-02 01:19:52 +00002002 if (ObjCMethodDecl *Setter = Node->getImplicitPropertySetter())
Aaron Ballmanb190f972014-01-03 17:59:55 +00002003 Setter->getSelector().print(OS);
John McCallb7bd14f2010-12-02 01:19:52 +00002004 else
2005 OS << "(null)";
2006 OS << "\"";
2007 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00002008 OS << " Kind=PropertyRef Property=\"" << *Node->getExplicitProperty() <<'"';
John McCallb7bd14f2010-12-02 01:19:52 +00002009 }
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00002010
Fariborz Jahanian681c0752010-10-14 16:04:05 +00002011 if (Node->isSuperReceiver())
2012 OS << " super";
Argyrios Kyrtzidisab468b02012-03-30 00:19:18 +00002013
2014 OS << " Messaging=";
2015 if (Node->isMessagingGetter() && Node->isMessagingSetter())
2016 OS << "Getter&Setter";
2017 else if (Node->isMessagingGetter())
2018 OS << "Getter";
2019 else if (Node->isMessagingSetter())
2020 OS << "Setter";
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002021}
2022
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002023void ASTDumper::VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002024 VisitExpr(Node);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002025 if (Node->isArraySubscriptRefExpr())
2026 OS << " Kind=ArraySubscript GetterForArray=\"";
2027 else
2028 OS << " Kind=DictionarySubscript GetterForDictionary=\"";
2029 if (Node->getAtIndexMethodDecl())
Aaron Ballmanb190f972014-01-03 17:59:55 +00002030 Node->getAtIndexMethodDecl()->getSelector().print(OS);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002031 else
2032 OS << "(null)";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00002033
Ted Kremeneke65b0862012-03-06 20:05:56 +00002034 if (Node->isArraySubscriptRefExpr())
2035 OS << "\" SetterForArray=\"";
2036 else
2037 OS << "\" SetterForDictionary=\"";
2038 if (Node->setAtIndexMethodDecl())
Aaron Ballmanb190f972014-01-03 17:59:55 +00002039 Node->setAtIndexMethodDecl()->getSelector().print(OS);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002040 else
2041 OS << "(null)";
2042}
2043
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002044void ASTDumper::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002045 VisitExpr(Node);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002046 OS << " " << (Node->getValue() ? "__objc_yes" : "__objc_no");
2047}
2048
Chris Lattnercbe4f772007-08-08 22:51:59 +00002049//===----------------------------------------------------------------------===//
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002050// Comments
2051//===----------------------------------------------------------------------===//
2052
2053const char *ASTDumper::getCommandName(unsigned CommandID) {
2054 if (Traits)
2055 return Traits->getCommandInfo(CommandID)->Name;
2056 const CommandInfo *Info = CommandTraits::getBuiltinCommandInfo(CommandID);
2057 if (Info)
2058 return Info->Name;
2059 return "<not a builtin command>";
2060}
2061
2062void ASTDumper::dumpFullComment(const FullComment *C) {
2063 if (!C)
2064 return;
2065
2066 FC = C;
2067 dumpComment(C);
Craig Topper36250ad2014-05-12 05:36:57 +00002068 FC = nullptr;
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002069}
2070
2071void ASTDumper::dumpComment(const Comment *C) {
Richard Smithf7514452014-10-30 21:02:37 +00002072 dumpChild([=] {
2073 if (!C) {
2074 ColorScope Color(*this, NullColor);
2075 OS << "<<<NULL>>>";
2076 return;
2077 }
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002078
Richard Smithf7514452014-10-30 21:02:37 +00002079 {
2080 ColorScope Color(*this, CommentColor);
2081 OS << C->getCommentKindName();
2082 }
2083 dumpPointer(C);
2084 dumpSourceRange(C->getSourceRange());
2085 ConstCommentVisitor<ASTDumper>::visit(C);
2086 for (Comment::child_iterator I = C->child_begin(), E = C->child_end();
2087 I != E; ++I)
2088 dumpComment(*I);
2089 });
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002090}
2091
2092void ASTDumper::visitTextComment(const TextComment *C) {
2093 OS << " Text=\"" << C->getText() << "\"";
2094}
2095
2096void ASTDumper::visitInlineCommandComment(const InlineCommandComment *C) {
2097 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
2098 switch (C->getRenderKind()) {
2099 case InlineCommandComment::RenderNormal:
2100 OS << " RenderNormal";
2101 break;
2102 case InlineCommandComment::RenderBold:
2103 OS << " RenderBold";
2104 break;
2105 case InlineCommandComment::RenderMonospaced:
2106 OS << " RenderMonospaced";
2107 break;
2108 case InlineCommandComment::RenderEmphasized:
2109 OS << " RenderEmphasized";
2110 break;
2111 }
2112
2113 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
2114 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
2115}
2116
2117void ASTDumper::visitHTMLStartTagComment(const HTMLStartTagComment *C) {
2118 OS << " Name=\"" << C->getTagName() << "\"";
2119 if (C->getNumAttrs() != 0) {
2120 OS << " Attrs: ";
2121 for (unsigned i = 0, e = C->getNumAttrs(); i != e; ++i) {
2122 const HTMLStartTagComment::Attribute &Attr = C->getAttr(i);
2123 OS << " \"" << Attr.Name << "=\"" << Attr.Value << "\"";
2124 }
2125 }
2126 if (C->isSelfClosing())
2127 OS << " SelfClosing";
2128}
2129
2130void ASTDumper::visitHTMLEndTagComment(const HTMLEndTagComment *C) {
2131 OS << " Name=\"" << C->getTagName() << "\"";
2132}
2133
2134void ASTDumper::visitBlockCommandComment(const BlockCommandComment *C) {
2135 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
2136 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
2137 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
2138}
2139
2140void ASTDumper::visitParamCommandComment(const ParamCommandComment *C) {
2141 OS << " " << ParamCommandComment::getDirectionAsString(C->getDirection());
2142
2143 if (C->isDirectionExplicit())
2144 OS << " explicitly";
2145 else
2146 OS << " implicitly";
2147
2148 if (C->hasParamName()) {
2149 if (C->isParamIndexValid())
2150 OS << " Param=\"" << C->getParamName(FC) << "\"";
2151 else
2152 OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
2153 }
2154
Dmitri Gribenkodbff5c72014-03-19 14:03:47 +00002155 if (C->isParamIndexValid() && !C->isVarArgParam())
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002156 OS << " ParamIndex=" << C->getParamIndex();
2157}
2158
2159void ASTDumper::visitTParamCommandComment(const TParamCommandComment *C) {
2160 if (C->hasParamName()) {
2161 if (C->isPositionValid())
2162 OS << " Param=\"" << C->getParamName(FC) << "\"";
2163 else
2164 OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
2165 }
2166
2167 if (C->isPositionValid()) {
2168 OS << " Position=<";
2169 for (unsigned i = 0, e = C->getDepth(); i != e; ++i) {
2170 OS << C->getIndex(i);
2171 if (i != e - 1)
2172 OS << ", ";
2173 }
2174 OS << ">";
2175 }
2176}
2177
2178void ASTDumper::visitVerbatimBlockComment(const VerbatimBlockComment *C) {
2179 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\""
2180 " CloseName=\"" << C->getCloseName() << "\"";
2181}
2182
2183void ASTDumper::visitVerbatimBlockLineComment(
2184 const VerbatimBlockLineComment *C) {
2185 OS << " Text=\"" << C->getText() << "\"";
2186}
2187
2188void ASTDumper::visitVerbatimLineComment(const VerbatimLineComment *C) {
2189 OS << " Text=\"" << C->getText() << "\"";
2190}
2191
2192//===----------------------------------------------------------------------===//
Richard Smithd5e7ff82014-10-31 01:17:45 +00002193// Type method implementations
2194//===----------------------------------------------------------------------===//
2195
2196void QualType::dump(const char *msg) const {
2197 if (msg)
2198 llvm::errs() << msg << ": ";
2199 dump();
2200}
2201
2202LLVM_DUMP_METHOD void QualType::dump() const {
2203 ASTDumper Dumper(llvm::errs(), nullptr, nullptr);
2204 Dumper.dumpTypeAsChild(*this);
2205}
2206
2207LLVM_DUMP_METHOD void Type::dump() const { QualType(this, 0).dump(); }
2208
2209//===----------------------------------------------------------------------===//
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002210// Decl method implementations
2211//===----------------------------------------------------------------------===//
2212
Alp Tokeref6b0072014-01-04 13:47:14 +00002213LLVM_DUMP_METHOD void Decl::dump() const { dump(llvm::errs()); }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002214
Alp Tokeref6b0072014-01-04 13:47:14 +00002215LLVM_DUMP_METHOD void Decl::dump(raw_ostream &OS) const {
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002216 ASTDumper P(OS, &getASTContext().getCommentCommandTraits(),
2217 &getASTContext().getSourceManager());
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002218 P.dumpDecl(this);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002219}
2220
Alp Tokeref6b0072014-01-04 13:47:14 +00002221LLVM_DUMP_METHOD void Decl::dumpColor() const {
Richard Trieud215b8d2013-01-26 01:31:20 +00002222 ASTDumper P(llvm::errs(), &getASTContext().getCommentCommandTraits(),
2223 &getASTContext().getSourceManager(), /*ShowColors*/true);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002224 P.dumpDecl(this);
Richard Trieud215b8d2013-01-26 01:31:20 +00002225}
Richard Smith33937e72013-06-22 21:49:40 +00002226
Alp Tokeref6b0072014-01-04 13:47:14 +00002227LLVM_DUMP_METHOD void DeclContext::dumpLookups() const {
Richard Smith6ea05822013-06-24 01:45:33 +00002228 dumpLookups(llvm::errs());
2229}
2230
Richard Smith35f986d2014-08-11 22:11:07 +00002231LLVM_DUMP_METHOD void DeclContext::dumpLookups(raw_ostream &OS,
2232 bool DumpDecls) const {
Richard Smith33937e72013-06-22 21:49:40 +00002233 const DeclContext *DC = this;
2234 while (!DC->isTranslationUnit())
2235 DC = DC->getParent();
2236 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
Richard Smith6ea05822013-06-24 01:45:33 +00002237 ASTDumper P(OS, &Ctx.getCommentCommandTraits(), &Ctx.getSourceManager());
Richard Smith35f986d2014-08-11 22:11:07 +00002238 P.dumpLookups(this, DumpDecls);
Richard Smith33937e72013-06-22 21:49:40 +00002239}
2240
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002241//===----------------------------------------------------------------------===//
Chris Lattnercbe4f772007-08-08 22:51:59 +00002242// Stmt method implementations
2243//===----------------------------------------------------------------------===//
2244
Alp Tokeref6b0072014-01-04 13:47:14 +00002245LLVM_DUMP_METHOD void Stmt::dump(SourceManager &SM) const {
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +00002246 dump(llvm::errs(), SM);
2247}
2248
Alp Tokeref6b0072014-01-04 13:47:14 +00002249LLVM_DUMP_METHOD void Stmt::dump(raw_ostream &OS, SourceManager &SM) const {
Craig Topper36250ad2014-05-12 05:36:57 +00002250 ASTDumper P(OS, nullptr, &SM);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002251 P.dumpStmt(this);
Chris Lattner779d5d92007-08-30 00:40:08 +00002252}
2253
Alp Tokeref6b0072014-01-04 13:47:14 +00002254LLVM_DUMP_METHOD void Stmt::dump() const {
Craig Topper36250ad2014-05-12 05:36:57 +00002255 ASTDumper P(llvm::errs(), nullptr, nullptr);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002256 P.dumpStmt(this);
Chris Lattnercbe4f772007-08-08 22:51:59 +00002257}
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002258
Alp Tokeref6b0072014-01-04 13:47:14 +00002259LLVM_DUMP_METHOD void Stmt::dumpColor() const {
Craig Topper36250ad2014-05-12 05:36:57 +00002260 ASTDumper P(llvm::errs(), nullptr, nullptr, /*ShowColors*/true);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002261 P.dumpStmt(this);
Richard Trieud215b8d2013-01-26 01:31:20 +00002262}
2263
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002264//===----------------------------------------------------------------------===//
2265// Comment method implementations
2266//===----------------------------------------------------------------------===//
2267
Craig Topper36250ad2014-05-12 05:36:57 +00002268LLVM_DUMP_METHOD void Comment::dump() const {
2269 dump(llvm::errs(), nullptr, nullptr);
2270}
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002271
Alp Tokeref6b0072014-01-04 13:47:14 +00002272LLVM_DUMP_METHOD void Comment::dump(const ASTContext &Context) const {
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002273 dump(llvm::errs(), &Context.getCommentCommandTraits(),
2274 &Context.getSourceManager());
2275}
2276
Alexander Kornienko00911f12013-01-15 12:20:21 +00002277void Comment::dump(raw_ostream &OS, const CommandTraits *Traits,
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002278 const SourceManager *SM) const {
2279 const FullComment *FC = dyn_cast<FullComment>(this);
2280 ASTDumper D(OS, Traits, SM);
2281 D.dumpFullComment(FC);
2282}
Richard Trieud215b8d2013-01-26 01:31:20 +00002283
Alp Tokeref6b0072014-01-04 13:47:14 +00002284LLVM_DUMP_METHOD void Comment::dumpColor() const {
Richard Trieud215b8d2013-01-26 01:31:20 +00002285 const FullComment *FC = dyn_cast<FullComment>(this);
Craig Topper36250ad2014-05-12 05:36:57 +00002286 ASTDumper D(llvm::errs(), nullptr, nullptr, /*ShowColors*/true);
Richard Trieud215b8d2013-01-26 01:31:20 +00002287 D.dumpFullComment(FC);
2288}