blob: 2c77f131d8528c22a2a0fb2530121bacc49c15c0 [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//
Chris Lattnercbe4f772007-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 Lattnera3bcb7a2006-11-04 07:16:25 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "clang/AST/StmtVisitor.h"
Chris Lattner5efbb332006-11-20 05:01:40 +000016#include "clang/AST/Decl.h"
Chris Lattner29375652006-12-04 18:06:35 +000017#include "clang/AST/ExprCXX.h"
Chris Lattner6c0ff132006-11-05 00:19:50 +000018#include "clang/Lex/IdentifierTable.h"
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000019#include "llvm/Support/Compiler.h"
20#include <iostream>
Chris Lattner6e9d9b32007-07-13 05:18:11 +000021#include <iomanip>
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// StmtPrinter Visitor
26//===----------------------------------------------------------------------===//
27
28namespace {
Chris Lattner62249a62007-08-21 04:04:25 +000029 class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor<StmtPrinter> {
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000030 std::ostream &OS;
31 unsigned IndentLevel;
32 public:
33 StmtPrinter(std::ostream &os) : OS(os), IndentLevel(0) {}
34
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000035 void PrintStmt(Stmt *S, int SubIndent = 1) {
36 IndentLevel += SubIndent;
Chris Lattnera076fde2007-05-31 18:21:33 +000037 if (S && isa<Expr>(S)) {
Chris Lattner882f7882006-11-04 18:52:07 +000038 // If this is an expr used in a stmt context, indent and newline it.
39 Indent();
Chris Lattner62249a62007-08-21 04:04:25 +000040 Visit(S);
Chris Lattnerfc068c12007-05-30 17:57:36 +000041 OS << ";\n";
Chris Lattner882f7882006-11-04 18:52:07 +000042 } else if (S) {
Chris Lattner62249a62007-08-21 04:04:25 +000043 Visit(S);
Chris Lattner882f7882006-11-04 18:52:07 +000044 } else {
Chris Lattner2f6ac262007-05-28 01:47:47 +000045 Indent() << "<<<NULL STATEMENT>>>\n";
Chris Lattner882f7882006-11-04 18:52:07 +000046 }
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000047 IndentLevel -= SubIndent;
Chris Lattner882f7882006-11-04 18:52:07 +000048 }
Chris Lattner073926e2007-05-20 23:04:55 +000049
50 void PrintRawCompoundStmt(CompoundStmt *S);
Chris Lattnerfdc195a2007-06-05 20:52:47 +000051 void PrintRawDecl(Decl *D);
Chris Lattnerc0a38dd2007-06-11 22:26:23 +000052 void PrintRawIfStmt(IfStmt *If);
53
Chris Lattner882f7882006-11-04 18:52:07 +000054 void PrintExpr(Expr *E) {
55 if (E)
Chris Lattner62249a62007-08-21 04:04:25 +000056 Visit(E);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000057 else
Chris Lattner882f7882006-11-04 18:52:07 +000058 OS << "<null expr>";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000059 }
60
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000061 std::ostream &Indent(int Delta = 0) const {
Chris Lattnerd5322cd2007-05-29 23:49:07 +000062 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000063 OS << " ";
64 return OS;
65 }
66
Chris Lattner62249a62007-08-21 04:04:25 +000067 void VisitStmt(Stmt *Node);
Steve Naroff7f890eb2007-02-27 02:53:10 +000068#define STMT(N, CLASS, PARENT) \
Chris Lattner62249a62007-08-21 04:04:25 +000069 void Visit##CLASS(CLASS *Node);
Chris Lattnerf2174b62006-11-04 20:59:27 +000070#include "clang/AST/StmtNodes.def"
Chris Lattner71e23ce2006-11-04 20:18:38 +000071 };
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000072}
73
Chris Lattner71e23ce2006-11-04 20:18:38 +000074//===----------------------------------------------------------------------===//
75// Stmt printing methods.
76//===----------------------------------------------------------------------===//
77
Chris Lattner882f7882006-11-04 18:52:07 +000078void StmtPrinter::VisitStmt(Stmt *Node) {
79 Indent() << "<<unknown stmt type>>\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000080}
81
Chris Lattner073926e2007-05-20 23:04:55 +000082/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
83/// with no newline after the }.
84void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
85 OS << "{\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000086 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
Chris Lattner882f7882006-11-04 18:52:07 +000087 I != E; ++I)
88 PrintStmt(*I);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000089
Chris Lattner073926e2007-05-20 23:04:55 +000090 Indent() << "}";
91}
92
Chris Lattnerfdc195a2007-06-05 20:52:47 +000093void StmtPrinter::PrintRawDecl(Decl *D) {
94 // FIXME: Need to complete/beautify this... this code simply shows the
Steve Naroff2a8ad182007-05-29 22:59:26 +000095 // nodes are where they need to be.
Chris Lattnerfdc195a2007-06-05 20:52:47 +000096 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
97 OS << "typedef " << localType->getUnderlyingType().getAsString();
98 OS << " " << localType->getName();
99 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattnerb4522b42007-06-02 05:27:01 +0000100 // Emit storage class for vardecls.
101 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
102 switch (V->getStorageClass()) {
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000103 default: assert(0 && "Unknown storage class!");
104 case VarDecl::None: break;
105 case VarDecl::Extern: OS << "extern "; break;
106 case VarDecl::Static: OS << "static "; break;
107 case VarDecl::Auto: OS << "auto "; break;
108 case VarDecl::Register: OS << "register "; break;
Chris Lattnerb4522b42007-06-02 05:27:01 +0000109 }
110 }
111
Chris Lattnere0c4ae12007-06-02 03:38:08 +0000112 std::string Name = VD->getName();
113 VD->getType().getAsStringInternal(Name);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000114 OS << Name;
115
Chris Lattner79c57592007-07-12 00:36:32 +0000116 // If this is a vardecl with an initializer, emit it.
117 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
118 if (V->getInit()) {
119 OS << " = ";
120 PrintExpr(V->getInit());
121 }
122 }
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000123 } else {
Chris Lattnere0c4ae12007-06-02 03:38:08 +0000124 // FIXME: "struct x;"
125 assert(0 && "Unexpected decl");
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000126 }
127}
128
129
130void StmtPrinter::VisitNullStmt(NullStmt *Node) {
131 Indent() << ";\n";
132}
133
134void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Chris Lattner776fac82007-06-09 00:53:06 +0000135 for (Decl *D = Node->getDecl(); D; D = D->getNextDeclarator()) {
136 Indent();
137 PrintRawDecl(D);
138 OS << ";\n";
139 }
Steve Naroff2a8ad182007-05-29 22:59:26 +0000140}
141
Chris Lattner073926e2007-05-20 23:04:55 +0000142void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
143 Indent();
144 PrintRawCompoundStmt(Node);
Chris Lattnerdf3cafb2007-05-31 05:08:56 +0000145 OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000146}
147
Chris Lattner6c0ff132006-11-05 00:19:50 +0000148void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000149 Indent(-1) << "case ";
Chris Lattner6c0ff132006-11-05 00:19:50 +0000150 PrintExpr(Node->getLHS());
151 if (Node->getRHS()) {
152 OS << " ... ";
153 PrintExpr(Node->getRHS());
154 }
155 OS << ":\n";
156
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000157 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000158}
159
160void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000161 Indent(-1) << "default:\n";
162 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000163}
164
165void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000166 Indent(-1) << Node->getName() << ":\n";
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000167 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000168}
169
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000170void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
171 OS << "if ";
Chris Lattner882f7882006-11-04 18:52:07 +0000172 PrintExpr(If->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000173
174 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
175 OS << ' ';
176 PrintRawCompoundStmt(CS);
177 OS << (If->getElse() ? ' ' : '\n');
178 } else {
179 OS << '\n';
180 PrintStmt(If->getThen());
181 if (If->getElse()) Indent();
182 }
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000183
Chris Lattner073926e2007-05-20 23:04:55 +0000184 if (Stmt *Else = If->getElse()) {
185 OS << "else";
186
187 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
188 OS << ' ';
189 PrintRawCompoundStmt(CS);
190 OS << '\n';
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000191 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
192 OS << ' ';
193 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000194 } else {
195 OS << '\n';
196 PrintStmt(If->getElse());
197 }
Chris Lattner882f7882006-11-04 18:52:07 +0000198 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000199}
200
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000201void StmtPrinter::VisitIfStmt(IfStmt *If) {
202 Indent();
203 PrintRawIfStmt(If);
204}
205
Chris Lattnerf2174b62006-11-04 20:59:27 +0000206void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
207 Indent() << "switch (";
208 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000209 OS << ")";
210
211 // Pretty print compoundstmt bodies (very common).
212 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
213 OS << " ";
214 PrintRawCompoundStmt(CS);
215 OS << "\n";
216 } else {
217 OS << "\n";
218 PrintStmt(Node->getBody());
219 }
Chris Lattnerf2174b62006-11-04 20:59:27 +0000220}
221
Anders Carlsson51873c22007-07-22 07:07:56 +0000222void StmtPrinter::VisitSwitchCase(SwitchCase*) {
223 assert(0 && "SwitchCase is an abstract class");
224}
225
Chris Lattner85ed8732006-11-04 20:40:44 +0000226void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
227 Indent() << "while (";
228 PrintExpr(Node->getCond());
229 OS << ")\n";
230 PrintStmt(Node->getBody());
231}
232
233void StmtPrinter::VisitDoStmt(DoStmt *Node) {
234 Indent() << "do\n";
235 PrintStmt(Node->getBody());
Chris Lattnera076fde2007-05-31 18:21:33 +0000236 Indent() << "while ";
Chris Lattner85ed8732006-11-04 20:40:44 +0000237 PrintExpr(Node->getCond());
Chris Lattnera076fde2007-05-31 18:21:33 +0000238 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000239}
240
Chris Lattner71e23ce2006-11-04 20:18:38 +0000241void StmtPrinter::VisitForStmt(ForStmt *Node) {
242 Indent() << "for (";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000243 if (Node->getInit()) {
244 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
245 PrintRawDecl(DS->getDecl());
246 else
247 PrintExpr(cast<Expr>(Node->getInit()));
248 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000249 OS << "; ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000250 if (Node->getCond())
251 PrintExpr(Node->getCond());
Chris Lattner71e23ce2006-11-04 20:18:38 +0000252 OS << "; ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000253 if (Node->getInc())
254 PrintExpr(Node->getInc());
Chris Lattner71e23ce2006-11-04 20:18:38 +0000255 OS << ")\n";
Chris Lattner85ed8732006-11-04 20:40:44 +0000256 PrintStmt(Node->getBody());
Chris Lattner71e23ce2006-11-04 20:18:38 +0000257}
258
Chris Lattner16976d32006-11-05 01:46:01 +0000259void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000260 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000261}
262
263void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000264 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000265 PrintExpr(Node->getTarget());
Chris Lattnerb4619482007-05-31 06:00:14 +0000266 OS << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000267}
268
269void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000270 Indent() << "continue;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000271}
272
273void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000274 Indent() << "break;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000275}
276
277
Chris Lattner882f7882006-11-04 18:52:07 +0000278void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
279 Indent() << "return";
280 if (Node->getRetValue()) {
281 OS << " ";
282 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000283 }
Chris Lattnerb4619482007-05-31 06:00:14 +0000284 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000285}
286
Chris Lattner71e23ce2006-11-04 20:18:38 +0000287//===----------------------------------------------------------------------===//
288// Expr printing methods.
289//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000290
Chris Lattner882f7882006-11-04 18:52:07 +0000291void StmtPrinter::VisitExpr(Expr *Node) {
292 OS << "<<unknown expr type>>";
293}
294
295void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Chris Lattner5efbb332006-11-20 05:01:40 +0000296 OS << Node->getDecl()->getName();
Chris Lattner882f7882006-11-04 18:52:07 +0000297}
298
Anders Carlsson625bfc82007-07-21 05:21:51 +0000299void StmtPrinter::VisitPreDefinedExpr(PreDefinedExpr *Node) {
300 switch (Node->getIdentType()) {
301 default:
302 assert(0 && "unknown case");
303 case PreDefinedExpr::Func:
304 OS << "__func__";
305 break;
306 case PreDefinedExpr::Function:
307 OS << "__FUNCTION__";
308 break;
309 case PreDefinedExpr::PrettyFunction:
310 OS << "__PRETTY_FUNCTION__";
311 break;
312 }
313}
314
Steve Naroffae4143e2007-04-26 20:39:23 +0000315void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner666115c2007-07-13 23:58:20 +0000316 // FIXME should print an L for wchar_t constants
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000317 unsigned value = Node->getValue();
Chris Lattner666115c2007-07-13 23:58:20 +0000318 switch (value) {
319 case '\\':
320 OS << "'\\\\'";
321 break;
322 case '\'':
323 OS << "'\\''";
324 break;
325 case '\a':
326 // TODO: K&R: the meaning of '\\a' is different in traditional C
327 OS << "'\\a'";
328 break;
329 case '\b':
330 OS << "'\\b'";
331 break;
332 // Nonstandard escape sequence.
333 /*case '\e':
334 OS << "'\\e'";
335 break;*/
336 case '\f':
337 OS << "'\\f'";
338 break;
339 case '\n':
340 OS << "'\\n'";
341 break;
342 case '\r':
343 OS << "'\\r'";
344 break;
345 case '\t':
346 OS << "'\\t'";
347 break;
348 case '\v':
349 OS << "'\\v'";
350 break;
351 default:
352 if (isprint(value) && value < 256) {
353 OS << "'" << (char)value << "'";
354 } else if (value < 256) {
355 OS << "'\\x" << std::hex << value << std::dec << "'";
356 } else {
357 // FIXME what to really do here?
358 OS << value;
359 }
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000360 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000361}
362
Steve Naroffdf7855b2007-02-21 23:46:25 +0000363void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000364 bool isSigned = Node->getType()->isSignedIntegerType();
365 OS << Node->getValue().toString(10, isSigned);
366
367 // Emit suffixes. Integer literals are always a builtin integer type.
368 switch (cast<BuiltinType>(Node->getType().getCanonicalType())->getKind()) {
369 default: assert(0 && "Unexpected type for integer literal!");
370 case BuiltinType::Int: break; // no suffix.
371 case BuiltinType::UInt: OS << 'U'; break;
372 case BuiltinType::Long: OS << 'L'; break;
373 case BuiltinType::ULong: OS << "UL"; break;
374 case BuiltinType::LongLong: OS << "LL"; break;
375 case BuiltinType::ULongLong: OS << "ULL"; break;
376 }
Chris Lattner882f7882006-11-04 18:52:07 +0000377}
Steve Naroffab624882007-02-21 22:05:47 +0000378void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattnerbf2f3862007-08-01 00:23:58 +0000379 // FIXME: print value more precisely.
380 OS << Node->getValue();
Chris Lattner882f7882006-11-04 18:52:07 +0000381}
Steve Naroffdf7855b2007-02-21 23:46:25 +0000382void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Chris Lattner882f7882006-11-04 18:52:07 +0000383 if (Str->isWide()) OS << 'L';
Chris Lattner5d8f4942006-11-04 20:29:31 +0000384 OS << '"';
385
386 // FIXME: this doesn't print wstrings right.
387 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
388 switch (Str->getStrData()[i]) {
389 default: OS << Str->getStrData()[i]; break;
390 // Handle some common ones to make dumps prettier.
391 case '\\': OS << "\\\\"; break;
392 case '"': OS << "\\\""; break;
393 case '\n': OS << "\\n"; break;
394 case '\t': OS << "\\t"; break;
395 case '\a': OS << "\\a"; break;
396 case '\b': OS << "\\b"; break;
397 }
398 }
399 OS << '"';
Chris Lattner882f7882006-11-04 18:52:07 +0000400}
401void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
402 OS << "(";
403 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000404 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000405}
406void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner15768702006-11-05 23:54:51 +0000407 if (!Node->isPostfix())
408 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000409 PrintExpr(Node->getSubExpr());
Chris Lattner15768702006-11-05 23:54:51 +0000410
411 if (Node->isPostfix())
412 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
413
Chris Lattner882f7882006-11-04 18:52:07 +0000414}
415void StmtPrinter::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
Chris Lattner33e8a552006-11-19 01:48:02 +0000416 OS << (Node->isSizeOf() ? "sizeof(" : "__alignof(");
Chris Lattner3dc3d772007-05-16 18:07:12 +0000417 OS << Node->getArgumentType().getAsString() << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000418}
419void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000420 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000421 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +0000422 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000423 OS << "]";
424}
425
426void StmtPrinter::VisitCallExpr(CallExpr *Call) {
427 PrintExpr(Call->getCallee());
428 OS << "(";
429 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
430 if (i) OS << ", ";
431 PrintExpr(Call->getArg(i));
432 }
433 OS << ")";
434}
435void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
436 PrintExpr(Node->getBase());
437 OS << (Node->isArrow() ? "->" : ".");
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000438
Chris Lattner24e0d6c2007-05-24 00:47:01 +0000439 FieldDecl *Field = Node->getMemberDecl();
440 assert(Field && "MemberExpr should alway reference a field!");
441 OS << Field->getName();
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000442}
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000443void StmtPrinter::VisitOCUVectorElementExpr(OCUVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +0000444 PrintExpr(Node->getBase());
445 OS << ".";
446 OS << Node->getAccessor().getName();
447}
Chris Lattner882f7882006-11-04 18:52:07 +0000448void StmtPrinter::VisitCastExpr(CastExpr *Node) {
Chris Lattner51aff8b2007-07-15 23:54:50 +0000449 OS << "(" << Node->getType().getAsString() << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000450 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000451}
Steve Naroff57eb2c52007-07-19 21:32:11 +0000452void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
453 OS << "(" << Node->getType().getAsString() << ")";
454 PrintExpr(Node->getInitializer());
455}
Steve Naroff7a5af782007-07-13 16:58:59 +0000456void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000457 // No need to print anything, simply forward to the sub expression.
458 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +0000459}
Chris Lattner882f7882006-11-04 18:52:07 +0000460void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
461 PrintExpr(Node->getLHS());
462 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
463 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000464}
Chris Lattner882f7882006-11-04 18:52:07 +0000465void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
466 PrintExpr(Node->getCond());
467 OS << " ? ";
468 PrintExpr(Node->getLHS());
Bill Wendling48fbdd02007-05-23 08:04:21 +0000469 OS << " : ";
Chris Lattner882f7882006-11-04 18:52:07 +0000470 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000471}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000472
Chris Lattnereefa10e2007-05-28 06:56:27 +0000473// GNU extensions.
474
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000475void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000476 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000477}
478
Chris Lattner366727f2007-07-24 16:58:17 +0000479void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
480 OS << "(";
481 PrintRawCompoundStmt(E->getSubStmt());
482 OS << ")";
483}
484
Steve Naroff78864672007-08-01 22:05:33 +0000485void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
486 OS << "__builtin_types_compatible_p(";
487 OS << Node->getArgType1().getAsString() << ",";
488 OS << Node->getArgType2().getAsString() << ")";
489}
490
Steve Naroff9efdabc2007-08-03 21:21:27 +0000491void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
492 OS << "__builtin_choose_expr(";
493 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +0000494 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000495 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +0000496 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000497 PrintExpr(Node->getRHS());
498 OS << ")";
499}
Chris Lattner366727f2007-07-24 16:58:17 +0000500
Chris Lattnereefa10e2007-05-28 06:56:27 +0000501// C++
502
503void StmtPrinter::VisitCXXCastExpr(CXXCastExpr *Node) {
Chris Lattner3f5e4682007-08-09 17:34:19 +0000504 OS << CXXCastExpr::getOpcodeStr(Node->getOpcode()) << '<';
Chris Lattnereefa10e2007-05-28 06:56:27 +0000505 OS << Node->getDestType().getAsString() << ">(";
506 PrintExpr(Node->getSubExpr());
507 OS << ")";
508}
509
510void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
511 OS << (Node->getValue() ? "true" : "false");
512}
513
Anders Carlsson76f4a902007-08-21 17:43:55 +0000514// Obj-C
515
516void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
517 OS << "@";
518 VisitStringLiteral(Node->getString());
519}
Chris Lattnereefa10e2007-05-28 06:56:27 +0000520
Anders Carlssonc5a81eb2007-08-22 15:14:15 +0000521void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
522 OS << "@encode(";
523 OS << Node->getEncodedType().getAsString() << ")";
524}
525
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000526//===----------------------------------------------------------------------===//
527// Stmt method implementations
528//===----------------------------------------------------------------------===//
529
Chris Lattnercbe4f772007-08-08 22:51:59 +0000530void Stmt::dumpPretty() const {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000531 // FIXME: eliminate use of <iostream>
Chris Lattnercbe4f772007-08-08 22:51:59 +0000532 printPretty(std::cerr);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000533}
534
Chris Lattnercbe4f772007-08-08 22:51:59 +0000535void Stmt::printPretty(std::ostream &OS) const {
Chris Lattner882f7882006-11-04 18:52:07 +0000536 if (this == 0) {
537 OS << "<NULL>";
538 return;
539 }
540
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000541 StmtPrinter P(OS);
Chris Lattner62249a62007-08-21 04:04:25 +0000542 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000543}