blob: 1ee3efab0a2c245a6e3221cd1b053060e10c5692 [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
Douglas Gregor61366e92008-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 Lattnerbad37852008-04-02 05:06:23 +000094 }
Chris Lattnerfd8f7da2007-08-09 00:36:22 +000095 }
Steve Naroff9dcbfa42007-09-01 21:08:38 +000096 void DumpStmt(const Stmt *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +000097 Indent();
98 fprintf(F, "(%s %p", Node->getStmtClassName(), (void*)Node);
Steve Naroff9dcbfa42007-09-01 21:08:38 +000099 DumpSourceRange(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000100 }
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000101 void DumpExpr(const Expr *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000102 DumpStmt(Node);
Chris Lattnerfd8f7da2007-08-09 00:36:22 +0000103 fprintf(F, " ");
104 DumpType(Node->getType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000105 }
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000106 void DumpSourceRange(const Stmt *Node);
Chris Lattnere300c872007-08-30 06:17:34 +0000107 void DumpLocation(SourceLocation Loc);
Chris Lattnere300c872007-08-30 06:17:34 +0000108
Chris Lattner17a1a722007-08-30 01:00:35 +0000109 // Stmts.
Chris Lattnerc5598cb2007-08-21 04:04:25 +0000110 void VisitStmt(Stmt *Node);
Ted Kremenek5399ce22007-12-12 06:59:42 +0000111 void VisitDeclStmt(DeclStmt *Node);
Chris Lattner17a1a722007-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 Lattnerd9f69102008-08-10 01:53:14 +0000118 void VisitPredefinedExpr(PredefinedExpr *Node);
Chris Lattner17a1a722007-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 Redl05189992008-11-11 17:56:53 +0000124 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000125 void VisitMemberExpr(MemberExpr *Node);
Nate Begeman213541a2008-04-18 23:10:10 +0000126 void VisitExtVectorElementExpr(ExtVectorElementExpr *Node);
Chris Lattner17a1a722007-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 Gregor49badde2008-10-27 19:41:14 +0000133 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Chris Lattner17a1a722007-08-30 01:00:35 +0000134 void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node);
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000135 void VisitCXXThisExpr(CXXThisExpr *Node);
Douglas Gregor49badde2008-10-27 19:41:14 +0000136 void VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node);
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000137
Chris Lattner17a1a722007-08-30 01:00:35 +0000138 // ObjC
139 void VisitObjCEncodeExpr(ObjCEncodeExpr *Node);
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000140 void VisitObjCMessageExpr(ObjCMessageExpr* Node);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000141 void VisitObjCSelectorExpr(ObjCSelectorExpr *Node);
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000142 void VisitObjCProtocolExpr(ObjCProtocolExpr *Node);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000143 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node);
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000144 void VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node);
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000145 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node);
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000146 void VisitObjCSuperExpr(ObjCSuperExpr *Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000147 };
148}
149
150//===----------------------------------------------------------------------===//
Chris Lattnere300c872007-08-30 06:17:34 +0000151// Utilities
152//===----------------------------------------------------------------------===//
153
154void StmtDumper::DumpLocation(SourceLocation Loc) {
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000155 SourceLocation SpellingLoc = SM->getSpellingLoc(Loc);
Chris Lattnere300c872007-08-30 06:17:34 +0000156
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.
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000159 const char *Filename = SM->getSourceName(SpellingLoc);
160 unsigned LineNo = SM->getLineNumber(SpellingLoc);
161 unsigned ColNo = SM->getColumnNumber(SpellingLoc);
Chris Lattnere300c872007-08-30 06:17:34 +0000162 if (strcmp(Filename, LastLocFilename) != 0) {
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000163 fprintf(stderr, "%s:%u:%u", Filename, LineNo, ColNo);
Chris Lattnere300c872007-08-30 06:17:34 +0000164 LastLocFilename = Filename;
165 LastLocLine = LineNo;
166 } else if (LineNo != LastLocLine) {
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000167 fprintf(stderr, "line:%u:%u", LineNo, ColNo);
Chris Lattnere300c872007-08-30 06:17:34 +0000168 LastLocLine = LineNo;
169 } else {
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000170 fprintf(stderr, "col:%u", ColNo);
Chris Lattnere300c872007-08-30 06:17:34 +0000171 }
172}
173
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000174void StmtDumper::DumpSourceRange(const Stmt *Node) {
Chris Lattnere300c872007-08-30 06:17:34 +0000175 // Can't translate locations if a SourceManager isn't available.
176 if (SM == 0) return;
177
178 // TODO: If the parent expression is available, we can print a delta vs its
179 // location.
180 SourceRange R = Node->getSourceRange();
181
182 fprintf(stderr, " <");
Chris Lattner311ff022007-10-16 22:36:42 +0000183 DumpLocation(R.getBegin());
184 if (R.getBegin() != R.getEnd()) {
Chris Lattnere300c872007-08-30 06:17:34 +0000185 fprintf(stderr, ", ");
Chris Lattner311ff022007-10-16 22:36:42 +0000186 DumpLocation(R.getEnd());
Chris Lattnere300c872007-08-30 06:17:34 +0000187 }
188 fprintf(stderr, ">");
189
190 // <t2.c:123:421[blah], t2.c:412:321>
191
192}
193
194
195//===----------------------------------------------------------------------===//
Chris Lattner6000dac2007-08-08 22:51:59 +0000196// Stmt printing methods.
197//===----------------------------------------------------------------------===//
198
199void StmtDumper::VisitStmt(Stmt *Node) {
Chris Lattner17a1a722007-08-30 01:00:35 +0000200 DumpStmt(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000201}
202
Chris Lattnerf9e05812007-08-09 18:03:18 +0000203void StmtDumper::DumpDeclarator(Decl *D) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000204 // FIXME: Need to complete/beautify this... this code simply shows the
205 // nodes are where they need to be.
206 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000207 fprintf(F, "\"typedef %s %s\"",
208 localType->getUnderlyingType().getAsString().c_str(),
Chris Lattner8ec03f52008-11-24 03:54:41 +0000209 localType->getNameAsString().c_str());
Chris Lattner6000dac2007-08-08 22:51:59 +0000210 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000211 fprintf(F, "\"");
Chris Lattner6000dac2007-08-08 22:51:59 +0000212 // Emit storage class for vardecls.
213 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
214 switch (V->getStorageClass()) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000215 default: assert(0 && "Unknown storage class!");
216 case VarDecl::None: break;
217 case VarDecl::Extern: fprintf(F, "extern "); break;
218 case VarDecl::Static: fprintf(F, "static "); break;
219 case VarDecl::Auto: fprintf(F, "auto "); break;
220 case VarDecl::Register: fprintf(F, "register "); break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000221 }
222 }
223
Chris Lattner39f34e92008-11-24 04:00:27 +0000224 std::string Name = VD->getNameAsString();
Chris Lattner6000dac2007-08-08 22:51:59 +0000225 VD->getType().getAsStringInternal(Name);
Chris Lattnerf9e05812007-08-09 18:03:18 +0000226 fprintf(F, "%s", Name.c_str());
Chris Lattner6000dac2007-08-08 22:51:59 +0000227
228 // If this is a vardecl with an initializer, emit it.
229 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
230 if (V->getInit()) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000231 fprintf(F, " =\n");
232 DumpSubTree(V->getInit());
Chris Lattner6000dac2007-08-08 22:51:59 +0000233 }
234 }
Chris Lattnerf9e05812007-08-09 18:03:18 +0000235 fprintf(F, "\"");
Steve Naroff92199282007-11-17 21:37:36 +0000236 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
237 // print a free standing tag decl (e.g. "struct x;").
238 const char *tagname;
239 if (const IdentifierInfo *II = TD->getIdentifier())
240 tagname = II->getName();
241 else
242 tagname = "<anonymous>";
243 fprintf(F, "\"%s %s;\"", TD->getKindName(), tagname);
244 // FIXME: print tag bodies.
Chris Lattner6000dac2007-08-08 22:51:59 +0000245 } else {
Chris Lattner6000dac2007-08-08 22:51:59 +0000246 assert(0 && "Unexpected decl");
247 }
Chris Lattner6000dac2007-08-08 22:51:59 +0000248}
249
Ted Kremenek5399ce22007-12-12 06:59:42 +0000250void StmtDumper::VisitDeclStmt(DeclStmt *Node) {
251 DumpStmt(Node);
252 fprintf(F,"\n");
Ted Kremenek04a72b72008-10-06 18:38:35 +0000253 for (DeclStmt::decl_iterator DI = Node->decl_begin(), DE = Node->decl_end();
254 DI != DE; ++DI) {
255 ScopedDecl* D = *DI;
Ted Kremenek5399ce22007-12-12 06:59:42 +0000256 ++IndentLevel;
257 Indent();
258 fprintf(F, "%p ", (void*) D);
259 DumpDeclarator(D);
260 if (D->getNextDeclarator())
261 fprintf(F,"\n");
262 --IndentLevel;
263 }
264}
265
Chris Lattner6000dac2007-08-08 22:51:59 +0000266void StmtDumper::VisitLabelStmt(LabelStmt *Node) {
267 DumpStmt(Node);
Chris Lattner3cb640b2008-07-26 19:24:43 +0000268 fprintf(F, " '%s'", Node->getName());
Chris Lattner6000dac2007-08-08 22:51:59 +0000269}
270
Chris Lattner6000dac2007-08-08 22:51:59 +0000271void StmtDumper::VisitGotoStmt(GotoStmt *Node) {
272 DumpStmt(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000273 fprintf(F, " '%s':%p", Node->getLabel()->getName(), (void*)Node->getLabel());
Chris Lattner6000dac2007-08-08 22:51:59 +0000274}
275
Chris Lattner6000dac2007-08-08 22:51:59 +0000276//===----------------------------------------------------------------------===//
277// Expr printing methods.
278//===----------------------------------------------------------------------===//
279
280void StmtDumper::VisitExpr(Expr *Node) {
281 DumpExpr(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000282}
283
284void StmtDumper::VisitDeclRefExpr(DeclRefExpr *Node) {
285 DumpExpr(Node);
Ted Kremenekeb641f92007-09-10 17:32:55 +0000286
287 fprintf(F, " ");
288 switch (Node->getDecl()->getKind()) {
289 case Decl::Function: fprintf(F,"FunctionDecl"); break;
Steve Naroff248a7532008-04-15 22:42:06 +0000290 case Decl::Var: fprintf(F,"Var"); break;
Chris Lattneraa9fc462007-10-08 21:37:32 +0000291 case Decl::ParmVar: fprintf(F,"ParmVar"); break;
Ted Kremenekeb641f92007-09-10 17:32:55 +0000292 case Decl::EnumConstant: fprintf(F,"EnumConstant"); break;
293 case Decl::Typedef: fprintf(F,"Typedef"); break;
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000294 case Decl::Record: fprintf(F,"Record"); break;
Ted Kremenekeb641f92007-09-10 17:32:55 +0000295 case Decl::Enum: fprintf(F,"Enum"); break;
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000296 case Decl::CXXRecord: fprintf(F,"CXXRecord"); break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000297 case Decl::ObjCInterface: fprintf(F,"ObjCInterface"); break;
298 case Decl::ObjCClass: fprintf(F,"ObjCClass"); break;
Ted Kremenekeb641f92007-09-10 17:32:55 +0000299 default: fprintf(F,"Decl"); break;
300 }
301
Chris Lattner39f34e92008-11-24 04:00:27 +0000302 fprintf(F, "='%s' %p", Node->getDecl()->getNameAsString().c_str(),
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000303 (void*)Node->getDecl());
Chris Lattner6000dac2007-08-08 22:51:59 +0000304}
305
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000306void StmtDumper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Steve Naroff466c2e32008-05-23 00:59:14 +0000307 DumpExpr(Node);
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000308
Steve Naroff466c2e32008-05-23 00:59:14 +0000309 fprintf(F, " %sDecl='%s' %p", Node->getDecl()->getDeclKindName(),
Chris Lattner8ec03f52008-11-24 03:54:41 +0000310 Node->getDecl()->getNameAsString().c_str(), (void*)Node->getDecl());
Steve Naroff218543b2008-05-23 22:01:24 +0000311 if (Node->isFreeIvar())
312 fprintf(F, " isFreeIvar");
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000313}
314
Chris Lattnerd9f69102008-08-10 01:53:14 +0000315void StmtDumper::VisitPredefinedExpr(PredefinedExpr *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000316 DumpExpr(Node);
317 switch (Node->getIdentType()) {
Chris Lattner0d17f6f2008-06-21 18:04:54 +0000318 default: assert(0 && "unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000319 case PredefinedExpr::Func: fprintf(F, " __func__"); break;
320 case PredefinedExpr::Function: fprintf(F, " __FUNCTION__"); break;
321 case PredefinedExpr::PrettyFunction: fprintf(F, " __PRETTY_FUNCTION__");break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000322 }
323}
324
325void StmtDumper::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnera0df31a2007-08-09 01:04:32 +0000326 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000327 fprintf(F, " %d", Node->getValue());
Chris Lattner6000dac2007-08-08 22:51:59 +0000328}
329
330void StmtDumper::VisitIntegerLiteral(IntegerLiteral *Node) {
331 DumpExpr(Node);
332
333 bool isSigned = Node->getType()->isSignedIntegerType();
Chris Lattnerb3938792007-08-30 00:53:54 +0000334 fprintf(F, " %s", Node->getValue().toString(10, isSigned).c_str());
Chris Lattner6000dac2007-08-08 22:51:59 +0000335}
336void StmtDumper::VisitFloatingLiteral(FloatingLiteral *Node) {
337 DumpExpr(Node);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000338 fprintf(F, " %f", Node->getValueAsApproximateDouble());
Chris Lattner6000dac2007-08-08 22:51:59 +0000339}
Chris Lattner5d661452007-08-26 03:42:43 +0000340
Chris Lattner6000dac2007-08-08 22:51:59 +0000341void StmtDumper::VisitStringLiteral(StringLiteral *Str) {
Chris Lattnera0df31a2007-08-09 01:04:32 +0000342 DumpExpr(Str);
343 // FIXME: this doesn't print wstrings right.
Chris Lattner5fc61072007-08-09 17:14:24 +0000344 fprintf(F, " %s\"", Str->isWide() ? "L" : "");
Chris Lattnera0df31a2007-08-09 01:04:32 +0000345
Chris Lattner6000dac2007-08-08 22:51:59 +0000346 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattner5fc61072007-08-09 17:14:24 +0000347 switch (char C = Str->getStrData()[i]) {
348 default:
349 if (isprint(C))
350 fputc(C, F);
351 else
352 fprintf(F, "\\%03o", C);
353 break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000354 // Handle some common ones to make dumps prettier.
Chris Lattner5fc61072007-08-09 17:14:24 +0000355 case '\\': fprintf(F, "\\\\"); break;
356 case '"': fprintf(F, "\\\""); break;
357 case '\n': fprintf(F, "\\n"); break;
358 case '\t': fprintf(F, "\\t"); break;
359 case '\a': fprintf(F, "\\a"); break;
360 case '\b': fprintf(F, "\\b"); break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000361 }
362 }
Chris Lattnerb3938792007-08-30 00:53:54 +0000363 fprintf(F, "\"");
Chris Lattner6000dac2007-08-08 22:51:59 +0000364}
Chris Lattner17a1a722007-08-30 01:00:35 +0000365
Chris Lattner6000dac2007-08-08 22:51:59 +0000366void StmtDumper::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000367 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000368 fprintf(F, " %s '%s'", Node->isPostfix() ? "postfix" : "prefix",
Chris Lattner13cb21f2007-08-09 17:35:30 +0000369 UnaryOperator::getOpcodeStr(Node->getOpcode()));
Chris Lattner6000dac2007-08-08 22:51:59 +0000370}
Sebastian Redl05189992008-11-11 17:56:53 +0000371void StmtDumper::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000372 DumpExpr(Node);
373 fprintf(F, " %s ", Node->isSizeOf() ? "sizeof" : "alignof");
Sebastian Redl05189992008-11-11 17:56:53 +0000374 if (Node->isArgumentType())
375 DumpType(Node->getArgumentType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000376}
Chris Lattner13cb21f2007-08-09 17:35:30 +0000377
Chris Lattner6000dac2007-08-08 22:51:59 +0000378void StmtDumper::VisitMemberExpr(MemberExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000379 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000380 fprintf(F, " %s%s %p", Node->isArrow() ? "->" : ".",
Chris Lattner077bf5e2008-11-24 03:33:13 +0000381 Node->getMemberDecl()->getNameAsString().c_str(),
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000382 (void*)Node->getMemberDecl());
Chris Lattner6000dac2007-08-08 22:51:59 +0000383}
Nate Begeman213541a2008-04-18 23:10:10 +0000384void StmtDumper::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000385 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000386 fprintf(F, " %s", Node->getAccessor().getName());
Chris Lattner6000dac2007-08-08 22:51:59 +0000387}
Chris Lattner6000dac2007-08-08 22:51:59 +0000388void StmtDumper::VisitBinaryOperator(BinaryOperator *Node) {
389 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000390 fprintf(F, " '%s'", BinaryOperator::getOpcodeStr(Node->getOpcode()));
Chris Lattnereb14fe82007-08-25 02:00:02 +0000391}
392void StmtDumper::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
393 DumpExpr(Node);
394 fprintf(F, " '%s' ComputeTy=",
395 BinaryOperator::getOpcodeStr(Node->getOpcode()));
396 DumpType(Node->getComputationType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000397}
Chris Lattner6000dac2007-08-08 22:51:59 +0000398
399// GNU extensions.
400
401void StmtDumper::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000402 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000403 fprintf(F, " %s %p", Node->getLabel()->getName(), (void*)Node->getLabel());
Chris Lattner6000dac2007-08-08 22:51:59 +0000404}
405
Chris Lattner6000dac2007-08-08 22:51:59 +0000406void StmtDumper::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000407 DumpExpr(Node);
408 fprintf(F, " ");
409 DumpType(Node->getArgType1());
410 fprintf(F, " ");
411 DumpType(Node->getArgType2());
Chris Lattner6000dac2007-08-08 22:51:59 +0000412}
413
Chris Lattnerf9e05812007-08-09 18:03:18 +0000414//===----------------------------------------------------------------------===//
415// C++ Expressions
416//===----------------------------------------------------------------------===//
Chris Lattner6000dac2007-08-08 22:51:59 +0000417
Douglas Gregor49badde2008-10-27 19:41:14 +0000418void StmtDumper::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000419 DumpExpr(Node);
Douglas Gregor49badde2008-10-27 19:41:14 +0000420 fprintf(F, " %s<%s>", Node->getCastName(),
421 Node->getTypeAsWritten().getAsString().c_str());
Chris Lattner6000dac2007-08-08 22:51:59 +0000422}
423
424void StmtDumper::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000425 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000426 fprintf(F, " %s", Node->getValue() ? "true" : "false");
Chris Lattner6000dac2007-08-08 22:51:59 +0000427}
428
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000429void StmtDumper::VisitCXXThisExpr(CXXThisExpr *Node) {
430 DumpExpr(Node);
431 fprintf(F, " this");
432}
433
Douglas Gregor49badde2008-10-27 19:41:14 +0000434void StmtDumper::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
435 DumpExpr(Node);
436 fprintf(F, " functional cast to %s",
437 Node->getTypeAsWritten().getAsString().c_str());
438}
439
Anders Carlsson55085182007-08-21 17:43:55 +0000440//===----------------------------------------------------------------------===//
441// Obj-C Expressions
442//===----------------------------------------------------------------------===//
443
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000444void StmtDumper::VisitObjCMessageExpr(ObjCMessageExpr* Node) {
445 DumpExpr(Node);
Chris Lattner077bf5e2008-11-24 03:33:13 +0000446 fprintf(F, " selector=%s", Node->getSelector().getAsString().c_str());
Ted Kremenekea958e572008-05-01 17:26:20 +0000447 IdentifierInfo* clsName = Node->getClassName();
448 if (clsName) fprintf(F, " class=%s", clsName->getName());
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000449}
450
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000451void StmtDumper::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
452 DumpExpr(Node);
453
454 fprintf(F, " ");
455 DumpType(Node->getEncodedType());
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000456}
457
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000458void StmtDumper::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
459 DumpExpr(Node);
460
461 fprintf(F, " ");
Chris Lattner077bf5e2008-11-24 03:33:13 +0000462 fprintf(F, "%s", Node->getSelector().getAsString().c_str());
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000463}
464
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000465void StmtDumper::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
466 DumpExpr(Node);
467
468 fprintf(F, " ");
Chris Lattner8ec03f52008-11-24 03:54:41 +0000469 fprintf(F, "%s", Node->getProtocol()->getNameAsString().c_str());
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000470}
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000471
472void StmtDumper::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
473 DumpExpr(Node);
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000474
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000475 fprintf(F, " Kind=PropertyRef Property=\"%s\"",
Chris Lattner8ec03f52008-11-24 03:54:41 +0000476 Node->getProperty()->getNameAsString().c_str());
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000477}
478
479void StmtDumper::VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node) {
480 DumpExpr(Node);
481
482 ObjCMethodDecl *Getter = Node->getGetterMethod();
483 ObjCMethodDecl *Setter = Node->getSetterMethod();
484 fprintf(F, " Kind=MethodRef Getter=\"%s\" Setter=\"%s\"",
Chris Lattner077bf5e2008-11-24 03:33:13 +0000485 Getter->getSelector().getAsString().c_str(),
486 Setter ? Setter->getSelector().getAsString().c_str() : "(null)");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000487}
488
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000489void StmtDumper::VisitObjCSuperExpr(ObjCSuperExpr *Node) {
490 DumpExpr(Node);
491 fprintf(F, " super");
492}
493
Chris Lattner6000dac2007-08-08 22:51:59 +0000494//===----------------------------------------------------------------------===//
495// Stmt method implementations
496//===----------------------------------------------------------------------===//
497
498/// dump - This does a local dump of the specified AST fragment. It dumps the
499/// specified node and a few nodes underneath it, but not the whole subtree.
500/// This is useful in a debugger.
Chris Lattnere300c872007-08-30 06:17:34 +0000501void Stmt::dump(SourceManager &SM) const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000502 StmtDumper P(&SM, stderr, 4);
Chris Lattnerb3938792007-08-30 00:53:54 +0000503 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattner0c727a32007-08-30 00:40:08 +0000504 fprintf(stderr, "\n");
505}
506
507/// dump - This does a local dump of the specified AST fragment. It dumps the
508/// specified node and a few nodes underneath it, but not the whole subtree.
509/// This is useful in a debugger.
Chris Lattner6000dac2007-08-08 22:51:59 +0000510void Stmt::dump() const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000511 StmtDumper P(0, stderr, 4);
Chris Lattnerb3938792007-08-30 00:53:54 +0000512 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattner0c727a32007-08-30 00:40:08 +0000513 fprintf(stderr, "\n");
514}
515
516/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
Chris Lattnere300c872007-08-30 06:17:34 +0000517void Stmt::dumpAll(SourceManager &SM) const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000518 StmtDumper P(&SM, stderr, ~0U);
Chris Lattnerb3938792007-08-30 00:53:54 +0000519 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattneree41ce52007-08-10 21:51:12 +0000520 fprintf(stderr, "\n");
Chris Lattner6000dac2007-08-08 22:51:59 +0000521}
522
523/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
524void Stmt::dumpAll() const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000525 StmtDumper P(0, stderr, ~0U);
Chris Lattnerb3938792007-08-30 00:53:54 +0000526 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattneree41ce52007-08-10 21:51:12 +0000527 fprintf(stderr, "\n");
Chris Lattner6000dac2007-08-08 22:51:59 +0000528}