blob: cf71d6b986a2b8ed70a6e9d544dc2482692f39b5 [file] [log] [blame]
Chris Lattner6000dac2007-08-08 22:51:59 +00001//===--- StmtDumper.cpp - Dumping implementation for Stmt ASTs ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-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 Lattner6000dac2007-08-08 22:51:59 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Stmt::dump/Stmt::print methods, which dump out the
11// AST in a form that exposes type details and other fields.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/AST/StmtVisitor.h"
Ted Kremenek91d1d7a2007-10-17 18:36:42 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor2a3009a2009-02-03 19:21:40 +000017#include "clang/AST/DeclCXX.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000018#include "clang/AST/PrettyPrinter.h"
Chris Lattnere300c872007-08-30 06:17:34 +000019#include "clang/Basic/SourceManager.h"
Chris Lattner6000dac2007-08-08 22:51:59 +000020#include "llvm/Support/Compiler.h"
21#include <cstdio>
22using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// StmtDumper Visitor
26//===----------------------------------------------------------------------===//
27
28namespace {
Chris Lattnerc5598cb2007-08-21 04:04:25 +000029 class VISIBILITY_HIDDEN StmtDumper : public StmtVisitor<StmtDumper> {
Chris Lattnere300c872007-08-30 06:17:34 +000030 SourceManager *SM;
Chris Lattner6000dac2007-08-08 22:51:59 +000031 FILE *F;
32 unsigned IndentLevel;
Mike Stump1eb44332009-09-09 15:08:12 +000033
Chris Lattner6000dac2007-08-08 22:51:59 +000034 /// MaxDepth - When doing a normal dump (not dumpAll) we only want to dump
35 /// the first few levels of an AST. This keeps track of how many ast levels
36 /// are left.
37 unsigned MaxDepth;
Mike Stump1eb44332009-09-09 15:08:12 +000038
Chris Lattnere300c872007-08-30 06:17:34 +000039 /// LastLocFilename/LastLocLine - Keep track of the last location we print
40 /// out so that we can print out deltas from then on out.
41 const char *LastLocFilename;
42 unsigned LastLocLine;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000043
Chris Lattner6000dac2007-08-08 22:51:59 +000044 public:
Chris Lattnere300c872007-08-30 06:17:34 +000045 StmtDumper(SourceManager *sm, FILE *f, unsigned maxDepth)
46 : SM(sm), F(f), IndentLevel(0-1), MaxDepth(maxDepth) {
47 LastLocFilename = "";
48 LastLocLine = ~0U;
49 }
Mike Stump1eb44332009-09-09 15:08:12 +000050
Chris Lattnerf9e05812007-08-09 18:03:18 +000051 void DumpSubTree(Stmt *S) {
Chris Lattner6000dac2007-08-08 22:51:59 +000052 // Prune the recursion if not using dump all.
53 if (MaxDepth == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +000054
Chris Lattnerf9e05812007-08-09 18:03:18 +000055 ++IndentLevel;
Chris Lattner6000dac2007-08-08 22:51:59 +000056 if (S) {
Ted Kremenek5399ce22007-12-12 06:59:42 +000057 if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
58 VisitDeclStmt(DS);
Mike Stump1eb44332009-09-09 15:08:12 +000059 else {
Ted Kremenek5399ce22007-12-12 06:59:42 +000060 Visit(S);
Mike Stump1eb44332009-09-09 15:08:12 +000061
Ted Kremenek5399ce22007-12-12 06:59:42 +000062 // Print out children.
63 Stmt::child_iterator CI = S->child_begin(), CE = S->child_end();
64 if (CI != CE) {
65 while (CI != CE) {
66 fprintf(F, "\n");
67 DumpSubTree(*CI++);
68 }
Chris Lattnerb3938792007-08-30 00:53:54 +000069 }
Ted Kremenek5399ce22007-12-12 06:59:42 +000070 fprintf(F, ")");
Chris Lattnerb3938792007-08-30 00:53:54 +000071 }
Chris Lattner6000dac2007-08-08 22:51:59 +000072 } else {
73 Indent();
Chris Lattner4a70adb2007-08-26 03:53:29 +000074 fprintf(F, "<<<NULL>>>");
Chris Lattner6000dac2007-08-08 22:51:59 +000075 }
Chris Lattnerf9e05812007-08-09 18:03:18 +000076 --IndentLevel;
Chris Lattner6000dac2007-08-08 22:51:59 +000077 }
Mike Stump1eb44332009-09-09 15:08:12 +000078
Chris Lattnerf9e05812007-08-09 18:03:18 +000079 void DumpDeclarator(Decl *D);
Mike Stump1eb44332009-09-09 15:08:12 +000080
Chris Lattner6000dac2007-08-08 22:51:59 +000081 void Indent() const {
82 for (int i = 0, e = IndentLevel; i < e; ++i)
83 fprintf(F, " ");
84 }
Mike Stump1eb44332009-09-09 15:08:12 +000085
Steve Naroff9dcbfa42007-09-01 21:08:38 +000086 void DumpType(QualType T) {
Chris Lattnerfd8f7da2007-08-09 00:36:22 +000087 fprintf(F, "'%s'", T.getAsString().c_str());
88
Douglas Gregor61366e92008-12-24 00:01:03 +000089 if (!T.isNull()) {
John McCall0953e762009-09-24 19:53:00 +000090 // If the type is sugared, also dump a (shallow) desugared type.
91 QualType Simplified = T.getDesugaredType();
92 if (Simplified != T)
Douglas Gregor61366e92008-12-24 00:01:03 +000093 fprintf(F, ":'%s'", Simplified.getAsString().c_str());
Chris Lattnerbad37852008-04-02 05:06:23 +000094 }
Chris Lattnerfd8f7da2007-08-09 00:36:22 +000095 }
Steve Naroff9dcbfa42007-09-01 21:08:38 +000096 void DumpStmt(const Stmt *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +000097 Indent();
98 fprintf(F, "(%s %p", Node->getStmtClassName(), (void*)Node);
Steve Naroff9dcbfa42007-09-01 21:08:38 +000099 DumpSourceRange(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000100 }
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000101 void DumpExpr(const Expr *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000102 DumpStmt(Node);
Chris Lattnerfd8f7da2007-08-09 00:36:22 +0000103 fprintf(F, " ");
104 DumpType(Node->getType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000105 }
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000106 void DumpSourceRange(const Stmt *Node);
Chris Lattnere300c872007-08-30 06:17:34 +0000107 void DumpLocation(SourceLocation Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Chris Lattner17a1a722007-08-30 01:00:35 +0000109 // Stmts.
Chris Lattnerc5598cb2007-08-21 04:04:25 +0000110 void VisitStmt(Stmt *Node);
Ted Kremenek5399ce22007-12-12 06:59:42 +0000111 void VisitDeclStmt(DeclStmt *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000112 void VisitLabelStmt(LabelStmt *Node);
113 void VisitGotoStmt(GotoStmt *Node);
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Chris Lattner17a1a722007-08-30 01:00:35 +0000115 // Exprs
116 void VisitExpr(Expr *Node);
Anders Carlsson27a5b9b2009-08-22 23:33:40 +0000117 void VisitCastExpr(CastExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000118 void VisitDeclRefExpr(DeclRefExpr *Node);
Chris Lattnerd9f69102008-08-10 01:53:14 +0000119 void VisitPredefinedExpr(PredefinedExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000120 void VisitCharacterLiteral(CharacterLiteral *Node);
121 void VisitIntegerLiteral(IntegerLiteral *Node);
122 void VisitFloatingLiteral(FloatingLiteral *Node);
123 void VisitStringLiteral(StringLiteral *Str);
124 void VisitUnaryOperator(UnaryOperator *Node);
Sebastian Redl05189992008-11-11 17:56:53 +0000125 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000126 void VisitMemberExpr(MemberExpr *Node);
Nate Begeman213541a2008-04-18 23:10:10 +0000127 void VisitExtVectorElementExpr(ExtVectorElementExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000128 void VisitBinaryOperator(BinaryOperator *Node);
129 void VisitCompoundAssignOperator(CompoundAssignOperator *Node);
130 void VisitAddrLabelExpr(AddrLabelExpr *Node);
131 void VisitTypesCompatibleExpr(TypesCompatibleExpr *Node);
132
133 // C++
Douglas Gregor49badde2008-10-27 19:41:14 +0000134 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000135 void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node);
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000136 void VisitCXXThisExpr(CXXThisExpr *Node);
Douglas Gregor49badde2008-10-27 19:41:14 +0000137 void VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node);
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000138 void VisitCXXConstructExpr(CXXConstructExpr *Node);
139 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node);
140 void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *Node);
141 void DumpCXXTemporary(CXXTemporary *Temporary);
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Chris Lattner17a1a722007-08-30 01:00:35 +0000143 // ObjC
144 void VisitObjCEncodeExpr(ObjCEncodeExpr *Node);
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000145 void VisitObjCMessageExpr(ObjCMessageExpr* Node);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000146 void VisitObjCSelectorExpr(ObjCSelectorExpr *Node);
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000147 void VisitObjCProtocolExpr(ObjCProtocolExpr *Node);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000148 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node);
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000149 void VisitObjCImplicitSetterGetterRefExpr(
150 ObjCImplicitSetterGetterRefExpr *Node);
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000151 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node);
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000152 void VisitObjCSuperExpr(ObjCSuperExpr *Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000153 };
154}
155
156//===----------------------------------------------------------------------===//
Chris Lattnere300c872007-08-30 06:17:34 +0000157// Utilities
158//===----------------------------------------------------------------------===//
159
160void StmtDumper::DumpLocation(SourceLocation Loc) {
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000161 SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000163 if (SpellingLoc.isInvalid()) {
164 fprintf(stderr, "<invalid sloc>");
165 return;
166 }
Chris Lattnere300c872007-08-30 06:17:34 +0000167
168 // The general format we print out is filename:line:col, but we drop pieces
169 // that haven't changed since the last loc printed.
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000170 PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
171
172 if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
173 fprintf(stderr, "%s:%u:%u", PLoc.getFilename(), PLoc.getLine(),
174 PLoc.getColumn());
175 LastLocFilename = PLoc.getFilename();
176 LastLocLine = PLoc.getLine();
177 } else if (PLoc.getLine() != LastLocLine) {
178 fprintf(stderr, "line:%u:%u", PLoc.getLine(), PLoc.getColumn());
179 LastLocLine = PLoc.getLine();
Chris Lattnere300c872007-08-30 06:17:34 +0000180 } else {
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000181 fprintf(stderr, "col:%u", PLoc.getColumn());
Chris Lattnere300c872007-08-30 06:17:34 +0000182 }
183}
184
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000185void StmtDumper::DumpSourceRange(const Stmt *Node) {
Chris Lattnere300c872007-08-30 06:17:34 +0000186 // Can't translate locations if a SourceManager isn't available.
187 if (SM == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Chris Lattnere300c872007-08-30 06:17:34 +0000189 // TODO: If the parent expression is available, we can print a delta vs its
190 // location.
191 SourceRange R = Node->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Chris Lattnere300c872007-08-30 06:17:34 +0000193 fprintf(stderr, " <");
Chris Lattner311ff022007-10-16 22:36:42 +0000194 DumpLocation(R.getBegin());
195 if (R.getBegin() != R.getEnd()) {
Chris Lattnere300c872007-08-30 06:17:34 +0000196 fprintf(stderr, ", ");
Chris Lattner311ff022007-10-16 22:36:42 +0000197 DumpLocation(R.getEnd());
Chris Lattnere300c872007-08-30 06:17:34 +0000198 }
199 fprintf(stderr, ">");
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Chris Lattnere300c872007-08-30 06:17:34 +0000201 // <t2.c:123:421[blah], t2.c:412:321>
202
203}
204
205
206//===----------------------------------------------------------------------===//
Chris Lattner6000dac2007-08-08 22:51:59 +0000207// Stmt printing methods.
208//===----------------------------------------------------------------------===//
209
210void StmtDumper::VisitStmt(Stmt *Node) {
Chris Lattner17a1a722007-08-30 01:00:35 +0000211 DumpStmt(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000212}
213
Chris Lattnerf9e05812007-08-09 18:03:18 +0000214void StmtDumper::DumpDeclarator(Decl *D) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000215 // FIXME: Need to complete/beautify this... this code simply shows the
216 // nodes are where they need to be.
217 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000218 fprintf(F, "\"typedef %s %s\"",
219 localType->getUnderlyingType().getAsString().c_str(),
Chris Lattner8ec03f52008-11-24 03:54:41 +0000220 localType->getNameAsString().c_str());
Chris Lattner6000dac2007-08-08 22:51:59 +0000221 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000222 fprintf(F, "\"");
Chris Lattner6000dac2007-08-08 22:51:59 +0000223 // Emit storage class for vardecls.
224 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +0000225 if (V->getStorageClass() != VarDecl::None)
Mike Stump1eb44332009-09-09 15:08:12 +0000226 fprintf(F, "%s ",
Daniel Dunbar5466c7b2009-04-14 02:25:56 +0000227 VarDecl::getStorageClassSpecifierString(V->getStorageClass()));
Chris Lattner6000dac2007-08-08 22:51:59 +0000228 }
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Chris Lattner39f34e92008-11-24 04:00:27 +0000230 std::string Name = VD->getNameAsString();
Mike Stump1eb44332009-09-09 15:08:12 +0000231 VD->getType().getAsStringInternal(Name,
Chris Lattnere4f21422009-06-30 01:26:17 +0000232 PrintingPolicy(VD->getASTContext().getLangOptions()));
Chris Lattnerf9e05812007-08-09 18:03:18 +0000233 fprintf(F, "%s", Name.c_str());
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Chris Lattner6000dac2007-08-08 22:51:59 +0000235 // If this is a vardecl with an initializer, emit it.
236 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
237 if (V->getInit()) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000238 fprintf(F, " =\n");
239 DumpSubTree(V->getInit());
Chris Lattner6000dac2007-08-08 22:51:59 +0000240 }
241 }
Chris Lattnerf9e05812007-08-09 18:03:18 +0000242 fprintf(F, "\"");
Steve Naroff92199282007-11-17 21:37:36 +0000243 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
244 // print a free standing tag decl (e.g. "struct x;").
245 const char *tagname;
246 if (const IdentifierInfo *II = TD->getIdentifier())
Daniel Dunbare013d682009-10-18 20:26:12 +0000247 tagname = II->getNameStart();
Steve Naroff92199282007-11-17 21:37:36 +0000248 else
249 tagname = "<anonymous>";
250 fprintf(F, "\"%s %s;\"", TD->getKindName(), tagname);
251 // FIXME: print tag bodies.
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000252 } else if (UsingDirectiveDecl *UD = dyn_cast<UsingDirectiveDecl>(D)) {
253 // print using-directive decl (e.g. "using namespace x;")
254 const char *ns;
255 if (const IdentifierInfo *II = UD->getNominatedNamespace()->getIdentifier())
Daniel Dunbare013d682009-10-18 20:26:12 +0000256 ns = II->getNameStart();
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000257 else
258 ns = "<anonymous>";
259 fprintf(F, "\"%s %s;\"",UD->getDeclKindName(), ns);
Chris Lattner6000dac2007-08-08 22:51:59 +0000260 } else {
Chris Lattner6000dac2007-08-08 22:51:59 +0000261 assert(0 && "Unexpected decl");
262 }
Chris Lattner6000dac2007-08-08 22:51:59 +0000263}
264
Ted Kremenek5399ce22007-12-12 06:59:42 +0000265void StmtDumper::VisitDeclStmt(DeclStmt *Node) {
266 DumpStmt(Node);
267 fprintf(F,"\n");
Ted Kremenek04a72b72008-10-06 18:38:35 +0000268 for (DeclStmt::decl_iterator DI = Node->decl_begin(), DE = Node->decl_end();
269 DI != DE; ++DI) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000270 Decl* D = *DI;
Ted Kremenek5399ce22007-12-12 06:59:42 +0000271 ++IndentLevel;
272 Indent();
273 fprintf(F, "%p ", (void*) D);
274 DumpDeclarator(D);
Chris Lattnerf2797252009-03-29 16:04:50 +0000275 if (DI+1 != DE)
Ted Kremenek5399ce22007-12-12 06:59:42 +0000276 fprintf(F,"\n");
277 --IndentLevel;
278 }
279}
280
Chris Lattner6000dac2007-08-08 22:51:59 +0000281void StmtDumper::VisitLabelStmt(LabelStmt *Node) {
282 DumpStmt(Node);
Chris Lattner3cb640b2008-07-26 19:24:43 +0000283 fprintf(F, " '%s'", Node->getName());
Chris Lattner6000dac2007-08-08 22:51:59 +0000284}
285
Chris Lattner6000dac2007-08-08 22:51:59 +0000286void StmtDumper::VisitGotoStmt(GotoStmt *Node) {
287 DumpStmt(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000288 fprintf(F, " '%s':%p", Node->getLabel()->getName(), (void*)Node->getLabel());
Chris Lattner6000dac2007-08-08 22:51:59 +0000289}
290
Chris Lattner6000dac2007-08-08 22:51:59 +0000291//===----------------------------------------------------------------------===//
292// Expr printing methods.
293//===----------------------------------------------------------------------===//
294
295void StmtDumper::VisitExpr(Expr *Node) {
296 DumpExpr(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000297}
298
Anders Carlsson27a5b9b2009-08-22 23:33:40 +0000299void StmtDumper::VisitCastExpr(CastExpr *Node) {
300 DumpExpr(Node);
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000301 fprintf(F, " <%s>", Node->getCastKindName());
Anders Carlsson27a5b9b2009-08-22 23:33:40 +0000302}
303
Chris Lattner6000dac2007-08-08 22:51:59 +0000304void StmtDumper::VisitDeclRefExpr(DeclRefExpr *Node) {
305 DumpExpr(Node);
Ted Kremenekeb641f92007-09-10 17:32:55 +0000306
307 fprintf(F, " ");
308 switch (Node->getDecl()->getKind()) {
Anders Carlsson27a5b9b2009-08-22 23:33:40 +0000309 default: fprintf(F,"Decl"); break;
310 case Decl::Function: fprintf(F,"FunctionDecl"); break;
311 case Decl::Var: fprintf(F,"Var"); break;
312 case Decl::ParmVar: fprintf(F,"ParmVar"); break;
313 case Decl::EnumConstant: fprintf(F,"EnumConstant"); break;
314 case Decl::Typedef: fprintf(F,"Typedef"); break;
315 case Decl::Record: fprintf(F,"Record"); break;
316 case Decl::Enum: fprintf(F,"Enum"); break;
317 case Decl::CXXRecord: fprintf(F,"CXXRecord"); break;
318 case Decl::ObjCInterface: fprintf(F,"ObjCInterface"); break;
319 case Decl::ObjCClass: fprintf(F,"ObjCClass"); break;
Ted Kremenekeb641f92007-09-10 17:32:55 +0000320 }
Mike Stump1eb44332009-09-09 15:08:12 +0000321
322 fprintf(F, "='%s' %p", Node->getDecl()->getNameAsString().c_str(),
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000323 (void*)Node->getDecl());
Chris Lattner6000dac2007-08-08 22:51:59 +0000324}
325
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000326void StmtDumper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Steve Naroff466c2e32008-05-23 00:59:14 +0000327 DumpExpr(Node);
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000328
Mike Stump1eb44332009-09-09 15:08:12 +0000329 fprintf(F, " %sDecl='%s' %p", Node->getDecl()->getDeclKindName(),
Chris Lattner8ec03f52008-11-24 03:54:41 +0000330 Node->getDecl()->getNameAsString().c_str(), (void*)Node->getDecl());
Steve Naroff218543b2008-05-23 22:01:24 +0000331 if (Node->isFreeIvar())
332 fprintf(F, " isFreeIvar");
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000333}
334
Chris Lattnerd9f69102008-08-10 01:53:14 +0000335void StmtDumper::VisitPredefinedExpr(PredefinedExpr *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000336 DumpExpr(Node);
337 switch (Node->getIdentType()) {
Chris Lattner0d17f6f2008-06-21 18:04:54 +0000338 default: assert(0 && "unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000339 case PredefinedExpr::Func: fprintf(F, " __func__"); break;
340 case PredefinedExpr::Function: fprintf(F, " __FUNCTION__"); break;
341 case PredefinedExpr::PrettyFunction: fprintf(F, " __PRETTY_FUNCTION__");break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000342 }
343}
344
345void StmtDumper::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnera0df31a2007-08-09 01:04:32 +0000346 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000347 fprintf(F, " %d", Node->getValue());
Chris Lattner6000dac2007-08-08 22:51:59 +0000348}
349
350void StmtDumper::VisitIntegerLiteral(IntegerLiteral *Node) {
351 DumpExpr(Node);
352
353 bool isSigned = Node->getType()->isSignedIntegerType();
Chris Lattnerb3938792007-08-30 00:53:54 +0000354 fprintf(F, " %s", Node->getValue().toString(10, isSigned).c_str());
Chris Lattner6000dac2007-08-08 22:51:59 +0000355}
356void StmtDumper::VisitFloatingLiteral(FloatingLiteral *Node) {
357 DumpExpr(Node);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000358 fprintf(F, " %f", Node->getValueAsApproximateDouble());
Chris Lattner6000dac2007-08-08 22:51:59 +0000359}
Chris Lattner5d661452007-08-26 03:42:43 +0000360
Chris Lattner6000dac2007-08-08 22:51:59 +0000361void StmtDumper::VisitStringLiteral(StringLiteral *Str) {
Chris Lattnera0df31a2007-08-09 01:04:32 +0000362 DumpExpr(Str);
363 // FIXME: this doesn't print wstrings right.
Chris Lattner5fc61072007-08-09 17:14:24 +0000364 fprintf(F, " %s\"", Str->isWide() ? "L" : "");
Chris Lattnera0df31a2007-08-09 01:04:32 +0000365
Chris Lattner6000dac2007-08-08 22:51:59 +0000366 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattner5fc61072007-08-09 17:14:24 +0000367 switch (char C = Str->getStrData()[i]) {
368 default:
369 if (isprint(C))
Mike Stump1eb44332009-09-09 15:08:12 +0000370 fputc(C, F);
Chris Lattner5fc61072007-08-09 17:14:24 +0000371 else
372 fprintf(F, "\\%03o", C);
373 break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000374 // Handle some common ones to make dumps prettier.
Chris Lattner5fc61072007-08-09 17:14:24 +0000375 case '\\': fprintf(F, "\\\\"); break;
376 case '"': fprintf(F, "\\\""); break;
377 case '\n': fprintf(F, "\\n"); break;
378 case '\t': fprintf(F, "\\t"); break;
379 case '\a': fprintf(F, "\\a"); break;
380 case '\b': fprintf(F, "\\b"); break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000381 }
382 }
Chris Lattnerb3938792007-08-30 00:53:54 +0000383 fprintf(F, "\"");
Chris Lattner6000dac2007-08-08 22:51:59 +0000384}
Chris Lattner17a1a722007-08-30 01:00:35 +0000385
Chris Lattner6000dac2007-08-08 22:51:59 +0000386void StmtDumper::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000387 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000388 fprintf(F, " %s '%s'", Node->isPostfix() ? "postfix" : "prefix",
Chris Lattner13cb21f2007-08-09 17:35:30 +0000389 UnaryOperator::getOpcodeStr(Node->getOpcode()));
Chris Lattner6000dac2007-08-08 22:51:59 +0000390}
Sebastian Redl05189992008-11-11 17:56:53 +0000391void StmtDumper::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000392 DumpExpr(Node);
393 fprintf(F, " %s ", Node->isSizeOf() ? "sizeof" : "alignof");
Sebastian Redl05189992008-11-11 17:56:53 +0000394 if (Node->isArgumentType())
395 DumpType(Node->getArgumentType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000396}
Chris Lattner13cb21f2007-08-09 17:35:30 +0000397
Chris Lattner6000dac2007-08-08 22:51:59 +0000398void StmtDumper::VisitMemberExpr(MemberExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000399 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000400 fprintf(F, " %s%s %p", Node->isArrow() ? "->" : ".",
Mike Stump1eb44332009-09-09 15:08:12 +0000401 Node->getMemberDecl()->getNameAsString().c_str(),
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000402 (void*)Node->getMemberDecl());
Chris Lattner6000dac2007-08-08 22:51:59 +0000403}
Nate Begeman213541a2008-04-18 23:10:10 +0000404void StmtDumper::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000405 DumpExpr(Node);
Daniel Dunbare013d682009-10-18 20:26:12 +0000406 fprintf(F, " %s", Node->getAccessor().getNameStart());
Chris Lattner6000dac2007-08-08 22:51:59 +0000407}
Chris Lattner6000dac2007-08-08 22:51:59 +0000408void StmtDumper::VisitBinaryOperator(BinaryOperator *Node) {
409 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000410 fprintf(F, " '%s'", BinaryOperator::getOpcodeStr(Node->getOpcode()));
Chris Lattnereb14fe82007-08-25 02:00:02 +0000411}
412void StmtDumper::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
413 DumpExpr(Node);
Eli Friedmanab3a8522009-03-28 01:22:36 +0000414 fprintf(F, " '%s' ComputeLHSTy=",
Chris Lattnereb14fe82007-08-25 02:00:02 +0000415 BinaryOperator::getOpcodeStr(Node->getOpcode()));
Eli Friedmanab3a8522009-03-28 01:22:36 +0000416 DumpType(Node->getComputationLHSType());
417 fprintf(F, " ComputeResultTy=");
418 DumpType(Node->getComputationResultType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000419}
Chris Lattner6000dac2007-08-08 22:51:59 +0000420
421// GNU extensions.
422
423void StmtDumper::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000424 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000425 fprintf(F, " %s %p", Node->getLabel()->getName(), (void*)Node->getLabel());
Chris Lattner6000dac2007-08-08 22:51:59 +0000426}
427
Chris Lattner6000dac2007-08-08 22:51:59 +0000428void StmtDumper::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000429 DumpExpr(Node);
430 fprintf(F, " ");
431 DumpType(Node->getArgType1());
432 fprintf(F, " ");
433 DumpType(Node->getArgType2());
Chris Lattner6000dac2007-08-08 22:51:59 +0000434}
435
Chris Lattnerf9e05812007-08-09 18:03:18 +0000436//===----------------------------------------------------------------------===//
437// C++ Expressions
438//===----------------------------------------------------------------------===//
Chris Lattner6000dac2007-08-08 22:51:59 +0000439
Douglas Gregor49badde2008-10-27 19:41:14 +0000440void StmtDumper::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000441 DumpExpr(Node);
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000442 fprintf(F, " %s<%s> <%s>", Node->getCastName(),
443 Node->getTypeAsWritten().getAsString().c_str(),
444 Node->getCastKindName());
Chris Lattner6000dac2007-08-08 22:51:59 +0000445}
446
447void StmtDumper::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000448 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000449 fprintf(F, " %s", Node->getValue() ? "true" : "false");
Chris Lattner6000dac2007-08-08 22:51:59 +0000450}
451
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000452void StmtDumper::VisitCXXThisExpr(CXXThisExpr *Node) {
453 DumpExpr(Node);
454 fprintf(F, " this");
455}
456
Douglas Gregor49badde2008-10-27 19:41:14 +0000457void StmtDumper::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
458 DumpExpr(Node);
Mike Stump1eb44332009-09-09 15:08:12 +0000459 fprintf(F, " functional cast to %s",
Douglas Gregor49badde2008-10-27 19:41:14 +0000460 Node->getTypeAsWritten().getAsString().c_str());
461}
462
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000463void StmtDumper::VisitCXXConstructExpr(CXXConstructExpr *Node) {
464 DumpExpr(Node);
465 if (Node->isElidable())
Anders Carlsson0e67f9d2009-08-14 02:39:47 +0000466 fprintf(F, " elidable");
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000467}
468
469void StmtDumper::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
470 DumpExpr(Node);
471 fprintf(F, " ");
472 DumpCXXTemporary(Node->getTemporary());
473}
474
475void StmtDumper::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *Node) {
476 DumpExpr(Node);
477 ++IndentLevel;
478 for (unsigned i = 0, e = Node->getNumTemporaries(); i != e; ++i) {
479 fprintf(F, "\n");
480 Indent();
481 DumpCXXTemporary(Node->getTemporary(i));
482 }
483 --IndentLevel;
484}
485
486void StmtDumper::DumpCXXTemporary(CXXTemporary *Temporary) {
487 fprintf(F, "(CXXTemporary %p)", (void *)Temporary);
488}
489
Anders Carlsson55085182007-08-21 17:43:55 +0000490//===----------------------------------------------------------------------===//
491// Obj-C Expressions
492//===----------------------------------------------------------------------===//
493
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000494void StmtDumper::VisitObjCMessageExpr(ObjCMessageExpr* Node) {
495 DumpExpr(Node);
Chris Lattner077bf5e2008-11-24 03:33:13 +0000496 fprintf(F, " selector=%s", Node->getSelector().getAsString().c_str());
Ted Kremenekea958e572008-05-01 17:26:20 +0000497 IdentifierInfo* clsName = Node->getClassName();
Daniel Dunbare013d682009-10-18 20:26:12 +0000498 if (clsName) fprintf(F, " class=%s", clsName->getNameStart());
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000499}
500
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000501void StmtDumper::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
502 DumpExpr(Node);
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000504 fprintf(F, " ");
505 DumpType(Node->getEncodedType());
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000506}
507
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000508void StmtDumper::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
509 DumpExpr(Node);
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000511 fprintf(F, " ");
Chris Lattner077bf5e2008-11-24 03:33:13 +0000512 fprintf(F, "%s", Node->getSelector().getAsString().c_str());
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000513}
514
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000515void StmtDumper::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
516 DumpExpr(Node);
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000518 fprintf(F, " ");
Chris Lattner8ec03f52008-11-24 03:54:41 +0000519 fprintf(F, "%s", Node->getProtocol()->getNameAsString().c_str());
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000520}
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000521
522void StmtDumper::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
523 DumpExpr(Node);
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000524
Mike Stump1eb44332009-09-09 15:08:12 +0000525 fprintf(F, " Kind=PropertyRef Property=\"%s\"",
Chris Lattner8ec03f52008-11-24 03:54:41 +0000526 Node->getProperty()->getNameAsString().c_str());
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000527}
528
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000529void StmtDumper::VisitObjCImplicitSetterGetterRefExpr(
530 ObjCImplicitSetterGetterRefExpr *Node) {
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000531 DumpExpr(Node);
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000533 ObjCMethodDecl *Getter = Node->getGetterMethod();
534 ObjCMethodDecl *Setter = Node->getSetterMethod();
Mike Stump1eb44332009-09-09 15:08:12 +0000535 fprintf(F, " Kind=MethodRef Getter=\"%s\" Setter=\"%s\"",
Chris Lattner077bf5e2008-11-24 03:33:13 +0000536 Getter->getSelector().getAsString().c_str(),
537 Setter ? Setter->getSelector().getAsString().c_str() : "(null)");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000538}
539
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000540void StmtDumper::VisitObjCSuperExpr(ObjCSuperExpr *Node) {
541 DumpExpr(Node);
542 fprintf(F, " super");
543}
544
Chris Lattner6000dac2007-08-08 22:51:59 +0000545//===----------------------------------------------------------------------===//
546// Stmt method implementations
547//===----------------------------------------------------------------------===//
548
549/// dump - This does a local dump of the specified AST fragment. It dumps the
550/// specified node and a few nodes underneath it, but not the whole subtree.
551/// This is useful in a debugger.
Chris Lattnere300c872007-08-30 06:17:34 +0000552void Stmt::dump(SourceManager &SM) const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000553 StmtDumper P(&SM, stderr, 4);
Chris Lattnerb3938792007-08-30 00:53:54 +0000554 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattner0c727a32007-08-30 00:40:08 +0000555 fprintf(stderr, "\n");
556}
557
558/// dump - This does a local dump of the specified AST fragment. It dumps the
559/// specified node and a few nodes underneath it, but not the whole subtree.
560/// This is useful in a debugger.
Chris Lattner6000dac2007-08-08 22:51:59 +0000561void Stmt::dump() const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000562 StmtDumper P(0, stderr, 4);
Chris Lattnerb3938792007-08-30 00:53:54 +0000563 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattner0c727a32007-08-30 00:40:08 +0000564 fprintf(stderr, "\n");
565}
566
567/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
Chris Lattnere300c872007-08-30 06:17:34 +0000568void Stmt::dumpAll(SourceManager &SM) const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000569 StmtDumper P(&SM, stderr, ~0U);
Chris Lattnerb3938792007-08-30 00:53:54 +0000570 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattneree41ce52007-08-10 21:51:12 +0000571 fprintf(stderr, "\n");
Chris Lattner6000dac2007-08-08 22:51:59 +0000572}
573
574/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
575void Stmt::dumpAll() const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000576 StmtDumper P(0, stderr, ~0U);
Chris Lattnerb3938792007-08-30 00:53:54 +0000577 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattneree41ce52007-08-10 21:51:12 +0000578 fprintf(stderr, "\n");
Chris Lattner6000dac2007-08-08 22:51:59 +0000579}