blob: 666cad6260397782e6c438c509bb3ab5c5d05c05 [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"
Chris Lattnere300c872007-08-30 06:17:34 +000017#include "clang/Basic/SourceManager.h"
Chris Lattner6000dac2007-08-08 22:51:59 +000018#include "llvm/Support/Compiler.h"
19#include <cstdio>
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// StmtDumper Visitor
24//===----------------------------------------------------------------------===//
25
26namespace {
Chris Lattnerc5598cb2007-08-21 04:04:25 +000027 class VISIBILITY_HIDDEN StmtDumper : public StmtVisitor<StmtDumper> {
Chris Lattnere300c872007-08-30 06:17:34 +000028 SourceManager *SM;
Chris Lattner6000dac2007-08-08 22:51:59 +000029 FILE *F;
30 unsigned IndentLevel;
31
32 /// MaxDepth - When doing a normal dump (not dumpAll) we only want to dump
33 /// the first few levels of an AST. This keeps track of how many ast levels
34 /// are left.
35 unsigned MaxDepth;
Chris Lattnere300c872007-08-30 06:17:34 +000036
37 /// LastLocFilename/LastLocLine - Keep track of the last location we print
38 /// out so that we can print out deltas from then on out.
39 const char *LastLocFilename;
40 unsigned LastLocLine;
Chris Lattner6000dac2007-08-08 22:51:59 +000041 public:
Chris Lattnere300c872007-08-30 06:17:34 +000042 StmtDumper(SourceManager *sm, FILE *f, unsigned maxDepth)
43 : SM(sm), F(f), IndentLevel(0-1), MaxDepth(maxDepth) {
44 LastLocFilename = "";
45 LastLocLine = ~0U;
46 }
Chris Lattner6000dac2007-08-08 22:51:59 +000047
Chris Lattnerf9e05812007-08-09 18:03:18 +000048 void DumpSubTree(Stmt *S) {
Chris Lattner6000dac2007-08-08 22:51:59 +000049 // Prune the recursion if not using dump all.
50 if (MaxDepth == 0) return;
51
Chris Lattnerf9e05812007-08-09 18:03:18 +000052 ++IndentLevel;
Chris Lattner6000dac2007-08-08 22:51:59 +000053 if (S) {
Ted Kremenek5399ce22007-12-12 06:59:42 +000054 if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
55 VisitDeclStmt(DS);
56 else {
57 Visit(S);
58
59 // Print out children.
60 Stmt::child_iterator CI = S->child_begin(), CE = S->child_end();
61 if (CI != CE) {
62 while (CI != CE) {
63 fprintf(F, "\n");
64 DumpSubTree(*CI++);
65 }
Chris Lattnerb3938792007-08-30 00:53:54 +000066 }
Ted Kremenek5399ce22007-12-12 06:59:42 +000067 fprintf(F, ")");
Chris Lattnerb3938792007-08-30 00:53:54 +000068 }
Chris Lattner6000dac2007-08-08 22:51:59 +000069 } else {
70 Indent();
Chris Lattner4a70adb2007-08-26 03:53:29 +000071 fprintf(F, "<<<NULL>>>");
Chris Lattner6000dac2007-08-08 22:51:59 +000072 }
Chris Lattnerf9e05812007-08-09 18:03:18 +000073 --IndentLevel;
Chris Lattner6000dac2007-08-08 22:51:59 +000074 }
75
Chris Lattnerf9e05812007-08-09 18:03:18 +000076 void DumpDeclarator(Decl *D);
Chris Lattner6000dac2007-08-08 22:51:59 +000077
78 void Indent() const {
79 for (int i = 0, e = IndentLevel; i < e; ++i)
80 fprintf(F, " ");
81 }
82
Steve Naroff9dcbfa42007-09-01 21:08:38 +000083 void DumpType(QualType T) {
Chris Lattnerfd8f7da2007-08-09 00:36:22 +000084 fprintf(F, "'%s'", T.getAsString().c_str());
85
86 // If the type is directly a typedef, strip off typedefness to give at
87 // least one level of concreteness.
Chris Lattnerbad37852008-04-02 05:06:23 +000088 if (TypedefType *TDT = dyn_cast<TypedefType>(T)) {
89 QualType Simplified =
90 TDT->LookThroughTypedefs().getQualifiedType(T.getCVRQualifiers());
91 fprintf(F, ":'%s'", Simplified.getAsString().c_str());
92 }
Chris Lattnerfd8f7da2007-08-09 00:36:22 +000093 }
Steve Naroff9dcbfa42007-09-01 21:08:38 +000094 void DumpStmt(const Stmt *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +000095 Indent();
96 fprintf(F, "(%s %p", Node->getStmtClassName(), (void*)Node);
Steve Naroff9dcbfa42007-09-01 21:08:38 +000097 DumpSourceRange(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +000098 }
Steve Naroff9dcbfa42007-09-01 21:08:38 +000099 void DumpExpr(const Expr *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000100 DumpStmt(Node);
Chris Lattnerfd8f7da2007-08-09 00:36:22 +0000101 fprintf(F, " ");
102 DumpType(Node->getType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000103 }
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000104 void DumpSourceRange(const Stmt *Node);
Chris Lattnere300c872007-08-30 06:17:34 +0000105 void DumpLocation(SourceLocation Loc);
Chris Lattnere300c872007-08-30 06:17:34 +0000106
Chris Lattner17a1a722007-08-30 01:00:35 +0000107 // Stmts.
Chris Lattnerc5598cb2007-08-21 04:04:25 +0000108 void VisitStmt(Stmt *Node);
Ted Kremenek5399ce22007-12-12 06:59:42 +0000109 void VisitDeclStmt(DeclStmt *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000110 void VisitLabelStmt(LabelStmt *Node);
111 void VisitGotoStmt(GotoStmt *Node);
112
113 // Exprs
114 void VisitExpr(Expr *Node);
115 void VisitDeclRefExpr(DeclRefExpr *Node);
Chris Lattnerd9f69102008-08-10 01:53:14 +0000116 void VisitPredefinedExpr(PredefinedExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000117 void VisitCharacterLiteral(CharacterLiteral *Node);
118 void VisitIntegerLiteral(IntegerLiteral *Node);
119 void VisitFloatingLiteral(FloatingLiteral *Node);
120 void VisitStringLiteral(StringLiteral *Str);
121 void VisitUnaryOperator(UnaryOperator *Node);
122 void VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node);
123 void VisitMemberExpr(MemberExpr *Node);
Nate Begeman213541a2008-04-18 23:10:10 +0000124 void VisitExtVectorElementExpr(ExtVectorElementExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000125 void VisitBinaryOperator(BinaryOperator *Node);
126 void VisitCompoundAssignOperator(CompoundAssignOperator *Node);
127 void VisitAddrLabelExpr(AddrLabelExpr *Node);
128 void VisitTypesCompatibleExpr(TypesCompatibleExpr *Node);
129
130 // C++
131 void VisitCXXCastExpr(CXXCastExpr *Node);
132 void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node);
133
134 // ObjC
135 void VisitObjCEncodeExpr(ObjCEncodeExpr *Node);
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000136 void VisitObjCMessageExpr(ObjCMessageExpr* Node);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000137 void VisitObjCSelectorExpr(ObjCSelectorExpr *Node);
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000138 void VisitObjCProtocolExpr(ObjCProtocolExpr *Node);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000139 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node);
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000140 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000141 };
142}
143
144//===----------------------------------------------------------------------===//
Chris Lattnere300c872007-08-30 06:17:34 +0000145// Utilities
146//===----------------------------------------------------------------------===//
147
148void StmtDumper::DumpLocation(SourceLocation Loc) {
149 SourceLocation PhysLoc = SM->getPhysicalLoc(Loc);
150
151 // The general format we print out is filename:line:col, but we drop pieces
152 // that haven't changed since the last loc printed.
153 const char *Filename = SM->getSourceName(PhysLoc);
154 unsigned LineNo = SM->getLineNumber(PhysLoc);
155 if (strcmp(Filename, LastLocFilename) != 0) {
156 fprintf(stderr, "%s:%u:%u", Filename, LineNo, SM->getColumnNumber(PhysLoc));
157 LastLocFilename = Filename;
158 LastLocLine = LineNo;
159 } else if (LineNo != LastLocLine) {
160 fprintf(stderr, "line:%u:%u", LineNo, SM->getColumnNumber(PhysLoc));
161 LastLocLine = LineNo;
162 } else {
163 fprintf(stderr, "col:%u", SM->getColumnNumber(PhysLoc));
164 }
165}
166
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000167void StmtDumper::DumpSourceRange(const Stmt *Node) {
Chris Lattnere300c872007-08-30 06:17:34 +0000168 // Can't translate locations if a SourceManager isn't available.
169 if (SM == 0) return;
170
171 // TODO: If the parent expression is available, we can print a delta vs its
172 // location.
173 SourceRange R = Node->getSourceRange();
174
175 fprintf(stderr, " <");
Chris Lattner311ff022007-10-16 22:36:42 +0000176 DumpLocation(R.getBegin());
177 if (R.getBegin() != R.getEnd()) {
Chris Lattnere300c872007-08-30 06:17:34 +0000178 fprintf(stderr, ", ");
Chris Lattner311ff022007-10-16 22:36:42 +0000179 DumpLocation(R.getEnd());
Chris Lattnere300c872007-08-30 06:17:34 +0000180 }
181 fprintf(stderr, ">");
182
183 // <t2.c:123:421[blah], t2.c:412:321>
184
185}
186
187
188//===----------------------------------------------------------------------===//
Chris Lattner6000dac2007-08-08 22:51:59 +0000189// Stmt printing methods.
190//===----------------------------------------------------------------------===//
191
192void StmtDumper::VisitStmt(Stmt *Node) {
Chris Lattner17a1a722007-08-30 01:00:35 +0000193 DumpStmt(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000194}
195
Chris Lattnerf9e05812007-08-09 18:03:18 +0000196void StmtDumper::DumpDeclarator(Decl *D) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000197 // FIXME: Need to complete/beautify this... this code simply shows the
198 // nodes are where they need to be.
199 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000200 fprintf(F, "\"typedef %s %s\"",
201 localType->getUnderlyingType().getAsString().c_str(),
202 localType->getName());
Chris Lattner6000dac2007-08-08 22:51:59 +0000203 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000204 fprintf(F, "\"");
Chris Lattner6000dac2007-08-08 22:51:59 +0000205 // Emit storage class for vardecls.
206 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
207 switch (V->getStorageClass()) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000208 default: assert(0 && "Unknown storage class!");
209 case VarDecl::None: break;
210 case VarDecl::Extern: fprintf(F, "extern "); break;
211 case VarDecl::Static: fprintf(F, "static "); break;
212 case VarDecl::Auto: fprintf(F, "auto "); break;
213 case VarDecl::Register: fprintf(F, "register "); break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000214 }
215 }
216
217 std::string Name = VD->getName();
218 VD->getType().getAsStringInternal(Name);
Chris Lattnerf9e05812007-08-09 18:03:18 +0000219 fprintf(F, "%s", Name.c_str());
Chris Lattner6000dac2007-08-08 22:51:59 +0000220
221 // If this is a vardecl with an initializer, emit it.
222 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
223 if (V->getInit()) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000224 fprintf(F, " =\n");
225 DumpSubTree(V->getInit());
Chris Lattner6000dac2007-08-08 22:51:59 +0000226 }
227 }
Chris Lattnerf9e05812007-08-09 18:03:18 +0000228 fprintf(F, "\"");
Steve Naroff92199282007-11-17 21:37:36 +0000229 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
230 // print a free standing tag decl (e.g. "struct x;").
231 const char *tagname;
232 if (const IdentifierInfo *II = TD->getIdentifier())
233 tagname = II->getName();
234 else
235 tagname = "<anonymous>";
236 fprintf(F, "\"%s %s;\"", TD->getKindName(), tagname);
237 // FIXME: print tag bodies.
Chris Lattner6000dac2007-08-08 22:51:59 +0000238 } else {
Chris Lattner6000dac2007-08-08 22:51:59 +0000239 assert(0 && "Unexpected decl");
240 }
Chris Lattner6000dac2007-08-08 22:51:59 +0000241}
242
Ted Kremenek5399ce22007-12-12 06:59:42 +0000243void StmtDumper::VisitDeclStmt(DeclStmt *Node) {
244 DumpStmt(Node);
245 fprintf(F,"\n");
Ted Kremenek04a72b72008-10-06 18:38:35 +0000246 for (DeclStmt::decl_iterator DI = Node->decl_begin(), DE = Node->decl_end();
247 DI != DE; ++DI) {
248 ScopedDecl* D = *DI;
Ted Kremenek5399ce22007-12-12 06:59:42 +0000249 ++IndentLevel;
250 Indent();
251 fprintf(F, "%p ", (void*) D);
252 DumpDeclarator(D);
253 if (D->getNextDeclarator())
254 fprintf(F,"\n");
255 --IndentLevel;
256 }
257}
258
Chris Lattner6000dac2007-08-08 22:51:59 +0000259void StmtDumper::VisitLabelStmt(LabelStmt *Node) {
260 DumpStmt(Node);
Chris Lattner3cb640b2008-07-26 19:24:43 +0000261 fprintf(F, " '%s'", Node->getName());
Chris Lattner6000dac2007-08-08 22:51:59 +0000262}
263
Chris Lattner6000dac2007-08-08 22:51:59 +0000264void StmtDumper::VisitGotoStmt(GotoStmt *Node) {
265 DumpStmt(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000266 fprintf(F, " '%s':%p", Node->getLabel()->getName(), (void*)Node->getLabel());
Chris Lattner6000dac2007-08-08 22:51:59 +0000267}
268
Chris Lattner6000dac2007-08-08 22:51:59 +0000269//===----------------------------------------------------------------------===//
270// Expr printing methods.
271//===----------------------------------------------------------------------===//
272
273void StmtDumper::VisitExpr(Expr *Node) {
274 DumpExpr(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000275}
276
277void StmtDumper::VisitDeclRefExpr(DeclRefExpr *Node) {
278 DumpExpr(Node);
Ted Kremenekeb641f92007-09-10 17:32:55 +0000279
280 fprintf(F, " ");
281 switch (Node->getDecl()->getKind()) {
282 case Decl::Function: fprintf(F,"FunctionDecl"); break;
Steve Naroff248a7532008-04-15 22:42:06 +0000283 case Decl::Var: fprintf(F,"Var"); break;
Chris Lattneraa9fc462007-10-08 21:37:32 +0000284 case Decl::ParmVar: fprintf(F,"ParmVar"); break;
Ted Kremenekeb641f92007-09-10 17:32:55 +0000285 case Decl::EnumConstant: fprintf(F,"EnumConstant"); break;
286 case Decl::Typedef: fprintf(F,"Typedef"); break;
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000287 case Decl::Record: fprintf(F,"Record"); break;
Ted Kremenekeb641f92007-09-10 17:32:55 +0000288 case Decl::Enum: fprintf(F,"Enum"); break;
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000289 case Decl::CXXRecord: fprintf(F,"CXXRecord"); break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000290 case Decl::ObjCInterface: fprintf(F,"ObjCInterface"); break;
291 case Decl::ObjCClass: fprintf(F,"ObjCClass"); break;
Ted Kremenekeb641f92007-09-10 17:32:55 +0000292 default: fprintf(F,"Decl"); break;
293 }
294
295 fprintf(F, "='%s' %p", Node->getDecl()->getName(), (void*)Node->getDecl());
Chris Lattner6000dac2007-08-08 22:51:59 +0000296}
297
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000298void StmtDumper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Steve Naroff466c2e32008-05-23 00:59:14 +0000299 DumpExpr(Node);
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000300
Steve Naroff466c2e32008-05-23 00:59:14 +0000301 fprintf(F, " %sDecl='%s' %p", Node->getDecl()->getDeclKindName(),
302 Node->getDecl()->getName(), (void*)Node->getDecl());
Steve Naroff218543b2008-05-23 22:01:24 +0000303 if (Node->isFreeIvar())
304 fprintf(F, " isFreeIvar");
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000305}
306
Chris Lattnerd9f69102008-08-10 01:53:14 +0000307void StmtDumper::VisitPredefinedExpr(PredefinedExpr *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000308 DumpExpr(Node);
309 switch (Node->getIdentType()) {
Chris Lattner0d17f6f2008-06-21 18:04:54 +0000310 default: assert(0 && "unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000311 case PredefinedExpr::Func: fprintf(F, " __func__"); break;
312 case PredefinedExpr::Function: fprintf(F, " __FUNCTION__"); break;
313 case PredefinedExpr::PrettyFunction: fprintf(F, " __PRETTY_FUNCTION__");break;
314 case PredefinedExpr::ObjCSuper: fprintf(F, "super"); break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000315 }
316}
317
318void StmtDumper::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnera0df31a2007-08-09 01:04:32 +0000319 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000320 fprintf(F, " %d", Node->getValue());
Chris Lattner6000dac2007-08-08 22:51:59 +0000321}
322
323void StmtDumper::VisitIntegerLiteral(IntegerLiteral *Node) {
324 DumpExpr(Node);
325
326 bool isSigned = Node->getType()->isSignedIntegerType();
Chris Lattnerb3938792007-08-30 00:53:54 +0000327 fprintf(F, " %s", Node->getValue().toString(10, isSigned).c_str());
Chris Lattner6000dac2007-08-08 22:51:59 +0000328}
329void StmtDumper::VisitFloatingLiteral(FloatingLiteral *Node) {
330 DumpExpr(Node);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000331 fprintf(F, " %f", Node->getValueAsApproximateDouble());
Chris Lattner6000dac2007-08-08 22:51:59 +0000332}
Chris Lattner5d661452007-08-26 03:42:43 +0000333
Chris Lattner6000dac2007-08-08 22:51:59 +0000334void StmtDumper::VisitStringLiteral(StringLiteral *Str) {
Chris Lattnera0df31a2007-08-09 01:04:32 +0000335 DumpExpr(Str);
336 // FIXME: this doesn't print wstrings right.
Chris Lattner5fc61072007-08-09 17:14:24 +0000337 fprintf(F, " %s\"", Str->isWide() ? "L" : "");
Chris Lattnera0df31a2007-08-09 01:04:32 +0000338
Chris Lattner6000dac2007-08-08 22:51:59 +0000339 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattner5fc61072007-08-09 17:14:24 +0000340 switch (char C = Str->getStrData()[i]) {
341 default:
342 if (isprint(C))
343 fputc(C, F);
344 else
345 fprintf(F, "\\%03o", C);
346 break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000347 // Handle some common ones to make dumps prettier.
Chris Lattner5fc61072007-08-09 17:14:24 +0000348 case '\\': fprintf(F, "\\\\"); break;
349 case '"': fprintf(F, "\\\""); break;
350 case '\n': fprintf(F, "\\n"); break;
351 case '\t': fprintf(F, "\\t"); break;
352 case '\a': fprintf(F, "\\a"); break;
353 case '\b': fprintf(F, "\\b"); break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000354 }
355 }
Chris Lattnerb3938792007-08-30 00:53:54 +0000356 fprintf(F, "\"");
Chris Lattner6000dac2007-08-08 22:51:59 +0000357}
Chris Lattner17a1a722007-08-30 01:00:35 +0000358
Chris Lattner6000dac2007-08-08 22:51:59 +0000359void StmtDumper::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000360 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000361 fprintf(F, " %s '%s'", Node->isPostfix() ? "postfix" : "prefix",
Chris Lattner13cb21f2007-08-09 17:35:30 +0000362 UnaryOperator::getOpcodeStr(Node->getOpcode()));
Chris Lattner6000dac2007-08-08 22:51:59 +0000363}
364void StmtDumper::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000365 DumpExpr(Node);
366 fprintf(F, " %s ", Node->isSizeOf() ? "sizeof" : "alignof");
367 DumpType(Node->getArgumentType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000368}
Chris Lattner13cb21f2007-08-09 17:35:30 +0000369
Chris Lattner6000dac2007-08-08 22:51:59 +0000370void StmtDumper::VisitMemberExpr(MemberExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000371 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000372 fprintf(F, " %s%s %p", Node->isArrow() ? "->" : ".",
Chris Lattner13cb21f2007-08-09 17:35:30 +0000373 Node->getMemberDecl()->getName(), (void*)Node->getMemberDecl());
Chris Lattner6000dac2007-08-08 22:51:59 +0000374}
Nate Begeman213541a2008-04-18 23:10:10 +0000375void StmtDumper::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000376 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000377 fprintf(F, " %s", Node->getAccessor().getName());
Chris Lattner6000dac2007-08-08 22:51:59 +0000378}
Chris Lattner6000dac2007-08-08 22:51:59 +0000379void StmtDumper::VisitBinaryOperator(BinaryOperator *Node) {
380 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000381 fprintf(F, " '%s'", BinaryOperator::getOpcodeStr(Node->getOpcode()));
Chris Lattnereb14fe82007-08-25 02:00:02 +0000382}
383void StmtDumper::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
384 DumpExpr(Node);
385 fprintf(F, " '%s' ComputeTy=",
386 BinaryOperator::getOpcodeStr(Node->getOpcode()));
387 DumpType(Node->getComputationType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000388}
Chris Lattner6000dac2007-08-08 22:51:59 +0000389
390// GNU extensions.
391
392void StmtDumper::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000393 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000394 fprintf(F, " %s %p", Node->getLabel()->getName(), (void*)Node->getLabel());
Chris Lattner6000dac2007-08-08 22:51:59 +0000395}
396
Chris Lattner6000dac2007-08-08 22:51:59 +0000397void StmtDumper::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000398 DumpExpr(Node);
399 fprintf(F, " ");
400 DumpType(Node->getArgType1());
401 fprintf(F, " ");
402 DumpType(Node->getArgType2());
Chris Lattner6000dac2007-08-08 22:51:59 +0000403}
404
Chris Lattnerf9e05812007-08-09 18:03:18 +0000405//===----------------------------------------------------------------------===//
406// C++ Expressions
407//===----------------------------------------------------------------------===//
Chris Lattner6000dac2007-08-08 22:51:59 +0000408
409void StmtDumper::VisitCXXCastExpr(CXXCastExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000410 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000411 fprintf(F, " %s", CXXCastExpr::getOpcodeStr(Node->getOpcode()));
Chris Lattner6000dac2007-08-08 22:51:59 +0000412}
413
414void StmtDumper::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000415 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000416 fprintf(F, " %s", Node->getValue() ? "true" : "false");
Chris Lattner6000dac2007-08-08 22:51:59 +0000417}
418
Anders Carlsson55085182007-08-21 17:43:55 +0000419//===----------------------------------------------------------------------===//
420// Obj-C Expressions
421//===----------------------------------------------------------------------===//
422
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000423void StmtDumper::VisitObjCMessageExpr(ObjCMessageExpr* Node) {
424 DumpExpr(Node);
425 fprintf(F, " selector=%s", Node->getSelector().getName().c_str());
Ted Kremenekea958e572008-05-01 17:26:20 +0000426 IdentifierInfo* clsName = Node->getClassName();
427 if (clsName) fprintf(F, " class=%s", clsName->getName());
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000428}
429
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000430void StmtDumper::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
431 DumpExpr(Node);
432
433 fprintf(F, " ");
434 DumpType(Node->getEncodedType());
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000435}
436
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000437void StmtDumper::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
438 DumpExpr(Node);
439
440 fprintf(F, " ");
Ted Kremenek97b7f262008-04-16 04:30:16 +0000441 Selector selector = Node->getSelector();
Fariborz Jahanianc5c42f52007-10-16 21:07:53 +0000442 fprintf(F, "%s", selector.getName().c_str());
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000443}
444
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000445void StmtDumper::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
446 DumpExpr(Node);
447
448 fprintf(F, " ");
449 fprintf(F, "%s", Node->getProtocol()->getName());
450}
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000451
452void StmtDumper::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
453 DumpExpr(Node);
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000454
455 if (Node->getKind() == ObjCPropertyRefExpr::MethodRef) {
456 ObjCMethodDecl *Getter = Node->getGetterMethod();
457 ObjCMethodDecl *Setter = Node->getSetterMethod();
458 fprintf(F, " Kind=MethodRef Getter=\"%s\" Setter=\"%s\"",
459 Getter->getSelector().getName().c_str(),
460 Setter ? Setter->getSelector().getName().c_str() : "(null)");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000461 } else {
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000462 fprintf(F, " Kind=PropertyRef Property=\"%s\"", Node->getProperty()->getName());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000463 }
464}
465
Chris Lattner6000dac2007-08-08 22:51:59 +0000466//===----------------------------------------------------------------------===//
467// Stmt method implementations
468//===----------------------------------------------------------------------===//
469
470/// dump - This does a local dump of the specified AST fragment. It dumps the
471/// specified node and a few nodes underneath it, but not the whole subtree.
472/// This is useful in a debugger.
Chris Lattnere300c872007-08-30 06:17:34 +0000473void Stmt::dump(SourceManager &SM) const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000474 StmtDumper P(&SM, stderr, 4);
Chris Lattnerb3938792007-08-30 00:53:54 +0000475 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattner0c727a32007-08-30 00:40:08 +0000476 fprintf(stderr, "\n");
477}
478
479/// dump - This does a local dump of the specified AST fragment. It dumps the
480/// specified node and a few nodes underneath it, but not the whole subtree.
481/// This is useful in a debugger.
Chris Lattner6000dac2007-08-08 22:51:59 +0000482void Stmt::dump() const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000483 StmtDumper P(0, stderr, 4);
Chris Lattnerb3938792007-08-30 00:53:54 +0000484 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattner0c727a32007-08-30 00:40:08 +0000485 fprintf(stderr, "\n");
486}
487
488/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
Chris Lattnere300c872007-08-30 06:17:34 +0000489void Stmt::dumpAll(SourceManager &SM) const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000490 StmtDumper P(&SM, stderr, ~0U);
Chris Lattnerb3938792007-08-30 00:53:54 +0000491 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattneree41ce52007-08-10 21:51:12 +0000492 fprintf(stderr, "\n");
Chris Lattner6000dac2007-08-08 22:51:59 +0000493}
494
495/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
496void Stmt::dumpAll() const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000497 StmtDumper P(0, stderr, ~0U);
Chris Lattnerb3938792007-08-30 00:53:54 +0000498 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattneree41ce52007-08-10 21:51:12 +0000499 fprintf(stderr, "\n");
Chris Lattner6000dac2007-08-08 22:51:59 +0000500}