blob: a3fff0243056a5b4c4fbb09b88b86ff2c5dba638 [file] [log] [blame]
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001//===--- StmtPrinter.cpp - Printing implementation for Stmt ASTs ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Stmt::dump/Stmt::print methods.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/StmtVisitor.h"
Chris Lattner5efbb332006-11-20 05:01:40 +000015#include "clang/AST/Decl.h"
Chris Lattner29375652006-12-04 18:06:35 +000016#include "clang/AST/ExprCXX.h"
Chris Lattner6c0ff132006-11-05 00:19:50 +000017#include "clang/Lex/IdentifierTable.h"
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000018#include "llvm/Support/Compiler.h"
19#include <iostream>
Chris Lattner6e9d9b32007-07-13 05:18:11 +000020#include <iomanip>
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000021using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// StmtPrinter Visitor
25//===----------------------------------------------------------------------===//
26
27namespace {
28 class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor {
29 std::ostream &OS;
30 unsigned IndentLevel;
31 public:
32 StmtPrinter(std::ostream &os) : OS(os), IndentLevel(0) {}
33
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000034 void PrintStmt(Stmt *S, int SubIndent = 1) {
35 IndentLevel += SubIndent;
Chris Lattnera076fde2007-05-31 18:21:33 +000036 if (S && isa<Expr>(S)) {
Chris Lattner882f7882006-11-04 18:52:07 +000037 // If this is an expr used in a stmt context, indent and newline it.
38 Indent();
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000039 S->visit(*this);
Chris Lattnerfc068c12007-05-30 17:57:36 +000040 OS << ";\n";
Chris Lattner882f7882006-11-04 18:52:07 +000041 } else if (S) {
42 S->visit(*this);
43 } else {
Chris Lattner2f6ac262007-05-28 01:47:47 +000044 Indent() << "<<<NULL STATEMENT>>>\n";
Chris Lattner882f7882006-11-04 18:52:07 +000045 }
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000046 IndentLevel -= SubIndent;
Chris Lattner882f7882006-11-04 18:52:07 +000047 }
Chris Lattner073926e2007-05-20 23:04:55 +000048
49 void PrintRawCompoundStmt(CompoundStmt *S);
Chris Lattnerfdc195a2007-06-05 20:52:47 +000050 void PrintRawDecl(Decl *D);
Chris Lattnerc0a38dd2007-06-11 22:26:23 +000051 void PrintRawIfStmt(IfStmt *If);
52
Chris Lattner882f7882006-11-04 18:52:07 +000053 void PrintExpr(Expr *E) {
54 if (E)
55 E->visit(*this);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000056 else
Chris Lattner882f7882006-11-04 18:52:07 +000057 OS << "<null expr>";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000058 }
59
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000060 std::ostream &Indent(int Delta = 0) const {
Chris Lattnerd5322cd2007-05-29 23:49:07 +000061 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000062 OS << " ";
63 return OS;
64 }
65
Chris Lattner882f7882006-11-04 18:52:07 +000066 virtual void VisitStmt(Stmt *Node);
Steve Naroff7f890eb2007-02-27 02:53:10 +000067#define STMT(N, CLASS, PARENT) \
Chris Lattnerf2174b62006-11-04 20:59:27 +000068 virtual void Visit##CLASS(CLASS *Node);
69#include "clang/AST/StmtNodes.def"
Chris Lattner71e23ce2006-11-04 20:18:38 +000070 };
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000071}
72
Chris Lattner71e23ce2006-11-04 20:18:38 +000073//===----------------------------------------------------------------------===//
74// Stmt printing methods.
75//===----------------------------------------------------------------------===//
76
Chris Lattner882f7882006-11-04 18:52:07 +000077void StmtPrinter::VisitStmt(Stmt *Node) {
78 Indent() << "<<unknown stmt type>>\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000079}
80
Chris Lattner073926e2007-05-20 23:04:55 +000081/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
82/// with no newline after the }.
83void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
84 OS << "{\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000085 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
Chris Lattner882f7882006-11-04 18:52:07 +000086 I != E; ++I)
87 PrintStmt(*I);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000088
Chris Lattner073926e2007-05-20 23:04:55 +000089 Indent() << "}";
90}
91
Chris Lattnerfdc195a2007-06-05 20:52:47 +000092void StmtPrinter::PrintRawDecl(Decl *D) {
93 // FIXME: Need to complete/beautify this... this code simply shows the
Steve Naroff2a8ad182007-05-29 22:59:26 +000094 // nodes are where they need to be.
Chris Lattnerfdc195a2007-06-05 20:52:47 +000095 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
96 OS << "typedef " << localType->getUnderlyingType().getAsString();
97 OS << " " << localType->getName();
98 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattnerb4522b42007-06-02 05:27:01 +000099 // Emit storage class for vardecls.
100 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
101 switch (V->getStorageClass()) {
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000102 default: assert(0 && "Unknown storage class!");
103 case VarDecl::None: break;
104 case VarDecl::Extern: OS << "extern "; break;
105 case VarDecl::Static: OS << "static "; break;
106 case VarDecl::Auto: OS << "auto "; break;
107 case VarDecl::Register: OS << "register "; break;
Chris Lattnerb4522b42007-06-02 05:27:01 +0000108 }
109 }
110
Chris Lattnere0c4ae12007-06-02 03:38:08 +0000111 std::string Name = VD->getName();
112 VD->getType().getAsStringInternal(Name);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000113 OS << Name;
114
Chris Lattner79c57592007-07-12 00:36:32 +0000115 // If this is a vardecl with an initializer, emit it.
116 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
117 if (V->getInit()) {
118 OS << " = ";
119 PrintExpr(V->getInit());
120 }
121 }
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000122 } else {
Chris Lattnere0c4ae12007-06-02 03:38:08 +0000123 // FIXME: "struct x;"
124 assert(0 && "Unexpected decl");
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000125 }
126}
127
128
129void StmtPrinter::VisitNullStmt(NullStmt *Node) {
130 Indent() << ";\n";
131}
132
133void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Chris Lattner776fac82007-06-09 00:53:06 +0000134 for (Decl *D = Node->getDecl(); D; D = D->getNextDeclarator()) {
135 Indent();
136 PrintRawDecl(D);
137 OS << ";\n";
138 }
Steve Naroff2a8ad182007-05-29 22:59:26 +0000139}
140
Chris Lattner073926e2007-05-20 23:04:55 +0000141void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
142 Indent();
143 PrintRawCompoundStmt(Node);
Chris Lattnerdf3cafb2007-05-31 05:08:56 +0000144 OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000145}
146
Chris Lattner6c0ff132006-11-05 00:19:50 +0000147void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000148 Indent(-1) << "case ";
Chris Lattner6c0ff132006-11-05 00:19:50 +0000149 PrintExpr(Node->getLHS());
150 if (Node->getRHS()) {
151 OS << " ... ";
152 PrintExpr(Node->getRHS());
153 }
154 OS << ":\n";
155
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000156 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000157}
158
159void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000160 Indent(-1) << "default:\n";
161 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000162}
163
164void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000165 Indent(-1) << Node->getName() << ":\n";
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000166 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000167}
168
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000169void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
170 OS << "if ";
Chris Lattner882f7882006-11-04 18:52:07 +0000171 PrintExpr(If->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000172
173 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
174 OS << ' ';
175 PrintRawCompoundStmt(CS);
176 OS << (If->getElse() ? ' ' : '\n');
177 } else {
178 OS << '\n';
179 PrintStmt(If->getThen());
180 if (If->getElse()) Indent();
181 }
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000182
Chris Lattner073926e2007-05-20 23:04:55 +0000183 if (Stmt *Else = If->getElse()) {
184 OS << "else";
185
186 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
187 OS << ' ';
188 PrintRawCompoundStmt(CS);
189 OS << '\n';
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000190 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
191 OS << ' ';
192 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000193 } else {
194 OS << '\n';
195 PrintStmt(If->getElse());
196 }
Chris Lattner882f7882006-11-04 18:52:07 +0000197 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000198}
199
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000200void StmtPrinter::VisitIfStmt(IfStmt *If) {
201 Indent();
202 PrintRawIfStmt(If);
203}
204
Chris Lattnerf2174b62006-11-04 20:59:27 +0000205void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
206 Indent() << "switch (";
207 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000208 OS << ")";
209
210 // Pretty print compoundstmt bodies (very common).
211 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
212 OS << " ";
213 PrintRawCompoundStmt(CS);
214 OS << "\n";
215 } else {
216 OS << "\n";
217 PrintStmt(Node->getBody());
218 }
Chris Lattnerf2174b62006-11-04 20:59:27 +0000219}
220
Chris Lattner85ed8732006-11-04 20:40:44 +0000221void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
222 Indent() << "while (";
223 PrintExpr(Node->getCond());
224 OS << ")\n";
225 PrintStmt(Node->getBody());
226}
227
228void StmtPrinter::VisitDoStmt(DoStmt *Node) {
229 Indent() << "do\n";
230 PrintStmt(Node->getBody());
Chris Lattnera076fde2007-05-31 18:21:33 +0000231 Indent() << "while ";
Chris Lattner85ed8732006-11-04 20:40:44 +0000232 PrintExpr(Node->getCond());
Chris Lattnera076fde2007-05-31 18:21:33 +0000233 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000234}
235
Chris Lattner71e23ce2006-11-04 20:18:38 +0000236void StmtPrinter::VisitForStmt(ForStmt *Node) {
237 Indent() << "for (";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000238 if (Node->getInit()) {
239 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
240 PrintRawDecl(DS->getDecl());
241 else
242 PrintExpr(cast<Expr>(Node->getInit()));
243 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000244 OS << "; ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000245 if (Node->getCond())
246 PrintExpr(Node->getCond());
Chris Lattner71e23ce2006-11-04 20:18:38 +0000247 OS << "; ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000248 if (Node->getInc())
249 PrintExpr(Node->getInc());
Chris Lattner71e23ce2006-11-04 20:18:38 +0000250 OS << ")\n";
Chris Lattner85ed8732006-11-04 20:40:44 +0000251 PrintStmt(Node->getBody());
Chris Lattner71e23ce2006-11-04 20:18:38 +0000252}
253
Chris Lattner16976d32006-11-05 01:46:01 +0000254void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000255 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000256}
257
258void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000259 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000260 PrintExpr(Node->getTarget());
Chris Lattnerb4619482007-05-31 06:00:14 +0000261 OS << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000262}
263
264void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000265 Indent() << "continue;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000266}
267
268void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000269 Indent() << "break;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000270}
271
272
Chris Lattner882f7882006-11-04 18:52:07 +0000273void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
274 Indent() << "return";
275 if (Node->getRetValue()) {
276 OS << " ";
277 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000278 }
Chris Lattnerb4619482007-05-31 06:00:14 +0000279 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000280}
281
Chris Lattner71e23ce2006-11-04 20:18:38 +0000282//===----------------------------------------------------------------------===//
283// Expr printing methods.
284//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000285
Chris Lattner882f7882006-11-04 18:52:07 +0000286void StmtPrinter::VisitExpr(Expr *Node) {
287 OS << "<<unknown expr type>>";
288}
289
290void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Chris Lattner5efbb332006-11-20 05:01:40 +0000291 OS << Node->getDecl()->getName();
Chris Lattner882f7882006-11-04 18:52:07 +0000292}
293
Steve Naroffae4143e2007-04-26 20:39:23 +0000294void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner666115c2007-07-13 23:58:20 +0000295 // FIXME should print an L for wchar_t constants
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000296 unsigned value = Node->getValue();
Chris Lattner666115c2007-07-13 23:58:20 +0000297 switch (value) {
298 case '\\':
299 OS << "'\\\\'";
300 break;
301 case '\'':
302 OS << "'\\''";
303 break;
304 case '\a':
305 // TODO: K&R: the meaning of '\\a' is different in traditional C
306 OS << "'\\a'";
307 break;
308 case '\b':
309 OS << "'\\b'";
310 break;
311 // Nonstandard escape sequence.
312 /*case '\e':
313 OS << "'\\e'";
314 break;*/
315 case '\f':
316 OS << "'\\f'";
317 break;
318 case '\n':
319 OS << "'\\n'";
320 break;
321 case '\r':
322 OS << "'\\r'";
323 break;
324 case '\t':
325 OS << "'\\t'";
326 break;
327 case '\v':
328 OS << "'\\v'";
329 break;
330 default:
331 if (isprint(value) && value < 256) {
332 OS << "'" << (char)value << "'";
333 } else if (value < 256) {
334 OS << "'\\x" << std::hex << value << std::dec << "'";
335 } else {
336 // FIXME what to really do here?
337 OS << value;
338 }
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000339 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000340}
341
Steve Naroffdf7855b2007-02-21 23:46:25 +0000342void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000343 bool isSigned = Node->getType()->isSignedIntegerType();
344 OS << Node->getValue().toString(10, isSigned);
345
346 // Emit suffixes. Integer literals are always a builtin integer type.
347 switch (cast<BuiltinType>(Node->getType().getCanonicalType())->getKind()) {
348 default: assert(0 && "Unexpected type for integer literal!");
349 case BuiltinType::Int: break; // no suffix.
350 case BuiltinType::UInt: OS << 'U'; break;
351 case BuiltinType::Long: OS << 'L'; break;
352 case BuiltinType::ULong: OS << "UL"; break;
353 case BuiltinType::LongLong: OS << "LL"; break;
354 case BuiltinType::ULongLong: OS << "ULL"; break;
355 }
Chris Lattner882f7882006-11-04 18:52:07 +0000356}
Steve Naroffab624882007-02-21 22:05:47 +0000357void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner882f7882006-11-04 18:52:07 +0000358 // FIXME: print value.
Chris Lattner06430412007-05-21 05:45:03 +0000359 OS << "~1.0~";
Chris Lattner882f7882006-11-04 18:52:07 +0000360}
Steve Naroffdf7855b2007-02-21 23:46:25 +0000361void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Chris Lattner882f7882006-11-04 18:52:07 +0000362 if (Str->isWide()) OS << 'L';
Chris Lattner5d8f4942006-11-04 20:29:31 +0000363 OS << '"';
364
365 // FIXME: this doesn't print wstrings right.
366 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
367 switch (Str->getStrData()[i]) {
368 default: OS << Str->getStrData()[i]; break;
369 // Handle some common ones to make dumps prettier.
370 case '\\': OS << "\\\\"; break;
371 case '"': OS << "\\\""; break;
372 case '\n': OS << "\\n"; break;
373 case '\t': OS << "\\t"; break;
374 case '\a': OS << "\\a"; break;
375 case '\b': OS << "\\b"; break;
376 }
377 }
378 OS << '"';
Chris Lattner882f7882006-11-04 18:52:07 +0000379}
380void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
381 OS << "(";
382 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000383 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000384}
385void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner15768702006-11-05 23:54:51 +0000386 if (!Node->isPostfix())
387 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000388 PrintExpr(Node->getSubExpr());
Chris Lattner15768702006-11-05 23:54:51 +0000389
390 if (Node->isPostfix())
391 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
392
Chris Lattner882f7882006-11-04 18:52:07 +0000393}
394void StmtPrinter::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
Chris Lattner33e8a552006-11-19 01:48:02 +0000395 OS << (Node->isSizeOf() ? "sizeof(" : "__alignof(");
Chris Lattner3dc3d772007-05-16 18:07:12 +0000396 OS << Node->getArgumentType().getAsString() << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000397}
398void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
399 PrintExpr(Node->getBase());
400 OS << "[";
401 PrintExpr(Node->getIdx());
402 OS << "]";
403}
404
405void StmtPrinter::VisitCallExpr(CallExpr *Call) {
406 PrintExpr(Call->getCallee());
407 OS << "(";
408 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
409 if (i) OS << ", ";
410 PrintExpr(Call->getArg(i));
411 }
412 OS << ")";
413}
414void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
415 PrintExpr(Node->getBase());
416 OS << (Node->isArrow() ? "->" : ".");
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000417
Chris Lattner24e0d6c2007-05-24 00:47:01 +0000418 FieldDecl *Field = Node->getMemberDecl();
419 assert(Field && "MemberExpr should alway reference a field!");
420 OS << Field->getName();
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000421}
Chris Lattner882f7882006-11-04 18:52:07 +0000422void StmtPrinter::VisitCastExpr(CastExpr *Node) {
Chris Lattner51aff8b2007-07-15 23:54:50 +0000423 OS << "(" << Node->getType().getAsString() << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000424 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000425}
Steve Naroff57eb2c52007-07-19 21:32:11 +0000426void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
427 OS << "(" << Node->getType().getAsString() << ")";
428 PrintExpr(Node->getInitializer());
429}
Steve Naroff7a5af782007-07-13 16:58:59 +0000430void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000431 // No need to print anything, simply forward to the sub expression.
432 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +0000433}
Chris Lattner882f7882006-11-04 18:52:07 +0000434void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
435 PrintExpr(Node->getLHS());
436 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
437 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000438}
Chris Lattner882f7882006-11-04 18:52:07 +0000439void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
440 PrintExpr(Node->getCond());
441 OS << " ? ";
442 PrintExpr(Node->getLHS());
Bill Wendling48fbdd02007-05-23 08:04:21 +0000443 OS << " : ";
Chris Lattner882f7882006-11-04 18:52:07 +0000444 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000445}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000446
Chris Lattnereefa10e2007-05-28 06:56:27 +0000447// GNU extensions.
448
449void StmtPrinter::VisitAddrLabel(AddrLabel *Node) {
450 OS << "&&" << Node->getLabel()->getName();
451
452}
453
454// C++
455
456void StmtPrinter::VisitCXXCastExpr(CXXCastExpr *Node) {
457 switch (Node->getOpcode()) {
458 default:
459 assert(0 && "Not a C++ cast expression");
460 abort();
461 case CXXCastExpr::ConstCast: OS << "const_cast<"; break;
462 case CXXCastExpr::DynamicCast: OS << "dynamic_cast<"; break;
463 case CXXCastExpr::ReinterpretCast: OS << "reinterpret_cast<"; break;
464 case CXXCastExpr::StaticCast: OS << "static_cast<"; break;
465 }
466
467 OS << Node->getDestType().getAsString() << ">(";
468 PrintExpr(Node->getSubExpr());
469 OS << ")";
470}
471
472void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
473 OS << (Node->getValue() ? "true" : "false");
474}
475
476
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000477//===----------------------------------------------------------------------===//
478// Stmt method implementations
479//===----------------------------------------------------------------------===//
480
481void Stmt::dump() const {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000482 // FIXME: eliminate use of <iostream>
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000483 print(std::cerr);
484}
485
486void Stmt::print(std::ostream &OS) const {
Chris Lattner882f7882006-11-04 18:52:07 +0000487 if (this == 0) {
488 OS << "<NULL>";
489 return;
490 }
491
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000492 StmtPrinter P(OS);
493 const_cast<Stmt*>(this)->visit(P);
494}