blob: 7832c74ae8988f5fd4cc635543598ca8fe476e03 [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 Lattner6e9d9b32007-07-13 05:18:11 +0000295 unsigned value = Node->getValue();
296 if (isprint(value)) {
297 OS << "'" << (char)value << "'";
298 } else {
299 // FIXME something to indicate this is a character literal?
300 OS << std::hex << std::setiosflags(std::ios_base::showbase) << value
301 << std::dec << std::resetiosflags(std::ios_base::showbase);
302 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000303}
304
Steve Naroffdf7855b2007-02-21 23:46:25 +0000305void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000306 bool isSigned = Node->getType()->isSignedIntegerType();
307 OS << Node->getValue().toString(10, isSigned);
308
309 // Emit suffixes. Integer literals are always a builtin integer type.
310 switch (cast<BuiltinType>(Node->getType().getCanonicalType())->getKind()) {
311 default: assert(0 && "Unexpected type for integer literal!");
312 case BuiltinType::Int: break; // no suffix.
313 case BuiltinType::UInt: OS << 'U'; break;
314 case BuiltinType::Long: OS << 'L'; break;
315 case BuiltinType::ULong: OS << "UL"; break;
316 case BuiltinType::LongLong: OS << "LL"; break;
317 case BuiltinType::ULongLong: OS << "ULL"; break;
318 }
Chris Lattner882f7882006-11-04 18:52:07 +0000319}
Steve Naroffab624882007-02-21 22:05:47 +0000320void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner882f7882006-11-04 18:52:07 +0000321 // FIXME: print value.
Chris Lattner06430412007-05-21 05:45:03 +0000322 OS << "~1.0~";
Chris Lattner882f7882006-11-04 18:52:07 +0000323}
Steve Naroffdf7855b2007-02-21 23:46:25 +0000324void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Chris Lattner882f7882006-11-04 18:52:07 +0000325 if (Str->isWide()) OS << 'L';
Chris Lattner5d8f4942006-11-04 20:29:31 +0000326 OS << '"';
327
328 // FIXME: this doesn't print wstrings right.
329 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
330 switch (Str->getStrData()[i]) {
331 default: OS << Str->getStrData()[i]; break;
332 // Handle some common ones to make dumps prettier.
333 case '\\': OS << "\\\\"; break;
334 case '"': OS << "\\\""; break;
335 case '\n': OS << "\\n"; break;
336 case '\t': OS << "\\t"; break;
337 case '\a': OS << "\\a"; break;
338 case '\b': OS << "\\b"; break;
339 }
340 }
341 OS << '"';
Chris Lattner882f7882006-11-04 18:52:07 +0000342}
343void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
344 OS << "(";
345 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000346 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000347}
348void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner15768702006-11-05 23:54:51 +0000349 if (!Node->isPostfix())
350 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000351 PrintExpr(Node->getSubExpr());
Chris Lattner15768702006-11-05 23:54:51 +0000352
353 if (Node->isPostfix())
354 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
355
Chris Lattner882f7882006-11-04 18:52:07 +0000356}
357void StmtPrinter::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
Chris Lattner33e8a552006-11-19 01:48:02 +0000358 OS << (Node->isSizeOf() ? "sizeof(" : "__alignof(");
Chris Lattner3dc3d772007-05-16 18:07:12 +0000359 OS << Node->getArgumentType().getAsString() << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000360}
361void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
362 PrintExpr(Node->getBase());
363 OS << "[";
364 PrintExpr(Node->getIdx());
365 OS << "]";
366}
367
368void StmtPrinter::VisitCallExpr(CallExpr *Call) {
369 PrintExpr(Call->getCallee());
370 OS << "(";
371 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
372 if (i) OS << ", ";
373 PrintExpr(Call->getArg(i));
374 }
375 OS << ")";
376}
377void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
378 PrintExpr(Node->getBase());
379 OS << (Node->isArrow() ? "->" : ".");
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000380
Chris Lattner24e0d6c2007-05-24 00:47:01 +0000381 FieldDecl *Field = Node->getMemberDecl();
382 assert(Field && "MemberExpr should alway reference a field!");
383 OS << Field->getName();
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000384}
Chris Lattner882f7882006-11-04 18:52:07 +0000385void StmtPrinter::VisitCastExpr(CastExpr *Node) {
Chris Lattner3dc3d772007-05-16 18:07:12 +0000386 OS << "(" << Node->getDestType().getAsString() << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000387 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000388}
Steve Naroff7a5af782007-07-13 16:58:59 +0000389void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000390 // No need to print anything, simply forward to the sub expression.
391 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +0000392}
Chris Lattner882f7882006-11-04 18:52:07 +0000393void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
394 PrintExpr(Node->getLHS());
395 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
396 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000397}
Chris Lattner882f7882006-11-04 18:52:07 +0000398void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
399 PrintExpr(Node->getCond());
400 OS << " ? ";
401 PrintExpr(Node->getLHS());
Bill Wendling48fbdd02007-05-23 08:04:21 +0000402 OS << " : ";
Chris Lattner882f7882006-11-04 18:52:07 +0000403 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000404}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000405
Chris Lattnereefa10e2007-05-28 06:56:27 +0000406// GNU extensions.
407
408void StmtPrinter::VisitAddrLabel(AddrLabel *Node) {
409 OS << "&&" << Node->getLabel()->getName();
410
411}
412
413// C++
414
415void StmtPrinter::VisitCXXCastExpr(CXXCastExpr *Node) {
416 switch (Node->getOpcode()) {
417 default:
418 assert(0 && "Not a C++ cast expression");
419 abort();
420 case CXXCastExpr::ConstCast: OS << "const_cast<"; break;
421 case CXXCastExpr::DynamicCast: OS << "dynamic_cast<"; break;
422 case CXXCastExpr::ReinterpretCast: OS << "reinterpret_cast<"; break;
423 case CXXCastExpr::StaticCast: OS << "static_cast<"; break;
424 }
425
426 OS << Node->getDestType().getAsString() << ">(";
427 PrintExpr(Node->getSubExpr());
428 OS << ")";
429}
430
431void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
432 OS << (Node->getValue() ? "true" : "false");
433}
434
435
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000436//===----------------------------------------------------------------------===//
437// Stmt method implementations
438//===----------------------------------------------------------------------===//
439
440void Stmt::dump() const {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000441 // FIXME: eliminate use of <iostream>
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000442 print(std::cerr);
443}
444
445void Stmt::print(std::ostream &OS) const {
Chris Lattner882f7882006-11-04 18:52:07 +0000446 if (this == 0) {
447 OS << "<NULL>";
448 return;
449 }
450
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000451 StmtPrinter P(OS);
452 const_cast<Stmt*>(this)->visit(P);
453}