blob: fa3b40cebcded1137194fbaed25f5856363aa81f [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"
Chris Lattnere300c872007-08-30 06:17:34 +000018#include "clang/Basic/SourceManager.h"
Chris Lattner6000dac2007-08-08 22:51:59 +000019#include "llvm/Support/Compiler.h"
20#include <cstdio>
21using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// StmtDumper Visitor
25//===----------------------------------------------------------------------===//
26
27namespace {
Chris Lattnerc5598cb2007-08-21 04:04:25 +000028 class VISIBILITY_HIDDEN StmtDumper : public StmtVisitor<StmtDumper> {
Chris Lattnere300c872007-08-30 06:17:34 +000029 SourceManager *SM;
Chris Lattner6000dac2007-08-08 22:51:59 +000030 FILE *F;
31 unsigned IndentLevel;
32
33 /// MaxDepth - When doing a normal dump (not dumpAll) we only want to dump
34 /// the first few levels of an AST. This keeps track of how many ast levels
35 /// are left.
36 unsigned MaxDepth;
Chris Lattnere300c872007-08-30 06:17:34 +000037
38 /// LastLocFilename/LastLocLine - Keep track of the last location we print
39 /// out so that we can print out deltas from then on out.
40 const char *LastLocFilename;
41 unsigned LastLocLine;
Chris Lattner6000dac2007-08-08 22:51:59 +000042 public:
Chris Lattnere300c872007-08-30 06:17:34 +000043 StmtDumper(SourceManager *sm, FILE *f, unsigned maxDepth)
44 : SM(sm), F(f), IndentLevel(0-1), MaxDepth(maxDepth) {
45 LastLocFilename = "";
46 LastLocLine = ~0U;
47 }
Chris Lattner6000dac2007-08-08 22:51:59 +000048
Chris Lattnerf9e05812007-08-09 18:03:18 +000049 void DumpSubTree(Stmt *S) {
Chris Lattner6000dac2007-08-08 22:51:59 +000050 // Prune the recursion if not using dump all.
51 if (MaxDepth == 0) return;
52
Chris Lattnerf9e05812007-08-09 18:03:18 +000053 ++IndentLevel;
Chris Lattner6000dac2007-08-08 22:51:59 +000054 if (S) {
Ted Kremenek5399ce22007-12-12 06:59:42 +000055 if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
56 VisitDeclStmt(DS);
57 else {
58 Visit(S);
59
60 // Print out children.
61 Stmt::child_iterator CI = S->child_begin(), CE = S->child_end();
62 if (CI != CE) {
63 while (CI != CE) {
64 fprintf(F, "\n");
65 DumpSubTree(*CI++);
66 }
Chris Lattnerb3938792007-08-30 00:53:54 +000067 }
Ted Kremenek5399ce22007-12-12 06:59:42 +000068 fprintf(F, ")");
Chris Lattnerb3938792007-08-30 00:53:54 +000069 }
Chris Lattner6000dac2007-08-08 22:51:59 +000070 } else {
71 Indent();
Chris Lattner4a70adb2007-08-26 03:53:29 +000072 fprintf(F, "<<<NULL>>>");
Chris Lattner6000dac2007-08-08 22:51:59 +000073 }
Chris Lattnerf9e05812007-08-09 18:03:18 +000074 --IndentLevel;
Chris Lattner6000dac2007-08-08 22:51:59 +000075 }
76
Chris Lattnerf9e05812007-08-09 18:03:18 +000077 void DumpDeclarator(Decl *D);
Chris Lattner6000dac2007-08-08 22:51:59 +000078
79 void Indent() const {
80 for (int i = 0, e = IndentLevel; i < e; ++i)
81 fprintf(F, " ");
82 }
83
Steve Naroff9dcbfa42007-09-01 21:08:38 +000084 void DumpType(QualType T) {
Chris Lattnerfd8f7da2007-08-09 00:36:22 +000085 fprintf(F, "'%s'", T.getAsString().c_str());
86
Douglas Gregor61366e92008-12-24 00:01:03 +000087 if (!T.isNull()) {
88 // If the type is directly a typedef, strip off typedefness to give at
89 // least one level of concreteness.
90 if (TypedefType *TDT = dyn_cast<TypedefType>(T)) {
91 QualType Simplified =
92 TDT->LookThroughTypedefs().getQualifiedType(T.getCVRQualifiers());
93 fprintf(F, ":'%s'", Simplified.getAsString().c_str());
94 }
Chris Lattnerbad37852008-04-02 05:06:23 +000095 }
Chris Lattnerfd8f7da2007-08-09 00:36:22 +000096 }
Steve Naroff9dcbfa42007-09-01 21:08:38 +000097 void DumpStmt(const Stmt *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +000098 Indent();
99 fprintf(F, "(%s %p", Node->getStmtClassName(), (void*)Node);
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000100 DumpSourceRange(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000101 }
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000102 void DumpExpr(const Expr *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000103 DumpStmt(Node);
Chris Lattnerfd8f7da2007-08-09 00:36:22 +0000104 fprintf(F, " ");
105 DumpType(Node->getType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000106 }
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000107 void DumpSourceRange(const Stmt *Node);
Chris Lattnere300c872007-08-30 06:17:34 +0000108 void DumpLocation(SourceLocation Loc);
Chris Lattnere300c872007-08-30 06:17:34 +0000109
Chris Lattner17a1a722007-08-30 01:00:35 +0000110 // Stmts.
Chris Lattnerc5598cb2007-08-21 04:04:25 +0000111 void VisitStmt(Stmt *Node);
Ted Kremenek5399ce22007-12-12 06:59:42 +0000112 void VisitDeclStmt(DeclStmt *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000113 void VisitLabelStmt(LabelStmt *Node);
114 void VisitGotoStmt(GotoStmt *Node);
115
116 // Exprs
117 void VisitExpr(Expr *Node);
118 void VisitDeclRefExpr(DeclRefExpr *Node);
Chris Lattnerd9f69102008-08-10 01:53:14 +0000119 void VisitPredefinedExpr(PredefinedExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000120 void VisitCharacterLiteral(CharacterLiteral *Node);
121 void VisitIntegerLiteral(IntegerLiteral *Node);
122 void VisitFloatingLiteral(FloatingLiteral *Node);
123 void VisitStringLiteral(StringLiteral *Str);
124 void VisitUnaryOperator(UnaryOperator *Node);
Sebastian Redl05189992008-11-11 17:56:53 +0000125 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000126 void VisitMemberExpr(MemberExpr *Node);
Nate Begeman213541a2008-04-18 23:10:10 +0000127 void VisitExtVectorElementExpr(ExtVectorElementExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000128 void VisitBinaryOperator(BinaryOperator *Node);
129 void VisitCompoundAssignOperator(CompoundAssignOperator *Node);
130 void VisitAddrLabelExpr(AddrLabelExpr *Node);
131 void VisitTypesCompatibleExpr(TypesCompatibleExpr *Node);
132
133 // C++
Douglas Gregor49badde2008-10-27 19:41:14 +0000134 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000135 void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node);
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000136 void VisitCXXThisExpr(CXXThisExpr *Node);
Douglas Gregor49badde2008-10-27 19:41:14 +0000137 void VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node);
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000138
Chris Lattner17a1a722007-08-30 01:00:35 +0000139 // ObjC
140 void VisitObjCEncodeExpr(ObjCEncodeExpr *Node);
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000141 void VisitObjCMessageExpr(ObjCMessageExpr* Node);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000142 void VisitObjCSelectorExpr(ObjCSelectorExpr *Node);
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000143 void VisitObjCProtocolExpr(ObjCProtocolExpr *Node);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000144 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node);
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000145 void VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node);
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000146 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node);
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000147 void VisitObjCSuperExpr(ObjCSuperExpr *Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000148 };
149}
150
151//===----------------------------------------------------------------------===//
Chris Lattnere300c872007-08-30 06:17:34 +0000152// Utilities
153//===----------------------------------------------------------------------===//
154
155void StmtDumper::DumpLocation(SourceLocation Loc) {
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000156 SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000157
158 if (SpellingLoc.isInvalid()) {
159 fprintf(stderr, "<invalid sloc>");
160 return;
161 }
Chris Lattnere300c872007-08-30 06:17:34 +0000162
163 // The general format we print out is filename:line:col, but we drop pieces
164 // that haven't changed since the last loc printed.
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000165 PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc);
166
167 if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) {
168 fprintf(stderr, "%s:%u:%u", PLoc.getFilename(), PLoc.getLine(),
169 PLoc.getColumn());
170 LastLocFilename = PLoc.getFilename();
171 LastLocLine = PLoc.getLine();
172 } else if (PLoc.getLine() != LastLocLine) {
173 fprintf(stderr, "line:%u:%u", PLoc.getLine(), PLoc.getColumn());
174 LastLocLine = PLoc.getLine();
Chris Lattnere300c872007-08-30 06:17:34 +0000175 } else {
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000176 fprintf(stderr, "col:%u", PLoc.getColumn());
Chris Lattnere300c872007-08-30 06:17:34 +0000177 }
178}
179
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000180void StmtDumper::DumpSourceRange(const Stmt *Node) {
Chris Lattnere300c872007-08-30 06:17:34 +0000181 // Can't translate locations if a SourceManager isn't available.
182 if (SM == 0) return;
183
184 // TODO: If the parent expression is available, we can print a delta vs its
185 // location.
186 SourceRange R = Node->getSourceRange();
187
188 fprintf(stderr, " <");
Chris Lattner311ff022007-10-16 22:36:42 +0000189 DumpLocation(R.getBegin());
190 if (R.getBegin() != R.getEnd()) {
Chris Lattnere300c872007-08-30 06:17:34 +0000191 fprintf(stderr, ", ");
Chris Lattner311ff022007-10-16 22:36:42 +0000192 DumpLocation(R.getEnd());
Chris Lattnere300c872007-08-30 06:17:34 +0000193 }
194 fprintf(stderr, ">");
195
196 // <t2.c:123:421[blah], t2.c:412:321>
197
198}
199
200
201//===----------------------------------------------------------------------===//
Chris Lattner6000dac2007-08-08 22:51:59 +0000202// Stmt printing methods.
203//===----------------------------------------------------------------------===//
204
205void StmtDumper::VisitStmt(Stmt *Node) {
Chris Lattner17a1a722007-08-30 01:00:35 +0000206 DumpStmt(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000207}
208
Chris Lattnerf9e05812007-08-09 18:03:18 +0000209void StmtDumper::DumpDeclarator(Decl *D) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000210 // FIXME: Need to complete/beautify this... this code simply shows the
211 // nodes are where they need to be.
212 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000213 fprintf(F, "\"typedef %s %s\"",
214 localType->getUnderlyingType().getAsString().c_str(),
Chris Lattner8ec03f52008-11-24 03:54:41 +0000215 localType->getNameAsString().c_str());
Chris Lattner6000dac2007-08-08 22:51:59 +0000216 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000217 fprintf(F, "\"");
Chris Lattner6000dac2007-08-08 22:51:59 +0000218 // Emit storage class for vardecls.
219 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
220 switch (V->getStorageClass()) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000221 default: assert(0 && "Unknown storage class!");
222 case VarDecl::None: break;
223 case VarDecl::Extern: fprintf(F, "extern "); break;
224 case VarDecl::Static: fprintf(F, "static "); break;
225 case VarDecl::Auto: fprintf(F, "auto "); break;
226 case VarDecl::Register: fprintf(F, "register "); break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000227 }
228 }
229
Chris Lattner39f34e92008-11-24 04:00:27 +0000230 std::string Name = VD->getNameAsString();
Chris Lattner6000dac2007-08-08 22:51:59 +0000231 VD->getType().getAsStringInternal(Name);
Chris Lattnerf9e05812007-08-09 18:03:18 +0000232 fprintf(F, "%s", Name.c_str());
Chris Lattner6000dac2007-08-08 22:51:59 +0000233
234 // If this is a vardecl with an initializer, emit it.
235 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
236 if (V->getInit()) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000237 fprintf(F, " =\n");
238 DumpSubTree(V->getInit());
Chris Lattner6000dac2007-08-08 22:51:59 +0000239 }
240 }
Chris Lattnerf9e05812007-08-09 18:03:18 +0000241 fprintf(F, "\"");
Steve Naroff92199282007-11-17 21:37:36 +0000242 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
243 // print a free standing tag decl (e.g. "struct x;").
244 const char *tagname;
245 if (const IdentifierInfo *II = TD->getIdentifier())
246 tagname = II->getName();
247 else
248 tagname = "<anonymous>";
249 fprintf(F, "\"%s %s;\"", TD->getKindName(), tagname);
250 // FIXME: print tag bodies.
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000251 } else if (UsingDirectiveDecl *UD = dyn_cast<UsingDirectiveDecl>(D)) {
252 // print using-directive decl (e.g. "using namespace x;")
253 const char *ns;
254 if (const IdentifierInfo *II = UD->getNominatedNamespace()->getIdentifier())
255 ns = II->getName();
256 else
257 ns = "<anonymous>";
258 fprintf(F, "\"%s %s;\"",UD->getDeclKindName(), ns);
Chris Lattner6000dac2007-08-08 22:51:59 +0000259 } else {
Chris Lattner6000dac2007-08-08 22:51:59 +0000260 assert(0 && "Unexpected decl");
261 }
Chris Lattner6000dac2007-08-08 22:51:59 +0000262}
263
Ted Kremenek5399ce22007-12-12 06:59:42 +0000264void StmtDumper::VisitDeclStmt(DeclStmt *Node) {
265 DumpStmt(Node);
266 fprintf(F,"\n");
Ted Kremenek04a72b72008-10-06 18:38:35 +0000267 for (DeclStmt::decl_iterator DI = Node->decl_begin(), DE = Node->decl_end();
268 DI != DE; ++DI) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000269 Decl* D = *DI;
Ted Kremenek5399ce22007-12-12 06:59:42 +0000270 ++IndentLevel;
271 Indent();
272 fprintf(F, "%p ", (void*) D);
273 DumpDeclarator(D);
Chris Lattnerf2797252009-03-29 16:04:50 +0000274 if (DI+1 != DE)
Ted Kremenek5399ce22007-12-12 06:59:42 +0000275 fprintf(F,"\n");
276 --IndentLevel;
277 }
278}
279
Chris Lattner6000dac2007-08-08 22:51:59 +0000280void StmtDumper::VisitLabelStmt(LabelStmt *Node) {
281 DumpStmt(Node);
Chris Lattner3cb640b2008-07-26 19:24:43 +0000282 fprintf(F, " '%s'", Node->getName());
Chris Lattner6000dac2007-08-08 22:51:59 +0000283}
284
Chris Lattner6000dac2007-08-08 22:51:59 +0000285void StmtDumper::VisitGotoStmt(GotoStmt *Node) {
286 DumpStmt(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000287 fprintf(F, " '%s':%p", Node->getLabel()->getName(), (void*)Node->getLabel());
Chris Lattner6000dac2007-08-08 22:51:59 +0000288}
289
Chris Lattner6000dac2007-08-08 22:51:59 +0000290//===----------------------------------------------------------------------===//
291// Expr printing methods.
292//===----------------------------------------------------------------------===//
293
294void StmtDumper::VisitExpr(Expr *Node) {
295 DumpExpr(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000296}
297
298void StmtDumper::VisitDeclRefExpr(DeclRefExpr *Node) {
299 DumpExpr(Node);
Ted Kremenekeb641f92007-09-10 17:32:55 +0000300
301 fprintf(F, " ");
302 switch (Node->getDecl()->getKind()) {
303 case Decl::Function: fprintf(F,"FunctionDecl"); break;
Steve Naroff248a7532008-04-15 22:42:06 +0000304 case Decl::Var: fprintf(F,"Var"); break;
Chris Lattneraa9fc462007-10-08 21:37:32 +0000305 case Decl::ParmVar: fprintf(F,"ParmVar"); break;
Ted Kremenekeb641f92007-09-10 17:32:55 +0000306 case Decl::EnumConstant: fprintf(F,"EnumConstant"); break;
307 case Decl::Typedef: fprintf(F,"Typedef"); break;
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000308 case Decl::Record: fprintf(F,"Record"); break;
Ted Kremenekeb641f92007-09-10 17:32:55 +0000309 case Decl::Enum: fprintf(F,"Enum"); break;
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000310 case Decl::CXXRecord: fprintf(F,"CXXRecord"); break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000311 case Decl::ObjCInterface: fprintf(F,"ObjCInterface"); break;
312 case Decl::ObjCClass: fprintf(F,"ObjCClass"); break;
Ted Kremenekeb641f92007-09-10 17:32:55 +0000313 default: fprintf(F,"Decl"); break;
314 }
315
Chris Lattner39f34e92008-11-24 04:00:27 +0000316 fprintf(F, "='%s' %p", Node->getDecl()->getNameAsString().c_str(),
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000317 (void*)Node->getDecl());
Chris Lattner6000dac2007-08-08 22:51:59 +0000318}
319
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000320void StmtDumper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Steve Naroff466c2e32008-05-23 00:59:14 +0000321 DumpExpr(Node);
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000322
Steve Naroff466c2e32008-05-23 00:59:14 +0000323 fprintf(F, " %sDecl='%s' %p", Node->getDecl()->getDeclKindName(),
Chris Lattner8ec03f52008-11-24 03:54:41 +0000324 Node->getDecl()->getNameAsString().c_str(), (void*)Node->getDecl());
Steve Naroff218543b2008-05-23 22:01:24 +0000325 if (Node->isFreeIvar())
326 fprintf(F, " isFreeIvar");
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000327}
328
Chris Lattnerd9f69102008-08-10 01:53:14 +0000329void StmtDumper::VisitPredefinedExpr(PredefinedExpr *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000330 DumpExpr(Node);
331 switch (Node->getIdentType()) {
Chris Lattner0d17f6f2008-06-21 18:04:54 +0000332 default: assert(0 && "unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000333 case PredefinedExpr::Func: fprintf(F, " __func__"); break;
334 case PredefinedExpr::Function: fprintf(F, " __FUNCTION__"); break;
335 case PredefinedExpr::PrettyFunction: fprintf(F, " __PRETTY_FUNCTION__");break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000336 }
337}
338
339void StmtDumper::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnera0df31a2007-08-09 01:04:32 +0000340 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000341 fprintf(F, " %d", Node->getValue());
Chris Lattner6000dac2007-08-08 22:51:59 +0000342}
343
344void StmtDumper::VisitIntegerLiteral(IntegerLiteral *Node) {
345 DumpExpr(Node);
346
347 bool isSigned = Node->getType()->isSignedIntegerType();
Chris Lattnerb3938792007-08-30 00:53:54 +0000348 fprintf(F, " %s", Node->getValue().toString(10, isSigned).c_str());
Chris Lattner6000dac2007-08-08 22:51:59 +0000349}
350void StmtDumper::VisitFloatingLiteral(FloatingLiteral *Node) {
351 DumpExpr(Node);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000352 fprintf(F, " %f", Node->getValueAsApproximateDouble());
Chris Lattner6000dac2007-08-08 22:51:59 +0000353}
Chris Lattner5d661452007-08-26 03:42:43 +0000354
Chris Lattner6000dac2007-08-08 22:51:59 +0000355void StmtDumper::VisitStringLiteral(StringLiteral *Str) {
Chris Lattnera0df31a2007-08-09 01:04:32 +0000356 DumpExpr(Str);
357 // FIXME: this doesn't print wstrings right.
Chris Lattner5fc61072007-08-09 17:14:24 +0000358 fprintf(F, " %s\"", Str->isWide() ? "L" : "");
Chris Lattnera0df31a2007-08-09 01:04:32 +0000359
Chris Lattner6000dac2007-08-08 22:51:59 +0000360 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattner5fc61072007-08-09 17:14:24 +0000361 switch (char C = Str->getStrData()[i]) {
362 default:
363 if (isprint(C))
364 fputc(C, F);
365 else
366 fprintf(F, "\\%03o", C);
367 break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000368 // Handle some common ones to make dumps prettier.
Chris Lattner5fc61072007-08-09 17:14:24 +0000369 case '\\': fprintf(F, "\\\\"); break;
370 case '"': fprintf(F, "\\\""); break;
371 case '\n': fprintf(F, "\\n"); break;
372 case '\t': fprintf(F, "\\t"); break;
373 case '\a': fprintf(F, "\\a"); break;
374 case '\b': fprintf(F, "\\b"); break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000375 }
376 }
Chris Lattnerb3938792007-08-30 00:53:54 +0000377 fprintf(F, "\"");
Chris Lattner6000dac2007-08-08 22:51:59 +0000378}
Chris Lattner17a1a722007-08-30 01:00:35 +0000379
Chris Lattner6000dac2007-08-08 22:51:59 +0000380void StmtDumper::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000381 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000382 fprintf(F, " %s '%s'", Node->isPostfix() ? "postfix" : "prefix",
Chris Lattner13cb21f2007-08-09 17:35:30 +0000383 UnaryOperator::getOpcodeStr(Node->getOpcode()));
Chris Lattner6000dac2007-08-08 22:51:59 +0000384}
Sebastian Redl05189992008-11-11 17:56:53 +0000385void StmtDumper::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000386 DumpExpr(Node);
387 fprintf(F, " %s ", Node->isSizeOf() ? "sizeof" : "alignof");
Sebastian Redl05189992008-11-11 17:56:53 +0000388 if (Node->isArgumentType())
389 DumpType(Node->getArgumentType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000390}
Chris Lattner13cb21f2007-08-09 17:35:30 +0000391
Chris Lattner6000dac2007-08-08 22:51:59 +0000392void StmtDumper::VisitMemberExpr(MemberExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000393 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000394 fprintf(F, " %s%s %p", Node->isArrow() ? "->" : ".",
Chris Lattner077bf5e2008-11-24 03:33:13 +0000395 Node->getMemberDecl()->getNameAsString().c_str(),
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000396 (void*)Node->getMemberDecl());
Chris Lattner6000dac2007-08-08 22:51:59 +0000397}
Nate Begeman213541a2008-04-18 23:10:10 +0000398void StmtDumper::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000399 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000400 fprintf(F, " %s", Node->getAccessor().getName());
Chris Lattner6000dac2007-08-08 22:51:59 +0000401}
Chris Lattner6000dac2007-08-08 22:51:59 +0000402void StmtDumper::VisitBinaryOperator(BinaryOperator *Node) {
403 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000404 fprintf(F, " '%s'", BinaryOperator::getOpcodeStr(Node->getOpcode()));
Chris Lattnereb14fe82007-08-25 02:00:02 +0000405}
406void StmtDumper::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
407 DumpExpr(Node);
Eli Friedmanab3a8522009-03-28 01:22:36 +0000408 fprintf(F, " '%s' ComputeLHSTy=",
Chris Lattnereb14fe82007-08-25 02:00:02 +0000409 BinaryOperator::getOpcodeStr(Node->getOpcode()));
Eli Friedmanab3a8522009-03-28 01:22:36 +0000410 DumpType(Node->getComputationLHSType());
411 fprintf(F, " ComputeResultTy=");
412 DumpType(Node->getComputationResultType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000413}
Chris Lattner6000dac2007-08-08 22:51:59 +0000414
415// GNU extensions.
416
417void StmtDumper::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000418 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000419 fprintf(F, " %s %p", Node->getLabel()->getName(), (void*)Node->getLabel());
Chris Lattner6000dac2007-08-08 22:51:59 +0000420}
421
Chris Lattner6000dac2007-08-08 22:51:59 +0000422void StmtDumper::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000423 DumpExpr(Node);
424 fprintf(F, " ");
425 DumpType(Node->getArgType1());
426 fprintf(F, " ");
427 DumpType(Node->getArgType2());
Chris Lattner6000dac2007-08-08 22:51:59 +0000428}
429
Chris Lattnerf9e05812007-08-09 18:03:18 +0000430//===----------------------------------------------------------------------===//
431// C++ Expressions
432//===----------------------------------------------------------------------===//
Chris Lattner6000dac2007-08-08 22:51:59 +0000433
Douglas Gregor49badde2008-10-27 19:41:14 +0000434void StmtDumper::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000435 DumpExpr(Node);
Douglas Gregor49badde2008-10-27 19:41:14 +0000436 fprintf(F, " %s<%s>", Node->getCastName(),
437 Node->getTypeAsWritten().getAsString().c_str());
Chris Lattner6000dac2007-08-08 22:51:59 +0000438}
439
440void StmtDumper::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000441 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000442 fprintf(F, " %s", Node->getValue() ? "true" : "false");
Chris Lattner6000dac2007-08-08 22:51:59 +0000443}
444
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000445void StmtDumper::VisitCXXThisExpr(CXXThisExpr *Node) {
446 DumpExpr(Node);
447 fprintf(F, " this");
448}
449
Douglas Gregor49badde2008-10-27 19:41:14 +0000450void StmtDumper::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
451 DumpExpr(Node);
452 fprintf(F, " functional cast to %s",
453 Node->getTypeAsWritten().getAsString().c_str());
454}
455
Anders Carlsson55085182007-08-21 17:43:55 +0000456//===----------------------------------------------------------------------===//
457// Obj-C Expressions
458//===----------------------------------------------------------------------===//
459
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000460void StmtDumper::VisitObjCMessageExpr(ObjCMessageExpr* Node) {
461 DumpExpr(Node);
Chris Lattner077bf5e2008-11-24 03:33:13 +0000462 fprintf(F, " selector=%s", Node->getSelector().getAsString().c_str());
Ted Kremenekea958e572008-05-01 17:26:20 +0000463 IdentifierInfo* clsName = Node->getClassName();
464 if (clsName) fprintf(F, " class=%s", clsName->getName());
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000465}
466
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000467void StmtDumper::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
468 DumpExpr(Node);
469
470 fprintf(F, " ");
471 DumpType(Node->getEncodedType());
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000472}
473
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000474void StmtDumper::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
475 DumpExpr(Node);
476
477 fprintf(F, " ");
Chris Lattner077bf5e2008-11-24 03:33:13 +0000478 fprintf(F, "%s", Node->getSelector().getAsString().c_str());
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000479}
480
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000481void StmtDumper::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
482 DumpExpr(Node);
483
484 fprintf(F, " ");
Chris Lattner8ec03f52008-11-24 03:54:41 +0000485 fprintf(F, "%s", Node->getProtocol()->getNameAsString().c_str());
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000486}
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000487
488void StmtDumper::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
489 DumpExpr(Node);
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000490
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000491 fprintf(F, " Kind=PropertyRef Property=\"%s\"",
Chris Lattner8ec03f52008-11-24 03:54:41 +0000492 Node->getProperty()->getNameAsString().c_str());
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000493}
494
495void StmtDumper::VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node) {
496 DumpExpr(Node);
497
498 ObjCMethodDecl *Getter = Node->getGetterMethod();
499 ObjCMethodDecl *Setter = Node->getSetterMethod();
500 fprintf(F, " Kind=MethodRef Getter=\"%s\" Setter=\"%s\"",
Chris Lattner077bf5e2008-11-24 03:33:13 +0000501 Getter->getSelector().getAsString().c_str(),
502 Setter ? Setter->getSelector().getAsString().c_str() : "(null)");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000503}
504
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000505void StmtDumper::VisitObjCSuperExpr(ObjCSuperExpr *Node) {
506 DumpExpr(Node);
507 fprintf(F, " super");
508}
509
Chris Lattner6000dac2007-08-08 22:51:59 +0000510//===----------------------------------------------------------------------===//
511// Stmt method implementations
512//===----------------------------------------------------------------------===//
513
514/// dump - This does a local dump of the specified AST fragment. It dumps the
515/// specified node and a few nodes underneath it, but not the whole subtree.
516/// This is useful in a debugger.
Chris Lattnere300c872007-08-30 06:17:34 +0000517void Stmt::dump(SourceManager &SM) const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000518 StmtDumper P(&SM, stderr, 4);
Chris Lattnerb3938792007-08-30 00:53:54 +0000519 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattner0c727a32007-08-30 00:40:08 +0000520 fprintf(stderr, "\n");
521}
522
523/// dump - This does a local dump of the specified AST fragment. It dumps the
524/// specified node and a few nodes underneath it, but not the whole subtree.
525/// This is useful in a debugger.
Chris Lattner6000dac2007-08-08 22:51:59 +0000526void Stmt::dump() const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000527 StmtDumper P(0, stderr, 4);
Chris Lattnerb3938792007-08-30 00:53:54 +0000528 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattner0c727a32007-08-30 00:40:08 +0000529 fprintf(stderr, "\n");
530}
531
532/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
Chris Lattnere300c872007-08-30 06:17:34 +0000533void Stmt::dumpAll(SourceManager &SM) const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000534 StmtDumper P(&SM, stderr, ~0U);
Chris Lattnerb3938792007-08-30 00:53:54 +0000535 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattneree41ce52007-08-10 21:51:12 +0000536 fprintf(stderr, "\n");
Chris Lattner6000dac2007-08-08 22:51:59 +0000537}
538
539/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
540void Stmt::dumpAll() const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000541 StmtDumper P(0, stderr, ~0U);
Chris Lattnerb3938792007-08-30 00:53:54 +0000542 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattneree41ce52007-08-10 21:51:12 +0000543 fprintf(stderr, "\n");
Chris Lattner6000dac2007-08-08 22:51:59 +0000544}