blob: ac6da0638d9bd5456c46d7e1b7318d9b50f57166 [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"
Ted Kremenek10364dd2007-10-17 18:36:42 +000017#include "clang/AST/DeclObjC.h"
Chris Lattner4b009652007-07-25 00:24:17 +000018#include "clang/AST/ExprCXX.h"
Ted Kremenek08176a52007-08-31 21:30:12 +000019#include "clang/AST/PrettyPrinter.h"
Chris Lattner2fd1c652007-10-07 08:58:51 +000020#include "clang/Basic/IdentifierTable.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021#include "llvm/Support/Compiler.h"
22#include <iostream>
23#include <iomanip>
24using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// StmtPrinter Visitor
28//===----------------------------------------------------------------------===//
29
30namespace {
Chris Lattner7ba21fa2007-08-21 04:04:25 +000031 class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor<StmtPrinter> {
Chris Lattner4b009652007-07-25 00:24:17 +000032 std::ostream &OS;
33 unsigned IndentLevel;
Ted Kremenek08176a52007-08-31 21:30:12 +000034 clang::PrinterHelper* Helper;
Chris Lattner4b009652007-07-25 00:24:17 +000035 public:
Ted Kremenek08176a52007-08-31 21:30:12 +000036 StmtPrinter(std::ostream &os, PrinterHelper* helper) :
37 OS(os), IndentLevel(0), Helper(helper) {}
Chris Lattner4b009652007-07-25 00:24:17 +000038
39 void PrintStmt(Stmt *S, int SubIndent = 1) {
40 IndentLevel += SubIndent;
41 if (S && isa<Expr>(S)) {
42 // If this is an expr used in a stmt context, indent and newline it.
43 Indent();
Chris Lattner7ba21fa2007-08-21 04:04:25 +000044 Visit(S);
Chris Lattner4b009652007-07-25 00:24:17 +000045 OS << ";\n";
46 } else if (S) {
Chris Lattner7ba21fa2007-08-21 04:04:25 +000047 Visit(S);
Chris Lattner4b009652007-07-25 00:24:17 +000048 } else {
49 Indent() << "<<<NULL STATEMENT>>>\n";
50 }
51 IndentLevel -= SubIndent;
52 }
53
54 void PrintRawCompoundStmt(CompoundStmt *S);
55 void PrintRawDecl(Decl *D);
56 void PrintRawIfStmt(IfStmt *If);
57
58 void PrintExpr(Expr *E) {
59 if (E)
Chris Lattner7ba21fa2007-08-21 04:04:25 +000060 Visit(E);
Chris Lattner4b009652007-07-25 00:24:17 +000061 else
62 OS << "<null expr>";
63 }
64
65 std::ostream &Indent(int Delta = 0) const {
66 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
67 OS << " ";
68 return OS;
69 }
70
Chris Lattner2af6a802007-08-30 17:59:59 +000071 bool PrintOffsetOfDesignator(Expr *E);
72 void VisitUnaryOffsetOf(UnaryOperator *Node);
73
Ted Kremenek08176a52007-08-31 21:30:12 +000074 void Visit(Stmt* S) {
75 if (Helper && Helper->handledStmt(S,OS))
76 return;
77 else StmtVisitor<StmtPrinter>::Visit(S);
78 }
79
Chris Lattner7ba21fa2007-08-21 04:04:25 +000080 void VisitStmt(Stmt *Node);
Chris Lattner4b009652007-07-25 00:24:17 +000081#define STMT(N, CLASS, PARENT) \
Chris Lattner7ba21fa2007-08-21 04:04:25 +000082 void Visit##CLASS(CLASS *Node);
Chris Lattner4b009652007-07-25 00:24:17 +000083#include "clang/AST/StmtNodes.def"
84 };
85}
86
87//===----------------------------------------------------------------------===//
88// Stmt printing methods.
89//===----------------------------------------------------------------------===//
90
91void StmtPrinter::VisitStmt(Stmt *Node) {
92 Indent() << "<<unknown stmt type>>\n";
93}
94
95/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
96/// with no newline after the }.
97void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
98 OS << "{\n";
99 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
100 I != E; ++I)
101 PrintStmt(*I);
102
103 Indent() << "}";
104}
105
106void StmtPrinter::PrintRawDecl(Decl *D) {
107 // FIXME: Need to complete/beautify this... this code simply shows the
108 // nodes are where they need to be.
109 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
110 OS << "typedef " << localType->getUnderlyingType().getAsString();
111 OS << " " << localType->getName();
112 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
113 // Emit storage class for vardecls.
114 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
115 switch (V->getStorageClass()) {
116 default: assert(0 && "Unknown storage class!");
117 case VarDecl::None: break;
118 case VarDecl::Extern: OS << "extern "; break;
119 case VarDecl::Static: OS << "static "; break;
120 case VarDecl::Auto: OS << "auto "; break;
121 case VarDecl::Register: OS << "register "; break;
122 }
123 }
124
125 std::string Name = VD->getName();
126 VD->getType().getAsStringInternal(Name);
127 OS << Name;
128
129 // If this is a vardecl with an initializer, emit it.
130 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
131 if (V->getInit()) {
132 OS << " = ";
133 PrintExpr(V->getInit());
134 }
135 }
136 } else {
137 // FIXME: "struct x;"
138 assert(0 && "Unexpected decl");
139 }
140}
141
142
143void StmtPrinter::VisitNullStmt(NullStmt *Node) {
144 Indent() << ";\n";
145}
146
147void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Steve Naroff2591e1b2007-09-13 23:52:58 +0000148 for (ScopedDecl *D = Node->getDecl(); D; D = D->getNextDeclarator()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000149 Indent();
150 PrintRawDecl(D);
151 OS << ";\n";
152 }
153}
154
155void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
156 Indent();
157 PrintRawCompoundStmt(Node);
158 OS << "\n";
159}
160
161void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
162 Indent(-1) << "case ";
163 PrintExpr(Node->getLHS());
164 if (Node->getRHS()) {
165 OS << " ... ";
166 PrintExpr(Node->getRHS());
167 }
168 OS << ":\n";
169
170 PrintStmt(Node->getSubStmt(), 0);
171}
172
173void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
174 Indent(-1) << "default:\n";
175 PrintStmt(Node->getSubStmt(), 0);
176}
177
178void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
179 Indent(-1) << Node->getName() << ":\n";
180 PrintStmt(Node->getSubStmt(), 0);
181}
182
183void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
184 OS << "if ";
185 PrintExpr(If->getCond());
186
187 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
188 OS << ' ';
189 PrintRawCompoundStmt(CS);
190 OS << (If->getElse() ? ' ' : '\n');
191 } else {
192 OS << '\n';
193 PrintStmt(If->getThen());
194 if (If->getElse()) Indent();
195 }
196
197 if (Stmt *Else = If->getElse()) {
198 OS << "else";
199
200 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
201 OS << ' ';
202 PrintRawCompoundStmt(CS);
203 OS << '\n';
204 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
205 OS << ' ';
206 PrintRawIfStmt(ElseIf);
207 } else {
208 OS << '\n';
209 PrintStmt(If->getElse());
210 }
211 }
212}
213
214void StmtPrinter::VisitIfStmt(IfStmt *If) {
215 Indent();
216 PrintRawIfStmt(If);
217}
218
219void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
220 Indent() << "switch (";
221 PrintExpr(Node->getCond());
222 OS << ")";
223
224 // Pretty print compoundstmt bodies (very common).
225 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
226 OS << " ";
227 PrintRawCompoundStmt(CS);
228 OS << "\n";
229 } else {
230 OS << "\n";
231 PrintStmt(Node->getBody());
232 }
233}
234
235void StmtPrinter::VisitSwitchCase(SwitchCase*) {
236 assert(0 && "SwitchCase is an abstract class");
237}
238
239void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
240 Indent() << "while (";
241 PrintExpr(Node->getCond());
242 OS << ")\n";
243 PrintStmt(Node->getBody());
244}
245
246void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattnercfe0ff02007-09-15 21:49:37 +0000247 Indent() << "do ";
248 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
249 PrintRawCompoundStmt(CS);
250 OS << " ";
251 } else {
252 OS << "\n";
253 PrintStmt(Node->getBody());
254 Indent();
255 }
256
257 OS << "while ";
Chris Lattner4b009652007-07-25 00:24:17 +0000258 PrintExpr(Node->getCond());
259 OS << ";\n";
260}
261
262void StmtPrinter::VisitForStmt(ForStmt *Node) {
263 Indent() << "for (";
264 if (Node->getInit()) {
265 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
266 PrintRawDecl(DS->getDecl());
267 else
268 PrintExpr(cast<Expr>(Node->getInit()));
269 }
Chris Lattnercfe0ff02007-09-15 21:49:37 +0000270 OS << ";";
271 if (Node->getCond()) {
272 OS << " ";
Chris Lattner4b009652007-07-25 00:24:17 +0000273 PrintExpr(Node->getCond());
Chris Lattnercfe0ff02007-09-15 21:49:37 +0000274 }
275 OS << ";";
276 if (Node->getInc()) {
277 OS << " ";
Chris Lattner4b009652007-07-25 00:24:17 +0000278 PrintExpr(Node->getInc());
Chris Lattnercfe0ff02007-09-15 21:49:37 +0000279 }
280 OS << ") ";
281
282 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
283 PrintRawCompoundStmt(CS);
284 OS << "\n";
285 } else {
286 OS << "\n";
287 PrintStmt(Node->getBody());
288 }
Chris Lattner4b009652007-07-25 00:24:17 +0000289}
290
291void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
292 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
293}
294
295void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
296 Indent() << "goto *";
297 PrintExpr(Node->getTarget());
298 OS << ";\n";
299}
300
301void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
302 Indent() << "continue;\n";
303}
304
305void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
306 Indent() << "break;\n";
307}
308
309
310void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
311 Indent() << "return";
312 if (Node->getRetValue()) {
313 OS << " ";
314 PrintExpr(Node->getRetValue());
315 }
316 OS << ";\n";
317}
318
Chris Lattner8a40a832007-10-29 04:04:16 +0000319
320void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
321 Indent() << "asm (/*todo*/);\n";
322}
323
Chris Lattner4b009652007-07-25 00:24:17 +0000324//===----------------------------------------------------------------------===//
325// Expr printing methods.
326//===----------------------------------------------------------------------===//
327
328void StmtPrinter::VisitExpr(Expr *Node) {
329 OS << "<<unknown expr type>>";
330}
331
332void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
333 OS << Node->getDecl()->getName();
334}
335
336void StmtPrinter::VisitPreDefinedExpr(PreDefinedExpr *Node) {
337 switch (Node->getIdentType()) {
338 default:
339 assert(0 && "unknown case");
340 case PreDefinedExpr::Func:
341 OS << "__func__";
342 break;
343 case PreDefinedExpr::Function:
344 OS << "__FUNCTION__";
345 break;
346 case PreDefinedExpr::PrettyFunction:
347 OS << "__PRETTY_FUNCTION__";
348 break;
349 }
350}
351
352void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
353 // FIXME should print an L for wchar_t constants
354 unsigned value = Node->getValue();
355 switch (value) {
356 case '\\':
357 OS << "'\\\\'";
358 break;
359 case '\'':
360 OS << "'\\''";
361 break;
362 case '\a':
363 // TODO: K&R: the meaning of '\\a' is different in traditional C
364 OS << "'\\a'";
365 break;
366 case '\b':
367 OS << "'\\b'";
368 break;
369 // Nonstandard escape sequence.
370 /*case '\e':
371 OS << "'\\e'";
372 break;*/
373 case '\f':
374 OS << "'\\f'";
375 break;
376 case '\n':
377 OS << "'\\n'";
378 break;
379 case '\r':
380 OS << "'\\r'";
381 break;
382 case '\t':
383 OS << "'\\t'";
384 break;
385 case '\v':
386 OS << "'\\v'";
387 break;
388 default:
389 if (isprint(value) && value < 256) {
390 OS << "'" << (char)value << "'";
391 } else if (value < 256) {
392 OS << "'\\x" << std::hex << value << std::dec << "'";
393 } else {
394 // FIXME what to really do here?
395 OS << value;
396 }
397 }
398}
399
400void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
401 bool isSigned = Node->getType()->isSignedIntegerType();
402 OS << Node->getValue().toString(10, isSigned);
403
404 // Emit suffixes. Integer literals are always a builtin integer type.
405 switch (cast<BuiltinType>(Node->getType().getCanonicalType())->getKind()) {
406 default: assert(0 && "Unexpected type for integer literal!");
407 case BuiltinType::Int: break; // no suffix.
408 case BuiltinType::UInt: OS << 'U'; break;
409 case BuiltinType::Long: OS << 'L'; break;
410 case BuiltinType::ULong: OS << "UL"; break;
411 case BuiltinType::LongLong: OS << "LL"; break;
412 case BuiltinType::ULongLong: OS << "ULL"; break;
413 }
414}
415void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner6a390402007-08-01 00:23:58 +0000416 // FIXME: print value more precisely.
Chris Lattner7f298762007-09-22 18:47:25 +0000417 OS << Node->getValueAsDouble();
Chris Lattner4b009652007-07-25 00:24:17 +0000418}
Chris Lattner1de66eb2007-08-26 03:42:43 +0000419
420void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
421 PrintExpr(Node->getSubExpr());
422 OS << "i";
423}
424
Chris Lattner4b009652007-07-25 00:24:17 +0000425void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
426 if (Str->isWide()) OS << 'L';
427 OS << '"';
Anders Carlsson55bfe0d2007-10-15 02:50:23 +0000428
Chris Lattner4b009652007-07-25 00:24:17 +0000429 // FIXME: this doesn't print wstrings right.
430 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
431 switch (Str->getStrData()[i]) {
432 default: OS << Str->getStrData()[i]; break;
433 // Handle some common ones to make dumps prettier.
434 case '\\': OS << "\\\\"; break;
435 case '"': OS << "\\\""; break;
436 case '\n': OS << "\\n"; break;
437 case '\t': OS << "\\t"; break;
438 case '\a': OS << "\\a"; break;
439 case '\b': OS << "\\b"; break;
440 }
441 }
442 OS << '"';
443}
444void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
445 OS << "(";
446 PrintExpr(Node->getSubExpr());
447 OS << ")";
448}
449void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner2b97b092007-08-23 21:46:40 +0000450 if (!Node->isPostfix()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000451 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner2b97b092007-08-23 21:46:40 +0000452
453 // Print a space if this is an "identifier operator" like sizeof or __real.
454 switch (Node->getOpcode()) {
455 default: break;
456 case UnaryOperator::SizeOf:
457 case UnaryOperator::AlignOf:
458 case UnaryOperator::Real:
459 case UnaryOperator::Imag:
460 case UnaryOperator::Extension:
461 OS << ' ';
462 break;
463 }
464 }
Chris Lattner4b009652007-07-25 00:24:17 +0000465 PrintExpr(Node->getSubExpr());
466
467 if (Node->isPostfix())
468 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner4b009652007-07-25 00:24:17 +0000469}
Chris Lattner2af6a802007-08-30 17:59:59 +0000470
471bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
472 if (isa<CompoundLiteralExpr>(E)) {
473 // Base case, print the type and comma.
474 OS << E->getType().getAsString() << ", ";
475 return true;
476 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
477 PrintOffsetOfDesignator(ASE->getLHS());
478 OS << "[";
479 PrintExpr(ASE->getRHS());
480 OS << "]";
481 return false;
482 } else {
483 MemberExpr *ME = cast<MemberExpr>(E);
484 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
485 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getName();
486 return false;
487 }
488}
489
490void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
491 OS << "__builtin_offsetof(";
492 PrintOffsetOfDesignator(Node->getSubExpr());
493 OS << ")";
494}
495
Chris Lattner4b009652007-07-25 00:24:17 +0000496void StmtPrinter::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
497 OS << (Node->isSizeOf() ? "sizeof(" : "__alignof(");
498 OS << Node->getArgumentType().getAsString() << ")";
499}
500void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000501 PrintExpr(Node->getLHS());
Chris Lattner4b009652007-07-25 00:24:17 +0000502 OS << "[";
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000503 PrintExpr(Node->getRHS());
Chris Lattner4b009652007-07-25 00:24:17 +0000504 OS << "]";
505}
506
507void StmtPrinter::VisitCallExpr(CallExpr *Call) {
508 PrintExpr(Call->getCallee());
509 OS << "(";
510 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
511 if (i) OS << ", ";
512 PrintExpr(Call->getArg(i));
513 }
514 OS << ")";
515}
516void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
517 PrintExpr(Node->getBase());
518 OS << (Node->isArrow() ? "->" : ".");
519
520 FieldDecl *Field = Node->getMemberDecl();
521 assert(Field && "MemberExpr should alway reference a field!");
522 OS << Field->getName();
523}
Chris Lattnera0d03a72007-08-03 17:31:20 +0000524void StmtPrinter::VisitOCUVectorElementExpr(OCUVectorElementExpr *Node) {
Steve Naroffc11705f2007-07-28 23:10:27 +0000525 PrintExpr(Node->getBase());
526 OS << ".";
527 OS << Node->getAccessor().getName();
528}
Chris Lattner4b009652007-07-25 00:24:17 +0000529void StmtPrinter::VisitCastExpr(CastExpr *Node) {
530 OS << "(" << Node->getType().getAsString() << ")";
531 PrintExpr(Node->getSubExpr());
532}
533void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
534 OS << "(" << Node->getType().getAsString() << ")";
535 PrintExpr(Node->getInitializer());
536}
537void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
538 // No need to print anything, simply forward to the sub expression.
539 PrintExpr(Node->getSubExpr());
540}
541void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
542 PrintExpr(Node->getLHS());
543 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
544 PrintExpr(Node->getRHS());
545}
Chris Lattner06078d22007-08-25 02:00:02 +0000546void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
547 PrintExpr(Node->getLHS());
548 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
549 PrintExpr(Node->getRHS());
550}
Chris Lattner4b009652007-07-25 00:24:17 +0000551void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
552 PrintExpr(Node->getCond());
553 OS << " ? ";
554 PrintExpr(Node->getLHS());
555 OS << " : ";
556 PrintExpr(Node->getRHS());
557}
558
559// GNU extensions.
560
Chris Lattnera0d03a72007-08-03 17:31:20 +0000561void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattner4b009652007-07-25 00:24:17 +0000562 OS << "&&" << Node->getLabel()->getName();
563}
564
565void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
566 OS << "(";
567 PrintRawCompoundStmt(E->getSubStmt());
568 OS << ")";
569}
570
Steve Naroff63bad2d2007-08-01 22:05:33 +0000571void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
572 OS << "__builtin_types_compatible_p(";
573 OS << Node->getArgType1().getAsString() << ",";
574 OS << Node->getArgType2().getAsString() << ")";
575}
576
Steve Naroff93c53012007-08-03 21:21:27 +0000577void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
578 OS << "__builtin_choose_expr(";
579 PrintExpr(Node->getCond());
Chris Lattner44fcf4f2007-08-04 00:20:15 +0000580 OS << ", ";
Steve Naroff93c53012007-08-03 21:21:27 +0000581 PrintExpr(Node->getLHS());
Chris Lattner44fcf4f2007-08-04 00:20:15 +0000582 OS << ", ";
Steve Naroff93c53012007-08-03 21:21:27 +0000583 PrintExpr(Node->getRHS());
584 OS << ")";
585}
Chris Lattner4b009652007-07-25 00:24:17 +0000586
Anders Carlsson762b7c72007-08-31 04:56:16 +0000587void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
588 OS << "{ ";
589 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
590 if (i) OS << ", ";
591 PrintExpr(Node->getInit(i));
592 }
593 OS << " }";
594}
595
Anders Carlsson36760332007-10-15 20:28:48 +0000596void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
597 OS << "va_arg(";
598 PrintExpr(Node->getSubExpr());
599 OS << ", ";
600 OS << Node->getType().getAsString();
601 OS << ")";
602}
603
Chris Lattner4b009652007-07-25 00:24:17 +0000604// C++
605
606void StmtPrinter::VisitCXXCastExpr(CXXCastExpr *Node) {
Chris Lattnerac485c12007-08-09 17:34:19 +0000607 OS << CXXCastExpr::getOpcodeStr(Node->getOpcode()) << '<';
Chris Lattner4b009652007-07-25 00:24:17 +0000608 OS << Node->getDestType().getAsString() << ">(";
609 PrintExpr(Node->getSubExpr());
610 OS << ")";
611}
612
613void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
614 OS << (Node->getValue() ? "true" : "false");
615}
616
Anders Carlssona66cad42007-08-21 17:43:55 +0000617// Obj-C
618
619void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
620 OS << "@";
621 VisitStringLiteral(Node->getString());
622}
Chris Lattner4b009652007-07-25 00:24:17 +0000623
Anders Carlsson8be1d402007-08-22 15:14:15 +0000624void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattnerc17ccab2007-10-18 00:39:29 +0000625 OS << "@encode(" << Node->getEncodedType().getAsString() << ")";
Anders Carlsson8be1d402007-08-22 15:14:15 +0000626}
627
Fariborz Jahanianf807c202007-10-16 20:40:23 +0000628void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattnerc17ccab2007-10-18 00:39:29 +0000629 OS << "@selector(" << Node->getSelector().getName() << ")";
Fariborz Jahanianf807c202007-10-16 20:40:23 +0000630}
631
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +0000632void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattnerc17ccab2007-10-18 00:39:29 +0000633 OS << "@protocol(" << Node->getProtocol()->getName() << ")";
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +0000634}
635
Steve Naroffc39ca262007-09-18 23:55:05 +0000636void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
637 OS << "[";
Steve Narofffa465d12007-10-02 20:01:56 +0000638 Expr *receiver = Mess->getReceiver();
639 if (receiver) PrintExpr(receiver);
640 else OS << Mess->getClassName()->getName();
641 Selector &selector = Mess->getSelector();
642 if (selector.isUnarySelector()) {
643 OS << " " << selector.getIdentifierInfoForSlot(0)->getName();
644 } else {
645 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Fariborz Jahanian9a111a02007-10-16 20:52:13 +0000646 if (selector.getIdentifierInfoForSlot(i))
647 OS << selector.getIdentifierInfoForSlot(i)->getName() << ":";
648 else
649 OS << ":";
Steve Narofffa465d12007-10-02 20:01:56 +0000650 PrintExpr(Mess->getArg(i));
651 }
Steve Naroffc39ca262007-09-18 23:55:05 +0000652 }
653 OS << "]";
654}
655
656
Chris Lattner4b009652007-07-25 00:24:17 +0000657//===----------------------------------------------------------------------===//
658// Stmt method implementations
659//===----------------------------------------------------------------------===//
660
Chris Lattner95578782007-08-08 22:51:59 +0000661void Stmt::dumpPretty() const {
Chris Lattner4b009652007-07-25 00:24:17 +0000662 // FIXME: eliminate use of <iostream>
Chris Lattner95578782007-08-08 22:51:59 +0000663 printPretty(std::cerr);
Chris Lattner4b009652007-07-25 00:24:17 +0000664}
665
Ted Kremenek08176a52007-08-31 21:30:12 +0000666void Stmt::printPretty(std::ostream &OS, PrinterHelper* Helper) const {
Chris Lattner4b009652007-07-25 00:24:17 +0000667 if (this == 0) {
668 OS << "<NULL>";
669 return;
670 }
671
Ted Kremenek08176a52007-08-31 21:30:12 +0000672 StmtPrinter P(OS, Helper);
Chris Lattner7ba21fa2007-08-21 04:04:25 +0000673 P.Visit(const_cast<Stmt*>(this));
Chris Lattner4b009652007-07-25 00:24:17 +0000674}
Ted Kremenek08176a52007-08-31 21:30:12 +0000675
676//===----------------------------------------------------------------------===//
677// PrinterHelper
678//===----------------------------------------------------------------------===//
679
680// Implement virtual destructor.
Gabor Greif61ce98c2007-09-11 15:32:40 +0000681PrinterHelper::~PrinterHelper() {}