blob: 145f8a1c223151a0617f71129fab62bd140efd5a [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.
330 // FIXME: this doesn't print strings with \0's in them.
331 fprintf(F, " \"%s\")", Str->getStrData());
332
Chris Lattner6000dac2007-08-08 22:51:59 +0000333#if 0
334 if (Str->isWide()) OS << 'L';
335 OS << '"';
336
Chris Lattner6000dac2007-08-08 22:51:59 +0000337 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
338 switch (Str->getStrData()[i]) {
339 default: OS << Str->getStrData()[i]; break;
340 // Handle some common ones to make dumps prettier.
341 case '\\': OS << "\\\\"; break;
342 case '"': OS << "\\\""; break;
343 case '\n': OS << "\\n"; break;
344 case '\t': OS << "\\t"; break;
345 case '\a': OS << "\\a"; break;
346 case '\b': OS << "\\b"; break;
347 }
348 }
349 OS << '"';
350#endif
351}
352void StmtDumper::VisitParenExpr(ParenExpr *Node) {
353 DumpExpr(Node);
354 fprintf(F, "\n");
355 DumpSubTree(Node->getSubExpr());
356 fprintf(F, ")");
357}
358void StmtDumper::VisitUnaryOperator(UnaryOperator *Node) {
359#if 0
360 if (!Node->isPostfix())
361 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
362 DumpExpr(Node->getSubExpr());
363
364 if (Node->isPostfix())
365 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
366
367#endif
368}
369void StmtDumper::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
370#if 0
371 OS << (Node->isSizeOf() ? "sizeof(" : "__alignof(");
372 OS << Node->getArgumentType().getAsString() << ")";
373#endif
374}
375void StmtDumper::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
376 DumpExpr(Node);
377 fprintf(F, "\n");
378 DumpSubTree(Node->getBase());
379 fprintf(F, "\n");
380 DumpSubTree(Node->getIdx());
381 fprintf(F, ")");
382}
383
384void StmtDumper::VisitCallExpr(CallExpr *Node) {
385 DumpExpr(Node);
386 fprintf(F, "\n");
387 DumpSubTree(Node->getCallee());
388
389 for (unsigned i = 0, e = Node->getNumArgs(); i != e; ++i) {
390 fprintf(F, "\n");
391 DumpSubTree(Node->getArg(i));
392 }
393 fprintf(F, ")");
394}
395void StmtDumper::VisitMemberExpr(MemberExpr *Node) {
396#if 0
397 DumpExpr(Node->getBase());
398 OS << (Node->isArrow() ? "->" : ".");
399
400 FieldDecl *Field = Node->getMemberDecl();
401 assert(Field && "MemberExpr should alway reference a field!");
402 OS << Field->getName();
403#endif
404}
405void StmtDumper::VisitOCUVectorElementExpr(OCUVectorElementExpr *Node) {
406#if 0
407 DumpExpr(Node->getBase());
408 OS << ".";
409 OS << Node->getAccessor().getName();
410#endif
411}
412void StmtDumper::VisitCastExpr(CastExpr *Node) {
413#if 0
414 OS << "(" << Node->getType().getAsString() << ")";
415 DumpExpr(Node->getSubExpr());
416#endif
417}
418void StmtDumper::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
419#if 0
420 OS << "(" << Node->getType().getAsString() << ")";
421 DumpExpr(Node->getInitializer());
422#endif
423}
424void StmtDumper::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
425 DumpExpr(Node);
426 fprintf(F, "\n");
427 DumpSubTree(Node->getSubExpr());
428 fprintf(F, ")");
429}
430void StmtDumper::VisitBinaryOperator(BinaryOperator *Node) {
431 DumpExpr(Node);
432 fprintf(F, " '%s'\n", BinaryOperator::getOpcodeStr(Node->getOpcode()));
433 DumpSubTree(Node->getLHS());
434 fprintf(F, "\n");
435 DumpSubTree(Node->getRHS());
436 fprintf(F, ")");
437}
438void StmtDumper::VisitConditionalOperator(ConditionalOperator *Node) {
439 DumpExpr(Node);
440 fprintf(F, "\n");
441 DumpSubTree(Node->getCond());
442 fprintf(F, "\n");
443 DumpSubTree(Node->getLHS());
444 fprintf(F, "\n");
445 DumpSubTree(Node->getRHS());
446 fprintf(F, ")");
447}
448
449// GNU extensions.
450
451void StmtDumper::VisitAddrLabelExpr(AddrLabelExpr *Node) {
452#if 0
453 OS << "&&" << Node->getLabel()->getName();
454#endif
455}
456
457void StmtDumper::VisitStmtExpr(StmtExpr *E) {
458#if 0
459 OS << "(";
460 DumpSubTree(E->getSubStmt());
461 OS << ")";
462#endif
463}
464
465void StmtDumper::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
466#if 0
467 OS << "__builtin_types_compatible_p(";
468 OS << Node->getArgType1().getAsString() << ",";
469 OS << Node->getArgType2().getAsString() << ")";
470#endif
471}
472
473void StmtDumper::VisitChooseExpr(ChooseExpr *Node) {
474#if 0
475 OS << "__builtin_choose_expr(";
476 DumpExpr(Node->getCond());
477 OS << ", ";
478 DumpExpr(Node->getLHS());
479 OS << ", ";
480 DumpExpr(Node->getRHS());
481 OS << ")";
482#endif
483}
484
485// C++
486
487void StmtDumper::VisitCXXCastExpr(CXXCastExpr *Node) {
488#if 0
489 switch (Node->getOpcode()) {
490 default:
491 assert(0 && "Not a C++ cast expression");
492 abort();
493 case CXXCastExpr::ConstCast: OS << "const_cast<"; break;
494 case CXXCastExpr::DynamicCast: OS << "dynamic_cast<"; break;
495 case CXXCastExpr::ReinterpretCast: OS << "reinterpret_cast<"; break;
496 case CXXCastExpr::StaticCast: OS << "static_cast<"; break;
497 }
498
499 OS << Node->getDestType().getAsString() << ">(";
500 DumpExpr(Node->getSubExpr());
501 OS << ")";
502#endif
503}
504
505void StmtDumper::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
506#if 0
507 OS << (Node->getValue() ? "true" : "false");
508#endif
509}
510
511
512//===----------------------------------------------------------------------===//
513// Stmt method implementations
514//===----------------------------------------------------------------------===//
515
516/// dump - This does a local dump of the specified AST fragment. It dumps the
517/// specified node and a few nodes underneath it, but not the whole subtree.
518/// This is useful in a debugger.
519void Stmt::dump() const {
520 StmtDumper P(stderr, 4);
521 const_cast<Stmt*>(this)->visit(P);
522}
523
524/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
525void Stmt::dumpAll() const {
526 StmtDumper P(stderr, ~0U);
527 const_cast<Stmt*>(this)->visit(P);
528}