blob: 0ccf20175422dc3475548cf3ad83ba6416fd632e [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);
122 void VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node);
123 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++
131 void VisitCXXCastExpr(CXXCastExpr *Node);
132 void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node);
133
134 // ObjC
135 void VisitObjCEncodeExpr(ObjCEncodeExpr *Node);
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000136 void VisitObjCMessageExpr(ObjCMessageExpr* Node);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000137 void VisitObjCSelectorExpr(ObjCSelectorExpr *Node);
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000138 void VisitObjCProtocolExpr(ObjCProtocolExpr *Node);
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000139 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000140 };
141}
142
143//===----------------------------------------------------------------------===//
Chris Lattnere300c872007-08-30 06:17:34 +0000144// Utilities
145//===----------------------------------------------------------------------===//
146
147void StmtDumper::DumpLocation(SourceLocation Loc) {
148 SourceLocation PhysLoc = SM->getPhysicalLoc(Loc);
149
150 // The general format we print out is filename:line:col, but we drop pieces
151 // that haven't changed since the last loc printed.
152 const char *Filename = SM->getSourceName(PhysLoc);
153 unsigned LineNo = SM->getLineNumber(PhysLoc);
154 if (strcmp(Filename, LastLocFilename) != 0) {
155 fprintf(stderr, "%s:%u:%u", Filename, LineNo, SM->getColumnNumber(PhysLoc));
156 LastLocFilename = Filename;
157 LastLocLine = LineNo;
158 } else if (LineNo != LastLocLine) {
159 fprintf(stderr, "line:%u:%u", LineNo, SM->getColumnNumber(PhysLoc));
160 LastLocLine = LineNo;
161 } else {
162 fprintf(stderr, "col:%u", SM->getColumnNumber(PhysLoc));
163 }
164}
165
Steve Naroff9dcbfa42007-09-01 21:08:38 +0000166void StmtDumper::DumpSourceRange(const Stmt *Node) {
Chris Lattnere300c872007-08-30 06:17:34 +0000167 // Can't translate locations if a SourceManager isn't available.
168 if (SM == 0) return;
169
170 // TODO: If the parent expression is available, we can print a delta vs its
171 // location.
172 SourceRange R = Node->getSourceRange();
173
174 fprintf(stderr, " <");
Chris Lattner311ff022007-10-16 22:36:42 +0000175 DumpLocation(R.getBegin());
176 if (R.getBegin() != R.getEnd()) {
Chris Lattnere300c872007-08-30 06:17:34 +0000177 fprintf(stderr, ", ");
Chris Lattner311ff022007-10-16 22:36:42 +0000178 DumpLocation(R.getEnd());
Chris Lattnere300c872007-08-30 06:17:34 +0000179 }
180 fprintf(stderr, ">");
181
182 // <t2.c:123:421[blah], t2.c:412:321>
183
184}
185
186
187//===----------------------------------------------------------------------===//
Chris Lattner6000dac2007-08-08 22:51:59 +0000188// Stmt printing methods.
189//===----------------------------------------------------------------------===//
190
191void StmtDumper::VisitStmt(Stmt *Node) {
Chris Lattner17a1a722007-08-30 01:00:35 +0000192 DumpStmt(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000193}
194
Chris Lattnerf9e05812007-08-09 18:03:18 +0000195void StmtDumper::DumpDeclarator(Decl *D) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000196 // FIXME: Need to complete/beautify this... this code simply shows the
197 // nodes are where they need to be.
198 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000199 fprintf(F, "\"typedef %s %s\"",
200 localType->getUnderlyingType().getAsString().c_str(),
201 localType->getName());
Chris Lattner6000dac2007-08-08 22:51:59 +0000202 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000203 fprintf(F, "\"");
Chris Lattner6000dac2007-08-08 22:51:59 +0000204 // Emit storage class for vardecls.
205 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
206 switch (V->getStorageClass()) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000207 default: assert(0 && "Unknown storage class!");
208 case VarDecl::None: break;
209 case VarDecl::Extern: fprintf(F, "extern "); break;
210 case VarDecl::Static: fprintf(F, "static "); break;
211 case VarDecl::Auto: fprintf(F, "auto "); break;
212 case VarDecl::Register: fprintf(F, "register "); break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000213 }
214 }
215
216 std::string Name = VD->getName();
217 VD->getType().getAsStringInternal(Name);
Chris Lattnerf9e05812007-08-09 18:03:18 +0000218 fprintf(F, "%s", Name.c_str());
Chris Lattner6000dac2007-08-08 22:51:59 +0000219
220 // If this is a vardecl with an initializer, emit it.
221 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
222 if (V->getInit()) {
Chris Lattnerf9e05812007-08-09 18:03:18 +0000223 fprintf(F, " =\n");
224 DumpSubTree(V->getInit());
Chris Lattner6000dac2007-08-08 22:51:59 +0000225 }
226 }
Chris Lattnerf9e05812007-08-09 18:03:18 +0000227 fprintf(F, "\"");
Steve Naroff92199282007-11-17 21:37:36 +0000228 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
229 // print a free standing tag decl (e.g. "struct x;").
230 const char *tagname;
231 if (const IdentifierInfo *II = TD->getIdentifier())
232 tagname = II->getName();
233 else
234 tagname = "<anonymous>";
235 fprintf(F, "\"%s %s;\"", TD->getKindName(), tagname);
236 // FIXME: print tag bodies.
Chris Lattner6000dac2007-08-08 22:51:59 +0000237 } else {
Chris Lattner6000dac2007-08-08 22:51:59 +0000238 assert(0 && "Unexpected decl");
239 }
Chris Lattner6000dac2007-08-08 22:51:59 +0000240}
241
Ted Kremenek5399ce22007-12-12 06:59:42 +0000242void StmtDumper::VisitDeclStmt(DeclStmt *Node) {
243 DumpStmt(Node);
244 fprintf(F,"\n");
245 for (ScopedDecl *D = Node->getDecl(); D; D = D->getNextDeclarator()) {
246 ++IndentLevel;
247 Indent();
248 fprintf(F, "%p ", (void*) D);
249 DumpDeclarator(D);
250 if (D->getNextDeclarator())
251 fprintf(F,"\n");
252 --IndentLevel;
253 }
254}
255
Chris Lattner6000dac2007-08-08 22:51:59 +0000256void StmtDumper::VisitLabelStmt(LabelStmt *Node) {
257 DumpStmt(Node);
Chris Lattner3cb640b2008-07-26 19:24:43 +0000258 fprintf(F, " '%s'", Node->getName());
Chris Lattner6000dac2007-08-08 22:51:59 +0000259}
260
Chris Lattner6000dac2007-08-08 22:51:59 +0000261void StmtDumper::VisitGotoStmt(GotoStmt *Node) {
262 DumpStmt(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000263 fprintf(F, " '%s':%p", Node->getLabel()->getName(), (void*)Node->getLabel());
Chris Lattner6000dac2007-08-08 22:51:59 +0000264}
265
Chris Lattner6000dac2007-08-08 22:51:59 +0000266//===----------------------------------------------------------------------===//
267// Expr printing methods.
268//===----------------------------------------------------------------------===//
269
270void StmtDumper::VisitExpr(Expr *Node) {
271 DumpExpr(Node);
Chris Lattner6000dac2007-08-08 22:51:59 +0000272}
273
274void StmtDumper::VisitDeclRefExpr(DeclRefExpr *Node) {
275 DumpExpr(Node);
Ted Kremenekeb641f92007-09-10 17:32:55 +0000276
277 fprintf(F, " ");
278 switch (Node->getDecl()->getKind()) {
279 case Decl::Function: fprintf(F,"FunctionDecl"); break;
Steve Naroff248a7532008-04-15 22:42:06 +0000280 case Decl::Var: fprintf(F,"Var"); break;
Chris Lattneraa9fc462007-10-08 21:37:32 +0000281 case Decl::ParmVar: fprintf(F,"ParmVar"); break;
Ted Kremenekeb641f92007-09-10 17:32:55 +0000282 case Decl::EnumConstant: fprintf(F,"EnumConstant"); break;
283 case Decl::Typedef: fprintf(F,"Typedef"); break;
284 case Decl::Struct: fprintf(F,"Struct"); break;
285 case Decl::Union: fprintf(F,"Union"); break;
286 case Decl::Class: fprintf(F,"Class"); break;
287 case Decl::Enum: fprintf(F,"Enum"); break;
Argyrios Kyrtzidis4515ff42008-06-09 23:36:53 +0000288 case Decl::CXXStruct: fprintf(F,"CXXStruct"); break;
289 case Decl::CXXUnion: fprintf(F,"CXXUnion"); break;
290 case Decl::CXXClass: fprintf(F,"CXXClass"); break;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000291 case Decl::ObjCInterface: fprintf(F,"ObjCInterface"); break;
292 case Decl::ObjCClass: fprintf(F,"ObjCClass"); break;
Ted Kremenekeb641f92007-09-10 17:32:55 +0000293 default: fprintf(F,"Decl"); break;
294 }
295
296 fprintf(F, "='%s' %p", Node->getDecl()->getName(), (void*)Node->getDecl());
Chris Lattner6000dac2007-08-08 22:51:59 +0000297}
298
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000299void StmtDumper::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Steve Naroff466c2e32008-05-23 00:59:14 +0000300 DumpExpr(Node);
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000301
Steve Naroff466c2e32008-05-23 00:59:14 +0000302 fprintf(F, " %sDecl='%s' %p", Node->getDecl()->getDeclKindName(),
303 Node->getDecl()->getName(), (void*)Node->getDecl());
Steve Naroff218543b2008-05-23 22:01:24 +0000304 if (Node->isFreeIvar())
305 fprintf(F, " isFreeIvar");
Steve Naroff3c64d9e2008-03-12 13:19:12 +0000306}
307
Chris Lattnerd9f69102008-08-10 01:53:14 +0000308void StmtDumper::VisitPredefinedExpr(PredefinedExpr *Node) {
Chris Lattner6000dac2007-08-08 22:51:59 +0000309 DumpExpr(Node);
310 switch (Node->getIdentType()) {
Chris Lattner0d17f6f2008-06-21 18:04:54 +0000311 default: assert(0 && "unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000312 case PredefinedExpr::Func: fprintf(F, " __func__"); break;
313 case PredefinedExpr::Function: fprintf(F, " __FUNCTION__"); break;
314 case PredefinedExpr::PrettyFunction: fprintf(F, " __PRETTY_FUNCTION__");break;
315 case PredefinedExpr::ObjCSuper: fprintf(F, "super"); break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000316 }
317}
318
319void StmtDumper::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnera0df31a2007-08-09 01:04:32 +0000320 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000321 fprintf(F, " %d", Node->getValue());
Chris Lattner6000dac2007-08-08 22:51:59 +0000322}
323
324void StmtDumper::VisitIntegerLiteral(IntegerLiteral *Node) {
325 DumpExpr(Node);
326
327 bool isSigned = Node->getType()->isSignedIntegerType();
Chris Lattnerb3938792007-08-30 00:53:54 +0000328 fprintf(F, " %s", Node->getValue().toString(10, isSigned).c_str());
Chris Lattner6000dac2007-08-08 22:51:59 +0000329}
330void StmtDumper::VisitFloatingLiteral(FloatingLiteral *Node) {
331 DumpExpr(Node);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000332 fprintf(F, " %f", Node->getValueAsApproximateDouble());
Chris Lattner6000dac2007-08-08 22:51:59 +0000333}
Chris Lattner5d661452007-08-26 03:42:43 +0000334
Chris Lattner6000dac2007-08-08 22:51:59 +0000335void StmtDumper::VisitStringLiteral(StringLiteral *Str) {
Chris Lattnera0df31a2007-08-09 01:04:32 +0000336 DumpExpr(Str);
337 // FIXME: this doesn't print wstrings right.
Chris Lattner5fc61072007-08-09 17:14:24 +0000338 fprintf(F, " %s\"", Str->isWide() ? "L" : "");
Chris Lattnera0df31a2007-08-09 01:04:32 +0000339
Chris Lattner6000dac2007-08-08 22:51:59 +0000340 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattner5fc61072007-08-09 17:14:24 +0000341 switch (char C = Str->getStrData()[i]) {
342 default:
343 if (isprint(C))
344 fputc(C, F);
345 else
346 fprintf(F, "\\%03o", C);
347 break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000348 // Handle some common ones to make dumps prettier.
Chris Lattner5fc61072007-08-09 17:14:24 +0000349 case '\\': fprintf(F, "\\\\"); break;
350 case '"': fprintf(F, "\\\""); break;
351 case '\n': fprintf(F, "\\n"); break;
352 case '\t': fprintf(F, "\\t"); break;
353 case '\a': fprintf(F, "\\a"); break;
354 case '\b': fprintf(F, "\\b"); break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000355 }
356 }
Chris Lattnerb3938792007-08-30 00:53:54 +0000357 fprintf(F, "\"");
Chris Lattner6000dac2007-08-08 22:51:59 +0000358}
Chris Lattner17a1a722007-08-30 01:00:35 +0000359
Chris Lattner6000dac2007-08-08 22:51:59 +0000360void StmtDumper::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000361 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000362 fprintf(F, " %s '%s'", Node->isPostfix() ? "postfix" : "prefix",
Chris Lattner13cb21f2007-08-09 17:35:30 +0000363 UnaryOperator::getOpcodeStr(Node->getOpcode()));
Chris Lattner6000dac2007-08-08 22:51:59 +0000364}
365void StmtDumper::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000366 DumpExpr(Node);
367 fprintf(F, " %s ", Node->isSizeOf() ? "sizeof" : "alignof");
368 DumpType(Node->getArgumentType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000369}
Chris Lattner13cb21f2007-08-09 17:35:30 +0000370
Chris Lattner6000dac2007-08-08 22:51:59 +0000371void StmtDumper::VisitMemberExpr(MemberExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000372 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000373 fprintf(F, " %s%s %p", Node->isArrow() ? "->" : ".",
Chris Lattner13cb21f2007-08-09 17:35:30 +0000374 Node->getMemberDecl()->getName(), (void*)Node->getMemberDecl());
Chris Lattner6000dac2007-08-08 22:51:59 +0000375}
Nate Begeman213541a2008-04-18 23:10:10 +0000376void StmtDumper::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000377 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000378 fprintf(F, " %s", Node->getAccessor().getName());
Chris Lattner6000dac2007-08-08 22:51:59 +0000379}
Chris Lattner6000dac2007-08-08 22:51:59 +0000380void StmtDumper::VisitBinaryOperator(BinaryOperator *Node) {
381 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000382 fprintf(F, " '%s'", BinaryOperator::getOpcodeStr(Node->getOpcode()));
Chris Lattnereb14fe82007-08-25 02:00:02 +0000383}
384void StmtDumper::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
385 DumpExpr(Node);
386 fprintf(F, " '%s' ComputeTy=",
387 BinaryOperator::getOpcodeStr(Node->getOpcode()));
388 DumpType(Node->getComputationType());
Chris Lattner6000dac2007-08-08 22:51:59 +0000389}
Chris Lattner6000dac2007-08-08 22:51:59 +0000390
391// GNU extensions.
392
393void StmtDumper::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000394 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000395 fprintf(F, " %s %p", Node->getLabel()->getName(), (void*)Node->getLabel());
Chris Lattner6000dac2007-08-08 22:51:59 +0000396}
397
Chris Lattner6000dac2007-08-08 22:51:59 +0000398void StmtDumper::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000399 DumpExpr(Node);
400 fprintf(F, " ");
401 DumpType(Node->getArgType1());
402 fprintf(F, " ");
403 DumpType(Node->getArgType2());
Chris Lattner6000dac2007-08-08 22:51:59 +0000404}
405
Chris Lattnerf9e05812007-08-09 18:03:18 +0000406//===----------------------------------------------------------------------===//
407// C++ Expressions
408//===----------------------------------------------------------------------===//
Chris Lattner6000dac2007-08-08 22:51:59 +0000409
410void StmtDumper::VisitCXXCastExpr(CXXCastExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000411 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000412 fprintf(F, " %s", CXXCastExpr::getOpcodeStr(Node->getOpcode()));
Chris Lattner6000dac2007-08-08 22:51:59 +0000413}
414
415void StmtDumper::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000416 DumpExpr(Node);
Chris Lattnerb3938792007-08-30 00:53:54 +0000417 fprintf(F, " %s", Node->getValue() ? "true" : "false");
Chris Lattner6000dac2007-08-08 22:51:59 +0000418}
419
Anders Carlsson55085182007-08-21 17:43:55 +0000420//===----------------------------------------------------------------------===//
421// Obj-C Expressions
422//===----------------------------------------------------------------------===//
423
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000424void StmtDumper::VisitObjCMessageExpr(ObjCMessageExpr* Node) {
425 DumpExpr(Node);
426 fprintf(F, " selector=%s", Node->getSelector().getName().c_str());
Ted Kremenekea958e572008-05-01 17:26:20 +0000427 IdentifierInfo* clsName = Node->getClassName();
428 if (clsName) fprintf(F, " class=%s", clsName->getName());
Ted Kremenekb3d914b2008-02-29 22:04:05 +0000429}
430
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000431void StmtDumper::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
432 DumpExpr(Node);
433
434 fprintf(F, " ");
435 DumpType(Node->getEncodedType());
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000436}
437
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000438void StmtDumper::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
439 DumpExpr(Node);
440
441 fprintf(F, " ");
Ted Kremenek97b7f262008-04-16 04:30:16 +0000442 Selector selector = Node->getSelector();
Fariborz Jahanianc5c42f52007-10-16 21:07:53 +0000443 fprintf(F, "%s", selector.getName().c_str());
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000444}
445
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000446void StmtDumper::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
447 DumpExpr(Node);
448
449 fprintf(F, " ");
450 fprintf(F, "%s", Node->getProtocol()->getName());
451}
Chris Lattner6000dac2007-08-08 22:51:59 +0000452//===----------------------------------------------------------------------===//
453// Stmt method implementations
454//===----------------------------------------------------------------------===//
455
456/// dump - This does a local dump of the specified AST fragment. It dumps the
457/// specified node and a few nodes underneath it, but not the whole subtree.
458/// This is useful in a debugger.
Chris Lattnere300c872007-08-30 06:17:34 +0000459void Stmt::dump(SourceManager &SM) const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000460 StmtDumper P(&SM, stderr, 4);
Chris Lattnerb3938792007-08-30 00:53:54 +0000461 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattner0c727a32007-08-30 00:40:08 +0000462 fprintf(stderr, "\n");
463}
464
465/// dump - This does a local dump of the specified AST fragment. It dumps the
466/// specified node and a few nodes underneath it, but not the whole subtree.
467/// This is useful in a debugger.
Chris Lattner6000dac2007-08-08 22:51:59 +0000468void Stmt::dump() const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000469 StmtDumper P(0, stderr, 4);
Chris Lattnerb3938792007-08-30 00:53:54 +0000470 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattner0c727a32007-08-30 00:40:08 +0000471 fprintf(stderr, "\n");
472}
473
474/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
Chris Lattnere300c872007-08-30 06:17:34 +0000475void Stmt::dumpAll(SourceManager &SM) const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000476 StmtDumper P(&SM, stderr, ~0U);
Chris Lattnerb3938792007-08-30 00:53:54 +0000477 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattneree41ce52007-08-10 21:51:12 +0000478 fprintf(stderr, "\n");
Chris Lattner6000dac2007-08-08 22:51:59 +0000479}
480
481/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
482void Stmt::dumpAll() const {
Chris Lattner0c727a32007-08-30 00:40:08 +0000483 StmtDumper P(0, stderr, ~0U);
Chris Lattnerb3938792007-08-30 00:53:54 +0000484 P.DumpSubTree(const_cast<Stmt*>(this));
Chris Lattneree41ce52007-08-10 21:51:12 +0000485 fprintf(stderr, "\n");
Chris Lattner6000dac2007-08-08 22:51:59 +0000486}