blob: 5d288778b3ee695af824193a47c4bf49f9a4b80a [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) {
169#if 0
170 Indent(-1) << "case ";
171 DumpExpr(Node->getLHS());
172 if (Node->getRHS()) {
173 OS << " ... ";
174 DumpExpr(Node->getRHS());
175 }
176 OS << ":\n";
177
178 DumpSubTree(Node->getSubStmt(), 0);
179#endif
180}
181
182void StmtDumper::VisitDefaultStmt(DefaultStmt *Node) {
183 DumpStmt(Node);
184 fprintf(F, "\n");
185 DumpSubTree(Node->getSubStmt());
186 fprintf(F, ")");
187}
188
189void StmtDumper::VisitLabelStmt(LabelStmt *Node) {
190 DumpStmt(Node);
191 fprintf(F, " '%s'\n", Node->getName());
192 DumpSubTree(Node->getSubStmt());
193 fprintf(F, "\n");
194}
195
196void StmtDumper::VisitIfStmt(IfStmt *Node) {
197 DumpStmt(Node);
198 fprintf(F, "\n");
199 DumpSubTree(Node->getCond());
200 fprintf(F, "\n");
201 DumpSubTree(Node->getThen());
202 fprintf(F, "\n");
203 DumpSubTree(Node->getElse());
204 fprintf(F, ")");
205}
206
207void StmtDumper::VisitSwitchStmt(SwitchStmt *Node) {
208 DumpStmt(Node);
209 fprintf(F, "\n");
210 DumpSubTree(Node->getCond());
211 fprintf(F, "\n");
212 DumpSubTree(Node->getBody());
213 fprintf(F, ")");
214}
215
216void StmtDumper::VisitSwitchCase(SwitchCase*) {
217 assert(0 && "SwitchCase is an abstract class");
218}
219
220void StmtDumper::VisitWhileStmt(WhileStmt *Node) {
221 DumpStmt(Node);
222 fprintf(F, "\n");
223 DumpSubTree(Node->getCond());
224 fprintf(F, "\n");
225 DumpSubTree(Node->getBody());
226 fprintf(F, ")");
227}
228
229void StmtDumper::VisitDoStmt(DoStmt *Node) {
230 DumpStmt(Node);
231 fprintf(F, "\n");
232 DumpSubTree(Node->getBody());
233 fprintf(F, "\n");
234 DumpSubTree(Node->getCond());
235 fprintf(F, ")");
236}
237
238void StmtDumper::VisitForStmt(ForStmt *Node) {
239 DumpStmt(Node);
240 fprintf(F, "\n");
241 DumpSubTree(Node->getInit());
242 fprintf(F, "\n");
243 DumpSubTree(Node->getCond());
244 fprintf(F, "\n");
245 DumpSubTree(Node->getInc());
246 fprintf(F, "\n");
247 DumpSubTree(Node->getBody());
248 fprintf(F, ")");
249}
250
251void StmtDumper::VisitGotoStmt(GotoStmt *Node) {
252 DumpStmt(Node);
253 fprintf(F, " '%s':%p)", Node->getLabel()->getName(), (void*)Node->getLabel());
254}
255
256void StmtDumper::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
257 DumpStmt(Node);
258 fprintf(F, "\n");
259 DumpSubTree(Node->getTarget());
260 fprintf(F, ")");
261}
262
263void StmtDumper::VisitContinueStmt(ContinueStmt *Node) {
264 DumpStmt(Node);
265 fprintf(F, ")");
266}
267
268void StmtDumper::VisitBreakStmt(BreakStmt *Node) {
269 DumpStmt(Node);
270 fprintf(F, ")");
271}
272
273
274void StmtDumper::VisitReturnStmt(ReturnStmt *Node) {
275 DumpStmt(Node);
276 if (Expr *RV = Node->getRetValue()) {
277 fprintf(F, "\n");
278 DumpSubTree(RV);
279 }
280 fprintf(F, ")");
281}
282
283//===----------------------------------------------------------------------===//
284// Expr printing methods.
285//===----------------------------------------------------------------------===//
286
287void StmtDumper::VisitExpr(Expr *Node) {
288 DumpExpr(Node);
289 fprintf(F, ": UNKNOWN EXPR to StmtDumper)");
290}
291
292void StmtDumper::VisitDeclRefExpr(DeclRefExpr *Node) {
293 DumpExpr(Node);
294 fprintf(F, " Decl='%s' %p)", Node->getDecl()->getName(),
295 (void*)Node->getDecl());
296}
297
298void StmtDumper::VisitPreDefinedExpr(PreDefinedExpr *Node) {
299 DumpExpr(Node);
300 switch (Node->getIdentType()) {
301 default:
302 assert(0 && "unknown case");
303 case PreDefinedExpr::Func:
304 fprintf(F, " __func__)");
305 break;
306 case PreDefinedExpr::Function:
307 fprintf(F, " __FUNCTION__)");
308 break;
309 case PreDefinedExpr::PrettyFunction:
310 fprintf(F, " __PRETTY_FUNCTION__)");
311 break;
312 }
313}
314
315void StmtDumper::VisitCharacterLiteral(CharacterLiteral *Node) {
316#if 0
317 // FIXME should print an L for wchar_t constants
318 unsigned value = Node->getValue();
319 switch (value) {
320 case '\\':
321 OS << "'\\\\'";
322 break;
323 case '\'':
324 OS << "'\\''";
325 break;
326 case '\a':
327 // TODO: K&R: the meaning of '\\a' is different in traditional C
328 OS << "'\\a'";
329 break;
330 case '\b':
331 OS << "'\\b'";
332 break;
333 // Nonstandard escape sequence.
334 /*case '\e':
335 OS << "'\\e'";
336 break;*/
337 case '\f':
338 OS << "'\\f'";
339 break;
340 case '\n':
341 OS << "'\\n'";
342 break;
343 case '\r':
344 OS << "'\\r'";
345 break;
346 case '\t':
347 OS << "'\\t'";
348 break;
349 case '\v':
350 OS << "'\\v'";
351 break;
352 default:
353 if (isprint(value) && value < 256) {
354 OS << "'" << (char)value << "'";
355 } else if (value < 256) {
356 OS << "'\\x" << std::hex << value << std::dec << "'";
357 } else {
358 // FIXME what to really do here?
359 OS << value;
360 }
361 }
362#endif
363}
364
365void StmtDumper::VisitIntegerLiteral(IntegerLiteral *Node) {
366 DumpExpr(Node);
367
368 bool isSigned = Node->getType()->isSignedIntegerType();
369 fprintf(F, " %s)", Node->getValue().toString(10, isSigned).c_str());
370}
371void StmtDumper::VisitFloatingLiteral(FloatingLiteral *Node) {
372 DumpExpr(Node);
373 fprintf(F, " %f)", Node->getValue());
374}
375void StmtDumper::VisitStringLiteral(StringLiteral *Str) {
376#if 0
377 if (Str->isWide()) OS << 'L';
378 OS << '"';
379
380 // FIXME: this doesn't print wstrings right.
381 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
382 switch (Str->getStrData()[i]) {
383 default: OS << Str->getStrData()[i]; break;
384 // Handle some common ones to make dumps prettier.
385 case '\\': OS << "\\\\"; break;
386 case '"': OS << "\\\""; break;
387 case '\n': OS << "\\n"; break;
388 case '\t': OS << "\\t"; break;
389 case '\a': OS << "\\a"; break;
390 case '\b': OS << "\\b"; break;
391 }
392 }
393 OS << '"';
394#endif
395}
396void StmtDumper::VisitParenExpr(ParenExpr *Node) {
397 DumpExpr(Node);
398 fprintf(F, "\n");
399 DumpSubTree(Node->getSubExpr());
400 fprintf(F, ")");
401}
402void StmtDumper::VisitUnaryOperator(UnaryOperator *Node) {
403#if 0
404 if (!Node->isPostfix())
405 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
406 DumpExpr(Node->getSubExpr());
407
408 if (Node->isPostfix())
409 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
410
411#endif
412}
413void StmtDumper::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
414#if 0
415 OS << (Node->isSizeOf() ? "sizeof(" : "__alignof(");
416 OS << Node->getArgumentType().getAsString() << ")";
417#endif
418}
419void StmtDumper::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
420 DumpExpr(Node);
421 fprintf(F, "\n");
422 DumpSubTree(Node->getBase());
423 fprintf(F, "\n");
424 DumpSubTree(Node->getIdx());
425 fprintf(F, ")");
426}
427
428void StmtDumper::VisitCallExpr(CallExpr *Node) {
429 DumpExpr(Node);
430 fprintf(F, "\n");
431 DumpSubTree(Node->getCallee());
432
433 for (unsigned i = 0, e = Node->getNumArgs(); i != e; ++i) {
434 fprintf(F, "\n");
435 DumpSubTree(Node->getArg(i));
436 }
437 fprintf(F, ")");
438}
439void StmtDumper::VisitMemberExpr(MemberExpr *Node) {
440#if 0
441 DumpExpr(Node->getBase());
442 OS << (Node->isArrow() ? "->" : ".");
443
444 FieldDecl *Field = Node->getMemberDecl();
445 assert(Field && "MemberExpr should alway reference a field!");
446 OS << Field->getName();
447#endif
448}
449void StmtDumper::VisitOCUVectorElementExpr(OCUVectorElementExpr *Node) {
450#if 0
451 DumpExpr(Node->getBase());
452 OS << ".";
453 OS << Node->getAccessor().getName();
454#endif
455}
456void StmtDumper::VisitCastExpr(CastExpr *Node) {
457#if 0
458 OS << "(" << Node->getType().getAsString() << ")";
459 DumpExpr(Node->getSubExpr());
460#endif
461}
462void StmtDumper::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
463#if 0
464 OS << "(" << Node->getType().getAsString() << ")";
465 DumpExpr(Node->getInitializer());
466#endif
467}
468void StmtDumper::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
469 DumpExpr(Node);
470 fprintf(F, "\n");
471 DumpSubTree(Node->getSubExpr());
472 fprintf(F, ")");
473}
474void StmtDumper::VisitBinaryOperator(BinaryOperator *Node) {
475 DumpExpr(Node);
476 fprintf(F, " '%s'\n", BinaryOperator::getOpcodeStr(Node->getOpcode()));
477 DumpSubTree(Node->getLHS());
478 fprintf(F, "\n");
479 DumpSubTree(Node->getRHS());
480 fprintf(F, ")");
481}
482void StmtDumper::VisitConditionalOperator(ConditionalOperator *Node) {
483 DumpExpr(Node);
484 fprintf(F, "\n");
485 DumpSubTree(Node->getCond());
486 fprintf(F, "\n");
487 DumpSubTree(Node->getLHS());
488 fprintf(F, "\n");
489 DumpSubTree(Node->getRHS());
490 fprintf(F, ")");
491}
492
493// GNU extensions.
494
495void StmtDumper::VisitAddrLabelExpr(AddrLabelExpr *Node) {
496#if 0
497 OS << "&&" << Node->getLabel()->getName();
498#endif
499}
500
501void StmtDumper::VisitStmtExpr(StmtExpr *E) {
502#if 0
503 OS << "(";
504 DumpSubTree(E->getSubStmt());
505 OS << ")";
506#endif
507}
508
509void StmtDumper::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
510#if 0
511 OS << "__builtin_types_compatible_p(";
512 OS << Node->getArgType1().getAsString() << ",";
513 OS << Node->getArgType2().getAsString() << ")";
514#endif
515}
516
517void StmtDumper::VisitChooseExpr(ChooseExpr *Node) {
518#if 0
519 OS << "__builtin_choose_expr(";
520 DumpExpr(Node->getCond());
521 OS << ", ";
522 DumpExpr(Node->getLHS());
523 OS << ", ";
524 DumpExpr(Node->getRHS());
525 OS << ")";
526#endif
527}
528
529// C++
530
531void StmtDumper::VisitCXXCastExpr(CXXCastExpr *Node) {
532#if 0
533 switch (Node->getOpcode()) {
534 default:
535 assert(0 && "Not a C++ cast expression");
536 abort();
537 case CXXCastExpr::ConstCast: OS << "const_cast<"; break;
538 case CXXCastExpr::DynamicCast: OS << "dynamic_cast<"; break;
539 case CXXCastExpr::ReinterpretCast: OS << "reinterpret_cast<"; break;
540 case CXXCastExpr::StaticCast: OS << "static_cast<"; break;
541 }
542
543 OS << Node->getDestType().getAsString() << ">(";
544 DumpExpr(Node->getSubExpr());
545 OS << ")";
546#endif
547}
548
549void StmtDumper::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
550#if 0
551 OS << (Node->getValue() ? "true" : "false");
552#endif
553}
554
555
556//===----------------------------------------------------------------------===//
557// Stmt method implementations
558//===----------------------------------------------------------------------===//
559
560/// dump - This does a local dump of the specified AST fragment. It dumps the
561/// specified node and a few nodes underneath it, but not the whole subtree.
562/// This is useful in a debugger.
563void Stmt::dump() const {
564 StmtDumper P(stderr, 4);
565 const_cast<Stmt*>(this)->visit(P);
566}
567
568/// dumpAll - This does a dump of the specified AST fragment and all subtrees.
569void Stmt::dumpAll() const {
570 StmtDumper P(stderr, ~0U);
571 const_cast<Stmt*>(this)->visit(P);
572}