blob: bc096bf0d9f397a046dcea610b97c348b239d008 [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);
120 void VisitDeclRefExpr(DeclRefExpr *Node);
Chris Lattnerd9f69102008-08-10 01:53:14 +0000121 void VisitPredefinedExpr(PredefinedExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000122 void VisitCharacterLiteral(CharacterLiteral *Node);
123 void VisitIntegerLiteral(IntegerLiteral *Node);
124 void VisitFloatingLiteral(FloatingLiteral *Node);
125 void VisitStringLiteral(StringLiteral *Str);
126 void VisitUnaryOperator(UnaryOperator *Node);
Sebastian Redl05189992008-11-11 17:56:53 +0000127 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000128 void VisitMemberExpr(MemberExpr *Node);
Nate Begeman213541a2008-04-18 23:10:10 +0000129 void VisitExtVectorElementExpr(ExtVectorElementExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000130 void VisitBinaryOperator(BinaryOperator *Node);
131 void VisitCompoundAssignOperator(CompoundAssignOperator *Node);
132 void VisitAddrLabelExpr(AddrLabelExpr *Node);
133 void VisitTypesCompatibleExpr(TypesCompatibleExpr *Node);
134
135 // C++
Douglas Gregor49badde2008-10-27 19:41:14 +0000136 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000137 void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node);
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000138 void VisitCXXThisExpr(CXXThisExpr *Node);
Douglas Gregor49badde2008-10-27 19:41:14 +0000139 void VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node);
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000140
Chris Lattner17a1a722007-08-30 01:00:35 +0000141 // ObjC
142 void VisitObjCEncodeExpr(ObjCEncodeExpr *Node);
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000143 void VisitObjCMessageExpr(ObjCMessageExpr* Node);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000144 void VisitObjCSelectorExpr(ObjCSelectorExpr *Node);
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000145 void VisitObjCProtocolExpr(ObjCProtocolExpr *Node);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000146 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node);
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000147 void VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node);
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000148 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node);
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000149 void VisitObjCSuperExpr(ObjCSuperExpr *Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000150 };
151}
152
153//===----------------------------------------------------------------------===//
Chris Lattnere300c872007-08-30 06:17:34 +0000154// Utilities
155//===----------------------------------------------------------------------===//
156
157void StmtDumper::DumpLocation(SourceLocation Loc) {
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000158 SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000159
160 if (SpellingLoc.isInvalid()) {
161 fprintf(stderr, "<invalid sloc>");
162 return;
163 }
Chris Lattnere300c872007-08-30 06:17:34 +0000164
165 // The general format we print out is filename:line:col, but we drop pieces
166 // that haven't changed since the last loc printed.
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000167 PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
168
169 if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
170 fprintf(stderr, "%s:%u:%u", PLoc.getFilename(), PLoc.getLine(),
171 PLoc.getColumn());
172 LastLocFilename = PLoc.getFilename();
173 LastLocLine = PLoc.getLine();
174 } else if (PLoc.getLine() != LastLocLine) {
175 fprintf(stderr, "line:%u:%u", PLoc.getLine(), PLoc.getColumn());
176 LastLocLine = PLoc.getLine();
Chris Lattnere300c872007-08-30 06:17:34 +0000177 } else {
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000178 fprintf(stderr, "col:%u", PLoc.getColumn());
Chris Lattnere300c872007-08-30 06:17:34 +0000179 }
180}
181
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000182void StmtDumper::DumpSourceRange(const Stmt *Node) {
Chris Lattnere300c872007-08-30 06:17:34 +0000183 // Can't translate locations if a SourceManager isn't available.
184 if (SM == 0) return;
185
186 // TODO: If the parent expression is available, we can print a delta vs its
187 // location.
188 SourceRange R = Node->getSourceRange();
189
190 fprintf(stderr, " <");
Chris Lattner311ff022007-10-16 22:36:42 +0000191 DumpLocation(R.getBegin());
192 if (R.getBegin() != R.getEnd()) {
Chris Lattnere300c872007-08-30 06:17:34 +0000193 fprintf(stderr, ", ");
Chris Lattner311ff022007-10-16 22:36:42 +0000194 DumpLocation(R.getEnd());
Chris Lattnere300c872007-08-30 06:17:34 +0000195 }
196 fprintf(stderr, ">");
197
198 // <t2.c:123:421[blah], t2.c:412:321>
199
200}
201
202
203//===----------------------------------------------------------------------===//
Chris Lattner6000dac2007-08-08 22:51:59 +0000204// Stmt printing methods.
205//===----------------------------------------------------------------------===//
206
207void StmtDumper::VisitStmt(Stmt *Node) {
Chris Lattner17a1a722007-08-30 01:00:35 +0000208 DumpStmt(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000209}
210
Chris Lattnerf9e05812007-08-09 18:03:18 +0000211void StmtDumper::DumpDeclarator(Decl *D) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000212 // FIXME: Need to complete/beautify this... this code simply shows the
213 // nodes are where they need to be.
214 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000215 fprintf(F, "\"typedef %s %s\"",
216 localType->getUnderlyingType().getAsString().c_str(),
Chris Lattner8ec03f52008-11-24 03:54:41 +0000217 localType->getNameAsString().c_str());
Chris Lattner6000dac2007-08-08 22:51:59 +0000218 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000219 fprintf(F, "\"");
Chris Lattner6000dac2007-08-08 22:51:59 +0000220 // Emit storage class for vardecls.
221 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +0000222 if (V->getStorageClass() != VarDecl::None)
223 fprintf(F, "%s ",
224 VarDecl::getStorageClassSpecifierString(V->getStorageClass()));
Chris Lattner6000dac2007-08-08 22:51:59 +0000225 }
226
Chris Lattner39f34e92008-11-24 04:00:27 +0000227 std::string Name = VD->getNameAsString();
Chris Lattnere4f21422009-06-30 01:26:17 +0000228 VD->getType().getAsStringInternal(Name,
229 PrintingPolicy(VD->getASTContext().getLangOptions()));
Chris Lattnerf9e05812007-08-09 18:03:18 +0000230 fprintf(F, "%s", Name.c_str());
Chris Lattner6000dac2007-08-08 22:51:59 +0000231
232 // If this is a vardecl with an initializer, emit it.
233 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
234 if (V->getInit()) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000235 fprintf(F, " =\n");
236 DumpSubTree(V->getInit());
Chris Lattner6000dac2007-08-08 22:51:59 +0000237 }
238 }
Chris Lattnerf9e05812007-08-09 18:03:18 +0000239 fprintf(F, "\"");
Steve Naroff92199282007-11-17 21:37:36 +0000240 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
241 // print a free standing tag decl (e.g. "struct x;").
242 const char *tagname;
243 if (const IdentifierInfo *II = TD->getIdentifier())
244 tagname = II->getName();
245 else
246 tagname = "<anonymous>";
247 fprintf(F, "\"%s %s;\"", TD->getKindName(), tagname);
248 // FIXME: print tag bodies.
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000249 } else if (UsingDirectiveDecl *UD = dyn_cast<UsingDirectiveDecl>(D)) {
250 // print using-directive decl (e.g. "using namespace x;")
251 const char *ns;
252 if (const IdentifierInfo *II = UD->getNominatedNamespace()->getIdentifier())
253 ns = II->getName();
254 else
255 ns = "<anonymous>";
256 fprintf(F, "\"%s %s;\"",UD->getDeclKindName(), ns);
Chris Lattner6000dac2007-08-08 22:51:59 +0000257 } else {
Chris Lattner6000dac2007-08-08 22:51:59 +0000258 assert(0 && "Unexpected decl");
259 }
Chris Lattner6000dac2007-08-08 22:51:59 +0000260}
261
Ted Kremenek5399ce22007-12-12 06:59:42 +0000262void StmtDumper::VisitDeclStmt(DeclStmt *Node) {
263 DumpStmt(Node);
264 fprintf(F,"\n");
Ted Kremenek04a72b72008-10-06 18:38:35 +0000265 for (DeclStmt::decl_iterator DI = Node->decl_begin(), DE = Node->decl_end();
266 DI != DE; ++DI) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000267 Decl* D = *DI;
Ted Kremenek5399ce22007-12-12 06:59:42 +0000268 ++IndentLevel;
269 Indent();
270 fprintf(F, "%p ", (void*) D);
271 DumpDeclarator(D);
Chris Lattnerf2797252009-03-29 16:04:50 +0000272 if (DI+1 != DE)
Ted Kremenek5399ce22007-12-12 06:59:42 +0000273 fprintf(F,"\n");
274 --IndentLevel;
275 }
276}
277
Chris Lattner6000dac2007-08-08 22:51:59 +0000278void StmtDumper::VisitLabelStmt(LabelStmt *Node) {
279 DumpStmt(Node);
Chris Lattner3cb640b2008-07-26 19:24:43 +0000280 fprintf(F, " '%s'", Node->getName());
Chris Lattner6000dac2007-08-08 22:51:59 +0000281}
282
Chris Lattner6000dac2007-08-08 22:51:59 +0000283void StmtDumper::VisitGotoStmt(GotoStmt *Node) {
284 DumpStmt(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000285 fprintf(F, " '%s':%p", Node->getLabel()->getName(), (void*)Node->getLabel());
Chris Lattner6000dac2007-08-08 22:51:59 +0000286}
287
Chris Lattner6000dac2007-08-08 22:51:59 +0000288//===----------------------------------------------------------------------===//
289// Expr printing methods.
290//===----------------------------------------------------------------------===//
291
292void StmtDumper::VisitExpr(Expr *Node) {
293 DumpExpr(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000294}
295
296void StmtDumper::VisitDeclRefExpr(DeclRefExpr *Node) {
297 DumpExpr(Node);
Ted Kremenekeb641f92007-09-10 17:32:55 +0000298
299 fprintf(F, " ");
300 switch (Node->getDecl()->getKind()) {
301 case Decl::Function: fprintf(F,"FunctionDecl"); break;
Steve Naroff248a7532008-04-15 22:42:06 +0000302 case Decl::Var: fprintf(F,"Var"); break;
Chris Lattneraa9fc462007-10-08 21:37:32 +0000303 case Decl::ParmVar: fprintf(F,"ParmVar"); break;
Ted Kremenekeb641f92007-09-10 17:32:55 +0000304 case Decl::EnumConstant: fprintf(F,"EnumConstant"); break;
305 case Decl::Typedef: fprintf(F,"Typedef"); break;
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000306 case Decl::Record: fprintf(F,"Record"); break;
Ted Kremenekeb641f92007-09-10 17:32:55 +0000307 case Decl::Enum: fprintf(F,"Enum"); break;
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000308 case Decl::CXXRecord: fprintf(F,"CXXRecord"); break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000309 case Decl::ObjCInterface: fprintf(F,"ObjCInterface"); break;
310 case Decl::ObjCClass: fprintf(F,"ObjCClass"); break;
Ted Kremenekeb641f92007-09-10 17:32:55 +0000311 default: fprintf(F,"Decl"); break;
312 }
313
Chris Lattner39f34e92008-11-24 04:00:27 +0000314 fprintf(F, "='%s' %p", Node->getDecl()->getNameAsString().c_str(),
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000315 (void*)Node->getDecl());
Chris Lattner6000dac2007-08-08 22:51:59 +0000316}
317
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000318void StmtDumper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Steve Naroff466c2e32008-05-23 00:59:14 +0000319 DumpExpr(Node);
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000320
Steve Naroff466c2e32008-05-23 00:59:14 +0000321 fprintf(F, " %sDecl='%s' %p", Node->getDecl()->getDeclKindName(),
Chris Lattner8ec03f52008-11-24 03:54:41 +0000322 Node->getDecl()->getNameAsString().c_str(), (void*)Node->getDecl());
Steve Naroff218543b2008-05-23 22:01:24 +0000323 if (Node->isFreeIvar())
324 fprintf(F, " isFreeIvar");
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000325}
326
Chris Lattnerd9f69102008-08-10 01:53:14 +0000327void StmtDumper::VisitPredefinedExpr(PredefinedExpr *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000328 DumpExpr(Node);
329 switch (Node->getIdentType()) {
Chris Lattner0d17f6f2008-06-21 18:04:54 +0000330 default: assert(0 && "unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000331 case PredefinedExpr::Func: fprintf(F, " __func__"); break;
332 case PredefinedExpr::Function: fprintf(F, " __FUNCTION__"); break;
333 case PredefinedExpr::PrettyFunction: fprintf(F, " __PRETTY_FUNCTION__");break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000334 }
335}
336
337void StmtDumper::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnera0df31a2007-08-09 01:04:32 +0000338 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000339 fprintf(F, " %d", Node->getValue());
Chris Lattner6000dac2007-08-08 22:51:59 +0000340}
341
342void StmtDumper::VisitIntegerLiteral(IntegerLiteral *Node) {
343 DumpExpr(Node);
344
345 bool isSigned = Node->getType()->isSignedIntegerType();
Chris Lattnerb3938792007-08-30 00:53:54 +0000346 fprintf(F, " %s", Node->getValue().toString(10, isSigned).c_str());
Chris Lattner6000dac2007-08-08 22:51:59 +0000347}
348void StmtDumper::VisitFloatingLiteral(FloatingLiteral *Node) {
349 DumpExpr(Node);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000350 fprintf(F, " %f", Node->getValueAsApproximateDouble());
Chris Lattner6000dac2007-08-08 22:51:59 +0000351}
Chris Lattner5d661452007-08-26 03:42:43 +0000352
Chris Lattner6000dac2007-08-08 22:51:59 +0000353void StmtDumper::VisitStringLiteral(StringLiteral *Str) {
Chris Lattnera0df31a2007-08-09 01:04:32 +0000354 DumpExpr(Str);
355 // FIXME: this doesn't print wstrings right.
Chris Lattner5fc61072007-08-09 17:14:24 +0000356 fprintf(F, " %s\"", Str->isWide() ? "L" : "");
Chris Lattnera0df31a2007-08-09 01:04:32 +0000357
Chris Lattner6000dac2007-08-08 22:51:59 +0000358 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattner5fc61072007-08-09 17:14:24 +0000359 switch (char C = Str->getStrData()[i]) {
360 default:
361 if (isprint(C))
362 fputc(C, F);
363 else
364 fprintf(F, "\\%03o", C);
365 break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000366 // Handle some common ones to make dumps prettier.
Chris Lattner5fc61072007-08-09 17:14:24 +0000367 case '\\': fprintf(F, "\\\\"); break;
368 case '"': fprintf(F, "\\\""); break;
369 case '\n': fprintf(F, "\\n"); break;
370 case '\t': fprintf(F, "\\t"); break;
371 case '\a': fprintf(F, "\\a"); break;
372 case '\b': fprintf(F, "\\b"); break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000373 }
374 }
Chris Lattnerb3938792007-08-30 00:53:54 +0000375 fprintf(F, "\"");
Chris Lattner6000dac2007-08-08 22:51:59 +0000376}
Chris Lattner17a1a722007-08-30 01:00:35 +0000377
Chris Lattner6000dac2007-08-08 22:51:59 +0000378void StmtDumper::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000379 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000380 fprintf(F, " %s '%s'", Node->isPostfix() ? "postfix" : "prefix",
Chris Lattner13cb21f2007-08-09 17:35:30 +0000381 UnaryOperator::getOpcodeStr(Node->getOpcode()));
Chris Lattner6000dac2007-08-08 22:51:59 +0000382}
Sebastian Redl05189992008-11-11 17:56:53 +0000383void StmtDumper::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000384 DumpExpr(Node);
385 fprintf(F, " %s ", Node->isSizeOf() ? "sizeof" : "alignof");
Sebastian Redl05189992008-11-11 17:56:53 +0000386 if (Node->isArgumentType())
387 DumpType(Node->getArgumentType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000388}
Chris Lattner13cb21f2007-08-09 17:35:30 +0000389
Chris Lattner6000dac2007-08-08 22:51:59 +0000390void StmtDumper::VisitMemberExpr(MemberExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000391 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000392 fprintf(F, " %s%s %p", Node->isArrow() ? "->" : ".",
Chris Lattner077bf5e2008-11-24 03:33:13 +0000393 Node->getMemberDecl()->getNameAsString().c_str(),
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000394 (void*)Node->getMemberDecl());
Chris Lattner6000dac2007-08-08 22:51:59 +0000395}
Nate Begeman213541a2008-04-18 23:10:10 +0000396void StmtDumper::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000397 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000398 fprintf(F, " %s", Node->getAccessor().getName());
Chris Lattner6000dac2007-08-08 22:51:59 +0000399}
Chris Lattner6000dac2007-08-08 22:51:59 +0000400void StmtDumper::VisitBinaryOperator(BinaryOperator *Node) {
401 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000402 fprintf(F, " '%s'", BinaryOperator::getOpcodeStr(Node->getOpcode()));
Chris Lattnereb14fe82007-08-25 02:00:02 +0000403}
404void StmtDumper::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
405 DumpExpr(Node);
Eli Friedmanab3a8522009-03-28 01:22:36 +0000406 fprintf(F, " '%s' ComputeLHSTy=",
Chris Lattnereb14fe82007-08-25 02:00:02 +0000407 BinaryOperator::getOpcodeStr(Node->getOpcode()));
Eli Friedmanab3a8522009-03-28 01:22:36 +0000408 DumpType(Node->getComputationLHSType());
409 fprintf(F, " ComputeResultTy=");
410 DumpType(Node->getComputationResultType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000411}
Chris Lattner6000dac2007-08-08 22:51:59 +0000412
413// GNU extensions.
414
415void StmtDumper::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000416 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000417 fprintf(F, " %s %p", Node->getLabel()->getName(), (void*)Node->getLabel());
Chris Lattner6000dac2007-08-08 22:51:59 +0000418}
419
Chris Lattner6000dac2007-08-08 22:51:59 +0000420void StmtDumper::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000421 DumpExpr(Node);
422 fprintf(F, " ");
423 DumpType(Node->getArgType1());
424 fprintf(F, " ");
425 DumpType(Node->getArgType2());
Chris Lattner6000dac2007-08-08 22:51:59 +0000426}
427
Chris Lattnerf9e05812007-08-09 18:03:18 +0000428//===----------------------------------------------------------------------===//
429// C++ Expressions
430//===----------------------------------------------------------------------===//
Chris Lattner6000dac2007-08-08 22:51:59 +0000431
Douglas Gregor49badde2008-10-27 19:41:14 +0000432void StmtDumper::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000433 DumpExpr(Node);
Douglas Gregor49badde2008-10-27 19:41:14 +0000434 fprintf(F, " %s<%s>", Node->getCastName(),
435 Node->getTypeAsWritten().getAsString().c_str());
Chris Lattner6000dac2007-08-08 22:51:59 +0000436}
437
438void StmtDumper::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000439 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000440 fprintf(F, " %s", Node->getValue() ? "true" : "false");
Chris Lattner6000dac2007-08-08 22:51:59 +0000441}
442
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000443void StmtDumper::VisitCXXThisExpr(CXXThisExpr *Node) {
444 DumpExpr(Node);
445 fprintf(F, " this");
446}
447
Douglas Gregor49badde2008-10-27 19:41:14 +0000448void StmtDumper::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
449 DumpExpr(Node);
450 fprintf(F, " functional cast to %s",
451 Node->getTypeAsWritten().getAsString().c_str());
452}
453
Anders Carlsson55085182007-08-21 17:43:55 +0000454//===----------------------------------------------------------------------===//
455// Obj-C Expressions
456//===----------------------------------------------------------------------===//
457
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000458void StmtDumper::VisitObjCMessageExpr(ObjCMessageExpr* Node) {
459 DumpExpr(Node);
Chris Lattner077bf5e2008-11-24 03:33:13 +0000460 fprintf(F, " selector=%s", Node->getSelector().getAsString().c_str());
Ted Kremenekea958e572008-05-01 17:26:20 +0000461 IdentifierInfo* clsName = Node->getClassName();
462 if (clsName) fprintf(F, " class=%s", clsName->getName());
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000463}
464
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000465void StmtDumper::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
466 DumpExpr(Node);
467
468 fprintf(F, " ");
469 DumpType(Node->getEncodedType());
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000470}
471
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000472void StmtDumper::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
473 DumpExpr(Node);
474
475 fprintf(F, " ");
Chris Lattner077bf5e2008-11-24 03:33:13 +0000476 fprintf(F, "%s", Node->getSelector().getAsString().c_str());
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000477}
478
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000479void StmtDumper::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
480 DumpExpr(Node);
481
482 fprintf(F, " ");
Chris Lattner8ec03f52008-11-24 03:54:41 +0000483 fprintf(F, "%s", Node->getProtocol()->getNameAsString().c_str());
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000484}
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000485
486void StmtDumper::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
487 DumpExpr(Node);
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000488
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000489 fprintf(F, " Kind=PropertyRef Property=\"%s\"",
Chris Lattner8ec03f52008-11-24 03:54:41 +0000490 Node->getProperty()->getNameAsString().c_str());
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000491}
492
493void StmtDumper::VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node) {
494 DumpExpr(Node);
495
496 ObjCMethodDecl *Getter = Node->getGetterMethod();
497 ObjCMethodDecl *Setter = Node->getSetterMethod();
498 fprintf(F, " Kind=MethodRef Getter=\"%s\" Setter=\"%s\"",
Chris Lattner077bf5e2008-11-24 03:33:13 +0000499 Getter->getSelector().getAsString().c_str(),
500 Setter ? Setter->getSelector().getAsString().c_str() : "(null)");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000501}
502
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000503void StmtDumper::VisitObjCSuperExpr(ObjCSuperExpr *Node) {
504 DumpExpr(Node);
505 fprintf(F, " super");
506}
507
Chris Lattner6000dac2007-08-08 22:51:59 +0000508//===----------------------------------------------------------------------===//
509// Stmt method implementations
510//===----------------------------------------------------------------------===//
511
512/// dump - This does a local dump of the specified AST fragment. It dumps the
513/// specified node and a few nodes underneath it, but not the whole subtree.
514/// This is useful in a debugger.
Chris Lattnere300c872007-08-30 06:17:34 +0000515void Stmt::dump(SourceManager &SM) const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000516 StmtDumper P(&SM, stderr, 4);
Chris Lattnerb3938792007-08-30 00:53:54 +0000517 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattner0c727a32007-08-30 00:40:08 +0000518 fprintf(stderr, "\n");
519}
520
521/// dump - This does a local dump of the specified AST fragment. It dumps the
522/// specified node and a few nodes underneath it, but not the whole subtree.
523/// This is useful in a debugger.
Chris Lattner6000dac2007-08-08 22:51:59 +0000524void Stmt::dump() const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000525 StmtDumper P(0, stderr, 4);
Chris Lattnerb3938792007-08-30 00:53:54 +0000526 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattner0c727a32007-08-30 00:40:08 +0000527 fprintf(stderr, "\n");
528}
529
530/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
Chris Lattnere300c872007-08-30 06:17:34 +0000531void Stmt::dumpAll(SourceManager &SM) const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000532 StmtDumper P(&SM, stderr, ~0U);
Chris Lattnerb3938792007-08-30 00:53:54 +0000533 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattneree41ce52007-08-10 21:51:12 +0000534 fprintf(stderr, "\n");
Chris Lattner6000dac2007-08-08 22:51:59 +0000535}
536
537/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
538void Stmt::dumpAll() const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000539 StmtDumper P(0, stderr, ~0U);
Chris Lattnerb3938792007-08-30 00:53:54 +0000540 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattneree41ce52007-08-10 21:51:12 +0000541 fprintf(stderr, "\n");
Chris Lattner6000dac2007-08-08 22:51:59 +0000542}