blob: a88399657bdc83fba11cc81bab50798861058a41 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- StmtPrinter.cpp - Printing 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.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/StmtVisitor.h"
15#include "clang/AST/Decl.h"
16#include "clang/AST/ExprCXX.h"
17#include "clang/Lex/IdentifierTable.h"
18#include "llvm/Support/Compiler.h"
19#include <iostream>
Chris Lattnerb0a721a2007-07-13 05:18:11 +000020#include <iomanip>
Reid Spencer5f016e22007-07-11 17:01:13 +000021using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// StmtPrinter Visitor
25//===----------------------------------------------------------------------===//
26
27namespace {
28 class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor {
29 std::ostream &OS;
30 unsigned IndentLevel;
31 public:
32 StmtPrinter(std::ostream &os) : OS(os), IndentLevel(0) {}
33
34 void PrintStmt(Stmt *S, int SubIndent = 1) {
35 IndentLevel += SubIndent;
36 if (S && isa<Expr>(S)) {
37 // If this is an expr used in a stmt context, indent and newline it.
38 Indent();
39 S->visit(*this);
40 OS << ";\n";
41 } else if (S) {
42 S->visit(*this);
43 } else {
44 Indent() << "<<<NULL STATEMENT>>>\n";
45 }
46 IndentLevel -= SubIndent;
47 }
48
49 void PrintRawCompoundStmt(CompoundStmt *S);
50 void PrintRawDecl(Decl *D);
51 void PrintRawIfStmt(IfStmt *If);
52
53 void PrintExpr(Expr *E) {
54 if (E)
55 E->visit(*this);
56 else
57 OS << "<null expr>";
58 }
59
60 std::ostream &Indent(int Delta = 0) const {
61 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
62 OS << " ";
63 return OS;
64 }
65
66 virtual void VisitStmt(Stmt *Node);
67#define STMT(N, CLASS, PARENT) \
68 virtual void Visit##CLASS(CLASS *Node);
69#include "clang/AST/StmtNodes.def"
70 };
71}
72
73//===----------------------------------------------------------------------===//
74// Stmt printing methods.
75//===----------------------------------------------------------------------===//
76
77void StmtPrinter::VisitStmt(Stmt *Node) {
78 Indent() << "<<unknown stmt type>>\n";
79}
80
81/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
82/// with no newline after the }.
83void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
84 OS << "{\n";
85 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
86 I != E; ++I)
87 PrintStmt(*I);
88
89 Indent() << "}";
90}
91
92void StmtPrinter::PrintRawDecl(Decl *D) {
93 // FIXME: Need to complete/beautify this... this code simply shows the
94 // nodes are where they need to be.
95 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
96 OS << "typedef " << localType->getUnderlyingType().getAsString();
97 OS << " " << localType->getName();
98 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
99 // Emit storage class for vardecls.
100 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
101 switch (V->getStorageClass()) {
102 default: assert(0 && "Unknown storage class!");
103 case VarDecl::None: break;
104 case VarDecl::Extern: OS << "extern "; break;
105 case VarDecl::Static: OS << "static "; break;
106 case VarDecl::Auto: OS << "auto "; break;
107 case VarDecl::Register: OS << "register "; break;
108 }
109 }
110
111 std::string Name = VD->getName();
112 VD->getType().getAsStringInternal(Name);
113 OS << Name;
114
Chris Lattner24c39902007-07-12 00:36:32 +0000115 // If this is a vardecl with an initializer, emit it.
116 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
117 if (V->getInit()) {
118 OS << " = ";
119 PrintExpr(V->getInit());
120 }
121 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 } else {
123 // FIXME: "struct x;"
124 assert(0 && "Unexpected decl");
125 }
126}
127
128
129void StmtPrinter::VisitNullStmt(NullStmt *Node) {
130 Indent() << ";\n";
131}
132
133void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
134 for (Decl *D = Node->getDecl(); D; D = D->getNextDeclarator()) {
135 Indent();
136 PrintRawDecl(D);
137 OS << ";\n";
138 }
139}
140
141void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
142 Indent();
143 PrintRawCompoundStmt(Node);
144 OS << "\n";
145}
146
147void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
148 Indent(-1) << "case ";
149 PrintExpr(Node->getLHS());
150 if (Node->getRHS()) {
151 OS << " ... ";
152 PrintExpr(Node->getRHS());
153 }
154 OS << ":\n";
155
156 PrintStmt(Node->getSubStmt(), 0);
157}
158
159void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
160 Indent(-1) << "default:\n";
161 PrintStmt(Node->getSubStmt(), 0);
162}
163
164void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
165 Indent(-1) << Node->getName() << ":\n";
166 PrintStmt(Node->getSubStmt(), 0);
167}
168
169void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
170 OS << "if ";
171 PrintExpr(If->getCond());
172
173 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
174 OS << ' ';
175 PrintRawCompoundStmt(CS);
176 OS << (If->getElse() ? ' ' : '\n');
177 } else {
178 OS << '\n';
179 PrintStmt(If->getThen());
180 if (If->getElse()) Indent();
181 }
182
183 if (Stmt *Else = If->getElse()) {
184 OS << "else";
185
186 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
187 OS << ' ';
188 PrintRawCompoundStmt(CS);
189 OS << '\n';
190 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
191 OS << ' ';
192 PrintRawIfStmt(ElseIf);
193 } else {
194 OS << '\n';
195 PrintStmt(If->getElse());
196 }
197 }
198}
199
200void StmtPrinter::VisitIfStmt(IfStmt *If) {
201 Indent();
202 PrintRawIfStmt(If);
203}
204
205void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
206 Indent() << "switch (";
207 PrintExpr(Node->getCond());
208 OS << ")";
209
210 // Pretty print compoundstmt bodies (very common).
211 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
212 OS << " ";
213 PrintRawCompoundStmt(CS);
214 OS << "\n";
215 } else {
216 OS << "\n";
217 PrintStmt(Node->getBody());
218 }
219}
220
221void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
222 Indent() << "while (";
223 PrintExpr(Node->getCond());
224 OS << ")\n";
225 PrintStmt(Node->getBody());
226}
227
228void StmtPrinter::VisitDoStmt(DoStmt *Node) {
229 Indent() << "do\n";
230 PrintStmt(Node->getBody());
231 Indent() << "while ";
232 PrintExpr(Node->getCond());
233 OS << ";\n";
234}
235
236void StmtPrinter::VisitForStmt(ForStmt *Node) {
237 Indent() << "for (";
238 if (Node->getInit()) {
239 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
240 PrintRawDecl(DS->getDecl());
241 else
242 PrintExpr(cast<Expr>(Node->getInit()));
243 }
244 OS << "; ";
245 if (Node->getCond())
246 PrintExpr(Node->getCond());
247 OS << "; ";
248 if (Node->getInc())
249 PrintExpr(Node->getInc());
250 OS << ")\n";
251 PrintStmt(Node->getBody());
252}
253
254void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
255 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
256}
257
258void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
259 Indent() << "goto *";
260 PrintExpr(Node->getTarget());
261 OS << ";\n";
262}
263
264void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
265 Indent() << "continue;\n";
266}
267
268void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
269 Indent() << "break;\n";
270}
271
272
273void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
274 Indent() << "return";
275 if (Node->getRetValue()) {
276 OS << " ";
277 PrintExpr(Node->getRetValue());
278 }
279 OS << ";\n";
280}
281
282//===----------------------------------------------------------------------===//
283// Expr printing methods.
284//===----------------------------------------------------------------------===//
285
286void StmtPrinter::VisitExpr(Expr *Node) {
287 OS << "<<unknown expr type>>";
288}
289
290void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
291 OS << Node->getDecl()->getName();
292}
293
294void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000295 // FIXME should print an L for wchar_t constants
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000296 unsigned value = Node->getValue();
Chris Lattner8bf9f072007-07-13 23:58:20 +0000297 switch (value) {
298 case '\\':
299 OS << "'\\\\'";
300 break;
301 case '\'':
302 OS << "'\\''";
303 break;
304 case '\a':
305 // TODO: K&R: the meaning of '\\a' is different in traditional C
306 OS << "'\\a'";
307 break;
308 case '\b':
309 OS << "'\\b'";
310 break;
311 // Nonstandard escape sequence.
312 /*case '\e':
313 OS << "'\\e'";
314 break;*/
315 case '\f':
316 OS << "'\\f'";
317 break;
318 case '\n':
319 OS << "'\\n'";
320 break;
321 case '\r':
322 OS << "'\\r'";
323 break;
324 case '\t':
325 OS << "'\\t'";
326 break;
327 case '\v':
328 OS << "'\\v'";
329 break;
330 default:
331 if (isprint(value) && value < 256) {
332 OS << "'" << (char)value << "'";
333 } else if (value < 256) {
334 OS << "'\\x" << std::hex << value << std::dec << "'";
335 } else {
336 // FIXME what to really do here?
337 OS << value;
338 }
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000339 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000340}
341
342void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
343 bool isSigned = Node->getType()->isSignedIntegerType();
344 OS << Node->getValue().toString(10, isSigned);
345
346 // Emit suffixes. Integer literals are always a builtin integer type.
347 switch (cast<BuiltinType>(Node->getType().getCanonicalType())->getKind()) {
348 default: assert(0 && "Unexpected type for integer literal!");
349 case BuiltinType::Int: break; // no suffix.
350 case BuiltinType::UInt: OS << 'U'; break;
351 case BuiltinType::Long: OS << 'L'; break;
352 case BuiltinType::ULong: OS << "UL"; break;
353 case BuiltinType::LongLong: OS << "LL"; break;
354 case BuiltinType::ULongLong: OS << "ULL"; break;
355 }
356}
357void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
358 // FIXME: print value.
359 OS << "~1.0~";
360}
361void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
362 if (Str->isWide()) OS << 'L';
363 OS << '"';
364
365 // FIXME: this doesn't print wstrings right.
366 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
367 switch (Str->getStrData()[i]) {
368 default: OS << Str->getStrData()[i]; break;
369 // Handle some common ones to make dumps prettier.
370 case '\\': OS << "\\\\"; break;
371 case '"': OS << "\\\""; break;
372 case '\n': OS << "\\n"; break;
373 case '\t': OS << "\\t"; break;
374 case '\a': OS << "\\a"; break;
375 case '\b': OS << "\\b"; break;
376 }
377 }
378 OS << '"';
379}
380void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
381 OS << "(";
382 PrintExpr(Node->getSubExpr());
383 OS << ")";
384}
385void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
386 if (!Node->isPostfix())
387 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
388 PrintExpr(Node->getSubExpr());
389
390 if (Node->isPostfix())
391 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
392
393}
394void StmtPrinter::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
395 OS << (Node->isSizeOf() ? "sizeof(" : "__alignof(");
396 OS << Node->getArgumentType().getAsString() << ")";
397}
398void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
399 PrintExpr(Node->getBase());
400 OS << "[";
401 PrintExpr(Node->getIdx());
402 OS << "]";
403}
404
405void StmtPrinter::VisitCallExpr(CallExpr *Call) {
406 PrintExpr(Call->getCallee());
407 OS << "(";
408 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
409 if (i) OS << ", ";
410 PrintExpr(Call->getArg(i));
411 }
412 OS << ")";
413}
414void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
415 PrintExpr(Node->getBase());
416 OS << (Node->isArrow() ? "->" : ".");
417
418 FieldDecl *Field = Node->getMemberDecl();
419 assert(Field && "MemberExpr should alway reference a field!");
420 OS << Field->getName();
421}
422void StmtPrinter::VisitCastExpr(CastExpr *Node) {
423 OS << "(" << Node->getDestType().getAsString() << ")";
424 PrintExpr(Node->getSubExpr());
425}
Steve Naroff49b45262007-07-13 16:58:59 +0000426void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroff90045e82007-07-13 23:32:42 +0000427 // No need to print anything, simply forward to the sub expression.
428 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +0000429}
Reid Spencer5f016e22007-07-11 17:01:13 +0000430void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
431 PrintExpr(Node->getLHS());
432 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
433 PrintExpr(Node->getRHS());
434}
435void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
436 PrintExpr(Node->getCond());
437 OS << " ? ";
438 PrintExpr(Node->getLHS());
439 OS << " : ";
440 PrintExpr(Node->getRHS());
441}
442
443// GNU extensions.
444
445void StmtPrinter::VisitAddrLabel(AddrLabel *Node) {
446 OS << "&&" << Node->getLabel()->getName();
447
448}
449
450// C++
451
452void StmtPrinter::VisitCXXCastExpr(CXXCastExpr *Node) {
453 switch (Node->getOpcode()) {
454 default:
455 assert(0 && "Not a C++ cast expression");
456 abort();
457 case CXXCastExpr::ConstCast: OS << "const_cast<"; break;
458 case CXXCastExpr::DynamicCast: OS << "dynamic_cast<"; break;
459 case CXXCastExpr::ReinterpretCast: OS << "reinterpret_cast<"; break;
460 case CXXCastExpr::StaticCast: OS << "static_cast<"; break;
461 }
462
463 OS << Node->getDestType().getAsString() << ">(";
464 PrintExpr(Node->getSubExpr());
465 OS << ")";
466}
467
468void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
469 OS << (Node->getValue() ? "true" : "false");
470}
471
472
473//===----------------------------------------------------------------------===//
474// Stmt method implementations
475//===----------------------------------------------------------------------===//
476
477void Stmt::dump() const {
478 // FIXME: eliminate use of <iostream>
479 print(std::cerr);
480}
481
482void Stmt::print(std::ostream &OS) const {
483 if (this == 0) {
484 OS << "<NULL>";
485 return;
486 }
487
488 StmtPrinter P(OS);
489 const_cast<Stmt*>(this)->visit(P);
490}