blob: c3ce92de60b7258f5b69a389c3ff23bb48483548 [file] [log] [blame]
Chris Lattner95578782007-08-08 22:51:59 +00001//===--- StmtDumper.cpp - Dumping implementation for Stmt ASTs ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattner95578782007-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 Kremenek10364dd2007-10-17 18:36:42 +000016#include "clang/AST/DeclObjC.h"
Chris Lattner99b994b2007-08-30 06:17:34 +000017#include "clang/Basic/SourceManager.h"
Chris Lattner95578782007-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 Lattner7ba21fa2007-08-21 04:04:25 +000027 class VISIBILITY_HIDDEN StmtDumper : public StmtVisitor<StmtDumper> {
Chris Lattner99b994b2007-08-30 06:17:34 +000028 SourceManager *SM;
Chris Lattner95578782007-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 Lattner99b994b2007-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 Lattner95578782007-08-08 22:51:59 +000041 public:
Chris Lattner99b994b2007-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 Lattner95578782007-08-08 22:51:59 +000047
Chris Lattner412a4192007-08-09 18:03:18 +000048 void DumpSubTree(Stmt *S) {
Chris Lattner95578782007-08-08 22:51:59 +000049 // Prune the recursion if not using dump all.
50 if (MaxDepth == 0) return;
51
Chris Lattner412a4192007-08-09 18:03:18 +000052 ++IndentLevel;
Chris Lattner95578782007-08-08 22:51:59 +000053 if (S) {
Ted Kremenek360d9a72007-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 Lattnerf3e2a252007-08-30 00:53:54 +000066 }
Ted Kremenek360d9a72007-12-12 06:59:42 +000067 fprintf(F, ")");
Chris Lattnerf3e2a252007-08-30 00:53:54 +000068 }
Chris Lattner95578782007-08-08 22:51:59 +000069 } else {
70 Indent();
Chris Lattnerf38cbc72007-08-26 03:53:29 +000071 fprintf(F, "<<<NULL>>>");
Chris Lattner95578782007-08-08 22:51:59 +000072 }
Chris Lattner412a4192007-08-09 18:03:18 +000073 --IndentLevel;
Chris Lattner95578782007-08-08 22:51:59 +000074 }
75
Chris Lattner412a4192007-08-09 18:03:18 +000076 void DumpDeclarator(Decl *D);
Chris Lattner95578782007-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 Naroffa610eab2007-09-01 21:08:38 +000083 void DumpType(QualType T) {
Chris Lattner7e4a2c72007-08-09 00:36:22 +000084 fprintf(F, "'%s'", T.getAsString().c_str());
85
Douglas Gregor62ae25a2008-12-24 00:01:03 +000086 if (!T.isNull()) {
87 // If the type is directly a typedef, strip off typedefness to give at
88 // least one level of concreteness.
89 if (TypedefType *TDT = dyn_cast<TypedefType>(T)) {
90 QualType Simplified =
91 TDT->LookThroughTypedefs().getQualifiedType(T.getCVRQualifiers());
92 fprintf(F, ":'%s'", Simplified.getAsString().c_str());
93 }
Chris Lattner6c755cf2008-04-02 05:06:23 +000094 }
Chris Lattner7e4a2c72007-08-09 00:36:22 +000095 }
Steve Naroffa610eab2007-09-01 21:08:38 +000096 void DumpStmt(const Stmt *Node) {
Chris Lattner95578782007-08-08 22:51:59 +000097 Indent();
98 fprintf(F, "(%s %p", Node->getStmtClassName(), (void*)Node);
Steve Naroffa610eab2007-09-01 21:08:38 +000099 DumpSourceRange(Node);
Chris Lattner95578782007-08-08 22:51:59 +0000100 }
Steve Naroffa610eab2007-09-01 21:08:38 +0000101 void DumpExpr(const Expr *Node) {
Chris Lattner95578782007-08-08 22:51:59 +0000102 DumpStmt(Node);
Chris Lattner7e4a2c72007-08-09 00:36:22 +0000103 fprintf(F, " ");
104 DumpType(Node->getType());
Chris Lattner95578782007-08-08 22:51:59 +0000105 }
Steve Naroffa610eab2007-09-01 21:08:38 +0000106 void DumpSourceRange(const Stmt *Node);
Chris Lattner99b994b2007-08-30 06:17:34 +0000107 void DumpLocation(SourceLocation Loc);
Chris Lattner99b994b2007-08-30 06:17:34 +0000108
Chris Lattner35122932007-08-30 01:00:35 +0000109 // Stmts.
Chris Lattner7ba21fa2007-08-21 04:04:25 +0000110 void VisitStmt(Stmt *Node);
Ted Kremenek360d9a72007-12-12 06:59:42 +0000111 void VisitDeclStmt(DeclStmt *Node);
Chris Lattner35122932007-08-30 01:00:35 +0000112 void VisitLabelStmt(LabelStmt *Node);
113 void VisitGotoStmt(GotoStmt *Node);
114
115 // Exprs
116 void VisitExpr(Expr *Node);
117 void VisitDeclRefExpr(DeclRefExpr *Node);
Chris Lattner69909292008-08-10 01:53:14 +0000118 void VisitPredefinedExpr(PredefinedExpr *Node);
Chris Lattner35122932007-08-30 01:00:35 +0000119 void VisitCharacterLiteral(CharacterLiteral *Node);
120 void VisitIntegerLiteral(IntegerLiteral *Node);
121 void VisitFloatingLiteral(FloatingLiteral *Node);
122 void VisitStringLiteral(StringLiteral *Str);
123 void VisitUnaryOperator(UnaryOperator *Node);
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000124 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node);
Chris Lattner35122932007-08-30 01:00:35 +0000125 void VisitMemberExpr(MemberExpr *Node);
Nate Begemanaf6ed502008-04-18 23:10:10 +0000126 void VisitExtVectorElementExpr(ExtVectorElementExpr *Node);
Chris Lattner35122932007-08-30 01:00:35 +0000127 void VisitBinaryOperator(BinaryOperator *Node);
128 void VisitCompoundAssignOperator(CompoundAssignOperator *Node);
129 void VisitAddrLabelExpr(AddrLabelExpr *Node);
130 void VisitTypesCompatibleExpr(TypesCompatibleExpr *Node);
131
132 // C++
Douglas Gregor21a04f32008-10-27 19:41:14 +0000133 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Chris Lattner35122932007-08-30 01:00:35 +0000134 void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node);
Douglas Gregord8606632008-11-04 14:56:14 +0000135 void VisitCXXThisExpr(CXXThisExpr *Node);
Douglas Gregor21a04f32008-10-27 19:41:14 +0000136 void VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node);
Douglas Gregord8606632008-11-04 14:56:14 +0000137
Chris Lattner35122932007-08-30 01:00:35 +0000138 // ObjC
139 void VisitObjCEncodeExpr(ObjCEncodeExpr *Node);
Ted Kremenek62ee3c22008-02-29 22:04:05 +0000140 void VisitObjCMessageExpr(ObjCMessageExpr* Node);
Fariborz Jahanianf807c202007-10-16 20:40:23 +0000141 void VisitObjCSelectorExpr(ObjCSelectorExpr *Node);
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +0000142 void VisitObjCProtocolExpr(ObjCProtocolExpr *Node);
Daniel Dunbardd851282008-08-30 05:35:15 +0000143 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node);
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000144 void VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node);
Steve Naroff6453aab2008-03-12 13:19:12 +0000145 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node);
Douglas Gregord8606632008-11-04 14:56:14 +0000146 void VisitObjCSuperExpr(ObjCSuperExpr *Node);
Chris Lattner95578782007-08-08 22:51:59 +0000147 };
148}
149
150//===----------------------------------------------------------------------===//
Chris Lattner99b994b2007-08-30 06:17:34 +0000151// Utilities
152//===----------------------------------------------------------------------===//
153
154void StmtDumper::DumpLocation(SourceLocation Loc) {
155 SourceLocation PhysLoc = SM->getPhysicalLoc(Loc);
156
157 // The general format we print out is filename:line:col, but we drop pieces
158 // that haven't changed since the last loc printed.
159 const char *Filename = SM->getSourceName(PhysLoc);
160 unsigned LineNo = SM->getLineNumber(PhysLoc);
161 if (strcmp(Filename, LastLocFilename) != 0) {
162 fprintf(stderr, "%s:%u:%u", Filename, LineNo, SM->getColumnNumber(PhysLoc));
163 LastLocFilename = Filename;
164 LastLocLine = LineNo;
165 } else if (LineNo != LastLocLine) {
166 fprintf(stderr, "line:%u:%u", LineNo, SM->getColumnNumber(PhysLoc));
167 LastLocLine = LineNo;
168 } else {
169 fprintf(stderr, "col:%u", SM->getColumnNumber(PhysLoc));
170 }
171}
172
Steve Naroffa610eab2007-09-01 21:08:38 +0000173void StmtDumper::DumpSourceRange(const Stmt *Node) {
Chris Lattner99b994b2007-08-30 06:17:34 +0000174 // Can't translate locations if a SourceManager isn't available.
175 if (SM == 0) return;
176
177 // TODO: If the parent expression is available, we can print a delta vs its
178 // location.
179 SourceRange R = Node->getSourceRange();
180
181 fprintf(stderr, " <");
Chris Lattner6fe8b272007-10-16 22:36:42 +0000182 DumpLocation(R.getBegin());
183 if (R.getBegin() != R.getEnd()) {
Chris Lattner99b994b2007-08-30 06:17:34 +0000184 fprintf(stderr, ", ");
Chris Lattner6fe8b272007-10-16 22:36:42 +0000185 DumpLocation(R.getEnd());
Chris Lattner99b994b2007-08-30 06:17:34 +0000186 }
187 fprintf(stderr, ">");
188
189 // <t2.c:123:421[blah], t2.c:412:321>
190
191}
192
193
194//===----------------------------------------------------------------------===//
Chris Lattner95578782007-08-08 22:51:59 +0000195// Stmt printing methods.
196//===----------------------------------------------------------------------===//
197
198void StmtDumper::VisitStmt(Stmt *Node) {
Chris Lattner35122932007-08-30 01:00:35 +0000199 DumpStmt(Node);
Chris Lattner95578782007-08-08 22:51:59 +0000200}
201
Chris Lattner412a4192007-08-09 18:03:18 +0000202void StmtDumper::DumpDeclarator(Decl *D) {
Chris Lattner95578782007-08-08 22:51:59 +0000203 // FIXME: Need to complete/beautify this... this code simply shows the
204 // nodes are where they need to be.
205 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
Chris Lattner412a4192007-08-09 18:03:18 +0000206 fprintf(F, "\"typedef %s %s\"",
207 localType->getUnderlyingType().getAsString().c_str(),
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000208 localType->getNameAsString().c_str());
Chris Lattner95578782007-08-08 22:51:59 +0000209 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattner412a4192007-08-09 18:03:18 +0000210 fprintf(F, "\"");
Chris Lattner95578782007-08-08 22:51:59 +0000211 // Emit storage class for vardecls.
212 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
213 switch (V->getStorageClass()) {
Chris Lattner412a4192007-08-09 18:03:18 +0000214 default: assert(0 && "Unknown storage class!");
215 case VarDecl::None: break;
216 case VarDecl::Extern: fprintf(F, "extern "); break;
217 case VarDecl::Static: fprintf(F, "static "); break;
218 case VarDecl::Auto: fprintf(F, "auto "); break;
219 case VarDecl::Register: fprintf(F, "register "); break;
Chris Lattner95578782007-08-08 22:51:59 +0000220 }
221 }
222
Chris Lattner6c5ec622008-11-24 04:00:27 +0000223 std::string Name = VD->getNameAsString();
Chris Lattner95578782007-08-08 22:51:59 +0000224 VD->getType().getAsStringInternal(Name);
Chris Lattner412a4192007-08-09 18:03:18 +0000225 fprintf(F, "%s", Name.c_str());
Chris Lattner95578782007-08-08 22:51:59 +0000226
227 // If this is a vardecl with an initializer, emit it.
228 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
229 if (V->getInit()) {
Chris Lattner412a4192007-08-09 18:03:18 +0000230 fprintf(F, " =\n");
231 DumpSubTree(V->getInit());
Chris Lattner95578782007-08-08 22:51:59 +0000232 }
233 }
Chris Lattner412a4192007-08-09 18:03:18 +0000234 fprintf(F, "\"");
Steve Naroffedafc0b2007-11-17 21:37:36 +0000235 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
236 // print a free standing tag decl (e.g. "struct x;").
237 const char *tagname;
238 if (const IdentifierInfo *II = TD->getIdentifier())
239 tagname = II->getName();
240 else
241 tagname = "<anonymous>";
242 fprintf(F, "\"%s %s;\"", TD->getKindName(), tagname);
243 // FIXME: print tag bodies.
Chris Lattner95578782007-08-08 22:51:59 +0000244 } else {
Chris Lattner95578782007-08-08 22:51:59 +0000245 assert(0 && "Unexpected decl");
246 }
Chris Lattner95578782007-08-08 22:51:59 +0000247}
248
Ted Kremenek360d9a72007-12-12 06:59:42 +0000249void StmtDumper::VisitDeclStmt(DeclStmt *Node) {
250 DumpStmt(Node);
251 fprintf(F,"\n");
Ted Kremenekf3f8a492008-10-06 18:38:35 +0000252 for (DeclStmt::decl_iterator DI = Node->decl_begin(), DE = Node->decl_end();
253 DI != DE; ++DI) {
254 ScopedDecl* D = *DI;
Ted Kremenek360d9a72007-12-12 06:59:42 +0000255 ++IndentLevel;
256 Indent();
257 fprintf(F, "%p ", (void*) D);
258 DumpDeclarator(D);
259 if (D->getNextDeclarator())
260 fprintf(F,"\n");
261 --IndentLevel;
262 }
263}
264
Chris Lattner95578782007-08-08 22:51:59 +0000265void StmtDumper::VisitLabelStmt(LabelStmt *Node) {
266 DumpStmt(Node);
Chris Lattnerb8a17eb2008-07-26 19:24:43 +0000267 fprintf(F, " '%s'", Node->getName());
Chris Lattner95578782007-08-08 22:51:59 +0000268}
269
Chris Lattner95578782007-08-08 22:51:59 +0000270void StmtDumper::VisitGotoStmt(GotoStmt *Node) {
271 DumpStmt(Node);
Chris Lattnerf3e2a252007-08-30 00:53:54 +0000272 fprintf(F, " '%s':%p", Node->getLabel()->getName(), (void*)Node->getLabel());
Chris Lattner95578782007-08-08 22:51:59 +0000273}
274
Chris Lattner95578782007-08-08 22:51:59 +0000275//===----------------------------------------------------------------------===//
276// Expr printing methods.
277//===----------------------------------------------------------------------===//
278
279void StmtDumper::VisitExpr(Expr *Node) {
280 DumpExpr(Node);
Chris Lattner95578782007-08-08 22:51:59 +0000281}
282
283void StmtDumper::VisitDeclRefExpr(DeclRefExpr *Node) {
284 DumpExpr(Node);
Ted Kremenek97a0ad52007-09-10 17:32:55 +0000285
286 fprintf(F, " ");
287 switch (Node->getDecl()->getKind()) {
288 case Decl::Function: fprintf(F,"FunctionDecl"); break;
Steve Naroff72a6ebc2008-04-15 22:42:06 +0000289 case Decl::Var: fprintf(F,"Var"); break;
Chris Lattner3289bca2007-10-08 21:37:32 +0000290 case Decl::ParmVar: fprintf(F,"ParmVar"); break;
Ted Kremenek97a0ad52007-09-10 17:32:55 +0000291 case Decl::EnumConstant: fprintf(F,"EnumConstant"); break;
292 case Decl::Typedef: fprintf(F,"Typedef"); break;
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000293 case Decl::Record: fprintf(F,"Record"); break;
Ted Kremenek97a0ad52007-09-10 17:32:55 +0000294 case Decl::Enum: fprintf(F,"Enum"); break;
Argiris Kirtzidis2538a8c2008-10-15 00:42:39 +0000295 case Decl::CXXRecord: fprintf(F,"CXXRecord"); break;
Ted Kremenek42730c52008-01-07 19:49:32 +0000296 case Decl::ObjCInterface: fprintf(F,"ObjCInterface"); break;
297 case Decl::ObjCClass: fprintf(F,"ObjCClass"); break;
Ted Kremenek97a0ad52007-09-10 17:32:55 +0000298 default: fprintf(F,"Decl"); break;
299 }
300
Chris Lattner6c5ec622008-11-24 04:00:27 +0000301 fprintf(F, "='%s' %p", Node->getDecl()->getNameAsString().c_str(),
Douglas Gregor24afd4a2008-11-17 14:58:09 +0000302 (void*)Node->getDecl());
Chris Lattner95578782007-08-08 22:51:59 +0000303}
304
Steve Naroff6453aab2008-03-12 13:19:12 +0000305void StmtDumper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Steve Naroffc38f4352008-05-23 00:59:14 +0000306 DumpExpr(Node);
Steve Naroff6453aab2008-03-12 13:19:12 +0000307
Steve Naroffc38f4352008-05-23 00:59:14 +0000308 fprintf(F, " %sDecl='%s' %p", Node->getDecl()->getDeclKindName(),
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000309 Node->getDecl()->getNameAsString().c_str(), (void*)Node->getDecl());
Steve Narofffc96eea2008-05-23 22:01:24 +0000310 if (Node->isFreeIvar())
311 fprintf(F, " isFreeIvar");
Steve Naroff6453aab2008-03-12 13:19:12 +0000312}
313
Chris Lattner69909292008-08-10 01:53:14 +0000314void StmtDumper::VisitPredefinedExpr(PredefinedExpr *Node) {
Chris Lattner95578782007-08-08 22:51:59 +0000315 DumpExpr(Node);
316 switch (Node->getIdentType()) {
Chris Lattner4423d372008-06-21 18:04:54 +0000317 default: assert(0 && "unknown case");
Chris Lattner69909292008-08-10 01:53:14 +0000318 case PredefinedExpr::Func: fprintf(F, " __func__"); break;
319 case PredefinedExpr::Function: fprintf(F, " __FUNCTION__"); break;
320 case PredefinedExpr::PrettyFunction: fprintf(F, " __PRETTY_FUNCTION__");break;
Chris Lattner95578782007-08-08 22:51:59 +0000321 }
322}
323
324void StmtDumper::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner9a6d6ae2007-08-09 01:04:32 +0000325 DumpExpr(Node);
Chris Lattnerf3e2a252007-08-30 00:53:54 +0000326 fprintf(F, " %d", Node->getValue());
Chris Lattner95578782007-08-08 22:51:59 +0000327}
328
329void StmtDumper::VisitIntegerLiteral(IntegerLiteral *Node) {
330 DumpExpr(Node);
331
332 bool isSigned = Node->getType()->isSignedIntegerType();
Chris Lattnerf3e2a252007-08-30 00:53:54 +0000333 fprintf(F, " %s", Node->getValue().toString(10, isSigned).c_str());
Chris Lattner95578782007-08-08 22:51:59 +0000334}
335void StmtDumper::VisitFloatingLiteral(FloatingLiteral *Node) {
336 DumpExpr(Node);
Chris Lattnere0391b22008-06-07 22:13:43 +0000337 fprintf(F, " %f", Node->getValueAsApproximateDouble());
Chris Lattner95578782007-08-08 22:51:59 +0000338}
Chris Lattner1de66eb2007-08-26 03:42:43 +0000339
Chris Lattner95578782007-08-08 22:51:59 +0000340void StmtDumper::VisitStringLiteral(StringLiteral *Str) {
Chris Lattner9a6d6ae2007-08-09 01:04:32 +0000341 DumpExpr(Str);
342 // FIXME: this doesn't print wstrings right.
Chris Lattner9fb40242007-08-09 17:14:24 +0000343 fprintf(F, " %s\"", Str->isWide() ? "L" : "");
Chris Lattner9a6d6ae2007-08-09 01:04:32 +0000344
Chris Lattner95578782007-08-08 22:51:59 +0000345 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattner9fb40242007-08-09 17:14:24 +0000346 switch (char C = Str->getStrData()[i]) {
347 default:
348 if (isprint(C))
349 fputc(C, F);
350 else
351 fprintf(F, "\\%03o", C);
352 break;
Chris Lattner95578782007-08-08 22:51:59 +0000353 // Handle some common ones to make dumps prettier.
Chris Lattner9fb40242007-08-09 17:14:24 +0000354 case '\\': fprintf(F, "\\\\"); break;
355 case '"': fprintf(F, "\\\""); break;
356 case '\n': fprintf(F, "\\n"); break;
357 case '\t': fprintf(F, "\\t"); break;
358 case '\a': fprintf(F, "\\a"); break;
359 case '\b': fprintf(F, "\\b"); break;
Chris Lattner95578782007-08-08 22:51:59 +0000360 }
361 }
Chris Lattnerf3e2a252007-08-30 00:53:54 +0000362 fprintf(F, "\"");
Chris Lattner95578782007-08-08 22:51:59 +0000363}
Chris Lattner35122932007-08-30 01:00:35 +0000364
Chris Lattner95578782007-08-08 22:51:59 +0000365void StmtDumper::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnerb4ee7b32007-08-09 17:35:30 +0000366 DumpExpr(Node);
Chris Lattnerf3e2a252007-08-30 00:53:54 +0000367 fprintf(F, " %s '%s'", Node->isPostfix() ? "postfix" : "prefix",
Chris Lattnerb4ee7b32007-08-09 17:35:30 +0000368 UnaryOperator::getOpcodeStr(Node->getOpcode()));
Chris Lattner95578782007-08-08 22:51:59 +0000369}
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000370void StmtDumper::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
Chris Lattnerb4ee7b32007-08-09 17:35:30 +0000371 DumpExpr(Node);
372 fprintf(F, " %s ", Node->isSizeOf() ? "sizeof" : "alignof");
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000373 if (Node->isArgumentType())
374 DumpType(Node->getArgumentType());
Chris Lattner95578782007-08-08 22:51:59 +0000375}
Chris Lattnerb4ee7b32007-08-09 17:35:30 +0000376
Chris Lattner95578782007-08-08 22:51:59 +0000377void StmtDumper::VisitMemberExpr(MemberExpr *Node) {
Chris Lattnerb4ee7b32007-08-09 17:35:30 +0000378 DumpExpr(Node);
Chris Lattnerf3e2a252007-08-30 00:53:54 +0000379 fprintf(F, " %s%s %p", Node->isArrow() ? "->" : ".",
Chris Lattner3a8f2942008-11-24 03:33:13 +0000380 Node->getMemberDecl()->getNameAsString().c_str(),
Douglas Gregor24afd4a2008-11-17 14:58:09 +0000381 (void*)Node->getMemberDecl());
Chris Lattner95578782007-08-08 22:51:59 +0000382}
Nate Begemanaf6ed502008-04-18 23:10:10 +0000383void StmtDumper::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Chris Lattnerb4ee7b32007-08-09 17:35:30 +0000384 DumpExpr(Node);
Chris Lattnerf3e2a252007-08-30 00:53:54 +0000385 fprintf(F, " %s", Node->getAccessor().getName());
Chris Lattner95578782007-08-08 22:51:59 +0000386}
Chris Lattner95578782007-08-08 22:51:59 +0000387void StmtDumper::VisitBinaryOperator(BinaryOperator *Node) {
388 DumpExpr(Node);
Chris Lattnerf3e2a252007-08-30 00:53:54 +0000389 fprintf(F, " '%s'", BinaryOperator::getOpcodeStr(Node->getOpcode()));
Chris Lattner06078d22007-08-25 02:00:02 +0000390}
391void StmtDumper::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
392 DumpExpr(Node);
393 fprintf(F, " '%s' ComputeTy=",
394 BinaryOperator::getOpcodeStr(Node->getOpcode()));
395 DumpType(Node->getComputationType());
Chris Lattner95578782007-08-08 22:51:59 +0000396}
Chris Lattner95578782007-08-08 22:51:59 +0000397
398// GNU extensions.
399
400void StmtDumper::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnerb4ee7b32007-08-09 17:35:30 +0000401 DumpExpr(Node);
Chris Lattnerf3e2a252007-08-30 00:53:54 +0000402 fprintf(F, " %s %p", Node->getLabel()->getName(), (void*)Node->getLabel());
Chris Lattner95578782007-08-08 22:51:59 +0000403}
404
Chris Lattner95578782007-08-08 22:51:59 +0000405void StmtDumper::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
Chris Lattnerb4ee7b32007-08-09 17:35:30 +0000406 DumpExpr(Node);
407 fprintf(F, " ");
408 DumpType(Node->getArgType1());
409 fprintf(F, " ");
410 DumpType(Node->getArgType2());
Chris Lattner95578782007-08-08 22:51:59 +0000411}
412
Chris Lattner412a4192007-08-09 18:03:18 +0000413//===----------------------------------------------------------------------===//
414// C++ Expressions
415//===----------------------------------------------------------------------===//
Chris Lattner95578782007-08-08 22:51:59 +0000416
Douglas Gregor21a04f32008-10-27 19:41:14 +0000417void StmtDumper::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
Chris Lattnerb4ee7b32007-08-09 17:35:30 +0000418 DumpExpr(Node);
Douglas Gregor21a04f32008-10-27 19:41:14 +0000419 fprintf(F, " %s<%s>", Node->getCastName(),
420 Node->getTypeAsWritten().getAsString().c_str());
Chris Lattner95578782007-08-08 22:51:59 +0000421}
422
423void StmtDumper::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
Chris Lattnerb4ee7b32007-08-09 17:35:30 +0000424 DumpExpr(Node);
Chris Lattnerf3e2a252007-08-30 00:53:54 +0000425 fprintf(F, " %s", Node->getValue() ? "true" : "false");
Chris Lattner95578782007-08-08 22:51:59 +0000426}
427
Douglas Gregord8606632008-11-04 14:56:14 +0000428void StmtDumper::VisitCXXThisExpr(CXXThisExpr *Node) {
429 DumpExpr(Node);
430 fprintf(F, " this");
431}
432
Douglas Gregor21a04f32008-10-27 19:41:14 +0000433void StmtDumper::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
434 DumpExpr(Node);
435 fprintf(F, " functional cast to %s",
436 Node->getTypeAsWritten().getAsString().c_str());
437}
438
Anders Carlssona66cad42007-08-21 17:43:55 +0000439//===----------------------------------------------------------------------===//
440// Obj-C Expressions
441//===----------------------------------------------------------------------===//
442
Ted Kremenek62ee3c22008-02-29 22:04:05 +0000443void StmtDumper::VisitObjCMessageExpr(ObjCMessageExpr* Node) {
444 DumpExpr(Node);
Chris Lattner3a8f2942008-11-24 03:33:13 +0000445 fprintf(F, " selector=%s", Node->getSelector().getAsString().c_str());
Ted Kremenekd029a6f2008-05-01 17:26:20 +0000446 IdentifierInfo* clsName = Node->getClassName();
447 if (clsName) fprintf(F, " class=%s", clsName->getName());
Ted Kremenek62ee3c22008-02-29 22:04:05 +0000448}
449
Anders Carlsson8be1d402007-08-22 15:14:15 +0000450void StmtDumper::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
451 DumpExpr(Node);
452
453 fprintf(F, " ");
454 DumpType(Node->getEncodedType());
Anders Carlsson8be1d402007-08-22 15:14:15 +0000455}
456
Fariborz Jahanianf807c202007-10-16 20:40:23 +0000457void StmtDumper::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
458 DumpExpr(Node);
459
460 fprintf(F, " ");
Chris Lattner3a8f2942008-11-24 03:33:13 +0000461 fprintf(F, "%s", Node->getSelector().getAsString().c_str());
Fariborz Jahanianf807c202007-10-16 20:40:23 +0000462}
463
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +0000464void StmtDumper::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
465 DumpExpr(Node);
466
467 fprintf(F, " ");
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000468 fprintf(F, "%s", Node->getProtocol()->getNameAsString().c_str());
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +0000469}
Daniel Dunbardd851282008-08-30 05:35:15 +0000470
471void StmtDumper::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
472 DumpExpr(Node);
Daniel Dunbarcc37ac52008-09-03 00:27:26 +0000473
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000474 fprintf(F, " Kind=PropertyRef Property=\"%s\"",
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000475 Node->getProperty()->getNameAsString().c_str());
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000476}
477
478void StmtDumper::VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node) {
479 DumpExpr(Node);
480
481 ObjCMethodDecl *Getter = Node->getGetterMethod();
482 ObjCMethodDecl *Setter = Node->getSetterMethod();
483 fprintf(F, " Kind=MethodRef Getter=\"%s\" Setter=\"%s\"",
Chris Lattner3a8f2942008-11-24 03:33:13 +0000484 Getter->getSelector().getAsString().c_str(),
485 Setter ? Setter->getSelector().getAsString().c_str() : "(null)");
Daniel Dunbardd851282008-08-30 05:35:15 +0000486}
487
Douglas Gregord8606632008-11-04 14:56:14 +0000488void StmtDumper::VisitObjCSuperExpr(ObjCSuperExpr *Node) {
489 DumpExpr(Node);
490 fprintf(F, " super");
491}
492
Chris Lattner95578782007-08-08 22:51:59 +0000493//===----------------------------------------------------------------------===//
494// Stmt method implementations
495//===----------------------------------------------------------------------===//
496
497/// dump - This does a local dump of the specified AST fragment. It dumps the
498/// specified node and a few nodes underneath it, but not the whole subtree.
499/// This is useful in a debugger.
Chris Lattner99b994b2007-08-30 06:17:34 +0000500void Stmt::dump(SourceManager &SM) const {
Chris Lattnerbbc51ad2007-08-30 00:40:08 +0000501 StmtDumper P(&SM, stderr, 4);
Chris Lattnerf3e2a252007-08-30 00:53:54 +0000502 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattnerbbc51ad2007-08-30 00:40:08 +0000503 fprintf(stderr, "\n");
504}
505
506/// dump - This does a local dump of the specified AST fragment. It dumps the
507/// specified node and a few nodes underneath it, but not the whole subtree.
508/// This is useful in a debugger.
Chris Lattner95578782007-08-08 22:51:59 +0000509void Stmt::dump() const {
Chris Lattnerbbc51ad2007-08-30 00:40:08 +0000510 StmtDumper P(0, stderr, 4);
Chris Lattnerf3e2a252007-08-30 00:53:54 +0000511 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattnerbbc51ad2007-08-30 00:40:08 +0000512 fprintf(stderr, "\n");
513}
514
515/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
Chris Lattner99b994b2007-08-30 06:17:34 +0000516void Stmt::dumpAll(SourceManager &SM) const {
Chris Lattnerbbc51ad2007-08-30 00:40:08 +0000517 StmtDumper P(&SM, stderr, ~0U);
Chris Lattnerf3e2a252007-08-30 00:53:54 +0000518 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattner7eaddd72007-08-10 21:51:12 +0000519 fprintf(stderr, "\n");
Chris Lattner95578782007-08-08 22:51:59 +0000520}
521
522/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
523void Stmt::dumpAll() const {
Chris Lattnerbbc51ad2007-08-30 00:40:08 +0000524 StmtDumper P(0, stderr, ~0U);
Chris Lattnerf3e2a252007-08-30 00:53:54 +0000525 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattner7eaddd72007-08-10 21:51:12 +0000526 fprintf(stderr, "\n");
Chris Lattner95578782007-08-08 22:51:59 +0000527}