blob: bdbbc12b1853a6694bb32748eb2253ad59ce7633 [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
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000221void StmtPrinter::VisitSwitchCase(SwitchCase*) {
222 assert(0 && "SwitchCase is an abstract class");
223}
224
Reid Spencer5f016e22007-07-11 17:01:13 +0000225void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
226 Indent() << "while (";
227 PrintExpr(Node->getCond());
228 OS << ")\n";
229 PrintStmt(Node->getBody());
230}
231
232void StmtPrinter::VisitDoStmt(DoStmt *Node) {
233 Indent() << "do\n";
234 PrintStmt(Node->getBody());
235 Indent() << "while ";
236 PrintExpr(Node->getCond());
237 OS << ";\n";
238}
239
240void StmtPrinter::VisitForStmt(ForStmt *Node) {
241 Indent() << "for (";
242 if (Node->getInit()) {
243 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
244 PrintRawDecl(DS->getDecl());
245 else
246 PrintExpr(cast<Expr>(Node->getInit()));
247 }
248 OS << "; ";
249 if (Node->getCond())
250 PrintExpr(Node->getCond());
251 OS << "; ";
252 if (Node->getInc())
253 PrintExpr(Node->getInc());
254 OS << ")\n";
255 PrintStmt(Node->getBody());
256}
257
258void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
259 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
260}
261
262void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
263 Indent() << "goto *";
264 PrintExpr(Node->getTarget());
265 OS << ";\n";
266}
267
268void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
269 Indent() << "continue;\n";
270}
271
272void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
273 Indent() << "break;\n";
274}
275
276
277void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
278 Indent() << "return";
279 if (Node->getRetValue()) {
280 OS << " ";
281 PrintExpr(Node->getRetValue());
282 }
283 OS << ";\n";
284}
285
286//===----------------------------------------------------------------------===//
287// Expr printing methods.
288//===----------------------------------------------------------------------===//
289
290void StmtPrinter::VisitExpr(Expr *Node) {
291 OS << "<<unknown expr type>>";
292}
293
294void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
295 OS << Node->getDecl()->getName();
296}
297
Anders Carlsson22742662007-07-21 05:21:51 +0000298void StmtPrinter::VisitPreDefinedExpr(PreDefinedExpr *Node) {
299 switch (Node->getIdentType()) {
300 default:
301 assert(0 && "unknown case");
302 case PreDefinedExpr::Func:
303 OS << "__func__";
304 break;
305 case PreDefinedExpr::Function:
306 OS << "__FUNCTION__";
307 break;
308 case PreDefinedExpr::PrettyFunction:
309 OS << "__PRETTY_FUNCTION__";
310 break;
311 }
312}
313
Reid Spencer5f016e22007-07-11 17:01:13 +0000314void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000315 // FIXME should print an L for wchar_t constants
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000316 unsigned value = Node->getValue();
Chris Lattner8bf9f072007-07-13 23:58:20 +0000317 switch (value) {
318 case '\\':
319 OS << "'\\\\'";
320 break;
321 case '\'':
322 OS << "'\\''";
323 break;
324 case '\a':
325 // TODO: K&R: the meaning of '\\a' is different in traditional C
326 OS << "'\\a'";
327 break;
328 case '\b':
329 OS << "'\\b'";
330 break;
331 // Nonstandard escape sequence.
332 /*case '\e':
333 OS << "'\\e'";
334 break;*/
335 case '\f':
336 OS << "'\\f'";
337 break;
338 case '\n':
339 OS << "'\\n'";
340 break;
341 case '\r':
342 OS << "'\\r'";
343 break;
344 case '\t':
345 OS << "'\\t'";
346 break;
347 case '\v':
348 OS << "'\\v'";
349 break;
350 default:
351 if (isprint(value) && value < 256) {
352 OS << "'" << (char)value << "'";
353 } else if (value < 256) {
354 OS << "'\\x" << std::hex << value << std::dec << "'";
355 } else {
356 // FIXME what to really do here?
357 OS << value;
358 }
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000359 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000360}
361
362void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
363 bool isSigned = Node->getType()->isSignedIntegerType();
364 OS << Node->getValue().toString(10, isSigned);
365
366 // Emit suffixes. Integer literals are always a builtin integer type.
367 switch (cast<BuiltinType>(Node->getType().getCanonicalType())->getKind()) {
368 default: assert(0 && "Unexpected type for integer literal!");
369 case BuiltinType::Int: break; // no suffix.
370 case BuiltinType::UInt: OS << 'U'; break;
371 case BuiltinType::Long: OS << 'L'; break;
372 case BuiltinType::ULong: OS << "UL"; break;
373 case BuiltinType::LongLong: OS << "LL"; break;
374 case BuiltinType::ULongLong: OS << "ULL"; break;
375 }
376}
377void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner86e499d2007-08-01 00:23:58 +0000378 // FIXME: print value more precisely.
379 OS << Node->getValue();
Reid Spencer5f016e22007-07-11 17:01:13 +0000380}
381void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
382 if (Str->isWide()) OS << 'L';
383 OS << '"';
384
385 // FIXME: this doesn't print wstrings right.
386 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
387 switch (Str->getStrData()[i]) {
388 default: OS << Str->getStrData()[i]; break;
389 // Handle some common ones to make dumps prettier.
390 case '\\': OS << "\\\\"; break;
391 case '"': OS << "\\\""; break;
392 case '\n': OS << "\\n"; break;
393 case '\t': OS << "\\t"; break;
394 case '\a': OS << "\\a"; break;
395 case '\b': OS << "\\b"; break;
396 }
397 }
398 OS << '"';
399}
400void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
401 OS << "(";
402 PrintExpr(Node->getSubExpr());
403 OS << ")";
404}
405void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
406 if (!Node->isPostfix())
407 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
408 PrintExpr(Node->getSubExpr());
409
410 if (Node->isPostfix())
411 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
412
413}
414void StmtPrinter::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
415 OS << (Node->isSizeOf() ? "sizeof(" : "__alignof(");
416 OS << Node->getArgumentType().getAsString() << ")";
417}
418void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
419 PrintExpr(Node->getBase());
420 OS << "[";
421 PrintExpr(Node->getIdx());
422 OS << "]";
423}
424
425void StmtPrinter::VisitCallExpr(CallExpr *Call) {
426 PrintExpr(Call->getCallee());
427 OS << "(";
428 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
429 if (i) OS << ", ";
430 PrintExpr(Call->getArg(i));
431 }
432 OS << ")";
433}
434void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
435 PrintExpr(Node->getBase());
436 OS << (Node->isArrow() ? "->" : ".");
437
438 FieldDecl *Field = Node->getMemberDecl();
439 assert(Field && "MemberExpr should alway reference a field!");
440 OS << Field->getName();
441}
Chris Lattner6481a572007-08-03 17:31:20 +0000442void StmtPrinter::VisitOCUVectorElementExpr(OCUVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +0000443 PrintExpr(Node->getBase());
444 OS << ".";
445 OS << Node->getAccessor().getName();
446}
Reid Spencer5f016e22007-07-11 17:01:13 +0000447void StmtPrinter::VisitCastExpr(CastExpr *Node) {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000448 OS << "(" << Node->getType().getAsString() << ")";
Reid Spencer5f016e22007-07-11 17:01:13 +0000449 PrintExpr(Node->getSubExpr());
450}
Steve Naroffaff1edd2007-07-19 21:32:11 +0000451void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
452 OS << "(" << Node->getType().getAsString() << ")";
453 PrintExpr(Node->getInitializer());
454}
Steve Naroff49b45262007-07-13 16:58:59 +0000455void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroff90045e82007-07-13 23:32:42 +0000456 // No need to print anything, simply forward to the sub expression.
457 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +0000458}
Reid Spencer5f016e22007-07-11 17:01:13 +0000459void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
460 PrintExpr(Node->getLHS());
461 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
462 PrintExpr(Node->getRHS());
463}
464void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
465 PrintExpr(Node->getCond());
466 OS << " ? ";
467 PrintExpr(Node->getLHS());
468 OS << " : ";
469 PrintExpr(Node->getRHS());
470}
471
472// GNU extensions.
473
Chris Lattner6481a572007-08-03 17:31:20 +0000474void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000475 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000476}
477
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000478void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
479 OS << "(";
480 PrintRawCompoundStmt(E->getSubStmt());
481 OS << ")";
482}
483
Steve Naroffd34e9152007-08-01 22:05:33 +0000484void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
485 OS << "__builtin_types_compatible_p(";
486 OS << Node->getArgType1().getAsString() << ",";
487 OS << Node->getArgType2().getAsString() << ")";
488}
489
Steve Naroffd04fdd52007-08-03 21:21:27 +0000490void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
491 OS << "__builtin_choose_expr(";
492 PrintExpr(Node->getCond());
493 OS << ",";
494 PrintExpr(Node->getLHS());
495 OS << ",";
496 PrintExpr(Node->getRHS());
497 OS << ")";
498}
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000499
Reid Spencer5f016e22007-07-11 17:01:13 +0000500// C++
501
502void StmtPrinter::VisitCXXCastExpr(CXXCastExpr *Node) {
503 switch (Node->getOpcode()) {
504 default:
505 assert(0 && "Not a C++ cast expression");
506 abort();
507 case CXXCastExpr::ConstCast: OS << "const_cast<"; break;
508 case CXXCastExpr::DynamicCast: OS << "dynamic_cast<"; break;
509 case CXXCastExpr::ReinterpretCast: OS << "reinterpret_cast<"; break;
510 case CXXCastExpr::StaticCast: OS << "static_cast<"; break;
511 }
512
513 OS << Node->getDestType().getAsString() << ">(";
514 PrintExpr(Node->getSubExpr());
515 OS << ")";
516}
517
518void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
519 OS << (Node->getValue() ? "true" : "false");
520}
521
522
523//===----------------------------------------------------------------------===//
524// Stmt method implementations
525//===----------------------------------------------------------------------===//
526
527void Stmt::dump() const {
528 // FIXME: eliminate use of <iostream>
529 print(std::cerr);
530}
531
532void Stmt::print(std::ostream &OS) const {
533 if (this == 0) {
534 OS << "<NULL>";
535 return;
536 }
537
538 StmtPrinter P(OS);
539 const_cast<Stmt*>(this)->visit(P);
540}