blob: a3a16aec5bbbc765e4a36c550d77294d7e018260 [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 << ", ";
875 PrintExpr(Node->getInit(i));
876 }
877 OS << " }";
878}
879
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000880void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
881 OS << "va_arg(";
882 PrintExpr(Node->getSubExpr());
883 OS << ", ";
884 OS << Node->getType().getAsString();
885 OS << ")";
886}
887
Chris Lattnereefa10e2007-05-28 06:56:27 +0000888// C++
Douglas Gregor993603d2008-11-14 16:09:21 +0000889void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
890 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
891 "",
892#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
893 Spelling,
894#include "clang/Basic/OperatorKinds.def"
895 };
896
897 OverloadedOperatorKind Kind = Node->getOperator();
898 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
899 if (Node->getNumArgs() == 1) {
900 OS << OpStrings[Kind] << ' ';
901 PrintExpr(Node->getArg(0));
902 } else {
903 PrintExpr(Node->getArg(0));
904 OS << ' ' << OpStrings[Kind];
905 }
906 } else if (Kind == OO_Call) {
907 PrintExpr(Node->getArg(0));
908 OS << '(';
909 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
910 if (ArgIdx > 1)
911 OS << ", ";
912 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
913 PrintExpr(Node->getArg(ArgIdx));
914 }
915 OS << ')';
916 } else if (Kind == OO_Subscript) {
917 PrintExpr(Node->getArg(0));
918 OS << '[';
919 PrintExpr(Node->getArg(1));
920 OS << ']';
921 } else if (Node->getNumArgs() == 1) {
922 OS << OpStrings[Kind] << ' ';
923 PrintExpr(Node->getArg(0));
924 } else if (Node->getNumArgs() == 2) {
925 PrintExpr(Node->getArg(0));
926 OS << ' ' << OpStrings[Kind] << ' ';
927 PrintExpr(Node->getArg(1));
928 } else {
929 assert(false && "unknown overloaded operator");
930 }
931}
Chris Lattnereefa10e2007-05-28 06:56:27 +0000932
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000933void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
934 VisitCallExpr(cast<CallExpr>(Node));
935}
936
Douglas Gregore200adc2008-10-27 19:41:14 +0000937void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
938 OS << Node->getCastName() << '<';
939 OS << Node->getTypeAsWritten().getAsString() << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +0000940 PrintExpr(Node->getSubExpr());
941 OS << ")";
942}
943
Douglas Gregore200adc2008-10-27 19:41:14 +0000944void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
945 VisitCXXNamedCastExpr(Node);
946}
947
948void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
949 VisitCXXNamedCastExpr(Node);
950}
951
952void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
953 VisitCXXNamedCastExpr(Node);
954}
955
956void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
957 VisitCXXNamedCastExpr(Node);
958}
959
Sebastian Redlc4704762008-11-11 11:37:55 +0000960void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
961 OS << "typeid(";
962 if (Node->isTypeOperand()) {
963 OS << Node->getTypeOperand().getAsString();
964 } else {
965 PrintExpr(Node->getExprOperand());
966 }
967 OS << ")";
968}
969
Chris Lattnereefa10e2007-05-28 06:56:27 +0000970void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
971 OS << (Node->getValue() ? "true" : "false");
972}
973
Douglas Gregor97a9c812008-11-04 14:32:21 +0000974void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
975 OS << "this";
976}
977
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000978void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
979 if (Node->getSubExpr() == 0)
980 OS << "throw";
981 else {
982 OS << "throw ";
983 PrintExpr(Node->getSubExpr());
984 }
985}
986
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000987void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
988 // Nothing to print: we picked up the default argument
989}
990
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000991void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
992 OS << Node->getType().getAsString();
993 OS << "(";
994 PrintExpr(Node->getSubExpr());
995 OS << ")";
996}
997
Douglas Gregordd04d332009-01-16 18:33:17 +0000998void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
999 OS << Node->getType().getAsString();
1000 OS << "(";
1001 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1002 ArgEnd = Node->arg_end();
1003 Arg != ArgEnd; ++Arg) {
1004 if (Arg != Node->arg_begin())
1005 OS << ", ";
1006 PrintExpr(*Arg);
1007 }
1008 OS << ")";
1009}
1010
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001011void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
1012 OS << Node->getType().getAsString() << "()";
1013}
1014
Argyrios Kyrtzidisaa479132008-09-09 23:47:53 +00001015void
1016StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
1017 PrintRawDecl(E->getVarDecl());
1018}
1019
Sebastian Redlbd150f42008-11-21 19:14:01 +00001020void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1021 if (E->isGlobalNew())
1022 OS << "::";
1023 OS << "new ";
1024 unsigned NumPlace = E->getNumPlacementArgs();
1025 if (NumPlace > 0) {
1026 OS << "(";
1027 PrintExpr(E->getPlacementArg(0));
1028 for (unsigned i = 1; i < NumPlace; ++i) {
1029 OS << ", ";
1030 PrintExpr(E->getPlacementArg(i));
1031 }
1032 OS << ") ";
1033 }
1034 if (E->isParenTypeId())
1035 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001036 std::string TypeS;
1037 if (Expr *Size = E->getArraySize()) {
1038 llvm::raw_string_ostream s(TypeS);
1039 Size->printPretty(s);
1040 s.flush();
1041 TypeS = "[" + TypeS + "]";
1042 }
1043 E->getAllocatedType().getAsStringInternal(TypeS);
1044 OS << TypeS;
Sebastian Redlbd150f42008-11-21 19:14:01 +00001045 if (E->isParenTypeId())
1046 OS << ")";
1047
1048 if (E->hasInitializer()) {
1049 OS << "(";
1050 unsigned NumCons = E->getNumConstructorArgs();
1051 if (NumCons > 0) {
1052 PrintExpr(E->getConstructorArg(0));
1053 for (unsigned i = 1; i < NumCons; ++i) {
1054 OS << ", ";
1055 PrintExpr(E->getConstructorArg(i));
1056 }
1057 }
1058 OS << ")";
1059 }
1060}
1061
1062void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1063 if (E->isGlobalDelete())
1064 OS << "::";
1065 OS << "delete ";
1066 if (E->isArrayForm())
1067 OS << "[] ";
1068 PrintExpr(E->getArgument());
1069}
1070
Douglas Gregorb0846b02008-12-06 00:22:45 +00001071void StmtPrinter::VisitCXXDependentNameExpr(CXXDependentNameExpr *E) {
1072 OS << E->getName()->getName();
1073}
1074
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001075static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1076 switch (UTT) {
1077 default: assert(false && "Unknown type trait");
1078 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1079 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1080 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1081 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1082 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1083 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1084 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1085 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1086 case UTT_IsAbstract: return "__is_abstract";
1087 case UTT_IsClass: return "__is_class";
1088 case UTT_IsEmpty: return "__is_empty";
1089 case UTT_IsEnum: return "__is_enum";
1090 case UTT_IsPOD: return "__is_pod";
1091 case UTT_IsPolymorphic: return "__is_polymorphic";
1092 case UTT_IsUnion: return "__is_union";
1093 }
1094}
1095
1096void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1097 OS << getTypeTraitName(E->getTrait()) << "("
1098 << E->getQueriedType().getAsString() << ")";
1099}
1100
Anders Carlsson76f4a902007-08-21 17:43:55 +00001101// Obj-C
1102
1103void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1104 OS << "@";
1105 VisitStringLiteral(Node->getString());
1106}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001107
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001108void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001109 OS << "@encode(" << Node->getEncodedType().getAsString() << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001110}
1111
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001112void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001113 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001114}
1115
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001116void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001117 OS << "@protocol(" << Node->getProtocol()->getNameAsString() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001118}
1119
Steve Naroffd54978b2007-09-18 23:55:05 +00001120void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1121 OS << "[";
Steve Naroffc6814ea2007-10-02 20:01:56 +00001122 Expr *receiver = Mess->getReceiver();
1123 if (receiver) PrintExpr(receiver);
1124 else OS << Mess->getClassName()->getName();
Ted Kremeneka06e7122008-05-02 17:32:38 +00001125 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00001126 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001127 if (selector.isUnarySelector()) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001128 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001129 } else {
1130 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001131 if (i < selector.getNumArgs()) {
1132 if (i > 0) OS << ' ';
1133 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001134 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00001135 else
1136 OS << ":";
1137 }
1138 else OS << ", "; // Handle variadic methods.
1139
Steve Naroffc6814ea2007-10-02 20:01:56 +00001140 PrintExpr(Mess->getArg(i));
1141 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001142 }
1143 OS << "]";
1144}
1145
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001146void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) {
1147 OS << "super";
1148}
1149
Steve Naroffc540d662008-09-03 18:15:37 +00001150void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001151 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00001152 OS << "^";
1153
1154 const FunctionType *AFT = Node->getFunctionType();
1155
1156 if (isa<FunctionTypeNoProto>(AFT)) {
1157 OS << "()";
Steve Naroff415d3d52008-10-08 17:01:13 +00001158 } else if (!BD->param_empty() || cast<FunctionTypeProto>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00001159 OS << '(';
1160 std::string ParamStr;
Steve Naroff415d3d52008-10-08 17:01:13 +00001161 for (BlockDecl::param_iterator AI = BD->param_begin(),
1162 E = BD->param_end(); AI != E; ++AI) {
1163 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001164 ParamStr = (*AI)->getNameAsString();
Steve Naroffc540d662008-09-03 18:15:37 +00001165 (*AI)->getType().getAsStringInternal(ParamStr);
1166 OS << ParamStr;
1167 }
1168
Steve Naroff415d3d52008-10-08 17:01:13 +00001169 const FunctionTypeProto *FT = cast<FunctionTypeProto>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00001170 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001171 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00001172 OS << "...";
1173 }
1174 OS << ')';
1175 }
1176}
1177
Steve Naroffc540d662008-09-03 18:15:37 +00001178void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001179 OS << Node->getDecl()->getNameAsString();
Steve Naroffc540d662008-09-03 18:15:37 +00001180}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001181//===----------------------------------------------------------------------===//
1182// Stmt method implementations
1183//===----------------------------------------------------------------------===//
1184
Chris Lattnercbe4f772007-08-08 22:51:59 +00001185void Stmt::dumpPretty() const {
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001186 printPretty(llvm::errs());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001187}
1188
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001189void Stmt::printPretty(llvm::raw_ostream &OS, PrinterHelper* Helper) const {
Chris Lattner882f7882006-11-04 18:52:07 +00001190 if (this == 0) {
1191 OS << "<NULL>";
1192 return;
1193 }
1194
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001195 StmtPrinter P(OS, Helper);
Chris Lattner62249a62007-08-21 04:04:25 +00001196 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001197}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001198
1199//===----------------------------------------------------------------------===//
1200// PrinterHelper
1201//===----------------------------------------------------------------------===//
1202
1203// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +00001204PrinterHelper::~PrinterHelper() {}