blob: bdbbc12b1853a6694bb32748eb2253ad59ce7633 [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
Anders Carlsson51873c22007-07-22 07:07:56 +0000221void StmtPrinter::VisitSwitchCase(SwitchCase*) {
222 assert(0 && "SwitchCase is an abstract class");
223}
224
Chris Lattner85ed8732006-11-04 20:40:44 +0000225void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
226 Indent() << "while (";
227 PrintExpr(Node->getCond());
228 OS << ")\n";
229 PrintStmt(Node->getBody());
230}
231
232void StmtPrinter::VisitDoStmt(DoStmt *Node) {
233 Indent() << "do\n";
234 PrintStmt(Node->getBody());
Chris Lattnera076fde2007-05-31 18:21:33 +0000235 Indent() << "while ";
Chris Lattner85ed8732006-11-04 20:40:44 +0000236 PrintExpr(Node->getCond());
Chris Lattnera076fde2007-05-31 18:21:33 +0000237 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000238}
239
Chris Lattner71e23ce2006-11-04 20:18:38 +0000240void StmtPrinter::VisitForStmt(ForStmt *Node) {
241 Indent() << "for (";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000242 if (Node->getInit()) {
243 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
244 PrintRawDecl(DS->getDecl());
245 else
246 PrintExpr(cast<Expr>(Node->getInit()));
247 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000248 OS << "; ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000249 if (Node->getCond())
250 PrintExpr(Node->getCond());
Chris Lattner71e23ce2006-11-04 20:18:38 +0000251 OS << "; ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000252 if (Node->getInc())
253 PrintExpr(Node->getInc());
Chris Lattner71e23ce2006-11-04 20:18:38 +0000254 OS << ")\n";
Chris Lattner85ed8732006-11-04 20:40:44 +0000255 PrintStmt(Node->getBody());
Chris Lattner71e23ce2006-11-04 20:18:38 +0000256}
257
Chris Lattner16976d32006-11-05 01:46:01 +0000258void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000259 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000260}
261
262void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000263 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000264 PrintExpr(Node->getTarget());
Chris Lattnerb4619482007-05-31 06:00:14 +0000265 OS << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000266}
267
268void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000269 Indent() << "continue;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000270}
271
272void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000273 Indent() << "break;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000274}
275
276
Chris Lattner882f7882006-11-04 18:52:07 +0000277void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
278 Indent() << "return";
279 if (Node->getRetValue()) {
280 OS << " ";
281 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000282 }
Chris Lattnerb4619482007-05-31 06:00:14 +0000283 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000284}
285
Chris Lattner71e23ce2006-11-04 20:18:38 +0000286//===----------------------------------------------------------------------===//
287// Expr printing methods.
288//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000289
Chris Lattner882f7882006-11-04 18:52:07 +0000290void StmtPrinter::VisitExpr(Expr *Node) {
291 OS << "<<unknown expr type>>";
292}
293
294void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Chris Lattner5efbb332006-11-20 05:01:40 +0000295 OS << Node->getDecl()->getName();
Chris Lattner882f7882006-11-04 18:52:07 +0000296}
297
Anders Carlsson625bfc82007-07-21 05:21:51 +0000298void StmtPrinter::VisitPreDefinedExpr(PreDefinedExpr *Node) {
299 switch (Node->getIdentType()) {
300 default:
301 assert(0 && "unknown case");
302 case PreDefinedExpr::Func:
303 OS << "__func__";
304 break;
305 case PreDefinedExpr::Function:
306 OS << "__FUNCTION__";
307 break;
308 case PreDefinedExpr::PrettyFunction:
309 OS << "__PRETTY_FUNCTION__";
310 break;
311 }
312}
313
Steve Naroffae4143e2007-04-26 20:39:23 +0000314void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner666115c2007-07-13 23:58:20 +0000315 // FIXME should print an L for wchar_t constants
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000316 unsigned value = Node->getValue();
Chris Lattner666115c2007-07-13 23:58:20 +0000317 switch (value) {
318 case '\\':
319 OS << "'\\\\'";
320 break;
321 case '\'':
322 OS << "'\\''";
323 break;
324 case '\a':
325 // TODO: K&R: the meaning of '\\a' is different in traditional C
326 OS << "'\\a'";
327 break;
328 case '\b':
329 OS << "'\\b'";
330 break;
331 // Nonstandard escape sequence.
332 /*case '\e':
333 OS << "'\\e'";
334 break;*/
335 case '\f':
336 OS << "'\\f'";
337 break;
338 case '\n':
339 OS << "'\\n'";
340 break;
341 case '\r':
342 OS << "'\\r'";
343 break;
344 case '\t':
345 OS << "'\\t'";
346 break;
347 case '\v':
348 OS << "'\\v'";
349 break;
350 default:
351 if (isprint(value) && value < 256) {
352 OS << "'" << (char)value << "'";
353 } else if (value < 256) {
354 OS << "'\\x" << std::hex << value << std::dec << "'";
355 } else {
356 // FIXME what to really do here?
357 OS << value;
358 }
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000359 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000360}
361
Steve Naroffdf7855b2007-02-21 23:46:25 +0000362void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000363 bool isSigned = Node->getType()->isSignedIntegerType();
364 OS << Node->getValue().toString(10, isSigned);
365
366 // Emit suffixes. Integer literals are always a builtin integer type.
367 switch (cast<BuiltinType>(Node->getType().getCanonicalType())->getKind()) {
368 default: assert(0 && "Unexpected type for integer literal!");
369 case BuiltinType::Int: break; // no suffix.
370 case BuiltinType::UInt: OS << 'U'; break;
371 case BuiltinType::Long: OS << 'L'; break;
372 case BuiltinType::ULong: OS << "UL"; break;
373 case BuiltinType::LongLong: OS << "LL"; break;
374 case BuiltinType::ULongLong: OS << "ULL"; break;
375 }
Chris Lattner882f7882006-11-04 18:52:07 +0000376}
Steve Naroffab624882007-02-21 22:05:47 +0000377void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattnerbf2f3862007-08-01 00:23:58 +0000378 // FIXME: print value more precisely.
379 OS << Node->getValue();
Chris Lattner882f7882006-11-04 18:52:07 +0000380}
Steve Naroffdf7855b2007-02-21 23:46:25 +0000381void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Chris Lattner882f7882006-11-04 18:52:07 +0000382 if (Str->isWide()) OS << 'L';
Chris Lattner5d8f4942006-11-04 20:29:31 +0000383 OS << '"';
384
385 // FIXME: this doesn't print wstrings right.
386 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
387 switch (Str->getStrData()[i]) {
388 default: OS << Str->getStrData()[i]; break;
389 // Handle some common ones to make dumps prettier.
390 case '\\': OS << "\\\\"; break;
391 case '"': OS << "\\\""; break;
392 case '\n': OS << "\\n"; break;
393 case '\t': OS << "\\t"; break;
394 case '\a': OS << "\\a"; break;
395 case '\b': OS << "\\b"; break;
396 }
397 }
398 OS << '"';
Chris Lattner882f7882006-11-04 18:52:07 +0000399}
400void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
401 OS << "(";
402 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000403 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000404}
405void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner15768702006-11-05 23:54:51 +0000406 if (!Node->isPostfix())
407 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000408 PrintExpr(Node->getSubExpr());
Chris Lattner15768702006-11-05 23:54:51 +0000409
410 if (Node->isPostfix())
411 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
412
Chris Lattner882f7882006-11-04 18:52:07 +0000413}
414void StmtPrinter::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
Chris Lattner33e8a552006-11-19 01:48:02 +0000415 OS << (Node->isSizeOf() ? "sizeof(" : "__alignof(");
Chris Lattner3dc3d772007-05-16 18:07:12 +0000416 OS << Node->getArgumentType().getAsString() << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000417}
418void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
419 PrintExpr(Node->getBase());
420 OS << "[";
421 PrintExpr(Node->getIdx());
422 OS << "]";
423}
424
425void StmtPrinter::VisitCallExpr(CallExpr *Call) {
426 PrintExpr(Call->getCallee());
427 OS << "(";
428 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
429 if (i) OS << ", ";
430 PrintExpr(Call->getArg(i));
431 }
432 OS << ")";
433}
434void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
435 PrintExpr(Node->getBase());
436 OS << (Node->isArrow() ? "->" : ".");
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000437
Chris Lattner24e0d6c2007-05-24 00:47:01 +0000438 FieldDecl *Field = Node->getMemberDecl();
439 assert(Field && "MemberExpr should alway reference a field!");
440 OS << Field->getName();
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000441}
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000442void StmtPrinter::VisitOCUVectorElementExpr(OCUVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +0000443 PrintExpr(Node->getBase());
444 OS << ".";
445 OS << Node->getAccessor().getName();
446}
Chris Lattner882f7882006-11-04 18:52:07 +0000447void StmtPrinter::VisitCastExpr(CastExpr *Node) {
Chris Lattner51aff8b2007-07-15 23:54:50 +0000448 OS << "(" << Node->getType().getAsString() << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000449 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000450}
Steve Naroff57eb2c52007-07-19 21:32:11 +0000451void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
452 OS << "(" << Node->getType().getAsString() << ")";
453 PrintExpr(Node->getInitializer());
454}
Steve Naroff7a5af782007-07-13 16:58:59 +0000455void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000456 // No need to print anything, simply forward to the sub expression.
457 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +0000458}
Chris Lattner882f7882006-11-04 18:52:07 +0000459void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
460 PrintExpr(Node->getLHS());
461 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
462 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000463}
Chris Lattner882f7882006-11-04 18:52:07 +0000464void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
465 PrintExpr(Node->getCond());
466 OS << " ? ";
467 PrintExpr(Node->getLHS());
Bill Wendling48fbdd02007-05-23 08:04:21 +0000468 OS << " : ";
Chris Lattner882f7882006-11-04 18:52:07 +0000469 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000470}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000471
Chris Lattnereefa10e2007-05-28 06:56:27 +0000472// GNU extensions.
473
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000474void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000475 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000476}
477
Chris Lattner366727f2007-07-24 16:58:17 +0000478void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
479 OS << "(";
480 PrintRawCompoundStmt(E->getSubStmt());
481 OS << ")";
482}
483
Steve Naroff78864672007-08-01 22:05:33 +0000484void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
485 OS << "__builtin_types_compatible_p(";
486 OS << Node->getArgType1().getAsString() << ",";
487 OS << Node->getArgType2().getAsString() << ")";
488}
489
Steve Naroff9efdabc2007-08-03 21:21:27 +0000490void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
491 OS << "__builtin_choose_expr(";
492 PrintExpr(Node->getCond());
493 OS << ",";
494 PrintExpr(Node->getLHS());
495 OS << ",";
496 PrintExpr(Node->getRHS());
497 OS << ")";
498}
Chris Lattner366727f2007-07-24 16:58:17 +0000499
Chris Lattnereefa10e2007-05-28 06:56:27 +0000500// C++
501
502void StmtPrinter::VisitCXXCastExpr(CXXCastExpr *Node) {
503 switch (Node->getOpcode()) {
504 default:
505 assert(0 && "Not a C++ cast expression");
506 abort();
507 case CXXCastExpr::ConstCast: OS << "const_cast<"; break;
508 case CXXCastExpr::DynamicCast: OS << "dynamic_cast<"; break;
509 case CXXCastExpr::ReinterpretCast: OS << "reinterpret_cast<"; break;
510 case CXXCastExpr::StaticCast: OS << "static_cast<"; break;
511 }
512
513 OS << Node->getDestType().getAsString() << ">(";
514 PrintExpr(Node->getSubExpr());
515 OS << ")";
516}
517
518void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
519 OS << (Node->getValue() ? "true" : "false");
520}
521
522
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000523//===----------------------------------------------------------------------===//
524// Stmt method implementations
525//===----------------------------------------------------------------------===//
526
527void Stmt::dump() const {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000528 // FIXME: eliminate use of <iostream>
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000529 print(std::cerr);
530}
531
532void Stmt::print(std::ostream &OS) const {
Chris Lattner882f7882006-11-04 18:52:07 +0000533 if (this == 0) {
534 OS << "<NULL>";
535 return;
536 }
537
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000538 StmtPrinter P(OS);
539 const_cast<Stmt*>(this)->visit(P);
540}