blob: 8a52dd245a5b6a79a1d618f975d31b9b2227e37d [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//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
16#include "clang/AST/Decl.h"
17#include "clang/AST/ExprCXX.h"
18#include "clang/Lex/IdentifierTable.h"
19#include "llvm/Support/Compiler.h"
20#include <cstdio>
21using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// StmtDumper Visitor
25//===----------------------------------------------------------------------===//
26
27namespace {
28 class VISIBILITY_HIDDEN StmtDumper : public StmtVisitor {
29 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;
36 public:
37 StmtDumper(FILE *f, unsigned maxDepth)
38 : F(f), IndentLevel(0), MaxDepth(maxDepth) {}
39
40 void DumpSubTree(Stmt *S, int SubIndent = 1) {
41 // Prune the recursion if not using dump all.
42 if (MaxDepth == 0) return;
43
44 IndentLevel += SubIndent;
45 if (S) {
46 S->visit(*this);
47 } else {
48 Indent();
49 fprintf(F, "<<<NULL>>>\n");
50 }
51 IndentLevel -= SubIndent;
52 }
53
54 void PrintRawDecl(Decl *D);
55
56 void Indent() const {
57 for (int i = 0, e = IndentLevel; i < e; ++i)
58 fprintf(F, " ");
59 }
60
Chris Lattnerfd8f7da2007-08-09 00:36:22 +000061 void DumpType(QualType T) const {
62 fprintf(F, "'%s'", T.getAsString().c_str());
63
64 // If the type is directly a typedef, strip off typedefness to give at
65 // least one level of concreteness.
66 if (TypedefType *TDT = dyn_cast<TypedefType>(T))
67 fprintf(F, ":'%s'", TDT->LookThroughTypedefs().getAsString().c_str());
68 }
69
Chris Lattner6000dac2007-08-08 22:51:59 +000070 void DumpStmt(const Stmt *Node) const {
71 Indent();
72 fprintf(F, "(%s %p", Node->getStmtClassName(), (void*)Node);
73 }
74
75 void DumpExpr(Expr *Node) const {
76 DumpStmt(Node);
Chris Lattnerfd8f7da2007-08-09 00:36:22 +000077 fprintf(F, " ");
78 DumpType(Node->getType());
Chris Lattner6000dac2007-08-08 22:51:59 +000079 }
80
81 virtual void VisitStmt(Stmt *Node);
82#define STMT(N, CLASS, PARENT) \
83 virtual void Visit##CLASS(CLASS *Node);
84#include "clang/AST/StmtNodes.def"
85 };
86}
87
88//===----------------------------------------------------------------------===//
89// Stmt printing methods.
90//===----------------------------------------------------------------------===//
91
92void StmtDumper::VisitStmt(Stmt *Node) {
93 Indent();
94 fprintf(F, "<<unknown stmt type>>\n");
95}
96
97void StmtDumper::PrintRawDecl(Decl *D) {
98#if 0
99 // FIXME: Need to complete/beautify this... this code simply shows the
100 // nodes are where they need to be.
101 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
102 OS << "typedef " << localType->getUnderlyingType().getAsString();
103 OS << " " << localType->getName();
104 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
105 // Emit storage class for vardecls.
106 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
107 switch (V->getStorageClass()) {
108 default: assert(0 && "Unknown storage class!");
109 case VarDecl::None: break;
110 case VarDecl::Extern: OS << "extern "; break;
111 case VarDecl::Static: OS << "static "; break;
112 case VarDecl::Auto: OS << "auto "; break;
113 case VarDecl::Register: OS << "register "; break;
114 }
115 }
116
117 std::string Name = VD->getName();
118 VD->getType().getAsStringInternal(Name);
119 OS << Name;
120
121 // If this is a vardecl with an initializer, emit it.
122 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
123 if (V->getInit()) {
124 OS << " = ";
125 DumpExpr(V->getInit());
126 }
127 }
128 } else {
129 // FIXME: "struct x;"
130 assert(0 && "Unexpected decl");
131 }
132#endif
133}
134
135
136void StmtDumper::VisitNullStmt(NullStmt *Node) {
137 DumpStmt(Node);
138 fprintf(F, ")");
139}
140
141void StmtDumper::VisitDeclStmt(DeclStmt *Node) {
142 DumpStmt(Node);
143 // FIXME: implement this better :)
144 fprintf(F, ")");
145#if 0
146 for (Decl *D = Node->getDecl(); D; D = D->getNextDeclarator()) {
147 Indent();
148 PrintRawDecl(D);
149 OS << ";\n";
150 }
151#endif
152}
153
154void StmtDumper::VisitCompoundStmt(CompoundStmt *Node) {
155 DumpStmt(Node);
156 if (!Node->body_empty()) fprintf(F, "\n");
157
158 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
159 I != E; ) {
160 DumpSubTree(*I);
161 ++I;
162 if (I != E)
163 fprintf(F, "\n");
164 }
165 fprintf(F, ")");
166}
167
168void StmtDumper::VisitCaseStmt(CaseStmt *Node) {
Chris Lattnera0df31a2007-08-09 01:04:32 +0000169 DumpStmt(Node);
170 fprintf(F, "\n");
171 DumpSubTree(Node->getLHS());
172 fprintf(F, "\n");
173 DumpSubTree(Node->getRHS());
174 fprintf(F, "\n");
175 DumpSubTree(Node->getSubStmt());
176 fprintf(F, ")");
Chris Lattner6000dac2007-08-08 22:51:59 +0000177}
178
179void StmtDumper::VisitDefaultStmt(DefaultStmt *Node) {
180 DumpStmt(Node);
181 fprintf(F, "\n");
182 DumpSubTree(Node->getSubStmt());
183 fprintf(F, ")");
184}
185
186void StmtDumper::VisitLabelStmt(LabelStmt *Node) {
187 DumpStmt(Node);
188 fprintf(F, " '%s'\n", Node->getName());
189 DumpSubTree(Node->getSubStmt());
190 fprintf(F, "\n");
191}
192
193void StmtDumper::VisitIfStmt(IfStmt *Node) {
194 DumpStmt(Node);
195 fprintf(F, "\n");
196 DumpSubTree(Node->getCond());
197 fprintf(F, "\n");
198 DumpSubTree(Node->getThen());
199 fprintf(F, "\n");
200 DumpSubTree(Node->getElse());
201 fprintf(F, ")");
202}
203
204void StmtDumper::VisitSwitchStmt(SwitchStmt *Node) {
205 DumpStmt(Node);
206 fprintf(F, "\n");
207 DumpSubTree(Node->getCond());
208 fprintf(F, "\n");
209 DumpSubTree(Node->getBody());
210 fprintf(F, ")");
211}
212
213void StmtDumper::VisitSwitchCase(SwitchCase*) {
214 assert(0 && "SwitchCase is an abstract class");
215}
216
217void StmtDumper::VisitWhileStmt(WhileStmt *Node) {
218 DumpStmt(Node);
219 fprintf(F, "\n");
220 DumpSubTree(Node->getCond());
221 fprintf(F, "\n");
222 DumpSubTree(Node->getBody());
223 fprintf(F, ")");
224}
225
226void StmtDumper::VisitDoStmt(DoStmt *Node) {
227 DumpStmt(Node);
228 fprintf(F, "\n");
229 DumpSubTree(Node->getBody());
230 fprintf(F, "\n");
231 DumpSubTree(Node->getCond());
232 fprintf(F, ")");
233}
234
235void StmtDumper::VisitForStmt(ForStmt *Node) {
236 DumpStmt(Node);
237 fprintf(F, "\n");
238 DumpSubTree(Node->getInit());
239 fprintf(F, "\n");
240 DumpSubTree(Node->getCond());
241 fprintf(F, "\n");
242 DumpSubTree(Node->getInc());
243 fprintf(F, "\n");
244 DumpSubTree(Node->getBody());
245 fprintf(F, ")");
246}
247
248void StmtDumper::VisitGotoStmt(GotoStmt *Node) {
249 DumpStmt(Node);
250 fprintf(F, " '%s':%p)", Node->getLabel()->getName(), (void*)Node->getLabel());
251}
252
253void StmtDumper::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
254 DumpStmt(Node);
255 fprintf(F, "\n");
256 DumpSubTree(Node->getTarget());
257 fprintf(F, ")");
258}
259
260void StmtDumper::VisitContinueStmt(ContinueStmt *Node) {
261 DumpStmt(Node);
262 fprintf(F, ")");
263}
264
265void StmtDumper::VisitBreakStmt(BreakStmt *Node) {
266 DumpStmt(Node);
267 fprintf(F, ")");
268}
269
270
271void StmtDumper::VisitReturnStmt(ReturnStmt *Node) {
272 DumpStmt(Node);
273 if (Expr *RV = Node->getRetValue()) {
274 fprintf(F, "\n");
275 DumpSubTree(RV);
276 }
277 fprintf(F, ")");
278}
279
280//===----------------------------------------------------------------------===//
281// Expr printing methods.
282//===----------------------------------------------------------------------===//
283
284void StmtDumper::VisitExpr(Expr *Node) {
285 DumpExpr(Node);
286 fprintf(F, ": UNKNOWN EXPR to StmtDumper)");
287}
288
289void StmtDumper::VisitDeclRefExpr(DeclRefExpr *Node) {
290 DumpExpr(Node);
291 fprintf(F, " Decl='%s' %p)", Node->getDecl()->getName(),
292 (void*)Node->getDecl());
293}
294
295void StmtDumper::VisitPreDefinedExpr(PreDefinedExpr *Node) {
296 DumpExpr(Node);
297 switch (Node->getIdentType()) {
298 default:
299 assert(0 && "unknown case");
300 case PreDefinedExpr::Func:
301 fprintf(F, " __func__)");
302 break;
303 case PreDefinedExpr::Function:
304 fprintf(F, " __FUNCTION__)");
305 break;
306 case PreDefinedExpr::PrettyFunction:
307 fprintf(F, " __PRETTY_FUNCTION__)");
308 break;
309 }
310}
311
312void StmtDumper::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnera0df31a2007-08-09 01:04:32 +0000313 DumpExpr(Node);
314 fprintf(F, " %d)", Node->getValue());
Chris Lattner6000dac2007-08-08 22:51:59 +0000315}
316
317void StmtDumper::VisitIntegerLiteral(IntegerLiteral *Node) {
318 DumpExpr(Node);
319
320 bool isSigned = Node->getType()->isSignedIntegerType();
321 fprintf(F, " %s)", Node->getValue().toString(10, isSigned).c_str());
322}
323void StmtDumper::VisitFloatingLiteral(FloatingLiteral *Node) {
324 DumpExpr(Node);
325 fprintf(F, " %f)", Node->getValue());
326}
327void StmtDumper::VisitStringLiteral(StringLiteral *Str) {
Chris Lattnera0df31a2007-08-09 01:04:32 +0000328 DumpExpr(Str);
329 // FIXME: this doesn't print wstrings right.
Chris Lattner5fc61072007-08-09 17:14:24 +0000330 fprintf(F, " %s\"", Str->isWide() ? "L" : "");
Chris Lattnera0df31a2007-08-09 01:04:32 +0000331
Chris Lattner6000dac2007-08-08 22:51:59 +0000332 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattner5fc61072007-08-09 17:14:24 +0000333 switch (char C = Str->getStrData()[i]) {
334 default:
335 if (isprint(C))
336 fputc(C, F);
337 else
338 fprintf(F, "\\%03o", C);
339 break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000340 // Handle some common ones to make dumps prettier.
Chris Lattner5fc61072007-08-09 17:14:24 +0000341 case '\\': fprintf(F, "\\\\"); break;
342 case '"': fprintf(F, "\\\""); break;
343 case '\n': fprintf(F, "\\n"); break;
344 case '\t': fprintf(F, "\\t"); break;
345 case '\a': fprintf(F, "\\a"); break;
346 case '\b': fprintf(F, "\\b"); break;
Chris Lattner6000dac2007-08-08 22:51:59 +0000347 }
348 }
Chris Lattner5fc61072007-08-09 17:14:24 +0000349 fprintf(F, "\")");
Chris Lattner6000dac2007-08-08 22:51:59 +0000350}
351void StmtDumper::VisitParenExpr(ParenExpr *Node) {
352 DumpExpr(Node);
353 fprintf(F, "\n");
354 DumpSubTree(Node->getSubExpr());
355 fprintf(F, ")");
356}
357void StmtDumper::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000358 DumpExpr(Node);
359 fprintf(F, " %s '%s'\n", Node->isPostfix() ? "postfix" : "prefix",
360 UnaryOperator::getOpcodeStr(Node->getOpcode()));
361 DumpSubTree(Node->getSubExpr());
362 fprintf(F, ")");
Chris Lattner6000dac2007-08-08 22:51:59 +0000363}
364void StmtDumper::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000365 DumpExpr(Node);
366 fprintf(F, " %s ", Node->isSizeOf() ? "sizeof" : "alignof");
367 DumpType(Node->getArgumentType());
368 fprintf(F, ")");
Chris Lattner6000dac2007-08-08 22:51:59 +0000369}
370void StmtDumper::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
371 DumpExpr(Node);
372 fprintf(F, "\n");
373 DumpSubTree(Node->getBase());
374 fprintf(F, "\n");
375 DumpSubTree(Node->getIdx());
376 fprintf(F, ")");
377}
378
379void StmtDumper::VisitCallExpr(CallExpr *Node) {
380 DumpExpr(Node);
381 fprintf(F, "\n");
382 DumpSubTree(Node->getCallee());
383
384 for (unsigned i = 0, e = Node->getNumArgs(); i != e; ++i) {
385 fprintf(F, "\n");
386 DumpSubTree(Node->getArg(i));
387 }
388 fprintf(F, ")");
389}
Chris Lattner13cb21f2007-08-09 17:35:30 +0000390
Chris Lattner6000dac2007-08-08 22:51:59 +0000391void StmtDumper::VisitMemberExpr(MemberExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000392 DumpExpr(Node);
393 fprintf(F, " %s%s %p\n", Node->isArrow() ? "->" : ".",
394 Node->getMemberDecl()->getName(), (void*)Node->getMemberDecl());
395 DumpSubTree(Node->getBase());
396 fprintf(F, ")");
Chris Lattner6000dac2007-08-08 22:51:59 +0000397}
398void StmtDumper::VisitOCUVectorElementExpr(OCUVectorElementExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000399 DumpExpr(Node);
400 fprintf(F, " %s\n", Node->getAccessor().getName());
401 DumpSubTree(Node->getBase());
402 fprintf(F, ")");
Chris Lattner6000dac2007-08-08 22:51:59 +0000403}
404void StmtDumper::VisitCastExpr(CastExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000405 DumpExpr(Node);
406 fprintf(F, "\n");
407 DumpSubTree(Node->getSubExpr());
408 fprintf(F, ")");
Chris Lattner6000dac2007-08-08 22:51:59 +0000409}
410void StmtDumper::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000411 DumpExpr(Node);
412 fprintf(F, "\n");
413 DumpSubTree(Node->getInitializer());
414 fprintf(F, ")");
Chris Lattner6000dac2007-08-08 22:51:59 +0000415}
416void StmtDumper::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
417 DumpExpr(Node);
418 fprintf(F, "\n");
419 DumpSubTree(Node->getSubExpr());
420 fprintf(F, ")");
421}
422void StmtDumper::VisitBinaryOperator(BinaryOperator *Node) {
423 DumpExpr(Node);
424 fprintf(F, " '%s'\n", BinaryOperator::getOpcodeStr(Node->getOpcode()));
425 DumpSubTree(Node->getLHS());
426 fprintf(F, "\n");
427 DumpSubTree(Node->getRHS());
428 fprintf(F, ")");
429}
430void StmtDumper::VisitConditionalOperator(ConditionalOperator *Node) {
431 DumpExpr(Node);
432 fprintf(F, "\n");
433 DumpSubTree(Node->getCond());
434 fprintf(F, "\n");
435 DumpSubTree(Node->getLHS());
436 fprintf(F, "\n");
437 DumpSubTree(Node->getRHS());
438 fprintf(F, ")");
439}
440
441// GNU extensions.
442
443void StmtDumper::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000444 DumpExpr(Node);
445 fprintf(F, " %s %p)", Node->getLabel()->getName(), (void*)Node->getLabel());
Chris Lattner6000dac2007-08-08 22:51:59 +0000446}
447
Chris Lattner13cb21f2007-08-09 17:35:30 +0000448void StmtDumper::VisitStmtExpr(StmtExpr *Node) {
449 DumpExpr(Node);
450 fprintf(F, "\n");
451 DumpSubTree(Node->getSubStmt());
452 fprintf(F, ")");
Chris Lattner6000dac2007-08-08 22:51:59 +0000453}
454
455void StmtDumper::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000456 DumpExpr(Node);
457 fprintf(F, " ");
458 DumpType(Node->getArgType1());
459 fprintf(F, " ");
460 DumpType(Node->getArgType2());
461 fprintf(F, ")");
Chris Lattner6000dac2007-08-08 22:51:59 +0000462}
463
464void StmtDumper::VisitChooseExpr(ChooseExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000465 DumpExpr(Node);
466 fprintf(F, "\n");
467 DumpSubTree(Node->getCond());
468 fprintf(F, "\n");
469 DumpSubTree(Node->getLHS());
470 fprintf(F, "\n");
471 DumpSubTree(Node->getRHS());
472 fprintf(F, ")");
Chris Lattner6000dac2007-08-08 22:51:59 +0000473}
474
475// C++
476
477void StmtDumper::VisitCXXCastExpr(CXXCastExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000478 DumpExpr(Node);
479 fprintf(F, " %s\n", CXXCastExpr::getOpcodeStr(Node->getOpcode()));
480 DumpSubTree(Node->getSubExpr());
481 fprintf(F, ")");
Chris Lattner6000dac2007-08-08 22:51:59 +0000482}
483
484void StmtDumper::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
Chris Lattner13cb21f2007-08-09 17:35:30 +0000485 DumpExpr(Node);
486 fprintf(F, " %s)", Node->getValue() ? "true" : "false");
Chris Lattner6000dac2007-08-08 22:51:59 +0000487}
488
489
490//===----------------------------------------------------------------------===//
491// Stmt method implementations
492//===----------------------------------------------------------------------===//
493
494/// dump - This does a local dump of the specified AST fragment. It dumps the
495/// specified node and a few nodes underneath it, but not the whole subtree.
496/// This is useful in a debugger.
497void Stmt::dump() const {
498 StmtDumper P(stderr, 4);
499 const_cast<Stmt*>(this)->visit(P);
500}
501
502/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
503void Stmt::dumpAll() const {
504 StmtDumper P(stderr, ~0U);
505 const_cast<Stmt*>(this)->visit(P);
506}