blob: 875af790201a071aecf490a3b2129fae763e0848 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +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//
Chris Lattner95578782007-08-08 22:51:59 +000010// This file implements the Stmt::dumpPretty/Stmt::printPretty methods, which
11// pretty print the AST back out to C code.
Chris Lattner4b009652007-07-25 00:24:17 +000012//
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 <iostream>
21#include <iomanip>
22using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// StmtPrinter Visitor
26//===----------------------------------------------------------------------===//
27
28namespace {
Chris Lattner7ba21fa2007-08-21 04:04:25 +000029 class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor<StmtPrinter> {
Chris Lattner4b009652007-07-25 00:24:17 +000030 std::ostream &OS;
31 unsigned IndentLevel;
32 public:
33 StmtPrinter(std::ostream &os) : OS(os), IndentLevel(0) {}
34
35 void PrintStmt(Stmt *S, int SubIndent = 1) {
36 IndentLevel += SubIndent;
37 if (S && isa<Expr>(S)) {
38 // If this is an expr used in a stmt context, indent and newline it.
39 Indent();
Chris Lattner7ba21fa2007-08-21 04:04:25 +000040 Visit(S);
Chris Lattner4b009652007-07-25 00:24:17 +000041 OS << ";\n";
42 } else if (S) {
Chris Lattner7ba21fa2007-08-21 04:04:25 +000043 Visit(S);
Chris Lattner4b009652007-07-25 00:24:17 +000044 } else {
45 Indent() << "<<<NULL STATEMENT>>>\n";
46 }
47 IndentLevel -= SubIndent;
48 }
49
50 void PrintRawCompoundStmt(CompoundStmt *S);
51 void PrintRawDecl(Decl *D);
52 void PrintRawIfStmt(IfStmt *If);
53
54 void PrintExpr(Expr *E) {
55 if (E)
Chris Lattner7ba21fa2007-08-21 04:04:25 +000056 Visit(E);
Chris Lattner4b009652007-07-25 00:24:17 +000057 else
58 OS << "<null expr>";
59 }
60
61 std::ostream &Indent(int Delta = 0) const {
62 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
63 OS << " ";
64 return OS;
65 }
66
Chris Lattner7ba21fa2007-08-21 04:04:25 +000067 void VisitStmt(Stmt *Node);
Chris Lattner4b009652007-07-25 00:24:17 +000068#define STMT(N, CLASS, PARENT) \
Chris Lattner7ba21fa2007-08-21 04:04:25 +000069 void Visit##CLASS(CLASS *Node);
Chris Lattner4b009652007-07-25 00:24:17 +000070#include "clang/AST/StmtNodes.def"
71 };
72}
73
74//===----------------------------------------------------------------------===//
75// Stmt printing methods.
76//===----------------------------------------------------------------------===//
77
78void StmtPrinter::VisitStmt(Stmt *Node) {
79 Indent() << "<<unknown stmt type>>\n";
80}
81
82/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
83/// with no newline after the }.
84void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
85 OS << "{\n";
86 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
87 I != E; ++I)
88 PrintStmt(*I);
89
90 Indent() << "}";
91}
92
93void StmtPrinter::PrintRawDecl(Decl *D) {
94 // FIXME: Need to complete/beautify this... this code simply shows the
95 // nodes are where they need to be.
96 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
97 OS << "typedef " << localType->getUnderlyingType().getAsString();
98 OS << " " << localType->getName();
99 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
100 // Emit storage class for vardecls.
101 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
102 switch (V->getStorageClass()) {
103 default: assert(0 && "Unknown storage class!");
104 case VarDecl::None: break;
105 case VarDecl::Extern: OS << "extern "; break;
106 case VarDecl::Static: OS << "static "; break;
107 case VarDecl::Auto: OS << "auto "; break;
108 case VarDecl::Register: OS << "register "; break;
109 }
110 }
111
112 std::string Name = VD->getName();
113 VD->getType().getAsStringInternal(Name);
114 OS << Name;
115
116 // If this is a vardecl with an initializer, emit it.
117 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
118 if (V->getInit()) {
119 OS << " = ";
120 PrintExpr(V->getInit());
121 }
122 }
123 } else {
124 // FIXME: "struct x;"
125 assert(0 && "Unexpected decl");
126 }
127}
128
129
130void StmtPrinter::VisitNullStmt(NullStmt *Node) {
131 Indent() << ";\n";
132}
133
134void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
135 for (Decl *D = Node->getDecl(); D; D = D->getNextDeclarator()) {
136 Indent();
137 PrintRawDecl(D);
138 OS << ";\n";
139 }
140}
141
142void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
143 Indent();
144 PrintRawCompoundStmt(Node);
145 OS << "\n";
146}
147
148void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
149 Indent(-1) << "case ";
150 PrintExpr(Node->getLHS());
151 if (Node->getRHS()) {
152 OS << " ... ";
153 PrintExpr(Node->getRHS());
154 }
155 OS << ":\n";
156
157 PrintStmt(Node->getSubStmt(), 0);
158}
159
160void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
161 Indent(-1) << "default:\n";
162 PrintStmt(Node->getSubStmt(), 0);
163}
164
165void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
166 Indent(-1) << Node->getName() << ":\n";
167 PrintStmt(Node->getSubStmt(), 0);
168}
169
170void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
171 OS << "if ";
172 PrintExpr(If->getCond());
173
174 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
175 OS << ' ';
176 PrintRawCompoundStmt(CS);
177 OS << (If->getElse() ? ' ' : '\n');
178 } else {
179 OS << '\n';
180 PrintStmt(If->getThen());
181 if (If->getElse()) Indent();
182 }
183
184 if (Stmt *Else = If->getElse()) {
185 OS << "else";
186
187 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
188 OS << ' ';
189 PrintRawCompoundStmt(CS);
190 OS << '\n';
191 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
192 OS << ' ';
193 PrintRawIfStmt(ElseIf);
194 } else {
195 OS << '\n';
196 PrintStmt(If->getElse());
197 }
198 }
199}
200
201void StmtPrinter::VisitIfStmt(IfStmt *If) {
202 Indent();
203 PrintRawIfStmt(If);
204}
205
206void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
207 Indent() << "switch (";
208 PrintExpr(Node->getCond());
209 OS << ")";
210
211 // Pretty print compoundstmt bodies (very common).
212 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
213 OS << " ";
214 PrintRawCompoundStmt(CS);
215 OS << "\n";
216 } else {
217 OS << "\n";
218 PrintStmt(Node->getBody());
219 }
220}
221
222void StmtPrinter::VisitSwitchCase(SwitchCase*) {
223 assert(0 && "SwitchCase is an abstract class");
224}
225
226void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
227 Indent() << "while (";
228 PrintExpr(Node->getCond());
229 OS << ")\n";
230 PrintStmt(Node->getBody());
231}
232
233void StmtPrinter::VisitDoStmt(DoStmt *Node) {
234 Indent() << "do\n";
235 PrintStmt(Node->getBody());
236 Indent() << "while ";
237 PrintExpr(Node->getCond());
238 OS << ";\n";
239}
240
241void StmtPrinter::VisitForStmt(ForStmt *Node) {
242 Indent() << "for (";
243 if (Node->getInit()) {
244 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
245 PrintRawDecl(DS->getDecl());
246 else
247 PrintExpr(cast<Expr>(Node->getInit()));
248 }
249 OS << "; ";
250 if (Node->getCond())
251 PrintExpr(Node->getCond());
252 OS << "; ";
253 if (Node->getInc())
254 PrintExpr(Node->getInc());
255 OS << ")\n";
256 PrintStmt(Node->getBody());
257}
258
259void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
260 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
261}
262
263void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
264 Indent() << "goto *";
265 PrintExpr(Node->getTarget());
266 OS << ";\n";
267}
268
269void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
270 Indent() << "continue;\n";
271}
272
273void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
274 Indent() << "break;\n";
275}
276
277
278void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
279 Indent() << "return";
280 if (Node->getRetValue()) {
281 OS << " ";
282 PrintExpr(Node->getRetValue());
283 }
284 OS << ";\n";
285}
286
287//===----------------------------------------------------------------------===//
288// Expr printing methods.
289//===----------------------------------------------------------------------===//
290
291void StmtPrinter::VisitExpr(Expr *Node) {
292 OS << "<<unknown expr type>>";
293}
294
295void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
296 OS << Node->getDecl()->getName();
297}
298
299void StmtPrinter::VisitPreDefinedExpr(PreDefinedExpr *Node) {
300 switch (Node->getIdentType()) {
301 default:
302 assert(0 && "unknown case");
303 case PreDefinedExpr::Func:
304 OS << "__func__";
305 break;
306 case PreDefinedExpr::Function:
307 OS << "__FUNCTION__";
308 break;
309 case PreDefinedExpr::PrettyFunction:
310 OS << "__PRETTY_FUNCTION__";
311 break;
312 }
313}
314
315void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
316 // FIXME should print an L for wchar_t constants
317 unsigned value = Node->getValue();
318 switch (value) {
319 case '\\':
320 OS << "'\\\\'";
321 break;
322 case '\'':
323 OS << "'\\''";
324 break;
325 case '\a':
326 // TODO: K&R: the meaning of '\\a' is different in traditional C
327 OS << "'\\a'";
328 break;
329 case '\b':
330 OS << "'\\b'";
331 break;
332 // Nonstandard escape sequence.
333 /*case '\e':
334 OS << "'\\e'";
335 break;*/
336 case '\f':
337 OS << "'\\f'";
338 break;
339 case '\n':
340 OS << "'\\n'";
341 break;
342 case '\r':
343 OS << "'\\r'";
344 break;
345 case '\t':
346 OS << "'\\t'";
347 break;
348 case '\v':
349 OS << "'\\v'";
350 break;
351 default:
352 if (isprint(value) && value < 256) {
353 OS << "'" << (char)value << "'";
354 } else if (value < 256) {
355 OS << "'\\x" << std::hex << value << std::dec << "'";
356 } else {
357 // FIXME what to really do here?
358 OS << value;
359 }
360 }
361}
362
363void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
364 bool isSigned = Node->getType()->isSignedIntegerType();
365 OS << Node->getValue().toString(10, isSigned);
366
367 // Emit suffixes. Integer literals are always a builtin integer type.
368 switch (cast<BuiltinType>(Node->getType().getCanonicalType())->getKind()) {
369 default: assert(0 && "Unexpected type for integer literal!");
370 case BuiltinType::Int: break; // no suffix.
371 case BuiltinType::UInt: OS << 'U'; break;
372 case BuiltinType::Long: OS << 'L'; break;
373 case BuiltinType::ULong: OS << "UL"; break;
374 case BuiltinType::LongLong: OS << "LL"; break;
375 case BuiltinType::ULongLong: OS << "ULL"; break;
376 }
377}
378void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner6a390402007-08-01 00:23:58 +0000379 // FIXME: print value more precisely.
380 OS << Node->getValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000381}
Chris Lattner1de66eb2007-08-26 03:42:43 +0000382
383void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
384 PrintExpr(Node->getSubExpr());
385 OS << "i";
386}
387
Chris Lattner4b009652007-07-25 00:24:17 +0000388void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
389 if (Str->isWide()) OS << 'L';
390 OS << '"';
391
392 // FIXME: this doesn't print wstrings right.
393 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
394 switch (Str->getStrData()[i]) {
395 default: OS << Str->getStrData()[i]; break;
396 // Handle some common ones to make dumps prettier.
397 case '\\': OS << "\\\\"; break;
398 case '"': OS << "\\\""; break;
399 case '\n': OS << "\\n"; break;
400 case '\t': OS << "\\t"; break;
401 case '\a': OS << "\\a"; break;
402 case '\b': OS << "\\b"; break;
403 }
404 }
405 OS << '"';
406}
407void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
408 OS << "(";
409 PrintExpr(Node->getSubExpr());
410 OS << ")";
411}
412void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner2b97b092007-08-23 21:46:40 +0000413 if (!Node->isPostfix()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000414 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner2b97b092007-08-23 21:46:40 +0000415
416 // Print a space if this is an "identifier operator" like sizeof or __real.
417 switch (Node->getOpcode()) {
418 default: break;
419 case UnaryOperator::SizeOf:
420 case UnaryOperator::AlignOf:
421 case UnaryOperator::Real:
422 case UnaryOperator::Imag:
423 case UnaryOperator::Extension:
424 OS << ' ';
425 break;
426 }
427 }
Chris Lattner4b009652007-07-25 00:24:17 +0000428 PrintExpr(Node->getSubExpr());
429
430 if (Node->isPostfix())
431 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
432
433}
434void StmtPrinter::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
435 OS << (Node->isSizeOf() ? "sizeof(" : "__alignof(");
436 OS << Node->getArgumentType().getAsString() << ")";
437}
438void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000439 PrintExpr(Node->getLHS());
Chris Lattner4b009652007-07-25 00:24:17 +0000440 OS << "[";
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000441 PrintExpr(Node->getRHS());
Chris Lattner4b009652007-07-25 00:24:17 +0000442 OS << "]";
443}
444
445void StmtPrinter::VisitCallExpr(CallExpr *Call) {
446 PrintExpr(Call->getCallee());
447 OS << "(";
448 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
449 if (i) OS << ", ";
450 PrintExpr(Call->getArg(i));
451 }
452 OS << ")";
453}
454void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
455 PrintExpr(Node->getBase());
456 OS << (Node->isArrow() ? "->" : ".");
457
458 FieldDecl *Field = Node->getMemberDecl();
459 assert(Field && "MemberExpr should alway reference a field!");
460 OS << Field->getName();
461}
Chris Lattnera0d03a72007-08-03 17:31:20 +0000462void StmtPrinter::VisitOCUVectorElementExpr(OCUVectorElementExpr *Node) {
Steve Naroffc11705f2007-07-28 23:10:27 +0000463 PrintExpr(Node->getBase());
464 OS << ".";
465 OS << Node->getAccessor().getName();
466}
Chris Lattner4b009652007-07-25 00:24:17 +0000467void StmtPrinter::VisitCastExpr(CastExpr *Node) {
468 OS << "(" << Node->getType().getAsString() << ")";
469 PrintExpr(Node->getSubExpr());
470}
471void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
472 OS << "(" << Node->getType().getAsString() << ")";
473 PrintExpr(Node->getInitializer());
474}
475void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
476 // No need to print anything, simply forward to the sub expression.
477 PrintExpr(Node->getSubExpr());
478}
479void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
480 PrintExpr(Node->getLHS());
481 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
482 PrintExpr(Node->getRHS());
483}
Chris Lattner06078d22007-08-25 02:00:02 +0000484void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
485 PrintExpr(Node->getLHS());
486 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
487 PrintExpr(Node->getRHS());
488}
Chris Lattner4b009652007-07-25 00:24:17 +0000489void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
490 PrintExpr(Node->getCond());
491 OS << " ? ";
492 PrintExpr(Node->getLHS());
493 OS << " : ";
494 PrintExpr(Node->getRHS());
495}
496
497// GNU extensions.
498
Chris Lattnera0d03a72007-08-03 17:31:20 +0000499void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattner4b009652007-07-25 00:24:17 +0000500 OS << "&&" << Node->getLabel()->getName();
501}
502
503void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
504 OS << "(";
505 PrintRawCompoundStmt(E->getSubStmt());
506 OS << ")";
507}
508
Steve Naroff63bad2d2007-08-01 22:05:33 +0000509void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
510 OS << "__builtin_types_compatible_p(";
511 OS << Node->getArgType1().getAsString() << ",";
512 OS << Node->getArgType2().getAsString() << ")";
513}
514
Steve Naroff93c53012007-08-03 21:21:27 +0000515void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
516 OS << "__builtin_choose_expr(";
517 PrintExpr(Node->getCond());
Chris Lattner44fcf4f2007-08-04 00:20:15 +0000518 OS << ", ";
Steve Naroff93c53012007-08-03 21:21:27 +0000519 PrintExpr(Node->getLHS());
Chris Lattner44fcf4f2007-08-04 00:20:15 +0000520 OS << ", ";
Steve Naroff93c53012007-08-03 21:21:27 +0000521 PrintExpr(Node->getRHS());
522 OS << ")";
523}
Chris Lattner4b009652007-07-25 00:24:17 +0000524
525// C++
526
527void StmtPrinter::VisitCXXCastExpr(CXXCastExpr *Node) {
Chris Lattnerac485c12007-08-09 17:34:19 +0000528 OS << CXXCastExpr::getOpcodeStr(Node->getOpcode()) << '<';
Chris Lattner4b009652007-07-25 00:24:17 +0000529 OS << Node->getDestType().getAsString() << ">(";
530 PrintExpr(Node->getSubExpr());
531 OS << ")";
532}
533
534void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
535 OS << (Node->getValue() ? "true" : "false");
536}
537
Anders Carlssona66cad42007-08-21 17:43:55 +0000538// Obj-C
539
540void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
541 OS << "@";
542 VisitStringLiteral(Node->getString());
543}
Chris Lattner4b009652007-07-25 00:24:17 +0000544
Anders Carlsson8be1d402007-08-22 15:14:15 +0000545void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
546 OS << "@encode(";
547 OS << Node->getEncodedType().getAsString() << ")";
548}
549
Chris Lattner4b009652007-07-25 00:24:17 +0000550//===----------------------------------------------------------------------===//
551// Stmt method implementations
552//===----------------------------------------------------------------------===//
553
Chris Lattner95578782007-08-08 22:51:59 +0000554void Stmt::dumpPretty() const {
Chris Lattner4b009652007-07-25 00:24:17 +0000555 // FIXME: eliminate use of <iostream>
Chris Lattner95578782007-08-08 22:51:59 +0000556 printPretty(std::cerr);
Chris Lattner4b009652007-07-25 00:24:17 +0000557}
558
Chris Lattner95578782007-08-08 22:51:59 +0000559void Stmt::printPretty(std::ostream &OS) const {
Chris Lattner4b009652007-07-25 00:24:17 +0000560 if (this == 0) {
561 OS << "<NULL>";
562 return;
563 }
564
565 StmtPrinter P(OS);
Chris Lattner7ba21fa2007-08-21 04:04:25 +0000566 P.Visit(const_cast<Stmt*>(this));
Chris Lattner4b009652007-07-25 00:24:17 +0000567}