blob: e74aee0aaa4a560b22a6537cdede3dd01577a3fb [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"
Alexey Bataev958b9e72016-03-31 09:30:50 +000021#include "clang/AST/DeclOpenMP.h"
Alexander Kornienko90ff6072012-12-20 02:09:13 +000022#include "clang/AST/DeclVisitor.h"
Benjamin Kramer31b382e2016-02-01 17:42:01 +000023#include "clang/AST/LocInfoType.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000024#include "clang/AST/StmtVisitor.h"
Richard Smithd5e7ff82014-10-31 01:17:45 +000025#include "clang/AST/TypeVisitor.h"
David Majnemerd9b1a4f2015-11-04 03:40:30 +000026#include "clang/Basic/Builtins.h"
Alexander Kornienko90ff6072012-12-20 02:09:13 +000027#include "clang/Basic/Module.h"
Chris Lattner11e30d32007-08-30 06:17:34 +000028#include "clang/Basic/SourceManager.h"
Daniel Dunbar34a96c82009-12-03 09:13:13 +000029#include "llvm/Support/raw_ostream.h"
Chris Lattnercbe4f772007-08-08 22:51:59 +000030using namespace clang;
Alexander Kornienkoebc17b52013-01-14 14:07:11 +000031using namespace clang::comments;
Chris Lattnercbe4f772007-08-08 22:51:59 +000032
33//===----------------------------------------------------------------------===//
Alexander Kornienko18ec81b2012-12-13 13:59:55 +000034// ASTDumper Visitor
Chris Lattnercbe4f772007-08-08 22:51:59 +000035//===----------------------------------------------------------------------===//
36
37namespace {
Richard Trieud215b8d2013-01-26 01:31:20 +000038 // Colors used for various parts of the AST dump
Richard Trieu532018f2014-03-06 01:09:03 +000039 // Do not use bold yellow for any text. It is hard to read on white screens.
Richard Trieud215b8d2013-01-26 01:31:20 +000040
41 struct TerminalColor {
42 raw_ostream::Colors Color;
43 bool Bold;
44 };
45
Richard Trieu532018f2014-03-06 01:09:03 +000046 // Red - CastColor
47 // Green - TypeColor
48 // Bold Green - DeclKindNameColor, UndeserializedColor
49 // Yellow - AddressColor, LocationColor
50 // Blue - CommentColor, NullColor, IndentColor
51 // Bold Blue - AttrColor
52 // Bold Magenta - StmtColor
53 // Cyan - ValueKindColor, ObjectKindColor
54 // Bold Cyan - ValueColor, DeclNameColor
55
Richard Trieud215b8d2013-01-26 01:31:20 +000056 // Decl kind names (VarDecl, FunctionDecl, etc)
57 static const TerminalColor DeclKindNameColor = { raw_ostream::GREEN, true };
58 // Attr names (CleanupAttr, GuardedByAttr, etc)
59 static const TerminalColor AttrColor = { raw_ostream::BLUE, true };
60 // Statement names (DeclStmt, ImplicitCastExpr, etc)
61 static const TerminalColor StmtColor = { raw_ostream::MAGENTA, true };
62 // Comment names (FullComment, ParagraphComment, TextComment, etc)
Richard Trieu532018f2014-03-06 01:09:03 +000063 static const TerminalColor CommentColor = { raw_ostream::BLUE, false };
Richard Trieud215b8d2013-01-26 01:31:20 +000064
65 // Type names (int, float, etc, plus user defined types)
66 static const TerminalColor TypeColor = { raw_ostream::GREEN, false };
67
68 // Pointer address
69 static const TerminalColor AddressColor = { raw_ostream::YELLOW, false };
70 // Source locations
71 static const TerminalColor LocationColor = { raw_ostream::YELLOW, false };
72
73 // lvalue/xvalue
74 static const TerminalColor ValueKindColor = { raw_ostream::CYAN, false };
75 // bitfield/objcproperty/objcsubscript/vectorcomponent
76 static const TerminalColor ObjectKindColor = { raw_ostream::CYAN, false };
77
78 // Null statements
79 static const TerminalColor NullColor = { raw_ostream::BLUE, false };
80
Richard Smith1d209d02013-05-23 01:49:11 +000081 // Undeserialized entities
82 static const TerminalColor UndeserializedColor = { raw_ostream::GREEN, true };
83
Richard Trieud215b8d2013-01-26 01:31:20 +000084 // CastKind from CastExpr's
85 static const TerminalColor CastColor = { raw_ostream::RED, false };
86
87 // Value of the statement
88 static const TerminalColor ValueColor = { raw_ostream::CYAN, true };
89 // Decl names
90 static const TerminalColor DeclNameColor = { raw_ostream::CYAN, true };
91
Richard Trieude5cc7d2013-01-31 01:44:26 +000092 // Indents ( `, -. | )
93 static const TerminalColor IndentColor = { raw_ostream::BLUE, false };
94
Alexander Kornienko90ff6072012-12-20 02:09:13 +000095 class ASTDumper
Alexander Kornienko540bacb2013-02-01 12:35:51 +000096 : public ConstDeclVisitor<ASTDumper>, public ConstStmtVisitor<ASTDumper>,
Richard Smithd5e7ff82014-10-31 01:17:45 +000097 public ConstCommentVisitor<ASTDumper>, public TypeVisitor<ASTDumper> {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000098 raw_ostream &OS;
Alexander Kornienkoebc17b52013-01-14 14:07:11 +000099 const CommandTraits *Traits;
100 const SourceManager *SM;
Mike Stump11289f42009-09-09 15:08:12 +0000101
Richard Smithf7514452014-10-30 21:02:37 +0000102 /// Pending[i] is an action to dump an entity at level i.
103 llvm::SmallVector<std::function<void(bool isLastChild)>, 32> Pending;
Richard Trieude5cc7d2013-01-31 01:44:26 +0000104
Richard Smith3a36ac12017-03-09 22:00:01 +0000105 /// Indicates whether we should trigger deserialization of nodes that had
106 /// not already been loaded.
107 bool Deserialize = false;
108
Richard Smithf7514452014-10-30 21:02:37 +0000109 /// Indicates whether we're at the top level.
Richard Smith3a36ac12017-03-09 22:00:01 +0000110 bool TopLevel = true;
Richard Trieude5cc7d2013-01-31 01:44:26 +0000111
Richard Smithf7514452014-10-30 21:02:37 +0000112 /// Indicates if we're handling the first child after entering a new depth.
Richard Smith3a36ac12017-03-09 22:00:01 +0000113 bool FirstChild = true;
Richard Smithf7514452014-10-30 21:02:37 +0000114
115 /// Prefix for currently-being-dumped entity.
116 std::string Prefix;
Richard Trieude5cc7d2013-01-31 01:44:26 +0000117
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000118 /// Keep track of the last location we print out so that we can
119 /// print out deltas from then on out.
Richard Smith3a36ac12017-03-09 22:00:01 +0000120 const char *LastLocFilename = "";
121 unsigned LastLocLine = ~0U;
Douglas Gregor7de59662009-05-29 20:38:28 +0000122
Alexander Kornienkoebc17b52013-01-14 14:07:11 +0000123 /// The \c FullComment parent of the comment being dumped.
Richard Smith3a36ac12017-03-09 22:00:01 +0000124 const FullComment *FC = nullptr;
Alexander Kornienkoebc17b52013-01-14 14:07:11 +0000125
Richard Trieud215b8d2013-01-26 01:31:20 +0000126 bool ShowColors;
127
Richard Smithf7514452014-10-30 21:02:37 +0000128 /// Dump a child of the current node.
129 template<typename Fn> void dumpChild(Fn doDumpChild) {
130 // If we're at the top level, there's nothing interesting to do; just
131 // run the dumper.
132 if (TopLevel) {
133 TopLevel = false;
134 doDumpChild();
135 while (!Pending.empty()) {
136 Pending.back()(true);
137 Pending.pop_back();
138 }
139 Prefix.clear();
140 OS << "\n";
141 TopLevel = true;
142 return;
Manuel Klimek874030e2012-11-07 00:33:12 +0000143 }
Richard Smithf7514452014-10-30 21:02:37 +0000144
145 const FullComment *OrigFC = FC;
146 auto dumpWithIndent = [this, doDumpChild, OrigFC](bool isLastChild) {
147 // Print out the appropriate tree structure and work out the prefix for
148 // children of this node. For instance:
149 //
150 // A Prefix = ""
151 // |-B Prefix = "| "
152 // | `-C Prefix = "| "
153 // `-D Prefix = " "
154 // |-E Prefix = " | "
155 // `-F Prefix = " "
156 // G Prefix = ""
157 //
158 // Note that the first level gets no prefix.
159 {
160 OS << '\n';
161 ColorScope Color(*this, IndentColor);
162 OS << Prefix << (isLastChild ? '`' : '|') << '-';
NAKAMURA Takumic73e4022014-10-31 00:30:37 +0000163 this->Prefix.push_back(isLastChild ? ' ' : '|');
164 this->Prefix.push_back(' ');
Richard Smithf7514452014-10-30 21:02:37 +0000165 }
166
167 FirstChild = true;
168 unsigned Depth = Pending.size();
169
170 FC = OrigFC;
171 doDumpChild();
172
173 // If any children are left, they're the last at their nesting level.
174 // Dump those ones out now.
175 while (Depth < Pending.size()) {
176 Pending.back()(true);
NAKAMURA Takumic73e4022014-10-31 00:30:37 +0000177 this->Pending.pop_back();
Richard Smithf7514452014-10-30 21:02:37 +0000178 }
179
180 // Restore the old prefix.
NAKAMURA Takumic73e4022014-10-31 00:30:37 +0000181 this->Prefix.resize(Prefix.size() - 2);
Richard Smithf7514452014-10-30 21:02:37 +0000182 };
183
184 if (FirstChild) {
185 Pending.push_back(std::move(dumpWithIndent));
186 } else {
187 Pending.back()(false);
188 Pending.back() = std::move(dumpWithIndent);
Manuel Klimek874030e2012-11-07 00:33:12 +0000189 }
Richard Smithf7514452014-10-30 21:02:37 +0000190 FirstChild = false;
191 }
Manuel Klimek874030e2012-11-07 00:33:12 +0000192
Richard Trieud215b8d2013-01-26 01:31:20 +0000193 class ColorScope {
194 ASTDumper &Dumper;
195 public:
196 ColorScope(ASTDumper &Dumper, TerminalColor Color)
197 : Dumper(Dumper) {
198 if (Dumper.ShowColors)
199 Dumper.OS.changeColor(Color.Color, Color.Bold);
200 }
201 ~ColorScope() {
202 if (Dumper.ShowColors)
203 Dumper.OS.resetColor();
204 }
205 };
206
Chris Lattnercbe4f772007-08-08 22:51:59 +0000207 public:
Alexander Kornienkoebc17b52013-01-14 14:07:11 +0000208 ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
209 const SourceManager *SM)
Richard Smith3a36ac12017-03-09 22:00:01 +0000210 : OS(OS), Traits(Traits), SM(SM),
Richard Trieud215b8d2013-01-26 01:31:20 +0000211 ShowColors(SM && SM->getDiagnostics().getShowColors()) { }
212
213 ASTDumper(raw_ostream &OS, const CommandTraits *Traits,
214 const SourceManager *SM, bool ShowColors)
Richard Smith3a36ac12017-03-09 22:00:01 +0000215 : OS(OS), Traits(Traits), SM(SM), ShowColors(ShowColors) {}
216
217 void setDeserialize(bool D) { Deserialize = D; }
Mike Stump11289f42009-09-09 15:08:12 +0000218
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000219 void dumpDecl(const Decl *D);
220 void dumpStmt(const Stmt *S);
Alexander Kornienkoebc17b52013-01-14 14:07:11 +0000221 void dumpFullComment(const FullComment *C);
Mike Stump11289f42009-09-09 15:08:12 +0000222
Richard Trieude5cc7d2013-01-31 01:44:26 +0000223 // Utilities
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000224 void dumpPointer(const void *Ptr);
225 void dumpSourceRange(SourceRange R);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000226 void dumpLocation(SourceLocation Loc);
Richard Smithd5e7ff82014-10-31 01:17:45 +0000227 void dumpBareType(QualType T, bool Desugar = true);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000228 void dumpType(QualType T);
Richard Smithd5e7ff82014-10-31 01:17:45 +0000229 void dumpTypeAsChild(QualType T);
230 void dumpTypeAsChild(const Type *T);
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000231 void dumpBareDeclRef(const Decl *Node);
Craig Topper36250ad2014-05-12 05:36:57 +0000232 void dumpDeclRef(const Decl *Node, const char *Label = nullptr);
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000233 void dumpName(const NamedDecl *D);
Richard Trieude5cc7d2013-01-31 01:44:26 +0000234 bool hasNodes(const DeclContext *DC);
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000235 void dumpDeclContext(const DeclContext *DC);
Richard Smith35f986d2014-08-11 22:11:07 +0000236 void dumpLookups(const DeclContext *DC, bool DumpDecls);
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000237 void dumpAttr(const Attr *A);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000238
239 // C++ Utilities
240 void dumpAccessSpecifier(AccessSpecifier AS);
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000241 void dumpCXXCtorInitializer(const CXXCtorInitializer *Init);
242 void dumpTemplateParameters(const TemplateParameterList *TPL);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000243 void dumpTemplateArgumentListInfo(const TemplateArgumentListInfo &TALI);
244 void dumpTemplateArgumentLoc(const TemplateArgumentLoc &A);
245 void dumpTemplateArgumentList(const TemplateArgumentList &TAL);
246 void dumpTemplateArgument(const TemplateArgument &A,
247 SourceRange R = SourceRange());
248
Douglas Gregor85f3f952015-07-07 03:57:15 +0000249 // Objective-C utilities.
250 void dumpObjCTypeParamList(const ObjCTypeParamList *typeParams);
251
Richard Smithd5e7ff82014-10-31 01:17:45 +0000252 // Types
253 void VisitComplexType(const ComplexType *T) {
254 dumpTypeAsChild(T->getElementType());
255 }
256 void VisitPointerType(const PointerType *T) {
257 dumpTypeAsChild(T->getPointeeType());
258 }
259 void VisitBlockPointerType(const BlockPointerType *T) {
260 dumpTypeAsChild(T->getPointeeType());
261 }
262 void VisitReferenceType(const ReferenceType *T) {
263 dumpTypeAsChild(T->getPointeeType());
264 }
265 void VisitRValueReferenceType(const ReferenceType *T) {
266 if (T->isSpelledAsLValue())
267 OS << " written as lvalue reference";
268 VisitReferenceType(T);
269 }
270 void VisitMemberPointerType(const MemberPointerType *T) {
271 dumpTypeAsChild(T->getClass());
272 dumpTypeAsChild(T->getPointeeType());
273 }
274 void VisitArrayType(const ArrayType *T) {
275 switch (T->getSizeModifier()) {
276 case ArrayType::Normal: break;
277 case ArrayType::Static: OS << " static"; break;
278 case ArrayType::Star: OS << " *"; break;
279 }
280 OS << " " << T->getIndexTypeQualifiers().getAsString();
281 dumpTypeAsChild(T->getElementType());
282 }
283 void VisitConstantArrayType(const ConstantArrayType *T) {
284 OS << " " << T->getSize();
285 VisitArrayType(T);
286 }
287 void VisitVariableArrayType(const VariableArrayType *T) {
288 OS << " ";
289 dumpSourceRange(T->getBracketsRange());
290 VisitArrayType(T);
291 dumpStmt(T->getSizeExpr());
292 }
293 void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
294 VisitArrayType(T);
295 OS << " ";
296 dumpSourceRange(T->getBracketsRange());
297 dumpStmt(T->getSizeExpr());
298 }
299 void VisitDependentSizedExtVectorType(
300 const DependentSizedExtVectorType *T) {
301 OS << " ";
302 dumpLocation(T->getAttributeLoc());
303 dumpTypeAsChild(T->getElementType());
304 dumpStmt(T->getSizeExpr());
305 }
306 void VisitVectorType(const VectorType *T) {
307 switch (T->getVectorKind()) {
308 case VectorType::GenericVector: break;
309 case VectorType::AltiVecVector: OS << " altivec"; break;
310 case VectorType::AltiVecPixel: OS << " altivec pixel"; break;
311 case VectorType::AltiVecBool: OS << " altivec bool"; break;
312 case VectorType::NeonVector: OS << " neon"; break;
313 case VectorType::NeonPolyVector: OS << " neon poly"; break;
314 }
315 OS << " " << T->getNumElements();
316 dumpTypeAsChild(T->getElementType());
317 }
318 void VisitFunctionType(const FunctionType *T) {
319 auto EI = T->getExtInfo();
320 if (EI.getNoReturn()) OS << " noreturn";
321 if (EI.getProducesResult()) OS << " produces_result";
322 if (EI.getHasRegParm()) OS << " regparm " << EI.getRegParm();
323 OS << " " << FunctionType::getNameForCallConv(EI.getCC());
324 dumpTypeAsChild(T->getReturnType());
325 }
326 void VisitFunctionProtoType(const FunctionProtoType *T) {
327 auto EPI = T->getExtProtoInfo();
328 if (EPI.HasTrailingReturn) OS << " trailing_return";
329 if (T->isConst()) OS << " const";
330 if (T->isVolatile()) OS << " volatile";
331 if (T->isRestrict()) OS << " restrict";
332 switch (EPI.RefQualifier) {
333 case RQ_None: break;
334 case RQ_LValue: OS << " &"; break;
335 case RQ_RValue: OS << " &&"; break;
336 }
337 // FIXME: Exception specification.
338 // FIXME: Consumed parameters.
339 VisitFunctionType(T);
340 for (QualType PT : T->getParamTypes())
341 dumpTypeAsChild(PT);
342 if (EPI.Variadic)
343 dumpChild([=] { OS << "..."; });
344 }
345 void VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
346 dumpDeclRef(T->getDecl());
347 }
348 void VisitTypedefType(const TypedefType *T) {
349 dumpDeclRef(T->getDecl());
350 }
351 void VisitTypeOfExprType(const TypeOfExprType *T) {
352 dumpStmt(T->getUnderlyingExpr());
353 }
354 void VisitDecltypeType(const DecltypeType *T) {
355 dumpStmt(T->getUnderlyingExpr());
356 }
357 void VisitUnaryTransformType(const UnaryTransformType *T) {
358 switch (T->getUTTKind()) {
359 case UnaryTransformType::EnumUnderlyingType:
360 OS << " underlying_type";
361 break;
362 }
363 dumpTypeAsChild(T->getBaseType());
364 }
365 void VisitTagType(const TagType *T) {
366 dumpDeclRef(T->getDecl());
367 }
368 void VisitAttributedType(const AttributedType *T) {
369 // FIXME: AttrKind
370 dumpTypeAsChild(T->getModifiedType());
371 }
372 void VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
373 OS << " depth " << T->getDepth() << " index " << T->getIndex();
374 if (T->isParameterPack()) OS << " pack";
375 dumpDeclRef(T->getDecl());
376 }
377 void VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) {
378 dumpTypeAsChild(T->getReplacedParameter());
379 }
380 void VisitSubstTemplateTypeParmPackType(
381 const SubstTemplateTypeParmPackType *T) {
382 dumpTypeAsChild(T->getReplacedParameter());
383 dumpTemplateArgument(T->getArgumentPack());
384 }
385 void VisitAutoType(const AutoType *T) {
386 if (T->isDecltypeAuto()) OS << " decltype(auto)";
387 if (!T->isDeduced())
388 OS << " undeduced";
389 }
390 void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
391 if (T->isTypeAlias()) OS << " alias";
392 OS << " "; T->getTemplateName().dump(OS);
393 for (auto &Arg : *T)
394 dumpTemplateArgument(Arg);
395 if (T->isTypeAlias())
396 dumpTypeAsChild(T->getAliasedType());
397 }
398 void VisitInjectedClassNameType(const InjectedClassNameType *T) {
399 dumpDeclRef(T->getDecl());
400 }
401 void VisitObjCInterfaceType(const ObjCInterfaceType *T) {
402 dumpDeclRef(T->getDecl());
403 }
404 void VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
405 dumpTypeAsChild(T->getPointeeType());
406 }
407 void VisitAtomicType(const AtomicType *T) {
408 dumpTypeAsChild(T->getValueType());
409 }
Xiuli Pan2d12e652016-05-03 05:37:07 +0000410 void VisitPipeType(const PipeType *T) {
411 dumpTypeAsChild(T->getElementType());
412 }
Richard Smithd5e7ff82014-10-31 01:17:45 +0000413 void VisitAdjustedType(const AdjustedType *T) {
414 dumpTypeAsChild(T->getOriginalType());
415 }
416 void VisitPackExpansionType(const PackExpansionType *T) {
417 if (auto N = T->getNumExpansions()) OS << " expansions " << *N;
418 if (!T->isSugared())
419 dumpTypeAsChild(T->getPattern());
420 }
421 // FIXME: ElaboratedType, DependentNameType,
422 // DependentTemplateSpecializationType, ObjCObjectType
423
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000424 // Decls
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000425 void VisitLabelDecl(const LabelDecl *D);
426 void VisitTypedefDecl(const TypedefDecl *D);
427 void VisitEnumDecl(const EnumDecl *D);
428 void VisitRecordDecl(const RecordDecl *D);
429 void VisitEnumConstantDecl(const EnumConstantDecl *D);
430 void VisitIndirectFieldDecl(const IndirectFieldDecl *D);
431 void VisitFunctionDecl(const FunctionDecl *D);
432 void VisitFieldDecl(const FieldDecl *D);
433 void VisitVarDecl(const VarDecl *D);
Richard Smithbdb84f32016-07-22 23:36:59 +0000434 void VisitDecompositionDecl(const DecompositionDecl *D);
Richard Smith7873de02016-08-11 22:25:46 +0000435 void VisitBindingDecl(const BindingDecl *D);
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000436 void VisitFileScopeAsmDecl(const FileScopeAsmDecl *D);
437 void VisitImportDecl(const ImportDecl *D);
Nico Weber66220292016-03-02 17:28:48 +0000438 void VisitPragmaCommentDecl(const PragmaCommentDecl *D);
Nico Webercbbaeb12016-03-02 19:28:54 +0000439 void VisitPragmaDetectMismatchDecl(const PragmaDetectMismatchDecl *D);
Alexey Bataev958b9e72016-03-31 09:30:50 +0000440 void VisitCapturedDecl(const CapturedDecl *D);
441
442 // OpenMP decls
443 void VisitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D);
444 void VisitOMPDeclareReductionDecl(const OMPDeclareReductionDecl *D);
445 void VisitOMPCapturedExprDecl(const OMPCapturedExprDecl *D);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000446
447 // C++ Decls
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000448 void VisitNamespaceDecl(const NamespaceDecl *D);
449 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D);
450 void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D);
451 void VisitTypeAliasDecl(const TypeAliasDecl *D);
452 void VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D);
453 void VisitCXXRecordDecl(const CXXRecordDecl *D);
454 void VisitStaticAssertDecl(const StaticAssertDecl *D);
Richard Smithcbdf7332014-03-18 02:07:28 +0000455 template<typename SpecializationDecl>
Richard Smithf7514452014-10-30 21:02:37 +0000456 void VisitTemplateDeclSpecialization(const SpecializationDecl *D,
Richard Smithcbdf7332014-03-18 02:07:28 +0000457 bool DumpExplicitInst,
458 bool DumpRefOnly);
Richard Smith20ade552014-03-17 23:34:53 +0000459 template<typename TemplateDecl>
460 void VisitTemplateDecl(const TemplateDecl *D, bool DumpExplicitInst);
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000461 void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D);
462 void VisitClassTemplateDecl(const ClassTemplateDecl *D);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000463 void VisitClassTemplateSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000464 const ClassTemplateSpecializationDecl *D);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000465 void VisitClassTemplatePartialSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000466 const ClassTemplatePartialSpecializationDecl *D);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000467 void VisitClassScopeFunctionSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000468 const ClassScopeFunctionSpecializationDecl *D);
David Majnemerd9b1a4f2015-11-04 03:40:30 +0000469 void VisitBuiltinTemplateDecl(const BuiltinTemplateDecl *D);
Richard Smithd25789a2013-09-18 01:36:02 +0000470 void VisitVarTemplateDecl(const VarTemplateDecl *D);
471 void VisitVarTemplateSpecializationDecl(
472 const VarTemplateSpecializationDecl *D);
473 void VisitVarTemplatePartialSpecializationDecl(
474 const VarTemplatePartialSpecializationDecl *D);
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000475 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D);
476 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D);
477 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D);
478 void VisitUsingDecl(const UsingDecl *D);
479 void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D);
480 void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D);
481 void VisitUsingShadowDecl(const UsingShadowDecl *D);
Richard Smith5179eb72016-06-28 19:03:57 +0000482 void VisitConstructorUsingShadowDecl(const ConstructorUsingShadowDecl *D);
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000483 void VisitLinkageSpecDecl(const LinkageSpecDecl *D);
484 void VisitAccessSpecDecl(const AccessSpecDecl *D);
485 void VisitFriendDecl(const FriendDecl *D);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000486
487 // ObjC Decls
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000488 void VisitObjCIvarDecl(const ObjCIvarDecl *D);
489 void VisitObjCMethodDecl(const ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000490 void VisitObjCTypeParamDecl(const ObjCTypeParamDecl *D);
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000491 void VisitObjCCategoryDecl(const ObjCCategoryDecl *D);
492 void VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D);
493 void VisitObjCProtocolDecl(const ObjCProtocolDecl *D);
494 void VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D);
495 void VisitObjCImplementationDecl(const ObjCImplementationDecl *D);
496 void VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D);
497 void VisitObjCPropertyDecl(const ObjCPropertyDecl *D);
498 void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D);
499 void VisitBlockDecl(const BlockDecl *D);
Mike Stump11289f42009-09-09 15:08:12 +0000500
Chris Lattner84ca3762007-08-30 01:00:35 +0000501 // Stmts.
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000502 void VisitStmt(const Stmt *Node);
503 void VisitDeclStmt(const DeclStmt *Node);
504 void VisitAttributedStmt(const AttributedStmt *Node);
505 void VisitLabelStmt(const LabelStmt *Node);
506 void VisitGotoStmt(const GotoStmt *Node);
Pavel Labath1ef83422013-09-04 14:35:00 +0000507 void VisitCXXCatchStmt(const CXXCatchStmt *Node);
Alexey Bataev958b9e72016-03-31 09:30:50 +0000508 void VisitCapturedStmt(const CapturedStmt *Node);
509
510 // OpenMP
511 void VisitOMPExecutableDirective(const OMPExecutableDirective *Node);
Mike Stump11289f42009-09-09 15:08:12 +0000512
Chris Lattner84ca3762007-08-30 01:00:35 +0000513 // Exprs
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000514 void VisitExpr(const Expr *Node);
515 void VisitCastExpr(const CastExpr *Node);
516 void VisitDeclRefExpr(const DeclRefExpr *Node);
517 void VisitPredefinedExpr(const PredefinedExpr *Node);
518 void VisitCharacterLiteral(const CharacterLiteral *Node);
519 void VisitIntegerLiteral(const IntegerLiteral *Node);
520 void VisitFloatingLiteral(const FloatingLiteral *Node);
521 void VisitStringLiteral(const StringLiteral *Str);
Richard Smithf0514962014-06-03 08:24:28 +0000522 void VisitInitListExpr(const InitListExpr *ILE);
Richard Smith410306b2016-12-12 02:53:20 +0000523 void VisitArrayInitLoopExpr(const ArrayInitLoopExpr *ILE);
524 void VisitArrayInitIndexExpr(const ArrayInitIndexExpr *ILE);
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000525 void VisitUnaryOperator(const UnaryOperator *Node);
526 void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Node);
527 void VisitMemberExpr(const MemberExpr *Node);
528 void VisitExtVectorElementExpr(const ExtVectorElementExpr *Node);
529 void VisitBinaryOperator(const BinaryOperator *Node);
530 void VisitCompoundAssignOperator(const CompoundAssignOperator *Node);
531 void VisitAddrLabelExpr(const AddrLabelExpr *Node);
532 void VisitBlockExpr(const BlockExpr *Node);
533 void VisitOpaqueValueExpr(const OpaqueValueExpr *Node);
Chris Lattner84ca3762007-08-30 01:00:35 +0000534
535 // C++
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000536 void VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node);
537 void VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node);
538 void VisitCXXThisExpr(const CXXThisExpr *Node);
539 void VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node);
Richard Smith39eca9b2017-08-23 22:12:08 +0000540 void VisitCXXUnresolvedConstructExpr(const CXXUnresolvedConstructExpr *Node);
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000541 void VisitCXXConstructExpr(const CXXConstructExpr *Node);
542 void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node);
Reid Kleckner5c682bc2015-03-19 18:09:25 +0000543 void VisitCXXNewExpr(const CXXNewExpr *Node);
544 void VisitCXXDeleteExpr(const CXXDeleteExpr *Node);
Richard Smithe6c01442013-06-05 00:46:14 +0000545 void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node);
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000546 void VisitExprWithCleanups(const ExprWithCleanups *Node);
547 void VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node);
548 void dumpCXXTemporary(const CXXTemporary *Temporary);
Faisal Vali2b391ab2013-09-26 19:54:12 +0000549 void VisitLambdaExpr(const LambdaExpr *Node) {
550 VisitExpr(Node);
551 dumpDecl(Node->getLambdaClass());
552 }
Serge Pavlov6b926032015-02-16 19:58:41 +0000553 void VisitSizeOfPackExpr(const SizeOfPackExpr *Node);
Alex Lorenzddbe0f52016-11-09 14:02:18 +0000554 void
555 VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *Node);
Mike Stump11289f42009-09-09 15:08:12 +0000556
Chris Lattner84ca3762007-08-30 01:00:35 +0000557 // ObjC
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000558 void VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node);
559 void VisitObjCEncodeExpr(const ObjCEncodeExpr *Node);
560 void VisitObjCMessageExpr(const ObjCMessageExpr *Node);
561 void VisitObjCBoxedExpr(const ObjCBoxedExpr *Node);
562 void VisitObjCSelectorExpr(const ObjCSelectorExpr *Node);
563 void VisitObjCProtocolExpr(const ObjCProtocolExpr *Node);
564 void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node);
565 void VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node);
566 void VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node);
567 void VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node);
Alexander Kornienkoebc17b52013-01-14 14:07:11 +0000568
569 // Comments.
570 const char *getCommandName(unsigned CommandID);
571 void dumpComment(const Comment *C);
572
573 // Inline comments.
574 void visitTextComment(const TextComment *C);
575 void visitInlineCommandComment(const InlineCommandComment *C);
576 void visitHTMLStartTagComment(const HTMLStartTagComment *C);
577 void visitHTMLEndTagComment(const HTMLEndTagComment *C);
578
579 // Block comments.
580 void visitBlockCommandComment(const BlockCommandComment *C);
581 void visitParamCommandComment(const ParamCommandComment *C);
582 void visitTParamCommandComment(const TParamCommandComment *C);
583 void visitVerbatimBlockComment(const VerbatimBlockComment *C);
584 void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C);
585 void visitVerbatimLineComment(const VerbatimLineComment *C);
Chris Lattnercbe4f772007-08-08 22:51:59 +0000586 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000587}
Chris Lattnercbe4f772007-08-08 22:51:59 +0000588
589//===----------------------------------------------------------------------===//
Chris Lattner11e30d32007-08-30 06:17:34 +0000590// Utilities
591//===----------------------------------------------------------------------===//
592
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000593void ASTDumper::dumpPointer(const void *Ptr) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000594 ColorScope Color(*this, AddressColor);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000595 OS << ' ' << Ptr;
596}
597
Alexander Kornienko18ec81b2012-12-13 13:59:55 +0000598void ASTDumper::dumpLocation(SourceLocation Loc) {
Alex McCarthye14ddef2014-05-02 20:24:11 +0000599 if (!SM)
600 return;
601
Richard Trieud215b8d2013-01-26 01:31:20 +0000602 ColorScope Color(*this, LocationColor);
Chris Lattner53e384f2009-01-16 07:00:02 +0000603 SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000604
Chris Lattner11e30d32007-08-30 06:17:34 +0000605 // The general format we print out is filename:line:col, but we drop pieces
606 // that haven't changed since the last loc printed.
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000607 PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
608
Douglas Gregor453b0122010-11-12 07:15:47 +0000609 if (PLoc.isInvalid()) {
610 OS << "<invalid sloc>";
611 return;
612 }
613
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000614 if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000615 OS << PLoc.getFilename() << ':' << PLoc.getLine()
616 << ':' << PLoc.getColumn();
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000617 LastLocFilename = PLoc.getFilename();
618 LastLocLine = PLoc.getLine();
619 } else if (PLoc.getLine() != LastLocLine) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000620 OS << "line" << ':' << PLoc.getLine()
621 << ':' << PLoc.getColumn();
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000622 LastLocLine = PLoc.getLine();
Chris Lattner11e30d32007-08-30 06:17:34 +0000623 } else {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000624 OS << "col" << ':' << PLoc.getColumn();
Chris Lattner11e30d32007-08-30 06:17:34 +0000625 }
626}
627
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000628void ASTDumper::dumpSourceRange(SourceRange R) {
Chris Lattner11e30d32007-08-30 06:17:34 +0000629 // Can't translate locations if a SourceManager isn't available.
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000630 if (!SM)
631 return;
Mike Stump11289f42009-09-09 15:08:12 +0000632
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000633 OS << " <";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000634 dumpLocation(R.getBegin());
Chris Lattnera7c19fe2007-10-16 22:36:42 +0000635 if (R.getBegin() != R.getEnd()) {
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000636 OS << ", ";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000637 dumpLocation(R.getEnd());
Chris Lattner11e30d32007-08-30 06:17:34 +0000638 }
Daniel Dunbar34a96c82009-12-03 09:13:13 +0000639 OS << ">";
Mike Stump11289f42009-09-09 15:08:12 +0000640
Chris Lattner11e30d32007-08-30 06:17:34 +0000641 // <t2.c:123:421[blah], t2.c:412:321>
642
643}
644
Richard Smithd5e7ff82014-10-31 01:17:45 +0000645void ASTDumper::dumpBareType(QualType T, bool Desugar) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000646 ColorScope Color(*this, TypeColor);
Richard Smithd5e7ff82014-10-31 01:17:45 +0000647
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000648 SplitQualType T_split = T.split();
649 OS << "'" << QualType::getAsString(T_split) << "'";
650
Richard Smithd5e7ff82014-10-31 01:17:45 +0000651 if (Desugar && !T.isNull()) {
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000652 // If the type is sugared, also dump a (shallow) desugared type.
653 SplitQualType D_split = T.getSplitDesugaredType();
654 if (T_split != D_split)
655 OS << ":'" << QualType::getAsString(D_split) << "'";
656 }
657}
658
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000659void ASTDumper::dumpType(QualType T) {
660 OS << ' ';
661 dumpBareType(T);
662}
663
Richard Smithd5e7ff82014-10-31 01:17:45 +0000664void ASTDumper::dumpTypeAsChild(QualType T) {
665 SplitQualType SQT = T.split();
666 if (!SQT.Quals.hasQualifiers())
667 return dumpTypeAsChild(SQT.Ty);
668
669 dumpChild([=] {
670 OS << "QualType";
671 dumpPointer(T.getAsOpaquePtr());
672 OS << " ";
673 dumpBareType(T, false);
674 OS << " " << T.split().Quals.getAsString();
675 dumpTypeAsChild(T.split().Ty);
676 });
677}
678
679void ASTDumper::dumpTypeAsChild(const Type *T) {
680 dumpChild([=] {
681 if (!T) {
682 ColorScope Color(*this, NullColor);
683 OS << "<<<NULL>>>";
684 return;
685 }
Serge Pavlova6adc9e2015-12-28 17:19:12 +0000686 if (const LocInfoType *LIT = llvm::dyn_cast<LocInfoType>(T)) {
687 {
688 ColorScope Color(*this, TypeColor);
689 OS << "LocInfo Type";
690 }
691 dumpPointer(T);
692 dumpTypeAsChild(LIT->getTypeSourceInfo()->getType());
693 return;
694 }
Richard Smithd5e7ff82014-10-31 01:17:45 +0000695
696 {
697 ColorScope Color(*this, TypeColor);
698 OS << T->getTypeClassName() << "Type";
699 }
700 dumpPointer(T);
701 OS << " ";
702 dumpBareType(QualType(T, 0), false);
703
704 QualType SingleStepDesugar =
705 T->getLocallyUnqualifiedSingleStepDesugaredType();
706 if (SingleStepDesugar != QualType(T, 0))
707 OS << " sugar";
708 if (T->isDependentType())
709 OS << " dependent";
710 else if (T->isInstantiationDependentType())
711 OS << " instantiation_dependent";
712 if (T->isVariablyModifiedType())
713 OS << " variably_modified";
714 if (T->containsUnexpandedParameterPack())
715 OS << " contains_unexpanded_pack";
716 if (T->isFromAST())
717 OS << " imported";
718
719 TypeVisitor<ASTDumper>::Visit(T);
720
721 if (SingleStepDesugar != QualType(T, 0))
722 dumpTypeAsChild(SingleStepDesugar);
723 });
724}
725
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000726void ASTDumper::dumpBareDeclRef(const Decl *D) {
Richard Smith5179eb72016-06-28 19:03:57 +0000727 if (!D) {
728 ColorScope Color(*this, NullColor);
729 OS << "<<<NULL>>>";
730 return;
731 }
732
Richard Trieud215b8d2013-01-26 01:31:20 +0000733 {
734 ColorScope Color(*this, DeclKindNameColor);
735 OS << D->getDeclKindName();
736 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000737 dumpPointer(D);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000738
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000739 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000740 ColorScope Color(*this, DeclNameColor);
David Blaikied4da8722013-05-14 21:04:00 +0000741 OS << " '" << ND->getDeclName() << '\'';
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000742 }
743
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000744 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
Alexander Kornienko61c93bd2012-12-11 15:28:09 +0000745 dumpType(VD->getType());
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000746}
747
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000748void ASTDumper::dumpDeclRef(const Decl *D, const char *Label) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000749 if (!D)
750 return;
751
Richard Smithf7514452014-10-30 21:02:37 +0000752 dumpChild([=]{
753 if (Label)
754 OS << Label << ' ';
755 dumpBareDeclRef(D);
756 });
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000757}
758
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000759void ASTDumper::dumpName(const NamedDecl *ND) {
Richard Trieud215b8d2013-01-26 01:31:20 +0000760 if (ND->getDeclName()) {
761 ColorScope Color(*this, DeclNameColor);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000762 OS << ' ' << ND->getNameAsString();
Richard Trieud215b8d2013-01-26 01:31:20 +0000763 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000764}
765
Richard Trieude5cc7d2013-01-31 01:44:26 +0000766bool ASTDumper::hasNodes(const DeclContext *DC) {
767 if (!DC)
768 return false;
769
Richard Smith1d209d02013-05-23 01:49:11 +0000770 return DC->hasExternalLexicalStorage() ||
Richard Smith3a36ac12017-03-09 22:00:01 +0000771 (Deserialize ? DC->decls_begin() != DC->decls_end()
772 : DC->noload_decls_begin() != DC->noload_decls_end());
Richard Trieude5cc7d2013-01-31 01:44:26 +0000773}
774
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000775void ASTDumper::dumpDeclContext(const DeclContext *DC) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000776 if (!DC)
777 return;
Richard Smithdcc2c452014-03-17 23:00:06 +0000778
Richard Smith3a36ac12017-03-09 22:00:01 +0000779 for (auto *D : (Deserialize ? DC->decls() : DC->noload_decls()))
Richard Smithf7514452014-10-30 21:02:37 +0000780 dumpDecl(D);
Richard Smithdcc2c452014-03-17 23:00:06 +0000781
782 if (DC->hasExternalLexicalStorage()) {
Richard Smithf7514452014-10-30 21:02:37 +0000783 dumpChild([=]{
784 ColorScope Color(*this, UndeserializedColor);
785 OS << "<undeserialized declarations>";
786 });
Richard Smith1d209d02013-05-23 01:49:11 +0000787 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000788}
789
Richard Smith35f986d2014-08-11 22:11:07 +0000790void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) {
Richard Smithf7514452014-10-30 21:02:37 +0000791 dumpChild([=] {
792 OS << "StoredDeclsMap ";
793 dumpBareDeclRef(cast<Decl>(DC));
Richard Smith33937e72013-06-22 21:49:40 +0000794
Richard Smithf7514452014-10-30 21:02:37 +0000795 const DeclContext *Primary = DC->getPrimaryContext();
796 if (Primary != DC) {
797 OS << " primary";
798 dumpPointer(cast<Decl>(Primary));
Richard Smith33937e72013-06-22 21:49:40 +0000799 }
800
Richard Smithf7514452014-10-30 21:02:37 +0000801 bool HasUndeserializedLookups = Primary->hasExternalVisibleStorage();
Richard Smith35f986d2014-08-11 22:11:07 +0000802
Richard Smith3a36ac12017-03-09 22:00:01 +0000803 for (auto I = Deserialize ? Primary->lookups_begin()
804 : Primary->noload_lookups_begin(),
805 E = Deserialize ? Primary->lookups_end()
806 : Primary->noload_lookups_end();
807 I != E; ++I) {
Richard Smithf7514452014-10-30 21:02:37 +0000808 DeclarationName Name = I.getLookupName();
Richard Smith3a36ac12017-03-09 22:00:01 +0000809 DeclContextLookupResult R = *I;
Richard Smith35f986d2014-08-11 22:11:07 +0000810
Richard Smithf7514452014-10-30 21:02:37 +0000811 dumpChild([=] {
812 OS << "DeclarationName ";
813 {
814 ColorScope Color(*this, DeclNameColor);
815 OS << '\'' << Name << '\'';
816 }
Richard Smith35f986d2014-08-11 22:11:07 +0000817
Richard Smithf7514452014-10-30 21:02:37 +0000818 for (DeclContextLookupResult::iterator RI = R.begin(), RE = R.end();
819 RI != RE; ++RI) {
820 dumpChild([=] {
821 dumpBareDeclRef(*RI);
822
823 if ((*RI)->isHidden())
824 OS << " hidden";
825
826 // If requested, dump the redecl chain for this lookup.
827 if (DumpDecls) {
828 // Dump earliest decl first.
829 std::function<void(Decl *)> DumpWithPrev = [&](Decl *D) {
830 if (Decl *Prev = D->getPreviousDecl())
831 DumpWithPrev(Prev);
832 dumpDecl(D);
833 };
834 DumpWithPrev(*RI);
835 }
836 });
837 }
838 });
Richard Smith33937e72013-06-22 21:49:40 +0000839 }
Richard Smith33937e72013-06-22 21:49:40 +0000840
Richard Smithf7514452014-10-30 21:02:37 +0000841 if (HasUndeserializedLookups) {
842 dumpChild([=] {
843 ColorScope Color(*this, UndeserializedColor);
844 OS << "<undeserialized lookups>";
845 });
846 }
847 });
Richard Smith33937e72013-06-22 21:49:40 +0000848}
849
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000850void ASTDumper::dumpAttr(const Attr *A) {
Richard Smithf7514452014-10-30 21:02:37 +0000851 dumpChild([=] {
852 {
853 ColorScope Color(*this, AttrColor);
Aaron Ballman36a53502014-01-16 13:03:14 +0000854
Richard Smithf7514452014-10-30 21:02:37 +0000855 switch (A->getKind()) {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000856#define ATTR(X) case attr::X: OS << #X; break;
857#include "clang/Basic/AttrList.inc"
Richard Smithf7514452014-10-30 21:02:37 +0000858 }
859 OS << "Attr";
Richard Trieud215b8d2013-01-26 01:31:20 +0000860 }
Richard Smithf7514452014-10-30 21:02:37 +0000861 dumpPointer(A);
862 dumpSourceRange(A->getRange());
863 if (A->isInherited())
864 OS << " Inherited";
865 if (A->isImplicit())
866 OS << " Implicit";
Hans Wennborgb0a8b4a2014-05-31 04:05:57 +0000867#include "clang/AST/AttrDump.inc"
Richard Smithf7514452014-10-30 21:02:37 +0000868 });
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000869}
870
Richard Smith71bec062013-10-15 21:58:30 +0000871static void dumpPreviousDeclImpl(raw_ostream &OS, ...) {}
872
873template<typename T>
874static void dumpPreviousDeclImpl(raw_ostream &OS, const Mergeable<T> *D) {
Rafael Espindola8db352d2013-10-17 15:37:26 +0000875 const T *First = D->getFirstDecl();
Richard Smith71bec062013-10-15 21:58:30 +0000876 if (First != D)
877 OS << " first " << First;
Richard Smithf5f43542013-02-07 01:35:44 +0000878}
879
880template<typename T>
Richard Smith71bec062013-10-15 21:58:30 +0000881static void dumpPreviousDeclImpl(raw_ostream &OS, const Redeclarable<T> *D) {
882 const T *Prev = D->getPreviousDecl();
883 if (Prev)
884 OS << " prev " << Prev;
Richard Smithf5f43542013-02-07 01:35:44 +0000885}
886
Richard Smith71bec062013-10-15 21:58:30 +0000887/// Dump the previous declaration in the redeclaration chain for a declaration,
888/// if any.
889static void dumpPreviousDecl(raw_ostream &OS, const Decl *D) {
Richard Smithf5f43542013-02-07 01:35:44 +0000890 switch (D->getKind()) {
891#define DECL(DERIVED, BASE) \
892 case Decl::DERIVED: \
Richard Smith71bec062013-10-15 21:58:30 +0000893 return dumpPreviousDeclImpl(OS, cast<DERIVED##Decl>(D));
Richard Smithf5f43542013-02-07 01:35:44 +0000894#define ABSTRACT_DECL(DECL)
895#include "clang/AST/DeclNodes.inc"
896 }
897 llvm_unreachable("Decl that isn't part of DeclNodes.inc!");
898}
899
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000900//===----------------------------------------------------------------------===//
901// C++ Utilities
902//===----------------------------------------------------------------------===//
903
904void ASTDumper::dumpAccessSpecifier(AccessSpecifier AS) {
905 switch (AS) {
906 case AS_none:
907 break;
908 case AS_public:
909 OS << "public";
910 break;
911 case AS_protected:
912 OS << "protected";
913 break;
914 case AS_private:
915 OS << "private";
916 break;
917 }
918}
919
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000920void ASTDumper::dumpCXXCtorInitializer(const CXXCtorInitializer *Init) {
Richard Smithf7514452014-10-30 21:02:37 +0000921 dumpChild([=] {
922 OS << "CXXCtorInitializer";
923 if (Init->isAnyMemberInitializer()) {
924 OS << ' ';
925 dumpBareDeclRef(Init->getAnyMember());
926 } else if (Init->isBaseInitializer()) {
927 dumpType(QualType(Init->getBaseClass(), 0));
928 } else if (Init->isDelegatingInitializer()) {
929 dumpType(Init->getTypeSourceInfo()->getType());
930 } else {
931 llvm_unreachable("Unknown initializer type");
932 }
933 dumpStmt(Init->getInit());
934 });
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000935}
936
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000937void ASTDumper::dumpTemplateParameters(const TemplateParameterList *TPL) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000938 if (!TPL)
939 return;
940
Alexander Kornienko787f4c32012-12-20 11:08:38 +0000941 for (TemplateParameterList::const_iterator I = TPL->begin(), E = TPL->end();
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000942 I != E; ++I)
943 dumpDecl(*I);
944}
945
946void ASTDumper::dumpTemplateArgumentListInfo(
947 const TemplateArgumentListInfo &TALI) {
Richard Smithf7514452014-10-30 21:02:37 +0000948 for (unsigned i = 0, e = TALI.size(); i < e; ++i)
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000949 dumpTemplateArgumentLoc(TALI[i]);
950}
951
952void ASTDumper::dumpTemplateArgumentLoc(const TemplateArgumentLoc &A) {
953 dumpTemplateArgument(A.getArgument(), A.getSourceRange());
954}
955
956void ASTDumper::dumpTemplateArgumentList(const TemplateArgumentList &TAL) {
957 for (unsigned i = 0, e = TAL.size(); i < e; ++i)
958 dumpTemplateArgument(TAL[i]);
959}
960
961void ASTDumper::dumpTemplateArgument(const TemplateArgument &A, SourceRange R) {
Richard Smithf7514452014-10-30 21:02:37 +0000962 dumpChild([=] {
963 OS << "TemplateArgument";
964 if (R.isValid())
965 dumpSourceRange(R);
Alexander Kornienko90ff6072012-12-20 02:09:13 +0000966
Richard Smithf7514452014-10-30 21:02:37 +0000967 switch (A.getKind()) {
968 case TemplateArgument::Null:
969 OS << " null";
970 break;
971 case TemplateArgument::Type:
972 OS << " type";
973 dumpType(A.getAsType());
974 break;
975 case TemplateArgument::Declaration:
976 OS << " decl";
977 dumpDeclRef(A.getAsDecl());
978 break;
979 case TemplateArgument::NullPtr:
980 OS << " nullptr";
981 break;
982 case TemplateArgument::Integral:
983 OS << " integral " << A.getAsIntegral();
984 break;
985 case TemplateArgument::Template:
986 OS << " template ";
987 A.getAsTemplate().dump(OS);
988 break;
989 case TemplateArgument::TemplateExpansion:
990 OS << " template expansion";
991 A.getAsTemplateOrTemplatePattern().dump(OS);
992 break;
993 case TemplateArgument::Expression:
994 OS << " expr";
995 dumpStmt(A.getAsExpr());
996 break;
997 case TemplateArgument::Pack:
998 OS << " pack";
999 for (TemplateArgument::pack_iterator I = A.pack_begin(), E = A.pack_end();
1000 I != E; ++I)
1001 dumpTemplateArgument(*I);
1002 break;
Richard Trieude5cc7d2013-01-31 01:44:26 +00001003 }
Richard Smithf7514452014-10-30 21:02:37 +00001004 });
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001005}
1006
Chris Lattner11e30d32007-08-30 06:17:34 +00001007//===----------------------------------------------------------------------===//
Douglas Gregor85f3f952015-07-07 03:57:15 +00001008// Objective-C Utilities
1009//===----------------------------------------------------------------------===//
1010void ASTDumper::dumpObjCTypeParamList(const ObjCTypeParamList *typeParams) {
1011 if (!typeParams)
1012 return;
1013
1014 for (auto typeParam : *typeParams) {
1015 dumpDecl(typeParam);
1016 }
1017}
1018
1019//===----------------------------------------------------------------------===//
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001020// Decl dumping methods.
Chris Lattnercbe4f772007-08-08 22:51:59 +00001021//===----------------------------------------------------------------------===//
1022
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001023void ASTDumper::dumpDecl(const Decl *D) {
Richard Smithf7514452014-10-30 21:02:37 +00001024 dumpChild([=] {
1025 if (!D) {
1026 ColorScope Color(*this, NullColor);
1027 OS << "<<<NULL>>>";
1028 return;
1029 }
Mike Stump11289f42009-09-09 15:08:12 +00001030
Richard Smithf7514452014-10-30 21:02:37 +00001031 {
1032 ColorScope Color(*this, DeclKindNameColor);
1033 OS << D->getDeclKindName() << "Decl";
1034 }
1035 dumpPointer(D);
1036 if (D->getLexicalDeclContext() != D->getDeclContext())
1037 OS << " parent " << cast<Decl>(D->getDeclContext());
1038 dumpPreviousDecl(OS, D);
1039 dumpSourceRange(D->getSourceRange());
1040 OS << ' ';
1041 dumpLocation(D->getLocation());
Richard Smith26342f92017-05-17 00:24:14 +00001042 if (D->isFromASTFile())
1043 OS << " imported";
1044 if (Module *M = D->getOwningModule())
Richard Smithf7514452014-10-30 21:02:37 +00001045 OS << " in " << M->getFullModuleName();
Richard Smitha2eb4092015-06-22 18:47:01 +00001046 if (auto *ND = dyn_cast<NamedDecl>(D))
1047 for (Module *M : D->getASTContext().getModulesWithMergedDefinition(
1048 const_cast<NamedDecl *>(ND)))
1049 dumpChild([=] { OS << "also in " << M->getFullModuleName(); });
Richard Smithf7514452014-10-30 21:02:37 +00001050 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
1051 if (ND->isHidden())
1052 OS << " hidden";
1053 if (D->isImplicit())
1054 OS << " implicit";
1055 if (D->isUsed())
1056 OS << " used";
1057 else if (D->isThisDeclarationReferenced())
1058 OS << " referenced";
1059 if (D->isInvalidDecl())
1060 OS << " invalid";
Hans Wennborg76b00532014-12-05 22:38:57 +00001061 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1062 if (FD->isConstexpr())
1063 OS << " constexpr";
1064
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001065
Richard Smithf7514452014-10-30 21:02:37 +00001066 ConstDeclVisitor<ASTDumper>::Visit(D);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001067
Richard Smithf7514452014-10-30 21:02:37 +00001068 for (Decl::attr_iterator I = D->attr_begin(), E = D->attr_end(); I != E;
1069 ++I)
1070 dumpAttr(*I);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001071
Richard Smithf7514452014-10-30 21:02:37 +00001072 if (const FullComment *Comment =
1073 D->getASTContext().getLocalCommentForDeclUncached(D))
1074 dumpFullComment(Comment);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001075
Richard Smithf7514452014-10-30 21:02:37 +00001076 // Decls within functions are visited by the body.
1077 if (!isa<FunctionDecl>(*D) && !isa<ObjCMethodDecl>(*D) &&
1078 hasNodes(dyn_cast<DeclContext>(D)))
1079 dumpDeclContext(cast<DeclContext>(D));
1080 });
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001081}
1082
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001083void ASTDumper::VisitLabelDecl(const LabelDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001084 dumpName(D);
1085}
1086
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001087void ASTDumper::VisitTypedefDecl(const TypedefDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001088 dumpName(D);
1089 dumpType(D->getUnderlyingType());
1090 if (D->isModulePrivate())
1091 OS << " __module_private__";
Richard Smithba3a4f92016-01-12 21:59:26 +00001092 dumpTypeAsChild(D->getUnderlyingType());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001093}
1094
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001095void ASTDumper::VisitEnumDecl(const EnumDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001096 if (D->isScoped()) {
1097 if (D->isScopedUsingClassTag())
1098 OS << " class";
1099 else
1100 OS << " struct";
1101 }
1102 dumpName(D);
1103 if (D->isModulePrivate())
1104 OS << " __module_private__";
1105 if (D->isFixed())
1106 dumpType(D->getIntegerType());
1107}
1108
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001109void ASTDumper::VisitRecordDecl(const RecordDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001110 OS << ' ' << D->getKindName();
1111 dumpName(D);
1112 if (D->isModulePrivate())
1113 OS << " __module_private__";
Richard Smith99bc1b92013-08-30 05:32:29 +00001114 if (D->isCompleteDefinition())
1115 OS << " definition";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001116}
1117
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001118void ASTDumper::VisitEnumConstantDecl(const EnumConstantDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001119 dumpName(D);
1120 dumpType(D->getType());
Richard Smithf7514452014-10-30 21:02:37 +00001121 if (const Expr *Init = D->getInitExpr())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001122 dumpStmt(Init);
1123}
1124
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001125void ASTDumper::VisitIndirectFieldDecl(const IndirectFieldDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001126 dumpName(D);
1127 dumpType(D->getType());
Richard Smithdcc2c452014-03-17 23:00:06 +00001128
Richard Smith8aa49222014-03-18 00:35:12 +00001129 for (auto *Child : D->chain())
Richard Smithf7514452014-10-30 21:02:37 +00001130 dumpDeclRef(Child);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001131}
1132
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001133void ASTDumper::VisitFunctionDecl(const FunctionDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001134 dumpName(D);
1135 dumpType(D->getType());
1136
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001137 StorageClass SC = D->getStorageClass();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001138 if (SC != SC_None)
1139 OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
1140 if (D->isInlineSpecified())
1141 OS << " inline";
1142 if (D->isVirtualAsWritten())
1143 OS << " virtual";
1144 if (D->isModulePrivate())
1145 OS << " __module_private__";
1146
1147 if (D->isPure())
1148 OS << " pure";
Richard Smith5a2e6b92016-11-21 23:43:54 +00001149 if (D->isDefaulted()) {
1150 OS << " default";
1151 if (D->isDeleted())
1152 OS << "_delete";
1153 }
1154 if (D->isDeletedAsWritten())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001155 OS << " delete";
Richard Smith5a2e6b92016-11-21 23:43:54 +00001156 if (D->isTrivial())
1157 OS << " trivial";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001158
Richard Smithadaa0152013-05-17 02:09:46 +00001159 if (const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>()) {
1160 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
Richard Smith8acb4282014-07-31 21:57:55 +00001161 switch (EPI.ExceptionSpec.Type) {
Richard Smithadaa0152013-05-17 02:09:46 +00001162 default: break;
1163 case EST_Unevaluated:
Richard Smith8acb4282014-07-31 21:57:55 +00001164 OS << " noexcept-unevaluated " << EPI.ExceptionSpec.SourceDecl;
Richard Smithadaa0152013-05-17 02:09:46 +00001165 break;
1166 case EST_Uninstantiated:
Richard Smith8acb4282014-07-31 21:57:55 +00001167 OS << " noexcept-uninstantiated " << EPI.ExceptionSpec.SourceTemplate;
Richard Smithadaa0152013-05-17 02:09:46 +00001168 break;
1169 }
1170 }
1171
Richard Smithf7514452014-10-30 21:02:37 +00001172 if (const FunctionTemplateSpecializationInfo *FTSI =
1173 D->getTemplateSpecializationInfo())
Richard Trieude5cc7d2013-01-31 01:44:26 +00001174 dumpTemplateArgumentList(*FTSI->TemplateArguments);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001175
Richard Smith8a639892015-01-24 01:07:20 +00001176 if (!D->param_begin() && D->getNumParams())
1177 dumpChild([=] { OS << "<<NULL params x " << D->getNumParams() << ">>"; });
1178 else
David Majnemera3debed2016-06-24 05:33:44 +00001179 for (const ParmVarDecl *Parameter : D->parameters())
1180 dumpDecl(Parameter);
Richard Smithf7514452014-10-30 21:02:37 +00001181
1182 if (const CXXConstructorDecl *C = dyn_cast<CXXConstructorDecl>(D))
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001183 for (CXXConstructorDecl::init_const_iterator I = C->init_begin(),
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001184 E = C->init_end();
Richard Smithf7514452014-10-30 21:02:37 +00001185 I != E; ++I)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001186 dumpCXXCtorInitializer(*I);
1187
Lenar Safin9ae21552017-07-29 20:42:58 +00001188 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
Lang Hames19e07e12017-06-20 21:06:00 +00001189 if (MD->size_overridden_methods() != 0) {
1190 auto dumpOverride =
1191 [=](const CXXMethodDecl *D) {
1192 SplitQualType T_split = D->getType().split();
Lang Hames971add52017-07-13 21:08:29 +00001193 OS << D << " " << D->getParent()->getName() << "::"
1194 << D->getNameAsString() << " '" << QualType::getAsString(T_split) << "'";
Lang Hames19e07e12017-06-20 21:06:00 +00001195 };
1196
1197 dumpChild([=] {
1198 auto FirstOverrideItr = MD->begin_overridden_methods();
1199 OS << "Overrides: [ ";
1200 dumpOverride(*FirstOverrideItr);
1201 for (const auto *Override :
1202 llvm::make_range(FirstOverrideItr + 1,
Lenar Safin9ae21552017-07-29 20:42:58 +00001203 MD->end_overridden_methods())) {
1204 OS << ", ";
Lang Hames19e07e12017-06-20 21:06:00 +00001205 dumpOverride(Override);
Lenar Safin9ae21552017-07-29 20:42:58 +00001206 }
Lang Hames19e07e12017-06-20 21:06:00 +00001207 OS << " ]";
1208 });
1209 }
Lenar Safin9ae21552017-07-29 20:42:58 +00001210 }
Lang Hames19e07e12017-06-20 21:06:00 +00001211
Richard Smithf7514452014-10-30 21:02:37 +00001212 if (D->doesThisDeclarationHaveABody())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001213 dumpStmt(D->getBody());
1214}
1215
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001216void ASTDumper::VisitFieldDecl(const FieldDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001217 dumpName(D);
1218 dumpType(D->getType());
1219 if (D->isMutable())
1220 OS << " mutable";
1221 if (D->isModulePrivate())
1222 OS << " __module_private__";
Richard Trieude5cc7d2013-01-31 01:44:26 +00001223
Richard Smithf7514452014-10-30 21:02:37 +00001224 if (D->isBitField())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001225 dumpStmt(D->getBitWidth());
Richard Smithf7514452014-10-30 21:02:37 +00001226 if (Expr *Init = D->getInClassInitializer())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001227 dumpStmt(Init);
1228}
1229
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001230void ASTDumper::VisitVarDecl(const VarDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001231 dumpName(D);
1232 dumpType(D->getType());
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001233 StorageClass SC = D->getStorageClass();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001234 if (SC != SC_None)
1235 OS << ' ' << VarDecl::getStorageClassSpecifierString(SC);
Richard Smithfd3834f2013-04-13 02:43:54 +00001236 switch (D->getTLSKind()) {
1237 case VarDecl::TLS_None: break;
1238 case VarDecl::TLS_Static: OS << " tls"; break;
1239 case VarDecl::TLS_Dynamic: OS << " tls_dynamic"; break;
1240 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001241 if (D->isModulePrivate())
1242 OS << " __module_private__";
1243 if (D->isNRVOVariable())
1244 OS << " nrvo";
Richard Smith62f19e72016-06-25 00:15:56 +00001245 if (D->isInline())
1246 OS << " inline";
1247 if (D->isConstexpr())
1248 OS << " constexpr";
Richard Trieude5cc7d2013-01-31 01:44:26 +00001249 if (D->hasInit()) {
Richard Smithd9967cc2014-07-10 22:54:03 +00001250 switch (D->getInitStyle()) {
1251 case VarDecl::CInit: OS << " cinit"; break;
1252 case VarDecl::CallInit: OS << " callinit"; break;
1253 case VarDecl::ListInit: OS << " listinit"; break;
1254 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001255 dumpStmt(D->getInit());
Richard Trieude5cc7d2013-01-31 01:44:26 +00001256 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001257}
1258
Richard Smithbdb84f32016-07-22 23:36:59 +00001259void ASTDumper::VisitDecompositionDecl(const DecompositionDecl *D) {
1260 VisitVarDecl(D);
1261 for (auto *B : D->bindings())
1262 dumpDecl(B);
1263}
1264
Richard Smith7873de02016-08-11 22:25:46 +00001265void ASTDumper::VisitBindingDecl(const BindingDecl *D) {
1266 dumpName(D);
1267 dumpType(D->getType());
1268 if (auto *E = D->getBinding())
1269 dumpStmt(E);
1270}
1271
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001272void ASTDumper::VisitFileScopeAsmDecl(const FileScopeAsmDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001273 dumpStmt(D->getAsmString());
1274}
1275
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001276void ASTDumper::VisitImportDecl(const ImportDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001277 OS << ' ' << D->getImportedModule()->getFullModuleName();
1278}
1279
Nico Weber66220292016-03-02 17:28:48 +00001280void ASTDumper::VisitPragmaCommentDecl(const PragmaCommentDecl *D) {
1281 OS << ' ';
1282 switch (D->getCommentKind()) {
1283 case PCK_Unknown: llvm_unreachable("unexpected pragma comment kind");
1284 case PCK_Compiler: OS << "compiler"; break;
1285 case PCK_ExeStr: OS << "exestr"; break;
1286 case PCK_Lib: OS << "lib"; break;
1287 case PCK_Linker: OS << "linker"; break;
1288 case PCK_User: OS << "user"; break;
1289 }
1290 StringRef Arg = D->getArg();
1291 if (!Arg.empty())
1292 OS << " \"" << Arg << "\"";
1293}
1294
Nico Webercbbaeb12016-03-02 19:28:54 +00001295void ASTDumper::VisitPragmaDetectMismatchDecl(
1296 const PragmaDetectMismatchDecl *D) {
1297 OS << " \"" << D->getName() << "\" \"" << D->getValue() << "\"";
1298}
1299
Alexey Bataev958b9e72016-03-31 09:30:50 +00001300void ASTDumper::VisitCapturedDecl(const CapturedDecl *D) {
1301 dumpStmt(D->getBody());
1302}
1303
1304//===----------------------------------------------------------------------===//
1305// OpenMP Declarations
1306//===----------------------------------------------------------------------===//
1307
1308void ASTDumper::VisitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D) {
1309 for (auto *E : D->varlists())
1310 dumpStmt(E);
1311}
1312
1313void ASTDumper::VisitOMPDeclareReductionDecl(const OMPDeclareReductionDecl *D) {
1314 dumpName(D);
1315 dumpType(D->getType());
1316 OS << " combiner";
1317 dumpStmt(D->getCombiner());
1318 if (auto *Initializer = D->getInitializer()) {
1319 OS << " initializer";
Alexey Bataev070f43a2017-09-06 14:49:58 +00001320 switch (D->getInitializerKind()) {
1321 case OMPDeclareReductionDecl::DirectInit:
1322 OS << " omp_priv = ";
1323 break;
1324 case OMPDeclareReductionDecl::CopyInit:
1325 OS << " omp_priv ()";
1326 break;
1327 case OMPDeclareReductionDecl::CallInit:
1328 break;
1329 }
Alexey Bataev958b9e72016-03-31 09:30:50 +00001330 dumpStmt(Initializer);
1331 }
1332}
1333
1334void ASTDumper::VisitOMPCapturedExprDecl(const OMPCapturedExprDecl *D) {
1335 dumpName(D);
1336 dumpType(D->getType());
1337 dumpStmt(D->getInit());
1338}
Nico Webercbbaeb12016-03-02 19:28:54 +00001339
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001340//===----------------------------------------------------------------------===//
1341// C++ Declarations
1342//===----------------------------------------------------------------------===//
1343
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001344void ASTDumper::VisitNamespaceDecl(const NamespaceDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001345 dumpName(D);
1346 if (D->isInline())
1347 OS << " inline";
1348 if (!D->isOriginalNamespace())
1349 dumpDeclRef(D->getOriginalNamespace(), "original");
1350}
1351
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001352void ASTDumper::VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001353 OS << ' ';
1354 dumpBareDeclRef(D->getNominatedNamespace());
1355}
1356
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001357void ASTDumper::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001358 dumpName(D);
1359 dumpDeclRef(D->getAliasedNamespace());
1360}
1361
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001362void ASTDumper::VisitTypeAliasDecl(const TypeAliasDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001363 dumpName(D);
1364 dumpType(D->getUnderlyingType());
Richard Smithba3a4f92016-01-12 21:59:26 +00001365 dumpTypeAsChild(D->getUnderlyingType());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001366}
1367
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001368void ASTDumper::VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001369 dumpName(D);
1370 dumpTemplateParameters(D->getTemplateParameters());
1371 dumpDecl(D->getTemplatedDecl());
1372}
1373
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001374void ASTDumper::VisitCXXRecordDecl(const CXXRecordDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001375 VisitRecordDecl(D);
1376 if (!D->isCompleteDefinition())
1377 return;
1378
Richard Smithdfc4bff2017-09-22 00:11:15 +00001379 dumpChild([=] {
1380 {
1381 ColorScope Color(*this, DeclKindNameColor);
1382 OS << "DefinitionData";
1383 }
1384#define FLAG(fn, name) if (D->fn()) OS << " " #name;
1385 FLAG(isParsingBaseSpecifiers, parsing_base_specifiers);
1386
1387 FLAG(isGenericLambda, generic);
1388 FLAG(isLambda, lambda);
1389
1390 FLAG(canPassInRegisters, pass_in_registers);
1391 FLAG(isEmpty, empty);
1392 FLAG(isAggregate, aggregate);
1393 FLAG(isStandardLayout, standard_layout);
1394 FLAG(isTriviallyCopyable, trivially_copyable);
1395 FLAG(isPOD, pod);
1396 FLAG(isTrivial, trivial);
1397 FLAG(isPolymorphic, polymorphic);
1398 FLAG(isAbstract, abstract);
1399 FLAG(isLiteral, literal);
1400
1401 FLAG(hasUserDeclaredConstructor, has_user_declared_ctor);
1402 FLAG(hasConstexprNonCopyMoveConstructor, has_constexpr_non_copy_move_ctor);
1403 FLAG(hasMutableFields, has_mutable_fields);
1404 FLAG(hasVariantMembers, has_variant_members);
1405 FLAG(allowConstDefaultInit, can_const_default_init);
1406
1407 dumpChild([=] {
1408 {
1409 ColorScope Color(*this, DeclKindNameColor);
1410 OS << "DefaultConstructor";
1411 }
1412 FLAG(hasDefaultConstructor, exists);
1413 FLAG(hasTrivialDefaultConstructor, trivial);
1414 FLAG(hasNonTrivialDefaultConstructor, non_trivial);
1415 FLAG(hasUserProvidedDefaultConstructor, user_provided);
1416 FLAG(hasConstexprDefaultConstructor, constexpr);
1417 FLAG(needsImplicitDefaultConstructor, needs_implicit);
1418 FLAG(defaultedDefaultConstructorIsConstexpr, defaulted_is_constexpr);
1419 });
1420
1421 dumpChild([=] {
1422 {
1423 ColorScope Color(*this, DeclKindNameColor);
1424 OS << "CopyConstructor";
1425 }
1426 FLAG(hasSimpleCopyConstructor, simple);
1427 FLAG(hasTrivialCopyConstructor, trivial);
1428 FLAG(hasNonTrivialCopyConstructor, non_trivial);
1429 FLAG(hasUserDeclaredCopyConstructor, user_declared);
1430 FLAG(hasCopyConstructorWithConstParam, has_const_param);
1431 FLAG(needsImplicitCopyConstructor, needs_implicit);
1432 FLAG(needsOverloadResolutionForCopyConstructor,
1433 needs_overload_resolution);
1434 if (!D->needsOverloadResolutionForCopyConstructor())
1435 FLAG(defaultedCopyConstructorIsDeleted, defaulted_is_deleted);
1436 FLAG(implicitCopyConstructorHasConstParam, implicit_has_const_param);
1437 });
1438
1439 dumpChild([=] {
1440 {
1441 ColorScope Color(*this, DeclKindNameColor);
1442 OS << "MoveConstructor";
1443 }
1444 FLAG(hasMoveConstructor, exists);
1445 FLAG(hasSimpleMoveConstructor, simple);
1446 FLAG(hasTrivialMoveConstructor, trivial);
1447 FLAG(hasNonTrivialMoveConstructor, non_trivial);
1448 FLAG(hasUserDeclaredMoveConstructor, user_declared);
1449 FLAG(needsImplicitMoveConstructor, needs_implicit);
1450 FLAG(needsOverloadResolutionForMoveConstructor,
1451 needs_overload_resolution);
1452 if (!D->needsOverloadResolutionForMoveConstructor())
1453 FLAG(defaultedMoveConstructorIsDeleted, defaulted_is_deleted);
1454 });
1455
1456 dumpChild([=] {
1457 {
1458 ColorScope Color(*this, DeclKindNameColor);
1459 OS << "CopyAssignment";
1460 }
1461 FLAG(hasTrivialCopyAssignment, trivial);
1462 FLAG(hasNonTrivialCopyAssignment, non_trivial);
1463 FLAG(hasCopyAssignmentWithConstParam, has_const_param);
1464 FLAG(hasUserDeclaredCopyAssignment, user_declared);
1465 FLAG(needsImplicitCopyAssignment, needs_implicit);
1466 FLAG(needsOverloadResolutionForCopyAssignment, needs_overload_resolution);
1467 FLAG(implicitCopyAssignmentHasConstParam, implicit_has_const_param);
1468 });
1469
1470 dumpChild([=] {
1471 {
1472 ColorScope Color(*this, DeclKindNameColor);
1473 OS << "MoveAssignment";
1474 }
1475 FLAG(hasMoveAssignment, exists);
1476 FLAG(hasSimpleMoveAssignment, simple);
1477 FLAG(hasTrivialMoveAssignment, trivial);
1478 FLAG(hasNonTrivialMoveAssignment, non_trivial);
1479 FLAG(hasUserDeclaredMoveAssignment, user_declared);
1480 FLAG(needsImplicitMoveAssignment, needs_implicit);
1481 FLAG(needsOverloadResolutionForMoveAssignment, needs_overload_resolution);
1482 });
1483
1484 dumpChild([=] {
1485 {
1486 ColorScope Color(*this, DeclKindNameColor);
1487 OS << "Destructor";
1488 }
1489 FLAG(hasSimpleDestructor, simple);
1490 FLAG(hasIrrelevantDestructor, irrelevant);
1491 FLAG(hasTrivialDestructor, trivial);
1492 FLAG(hasNonTrivialDestructor, non_trivial);
1493 FLAG(hasUserDeclaredDestructor, user_declared);
1494 FLAG(needsImplicitDestructor, needs_implicit);
1495 FLAG(needsOverloadResolutionForDestructor, needs_overload_resolution);
1496 if (!D->needsOverloadResolutionForDestructor())
1497 FLAG(defaultedDestructorIsDeleted, defaulted_is_deleted);
1498 });
1499 });
1500
Aaron Ballman574705e2014-03-13 15:41:46 +00001501 for (const auto &I : D->bases()) {
Richard Smithf7514452014-10-30 21:02:37 +00001502 dumpChild([=] {
1503 if (I.isVirtual())
1504 OS << "virtual ";
1505 dumpAccessSpecifier(I.getAccessSpecifier());
1506 dumpType(I.getType());
1507 if (I.isPackExpansion())
1508 OS << "...";
1509 });
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001510 }
1511}
1512
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001513void ASTDumper::VisitStaticAssertDecl(const StaticAssertDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001514 dumpStmt(D->getAssertExpr());
1515 dumpStmt(D->getMessage());
1516}
1517
Richard Smithcbdf7332014-03-18 02:07:28 +00001518template<typename SpecializationDecl>
Richard Smithf7514452014-10-30 21:02:37 +00001519void ASTDumper::VisitTemplateDeclSpecialization(const SpecializationDecl *D,
Richard Smithcbdf7332014-03-18 02:07:28 +00001520 bool DumpExplicitInst,
1521 bool DumpRefOnly) {
1522 bool DumpedAny = false;
1523 for (auto *RedeclWithBadType : D->redecls()) {
1524 // FIXME: The redecls() range sometimes has elements of a less-specific
1525 // type. (In particular, ClassTemplateSpecializationDecl::redecls() gives
1526 // us TagDecls, and should give CXXRecordDecls).
1527 auto *Redecl = dyn_cast<SpecializationDecl>(RedeclWithBadType);
1528 if (!Redecl) {
1529 // Found the injected-class-name for a class template. This will be dumped
1530 // as part of its surrounding class so we don't need to dump it here.
1531 assert(isa<CXXRecordDecl>(RedeclWithBadType) &&
1532 "expected an injected-class-name");
1533 continue;
1534 }
1535
1536 switch (Redecl->getTemplateSpecializationKind()) {
1537 case TSK_ExplicitInstantiationDeclaration:
1538 case TSK_ExplicitInstantiationDefinition:
1539 if (!DumpExplicitInst)
1540 break;
1541 // Fall through.
1542 case TSK_Undeclared:
1543 case TSK_ImplicitInstantiation:
Richard Smithf7514452014-10-30 21:02:37 +00001544 if (DumpRefOnly)
1545 dumpDeclRef(Redecl);
1546 else
1547 dumpDecl(Redecl);
Richard Smithcbdf7332014-03-18 02:07:28 +00001548 DumpedAny = true;
1549 break;
1550 case TSK_ExplicitSpecialization:
1551 break;
1552 }
1553 }
1554
1555 // Ensure we dump at least one decl for each specialization.
1556 if (!DumpedAny)
Richard Smithf7514452014-10-30 21:02:37 +00001557 dumpDeclRef(D);
Richard Smithcbdf7332014-03-18 02:07:28 +00001558}
1559
Richard Smith20ade552014-03-17 23:34:53 +00001560template<typename TemplateDecl>
1561void ASTDumper::VisitTemplateDecl(const TemplateDecl *D,
1562 bool DumpExplicitInst) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001563 dumpName(D);
1564 dumpTemplateParameters(D->getTemplateParameters());
Richard Smithdcc2c452014-03-17 23:00:06 +00001565
Richard Smithf7514452014-10-30 21:02:37 +00001566 dumpDecl(D->getTemplatedDecl());
Richard Smithdcc2c452014-03-17 23:00:06 +00001567
Richard Smithcbdf7332014-03-18 02:07:28 +00001568 for (auto *Child : D->specializations())
Richard Smithf7514452014-10-30 21:02:37 +00001569 VisitTemplateDeclSpecialization(Child, DumpExplicitInst,
Richard Smithcbdf7332014-03-18 02:07:28 +00001570 !D->isCanonicalDecl());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001571}
1572
Richard Smith20ade552014-03-17 23:34:53 +00001573void ASTDumper::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
1574 // FIXME: We don't add a declaration of a function template specialization
1575 // to its context when it's explicitly instantiated, so dump explicit
1576 // instantiations when we dump the template itself.
1577 VisitTemplateDecl(D, true);
1578}
1579
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001580void ASTDumper::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
Richard Smith20ade552014-03-17 23:34:53 +00001581 VisitTemplateDecl(D, false);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001582}
1583
1584void ASTDumper::VisitClassTemplateSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001585 const ClassTemplateSpecializationDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001586 VisitCXXRecordDecl(D);
1587 dumpTemplateArgumentList(D->getTemplateArgs());
1588}
1589
1590void ASTDumper::VisitClassTemplatePartialSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001591 const ClassTemplatePartialSpecializationDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001592 VisitClassTemplateSpecializationDecl(D);
1593 dumpTemplateParameters(D->getTemplateParameters());
1594}
1595
1596void ASTDumper::VisitClassScopeFunctionSpecializationDecl(
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001597 const ClassScopeFunctionSpecializationDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001598 dumpDeclRef(D->getSpecialization());
1599 if (D->hasExplicitTemplateArgs())
1600 dumpTemplateArgumentListInfo(D->templateArgs());
1601}
1602
Richard Smithd25789a2013-09-18 01:36:02 +00001603void ASTDumper::VisitVarTemplateDecl(const VarTemplateDecl *D) {
Richard Smith20ade552014-03-17 23:34:53 +00001604 VisitTemplateDecl(D, false);
Richard Smithd25789a2013-09-18 01:36:02 +00001605}
1606
David Majnemerd9b1a4f2015-11-04 03:40:30 +00001607void ASTDumper::VisitBuiltinTemplateDecl(const BuiltinTemplateDecl *D) {
1608 dumpName(D);
1609 dumpTemplateParameters(D->getTemplateParameters());
1610}
1611
Richard Smithd25789a2013-09-18 01:36:02 +00001612void ASTDumper::VisitVarTemplateSpecializationDecl(
1613 const VarTemplateSpecializationDecl *D) {
1614 dumpTemplateArgumentList(D->getTemplateArgs());
1615 VisitVarDecl(D);
1616}
1617
1618void ASTDumper::VisitVarTemplatePartialSpecializationDecl(
1619 const VarTemplatePartialSpecializationDecl *D) {
1620 dumpTemplateParameters(D->getTemplateParameters());
1621 VisitVarTemplateSpecializationDecl(D);
1622}
1623
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001624void ASTDumper::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001625 if (D->wasDeclaredWithTypename())
1626 OS << " typename";
1627 else
1628 OS << " class";
Richard Smith1832a022017-02-21 02:04:03 +00001629 OS << " depth " << D->getDepth() << " index " << D->getIndex();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001630 if (D->isParameterPack())
1631 OS << " ...";
1632 dumpName(D);
Richard Smithf7514452014-10-30 21:02:37 +00001633 if (D->hasDefaultArgument())
Richard Smithecf74ff2014-03-23 20:50:39 +00001634 dumpTemplateArgument(D->getDefaultArgument());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001635}
1636
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001637void ASTDumper::VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001638 dumpType(D->getType());
Richard Smith1832a022017-02-21 02:04:03 +00001639 OS << " depth " << D->getDepth() << " index " << D->getIndex();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001640 if (D->isParameterPack())
1641 OS << " ...";
1642 dumpName(D);
Richard Smithf7514452014-10-30 21:02:37 +00001643 if (D->hasDefaultArgument())
Richard Smithecf74ff2014-03-23 20:50:39 +00001644 dumpTemplateArgument(D->getDefaultArgument());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001645}
1646
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001647void ASTDumper::VisitTemplateTemplateParmDecl(
1648 const TemplateTemplateParmDecl *D) {
Richard Smith1832a022017-02-21 02:04:03 +00001649 OS << " depth " << D->getDepth() << " index " << D->getIndex();
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001650 if (D->isParameterPack())
1651 OS << " ...";
1652 dumpName(D);
1653 dumpTemplateParameters(D->getTemplateParameters());
Richard Smithf7514452014-10-30 21:02:37 +00001654 if (D->hasDefaultArgument())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001655 dumpTemplateArgumentLoc(D->getDefaultArgument());
1656}
1657
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001658void ASTDumper::VisitUsingDecl(const UsingDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001659 OS << ' ';
Dawn Perchikddd03bf2015-12-05 22:37:55 +00001660 if (D->getQualifier())
1661 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001662 OS << D->getNameAsString();
1663}
1664
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001665void ASTDumper::VisitUnresolvedUsingTypenameDecl(
1666 const UnresolvedUsingTypenameDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001667 OS << ' ';
Dawn Perchikddd03bf2015-12-05 22:37:55 +00001668 if (D->getQualifier())
1669 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001670 OS << D->getNameAsString();
1671}
1672
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001673void ASTDumper::VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001674 OS << ' ';
Dawn Perchikddd03bf2015-12-05 22:37:55 +00001675 if (D->getQualifier())
1676 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001677 OS << D->getNameAsString();
1678 dumpType(D->getType());
1679}
1680
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001681void ASTDumper::VisitUsingShadowDecl(const UsingShadowDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001682 OS << ' ';
1683 dumpBareDeclRef(D->getTargetDecl());
Richard Smithba3a4f92016-01-12 21:59:26 +00001684 if (auto *TD = dyn_cast<TypeDecl>(D->getUnderlyingDecl()))
1685 dumpTypeAsChild(TD->getTypeForDecl());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001686}
1687
Richard Smith5179eb72016-06-28 19:03:57 +00001688void ASTDumper::VisitConstructorUsingShadowDecl(
1689 const ConstructorUsingShadowDecl *D) {
1690 if (D->constructsVirtualBase())
1691 OS << " virtual";
1692
1693 dumpChild([=] {
1694 OS << "target ";
1695 dumpBareDeclRef(D->getTargetDecl());
1696 });
1697
1698 dumpChild([=] {
1699 OS << "nominated ";
1700 dumpBareDeclRef(D->getNominatedBaseClass());
1701 OS << ' ';
1702 dumpBareDeclRef(D->getNominatedBaseClassShadowDecl());
1703 });
1704
1705 dumpChild([=] {
1706 OS << "constructed ";
1707 dumpBareDeclRef(D->getConstructedBaseClass());
1708 OS << ' ';
1709 dumpBareDeclRef(D->getConstructedBaseClassShadowDecl());
1710 });
1711}
1712
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001713void ASTDumper::VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001714 switch (D->getLanguage()) {
1715 case LinkageSpecDecl::lang_c: OS << " C"; break;
1716 case LinkageSpecDecl::lang_cxx: OS << " C++"; break;
1717 }
1718}
1719
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001720void ASTDumper::VisitAccessSpecDecl(const AccessSpecDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001721 OS << ' ';
1722 dumpAccessSpecifier(D->getAccess());
1723}
1724
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001725void ASTDumper::VisitFriendDecl(const FriendDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001726 if (TypeSourceInfo *T = D->getFriendType())
1727 dumpType(T->getType());
1728 else
1729 dumpDecl(D->getFriendDecl());
1730}
1731
1732//===----------------------------------------------------------------------===//
1733// Obj-C Declarations
1734//===----------------------------------------------------------------------===//
1735
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001736void ASTDumper::VisitObjCIvarDecl(const ObjCIvarDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001737 dumpName(D);
1738 dumpType(D->getType());
1739 if (D->getSynthesize())
1740 OS << " synthesize";
1741
1742 switch (D->getAccessControl()) {
1743 case ObjCIvarDecl::None:
1744 OS << " none";
1745 break;
1746 case ObjCIvarDecl::Private:
1747 OS << " private";
1748 break;
1749 case ObjCIvarDecl::Protected:
1750 OS << " protected";
1751 break;
1752 case ObjCIvarDecl::Public:
1753 OS << " public";
1754 break;
1755 case ObjCIvarDecl::Package:
1756 OS << " package";
1757 break;
1758 }
1759}
1760
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001761void ASTDumper::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001762 if (D->isInstanceMethod())
1763 OS << " -";
1764 else
1765 OS << " +";
1766 dumpName(D);
Alp Toker314cc812014-01-25 16:55:45 +00001767 dumpType(D->getReturnType());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001768
Richard Trieude5cc7d2013-01-31 01:44:26 +00001769 if (D->isThisDeclarationADefinition()) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001770 dumpDeclContext(D);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001771 } else {
David Majnemera3debed2016-06-24 05:33:44 +00001772 for (const ParmVarDecl *Parameter : D->parameters())
1773 dumpDecl(Parameter);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001774 }
1775
Richard Smithf7514452014-10-30 21:02:37 +00001776 if (D->isVariadic())
1777 dumpChild([=] { OS << "..."; });
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001778
Richard Smithf7514452014-10-30 21:02:37 +00001779 if (D->hasBody())
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001780 dumpStmt(D->getBody());
1781}
1782
Douglas Gregor85f3f952015-07-07 03:57:15 +00001783void ASTDumper::VisitObjCTypeParamDecl(const ObjCTypeParamDecl *D) {
1784 dumpName(D);
Douglas Gregor1ac1b632015-07-07 03:58:54 +00001785 switch (D->getVariance()) {
1786 case ObjCTypeParamVariance::Invariant:
1787 break;
1788
1789 case ObjCTypeParamVariance::Covariant:
1790 OS << " covariant";
1791 break;
1792
1793 case ObjCTypeParamVariance::Contravariant:
1794 OS << " contravariant";
1795 break;
1796 }
1797
Douglas Gregor85f3f952015-07-07 03:57:15 +00001798 if (D->hasExplicitBound())
1799 OS << " bounded";
1800 dumpType(D->getUnderlyingType());
1801}
1802
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001803void ASTDumper::VisitObjCCategoryDecl(const ObjCCategoryDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001804 dumpName(D);
1805 dumpDeclRef(D->getClassInterface());
Douglas Gregor85f3f952015-07-07 03:57:15 +00001806 dumpObjCTypeParamList(D->getTypeParamList());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001807 dumpDeclRef(D->getImplementation());
1808 for (ObjCCategoryDecl::protocol_iterator I = D->protocol_begin(),
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001809 E = D->protocol_end();
Richard Smithf7514452014-10-30 21:02:37 +00001810 I != E; ++I)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001811 dumpDeclRef(*I);
1812}
1813
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001814void ASTDumper::VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001815 dumpName(D);
1816 dumpDeclRef(D->getClassInterface());
1817 dumpDeclRef(D->getCategoryDecl());
1818}
1819
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001820void ASTDumper::VisitObjCProtocolDecl(const ObjCProtocolDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001821 dumpName(D);
Richard Smithdcc2c452014-03-17 23:00:06 +00001822
Richard Smith7fcb35f2014-03-18 02:37:59 +00001823 for (auto *Child : D->protocols())
Richard Smithf7514452014-10-30 21:02:37 +00001824 dumpDeclRef(Child);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001825}
1826
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001827void ASTDumper::VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001828 dumpName(D);
Douglas Gregor85f3f952015-07-07 03:57:15 +00001829 dumpObjCTypeParamList(D->getTypeParamListAsWritten());
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001830 dumpDeclRef(D->getSuperClass(), "super");
Richard Smithdcc2c452014-03-17 23:00:06 +00001831
Richard Smithf7514452014-10-30 21:02:37 +00001832 dumpDeclRef(D->getImplementation());
Richard Smith7fcb35f2014-03-18 02:37:59 +00001833 for (auto *Child : D->protocols())
Richard Smithf7514452014-10-30 21:02:37 +00001834 dumpDeclRef(Child);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001835}
1836
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001837void ASTDumper::VisitObjCImplementationDecl(const ObjCImplementationDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001838 dumpName(D);
1839 dumpDeclRef(D->getSuperClass(), "super");
1840 dumpDeclRef(D->getClassInterface());
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001841 for (ObjCImplementationDecl::init_const_iterator I = D->init_begin(),
1842 E = D->init_end();
Richard Smithf7514452014-10-30 21:02:37 +00001843 I != E; ++I)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001844 dumpCXXCtorInitializer(*I);
1845}
1846
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001847void ASTDumper::VisitObjCCompatibleAliasDecl(const ObjCCompatibleAliasDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001848 dumpName(D);
1849 dumpDeclRef(D->getClassInterface());
1850}
1851
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001852void ASTDumper::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001853 dumpName(D);
1854 dumpType(D->getType());
1855
1856 if (D->getPropertyImplementation() == ObjCPropertyDecl::Required)
1857 OS << " required";
1858 else if (D->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1859 OS << " optional";
1860
1861 ObjCPropertyDecl::PropertyAttributeKind Attrs = D->getPropertyAttributes();
1862 if (Attrs != ObjCPropertyDecl::OBJC_PR_noattr) {
1863 if (Attrs & ObjCPropertyDecl::OBJC_PR_readonly)
1864 OS << " readonly";
1865 if (Attrs & ObjCPropertyDecl::OBJC_PR_assign)
1866 OS << " assign";
1867 if (Attrs & ObjCPropertyDecl::OBJC_PR_readwrite)
1868 OS << " readwrite";
1869 if (Attrs & ObjCPropertyDecl::OBJC_PR_retain)
1870 OS << " retain";
1871 if (Attrs & ObjCPropertyDecl::OBJC_PR_copy)
1872 OS << " copy";
1873 if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic)
1874 OS << " nonatomic";
1875 if (Attrs & ObjCPropertyDecl::OBJC_PR_atomic)
1876 OS << " atomic";
1877 if (Attrs & ObjCPropertyDecl::OBJC_PR_weak)
1878 OS << " weak";
1879 if (Attrs & ObjCPropertyDecl::OBJC_PR_strong)
1880 OS << " strong";
1881 if (Attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained)
1882 OS << " unsafe_unretained";
Manman Ren387ff7f2016-01-26 18:52:43 +00001883 if (Attrs & ObjCPropertyDecl::OBJC_PR_class)
1884 OS << " class";
Richard Smithf7514452014-10-30 21:02:37 +00001885 if (Attrs & ObjCPropertyDecl::OBJC_PR_getter)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001886 dumpDeclRef(D->getGetterMethodDecl(), "getter");
Richard Smithf7514452014-10-30 21:02:37 +00001887 if (Attrs & ObjCPropertyDecl::OBJC_PR_setter)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001888 dumpDeclRef(D->getSetterMethodDecl(), "setter");
1889 }
1890}
1891
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001892void ASTDumper::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001893 dumpName(D->getPropertyDecl());
1894 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
1895 OS << " synthesize";
1896 else
1897 OS << " dynamic";
1898 dumpDeclRef(D->getPropertyDecl());
1899 dumpDeclRef(D->getPropertyIvarDecl());
1900}
1901
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001902void ASTDumper::VisitBlockDecl(const BlockDecl *D) {
David Majnemer59f77922016-06-24 04:05:48 +00001903 for (auto I : D->parameters())
Aaron Ballmanb2b8b1d2014-03-07 16:09:59 +00001904 dumpDecl(I);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001905
Richard Smithf7514452014-10-30 21:02:37 +00001906 if (D->isVariadic())
1907 dumpChild([=]{ OS << "..."; });
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001908
Richard Smithf7514452014-10-30 21:02:37 +00001909 if (D->capturesCXXThis())
1910 dumpChild([=]{ OS << "capture this"; });
1911
Aaron Ballman9371dd22014-03-14 18:34:04 +00001912 for (const auto &I : D->captures()) {
Richard Smithf7514452014-10-30 21:02:37 +00001913 dumpChild([=] {
1914 OS << "capture";
1915 if (I.isByRef())
1916 OS << " byref";
1917 if (I.isNested())
1918 OS << " nested";
1919 if (I.getVariable()) {
1920 OS << ' ';
1921 dumpBareDeclRef(I.getVariable());
1922 }
1923 if (I.hasCopyExpr())
1924 dumpStmt(I.getCopyExpr());
1925 });
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001926 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001927 dumpStmt(D->getBody());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001928}
1929
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001930//===----------------------------------------------------------------------===//
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00001931// Stmt dumping methods.
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001932//===----------------------------------------------------------------------===//
1933
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001934void ASTDumper::dumpStmt(const Stmt *S) {
Richard Smithf7514452014-10-30 21:02:37 +00001935 dumpChild([=] {
1936 if (!S) {
1937 ColorScope Color(*this, NullColor);
1938 OS << "<<<NULL>>>";
1939 return;
1940 }
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001941
Richard Smithf7514452014-10-30 21:02:37 +00001942 if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
1943 VisitDeclStmt(DS);
1944 return;
1945 }
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001946
Richard Smithf7514452014-10-30 21:02:37 +00001947 ConstStmtVisitor<ASTDumper>::Visit(S);
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001948
Benjamin Kramer642f1732015-07-02 21:03:14 +00001949 for (const Stmt *SubStmt : S->children())
1950 dumpStmt(SubStmt);
Richard Smithf7514452014-10-30 21:02:37 +00001951 });
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001952}
1953
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001954void ASTDumper::VisitStmt(const Stmt *Node) {
Richard Trieud215b8d2013-01-26 01:31:20 +00001955 {
1956 ColorScope Color(*this, StmtColor);
1957 OS << Node->getStmtClassName();
1958 }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001959 dumpPointer(Node);
1960 dumpSourceRange(Node->getSourceRange());
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001961}
1962
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001963void ASTDumper::VisitDeclStmt(const DeclStmt *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001964 VisitStmt(Node);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001965 for (DeclStmt::const_decl_iterator I = Node->decl_begin(),
1966 E = Node->decl_end();
Richard Smithf7514452014-10-30 21:02:37 +00001967 I != E; ++I)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001968 dumpDecl(*I);
Ted Kremenek433a4922007-12-12 06:59:42 +00001969}
1970
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001971void ASTDumper::VisitAttributedStmt(const AttributedStmt *Node) {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001972 VisitStmt(Node);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001973 for (ArrayRef<const Attr *>::iterator I = Node->getAttrs().begin(),
1974 E = Node->getAttrs().end();
Richard Smithf7514452014-10-30 21:02:37 +00001975 I != E; ++I)
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001976 dumpAttr(*I);
1977}
1978
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001979void ASTDumper::VisitLabelStmt(const LabelStmt *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001980 VisitStmt(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00001981 OS << " '" << Node->getName() << "'";
Chris Lattnercbe4f772007-08-08 22:51:59 +00001982}
1983
Alexander Kornienko540bacb2013-02-01 12:35:51 +00001984void ASTDumper::VisitGotoStmt(const GotoStmt *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00001985 VisitStmt(Node);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00001986 OS << " '" << Node->getLabel()->getName() << "'";
1987 dumpPointer(Node->getLabel());
Chris Lattnercbe4f772007-08-08 22:51:59 +00001988}
1989
Pavel Labath1ef83422013-09-04 14:35:00 +00001990void ASTDumper::VisitCXXCatchStmt(const CXXCatchStmt *Node) {
1991 VisitStmt(Node);
1992 dumpDecl(Node->getExceptionDecl());
1993}
1994
Alexey Bataev958b9e72016-03-31 09:30:50 +00001995void ASTDumper::VisitCapturedStmt(const CapturedStmt *Node) {
1996 VisitStmt(Node);
1997 dumpDecl(Node->getCapturedDecl());
1998}
1999
2000//===----------------------------------------------------------------------===//
2001// OpenMP dumping methods.
2002//===----------------------------------------------------------------------===//
2003
2004void ASTDumper::VisitOMPExecutableDirective(
2005 const OMPExecutableDirective *Node) {
2006 VisitStmt(Node);
2007 for (auto *C : Node->clauses()) {
2008 dumpChild([=] {
2009 if (!C) {
2010 ColorScope Color(*this, NullColor);
2011 OS << "<<<NULL>>> OMPClause";
2012 return;
2013 }
2014 {
2015 ColorScope Color(*this, AttrColor);
2016 StringRef ClauseName(getOpenMPClauseName(C->getClauseKind()));
2017 OS << "OMP" << ClauseName.substr(/*Start=*/0, /*N=*/1).upper()
2018 << ClauseName.drop_front() << "Clause";
2019 }
2020 dumpPointer(C);
2021 dumpSourceRange(SourceRange(C->getLocStart(), C->getLocEnd()));
2022 if (C->isImplicit())
2023 OS << " <implicit>";
2024 for (auto *S : C->children())
2025 dumpStmt(S);
2026 });
2027 }
2028}
2029
Chris Lattnercbe4f772007-08-08 22:51:59 +00002030//===----------------------------------------------------------------------===//
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00002031// Expr dumping methods.
Chris Lattnercbe4f772007-08-08 22:51:59 +00002032//===----------------------------------------------------------------------===//
2033
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002034void ASTDumper::VisitExpr(const Expr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002035 VisitStmt(Node);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00002036 dumpType(Node->getType());
2037
Richard Trieud215b8d2013-01-26 01:31:20 +00002038 {
2039 ColorScope Color(*this, ValueKindColor);
2040 switch (Node->getValueKind()) {
2041 case VK_RValue:
2042 break;
2043 case VK_LValue:
2044 OS << " lvalue";
2045 break;
2046 case VK_XValue:
2047 OS << " xvalue";
2048 break;
2049 }
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00002050 }
2051
Richard Trieud215b8d2013-01-26 01:31:20 +00002052 {
2053 ColorScope Color(*this, ObjectKindColor);
2054 switch (Node->getObjectKind()) {
2055 case OK_Ordinary:
2056 break;
2057 case OK_BitField:
2058 OS << " bitfield";
2059 break;
2060 case OK_ObjCProperty:
2061 OS << " objcproperty";
2062 break;
2063 case OK_ObjCSubscript:
2064 OS << " objcsubscript";
2065 break;
2066 case OK_VectorComponent:
2067 OS << " vectorcomponent";
2068 break;
2069 }
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00002070 }
Chris Lattnercbe4f772007-08-08 22:51:59 +00002071}
2072
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002073static void dumpBasePath(raw_ostream &OS, const CastExpr *Node) {
John McCallcf142162010-08-07 06:22:56 +00002074 if (Node->path_empty())
Anders Carlssona70cff62010-04-24 19:06:50 +00002075 return;
2076
2077 OS << " (";
2078 bool First = true;
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002079 for (CastExpr::path_const_iterator I = Node->path_begin(),
2080 E = Node->path_end();
2081 I != E; ++I) {
Anders Carlssona70cff62010-04-24 19:06:50 +00002082 const CXXBaseSpecifier *Base = *I;
2083 if (!First)
2084 OS << " -> ";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00002085
Anders Carlssona70cff62010-04-24 19:06:50 +00002086 const CXXRecordDecl *RD =
2087 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00002088
Anders Carlssona70cff62010-04-24 19:06:50 +00002089 if (Base->isVirtual())
2090 OS << "virtual ";
2091 OS << RD->getName();
2092 First = false;
2093 }
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00002094
Anders Carlssona70cff62010-04-24 19:06:50 +00002095 OS << ')';
2096}
2097
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002098void ASTDumper::VisitCastExpr(const CastExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002099 VisitExpr(Node);
Richard Trieud215b8d2013-01-26 01:31:20 +00002100 OS << " <";
2101 {
2102 ColorScope Color(*this, CastColor);
2103 OS << Node->getCastKindName();
2104 }
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00002105 dumpBasePath(OS, Node);
Anders Carlssona70cff62010-04-24 19:06:50 +00002106 OS << ">";
Anders Carlssond7923c62009-08-22 23:33:40 +00002107}
2108
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002109void ASTDumper::VisitDeclRefExpr(const DeclRefExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002110 VisitExpr(Node);
Ted Kremenek5f64ca82007-09-10 17:32:55 +00002111
Daniel Dunbar34a96c82009-12-03 09:13:13 +00002112 OS << " ";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002113 dumpBareDeclRef(Node->getDecl());
Chandler Carruth8d26bb02011-05-01 23:48:14 +00002114 if (Node->getDecl() != Node->getFoundDecl()) {
2115 OS << " (";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002116 dumpBareDeclRef(Node->getFoundDecl());
Chandler Carruth8d26bb02011-05-01 23:48:14 +00002117 OS << ")";
2118 }
John McCall351762c2011-02-07 10:33:21 +00002119}
2120
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002121void ASTDumper::VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002122 VisitExpr(Node);
John McCall76d09942009-12-11 21:50:11 +00002123 OS << " (";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00002124 if (!Node->requiresADL())
2125 OS << "no ";
Benjamin Kramerb11416d2010-04-17 09:33:03 +00002126 OS << "ADL) = '" << Node->getName() << '\'';
John McCall76d09942009-12-11 21:50:11 +00002127
2128 UnresolvedLookupExpr::decls_iterator
2129 I = Node->decls_begin(), E = Node->decls_end();
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00002130 if (I == E)
2131 OS << " empty";
John McCall76d09942009-12-11 21:50:11 +00002132 for (; I != E; ++I)
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002133 dumpPointer(*I);
John McCall76d09942009-12-11 21:50:11 +00002134}
2135
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002136void ASTDumper::VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002137 VisitExpr(Node);
Steve Naroff5d5efca2008-03-12 13:19:12 +00002138
Richard Trieud215b8d2013-01-26 01:31:20 +00002139 {
2140 ColorScope Color(*this, DeclKindNameColor);
2141 OS << " " << Node->getDecl()->getDeclKindName() << "Decl";
2142 }
2143 OS << "='" << *Node->getDecl() << "'";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002144 dumpPointer(Node->getDecl());
Steve Naroffb3424a92008-05-23 22:01:24 +00002145 if (Node->isFreeIvar())
Daniel Dunbar34a96c82009-12-03 09:13:13 +00002146 OS << " isFreeIvar";
Steve Naroff5d5efca2008-03-12 13:19:12 +00002147}
2148
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002149void ASTDumper::VisitPredefinedExpr(const PredefinedExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002150 VisitExpr(Node);
Alexey Bataevec474782014-10-09 08:45:04 +00002151 OS << " " << PredefinedExpr::getIdentTypeName(Node->getIdentType());
Chris Lattnercbe4f772007-08-08 22:51:59 +00002152}
2153
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002154void ASTDumper::VisitCharacterLiteral(const CharacterLiteral *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002155 VisitExpr(Node);
Richard Trieud215b8d2013-01-26 01:31:20 +00002156 ColorScope Color(*this, ValueColor);
Richard Trieu364ee422011-11-03 23:56:23 +00002157 OS << " " << Node->getValue();
Chris Lattnercbe4f772007-08-08 22:51:59 +00002158}
2159
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002160void ASTDumper::VisitIntegerLiteral(const IntegerLiteral *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002161 VisitExpr(Node);
Chris Lattnercbe4f772007-08-08 22:51:59 +00002162
2163 bool isSigned = Node->getType()->isSignedIntegerType();
Richard Trieud215b8d2013-01-26 01:31:20 +00002164 ColorScope Color(*this, ValueColor);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00002165 OS << " " << Node->getValue().toString(10, isSigned);
Chris Lattnercbe4f772007-08-08 22:51:59 +00002166}
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00002167
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002168void ASTDumper::VisitFloatingLiteral(const FloatingLiteral *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002169 VisitExpr(Node);
Richard Trieud215b8d2013-01-26 01:31:20 +00002170 ColorScope Color(*this, ValueColor);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00002171 OS << " " << Node->getValueAsApproximateDouble();
Chris Lattnercbe4f772007-08-08 22:51:59 +00002172}
Chris Lattner1c20a172007-08-26 03:42:43 +00002173
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002174void ASTDumper::VisitStringLiteral(const StringLiteral *Str) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002175 VisitExpr(Str);
Richard Trieud215b8d2013-01-26 01:31:20 +00002176 ColorScope Color(*this, ValueColor);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00002177 OS << " ";
Richard Trieudc355912012-06-13 20:25:24 +00002178 Str->outputString(OS);
Chris Lattnercbe4f772007-08-08 22:51:59 +00002179}
Chris Lattner84ca3762007-08-30 01:00:35 +00002180
Richard Smithf0514962014-06-03 08:24:28 +00002181void ASTDumper::VisitInitListExpr(const InitListExpr *ILE) {
2182 VisitExpr(ILE);
2183 if (auto *Filler = ILE->getArrayFiller()) {
Richard Smithf7514452014-10-30 21:02:37 +00002184 dumpChild([=] {
2185 OS << "array filler";
2186 dumpStmt(Filler);
2187 });
Richard Smithf0514962014-06-03 08:24:28 +00002188 }
2189 if (auto *Field = ILE->getInitializedFieldInUnion()) {
2190 OS << " field ";
2191 dumpBareDeclRef(Field);
2192 }
2193}
2194
Richard Smith410306b2016-12-12 02:53:20 +00002195void ASTDumper::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) {
2196 VisitExpr(E);
2197}
2198
2199void ASTDumper::VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E) {
2200 VisitExpr(E);
2201}
2202
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002203void ASTDumper::VisitUnaryOperator(const UnaryOperator *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002204 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00002205 OS << " " << (Node->isPostfix() ? "postfix" : "prefix")
2206 << " '" << UnaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
Chris Lattnercbe4f772007-08-08 22:51:59 +00002207}
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00002208
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002209void ASTDumper::VisitUnaryExprOrTypeTraitExpr(
2210 const UnaryExprOrTypeTraitExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002211 VisitExpr(Node);
Peter Collingbournee190dee2011-03-11 19:24:49 +00002212 switch(Node->getKind()) {
2213 case UETT_SizeOf:
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002214 OS << " sizeof";
Peter Collingbournee190dee2011-03-11 19:24:49 +00002215 break;
2216 case UETT_AlignOf:
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002217 OS << " alignof";
Peter Collingbournee190dee2011-03-11 19:24:49 +00002218 break;
2219 case UETT_VecStep:
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002220 OS << " vec_step";
Peter Collingbournee190dee2011-03-11 19:24:49 +00002221 break;
Alexey Bataev00396512015-07-02 03:40:19 +00002222 case UETT_OpenMPRequiredSimdAlign:
2223 OS << " __builtin_omp_required_simd_align";
2224 break;
Peter Collingbournee190dee2011-03-11 19:24:49 +00002225 }
Sebastian Redl6f282892008-11-11 17:56:53 +00002226 if (Node->isArgumentType())
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00002227 dumpType(Node->getArgumentType());
Chris Lattnercbe4f772007-08-08 22:51:59 +00002228}
Chris Lattnerdb3b3ff2007-08-09 17:35:30 +00002229
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002230void ASTDumper::VisitMemberExpr(const MemberExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002231 VisitExpr(Node);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002232 OS << " " << (Node->isArrow() ? "->" : ".") << *Node->getMemberDecl();
2233 dumpPointer(Node->getMemberDecl());
Chris Lattnercbe4f772007-08-08 22:51:59 +00002234}
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00002235
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002236void ASTDumper::VisitExtVectorElementExpr(const ExtVectorElementExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002237 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00002238 OS << " " << Node->getAccessor().getNameStart();
Chris Lattnercbe4f772007-08-08 22:51:59 +00002239}
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00002240
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002241void ASTDumper::VisitBinaryOperator(const BinaryOperator *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002242 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00002243 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) << "'";
Chris Lattner86928112007-08-25 02:00:02 +00002244}
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00002245
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002246void ASTDumper::VisitCompoundAssignOperator(
2247 const CompoundAssignOperator *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002248 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00002249 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode())
2250 << "' ComputeLHSTy=";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002251 dumpBareType(Node->getComputationLHSType());
Daniel Dunbar34a96c82009-12-03 09:13:13 +00002252 OS << " ComputeResultTy=";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002253 dumpBareType(Node->getComputationResultType());
Chris Lattnercbe4f772007-08-08 22:51:59 +00002254}
Chris Lattnercbe4f772007-08-08 22:51:59 +00002255
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002256void ASTDumper::VisitBlockExpr(const BlockExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002257 VisitExpr(Node);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002258 dumpDecl(Node->getBlockDecl());
John McCall351762c2011-02-07 10:33:21 +00002259}
2260
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002261void ASTDumper::VisitOpaqueValueExpr(const OpaqueValueExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002262 VisitExpr(Node);
John McCallfe96e0b2011-11-06 09:01:30 +00002263
Richard Smithf7514452014-10-30 21:02:37 +00002264 if (Expr *Source = Node->getSourceExpr())
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002265 dumpStmt(Source);
John McCallfe96e0b2011-11-06 09:01:30 +00002266}
2267
Chris Lattnercbe4f772007-08-08 22:51:59 +00002268// GNU extensions.
2269
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002270void ASTDumper::VisitAddrLabelExpr(const AddrLabelExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002271 VisitExpr(Node);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002272 OS << " " << Node->getLabel()->getName();
2273 dumpPointer(Node->getLabel());
Chris Lattnercbe4f772007-08-08 22:51:59 +00002274}
2275
Chris Lattner8f184b12007-08-09 18:03:18 +00002276//===----------------------------------------------------------------------===//
2277// C++ Expressions
2278//===----------------------------------------------------------------------===//
Chris Lattnercbe4f772007-08-08 22:51:59 +00002279
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002280void ASTDumper::VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002281 VisitExpr(Node);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00002282 OS << " " << Node->getCastName()
Daniel Dunbar34a96c82009-12-03 09:13:13 +00002283 << "<" << Node->getTypeAsWritten().getAsString() << ">"
Anders Carlssona70cff62010-04-24 19:06:50 +00002284 << " <" << Node->getCastKindName();
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00002285 dumpBasePath(OS, Node);
Anders Carlssona70cff62010-04-24 19:06:50 +00002286 OS << ">";
Chris Lattnercbe4f772007-08-08 22:51:59 +00002287}
2288
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002289void ASTDumper::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002290 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00002291 OS << " " << (Node->getValue() ? "true" : "false");
Chris Lattnercbe4f772007-08-08 22:51:59 +00002292}
2293
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002294void ASTDumper::VisitCXXThisExpr(const CXXThisExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002295 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00002296 OS << " this";
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002297}
2298
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002299void ASTDumper::VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002300 VisitExpr(Node);
Eli Friedman29538892011-09-02 17:38:59 +00002301 OS << " functional cast to " << Node->getTypeAsWritten().getAsString()
2302 << " <" << Node->getCastKindName() << ">";
Douglas Gregore200adc2008-10-27 19:41:14 +00002303}
2304
Richard Smith39eca9b2017-08-23 22:12:08 +00002305void ASTDumper::VisitCXXUnresolvedConstructExpr(
2306 const CXXUnresolvedConstructExpr *Node) {
2307 VisitExpr(Node);
2308 dumpType(Node->getTypeAsWritten());
2309 if (Node->isListInitialization())
2310 OS << " list";
2311}
2312
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002313void ASTDumper::VisitCXXConstructExpr(const CXXConstructExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002314 VisitExpr(Node);
John McCalleba90cd2010-02-02 19:03:45 +00002315 CXXConstructorDecl *Ctor = Node->getConstructor();
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00002316 dumpType(Ctor->getType());
Anders Carlsson073846832009-08-12 00:21:52 +00002317 if (Node->isElidable())
Daniel Dunbar34a96c82009-12-03 09:13:13 +00002318 OS << " elidable";
Richard Smith39eca9b2017-08-23 22:12:08 +00002319 if (Node->isListInitialization())
2320 OS << " list";
2321 if (Node->isStdInitListInitialization())
2322 OS << " std::initializer_list";
John McCall85370042010-08-07 06:38:55 +00002323 if (Node->requiresZeroInitialization())
2324 OS << " zeroing";
Anders Carlsson073846832009-08-12 00:21:52 +00002325}
2326
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002327void ASTDumper::VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002328 VisitExpr(Node);
Daniel Dunbar34a96c82009-12-03 09:13:13 +00002329 OS << " ";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00002330 dumpCXXTemporary(Node->getTemporary());
Anders Carlsson073846832009-08-12 00:21:52 +00002331}
2332
Reid Kleckner5c682bc2015-03-19 18:09:25 +00002333void ASTDumper::VisitCXXNewExpr(const CXXNewExpr *Node) {
2334 VisitExpr(Node);
Reid Kleckner5c682bc2015-03-19 18:09:25 +00002335 if (Node->isGlobalNew())
Reid Kleckner461c0c62015-03-19 18:47:47 +00002336 OS << " global";
Reid Kleckner5c682bc2015-03-19 18:09:25 +00002337 if (Node->isArray())
Reid Kleckner461c0c62015-03-19 18:47:47 +00002338 OS << " array";
2339 if (Node->getOperatorNew()) {
2340 OS << ' ';
2341 dumpBareDeclRef(Node->getOperatorNew());
2342 }
Reid Kleckner5c682bc2015-03-19 18:09:25 +00002343 // We could dump the deallocation function used in case of error, but it's
2344 // usually not that interesting.
2345}
2346
2347void ASTDumper::VisitCXXDeleteExpr(const CXXDeleteExpr *Node) {
2348 VisitExpr(Node);
Reid Kleckner5c682bc2015-03-19 18:09:25 +00002349 if (Node->isGlobalDelete())
Reid Kleckner461c0c62015-03-19 18:47:47 +00002350 OS << " global";
Reid Kleckner5c682bc2015-03-19 18:09:25 +00002351 if (Node->isArrayForm())
Reid Kleckner461c0c62015-03-19 18:47:47 +00002352 OS << " array";
2353 if (Node->getOperatorDelete()) {
2354 OS << ' ';
2355 dumpBareDeclRef(Node->getOperatorDelete());
2356 }
Reid Kleckner5c682bc2015-03-19 18:09:25 +00002357}
2358
Richard Smithe6c01442013-06-05 00:46:14 +00002359void
2360ASTDumper::VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Node) {
2361 VisitExpr(Node);
2362 if (const ValueDecl *VD = Node->getExtendingDecl()) {
2363 OS << " extended by ";
2364 dumpBareDeclRef(VD);
2365 }
2366}
2367
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002368void ASTDumper::VisitExprWithCleanups(const ExprWithCleanups *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002369 VisitExpr(Node);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002370 for (unsigned i = 0, e = Node->getNumObjects(); i != e; ++i)
2371 dumpDeclRef(Node->getObject(i), "cleanup");
Anders Carlsson073846832009-08-12 00:21:52 +00002372}
2373
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002374void ASTDumper::dumpCXXTemporary(const CXXTemporary *Temporary) {
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002375 OS << "(CXXTemporary";
2376 dumpPointer(Temporary);
2377 OS << ")";
Anders Carlsson073846832009-08-12 00:21:52 +00002378}
2379
Serge Pavlov6b926032015-02-16 19:58:41 +00002380void ASTDumper::VisitSizeOfPackExpr(const SizeOfPackExpr *Node) {
2381 VisitExpr(Node);
2382 dumpPointer(Node->getPack());
2383 dumpName(Node->getPack());
Richard Smithd784e682015-09-23 21:41:42 +00002384 if (Node->isPartiallySubstituted())
2385 for (const auto &A : Node->getPartialArguments())
2386 dumpTemplateArgument(A);
Serge Pavlov6b926032015-02-16 19:58:41 +00002387}
2388
Alex Lorenzddbe0f52016-11-09 14:02:18 +00002389void ASTDumper::VisitCXXDependentScopeMemberExpr(
2390 const CXXDependentScopeMemberExpr *Node) {
2391 VisitExpr(Node);
2392 OS << " " << (Node->isArrow() ? "->" : ".") << Node->getMember();
2393}
Serge Pavlov6b926032015-02-16 19:58:41 +00002394
Anders Carlsson76f4a902007-08-21 17:43:55 +00002395//===----------------------------------------------------------------------===//
2396// Obj-C Expressions
2397//===----------------------------------------------------------------------===//
2398
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002399void ASTDumper::VisitObjCMessageExpr(const ObjCMessageExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002400 VisitExpr(Node);
Aaron Ballmanb190f972014-01-03 17:59:55 +00002401 OS << " selector=";
2402 Node->getSelector().print(OS);
Douglas Gregor9a129192010-04-21 00:45:42 +00002403 switch (Node->getReceiverKind()) {
2404 case ObjCMessageExpr::Instance:
2405 break;
2406
2407 case ObjCMessageExpr::Class:
2408 OS << " class=";
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002409 dumpBareType(Node->getClassReceiver());
Douglas Gregor9a129192010-04-21 00:45:42 +00002410 break;
2411
2412 case ObjCMessageExpr::SuperInstance:
2413 OS << " super (instance)";
2414 break;
2415
2416 case ObjCMessageExpr::SuperClass:
2417 OS << " super (class)";
2418 break;
2419 }
Ted Kremenek36748da2008-02-29 22:04:05 +00002420}
2421
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002422void ASTDumper::VisitObjCBoxedExpr(const ObjCBoxedExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002423 VisitExpr(Node);
Richard Trieu4b259c82016-06-09 22:03:04 +00002424 if (auto *BoxingMethod = Node->getBoxingMethod()) {
2425 OS << " selector=";
2426 BoxingMethod->getSelector().print(OS);
2427 }
Argyrios Kyrtzidis9dd40892012-05-10 20:02:31 +00002428}
2429
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002430void ASTDumper::VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002431 VisitStmt(Node);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002432 if (const VarDecl *CatchParam = Node->getCatchParamDecl())
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002433 dumpDecl(CatchParam);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002434 else
Douglas Gregor96c79492010-04-23 22:50:49 +00002435 OS << " catch all";
Douglas Gregor96c79492010-04-23 22:50:49 +00002436}
2437
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002438void ASTDumper::VisitObjCEncodeExpr(const ObjCEncodeExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002439 VisitExpr(Node);
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00002440 dumpType(Node->getEncodedType());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002441}
2442
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002443void ASTDumper::VisitObjCSelectorExpr(const ObjCSelectorExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002444 VisitExpr(Node);
Mike Stump11289f42009-09-09 15:08:12 +00002445
Aaron Ballmanb190f972014-01-03 17:59:55 +00002446 OS << " ";
2447 Node->getSelector().print(OS);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002448}
2449
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002450void ASTDumper::VisitObjCProtocolExpr(const ObjCProtocolExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002451 VisitExpr(Node);
Mike Stump11289f42009-09-09 15:08:12 +00002452
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00002453 OS << ' ' << *Node->getProtocol();
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002454}
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00002455
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002456void ASTDumper::VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002457 VisitExpr(Node);
John McCallb7bd14f2010-12-02 01:19:52 +00002458 if (Node->isImplicitProperty()) {
Fariborz Jahanian0f0b3022010-12-22 19:46:35 +00002459 OS << " Kind=MethodRef Getter=\"";
2460 if (Node->getImplicitPropertyGetter())
Aaron Ballmanb190f972014-01-03 17:59:55 +00002461 Node->getImplicitPropertyGetter()->getSelector().print(OS);
Fariborz Jahanian0f0b3022010-12-22 19:46:35 +00002462 else
2463 OS << "(null)";
2464
2465 OS << "\" Setter=\"";
John McCallb7bd14f2010-12-02 01:19:52 +00002466 if (ObjCMethodDecl *Setter = Node->getImplicitPropertySetter())
Aaron Ballmanb190f972014-01-03 17:59:55 +00002467 Setter->getSelector().print(OS);
John McCallb7bd14f2010-12-02 01:19:52 +00002468 else
2469 OS << "(null)";
2470 OS << "\"";
2471 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00002472 OS << " Kind=PropertyRef Property=\"" << *Node->getExplicitProperty() <<'"';
John McCallb7bd14f2010-12-02 01:19:52 +00002473 }
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00002474
Fariborz Jahanian681c0752010-10-14 16:04:05 +00002475 if (Node->isSuperReceiver())
2476 OS << " super";
Argyrios Kyrtzidisab468b02012-03-30 00:19:18 +00002477
2478 OS << " Messaging=";
2479 if (Node->isMessagingGetter() && Node->isMessagingSetter())
2480 OS << "Getter&Setter";
2481 else if (Node->isMessagingGetter())
2482 OS << "Getter";
2483 else if (Node->isMessagingSetter())
2484 OS << "Setter";
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002485}
2486
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002487void ASTDumper::VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002488 VisitExpr(Node);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002489 if (Node->isArraySubscriptRefExpr())
2490 OS << " Kind=ArraySubscript GetterForArray=\"";
2491 else
2492 OS << " Kind=DictionarySubscript GetterForDictionary=\"";
2493 if (Node->getAtIndexMethodDecl())
Aaron Ballmanb190f972014-01-03 17:59:55 +00002494 Node->getAtIndexMethodDecl()->getSelector().print(OS);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002495 else
2496 OS << "(null)";
Alexander Kornienko61c93bd2012-12-11 15:28:09 +00002497
Ted Kremeneke65b0862012-03-06 20:05:56 +00002498 if (Node->isArraySubscriptRefExpr())
2499 OS << "\" SetterForArray=\"";
2500 else
2501 OS << "\" SetterForDictionary=\"";
2502 if (Node->setAtIndexMethodDecl())
Aaron Ballmanb190f972014-01-03 17:59:55 +00002503 Node->setAtIndexMethodDecl()->getSelector().print(OS);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002504 else
2505 OS << "(null)";
2506}
2507
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002508void ASTDumper::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node) {
Alexander Kornienko7bd0f9b2012-12-11 15:20:44 +00002509 VisitExpr(Node);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002510 OS << " " << (Node->getValue() ? "__objc_yes" : "__objc_no");
2511}
2512
Chris Lattnercbe4f772007-08-08 22:51:59 +00002513//===----------------------------------------------------------------------===//
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002514// Comments
2515//===----------------------------------------------------------------------===//
2516
2517const char *ASTDumper::getCommandName(unsigned CommandID) {
2518 if (Traits)
2519 return Traits->getCommandInfo(CommandID)->Name;
2520 const CommandInfo *Info = CommandTraits::getBuiltinCommandInfo(CommandID);
2521 if (Info)
2522 return Info->Name;
2523 return "<not a builtin command>";
2524}
2525
2526void ASTDumper::dumpFullComment(const FullComment *C) {
2527 if (!C)
2528 return;
2529
2530 FC = C;
2531 dumpComment(C);
Craig Topper36250ad2014-05-12 05:36:57 +00002532 FC = nullptr;
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002533}
2534
2535void ASTDumper::dumpComment(const Comment *C) {
Richard Smithf7514452014-10-30 21:02:37 +00002536 dumpChild([=] {
2537 if (!C) {
2538 ColorScope Color(*this, NullColor);
2539 OS << "<<<NULL>>>";
2540 return;
2541 }
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002542
Richard Smithf7514452014-10-30 21:02:37 +00002543 {
2544 ColorScope Color(*this, CommentColor);
2545 OS << C->getCommentKindName();
2546 }
2547 dumpPointer(C);
2548 dumpSourceRange(C->getSourceRange());
2549 ConstCommentVisitor<ASTDumper>::visit(C);
2550 for (Comment::child_iterator I = C->child_begin(), E = C->child_end();
2551 I != E; ++I)
2552 dumpComment(*I);
2553 });
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002554}
2555
2556void ASTDumper::visitTextComment(const TextComment *C) {
2557 OS << " Text=\"" << C->getText() << "\"";
2558}
2559
2560void ASTDumper::visitInlineCommandComment(const InlineCommandComment *C) {
2561 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
2562 switch (C->getRenderKind()) {
2563 case InlineCommandComment::RenderNormal:
2564 OS << " RenderNormal";
2565 break;
2566 case InlineCommandComment::RenderBold:
2567 OS << " RenderBold";
2568 break;
2569 case InlineCommandComment::RenderMonospaced:
2570 OS << " RenderMonospaced";
2571 break;
2572 case InlineCommandComment::RenderEmphasized:
2573 OS << " RenderEmphasized";
2574 break;
2575 }
2576
2577 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
2578 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
2579}
2580
2581void ASTDumper::visitHTMLStartTagComment(const HTMLStartTagComment *C) {
2582 OS << " Name=\"" << C->getTagName() << "\"";
2583 if (C->getNumAttrs() != 0) {
2584 OS << " Attrs: ";
2585 for (unsigned i = 0, e = C->getNumAttrs(); i != e; ++i) {
2586 const HTMLStartTagComment::Attribute &Attr = C->getAttr(i);
2587 OS << " \"" << Attr.Name << "=\"" << Attr.Value << "\"";
2588 }
2589 }
2590 if (C->isSelfClosing())
2591 OS << " SelfClosing";
2592}
2593
2594void ASTDumper::visitHTMLEndTagComment(const HTMLEndTagComment *C) {
2595 OS << " Name=\"" << C->getTagName() << "\"";
2596}
2597
2598void ASTDumper::visitBlockCommandComment(const BlockCommandComment *C) {
2599 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\"";
2600 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i)
2601 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\"";
2602}
2603
2604void ASTDumper::visitParamCommandComment(const ParamCommandComment *C) {
2605 OS << " " << ParamCommandComment::getDirectionAsString(C->getDirection());
2606
2607 if (C->isDirectionExplicit())
2608 OS << " explicitly";
2609 else
2610 OS << " implicitly";
2611
2612 if (C->hasParamName()) {
2613 if (C->isParamIndexValid())
2614 OS << " Param=\"" << C->getParamName(FC) << "\"";
2615 else
2616 OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
2617 }
2618
Dmitri Gribenkodbff5c72014-03-19 14:03:47 +00002619 if (C->isParamIndexValid() && !C->isVarArgParam())
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002620 OS << " ParamIndex=" << C->getParamIndex();
2621}
2622
2623void ASTDumper::visitTParamCommandComment(const TParamCommandComment *C) {
2624 if (C->hasParamName()) {
2625 if (C->isPositionValid())
2626 OS << " Param=\"" << C->getParamName(FC) << "\"";
2627 else
2628 OS << " Param=\"" << C->getParamNameAsWritten() << "\"";
2629 }
2630
2631 if (C->isPositionValid()) {
2632 OS << " Position=<";
2633 for (unsigned i = 0, e = C->getDepth(); i != e; ++i) {
2634 OS << C->getIndex(i);
2635 if (i != e - 1)
2636 OS << ", ";
2637 }
2638 OS << ">";
2639 }
2640}
2641
2642void ASTDumper::visitVerbatimBlockComment(const VerbatimBlockComment *C) {
2643 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\""
2644 " CloseName=\"" << C->getCloseName() << "\"";
2645}
2646
2647void ASTDumper::visitVerbatimBlockLineComment(
2648 const VerbatimBlockLineComment *C) {
2649 OS << " Text=\"" << C->getText() << "\"";
2650}
2651
2652void ASTDumper::visitVerbatimLineComment(const VerbatimLineComment *C) {
2653 OS << " Text=\"" << C->getText() << "\"";
2654}
2655
2656//===----------------------------------------------------------------------===//
Richard Smithd5e7ff82014-10-31 01:17:45 +00002657// Type method implementations
2658//===----------------------------------------------------------------------===//
2659
2660void QualType::dump(const char *msg) const {
2661 if (msg)
2662 llvm::errs() << msg << ": ";
2663 dump();
2664}
2665
Richard Smith14d04842016-11-02 23:57:18 +00002666LLVM_DUMP_METHOD void QualType::dump() const { dump(llvm::errs()); }
2667
2668LLVM_DUMP_METHOD void QualType::dump(llvm::raw_ostream &OS) const {
2669 ASTDumper Dumper(OS, nullptr, nullptr);
Richard Smithd5e7ff82014-10-31 01:17:45 +00002670 Dumper.dumpTypeAsChild(*this);
2671}
2672
Richard Smith14d04842016-11-02 23:57:18 +00002673LLVM_DUMP_METHOD void Type::dump() const { dump(llvm::errs()); }
2674
2675LLVM_DUMP_METHOD void Type::dump(llvm::raw_ostream &OS) const {
2676 QualType(this, 0).dump(OS);
2677}
Richard Smithd5e7ff82014-10-31 01:17:45 +00002678
2679//===----------------------------------------------------------------------===//
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002680// Decl method implementations
2681//===----------------------------------------------------------------------===//
2682
Alp Tokeref6b0072014-01-04 13:47:14 +00002683LLVM_DUMP_METHOD void Decl::dump() const { dump(llvm::errs()); }
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002684
Richard Smith3a36ac12017-03-09 22:00:01 +00002685LLVM_DUMP_METHOD void Decl::dump(raw_ostream &OS, bool Deserialize) const {
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002686 ASTDumper P(OS, &getASTContext().getCommentCommandTraits(),
2687 &getASTContext().getSourceManager());
Richard Smith3a36ac12017-03-09 22:00:01 +00002688 P.setDeserialize(Deserialize);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002689 P.dumpDecl(this);
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002690}
2691
Alp Tokeref6b0072014-01-04 13:47:14 +00002692LLVM_DUMP_METHOD void Decl::dumpColor() const {
Richard Trieud215b8d2013-01-26 01:31:20 +00002693 ASTDumper P(llvm::errs(), &getASTContext().getCommentCommandTraits(),
2694 &getASTContext().getSourceManager(), /*ShowColors*/true);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002695 P.dumpDecl(this);
Richard Trieud215b8d2013-01-26 01:31:20 +00002696}
Richard Smith33937e72013-06-22 21:49:40 +00002697
Alp Tokeref6b0072014-01-04 13:47:14 +00002698LLVM_DUMP_METHOD void DeclContext::dumpLookups() const {
Richard Smith6ea05822013-06-24 01:45:33 +00002699 dumpLookups(llvm::errs());
2700}
2701
Richard Smith35f986d2014-08-11 22:11:07 +00002702LLVM_DUMP_METHOD void DeclContext::dumpLookups(raw_ostream &OS,
Richard Smith3a36ac12017-03-09 22:00:01 +00002703 bool DumpDecls,
2704 bool Deserialize) const {
Richard Smith33937e72013-06-22 21:49:40 +00002705 const DeclContext *DC = this;
2706 while (!DC->isTranslationUnit())
2707 DC = DC->getParent();
2708 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
Richard Smith6ea05822013-06-24 01:45:33 +00002709 ASTDumper P(OS, &Ctx.getCommentCommandTraits(), &Ctx.getSourceManager());
Richard Smith3a36ac12017-03-09 22:00:01 +00002710 P.setDeserialize(Deserialize);
Richard Smith35f986d2014-08-11 22:11:07 +00002711 P.dumpLookups(this, DumpDecls);
Richard Smith33937e72013-06-22 21:49:40 +00002712}
2713
Alexander Kornienko90ff6072012-12-20 02:09:13 +00002714//===----------------------------------------------------------------------===//
Chris Lattnercbe4f772007-08-08 22:51:59 +00002715// Stmt method implementations
2716//===----------------------------------------------------------------------===//
2717
Alp Tokeref6b0072014-01-04 13:47:14 +00002718LLVM_DUMP_METHOD void Stmt::dump(SourceManager &SM) const {
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +00002719 dump(llvm::errs(), SM);
2720}
2721
Alp Tokeref6b0072014-01-04 13:47:14 +00002722LLVM_DUMP_METHOD void Stmt::dump(raw_ostream &OS, SourceManager &SM) const {
Craig Topper36250ad2014-05-12 05:36:57 +00002723 ASTDumper P(OS, nullptr, &SM);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002724 P.dumpStmt(this);
Chris Lattner779d5d92007-08-30 00:40:08 +00002725}
2726
Faisal Vali2da8ed92015-03-22 13:35:56 +00002727LLVM_DUMP_METHOD void Stmt::dump(raw_ostream &OS) const {
2728 ASTDumper P(OS, nullptr, nullptr);
2729 P.dumpStmt(this);
2730}
2731
Alp Tokeref6b0072014-01-04 13:47:14 +00002732LLVM_DUMP_METHOD void Stmt::dump() const {
Craig Topper36250ad2014-05-12 05:36:57 +00002733 ASTDumper P(llvm::errs(), nullptr, nullptr);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002734 P.dumpStmt(this);
Chris Lattnercbe4f772007-08-08 22:51:59 +00002735}
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002736
Alp Tokeref6b0072014-01-04 13:47:14 +00002737LLVM_DUMP_METHOD void Stmt::dumpColor() const {
Craig Topper36250ad2014-05-12 05:36:57 +00002738 ASTDumper P(llvm::errs(), nullptr, nullptr, /*ShowColors*/true);
Alexander Kornienko540bacb2013-02-01 12:35:51 +00002739 P.dumpStmt(this);
Richard Trieud215b8d2013-01-26 01:31:20 +00002740}
2741
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002742//===----------------------------------------------------------------------===//
2743// Comment method implementations
2744//===----------------------------------------------------------------------===//
2745
Craig Topper36250ad2014-05-12 05:36:57 +00002746LLVM_DUMP_METHOD void Comment::dump() const {
2747 dump(llvm::errs(), nullptr, nullptr);
2748}
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002749
Alp Tokeref6b0072014-01-04 13:47:14 +00002750LLVM_DUMP_METHOD void Comment::dump(const ASTContext &Context) const {
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002751 dump(llvm::errs(), &Context.getCommentCommandTraits(),
2752 &Context.getSourceManager());
2753}
2754
Alexander Kornienko00911f12013-01-15 12:20:21 +00002755void Comment::dump(raw_ostream &OS, const CommandTraits *Traits,
Alexander Kornienkoebc17b52013-01-14 14:07:11 +00002756 const SourceManager *SM) const {
2757 const FullComment *FC = dyn_cast<FullComment>(this);
2758 ASTDumper D(OS, Traits, SM);
2759 D.dumpFullComment(FC);
2760}
Richard Trieud215b8d2013-01-26 01:31:20 +00002761
Alp Tokeref6b0072014-01-04 13:47:14 +00002762LLVM_DUMP_METHOD void Comment::dumpColor() const {
Richard Trieud215b8d2013-01-26 01:31:20 +00002763 const FullComment *FC = dyn_cast<FullComment>(this);
Craig Topper36250ad2014-05-12 05:36:57 +00002764 ASTDumper D(llvm::errs(), nullptr, nullptr, /*ShowColors*/true);
Richard Trieud215b8d2013-01-26 01:31:20 +00002765 D.dumpFullComment(FC);
2766}