blob: 11dd2ef7677b96d922c1457bb1da8953dd500cb2 [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//
Chris Lattner6000dac2007-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.
Reid Spencer5f016e22007-07-11 17:01:13 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "clang/AST/StmtVisitor.h"
16#include "clang/AST/Decl.h"
17#include "clang/AST/ExprCXX.h"
Ted Kremenek42a509f2007-08-31 21:30:12 +000018#include "clang/AST/PrettyPrinter.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/Lex/IdentifierTable.h"
20#include "llvm/Support/Compiler.h"
21#include <iostream>
Chris Lattnerb0a721a2007-07-13 05:18:11 +000022#include <iomanip>
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// StmtPrinter Visitor
27//===----------------------------------------------------------------------===//
28
29namespace {
Chris Lattnerc5598cb2007-08-21 04:04:25 +000030 class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor<StmtPrinter> {
Reid Spencer5f016e22007-07-11 17:01:13 +000031 std::ostream &OS;
32 unsigned IndentLevel;
Ted Kremenek42a509f2007-08-31 21:30:12 +000033 clang::PrinterHelper* Helper;
Reid Spencer5f016e22007-07-11 17:01:13 +000034 public:
Ted Kremenek42a509f2007-08-31 21:30:12 +000035 StmtPrinter(std::ostream &os, PrinterHelper* helper) :
36 OS(os), IndentLevel(0), Helper(helper) {}
Reid Spencer5f016e22007-07-11 17:01:13 +000037
38 void PrintStmt(Stmt *S, int SubIndent = 1) {
39 IndentLevel += SubIndent;
40 if (S && isa<Expr>(S)) {
41 // If this is an expr used in a stmt context, indent and newline it.
42 Indent();
Chris Lattnerc5598cb2007-08-21 04:04:25 +000043 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000044 OS << ";\n";
45 } else if (S) {
Chris Lattnerc5598cb2007-08-21 04:04:25 +000046 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000047 } else {
48 Indent() << "<<<NULL STATEMENT>>>\n";
49 }
50 IndentLevel -= SubIndent;
51 }
52
53 void PrintRawCompoundStmt(CompoundStmt *S);
54 void PrintRawDecl(Decl *D);
55 void PrintRawIfStmt(IfStmt *If);
56
57 void PrintExpr(Expr *E) {
58 if (E)
Chris Lattnerc5598cb2007-08-21 04:04:25 +000059 Visit(E);
Reid Spencer5f016e22007-07-11 17:01:13 +000060 else
61 OS << "<null expr>";
62 }
63
64 std::ostream &Indent(int Delta = 0) const {
65 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
66 OS << " ";
67 return OS;
68 }
69
Chris Lattner704fe352007-08-30 17:59:59 +000070 bool PrintOffsetOfDesignator(Expr *E);
71 void VisitUnaryOffsetOf(UnaryOperator *Node);
72
Ted Kremenek42a509f2007-08-31 21:30:12 +000073 void Visit(Stmt* S) {
74 if (Helper && Helper->handledStmt(S,OS))
75 return;
76 else StmtVisitor<StmtPrinter>::Visit(S);
77 }
78
Chris Lattnerc5598cb2007-08-21 04:04:25 +000079 void VisitStmt(Stmt *Node);
Reid Spencer5f016e22007-07-11 17:01:13 +000080#define STMT(N, CLASS, PARENT) \
Chris Lattnerc5598cb2007-08-21 04:04:25 +000081 void Visit##CLASS(CLASS *Node);
Reid Spencer5f016e22007-07-11 17:01:13 +000082#include "clang/AST/StmtNodes.def"
83 };
84}
85
86//===----------------------------------------------------------------------===//
87// Stmt printing methods.
88//===----------------------------------------------------------------------===//
89
90void StmtPrinter::VisitStmt(Stmt *Node) {
91 Indent() << "<<unknown stmt type>>\n";
92}
93
94/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
95/// with no newline after the }.
96void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
97 OS << "{\n";
98 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
99 I != E; ++I)
100 PrintStmt(*I);
101
102 Indent() << "}";
103}
104
105void StmtPrinter::PrintRawDecl(Decl *D) {
106 // FIXME: Need to complete/beautify this... this code simply shows the
107 // nodes are where they need to be.
108 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
109 OS << "typedef " << localType->getUnderlyingType().getAsString();
110 OS << " " << localType->getName();
111 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
112 // Emit storage class for vardecls.
113 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
114 switch (V->getStorageClass()) {
115 default: assert(0 && "Unknown storage class!");
116 case VarDecl::None: break;
117 case VarDecl::Extern: OS << "extern "; break;
118 case VarDecl::Static: OS << "static "; break;
119 case VarDecl::Auto: OS << "auto "; break;
120 case VarDecl::Register: OS << "register "; break;
121 }
122 }
123
124 std::string Name = VD->getName();
125 VD->getType().getAsStringInternal(Name);
126 OS << Name;
127
Chris Lattner24c39902007-07-12 00:36:32 +0000128 // If this is a vardecl with an initializer, emit it.
129 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
130 if (V->getInit()) {
131 OS << " = ";
132 PrintExpr(V->getInit());
133 }
134 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 } else {
136 // FIXME: "struct x;"
137 assert(0 && "Unexpected decl");
138 }
139}
140
141
142void StmtPrinter::VisitNullStmt(NullStmt *Node) {
143 Indent() << ";\n";
144}
145
146void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Steve Naroff94745042007-09-13 23:52:58 +0000147 for (ScopedDecl *D = Node->getDecl(); D; D = D->getNextDeclarator()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 Indent();
149 PrintRawDecl(D);
150 OS << ";\n";
151 }
152}
153
154void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
155 Indent();
156 PrintRawCompoundStmt(Node);
157 OS << "\n";
158}
159
160void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
161 Indent(-1) << "case ";
162 PrintExpr(Node->getLHS());
163 if (Node->getRHS()) {
164 OS << " ... ";
165 PrintExpr(Node->getRHS());
166 }
167 OS << ":\n";
168
169 PrintStmt(Node->getSubStmt(), 0);
170}
171
172void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
173 Indent(-1) << "default:\n";
174 PrintStmt(Node->getSubStmt(), 0);
175}
176
177void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
178 Indent(-1) << Node->getName() << ":\n";
179 PrintStmt(Node->getSubStmt(), 0);
180}
181
182void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
183 OS << "if ";
184 PrintExpr(If->getCond());
185
186 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
187 OS << ' ';
188 PrintRawCompoundStmt(CS);
189 OS << (If->getElse() ? ' ' : '\n');
190 } else {
191 OS << '\n';
192 PrintStmt(If->getThen());
193 if (If->getElse()) Indent();
194 }
195
196 if (Stmt *Else = If->getElse()) {
197 OS << "else";
198
199 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
200 OS << ' ';
201 PrintRawCompoundStmt(CS);
202 OS << '\n';
203 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
204 OS << ' ';
205 PrintRawIfStmt(ElseIf);
206 } else {
207 OS << '\n';
208 PrintStmt(If->getElse());
209 }
210 }
211}
212
213void StmtPrinter::VisitIfStmt(IfStmt *If) {
214 Indent();
215 PrintRawIfStmt(If);
216}
217
218void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
219 Indent() << "switch (";
220 PrintExpr(Node->getCond());
221 OS << ")";
222
223 // Pretty print compoundstmt bodies (very common).
224 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
225 OS << " ";
226 PrintRawCompoundStmt(CS);
227 OS << "\n";
228 } else {
229 OS << "\n";
230 PrintStmt(Node->getBody());
231 }
232}
233
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000234void StmtPrinter::VisitSwitchCase(SwitchCase*) {
235 assert(0 && "SwitchCase is an abstract class");
236}
237
Reid Spencer5f016e22007-07-11 17:01:13 +0000238void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
239 Indent() << "while (";
240 PrintExpr(Node->getCond());
241 OS << ")\n";
242 PrintStmt(Node->getBody());
243}
244
245void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner8bdcc472007-09-15 21:49:37 +0000246 Indent() << "do ";
247 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
248 PrintRawCompoundStmt(CS);
249 OS << " ";
250 } else {
251 OS << "\n";
252 PrintStmt(Node->getBody());
253 Indent();
254 }
255
256 OS << "while ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 PrintExpr(Node->getCond());
258 OS << ";\n";
259}
260
261void StmtPrinter::VisitForStmt(ForStmt *Node) {
262 Indent() << "for (";
263 if (Node->getInit()) {
264 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
265 PrintRawDecl(DS->getDecl());
266 else
267 PrintExpr(cast<Expr>(Node->getInit()));
268 }
Chris Lattner8bdcc472007-09-15 21:49:37 +0000269 OS << ";";
270 if (Node->getCond()) {
271 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000272 PrintExpr(Node->getCond());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000273 }
274 OS << ";";
275 if (Node->getInc()) {
276 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000277 PrintExpr(Node->getInc());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000278 }
279 OS << ") ";
280
281 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
282 PrintRawCompoundStmt(CS);
283 OS << "\n";
284 } else {
285 OS << "\n";
286 PrintStmt(Node->getBody());
287 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000288}
289
290void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
291 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
292}
293
294void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
295 Indent() << "goto *";
296 PrintExpr(Node->getTarget());
297 OS << ";\n";
298}
299
300void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
301 Indent() << "continue;\n";
302}
303
304void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
305 Indent() << "break;\n";
306}
307
308
309void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
310 Indent() << "return";
311 if (Node->getRetValue()) {
312 OS << " ";
313 PrintExpr(Node->getRetValue());
314 }
315 OS << ";\n";
316}
317
318//===----------------------------------------------------------------------===//
319// Expr printing methods.
320//===----------------------------------------------------------------------===//
321
322void StmtPrinter::VisitExpr(Expr *Node) {
323 OS << "<<unknown expr type>>";
324}
325
326void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
327 OS << Node->getDecl()->getName();
328}
329
Anders Carlsson22742662007-07-21 05:21:51 +0000330void StmtPrinter::VisitPreDefinedExpr(PreDefinedExpr *Node) {
331 switch (Node->getIdentType()) {
332 default:
333 assert(0 && "unknown case");
334 case PreDefinedExpr::Func:
335 OS << "__func__";
336 break;
337 case PreDefinedExpr::Function:
338 OS << "__FUNCTION__";
339 break;
340 case PreDefinedExpr::PrettyFunction:
341 OS << "__PRETTY_FUNCTION__";
342 break;
343 }
344}
345
Reid Spencer5f016e22007-07-11 17:01:13 +0000346void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000347 // FIXME should print an L for wchar_t constants
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000348 unsigned value = Node->getValue();
Chris Lattner8bf9f072007-07-13 23:58:20 +0000349 switch (value) {
350 case '\\':
351 OS << "'\\\\'";
352 break;
353 case '\'':
354 OS << "'\\''";
355 break;
356 case '\a':
357 // TODO: K&R: the meaning of '\\a' is different in traditional C
358 OS << "'\\a'";
359 break;
360 case '\b':
361 OS << "'\\b'";
362 break;
363 // Nonstandard escape sequence.
364 /*case '\e':
365 OS << "'\\e'";
366 break;*/
367 case '\f':
368 OS << "'\\f'";
369 break;
370 case '\n':
371 OS << "'\\n'";
372 break;
373 case '\r':
374 OS << "'\\r'";
375 break;
376 case '\t':
377 OS << "'\\t'";
378 break;
379 case '\v':
380 OS << "'\\v'";
381 break;
382 default:
383 if (isprint(value) && value < 256) {
384 OS << "'" << (char)value << "'";
385 } else if (value < 256) {
386 OS << "'\\x" << std::hex << value << std::dec << "'";
387 } else {
388 // FIXME what to really do here?
389 OS << value;
390 }
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000391 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000392}
393
394void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
395 bool isSigned = Node->getType()->isSignedIntegerType();
396 OS << Node->getValue().toString(10, isSigned);
397
398 // Emit suffixes. Integer literals are always a builtin integer type.
399 switch (cast<BuiltinType>(Node->getType().getCanonicalType())->getKind()) {
400 default: assert(0 && "Unexpected type for integer literal!");
401 case BuiltinType::Int: break; // no suffix.
402 case BuiltinType::UInt: OS << 'U'; break;
403 case BuiltinType::Long: OS << 'L'; break;
404 case BuiltinType::ULong: OS << "UL"; break;
405 case BuiltinType::LongLong: OS << "LL"; break;
406 case BuiltinType::ULongLong: OS << "ULL"; break;
407 }
408}
409void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner86e499d2007-08-01 00:23:58 +0000410 // FIXME: print value more precisely.
411 OS << Node->getValue();
Reid Spencer5f016e22007-07-11 17:01:13 +0000412}
Chris Lattner5d661452007-08-26 03:42:43 +0000413
414void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
415 PrintExpr(Node->getSubExpr());
416 OS << "i";
417}
418
Reid Spencer5f016e22007-07-11 17:01:13 +0000419void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
420 if (Str->isWide()) OS << 'L';
421 OS << '"';
422
423 // FIXME: this doesn't print wstrings right.
424 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
425 switch (Str->getStrData()[i]) {
426 default: OS << Str->getStrData()[i]; break;
427 // Handle some common ones to make dumps prettier.
428 case '\\': OS << "\\\\"; break;
429 case '"': OS << "\\\""; break;
430 case '\n': OS << "\\n"; break;
431 case '\t': OS << "\\t"; break;
432 case '\a': OS << "\\a"; break;
433 case '\b': OS << "\\b"; break;
434 }
435 }
436 OS << '"';
437}
438void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
439 OS << "(";
440 PrintExpr(Node->getSubExpr());
441 OS << ")";
442}
443void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +0000444 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000445 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner296bf192007-08-23 21:46:40 +0000446
447 // Print a space if this is an "identifier operator" like sizeof or __real.
448 switch (Node->getOpcode()) {
449 default: break;
450 case UnaryOperator::SizeOf:
451 case UnaryOperator::AlignOf:
452 case UnaryOperator::Real:
453 case UnaryOperator::Imag:
454 case UnaryOperator::Extension:
455 OS << ' ';
456 break;
457 }
458 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000459 PrintExpr(Node->getSubExpr());
460
461 if (Node->isPostfix())
462 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +0000463}
Chris Lattner704fe352007-08-30 17:59:59 +0000464
465bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
466 if (isa<CompoundLiteralExpr>(E)) {
467 // Base case, print the type and comma.
468 OS << E->getType().getAsString() << ", ";
469 return true;
470 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
471 PrintOffsetOfDesignator(ASE->getLHS());
472 OS << "[";
473 PrintExpr(ASE->getRHS());
474 OS << "]";
475 return false;
476 } else {
477 MemberExpr *ME = cast<MemberExpr>(E);
478 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
479 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getName();
480 return false;
481 }
482}
483
484void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
485 OS << "__builtin_offsetof(";
486 PrintOffsetOfDesignator(Node->getSubExpr());
487 OS << ")";
488}
489
Reid Spencer5f016e22007-07-11 17:01:13 +0000490void StmtPrinter::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
491 OS << (Node->isSizeOf() ? "sizeof(" : "__alignof(");
492 OS << Node->getArgumentType().getAsString() << ")";
493}
494void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +0000495 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000496 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +0000497 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000498 OS << "]";
499}
500
501void StmtPrinter::VisitCallExpr(CallExpr *Call) {
502 PrintExpr(Call->getCallee());
503 OS << "(";
504 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
505 if (i) OS << ", ";
506 PrintExpr(Call->getArg(i));
507 }
508 OS << ")";
509}
510void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
511 PrintExpr(Node->getBase());
512 OS << (Node->isArrow() ? "->" : ".");
513
514 FieldDecl *Field = Node->getMemberDecl();
515 assert(Field && "MemberExpr should alway reference a field!");
516 OS << Field->getName();
517}
Chris Lattner6481a572007-08-03 17:31:20 +0000518void StmtPrinter::VisitOCUVectorElementExpr(OCUVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +0000519 PrintExpr(Node->getBase());
520 OS << ".";
521 OS << Node->getAccessor().getName();
522}
Reid Spencer5f016e22007-07-11 17:01:13 +0000523void StmtPrinter::VisitCastExpr(CastExpr *Node) {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000524 OS << "(" << Node->getType().getAsString() << ")";
Reid Spencer5f016e22007-07-11 17:01:13 +0000525 PrintExpr(Node->getSubExpr());
526}
Steve Naroffaff1edd2007-07-19 21:32:11 +0000527void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
528 OS << "(" << Node->getType().getAsString() << ")";
529 PrintExpr(Node->getInitializer());
530}
Steve Naroff49b45262007-07-13 16:58:59 +0000531void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroff90045e82007-07-13 23:32:42 +0000532 // No need to print anything, simply forward to the sub expression.
533 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +0000534}
Reid Spencer5f016e22007-07-11 17:01:13 +0000535void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
536 PrintExpr(Node->getLHS());
537 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
538 PrintExpr(Node->getRHS());
539}
Chris Lattnereb14fe82007-08-25 02:00:02 +0000540void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
541 PrintExpr(Node->getLHS());
542 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
543 PrintExpr(Node->getRHS());
544}
Reid Spencer5f016e22007-07-11 17:01:13 +0000545void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
546 PrintExpr(Node->getCond());
547 OS << " ? ";
548 PrintExpr(Node->getLHS());
549 OS << " : ";
550 PrintExpr(Node->getRHS());
551}
552
553// GNU extensions.
554
Chris Lattner6481a572007-08-03 17:31:20 +0000555void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000556 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000557}
558
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000559void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
560 OS << "(";
561 PrintRawCompoundStmt(E->getSubStmt());
562 OS << ")";
563}
564
Steve Naroffd34e9152007-08-01 22:05:33 +0000565void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
566 OS << "__builtin_types_compatible_p(";
567 OS << Node->getArgType1().getAsString() << ",";
568 OS << Node->getArgType2().getAsString() << ")";
569}
570
Steve Naroffd04fdd52007-08-03 21:21:27 +0000571void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
572 OS << "__builtin_choose_expr(";
573 PrintExpr(Node->getCond());
Chris Lattner94f05e32007-08-04 00:20:15 +0000574 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000575 PrintExpr(Node->getLHS());
Chris Lattner94f05e32007-08-04 00:20:15 +0000576 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000577 PrintExpr(Node->getRHS());
578 OS << ")";
579}
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000580
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000581void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
582 OS << "{ ";
583 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
584 if (i) OS << ", ";
585 PrintExpr(Node->getInit(i));
586 }
587 OS << " }";
588}
589
Reid Spencer5f016e22007-07-11 17:01:13 +0000590// C++
591
592void StmtPrinter::VisitCXXCastExpr(CXXCastExpr *Node) {
Chris Lattner36460ee2007-08-09 17:34:19 +0000593 OS << CXXCastExpr::getOpcodeStr(Node->getOpcode()) << '<';
Reid Spencer5f016e22007-07-11 17:01:13 +0000594 OS << Node->getDestType().getAsString() << ">(";
595 PrintExpr(Node->getSubExpr());
596 OS << ")";
597}
598
599void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
600 OS << (Node->getValue() ? "true" : "false");
601}
602
Anders Carlsson55085182007-08-21 17:43:55 +0000603// Obj-C
604
605void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
606 OS << "@";
607 VisitStringLiteral(Node->getString());
608}
Reid Spencer5f016e22007-07-11 17:01:13 +0000609
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000610void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
611 OS << "@encode(";
612 OS << Node->getEncodedType().getAsString() << ")";
613}
614
Steve Naroff563477d2007-09-18 23:55:05 +0000615void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
616 OS << "[";
617 PrintExpr(Mess->getReceiver());
618 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
619 // FIXME: get/print keyword...
620 PrintExpr(Mess->getArg(i));
621 }
622 OS << "]";
623}
624
625
Reid Spencer5f016e22007-07-11 17:01:13 +0000626//===----------------------------------------------------------------------===//
627// Stmt method implementations
628//===----------------------------------------------------------------------===//
629
Chris Lattner6000dac2007-08-08 22:51:59 +0000630void Stmt::dumpPretty() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000631 // FIXME: eliminate use of <iostream>
Chris Lattner6000dac2007-08-08 22:51:59 +0000632 printPretty(std::cerr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000633}
634
Ted Kremenek42a509f2007-08-31 21:30:12 +0000635void Stmt::printPretty(std::ostream &OS, PrinterHelper* Helper) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000636 if (this == 0) {
637 OS << "<NULL>";
638 return;
639 }
640
Ted Kremenek42a509f2007-08-31 21:30:12 +0000641 StmtPrinter P(OS, Helper);
Chris Lattnerc5598cb2007-08-21 04:04:25 +0000642 P.Visit(const_cast<Stmt*>(this));
Reid Spencer5f016e22007-07-11 17:01:13 +0000643}
Ted Kremenek42a509f2007-08-31 21:30:12 +0000644
645//===----------------------------------------------------------------------===//
646// PrinterHelper
647//===----------------------------------------------------------------------===//
648
649// Implement virtual destructor.
Gabor Greif84675832007-09-11 15:32:40 +0000650PrinterHelper::~PrinterHelper() {}