blob: 1f07db76925d2901753de832a7a2b48999430bb0 [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//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00007//
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"
Douglas Gregorc7acfdf2009-01-06 05:10:23 +000016#include "clang/AST/DeclCXX.h"
Ted Kremenek5c84c012007-10-17 18:36:42 +000017#include "clang/AST/DeclObjC.h"
Ted Kremenek04f3cee2007-08-31 21:30:12 +000018#include "clang/AST/PrettyPrinter.h"
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000019#include "llvm/Support/Compiler.h"
Ted Kremenek871422e2007-11-26 22:50:46 +000020#include "llvm/Support/Streams.h"
Ted Kremenek2d470fc2008-09-13 05:16:45 +000021#include "llvm/Support/Format.h"
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> {
Ted Kremenek2d470fc2008-09-13 05:16:45 +000030 llvm::raw_ostream &OS;
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000031 unsigned IndentLevel;
Ted Kremenek04f3cee2007-08-31 21:30:12 +000032 clang::PrinterHelper* Helper;
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000033 public:
Ted Kremenek2d470fc2008-09-13 05:16:45 +000034 StmtPrinter(llvm::raw_ostream &os, PrinterHelper* helper) :
Ted Kremenek04f3cee2007-08-31 21:30:12 +000035 OS(os), IndentLevel(0), Helper(helper) {}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000036
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000037 void PrintStmt(Stmt *S, int SubIndent = 1) {
38 IndentLevel += SubIndent;
Chris Lattnera076fde2007-05-31 18:21:33 +000039 if (S && isa<Expr>(S)) {
Chris Lattner882f7882006-11-04 18:52:07 +000040 // If this is an expr used in a stmt context, indent and newline it.
41 Indent();
Chris Lattner62249a62007-08-21 04:04:25 +000042 Visit(S);
Chris Lattnerfc068c12007-05-30 17:57:36 +000043 OS << ";\n";
Chris Lattner882f7882006-11-04 18:52:07 +000044 } else if (S) {
Chris Lattner62249a62007-08-21 04:04:25 +000045 Visit(S);
Chris Lattner882f7882006-11-04 18:52:07 +000046 } else {
Chris Lattner2f6ac262007-05-28 01:47:47 +000047 Indent() << "<<<NULL STATEMENT>>>\n";
Chris Lattner882f7882006-11-04 18:52:07 +000048 }
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000049 IndentLevel -= SubIndent;
Chris Lattner882f7882006-11-04 18:52:07 +000050 }
Chris Lattner073926e2007-05-20 23:04:55 +000051
52 void PrintRawCompoundStmt(CompoundStmt *S);
Chris Lattnerfdc195a2007-06-05 20:52:47 +000053 void PrintRawDecl(Decl *D);
Ted Kremenek15e6b402008-10-06 18:39:36 +000054 void PrintRawDeclStmt(DeclStmt *S);
Chris Lattnerc0a38dd2007-06-11 22:26:23 +000055 void PrintRawIfStmt(IfStmt *If);
Sebastian Redl9b244a82008-12-22 21:35:02 +000056 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
Chris Lattnerc0a38dd2007-06-11 22:26:23 +000057
Chris Lattner882f7882006-11-04 18:52:07 +000058 void PrintExpr(Expr *E) {
59 if (E)
Chris Lattner62249a62007-08-21 04:04:25 +000060 Visit(E);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000061 else
Chris Lattner882f7882006-11-04 18:52:07 +000062 OS << "<null expr>";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000063 }
64
Ted Kremenek2d470fc2008-09-13 05:16:45 +000065 llvm::raw_ostream &Indent(int Delta = 0) const {
Chris Lattnerd5322cd2007-05-29 23:49:07 +000066 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000067 OS << " ";
68 return OS;
69 }
70
Chris Lattner98dbf0a2007-08-30 17:59:59 +000071 bool PrintOffsetOfDesignator(Expr *E);
72 void VisitUnaryOffsetOf(UnaryOperator *Node);
73
Ted Kremenek04f3cee2007-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 Lattner62249a62007-08-21 04:04:25 +000080 void VisitStmt(Stmt *Node);
Douglas Gregorbe35ce92008-11-14 12:46:07 +000081#define STMT(CLASS, PARENT) \
Chris Lattner62249a62007-08-21 04:04:25 +000082 void Visit##CLASS(CLASS *Node);
Chris Lattnerf2174b62006-11-04 20:59:27 +000083#include "clang/AST/StmtNodes.def"
Chris Lattner71e23ce2006-11-04 20:18:38 +000084 };
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000085}
86
Chris Lattner71e23ce2006-11-04 20:18:38 +000087//===----------------------------------------------------------------------===//
88// Stmt printing methods.
89//===----------------------------------------------------------------------===//
90
Chris Lattner882f7882006-11-04 18:52:07 +000091void StmtPrinter::VisitStmt(Stmt *Node) {
92 Indent() << "<<unknown stmt type>>\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000093}
94
Chris Lattner073926e2007-05-20 23:04:55 +000095/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
96/// with no newline after the }.
97void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
98 OS << "{\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000099 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
Chris Lattner882f7882006-11-04 18:52:07 +0000100 I != E; ++I)
101 PrintStmt(*I);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000102
Chris Lattner073926e2007-05-20 23:04:55 +0000103 Indent() << "}";
104}
105
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000106void StmtPrinter::PrintRawDecl(Decl *D) {
107 // FIXME: Need to complete/beautify this... this code simply shows the
Steve Naroff2a8ad182007-05-29 22:59:26 +0000108 // nodes are where they need to be.
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000109 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
110 OS << "typedef " << localType->getUnderlyingType().getAsString();
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000111 OS << " " << localType->getNameAsString();
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000112 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattnerb4522b42007-06-02 05:27:01 +0000113 // Emit storage class for vardecls.
114 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
115 switch (V->getStorageClass()) {
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000116 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;
Chris Lattnerb4522b42007-06-02 05:27:01 +0000122 }
123 }
124
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000125 std::string Name = VD->getNameAsString();
Chris Lattnere0c4ae12007-06-02 03:38:08 +0000126 VD->getType().getAsStringInternal(Name);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000127 OS << Name;
128
Chris Lattner79c57592007-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 }
Steve Naroffc1e6d602007-11-17 21:21:01 +0000136 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
137 // print a free standing tag decl (e.g. "struct x;").
138 OS << TD->getKindName();
139 OS << " ";
140 if (const IdentifierInfo *II = TD->getIdentifier())
141 OS << II->getName();
142 else
143 OS << "<anonymous>";
144 // FIXME: print tag bodies.
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000145 } else {
Chris Lattnere0c4ae12007-06-02 03:38:08 +0000146 assert(0 && "Unexpected decl");
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000147 }
148}
149
Ted Kremenek15e6b402008-10-06 18:39:36 +0000150void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
151 bool isFirst = false;
152
153 for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();
154 I != E; ++I) {
155
156 if (!isFirst) OS << ", ";
157 else isFirst = false;
158
159 PrintRawDecl(*I);
160 }
161}
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000162
163void StmtPrinter::VisitNullStmt(NullStmt *Node) {
164 Indent() << ";\n";
165}
166
167void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Ted Kremenek15e6b402008-10-06 18:39:36 +0000168 for (DeclStmt::decl_iterator I = Node->decl_begin(), E = Node->decl_end();
169 I!=E; ++I) {
Chris Lattner776fac82007-06-09 00:53:06 +0000170 Indent();
Ted Kremenek15e6b402008-10-06 18:39:36 +0000171 PrintRawDecl(*I);
Chris Lattner776fac82007-06-09 00:53:06 +0000172 OS << ";\n";
173 }
Steve Naroff2a8ad182007-05-29 22:59:26 +0000174}
175
Chris Lattner073926e2007-05-20 23:04:55 +0000176void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
177 Indent();
178 PrintRawCompoundStmt(Node);
Chris Lattnerdf3cafb2007-05-31 05:08:56 +0000179 OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000180}
181
Chris Lattner6c0ff132006-11-05 00:19:50 +0000182void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000183 Indent(-1) << "case ";
Chris Lattner6c0ff132006-11-05 00:19:50 +0000184 PrintExpr(Node->getLHS());
185 if (Node->getRHS()) {
186 OS << " ... ";
187 PrintExpr(Node->getRHS());
188 }
189 OS << ":\n";
190
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000191 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000192}
193
194void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000195 Indent(-1) << "default:\n";
196 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000197}
198
199void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000200 Indent(-1) << Node->getName() << ":\n";
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000201 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000202}
203
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000204void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
205 OS << "if ";
Chris Lattner882f7882006-11-04 18:52:07 +0000206 PrintExpr(If->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000207
208 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
209 OS << ' ';
210 PrintRawCompoundStmt(CS);
211 OS << (If->getElse() ? ' ' : '\n');
212 } else {
213 OS << '\n';
214 PrintStmt(If->getThen());
215 if (If->getElse()) Indent();
216 }
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000217
Chris Lattner073926e2007-05-20 23:04:55 +0000218 if (Stmt *Else = If->getElse()) {
219 OS << "else";
220
221 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
222 OS << ' ';
223 PrintRawCompoundStmt(CS);
224 OS << '\n';
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000225 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
226 OS << ' ';
227 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000228 } else {
229 OS << '\n';
230 PrintStmt(If->getElse());
231 }
Chris Lattner882f7882006-11-04 18:52:07 +0000232 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000233}
234
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000235void StmtPrinter::VisitIfStmt(IfStmt *If) {
236 Indent();
237 PrintRawIfStmt(If);
238}
239
Chris Lattnerf2174b62006-11-04 20:59:27 +0000240void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
241 Indent() << "switch (";
242 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000243 OS << ")";
244
245 // Pretty print compoundstmt bodies (very common).
246 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
247 OS << " ";
248 PrintRawCompoundStmt(CS);
249 OS << "\n";
250 } else {
251 OS << "\n";
252 PrintStmt(Node->getBody());
253 }
Chris Lattnerf2174b62006-11-04 20:59:27 +0000254}
255
Anders Carlsson51873c22007-07-22 07:07:56 +0000256void StmtPrinter::VisitSwitchCase(SwitchCase*) {
257 assert(0 && "SwitchCase is an abstract class");
258}
259
Chris Lattner85ed8732006-11-04 20:40:44 +0000260void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
261 Indent() << "while (";
262 PrintExpr(Node->getCond());
263 OS << ")\n";
264 PrintStmt(Node->getBody());
265}
266
267void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000268 Indent() << "do ";
269 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
270 PrintRawCompoundStmt(CS);
271 OS << " ";
272 } else {
273 OS << "\n";
274 PrintStmt(Node->getBody());
275 Indent();
276 }
277
278 OS << "while ";
Chris Lattner85ed8732006-11-04 20:40:44 +0000279 PrintExpr(Node->getCond());
Chris Lattnera076fde2007-05-31 18:21:33 +0000280 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000281}
282
Chris Lattner71e23ce2006-11-04 20:18:38 +0000283void StmtPrinter::VisitForStmt(ForStmt *Node) {
284 Indent() << "for (";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000285 if (Node->getInit()) {
286 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000287 PrintRawDeclStmt(DS);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000288 else
289 PrintExpr(cast<Expr>(Node->getInit()));
290 }
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000291 OS << ";";
292 if (Node->getCond()) {
293 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000294 PrintExpr(Node->getCond());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000295 }
296 OS << ";";
297 if (Node->getInc()) {
298 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000299 PrintExpr(Node->getInc());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000300 }
301 OS << ") ";
302
303 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
304 PrintRawCompoundStmt(CS);
305 OS << "\n";
306 } else {
307 OS << "\n";
308 PrintStmt(Node->getBody());
309 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000310}
311
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000312void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian83615522008-01-02 22:54:34 +0000313 Indent() << "for (";
314 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000315 PrintRawDeclStmt(DS);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000316 else
317 PrintExpr(cast<Expr>(Node->getElement()));
318 OS << " in ";
319 PrintExpr(Node->getCollection());
320 OS << ") ";
321
322 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
323 PrintRawCompoundStmt(CS);
324 OS << "\n";
325 } else {
326 OS << "\n";
327 PrintStmt(Node->getBody());
328 }
329}
330
Chris Lattner16976d32006-11-05 01:46:01 +0000331void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000332 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000333}
334
335void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000336 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000337 PrintExpr(Node->getTarget());
Chris Lattnerb4619482007-05-31 06:00:14 +0000338 OS << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000339}
340
341void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000342 Indent() << "continue;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000343}
344
345void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000346 Indent() << "break;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000347}
348
349
Chris Lattner882f7882006-11-04 18:52:07 +0000350void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
351 Indent() << "return";
352 if (Node->getRetValue()) {
353 OS << " ";
354 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000355 }
Chris Lattnerb4619482007-05-31 06:00:14 +0000356 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000357}
358
Chris Lattner73c56c02007-10-29 04:04:16 +0000359
360void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson660bdd12007-11-23 23:12:25 +0000361 Indent() << "asm ";
362
363 if (Node->isVolatile())
364 OS << "volatile ";
365
366 OS << "(";
Anders Carlsson81a5a692007-11-20 19:21:03 +0000367 VisitStringLiteral(Node->getAsmString());
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000368
369 // Outputs
370 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
371 Node->getNumClobbers() != 0)
372 OS << " : ";
373
374 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
375 if (i != 0)
376 OS << ", ";
377
378 if (!Node->getOutputName(i).empty()) {
379 OS << '[';
380 OS << Node->getOutputName(i);
381 OS << "] ";
382 }
383
384 VisitStringLiteral(Node->getOutputConstraint(i));
385 OS << " ";
386 Visit(Node->getOutputExpr(i));
387 }
388
389 // Inputs
390 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
391 OS << " : ";
392
393 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
394 if (i != 0)
395 OS << ", ";
396
397 if (!Node->getInputName(i).empty()) {
398 OS << '[';
399 OS << Node->getInputName(i);
400 OS << "] ";
401 }
402
403 VisitStringLiteral(Node->getInputConstraint(i));
404 OS << " ";
405 Visit(Node->getInputExpr(i));
406 }
407
408 // Clobbers
409 if (Node->getNumClobbers() != 0)
410 OS << " : ";
411
412 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
413 if (i != 0)
414 OS << ", ";
415
416 VisitStringLiteral(Node->getClobber(i));
417 }
418
Anders Carlsson81a5a692007-11-20 19:21:03 +0000419 OS << ");\n";
Chris Lattner73c56c02007-10-29 04:04:16 +0000420}
421
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000422void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000423 Indent() << "@try";
424 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
425 PrintRawCompoundStmt(TS);
426 OS << "\n";
427 }
428
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000429 for (ObjCAtCatchStmt *catchStmt =
430 static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts());
Fariborz Jahanian88157952007-11-02 18:16:07 +0000431 catchStmt;
432 catchStmt =
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000433 static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000434 Indent() << "@catch(";
435 if (catchStmt->getCatchParamStmt()) {
436 if (DeclStmt *DS = dyn_cast<DeclStmt>(catchStmt->getCatchParamStmt()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000437 PrintRawDeclStmt(DS);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000438 }
439 OS << ")";
440 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody()))
441 {
442 PrintRawCompoundStmt(CS);
443 OS << "\n";
444 }
445 }
446
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000447 if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>(
Fariborz Jahaniandefbf9a2007-11-07 00:46:42 +0000448 Node->getFinallyStmt())) {
449 Indent() << "@finally";
450 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian88157952007-11-02 18:16:07 +0000451 OS << "\n";
452 }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000453}
454
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000455void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000456}
457
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000458void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000459 Indent() << "@catch (...) { /* todo */ } \n";
460}
461
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000462void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000463 Indent() << "@throw";
464 if (Node->getThrowExpr()) {
465 OS << " ";
466 PrintExpr(Node->getThrowExpr());
467 }
468 OS << ";\n";
469}
470
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000471void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000472 Indent() << "@synchronized (";
473 PrintExpr(Node->getSynchExpr());
474 OS << ")";
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000475 PrintRawCompoundStmt(Node->getSynchBody());
476 OS << "\n";
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000477}
478
Sebastian Redl9b244a82008-12-22 21:35:02 +0000479void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
480 OS << "catch (";
Sebastian Redl54c04d42008-12-22 19:15:10 +0000481 if (Decl *ExDecl = Node->getExceptionDecl())
482 PrintRawDecl(ExDecl);
483 else
484 OS << "...";
485 OS << ") ";
486 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl9b244a82008-12-22 21:35:02 +0000487}
488
489void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
490 Indent();
491 PrintRawCXXCatchStmt(Node);
492 OS << "\n";
493}
494
495void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
496 Indent() << "try ";
497 PrintRawCompoundStmt(Node->getTryBlock());
498 for(unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
499 OS << " ";
500 PrintRawCXXCatchStmt(Node->getHandler(i));
501 }
Sebastian Redl54c04d42008-12-22 19:15:10 +0000502 OS << "\n";
503}
504
Chris Lattner71e23ce2006-11-04 20:18:38 +0000505//===----------------------------------------------------------------------===//
506// Expr printing methods.
507//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000508
Chris Lattner882f7882006-11-04 18:52:07 +0000509void StmtPrinter::VisitExpr(Expr *Node) {
510 OS << "<<unknown expr type>>";
511}
512
513void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000514 OS << Node->getDecl()->getNameAsString();
Chris Lattner882f7882006-11-04 18:52:07 +0000515}
516
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000517void StmtPrinter::VisitQualifiedDeclRefExpr(QualifiedDeclRefExpr *Node) {
518 // FIXME: Should we keep enough information in QualifiedDeclRefExpr
519 // to produce the same qualification that the user wrote?
520 llvm::SmallVector<DeclContext *, 4> Contexts;
521
522 NamedDecl *D = Node->getDecl();
523
524 // Build up a stack of contexts.
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000525 DeclContext *Ctx = D->getDeclContext();
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000526 for (; Ctx; Ctx = Ctx->getParent())
527 if (!Ctx->isTransparentContext())
528 Contexts.push_back(Ctx);
529
530 while (!Contexts.empty()) {
531 DeclContext *Ctx = Contexts.back();
532 if (isa<TranslationUnitDecl>(Ctx))
533 OS << "::";
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000534 else if (NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
535 OS << ND->getNameAsString() << "::";
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000536 Contexts.pop_back();
537 }
538
539 OS << D->getNameAsString();
540}
541
Steve Naroffe46504b2007-11-12 14:29:37 +0000542void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000543 if (Node->getBase()) {
544 PrintExpr(Node->getBase());
545 OS << (Node->isArrow() ? "->" : ".");
546 }
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000547 OS << Node->getDecl()->getNameAsString();
Steve Naroffe46504b2007-11-12 14:29:37 +0000548}
549
Steve Naroffec944032008-05-30 00:40:33 +0000550void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
551 if (Node->getBase()) {
552 PrintExpr(Node->getBase());
553 OS << ".";
554 }
Steve Naroff4588d0f2008-12-04 16:24:46 +0000555 OS << Node->getProperty()->getNameAsCString();
Steve Naroffec944032008-05-30 00:40:33 +0000556}
557
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000558void StmtPrinter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node) {
559 if (Node->getBase()) {
560 PrintExpr(Node->getBase());
561 OS << ".";
562 }
563 // FIXME: Setter/Getter names
564}
565
Chris Lattner6307f192008-08-10 01:53:14 +0000566void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson625bfc82007-07-21 05:21:51 +0000567 switch (Node->getIdentType()) {
568 default:
569 assert(0 && "unknown case");
Chris Lattner6307f192008-08-10 01:53:14 +0000570 case PredefinedExpr::Func:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000571 OS << "__func__";
572 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000573 case PredefinedExpr::Function:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000574 OS << "__FUNCTION__";
575 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000576 case PredefinedExpr::PrettyFunction:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000577 OS << "__PRETTY_FUNCTION__";
578 break;
579 }
580}
581
Steve Naroffae4143e2007-04-26 20:39:23 +0000582void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000583 unsigned value = Node->getValue();
Chris Lattnera5678cc2008-06-07 22:35:38 +0000584 if (Node->isWide())
585 OS << "L";
Chris Lattner666115c2007-07-13 23:58:20 +0000586 switch (value) {
587 case '\\':
588 OS << "'\\\\'";
589 break;
590 case '\'':
591 OS << "'\\''";
592 break;
593 case '\a':
594 // TODO: K&R: the meaning of '\\a' is different in traditional C
595 OS << "'\\a'";
596 break;
597 case '\b':
598 OS << "'\\b'";
599 break;
600 // Nonstandard escape sequence.
601 /*case '\e':
602 OS << "'\\e'";
603 break;*/
604 case '\f':
605 OS << "'\\f'";
606 break;
607 case '\n':
608 OS << "'\\n'";
609 break;
610 case '\r':
611 OS << "'\\r'";
612 break;
613 case '\t':
614 OS << "'\\t'";
615 break;
616 case '\v':
617 OS << "'\\v'";
618 break;
619 default:
Ted Kremenek652d18e2008-02-23 00:52:04 +0000620 if (value < 256 && isprint(value)) {
Chris Lattner666115c2007-07-13 23:58:20 +0000621 OS << "'" << (char)value << "'";
622 } else if (value < 256) {
Ted Kremenek2d470fc2008-09-13 05:16:45 +0000623 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner666115c2007-07-13 23:58:20 +0000624 } else {
625 // FIXME what to really do here?
626 OS << value;
627 }
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000628 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000629}
630
Steve Naroffdf7855b2007-02-21 23:46:25 +0000631void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000632 bool isSigned = Node->getType()->isSignedIntegerType();
633 OS << Node->getValue().toString(10, isSigned);
634
635 // Emit suffixes. Integer literals are always a builtin integer type.
Chris Lattner574dee62008-07-26 22:17:49 +0000636 switch (Node->getType()->getAsBuiltinType()->getKind()) {
Chris Lattner06430412007-05-21 05:45:03 +0000637 default: assert(0 && "Unexpected type for integer literal!");
638 case BuiltinType::Int: break; // no suffix.
639 case BuiltinType::UInt: OS << 'U'; break;
640 case BuiltinType::Long: OS << 'L'; break;
641 case BuiltinType::ULong: OS << "UL"; break;
642 case BuiltinType::LongLong: OS << "LL"; break;
643 case BuiltinType::ULongLong: OS << "ULL"; break;
644 }
Chris Lattner882f7882006-11-04 18:52:07 +0000645}
Steve Naroffab624882007-02-21 22:05:47 +0000646void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattnerbf2f3862007-08-01 00:23:58 +0000647 // FIXME: print value more precisely.
Chris Lattnera0173132008-06-07 22:13:43 +0000648 OS << Node->getValueAsApproximateDouble();
Chris Lattner882f7882006-11-04 18:52:07 +0000649}
Chris Lattner1c20a172007-08-26 03:42:43 +0000650
651void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
652 PrintExpr(Node->getSubExpr());
653 OS << "i";
654}
655
Steve Naroffdf7855b2007-02-21 23:46:25 +0000656void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Chris Lattner882f7882006-11-04 18:52:07 +0000657 if (Str->isWide()) OS << 'L';
Chris Lattner5d8f4942006-11-04 20:29:31 +0000658 OS << '"';
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000659
Chris Lattner5d8f4942006-11-04 20:29:31 +0000660 // FIXME: this doesn't print wstrings right.
661 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattnerbb96cc42009-01-16 19:25:18 +0000662 unsigned char Char = Str->getStrData()[i];
663
664 switch (Char) {
665 default:
666 if (isprint(Char))
667 OS << (char)Char;
668 else // Output anything hard as an octal escape.
669 OS << '\\'
670 << (char)('0'+ ((Char >> 6) & 7))
671 << (char)('0'+ ((Char >> 3) & 7))
672 << (char)('0'+ ((Char >> 0) & 7));
673 break;
674 // Handle some common non-printable cases to make dumps prettier.
Chris Lattner5d8f4942006-11-04 20:29:31 +0000675 case '\\': OS << "\\\\"; break;
676 case '"': OS << "\\\""; break;
677 case '\n': OS << "\\n"; break;
678 case '\t': OS << "\\t"; break;
679 case '\a': OS << "\\a"; break;
680 case '\b': OS << "\\b"; break;
681 }
682 }
683 OS << '"';
Chris Lattner882f7882006-11-04 18:52:07 +0000684}
685void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
686 OS << "(";
687 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000688 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000689}
690void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +0000691 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +0000692 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattnere5b60442007-08-23 21:46:40 +0000693
Sebastian Redl6f282892008-11-11 17:56:53 +0000694 // Print a space if this is an "identifier operator" like __real.
Chris Lattnere5b60442007-08-23 21:46:40 +0000695 switch (Node->getOpcode()) {
696 default: break;
Chris Lattnere5b60442007-08-23 21:46:40 +0000697 case UnaryOperator::Real:
698 case UnaryOperator::Imag:
699 case UnaryOperator::Extension:
700 OS << ' ';
701 break;
702 }
703 }
Chris Lattner882f7882006-11-04 18:52:07 +0000704 PrintExpr(Node->getSubExpr());
Chris Lattner15768702006-11-05 23:54:51 +0000705
706 if (Node->isPostfix())
707 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000708}
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000709
710bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
711 if (isa<CompoundLiteralExpr>(E)) {
712 // Base case, print the type and comma.
713 OS << E->getType().getAsString() << ", ";
714 return true;
715 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
716 PrintOffsetOfDesignator(ASE->getLHS());
717 OS << "[";
718 PrintExpr(ASE->getRHS());
719 OS << "]";
720 return false;
721 } else {
722 MemberExpr *ME = cast<MemberExpr>(E);
723 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000724 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getNameAsString();
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000725 return false;
726 }
727}
728
729void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
730 OS << "__builtin_offsetof(";
731 PrintOffsetOfDesignator(Node->getSubExpr());
732 OS << ")";
733}
734
Sebastian Redl6f282892008-11-11 17:56:53 +0000735void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
736 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
737 if (Node->isArgumentType())
738 OS << "(" << Node->getArgumentType().getAsString() << ")";
739 else {
740 OS << " ";
741 PrintExpr(Node->getArgumentExpr());
742 }
Chris Lattner882f7882006-11-04 18:52:07 +0000743}
744void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000745 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000746 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +0000747 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000748 OS << "]";
749}
750
751void StmtPrinter::VisitCallExpr(CallExpr *Call) {
752 PrintExpr(Call->getCallee());
753 OS << "(";
754 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000755 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
756 // Don't print any defaulted arguments
757 break;
758 }
759
Chris Lattner882f7882006-11-04 18:52:07 +0000760 if (i) OS << ", ";
761 PrintExpr(Call->getArg(i));
762 }
763 OS << ")";
764}
765void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregore4b414c2009-01-08 22:45:41 +0000766 // FIXME: Suppress printing implicit bases (like "this")
767 PrintExpr(Node->getBase());
768 OS << (Node->isArrow() ? "->" : ".");
769 // FIXME: Suppress printing references to unnamed objects
770 // representing anonymous unions/structs
Douglas Gregor2eedc3a2008-12-20 23:49:58 +0000771 OS << Node->getMemberDecl()->getNameAsString();
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000772}
Nate Begemance4d7fc2008-04-18 23:10:10 +0000773void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +0000774 PrintExpr(Node->getBase());
775 OS << ".";
776 OS << Node->getAccessor().getName();
777}
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +0000778void StmtPrinter::VisitCastExpr(CastExpr *) {
779 assert(0 && "CastExpr is an abstract class");
780}
Douglas Gregore200adc2008-10-27 19:41:14 +0000781void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *) {
782 assert(0 && "ExplicitCastExpr is an abstract class");
783}
Douglas Gregorf19b2312008-10-28 15:36:24 +0000784void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Chris Lattner51aff8b2007-07-15 23:54:50 +0000785 OS << "(" << Node->getType().getAsString() << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000786 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000787}
Steve Naroff57eb2c52007-07-19 21:32:11 +0000788void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
789 OS << "(" << Node->getType().getAsString() << ")";
790 PrintExpr(Node->getInitializer());
791}
Steve Naroff7a5af782007-07-13 16:58:59 +0000792void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000793 // No need to print anything, simply forward to the sub expression.
794 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +0000795}
Chris Lattner882f7882006-11-04 18:52:07 +0000796void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
797 PrintExpr(Node->getLHS());
798 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
799 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000800}
Chris Lattner86928112007-08-25 02:00:02 +0000801void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
802 PrintExpr(Node->getLHS());
803 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
804 PrintExpr(Node->getRHS());
805}
Chris Lattner882f7882006-11-04 18:52:07 +0000806void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
807 PrintExpr(Node->getCond());
Ted Kremenekebb1c0c2007-11-26 18:27:54 +0000808
809 if (Node->getLHS()) {
810 OS << " ? ";
811 PrintExpr(Node->getLHS());
812 OS << " : ";
813 }
814 else { // Handle GCC extention where LHS can be NULL.
815 OS << " ?: ";
816 }
817
Chris Lattner882f7882006-11-04 18:52:07 +0000818 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000819}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000820
Chris Lattnereefa10e2007-05-28 06:56:27 +0000821// GNU extensions.
822
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000823void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000824 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000825}
826
Chris Lattner366727f2007-07-24 16:58:17 +0000827void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
828 OS << "(";
829 PrintRawCompoundStmt(E->getSubStmt());
830 OS << ")";
831}
832
Steve Naroff78864672007-08-01 22:05:33 +0000833void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
834 OS << "__builtin_types_compatible_p(";
835 OS << Node->getArgType1().getAsString() << ",";
836 OS << Node->getArgType2().getAsString() << ")";
837}
838
Steve Naroff9efdabc2007-08-03 21:21:27 +0000839void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
840 OS << "__builtin_choose_expr(";
841 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +0000842 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000843 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +0000844 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000845 PrintExpr(Node->getRHS());
846 OS << ")";
847}
Chris Lattner366727f2007-07-24 16:58:17 +0000848
Douglas Gregor3be4b122008-11-29 04:51:27 +0000849void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
850 OS << "__null";
851}
852
Nate Begeman1e36a852008-01-17 17:46:27 +0000853void StmtPrinter::VisitOverloadExpr(OverloadExpr *Node) {
854 OS << "__builtin_overload(";
Nate Begeman936b2072008-01-30 20:50:20 +0000855 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
Nate Begeman1e36a852008-01-17 17:46:27 +0000856 if (i) OS << ", ";
Nate Begeman936b2072008-01-30 20:50:20 +0000857 PrintExpr(Node->getExpr(i));
Nate Begeman1e36a852008-01-17 17:46:27 +0000858 }
859 OS << ")";
860}
861
Eli Friedmana1b4ed82008-05-14 19:38:39 +0000862void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
863 OS << "__builtin_shufflevector(";
864 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
865 if (i) OS << ", ";
866 PrintExpr(Node->getExpr(i));
867 }
868 OS << ")";
869}
870
Anders Carlsson4692db02007-08-31 04:56:16 +0000871void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
872 OS << "{ ";
873 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
874 if (i) OS << ", ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000875 if (Node->getInit(i))
876 PrintExpr(Node->getInit(i));
877 else
878 OS << "0";
Anders Carlsson4692db02007-08-31 04:56:16 +0000879 }
880 OS << " }";
881}
882
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000883void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000884 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
885 DEnd = Node->designators_end();
886 D != DEnd; ++D) {
887 if (D->isFieldDesignator()) {
888 if (D->getDotLoc().isInvalid())
889 OS << D->getFieldName()->getName() << ":";
890 else
891 OS << "." << D->getFieldName()->getName();
892 } else {
893 OS << "[";
894 if (D->isArrayDesignator()) {
895 PrintExpr(Node->getArrayIndex(*D));
896 } else {
897 PrintExpr(Node->getArrayRangeStart(*D));
898 OS << " ... ";
899 PrintExpr(Node->getArrayRangeEnd(*D));
900 }
901 OS << "]";
902 }
903 }
904
905 OS << " = ";
906 PrintExpr(Node->getInit());
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000907}
908
Douglas Gregor0202cb42009-01-29 17:44:32 +0000909void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
910 OS << "/*implicit*/" << Node->getType().getAsString() << "()";
911}
912
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000913void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
914 OS << "va_arg(";
915 PrintExpr(Node->getSubExpr());
916 OS << ", ";
917 OS << Node->getType().getAsString();
918 OS << ")";
919}
920
Chris Lattnereefa10e2007-05-28 06:56:27 +0000921// C++
Douglas Gregor993603d2008-11-14 16:09:21 +0000922void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
923 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
924 "",
925#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
926 Spelling,
927#include "clang/Basic/OperatorKinds.def"
928 };
929
930 OverloadedOperatorKind Kind = Node->getOperator();
931 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
932 if (Node->getNumArgs() == 1) {
933 OS << OpStrings[Kind] << ' ';
934 PrintExpr(Node->getArg(0));
935 } else {
936 PrintExpr(Node->getArg(0));
937 OS << ' ' << OpStrings[Kind];
938 }
939 } else if (Kind == OO_Call) {
940 PrintExpr(Node->getArg(0));
941 OS << '(';
942 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
943 if (ArgIdx > 1)
944 OS << ", ";
945 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
946 PrintExpr(Node->getArg(ArgIdx));
947 }
948 OS << ')';
949 } else if (Kind == OO_Subscript) {
950 PrintExpr(Node->getArg(0));
951 OS << '[';
952 PrintExpr(Node->getArg(1));
953 OS << ']';
954 } else if (Node->getNumArgs() == 1) {
955 OS << OpStrings[Kind] << ' ';
956 PrintExpr(Node->getArg(0));
957 } else if (Node->getNumArgs() == 2) {
958 PrintExpr(Node->getArg(0));
959 OS << ' ' << OpStrings[Kind] << ' ';
960 PrintExpr(Node->getArg(1));
961 } else {
962 assert(false && "unknown overloaded operator");
963 }
964}
Chris Lattnereefa10e2007-05-28 06:56:27 +0000965
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000966void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
967 VisitCallExpr(cast<CallExpr>(Node));
968}
969
Douglas Gregore200adc2008-10-27 19:41:14 +0000970void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
971 OS << Node->getCastName() << '<';
972 OS << Node->getTypeAsWritten().getAsString() << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +0000973 PrintExpr(Node->getSubExpr());
974 OS << ")";
975}
976
Douglas Gregore200adc2008-10-27 19:41:14 +0000977void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
978 VisitCXXNamedCastExpr(Node);
979}
980
981void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
982 VisitCXXNamedCastExpr(Node);
983}
984
985void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
986 VisitCXXNamedCastExpr(Node);
987}
988
989void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
990 VisitCXXNamedCastExpr(Node);
991}
992
Sebastian Redlc4704762008-11-11 11:37:55 +0000993void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
994 OS << "typeid(";
995 if (Node->isTypeOperand()) {
996 OS << Node->getTypeOperand().getAsString();
997 } else {
998 PrintExpr(Node->getExprOperand());
999 }
1000 OS << ")";
1001}
1002
Chris Lattnereefa10e2007-05-28 06:56:27 +00001003void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1004 OS << (Node->getValue() ? "true" : "false");
1005}
1006
Douglas Gregor97a9c812008-11-04 14:32:21 +00001007void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1008 OS << "this";
1009}
1010
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001011void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1012 if (Node->getSubExpr() == 0)
1013 OS << "throw";
1014 else {
1015 OS << "throw ";
1016 PrintExpr(Node->getSubExpr());
1017 }
1018}
1019
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001020void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1021 // Nothing to print: we picked up the default argument
1022}
1023
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001024void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1025 OS << Node->getType().getAsString();
1026 OS << "(";
1027 PrintExpr(Node->getSubExpr());
1028 OS << ")";
1029}
1030
Douglas Gregordd04d332009-01-16 18:33:17 +00001031void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1032 OS << Node->getType().getAsString();
1033 OS << "(";
1034 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1035 ArgEnd = Node->arg_end();
1036 Arg != ArgEnd; ++Arg) {
1037 if (Arg != Node->arg_begin())
1038 OS << ", ";
1039 PrintExpr(*Arg);
1040 }
1041 OS << ")";
1042}
1043
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001044void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
1045 OS << Node->getType().getAsString() << "()";
1046}
1047
Argyrios Kyrtzidisaa479132008-09-09 23:47:53 +00001048void
1049StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
1050 PrintRawDecl(E->getVarDecl());
1051}
1052
Sebastian Redlbd150f42008-11-21 19:14:01 +00001053void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1054 if (E->isGlobalNew())
1055 OS << "::";
1056 OS << "new ";
1057 unsigned NumPlace = E->getNumPlacementArgs();
1058 if (NumPlace > 0) {
1059 OS << "(";
1060 PrintExpr(E->getPlacementArg(0));
1061 for (unsigned i = 1; i < NumPlace; ++i) {
1062 OS << ", ";
1063 PrintExpr(E->getPlacementArg(i));
1064 }
1065 OS << ") ";
1066 }
1067 if (E->isParenTypeId())
1068 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001069 std::string TypeS;
1070 if (Expr *Size = E->getArraySize()) {
1071 llvm::raw_string_ostream s(TypeS);
1072 Size->printPretty(s);
1073 s.flush();
1074 TypeS = "[" + TypeS + "]";
1075 }
1076 E->getAllocatedType().getAsStringInternal(TypeS);
1077 OS << TypeS;
Sebastian Redlbd150f42008-11-21 19:14:01 +00001078 if (E->isParenTypeId())
1079 OS << ")";
1080
1081 if (E->hasInitializer()) {
1082 OS << "(";
1083 unsigned NumCons = E->getNumConstructorArgs();
1084 if (NumCons > 0) {
1085 PrintExpr(E->getConstructorArg(0));
1086 for (unsigned i = 1; i < NumCons; ++i) {
1087 OS << ", ";
1088 PrintExpr(E->getConstructorArg(i));
1089 }
1090 }
1091 OS << ")";
1092 }
1093}
1094
1095void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1096 if (E->isGlobalDelete())
1097 OS << "::";
1098 OS << "delete ";
1099 if (E->isArrayForm())
1100 OS << "[] ";
1101 PrintExpr(E->getArgument());
1102}
1103
Douglas Gregorb8a9a412009-02-04 15:01:18 +00001104void StmtPrinter::VisitUnresolvedFunctionNameExpr(UnresolvedFunctionNameExpr *E) {
1105 OS << E->getName().getAsString();
Douglas Gregorb0846b02008-12-06 00:22:45 +00001106}
1107
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001108static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1109 switch (UTT) {
1110 default: assert(false && "Unknown type trait");
1111 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1112 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1113 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1114 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1115 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1116 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1117 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1118 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1119 case UTT_IsAbstract: return "__is_abstract";
1120 case UTT_IsClass: return "__is_class";
1121 case UTT_IsEmpty: return "__is_empty";
1122 case UTT_IsEnum: return "__is_enum";
1123 case UTT_IsPOD: return "__is_pod";
1124 case UTT_IsPolymorphic: return "__is_polymorphic";
1125 case UTT_IsUnion: return "__is_union";
1126 }
1127}
1128
1129void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1130 OS << getTypeTraitName(E->getTrait()) << "("
1131 << E->getQueriedType().getAsString() << ")";
1132}
1133
Anders Carlsson76f4a902007-08-21 17:43:55 +00001134// Obj-C
1135
1136void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1137 OS << "@";
1138 VisitStringLiteral(Node->getString());
1139}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001140
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001141void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001142 OS << "@encode(" << Node->getEncodedType().getAsString() << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001143}
1144
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001145void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001146 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001147}
1148
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001149void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001150 OS << "@protocol(" << Node->getProtocol()->getNameAsString() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001151}
1152
Steve Naroffd54978b2007-09-18 23:55:05 +00001153void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1154 OS << "[";
Steve Naroffc6814ea2007-10-02 20:01:56 +00001155 Expr *receiver = Mess->getReceiver();
1156 if (receiver) PrintExpr(receiver);
1157 else OS << Mess->getClassName()->getName();
Ted Kremeneka06e7122008-05-02 17:32:38 +00001158 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00001159 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001160 if (selector.isUnarySelector()) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001161 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001162 } else {
1163 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001164 if (i < selector.getNumArgs()) {
1165 if (i > 0) OS << ' ';
1166 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001167 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00001168 else
1169 OS << ":";
1170 }
1171 else OS << ", "; // Handle variadic methods.
1172
Steve Naroffc6814ea2007-10-02 20:01:56 +00001173 PrintExpr(Mess->getArg(i));
1174 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001175 }
1176 OS << "]";
1177}
1178
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001179void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) {
1180 OS << "super";
1181}
1182
Steve Naroffc540d662008-09-03 18:15:37 +00001183void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001184 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00001185 OS << "^";
1186
1187 const FunctionType *AFT = Node->getFunctionType();
1188
1189 if (isa<FunctionTypeNoProto>(AFT)) {
1190 OS << "()";
Steve Naroff415d3d52008-10-08 17:01:13 +00001191 } else if (!BD->param_empty() || cast<FunctionTypeProto>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00001192 OS << '(';
1193 std::string ParamStr;
Steve Naroff415d3d52008-10-08 17:01:13 +00001194 for (BlockDecl::param_iterator AI = BD->param_begin(),
1195 E = BD->param_end(); AI != E; ++AI) {
1196 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001197 ParamStr = (*AI)->getNameAsString();
Steve Naroffc540d662008-09-03 18:15:37 +00001198 (*AI)->getType().getAsStringInternal(ParamStr);
1199 OS << ParamStr;
1200 }
1201
Steve Naroff415d3d52008-10-08 17:01:13 +00001202 const FunctionTypeProto *FT = cast<FunctionTypeProto>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00001203 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001204 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00001205 OS << "...";
1206 }
1207 OS << ')';
1208 }
1209}
1210
Steve Naroffc540d662008-09-03 18:15:37 +00001211void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001212 OS << Node->getDecl()->getNameAsString();
Steve Naroffc540d662008-09-03 18:15:37 +00001213}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001214//===----------------------------------------------------------------------===//
1215// Stmt method implementations
1216//===----------------------------------------------------------------------===//
1217
Chris Lattnercbe4f772007-08-08 22:51:59 +00001218void Stmt::dumpPretty() const {
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001219 llvm::raw_ostream &OS = llvm::errs();
1220 printPretty(OS);
1221 OS.flush();
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001222}
1223
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001224void Stmt::printPretty(llvm::raw_ostream &OS, PrinterHelper* Helper) const {
Chris Lattner882f7882006-11-04 18:52:07 +00001225 if (this == 0) {
1226 OS << "<NULL>";
1227 return;
1228 }
1229
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001230 StmtPrinter P(OS, Helper);
Chris Lattner62249a62007-08-21 04:04:25 +00001231 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001232}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001233
1234//===----------------------------------------------------------------------===//
1235// PrinterHelper
1236//===----------------------------------------------------------------------===//
1237
1238// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +00001239PrinterHelper::~PrinterHelper() {}