blob: d96a0486cf28d327ee649ae8dbb5595d2e55e529 [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"
Ted Kremenek91d1d7a2007-10-17 18:36:42 +000017#include "clang/AST/DeclObjC.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/AST/ExprCXX.h"
Ted Kremenek42a509f2007-08-31 21:30:12 +000019#include "clang/AST/PrettyPrinter.h"
Chris Lattnerc7229c32007-10-07 08:58:51 +000020#include "clang/Basic/IdentifierTable.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "llvm/Support/Compiler.h"
22#include <iostream>
Chris Lattnerb0a721a2007-07-13 05:18:11 +000023#include <iomanip>
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// StmtPrinter Visitor
28//===----------------------------------------------------------------------===//
29
30namespace {
Chris Lattnerc5598cb2007-08-21 04:04:25 +000031 class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor<StmtPrinter> {
Reid Spencer5f016e22007-07-11 17:01:13 +000032 std::ostream &OS;
33 unsigned IndentLevel;
Ted Kremenek42a509f2007-08-31 21:30:12 +000034 clang::PrinterHelper* Helper;
Reid Spencer5f016e22007-07-11 17:01:13 +000035 public:
Ted Kremenek42a509f2007-08-31 21:30:12 +000036 StmtPrinter(std::ostream &os, PrinterHelper* helper) :
37 OS(os), IndentLevel(0), Helper(helper) {}
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnerc5598cb2007-08-21 04:04:25 +000044 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000045 OS << ";\n";
46 } else if (S) {
Chris Lattnerc5598cb2007-08-21 04:04:25 +000047 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnerc5598cb2007-08-21 04:04:25 +000060 Visit(E);
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner704fe352007-08-30 17:59:59 +000071 bool PrintOffsetOfDesignator(Expr *E);
72 void VisitUnaryOffsetOf(UnaryOperator *Node);
73
Ted Kremenek42a509f2007-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 Lattnerc5598cb2007-08-21 04:04:25 +000080 void VisitStmt(Stmt *Node);
Reid Spencer5f016e22007-07-11 17:01:13 +000081#define STMT(N, CLASS, PARENT) \
Chris Lattnerc5598cb2007-08-21 04:04:25 +000082 void Visit##CLASS(CLASS *Node);
Reid Spencer5f016e22007-07-11 17:01:13 +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
Chris Lattner24c39902007-07-12 00:36:32 +0000129 // 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 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 } 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 Naroff94745042007-09-13 23:52:58 +0000148 for (ScopedDecl *D = Node->getDecl(); D; D = D->getNextDeclarator()) {
Reid Spencer5f016e22007-07-11 17:01:13 +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
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000235void StmtPrinter::VisitSwitchCase(SwitchCase*) {
236 assert(0 && "SwitchCase is an abstract class");
237}
238
Reid Spencer5f016e22007-07-11 17:01:13 +0000239void 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 Lattner8bdcc472007-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 ";
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner8bdcc472007-09-15 21:49:37 +0000270 OS << ";";
271 if (Node->getCond()) {
272 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000273 PrintExpr(Node->getCond());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000274 }
275 OS << ";";
276 if (Node->getInc()) {
277 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 PrintExpr(Node->getInc());
Chris Lattner8bdcc472007-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 }
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnerfe795952007-10-29 04:04:16 +0000319
320void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
321 Indent() << "asm (/*todo*/);\n";
322}
323
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000324void StmtPrinter::VisitObjcAtTryStmt(ObjcAtTryStmt *Node) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000325 Indent() << "@try";
326 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
327 PrintRawCompoundStmt(TS);
328 OS << "\n";
329 }
330
331 for (ObjcAtCatchStmt *catchStmt =
332 static_cast<ObjcAtCatchStmt *>(Node->getCatchStmts());
333 catchStmt;
334 catchStmt =
335 static_cast<ObjcAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
336 Indent() << "@catch(";
337 if (catchStmt->getCatchParamStmt()) {
338 if (DeclStmt *DS = dyn_cast<DeclStmt>(catchStmt->getCatchParamStmt()))
339 PrintRawDecl(DS->getDecl());
340 }
341 OS << ")";
342 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody()))
343 {
344 PrintRawCompoundStmt(CS);
345 OS << "\n";
346 }
347 }
348
Fariborz Jahanian1e7eab42007-11-07 00:46:42 +0000349 if (ObjcAtFinallyStmt *FS =static_cast<ObjcAtFinallyStmt *>(
350 Node->getFinallyStmt())) {
351 Indent() << "@finally";
352 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000353 OS << "\n";
354 }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000355}
356
357void StmtPrinter::VisitObjcAtFinallyStmt(ObjcAtFinallyStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000358}
359
360void StmtPrinter::VisitObjcAtCatchStmt (ObjcAtCatchStmt *Node) {
361 Indent() << "@catch (...) { /* todo */ } \n";
362}
363
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000364void StmtPrinter::VisitObjcAtThrowStmt (ObjcAtThrowStmt *Node) {
365 Indent() << "@throw";
366 if (Node->getThrowExpr()) {
367 OS << " ";
368 PrintExpr(Node->getThrowExpr());
369 }
370 OS << ";\n";
371}
372
Reid Spencer5f016e22007-07-11 17:01:13 +0000373//===----------------------------------------------------------------------===//
374// Expr printing methods.
375//===----------------------------------------------------------------------===//
376
377void StmtPrinter::VisitExpr(Expr *Node) {
378 OS << "<<unknown expr type>>";
379}
380
381void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
382 OS << Node->getDecl()->getName();
383}
384
Steve Naroff7779db42007-11-12 14:29:37 +0000385void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000386 if (Node->getBase()) {
387 PrintExpr(Node->getBase());
388 OS << (Node->isArrow() ? "->" : ".");
389 }
Steve Naroff7779db42007-11-12 14:29:37 +0000390 OS << Node->getDecl()->getName();
391}
392
Anders Carlsson22742662007-07-21 05:21:51 +0000393void StmtPrinter::VisitPreDefinedExpr(PreDefinedExpr *Node) {
394 switch (Node->getIdentType()) {
395 default:
396 assert(0 && "unknown case");
397 case PreDefinedExpr::Func:
398 OS << "__func__";
399 break;
400 case PreDefinedExpr::Function:
401 OS << "__FUNCTION__";
402 break;
403 case PreDefinedExpr::PrettyFunction:
404 OS << "__PRETTY_FUNCTION__";
405 break;
406 }
407}
408
Reid Spencer5f016e22007-07-11 17:01:13 +0000409void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000410 // FIXME should print an L for wchar_t constants
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000411 unsigned value = Node->getValue();
Chris Lattner8bf9f072007-07-13 23:58:20 +0000412 switch (value) {
413 case '\\':
414 OS << "'\\\\'";
415 break;
416 case '\'':
417 OS << "'\\''";
418 break;
419 case '\a':
420 // TODO: K&R: the meaning of '\\a' is different in traditional C
421 OS << "'\\a'";
422 break;
423 case '\b':
424 OS << "'\\b'";
425 break;
426 // Nonstandard escape sequence.
427 /*case '\e':
428 OS << "'\\e'";
429 break;*/
430 case '\f':
431 OS << "'\\f'";
432 break;
433 case '\n':
434 OS << "'\\n'";
435 break;
436 case '\r':
437 OS << "'\\r'";
438 break;
439 case '\t':
440 OS << "'\\t'";
441 break;
442 case '\v':
443 OS << "'\\v'";
444 break;
445 default:
446 if (isprint(value) && value < 256) {
447 OS << "'" << (char)value << "'";
448 } else if (value < 256) {
449 OS << "'\\x" << std::hex << value << std::dec << "'";
450 } else {
451 // FIXME what to really do here?
452 OS << value;
453 }
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000454 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000455}
456
457void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
458 bool isSigned = Node->getType()->isSignedIntegerType();
459 OS << Node->getValue().toString(10, isSigned);
460
461 // Emit suffixes. Integer literals are always a builtin integer type.
462 switch (cast<BuiltinType>(Node->getType().getCanonicalType())->getKind()) {
463 default: assert(0 && "Unexpected type for integer literal!");
464 case BuiltinType::Int: break; // no suffix.
465 case BuiltinType::UInt: OS << 'U'; break;
466 case BuiltinType::Long: OS << 'L'; break;
467 case BuiltinType::ULong: OS << "UL"; break;
468 case BuiltinType::LongLong: OS << "LL"; break;
469 case BuiltinType::ULongLong: OS << "ULL"; break;
470 }
471}
472void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner86e499d2007-08-01 00:23:58 +0000473 // FIXME: print value more precisely.
Chris Lattnerc9bec4b2007-09-22 18:47:25 +0000474 OS << Node->getValueAsDouble();
Reid Spencer5f016e22007-07-11 17:01:13 +0000475}
Chris Lattner5d661452007-08-26 03:42:43 +0000476
477void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
478 PrintExpr(Node->getSubExpr());
479 OS << "i";
480}
481
Reid Spencer5f016e22007-07-11 17:01:13 +0000482void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
483 if (Str->isWide()) OS << 'L';
484 OS << '"';
Anders Carlssonee98ac52007-10-15 02:50:23 +0000485
Reid Spencer5f016e22007-07-11 17:01:13 +0000486 // FIXME: this doesn't print wstrings right.
487 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
488 switch (Str->getStrData()[i]) {
489 default: OS << Str->getStrData()[i]; break;
490 // Handle some common ones to make dumps prettier.
491 case '\\': OS << "\\\\"; break;
492 case '"': OS << "\\\""; break;
493 case '\n': OS << "\\n"; break;
494 case '\t': OS << "\\t"; break;
495 case '\a': OS << "\\a"; break;
496 case '\b': OS << "\\b"; break;
497 }
498 }
499 OS << '"';
500}
501void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
502 OS << "(";
503 PrintExpr(Node->getSubExpr());
504 OS << ")";
505}
506void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +0000507 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000508 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner296bf192007-08-23 21:46:40 +0000509
510 // Print a space if this is an "identifier operator" like sizeof or __real.
511 switch (Node->getOpcode()) {
512 default: break;
513 case UnaryOperator::SizeOf:
514 case UnaryOperator::AlignOf:
515 case UnaryOperator::Real:
516 case UnaryOperator::Imag:
517 case UnaryOperator::Extension:
518 OS << ' ';
519 break;
520 }
521 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000522 PrintExpr(Node->getSubExpr());
523
524 if (Node->isPostfix())
525 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +0000526}
Chris Lattner704fe352007-08-30 17:59:59 +0000527
528bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
529 if (isa<CompoundLiteralExpr>(E)) {
530 // Base case, print the type and comma.
531 OS << E->getType().getAsString() << ", ";
532 return true;
533 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
534 PrintOffsetOfDesignator(ASE->getLHS());
535 OS << "[";
536 PrintExpr(ASE->getRHS());
537 OS << "]";
538 return false;
539 } else {
540 MemberExpr *ME = cast<MemberExpr>(E);
541 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
542 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getName();
543 return false;
544 }
545}
546
547void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
548 OS << "__builtin_offsetof(";
549 PrintOffsetOfDesignator(Node->getSubExpr());
550 OS << ")";
551}
552
Reid Spencer5f016e22007-07-11 17:01:13 +0000553void StmtPrinter::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
554 OS << (Node->isSizeOf() ? "sizeof(" : "__alignof(");
555 OS << Node->getArgumentType().getAsString() << ")";
556}
557void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +0000558 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000559 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +0000560 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000561 OS << "]";
562}
563
564void StmtPrinter::VisitCallExpr(CallExpr *Call) {
565 PrintExpr(Call->getCallee());
566 OS << "(";
567 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
568 if (i) OS << ", ";
569 PrintExpr(Call->getArg(i));
570 }
571 OS << ")";
572}
573void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
574 PrintExpr(Node->getBase());
575 OS << (Node->isArrow() ? "->" : ".");
576
577 FieldDecl *Field = Node->getMemberDecl();
578 assert(Field && "MemberExpr should alway reference a field!");
579 OS << Field->getName();
580}
Chris Lattner6481a572007-08-03 17:31:20 +0000581void StmtPrinter::VisitOCUVectorElementExpr(OCUVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +0000582 PrintExpr(Node->getBase());
583 OS << ".";
584 OS << Node->getAccessor().getName();
585}
Reid Spencer5f016e22007-07-11 17:01:13 +0000586void StmtPrinter::VisitCastExpr(CastExpr *Node) {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000587 OS << "(" << Node->getType().getAsString() << ")";
Reid Spencer5f016e22007-07-11 17:01:13 +0000588 PrintExpr(Node->getSubExpr());
589}
Steve Naroffaff1edd2007-07-19 21:32:11 +0000590void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
591 OS << "(" << Node->getType().getAsString() << ")";
592 PrintExpr(Node->getInitializer());
593}
Steve Naroff49b45262007-07-13 16:58:59 +0000594void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroff90045e82007-07-13 23:32:42 +0000595 // No need to print anything, simply forward to the sub expression.
596 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +0000597}
Reid Spencer5f016e22007-07-11 17:01:13 +0000598void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
599 PrintExpr(Node->getLHS());
600 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
601 PrintExpr(Node->getRHS());
602}
Chris Lattnereb14fe82007-08-25 02:00:02 +0000603void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
604 PrintExpr(Node->getLHS());
605 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
606 PrintExpr(Node->getRHS());
607}
Reid Spencer5f016e22007-07-11 17:01:13 +0000608void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
609 PrintExpr(Node->getCond());
610 OS << " ? ";
611 PrintExpr(Node->getLHS());
612 OS << " : ";
613 PrintExpr(Node->getRHS());
614}
615
616// GNU extensions.
617
Chris Lattner6481a572007-08-03 17:31:20 +0000618void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000619 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000620}
621
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000622void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
623 OS << "(";
624 PrintRawCompoundStmt(E->getSubStmt());
625 OS << ")";
626}
627
Steve Naroffd34e9152007-08-01 22:05:33 +0000628void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
629 OS << "__builtin_types_compatible_p(";
630 OS << Node->getArgType1().getAsString() << ",";
631 OS << Node->getArgType2().getAsString() << ")";
632}
633
Steve Naroffd04fdd52007-08-03 21:21:27 +0000634void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
635 OS << "__builtin_choose_expr(";
636 PrintExpr(Node->getCond());
Chris Lattner94f05e32007-08-04 00:20:15 +0000637 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000638 PrintExpr(Node->getLHS());
Chris Lattner94f05e32007-08-04 00:20:15 +0000639 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000640 PrintExpr(Node->getRHS());
641 OS << ")";
642}
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000643
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000644void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
645 OS << "{ ";
646 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
647 if (i) OS << ", ";
648 PrintExpr(Node->getInit(i));
649 }
650 OS << " }";
651}
652
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000653void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
654 OS << "va_arg(";
655 PrintExpr(Node->getSubExpr());
656 OS << ", ";
657 OS << Node->getType().getAsString();
658 OS << ")";
659}
660
Reid Spencer5f016e22007-07-11 17:01:13 +0000661// C++
662
663void StmtPrinter::VisitCXXCastExpr(CXXCastExpr *Node) {
Chris Lattner36460ee2007-08-09 17:34:19 +0000664 OS << CXXCastExpr::getOpcodeStr(Node->getOpcode()) << '<';
Reid Spencer5f016e22007-07-11 17:01:13 +0000665 OS << Node->getDestType().getAsString() << ">(";
666 PrintExpr(Node->getSubExpr());
667 OS << ")";
668}
669
670void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
671 OS << (Node->getValue() ? "true" : "false");
672}
673
Anders Carlsson55085182007-08-21 17:43:55 +0000674// Obj-C
675
676void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
677 OS << "@";
678 VisitStringLiteral(Node->getString());
679}
Reid Spencer5f016e22007-07-11 17:01:13 +0000680
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000681void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner994f9392007-10-18 00:39:29 +0000682 OS << "@encode(" << Node->getEncodedType().getAsString() << ")";
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000683}
684
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000685void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner994f9392007-10-18 00:39:29 +0000686 OS << "@selector(" << Node->getSelector().getName() << ")";
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000687}
688
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000689void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner994f9392007-10-18 00:39:29 +0000690 OS << "@protocol(" << Node->getProtocol()->getName() << ")";
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000691}
692
Steve Naroff563477d2007-09-18 23:55:05 +0000693void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
694 OS << "[";
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000695 Expr *receiver = Mess->getReceiver();
696 if (receiver) PrintExpr(receiver);
697 else OS << Mess->getClassName()->getName();
698 Selector &selector = Mess->getSelector();
699 if (selector.isUnarySelector()) {
700 OS << " " << selector.getIdentifierInfoForSlot(0)->getName();
701 } else {
702 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Fariborz Jahanianf24d95a2007-10-16 20:52:13 +0000703 if (selector.getIdentifierInfoForSlot(i))
704 OS << selector.getIdentifierInfoForSlot(i)->getName() << ":";
705 else
706 OS << ":";
Steve Naroff6a8a9a42007-10-02 20:01:56 +0000707 PrintExpr(Mess->getArg(i));
708 }
Steve Naroff563477d2007-09-18 23:55:05 +0000709 }
710 OS << "]";
711}
712
Reid Spencer5f016e22007-07-11 17:01:13 +0000713//===----------------------------------------------------------------------===//
714// Stmt method implementations
715//===----------------------------------------------------------------------===//
716
Chris Lattner6000dac2007-08-08 22:51:59 +0000717void Stmt::dumpPretty() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000718 // FIXME: eliminate use of <iostream>
Chris Lattner6000dac2007-08-08 22:51:59 +0000719 printPretty(std::cerr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000720}
721
Ted Kremenek42a509f2007-08-31 21:30:12 +0000722void Stmt::printPretty(std::ostream &OS, PrinterHelper* Helper) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000723 if (this == 0) {
724 OS << "<NULL>";
725 return;
726 }
727
Ted Kremenek42a509f2007-08-31 21:30:12 +0000728 StmtPrinter P(OS, Helper);
Chris Lattnerc5598cb2007-08-21 04:04:25 +0000729 P.Visit(const_cast<Stmt*>(this));
Reid Spencer5f016e22007-07-11 17:01:13 +0000730}
Ted Kremenek42a509f2007-08-31 21:30:12 +0000731
732//===----------------------------------------------------------------------===//
733// PrinterHelper
734//===----------------------------------------------------------------------===//
735
736// Implement virtual destructor.
Gabor Greif84675832007-09-11 15:32:40 +0000737PrinterHelper::~PrinterHelper() {}