blob: f7330c2a9dcfde5b4f65b73b1f9a19649a2d09f4 [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);
Sebastian Redl05189992008-11-11 17:56:53 +0000122 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000123 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++
Douglas Gregor49badde2008-10-27 19:41:14 +0000131 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000132 void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node);
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000133 void VisitCXXThisExpr(CXXThisExpr *Node);
Douglas Gregor49badde2008-10-27 19:41:14 +0000134 void VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node);
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000135
Chris Lattner17a1a722007-08-30 01:00:35 +0000136 // ObjC
137 void VisitObjCEncodeExpr(ObjCEncodeExpr *Node);
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000138 void VisitObjCMessageExpr(ObjCMessageExpr* Node);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000139 void VisitObjCSelectorExpr(ObjCSelectorExpr *Node);
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000140 void VisitObjCProtocolExpr(ObjCProtocolExpr *Node);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000141 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node);
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000142 void VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node);
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000143 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node);
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000144 void VisitObjCSuperExpr(ObjCSuperExpr *Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000145 };
146}
147
148//===----------------------------------------------------------------------===//
Chris Lattnere300c872007-08-30 06:17:34 +0000149// Utilities
150//===----------------------------------------------------------------------===//
151
152void StmtDumper::DumpLocation(SourceLocation Loc) {
153 SourceLocation PhysLoc = SM->getPhysicalLoc(Loc);
154
155 // The general format we print out is filename:line:col, but we drop pieces
156 // that haven't changed since the last loc printed.
157 const char *Filename = SM->getSourceName(PhysLoc);
158 unsigned LineNo = SM->getLineNumber(PhysLoc);
159 if (strcmp(Filename, LastLocFilename) != 0) {
160 fprintf(stderr, "%s:%u:%u", Filename, LineNo, SM->getColumnNumber(PhysLoc));
161 LastLocFilename = Filename;
162 LastLocLine = LineNo;
163 } else if (LineNo != LastLocLine) {
164 fprintf(stderr, "line:%u:%u", LineNo, SM->getColumnNumber(PhysLoc));
165 LastLocLine = LineNo;
166 } else {
167 fprintf(stderr, "col:%u", SM->getColumnNumber(PhysLoc));
168 }
169}
170
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000171void StmtDumper::DumpSourceRange(const Stmt *Node) {
Chris Lattnere300c872007-08-30 06:17:34 +0000172 // Can't translate locations if a SourceManager isn't available.
173 if (SM == 0) return;
174
175 // TODO: If the parent expression is available, we can print a delta vs its
176 // location.
177 SourceRange R = Node->getSourceRange();
178
179 fprintf(stderr, " <");
Chris Lattner311ff022007-10-16 22:36:42 +0000180 DumpLocation(R.getBegin());
181 if (R.getBegin() != R.getEnd()) {
Chris Lattnere300c872007-08-30 06:17:34 +0000182 fprintf(stderr, ", ");
Chris Lattner311ff022007-10-16 22:36:42 +0000183 DumpLocation(R.getEnd());
Chris Lattnere300c872007-08-30 06:17:34 +0000184 }
185 fprintf(stderr, ">");
186
187 // <t2.c:123:421[blah], t2.c:412:321>
188
189}
190
191
192//===----------------------------------------------------------------------===//
Chris Lattner6000dac2007-08-08 22:51:59 +0000193// Stmt printing methods.
194//===----------------------------------------------------------------------===//
195
196void StmtDumper::VisitStmt(Stmt *Node) {
Chris Lattner17a1a722007-08-30 01:00:35 +0000197 DumpStmt(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000198}
199
Chris Lattnerf9e05812007-08-09 18:03:18 +0000200void StmtDumper::DumpDeclarator(Decl *D) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000201 // FIXME: Need to complete/beautify this... this code simply shows the
202 // nodes are where they need to be.
203 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000204 fprintf(F, "\"typedef %s %s\"",
205 localType->getUnderlyingType().getAsString().c_str(),
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000206 localType->getIdentifierName());
Chris Lattner6000dac2007-08-08 22:51:59 +0000207 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000208 fprintf(F, "\"");
Chris Lattner6000dac2007-08-08 22:51:59 +0000209 // Emit storage class for vardecls.
210 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
211 switch (V->getStorageClass()) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000212 default: assert(0 && "Unknown storage class!");
213 case VarDecl::None: break;
214 case VarDecl::Extern: fprintf(F, "extern "); break;
215 case VarDecl::Static: fprintf(F, "static "); break;
216 case VarDecl::Auto: fprintf(F, "auto "); break;
217 case VarDecl::Register: fprintf(F, "register "); break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000218 }
219 }
220
221 std::string Name = VD->getName();
222 VD->getType().getAsStringInternal(Name);
Chris Lattnerf9e05812007-08-09 18:03:18 +0000223 fprintf(F, "%s", Name.c_str());
Chris Lattner6000dac2007-08-08 22:51:59 +0000224
225 // If this is a vardecl with an initializer, emit it.
226 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
227 if (V->getInit()) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000228 fprintf(F, " =\n");
229 DumpSubTree(V->getInit());
Chris Lattner6000dac2007-08-08 22:51:59 +0000230 }
231 }
Chris Lattnerf9e05812007-08-09 18:03:18 +0000232 fprintf(F, "\"");
Steve Naroff92199282007-11-17 21:37:36 +0000233 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
234 // print a free standing tag decl (e.g. "struct x;").
235 const char *tagname;
236 if (const IdentifierInfo *II = TD->getIdentifier())
237 tagname = II->getName();
238 else
239 tagname = "<anonymous>";
240 fprintf(F, "\"%s %s;\"", TD->getKindName(), tagname);
241 // FIXME: print tag bodies.
Chris Lattner6000dac2007-08-08 22:51:59 +0000242 } else {
Chris Lattner6000dac2007-08-08 22:51:59 +0000243 assert(0 && "Unexpected decl");
244 }
Chris Lattner6000dac2007-08-08 22:51:59 +0000245}
246
Ted Kremenek5399ce22007-12-12 06:59:42 +0000247void StmtDumper::VisitDeclStmt(DeclStmt *Node) {
248 DumpStmt(Node);
249 fprintf(F,"\n");
Ted Kremenek04a72b72008-10-06 18:38:35 +0000250 for (DeclStmt::decl_iterator DI = Node->decl_begin(), DE = Node->decl_end();
251 DI != DE; ++DI) {
252 ScopedDecl* D = *DI;
Ted Kremenek5399ce22007-12-12 06:59:42 +0000253 ++IndentLevel;
254 Indent();
255 fprintf(F, "%p ", (void*) D);
256 DumpDeclarator(D);
257 if (D->getNextDeclarator())
258 fprintf(F,"\n");
259 --IndentLevel;
260 }
261}
262
Chris Lattner6000dac2007-08-08 22:51:59 +0000263void StmtDumper::VisitLabelStmt(LabelStmt *Node) {
264 DumpStmt(Node);
Chris Lattner3cb640b2008-07-26 19:24:43 +0000265 fprintf(F, " '%s'", Node->getName());
Chris Lattner6000dac2007-08-08 22:51:59 +0000266}
267
Chris Lattner6000dac2007-08-08 22:51:59 +0000268void StmtDumper::VisitGotoStmt(GotoStmt *Node) {
269 DumpStmt(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000270 fprintf(F, " '%s':%p", Node->getLabel()->getName(), (void*)Node->getLabel());
Chris Lattner6000dac2007-08-08 22:51:59 +0000271}
272
Chris Lattner6000dac2007-08-08 22:51:59 +0000273//===----------------------------------------------------------------------===//
274// Expr printing methods.
275//===----------------------------------------------------------------------===//
276
277void StmtDumper::VisitExpr(Expr *Node) {
278 DumpExpr(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000279}
280
281void StmtDumper::VisitDeclRefExpr(DeclRefExpr *Node) {
282 DumpExpr(Node);
Ted Kremenekeb641f92007-09-10 17:32:55 +0000283
284 fprintf(F, " ");
285 switch (Node->getDecl()->getKind()) {
286 case Decl::Function: fprintf(F,"FunctionDecl"); break;
Steve Naroff248a7532008-04-15 22:42:06 +0000287 case Decl::Var: fprintf(F,"Var"); break;
Chris Lattneraa9fc462007-10-08 21:37:32 +0000288 case Decl::ParmVar: fprintf(F,"ParmVar"); break;
Ted Kremenekeb641f92007-09-10 17:32:55 +0000289 case Decl::EnumConstant: fprintf(F,"EnumConstant"); break;
290 case Decl::Typedef: fprintf(F,"Typedef"); break;
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000291 case Decl::Record: fprintf(F,"Record"); break;
Ted Kremenekeb641f92007-09-10 17:32:55 +0000292 case Decl::Enum: fprintf(F,"Enum"); break;
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000293 case Decl::CXXRecord: fprintf(F,"CXXRecord"); break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000294 case Decl::ObjCInterface: fprintf(F,"ObjCInterface"); break;
295 case Decl::ObjCClass: fprintf(F,"ObjCClass"); break;
Ted Kremenekeb641f92007-09-10 17:32:55 +0000296 default: fprintf(F,"Decl"); break;
297 }
298
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000299 fprintf(F, "='%s' %p", Node->getDecl()->getName().c_str(),
300 (void*)Node->getDecl());
Chris Lattner6000dac2007-08-08 22:51:59 +0000301}
302
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000303void StmtDumper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Steve Naroff466c2e32008-05-23 00:59:14 +0000304 DumpExpr(Node);
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000305
Steve Naroff466c2e32008-05-23 00:59:14 +0000306 fprintf(F, " %sDecl='%s' %p", Node->getDecl()->getDeclKindName(),
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000307 Node->getDecl()->getIdentifierName(),
308 (void*)Node->getDecl());
Steve Naroff218543b2008-05-23 22:01:24 +0000309 if (Node->isFreeIvar())
310 fprintf(F, " isFreeIvar");
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000311}
312
Chris Lattnerd9f69102008-08-10 01:53:14 +0000313void StmtDumper::VisitPredefinedExpr(PredefinedExpr *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000314 DumpExpr(Node);
315 switch (Node->getIdentType()) {
Chris Lattner0d17f6f2008-06-21 18:04:54 +0000316 default: assert(0 && "unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000317 case PredefinedExpr::Func: fprintf(F, " __func__"); break;
318 case PredefinedExpr::Function: fprintf(F, " __FUNCTION__"); break;
319 case PredefinedExpr::PrettyFunction: fprintf(F, " __PRETTY_FUNCTION__");break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000320 }
321}
322
323void StmtDumper::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnera0df31a2007-08-09 01:04:32 +0000324 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000325 fprintf(F, " %d", Node->getValue());
Chris Lattner6000dac2007-08-08 22:51:59 +0000326}
327
328void StmtDumper::VisitIntegerLiteral(IntegerLiteral *Node) {
329 DumpExpr(Node);
330
331 bool isSigned = Node->getType()->isSignedIntegerType();
Chris Lattnerb3938792007-08-30 00:53:54 +0000332 fprintf(F, " %s", Node->getValue().toString(10, isSigned).c_str());
Chris Lattner6000dac2007-08-08 22:51:59 +0000333}
334void StmtDumper::VisitFloatingLiteral(FloatingLiteral *Node) {
335 DumpExpr(Node);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000336 fprintf(F, " %f", Node->getValueAsApproximateDouble());
Chris Lattner6000dac2007-08-08 22:51:59 +0000337}
Chris Lattner5d661452007-08-26 03:42:43 +0000338
Chris Lattner6000dac2007-08-08 22:51:59 +0000339void StmtDumper::VisitStringLiteral(StringLiteral *Str) {
Chris Lattnera0df31a2007-08-09 01:04:32 +0000340 DumpExpr(Str);
341 // FIXME: this doesn't print wstrings right.
Chris Lattner5fc61072007-08-09 17:14:24 +0000342 fprintf(F, " %s\"", Str->isWide() ? "L" : "");
Chris Lattnera0df31a2007-08-09 01:04:32 +0000343
Chris Lattner6000dac2007-08-08 22:51:59 +0000344 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattner5fc61072007-08-09 17:14:24 +0000345 switch (char C = Str->getStrData()[i]) {
346 default:
347 if (isprint(C))
348 fputc(C, F);
349 else
350 fprintf(F, "\\%03o", C);
351 break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000352 // Handle some common ones to make dumps prettier.
Chris Lattner5fc61072007-08-09 17:14:24 +0000353 case '\\': fprintf(F, "\\\\"); break;
354 case '"': fprintf(F, "\\\""); break;
355 case '\n': fprintf(F, "\\n"); break;
356 case '\t': fprintf(F, "\\t"); break;
357 case '\a': fprintf(F, "\\a"); break;
358 case '\b': fprintf(F, "\\b"); break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000359 }
360 }
Chris Lattnerb3938792007-08-30 00:53:54 +0000361 fprintf(F, "\"");
Chris Lattner6000dac2007-08-08 22:51:59 +0000362}
Chris Lattner17a1a722007-08-30 01:00:35 +0000363
Chris Lattner6000dac2007-08-08 22:51:59 +0000364void StmtDumper::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000365 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000366 fprintf(F, " %s '%s'", Node->isPostfix() ? "postfix" : "prefix",
Chris Lattner13cb21f2007-08-09 17:35:30 +0000367 UnaryOperator::getOpcodeStr(Node->getOpcode()));
Chris Lattner6000dac2007-08-08 22:51:59 +0000368}
Sebastian Redl05189992008-11-11 17:56:53 +0000369void StmtDumper::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000370 DumpExpr(Node);
371 fprintf(F, " %s ", Node->isSizeOf() ? "sizeof" : "alignof");
Sebastian Redl05189992008-11-11 17:56:53 +0000372 if (Node->isArgumentType())
373 DumpType(Node->getArgumentType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000374}
Chris Lattner13cb21f2007-08-09 17:35:30 +0000375
Chris Lattner6000dac2007-08-08 22:51:59 +0000376void StmtDumper::VisitMemberExpr(MemberExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000377 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000378 fprintf(F, " %s%s %p", Node->isArrow() ? "->" : ".",
Chris Lattner077bf5e2008-11-24 03:33:13 +0000379 Node->getMemberDecl()->getNameAsString().c_str(),
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000380 (void*)Node->getMemberDecl());
Chris Lattner6000dac2007-08-08 22:51:59 +0000381}
Nate Begeman213541a2008-04-18 23:10:10 +0000382void StmtDumper::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000383 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000384 fprintf(F, " %s", Node->getAccessor().getName());
Chris Lattner6000dac2007-08-08 22:51:59 +0000385}
Chris Lattner6000dac2007-08-08 22:51:59 +0000386void StmtDumper::VisitBinaryOperator(BinaryOperator *Node) {
387 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000388 fprintf(F, " '%s'", BinaryOperator::getOpcodeStr(Node->getOpcode()));
Chris Lattnereb14fe82007-08-25 02:00:02 +0000389}
390void StmtDumper::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
391 DumpExpr(Node);
392 fprintf(F, " '%s' ComputeTy=",
393 BinaryOperator::getOpcodeStr(Node->getOpcode()));
394 DumpType(Node->getComputationType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000395}
Chris Lattner6000dac2007-08-08 22:51:59 +0000396
397// GNU extensions.
398
399void StmtDumper::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000400 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000401 fprintf(F, " %s %p", Node->getLabel()->getName(), (void*)Node->getLabel());
Chris Lattner6000dac2007-08-08 22:51:59 +0000402}
403
Chris Lattner6000dac2007-08-08 22:51:59 +0000404void StmtDumper::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000405 DumpExpr(Node);
406 fprintf(F, " ");
407 DumpType(Node->getArgType1());
408 fprintf(F, " ");
409 DumpType(Node->getArgType2());
Chris Lattner6000dac2007-08-08 22:51:59 +0000410}
411
Chris Lattnerf9e05812007-08-09 18:03:18 +0000412//===----------------------------------------------------------------------===//
413// C++ Expressions
414//===----------------------------------------------------------------------===//
Chris Lattner6000dac2007-08-08 22:51:59 +0000415
Douglas Gregor49badde2008-10-27 19:41:14 +0000416void StmtDumper::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000417 DumpExpr(Node);
Douglas Gregor49badde2008-10-27 19:41:14 +0000418 fprintf(F, " %s<%s>", Node->getCastName(),
419 Node->getTypeAsWritten().getAsString().c_str());
Chris Lattner6000dac2007-08-08 22:51:59 +0000420}
421
422void StmtDumper::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000423 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000424 fprintf(F, " %s", Node->getValue() ? "true" : "false");
Chris Lattner6000dac2007-08-08 22:51:59 +0000425}
426
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000427void StmtDumper::VisitCXXThisExpr(CXXThisExpr *Node) {
428 DumpExpr(Node);
429 fprintf(F, " this");
430}
431
Douglas Gregor49badde2008-10-27 19:41:14 +0000432void StmtDumper::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
433 DumpExpr(Node);
434 fprintf(F, " functional cast to %s",
435 Node->getTypeAsWritten().getAsString().c_str());
436}
437
Anders Carlsson55085182007-08-21 17:43:55 +0000438//===----------------------------------------------------------------------===//
439// Obj-C Expressions
440//===----------------------------------------------------------------------===//
441
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000442void StmtDumper::VisitObjCMessageExpr(ObjCMessageExpr* Node) {
443 DumpExpr(Node);
Chris Lattner077bf5e2008-11-24 03:33:13 +0000444 fprintf(F, " selector=%s", Node->getSelector().getAsString().c_str());
Ted Kremenekea958e572008-05-01 17:26:20 +0000445 IdentifierInfo* clsName = Node->getClassName();
446 if (clsName) fprintf(F, " class=%s", clsName->getName());
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000447}
448
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000449void StmtDumper::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
450 DumpExpr(Node);
451
452 fprintf(F, " ");
453 DumpType(Node->getEncodedType());
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000454}
455
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000456void StmtDumper::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
457 DumpExpr(Node);
458
459 fprintf(F, " ");
Chris Lattner077bf5e2008-11-24 03:33:13 +0000460 fprintf(F, "%s", Node->getSelector().getAsString().c_str());
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000461}
462
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000463void StmtDumper::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
464 DumpExpr(Node);
465
466 fprintf(F, " ");
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000467 fprintf(F, "%s", Node->getProtocol()->getIdentifierName());
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000468}
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000469
470void StmtDumper::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
471 DumpExpr(Node);
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000472
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000473 fprintf(F, " Kind=PropertyRef Property=\"%s\"",
474 Node->getProperty()->getIdentifierName());
475}
476
477void StmtDumper::VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node) {
478 DumpExpr(Node);
479
480 ObjCMethodDecl *Getter = Node->getGetterMethod();
481 ObjCMethodDecl *Setter = Node->getSetterMethod();
482 fprintf(F, " Kind=MethodRef Getter=\"%s\" Setter=\"%s\"",
Chris Lattner077bf5e2008-11-24 03:33:13 +0000483 Getter->getSelector().getAsString().c_str(),
484 Setter ? Setter->getSelector().getAsString().c_str() : "(null)");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000485}
486
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000487void StmtDumper::VisitObjCSuperExpr(ObjCSuperExpr *Node) {
488 DumpExpr(Node);
489 fprintf(F, " super");
490}
491
Chris Lattner6000dac2007-08-08 22:51:59 +0000492//===----------------------------------------------------------------------===//
493// Stmt method implementations
494//===----------------------------------------------------------------------===//
495
496/// dump - This does a local dump of the specified AST fragment. It dumps the
497/// specified node and a few nodes underneath it, but not the whole subtree.
498/// This is useful in a debugger.
Chris Lattnere300c872007-08-30 06:17:34 +0000499void Stmt::dump(SourceManager &SM) const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000500 StmtDumper P(&SM, stderr, 4);
Chris Lattnerb3938792007-08-30 00:53:54 +0000501 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattner0c727a32007-08-30 00:40:08 +0000502 fprintf(stderr, "\n");
503}
504
505/// dump - This does a local dump of the specified AST fragment. It dumps the
506/// specified node and a few nodes underneath it, but not the whole subtree.
507/// This is useful in a debugger.
Chris Lattner6000dac2007-08-08 22:51:59 +0000508void Stmt::dump() const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000509 StmtDumper P(0, stderr, 4);
Chris Lattnerb3938792007-08-30 00:53:54 +0000510 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattner0c727a32007-08-30 00:40:08 +0000511 fprintf(stderr, "\n");
512}
513
514/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
Chris Lattnere300c872007-08-30 06:17:34 +0000515void Stmt::dumpAll(SourceManager &SM) const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000516 StmtDumper P(&SM, stderr, ~0U);
Chris Lattnerb3938792007-08-30 00:53:54 +0000517 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattneree41ce52007-08-10 21:51:12 +0000518 fprintf(stderr, "\n");
Chris Lattner6000dac2007-08-08 22:51:59 +0000519}
520
521/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
522void Stmt::dumpAll() const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000523 StmtDumper P(0, stderr, ~0U);
Chris Lattnerb3938792007-08-30 00:53:54 +0000524 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattneree41ce52007-08-10 21:51:12 +0000525 fprintf(stderr, "\n");
Chris Lattner6000dac2007-08-08 22:51:59 +0000526}