blob: 7d8d64cb63ab22fbc00fa17b6cde690478d330d3 [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;
33
34 /// 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;
Chris Lattnere300c872007-08-30 06:17:34 +000038
39 /// 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 }
Chris Lattner6000dac2007-08-08 22:51:59 +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;
54
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);
59 else {
60 Visit(S);
61
62 // 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 }
78
Chris Lattnerf9e05812007-08-09 18:03:18 +000079 void DumpDeclarator(Decl *D);
Chris Lattner6000dac2007-08-08 22:51:59 +000080
81 void Indent() const {
82 for (int i = 0, e = IndentLevel; i < e; ++i)
83 fprintf(F, " ");
84 }
85
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()) {
90 // If the type is directly a typedef, strip off typedefness to give at
91 // least one level of concreteness.
92 if (TypedefType *TDT = dyn_cast<TypedefType>(T)) {
93 QualType Simplified =
94 TDT->LookThroughTypedefs().getQualifiedType(T.getCVRQualifiers());
95 fprintf(F, ":'%s'", Simplified.getAsString().c_str());
96 }
Chris Lattnerbad37852008-04-02 05:06:23 +000097 }
Chris Lattnerfd8f7da2007-08-09 00:36:22 +000098 }
Steve Naroff9dcbfa42007-09-01 21:08:38 +000099 void DumpStmt(const Stmt *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000100 Indent();
101 fprintf(F, "(%s %p", Node->getStmtClassName(), (void*)Node);
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000102 DumpSourceRange(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000103 }
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000104 void DumpExpr(const Expr *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000105 DumpStmt(Node);
Chris Lattnerfd8f7da2007-08-09 00:36:22 +0000106 fprintf(F, " ");
107 DumpType(Node->getType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000108 }
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000109 void DumpSourceRange(const Stmt *Node);
Chris Lattnere300c872007-08-30 06:17:34 +0000110 void DumpLocation(SourceLocation Loc);
Chris Lattnere300c872007-08-30 06:17:34 +0000111
Chris Lattner17a1a722007-08-30 01:00:35 +0000112 // Stmts.
Chris Lattnerc5598cb2007-08-21 04:04:25 +0000113 void VisitStmt(Stmt *Node);
Ted Kremenek5399ce22007-12-12 06:59:42 +0000114 void VisitDeclStmt(DeclStmt *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000115 void VisitLabelStmt(LabelStmt *Node);
116 void VisitGotoStmt(GotoStmt *Node);
117
118 // Exprs
119 void VisitExpr(Expr *Node);
Anders Carlsson27a5b9b2009-08-22 23:33:40 +0000120 void VisitCastExpr(CastExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000121 void VisitDeclRefExpr(DeclRefExpr *Node);
Chris Lattnerd9f69102008-08-10 01:53:14 +0000122 void VisitPredefinedExpr(PredefinedExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000123 void VisitCharacterLiteral(CharacterLiteral *Node);
124 void VisitIntegerLiteral(IntegerLiteral *Node);
125 void VisitFloatingLiteral(FloatingLiteral *Node);
126 void VisitStringLiteral(StringLiteral *Str);
127 void VisitUnaryOperator(UnaryOperator *Node);
Sebastian Redl05189992008-11-11 17:56:53 +0000128 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000129 void VisitMemberExpr(MemberExpr *Node);
Nate Begeman213541a2008-04-18 23:10:10 +0000130 void VisitExtVectorElementExpr(ExtVectorElementExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000131 void VisitBinaryOperator(BinaryOperator *Node);
132 void VisitCompoundAssignOperator(CompoundAssignOperator *Node);
133 void VisitAddrLabelExpr(AddrLabelExpr *Node);
134 void VisitTypesCompatibleExpr(TypesCompatibleExpr *Node);
135
136 // C++
Douglas Gregor49badde2008-10-27 19:41:14 +0000137 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000138 void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node);
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000139 void VisitCXXThisExpr(CXXThisExpr *Node);
Douglas Gregor49badde2008-10-27 19:41:14 +0000140 void VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node);
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000141 void VisitCXXConstructExpr(CXXConstructExpr *Node);
142 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node);
143 void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *Node);
144 void DumpCXXTemporary(CXXTemporary *Temporary);
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000145
Chris Lattner17a1a722007-08-30 01:00:35 +0000146 // ObjC
147 void VisitObjCEncodeExpr(ObjCEncodeExpr *Node);
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000148 void VisitObjCMessageExpr(ObjCMessageExpr* Node);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000149 void VisitObjCSelectorExpr(ObjCSelectorExpr *Node);
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000150 void VisitObjCProtocolExpr(ObjCProtocolExpr *Node);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000151 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node);
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000152 void VisitObjCImplicitSetterGetterRefExpr(
153 ObjCImplicitSetterGetterRefExpr *Node);
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000154 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node);
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000155 void VisitObjCSuperExpr(ObjCSuperExpr *Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000156 };
157}
158
159//===----------------------------------------------------------------------===//
Chris Lattnere300c872007-08-30 06:17:34 +0000160// Utilities
161//===----------------------------------------------------------------------===//
162
163void StmtDumper::DumpLocation(SourceLocation Loc) {
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000164 SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000165
166 if (SpellingLoc.isInvalid()) {
167 fprintf(stderr, "<invalid sloc>");
168 return;
169 }
Chris Lattnere300c872007-08-30 06:17:34 +0000170
171 // The general format we print out is filename:line:col, but we drop pieces
172 // that haven't changed since the last loc printed.
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000173 PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
174
175 if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
176 fprintf(stderr, "%s:%u:%u", PLoc.getFilename(), PLoc.getLine(),
177 PLoc.getColumn());
178 LastLocFilename = PLoc.getFilename();
179 LastLocLine = PLoc.getLine();
180 } else if (PLoc.getLine() != LastLocLine) {
181 fprintf(stderr, "line:%u:%u", PLoc.getLine(), PLoc.getColumn());
182 LastLocLine = PLoc.getLine();
Chris Lattnere300c872007-08-30 06:17:34 +0000183 } else {
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000184 fprintf(stderr, "col:%u", PLoc.getColumn());
Chris Lattnere300c872007-08-30 06:17:34 +0000185 }
186}
187
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000188void StmtDumper::DumpSourceRange(const Stmt *Node) {
Chris Lattnere300c872007-08-30 06:17:34 +0000189 // Can't translate locations if a SourceManager isn't available.
190 if (SM == 0) return;
191
192 // TODO: If the parent expression is available, we can print a delta vs its
193 // location.
194 SourceRange R = Node->getSourceRange();
195
196 fprintf(stderr, " <");
Chris Lattner311ff022007-10-16 22:36:42 +0000197 DumpLocation(R.getBegin());
198 if (R.getBegin() != R.getEnd()) {
Chris Lattnere300c872007-08-30 06:17:34 +0000199 fprintf(stderr, ", ");
Chris Lattner311ff022007-10-16 22:36:42 +0000200 DumpLocation(R.getEnd());
Chris Lattnere300c872007-08-30 06:17:34 +0000201 }
202 fprintf(stderr, ">");
203
204 // <t2.c:123:421[blah], t2.c:412:321>
205
206}
207
208
209//===----------------------------------------------------------------------===//
Chris Lattner6000dac2007-08-08 22:51:59 +0000210// Stmt printing methods.
211//===----------------------------------------------------------------------===//
212
213void StmtDumper::VisitStmt(Stmt *Node) {
Chris Lattner17a1a722007-08-30 01:00:35 +0000214 DumpStmt(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000215}
216
Chris Lattnerf9e05812007-08-09 18:03:18 +0000217void StmtDumper::DumpDeclarator(Decl *D) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000218 // FIXME: Need to complete/beautify this... this code simply shows the
219 // nodes are where they need to be.
220 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000221 fprintf(F, "\"typedef %s %s\"",
222 localType->getUnderlyingType().getAsString().c_str(),
Chris Lattner8ec03f52008-11-24 03:54:41 +0000223 localType->getNameAsString().c_str());
Chris Lattner6000dac2007-08-08 22:51:59 +0000224 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000225 fprintf(F, "\"");
Chris Lattner6000dac2007-08-08 22:51:59 +0000226 // Emit storage class for vardecls.
227 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +0000228 if (V->getStorageClass() != VarDecl::None)
229 fprintf(F, "%s ",
230 VarDecl::getStorageClassSpecifierString(V->getStorageClass()));
Chris Lattner6000dac2007-08-08 22:51:59 +0000231 }
232
Chris Lattner39f34e92008-11-24 04:00:27 +0000233 std::string Name = VD->getNameAsString();
Chris Lattnere4f21422009-06-30 01:26:17 +0000234 VD->getType().getAsStringInternal(Name,
235 PrintingPolicy(VD->getASTContext().getLangOptions()));
Chris Lattnerf9e05812007-08-09 18:03:18 +0000236 fprintf(F, "%s", Name.c_str());
Chris Lattner6000dac2007-08-08 22:51:59 +0000237
238 // If this is a vardecl with an initializer, emit it.
239 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
240 if (V->getInit()) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000241 fprintf(F, " =\n");
242 DumpSubTree(V->getInit());
Chris Lattner6000dac2007-08-08 22:51:59 +0000243 }
244 }
Chris Lattnerf9e05812007-08-09 18:03:18 +0000245 fprintf(F, "\"");
Steve Naroff92199282007-11-17 21:37:36 +0000246 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
247 // print a free standing tag decl (e.g. "struct x;").
248 const char *tagname;
249 if (const IdentifierInfo *II = TD->getIdentifier())
250 tagname = II->getName();
251 else
252 tagname = "<anonymous>";
253 fprintf(F, "\"%s %s;\"", TD->getKindName(), tagname);
254 // FIXME: print tag bodies.
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000255 } else if (UsingDirectiveDecl *UD = dyn_cast<UsingDirectiveDecl>(D)) {
256 // print using-directive decl (e.g. "using namespace x;")
257 const char *ns;
258 if (const IdentifierInfo *II = UD->getNominatedNamespace()->getIdentifier())
259 ns = II->getName();
260 else
261 ns = "<anonymous>";
262 fprintf(F, "\"%s %s;\"",UD->getDeclKindName(), ns);
Chris Lattner6000dac2007-08-08 22:51:59 +0000263 } else {
Chris Lattner6000dac2007-08-08 22:51:59 +0000264 assert(0 && "Unexpected decl");
265 }
Chris Lattner6000dac2007-08-08 22:51:59 +0000266}
267
Ted Kremenek5399ce22007-12-12 06:59:42 +0000268void StmtDumper::VisitDeclStmt(DeclStmt *Node) {
269 DumpStmt(Node);
270 fprintf(F,"\n");
Ted Kremenek04a72b72008-10-06 18:38:35 +0000271 for (DeclStmt::decl_iterator DI = Node->decl_begin(), DE = Node->decl_end();
272 DI != DE; ++DI) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000273 Decl* D = *DI;
Ted Kremenek5399ce22007-12-12 06:59:42 +0000274 ++IndentLevel;
275 Indent();
276 fprintf(F, "%p ", (void*) D);
277 DumpDeclarator(D);
Chris Lattnerf2797252009-03-29 16:04:50 +0000278 if (DI+1 != DE)
Ted Kremenek5399ce22007-12-12 06:59:42 +0000279 fprintf(F,"\n");
280 --IndentLevel;
281 }
282}
283
Chris Lattner6000dac2007-08-08 22:51:59 +0000284void StmtDumper::VisitLabelStmt(LabelStmt *Node) {
285 DumpStmt(Node);
Chris Lattner3cb640b2008-07-26 19:24:43 +0000286 fprintf(F, " '%s'", Node->getName());
Chris Lattner6000dac2007-08-08 22:51:59 +0000287}
288
Chris Lattner6000dac2007-08-08 22:51:59 +0000289void StmtDumper::VisitGotoStmt(GotoStmt *Node) {
290 DumpStmt(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000291 fprintf(F, " '%s':%p", Node->getLabel()->getName(), (void*)Node->getLabel());
Chris Lattner6000dac2007-08-08 22:51:59 +0000292}
293
Chris Lattner6000dac2007-08-08 22:51:59 +0000294//===----------------------------------------------------------------------===//
295// Expr printing methods.
296//===----------------------------------------------------------------------===//
297
298void StmtDumper::VisitExpr(Expr *Node) {
299 DumpExpr(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000300}
301
Anders Carlsson27a5b9b2009-08-22 23:33:40 +0000302void StmtDumper::VisitCastExpr(CastExpr *Node) {
303 DumpExpr(Node);
304 fprintf(F, " ");
305 switch (Node->getCastKind()) {
306 case CastExpr::CK_Unknown:
307 fprintf(F, "<Unknown>");
308 break;
309 case CastExpr::CK_BitCast:
310 fprintf(F, "<BitCast>");
311 break;
312 case CastExpr::CK_NoOp:
313 fprintf(F, "<NoOp>");
314 break;
315 case CastExpr::CK_DerivedToBase:
316 fprintf(F, "<DerivedToBase>");
317 break;
318 case CastExpr::CK_Dynamic:
319 fprintf(F, "<Dynamic>");
320 break;
321 case CastExpr::CK_ToUnion:
322 fprintf(F, "<ToUnion>");
323 break;
324 case CastExpr::CK_ArrayToPointerDecay:
325 fprintf(F, "<ArrayToPointerDecay>");
326 break;
Anders Carlssonb633c4e2009-09-01 20:37:18 +0000327 case CastExpr::CK_FunctionToPointerDecay:
328 fprintf(F, "<FunctionToPointerDecay>");
329 break;
Anders Carlsson27a5b9b2009-08-22 23:33:40 +0000330 case CastExpr::CK_NullToMemberPointer:
331 fprintf(F, "<NullToMemberPointer>");
332 break;
333 case CastExpr::CK_BaseToDerivedMemberPointer:
334 fprintf(F, "<BaseToDerivedMemberPointer>");
335 break;
Mike Stumpbe3289d2009-08-26 21:11:25 +0000336 case CastExpr::CK_UserDefinedConversion:
337 fprintf(F, "<UserDefinedConversion>");
338 break;
Fariborz Jahanian7fe5d722009-08-28 22:04:50 +0000339 case CastExpr::CK_ConstructorConversion:
340 fprintf(F, "<ConstructorConversion>");
341 break;
Anders Carlsson27a5b9b2009-08-22 23:33:40 +0000342 }
343}
344
Chris Lattner6000dac2007-08-08 22:51:59 +0000345void StmtDumper::VisitDeclRefExpr(DeclRefExpr *Node) {
346 DumpExpr(Node);
Ted Kremenekeb641f92007-09-10 17:32:55 +0000347
348 fprintf(F, " ");
349 switch (Node->getDecl()->getKind()) {
Anders Carlsson27a5b9b2009-08-22 23:33:40 +0000350 default: fprintf(F,"Decl"); break;
351 case Decl::Function: fprintf(F,"FunctionDecl"); break;
352 case Decl::Var: fprintf(F,"Var"); break;
353 case Decl::ParmVar: fprintf(F,"ParmVar"); break;
354 case Decl::EnumConstant: fprintf(F,"EnumConstant"); break;
355 case Decl::Typedef: fprintf(F,"Typedef"); break;
356 case Decl::Record: fprintf(F,"Record"); break;
357 case Decl::Enum: fprintf(F,"Enum"); break;
358 case Decl::CXXRecord: fprintf(F,"CXXRecord"); break;
359 case Decl::ObjCInterface: fprintf(F,"ObjCInterface"); break;
360 case Decl::ObjCClass: fprintf(F,"ObjCClass"); break;
Ted Kremenekeb641f92007-09-10 17:32:55 +0000361 }
362
Chris Lattner39f34e92008-11-24 04:00:27 +0000363 fprintf(F, "='%s' %p", Node->getDecl()->getNameAsString().c_str(),
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000364 (void*)Node->getDecl());
Chris Lattner6000dac2007-08-08 22:51:59 +0000365}
366
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000367void StmtDumper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Steve Naroff466c2e32008-05-23 00:59:14 +0000368 DumpExpr(Node);
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000369
Steve Naroff466c2e32008-05-23 00:59:14 +0000370 fprintf(F, " %sDecl='%s' %p", Node->getDecl()->getDeclKindName(),
Chris Lattner8ec03f52008-11-24 03:54:41 +0000371 Node->getDecl()->getNameAsString().c_str(), (void*)Node->getDecl());
Steve Naroff218543b2008-05-23 22:01:24 +0000372 if (Node->isFreeIvar())
373 fprintf(F, " isFreeIvar");
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000374}
375
Chris Lattnerd9f69102008-08-10 01:53:14 +0000376void StmtDumper::VisitPredefinedExpr(PredefinedExpr *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000377 DumpExpr(Node);
378 switch (Node->getIdentType()) {
Chris Lattner0d17f6f2008-06-21 18:04:54 +0000379 default: assert(0 && "unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000380 case PredefinedExpr::Func: fprintf(F, " __func__"); break;
381 case PredefinedExpr::Function: fprintf(F, " __FUNCTION__"); break;
382 case PredefinedExpr::PrettyFunction: fprintf(F, " __PRETTY_FUNCTION__");break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000383 }
384}
385
386void StmtDumper::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnera0df31a2007-08-09 01:04:32 +0000387 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000388 fprintf(F, " %d", Node->getValue());
Chris Lattner6000dac2007-08-08 22:51:59 +0000389}
390
391void StmtDumper::VisitIntegerLiteral(IntegerLiteral *Node) {
392 DumpExpr(Node);
393
394 bool isSigned = Node->getType()->isSignedIntegerType();
Chris Lattnerb3938792007-08-30 00:53:54 +0000395 fprintf(F, " %s", Node->getValue().toString(10, isSigned).c_str());
Chris Lattner6000dac2007-08-08 22:51:59 +0000396}
397void StmtDumper::VisitFloatingLiteral(FloatingLiteral *Node) {
398 DumpExpr(Node);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000399 fprintf(F, " %f", Node->getValueAsApproximateDouble());
Chris Lattner6000dac2007-08-08 22:51:59 +0000400}
Chris Lattner5d661452007-08-26 03:42:43 +0000401
Chris Lattner6000dac2007-08-08 22:51:59 +0000402void StmtDumper::VisitStringLiteral(StringLiteral *Str) {
Chris Lattnera0df31a2007-08-09 01:04:32 +0000403 DumpExpr(Str);
404 // FIXME: this doesn't print wstrings right.
Chris Lattner5fc61072007-08-09 17:14:24 +0000405 fprintf(F, " %s\"", Str->isWide() ? "L" : "");
Chris Lattnera0df31a2007-08-09 01:04:32 +0000406
Chris Lattner6000dac2007-08-08 22:51:59 +0000407 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattner5fc61072007-08-09 17:14:24 +0000408 switch (char C = Str->getStrData()[i]) {
409 default:
410 if (isprint(C))
411 fputc(C, F);
412 else
413 fprintf(F, "\\%03o", C);
414 break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000415 // Handle some common ones to make dumps prettier.
Chris Lattner5fc61072007-08-09 17:14:24 +0000416 case '\\': fprintf(F, "\\\\"); break;
417 case '"': fprintf(F, "\\\""); break;
418 case '\n': fprintf(F, "\\n"); break;
419 case '\t': fprintf(F, "\\t"); break;
420 case '\a': fprintf(F, "\\a"); break;
421 case '\b': fprintf(F, "\\b"); break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000422 }
423 }
Chris Lattnerb3938792007-08-30 00:53:54 +0000424 fprintf(F, "\"");
Chris Lattner6000dac2007-08-08 22:51:59 +0000425}
Chris Lattner17a1a722007-08-30 01:00:35 +0000426
Chris Lattner6000dac2007-08-08 22:51:59 +0000427void StmtDumper::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000428 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000429 fprintf(F, " %s '%s'", Node->isPostfix() ? "postfix" : "prefix",
Chris Lattner13cb21f2007-08-09 17:35:30 +0000430 UnaryOperator::getOpcodeStr(Node->getOpcode()));
Chris Lattner6000dac2007-08-08 22:51:59 +0000431}
Sebastian Redl05189992008-11-11 17:56:53 +0000432void StmtDumper::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000433 DumpExpr(Node);
434 fprintf(F, " %s ", Node->isSizeOf() ? "sizeof" : "alignof");
Sebastian Redl05189992008-11-11 17:56:53 +0000435 if (Node->isArgumentType())
436 DumpType(Node->getArgumentType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000437}
Chris Lattner13cb21f2007-08-09 17:35:30 +0000438
Chris Lattner6000dac2007-08-08 22:51:59 +0000439void StmtDumper::VisitMemberExpr(MemberExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000440 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000441 fprintf(F, " %s%s %p", Node->isArrow() ? "->" : ".",
Chris Lattner077bf5e2008-11-24 03:33:13 +0000442 Node->getMemberDecl()->getNameAsString().c_str(),
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000443 (void*)Node->getMemberDecl());
Chris Lattner6000dac2007-08-08 22:51:59 +0000444}
Nate Begeman213541a2008-04-18 23:10:10 +0000445void StmtDumper::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000446 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000447 fprintf(F, " %s", Node->getAccessor().getName());
Chris Lattner6000dac2007-08-08 22:51:59 +0000448}
Chris Lattner6000dac2007-08-08 22:51:59 +0000449void StmtDumper::VisitBinaryOperator(BinaryOperator *Node) {
450 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000451 fprintf(F, " '%s'", BinaryOperator::getOpcodeStr(Node->getOpcode()));
Chris Lattnereb14fe82007-08-25 02:00:02 +0000452}
453void StmtDumper::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
454 DumpExpr(Node);
Eli Friedmanab3a8522009-03-28 01:22:36 +0000455 fprintf(F, " '%s' ComputeLHSTy=",
Chris Lattnereb14fe82007-08-25 02:00:02 +0000456 BinaryOperator::getOpcodeStr(Node->getOpcode()));
Eli Friedmanab3a8522009-03-28 01:22:36 +0000457 DumpType(Node->getComputationLHSType());
458 fprintf(F, " ComputeResultTy=");
459 DumpType(Node->getComputationResultType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000460}
Chris Lattner6000dac2007-08-08 22:51:59 +0000461
462// GNU extensions.
463
464void StmtDumper::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000465 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000466 fprintf(F, " %s %p", Node->getLabel()->getName(), (void*)Node->getLabel());
Chris Lattner6000dac2007-08-08 22:51:59 +0000467}
468
Chris Lattner6000dac2007-08-08 22:51:59 +0000469void StmtDumper::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000470 DumpExpr(Node);
471 fprintf(F, " ");
472 DumpType(Node->getArgType1());
473 fprintf(F, " ");
474 DumpType(Node->getArgType2());
Chris Lattner6000dac2007-08-08 22:51:59 +0000475}
476
Chris Lattnerf9e05812007-08-09 18:03:18 +0000477//===----------------------------------------------------------------------===//
478// C++ Expressions
479//===----------------------------------------------------------------------===//
Chris Lattner6000dac2007-08-08 22:51:59 +0000480
Douglas Gregor49badde2008-10-27 19:41:14 +0000481void StmtDumper::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000482 DumpExpr(Node);
Douglas Gregor49badde2008-10-27 19:41:14 +0000483 fprintf(F, " %s<%s>", Node->getCastName(),
484 Node->getTypeAsWritten().getAsString().c_str());
Chris Lattner6000dac2007-08-08 22:51:59 +0000485}
486
487void StmtDumper::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000488 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000489 fprintf(F, " %s", Node->getValue() ? "true" : "false");
Chris Lattner6000dac2007-08-08 22:51:59 +0000490}
491
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000492void StmtDumper::VisitCXXThisExpr(CXXThisExpr *Node) {
493 DumpExpr(Node);
494 fprintf(F, " this");
495}
496
Douglas Gregor49badde2008-10-27 19:41:14 +0000497void StmtDumper::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
498 DumpExpr(Node);
499 fprintf(F, " functional cast to %s",
500 Node->getTypeAsWritten().getAsString().c_str());
501}
502
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000503void StmtDumper::VisitCXXConstructExpr(CXXConstructExpr *Node) {
504 DumpExpr(Node);
505 if (Node->isElidable())
Anders Carlsson0e67f9d2009-08-14 02:39:47 +0000506 fprintf(F, " elidable");
Anders Carlsson0eca1b62009-08-12 00:21:52 +0000507}
508
509void StmtDumper::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
510 DumpExpr(Node);
511 fprintf(F, " ");
512 DumpCXXTemporary(Node->getTemporary());
513}
514
515void StmtDumper::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *Node) {
516 DumpExpr(Node);
517 ++IndentLevel;
518 for (unsigned i = 0, e = Node->getNumTemporaries(); i != e; ++i) {
519 fprintf(F, "\n");
520 Indent();
521 DumpCXXTemporary(Node->getTemporary(i));
522 }
523 --IndentLevel;
524}
525
526void StmtDumper::DumpCXXTemporary(CXXTemporary *Temporary) {
527 fprintf(F, "(CXXTemporary %p)", (void *)Temporary);
528}
529
Anders Carlsson55085182007-08-21 17:43:55 +0000530//===----------------------------------------------------------------------===//
531// Obj-C Expressions
532//===----------------------------------------------------------------------===//
533
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000534void StmtDumper::VisitObjCMessageExpr(ObjCMessageExpr* Node) {
535 DumpExpr(Node);
Chris Lattner077bf5e2008-11-24 03:33:13 +0000536 fprintf(F, " selector=%s", Node->getSelector().getAsString().c_str());
Ted Kremenekea958e572008-05-01 17:26:20 +0000537 IdentifierInfo* clsName = Node->getClassName();
538 if (clsName) fprintf(F, " class=%s", clsName->getName());
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000539}
540
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000541void StmtDumper::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
542 DumpExpr(Node);
543
544 fprintf(F, " ");
545 DumpType(Node->getEncodedType());
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000546}
547
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000548void StmtDumper::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
549 DumpExpr(Node);
550
551 fprintf(F, " ");
Chris Lattner077bf5e2008-11-24 03:33:13 +0000552 fprintf(F, "%s", Node->getSelector().getAsString().c_str());
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000553}
554
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000555void StmtDumper::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
556 DumpExpr(Node);
557
558 fprintf(F, " ");
Chris Lattner8ec03f52008-11-24 03:54:41 +0000559 fprintf(F, "%s", Node->getProtocol()->getNameAsString().c_str());
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000560}
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000561
562void StmtDumper::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
563 DumpExpr(Node);
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000564
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000565 fprintf(F, " Kind=PropertyRef Property=\"%s\"",
Chris Lattner8ec03f52008-11-24 03:54:41 +0000566 Node->getProperty()->getNameAsString().c_str());
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000567}
568
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000569void StmtDumper::VisitObjCImplicitSetterGetterRefExpr(
570 ObjCImplicitSetterGetterRefExpr *Node) {
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000571 DumpExpr(Node);
572
573 ObjCMethodDecl *Getter = Node->getGetterMethod();
574 ObjCMethodDecl *Setter = Node->getSetterMethod();
575 fprintf(F, " Kind=MethodRef Getter=\"%s\" Setter=\"%s\"",
Chris Lattner077bf5e2008-11-24 03:33:13 +0000576 Getter->getSelector().getAsString().c_str(),
577 Setter ? Setter->getSelector().getAsString().c_str() : "(null)");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000578}
579
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000580void StmtDumper::VisitObjCSuperExpr(ObjCSuperExpr *Node) {
581 DumpExpr(Node);
582 fprintf(F, " super");
583}
584
Chris Lattner6000dac2007-08-08 22:51:59 +0000585//===----------------------------------------------------------------------===//
586// Stmt method implementations
587//===----------------------------------------------------------------------===//
588
589/// dump - This does a local dump of the specified AST fragment. It dumps the
590/// specified node and a few nodes underneath it, but not the whole subtree.
591/// This is useful in a debugger.
Chris Lattnere300c872007-08-30 06:17:34 +0000592void Stmt::dump(SourceManager &SM) const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000593 StmtDumper P(&SM, stderr, 4);
Chris Lattnerb3938792007-08-30 00:53:54 +0000594 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattner0c727a32007-08-30 00:40:08 +0000595 fprintf(stderr, "\n");
596}
597
598/// dump - This does a local dump of the specified AST fragment. It dumps the
599/// specified node and a few nodes underneath it, but not the whole subtree.
600/// This is useful in a debugger.
Chris Lattner6000dac2007-08-08 22:51:59 +0000601void Stmt::dump() const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000602 StmtDumper P(0, stderr, 4);
Chris Lattnerb3938792007-08-30 00:53:54 +0000603 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattner0c727a32007-08-30 00:40:08 +0000604 fprintf(stderr, "\n");
605}
606
607/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
Chris Lattnere300c872007-08-30 06:17:34 +0000608void Stmt::dumpAll(SourceManager &SM) const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000609 StmtDumper P(&SM, stderr, ~0U);
Chris Lattnerb3938792007-08-30 00:53:54 +0000610 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattneree41ce52007-08-10 21:51:12 +0000611 fprintf(stderr, "\n");
Chris Lattner6000dac2007-08-08 22:51:59 +0000612}
613
614/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
615void Stmt::dumpAll() const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000616 StmtDumper P(0, stderr, ~0U);
Chris Lattnerb3938792007-08-30 00:53:54 +0000617 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattneree41ce52007-08-10 21:51:12 +0000618 fprintf(stderr, "\n");
Chris Lattner6000dac2007-08-08 22:51:59 +0000619}