blob: 49292afe67e3479ae1d43dd5705f5873ab3a87ad [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) {
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000205 OS << "if (";
Chris Lattner882f7882006-11-04 18:52:07 +0000206 PrintExpr(If->getCond());
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000207 OS << ')';
Chris Lattner073926e2007-05-20 23:04:55 +0000208
209 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
210 OS << ' ';
211 PrintRawCompoundStmt(CS);
212 OS << (If->getElse() ? ' ' : '\n');
213 } else {
214 OS << '\n';
215 PrintStmt(If->getThen());
216 if (If->getElse()) Indent();
217 }
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000218
Chris Lattner073926e2007-05-20 23:04:55 +0000219 if (Stmt *Else = If->getElse()) {
220 OS << "else";
221
222 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
223 OS << ' ';
224 PrintRawCompoundStmt(CS);
225 OS << '\n';
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000226 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
227 OS << ' ';
228 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000229 } else {
230 OS << '\n';
231 PrintStmt(If->getElse());
232 }
Chris Lattner882f7882006-11-04 18:52:07 +0000233 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000234}
235
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000236void StmtPrinter::VisitIfStmt(IfStmt *If) {
237 Indent();
238 PrintRawIfStmt(If);
239}
240
Chris Lattnerf2174b62006-11-04 20:59:27 +0000241void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
242 Indent() << "switch (";
243 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000244 OS << ")";
245
246 // Pretty print compoundstmt bodies (very common).
247 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
248 OS << " ";
249 PrintRawCompoundStmt(CS);
250 OS << "\n";
251 } else {
252 OS << "\n";
253 PrintStmt(Node->getBody());
254 }
Chris Lattnerf2174b62006-11-04 20:59:27 +0000255}
256
Anders Carlsson51873c22007-07-22 07:07:56 +0000257void StmtPrinter::VisitSwitchCase(SwitchCase*) {
258 assert(0 && "SwitchCase is an abstract class");
259}
260
Chris Lattner85ed8732006-11-04 20:40:44 +0000261void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
262 Indent() << "while (";
263 PrintExpr(Node->getCond());
264 OS << ")\n";
265 PrintStmt(Node->getBody());
266}
267
268void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000269 Indent() << "do ";
270 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
271 PrintRawCompoundStmt(CS);
272 OS << " ";
273 } else {
274 OS << "\n";
275 PrintStmt(Node->getBody());
276 Indent();
277 }
278
279 OS << "while ";
Chris Lattner85ed8732006-11-04 20:40:44 +0000280 PrintExpr(Node->getCond());
Chris Lattnera076fde2007-05-31 18:21:33 +0000281 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000282}
283
Chris Lattner71e23ce2006-11-04 20:18:38 +0000284void StmtPrinter::VisitForStmt(ForStmt *Node) {
285 Indent() << "for (";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000286 if (Node->getInit()) {
287 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000288 PrintRawDeclStmt(DS);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000289 else
290 PrintExpr(cast<Expr>(Node->getInit()));
291 }
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000292 OS << ";";
293 if (Node->getCond()) {
294 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000295 PrintExpr(Node->getCond());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000296 }
297 OS << ";";
298 if (Node->getInc()) {
299 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000300 PrintExpr(Node->getInc());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000301 }
302 OS << ") ";
303
304 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
305 PrintRawCompoundStmt(CS);
306 OS << "\n";
307 } else {
308 OS << "\n";
309 PrintStmt(Node->getBody());
310 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000311}
312
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000313void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian83615522008-01-02 22:54:34 +0000314 Indent() << "for (";
315 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000316 PrintRawDeclStmt(DS);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000317 else
318 PrintExpr(cast<Expr>(Node->getElement()));
319 OS << " in ";
320 PrintExpr(Node->getCollection());
321 OS << ") ";
322
323 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
324 PrintRawCompoundStmt(CS);
325 OS << "\n";
326 } else {
327 OS << "\n";
328 PrintStmt(Node->getBody());
329 }
330}
331
Chris Lattner16976d32006-11-05 01:46:01 +0000332void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000333 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000334}
335
336void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000337 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000338 PrintExpr(Node->getTarget());
Chris Lattnerb4619482007-05-31 06:00:14 +0000339 OS << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000340}
341
342void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000343 Indent() << "continue;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000344}
345
346void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000347 Indent() << "break;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000348}
349
350
Chris Lattner882f7882006-11-04 18:52:07 +0000351void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
352 Indent() << "return";
353 if (Node->getRetValue()) {
354 OS << " ";
355 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000356 }
Chris Lattnerb4619482007-05-31 06:00:14 +0000357 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000358}
359
Chris Lattner73c56c02007-10-29 04:04:16 +0000360
361void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson660bdd12007-11-23 23:12:25 +0000362 Indent() << "asm ";
363
364 if (Node->isVolatile())
365 OS << "volatile ";
366
367 OS << "(";
Anders Carlsson81a5a692007-11-20 19:21:03 +0000368 VisitStringLiteral(Node->getAsmString());
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000369
370 // Outputs
371 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
372 Node->getNumClobbers() != 0)
373 OS << " : ";
374
375 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
376 if (i != 0)
377 OS << ", ";
378
379 if (!Node->getOutputName(i).empty()) {
380 OS << '[';
381 OS << Node->getOutputName(i);
382 OS << "] ";
383 }
384
385 VisitStringLiteral(Node->getOutputConstraint(i));
386 OS << " ";
387 Visit(Node->getOutputExpr(i));
388 }
389
390 // Inputs
391 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
392 OS << " : ";
393
394 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
395 if (i != 0)
396 OS << ", ";
397
398 if (!Node->getInputName(i).empty()) {
399 OS << '[';
400 OS << Node->getInputName(i);
401 OS << "] ";
402 }
403
404 VisitStringLiteral(Node->getInputConstraint(i));
405 OS << " ";
406 Visit(Node->getInputExpr(i));
407 }
408
409 // Clobbers
410 if (Node->getNumClobbers() != 0)
411 OS << " : ";
412
413 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
414 if (i != 0)
415 OS << ", ";
416
417 VisitStringLiteral(Node->getClobber(i));
418 }
419
Anders Carlsson81a5a692007-11-20 19:21:03 +0000420 OS << ");\n";
Chris Lattner73c56c02007-10-29 04:04:16 +0000421}
422
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000423void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000424 Indent() << "@try";
425 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
426 PrintRawCompoundStmt(TS);
427 OS << "\n";
428 }
429
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000430 for (ObjCAtCatchStmt *catchStmt =
431 static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts());
Fariborz Jahanian88157952007-11-02 18:16:07 +0000432 catchStmt;
433 catchStmt =
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000434 static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000435 Indent() << "@catch(";
436 if (catchStmt->getCatchParamStmt()) {
437 if (DeclStmt *DS = dyn_cast<DeclStmt>(catchStmt->getCatchParamStmt()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000438 PrintRawDeclStmt(DS);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000439 }
440 OS << ")";
441 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody()))
442 {
443 PrintRawCompoundStmt(CS);
444 OS << "\n";
445 }
446 }
447
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000448 if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>(
Fariborz Jahaniandefbf9a2007-11-07 00:46:42 +0000449 Node->getFinallyStmt())) {
450 Indent() << "@finally";
451 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian88157952007-11-02 18:16:07 +0000452 OS << "\n";
453 }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000454}
455
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000456void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000457}
458
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000459void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000460 Indent() << "@catch (...) { /* todo */ } \n";
461}
462
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000463void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000464 Indent() << "@throw";
465 if (Node->getThrowExpr()) {
466 OS << " ";
467 PrintExpr(Node->getThrowExpr());
468 }
469 OS << ";\n";
470}
471
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000472void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000473 Indent() << "@synchronized (";
474 PrintExpr(Node->getSynchExpr());
475 OS << ")";
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000476 PrintRawCompoundStmt(Node->getSynchBody());
477 OS << "\n";
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000478}
479
Sebastian Redl9b244a82008-12-22 21:35:02 +0000480void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
481 OS << "catch (";
Sebastian Redl54c04d42008-12-22 19:15:10 +0000482 if (Decl *ExDecl = Node->getExceptionDecl())
483 PrintRawDecl(ExDecl);
484 else
485 OS << "...";
486 OS << ") ";
487 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl9b244a82008-12-22 21:35:02 +0000488}
489
490void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
491 Indent();
492 PrintRawCXXCatchStmt(Node);
493 OS << "\n";
494}
495
496void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
497 Indent() << "try ";
498 PrintRawCompoundStmt(Node->getTryBlock());
499 for(unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
500 OS << " ";
501 PrintRawCXXCatchStmt(Node->getHandler(i));
502 }
Sebastian Redl54c04d42008-12-22 19:15:10 +0000503 OS << "\n";
504}
505
Chris Lattner71e23ce2006-11-04 20:18:38 +0000506//===----------------------------------------------------------------------===//
507// Expr printing methods.
508//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000509
Chris Lattner882f7882006-11-04 18:52:07 +0000510void StmtPrinter::VisitExpr(Expr *Node) {
511 OS << "<<unknown expr type>>";
512}
513
514void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000515 OS << Node->getDecl()->getNameAsString();
Chris Lattner882f7882006-11-04 18:52:07 +0000516}
517
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000518void StmtPrinter::VisitQualifiedDeclRefExpr(QualifiedDeclRefExpr *Node) {
519 // FIXME: Should we keep enough information in QualifiedDeclRefExpr
520 // to produce the same qualification that the user wrote?
521 llvm::SmallVector<DeclContext *, 4> Contexts;
522
523 NamedDecl *D = Node->getDecl();
524
525 // Build up a stack of contexts.
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000526 DeclContext *Ctx = D->getDeclContext();
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000527 for (; Ctx; Ctx = Ctx->getParent())
528 if (!Ctx->isTransparentContext())
529 Contexts.push_back(Ctx);
530
531 while (!Contexts.empty()) {
532 DeclContext *Ctx = Contexts.back();
533 if (isa<TranslationUnitDecl>(Ctx))
534 OS << "::";
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000535 else if (NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
536 OS << ND->getNameAsString() << "::";
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000537 Contexts.pop_back();
538 }
539
540 OS << D->getNameAsString();
541}
542
Steve Naroffe46504b2007-11-12 14:29:37 +0000543void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000544 if (Node->getBase()) {
545 PrintExpr(Node->getBase());
546 OS << (Node->isArrow() ? "->" : ".");
547 }
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000548 OS << Node->getDecl()->getNameAsString();
Steve Naroffe46504b2007-11-12 14:29:37 +0000549}
550
Steve Naroffec944032008-05-30 00:40:33 +0000551void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
552 if (Node->getBase()) {
553 PrintExpr(Node->getBase());
554 OS << ".";
555 }
Steve Naroff4588d0f2008-12-04 16:24:46 +0000556 OS << Node->getProperty()->getNameAsCString();
Steve Naroffec944032008-05-30 00:40:33 +0000557}
558
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000559void StmtPrinter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node) {
560 if (Node->getBase()) {
561 PrintExpr(Node->getBase());
562 OS << ".";
563 }
564 // FIXME: Setter/Getter names
565}
566
Chris Lattner6307f192008-08-10 01:53:14 +0000567void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson625bfc82007-07-21 05:21:51 +0000568 switch (Node->getIdentType()) {
569 default:
570 assert(0 && "unknown case");
Chris Lattner6307f192008-08-10 01:53:14 +0000571 case PredefinedExpr::Func:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000572 OS << "__func__";
573 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000574 case PredefinedExpr::Function:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000575 OS << "__FUNCTION__";
576 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000577 case PredefinedExpr::PrettyFunction:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000578 OS << "__PRETTY_FUNCTION__";
579 break;
580 }
581}
582
Steve Naroffae4143e2007-04-26 20:39:23 +0000583void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000584 unsigned value = Node->getValue();
Chris Lattnera5678cc2008-06-07 22:35:38 +0000585 if (Node->isWide())
586 OS << "L";
Chris Lattner666115c2007-07-13 23:58:20 +0000587 switch (value) {
588 case '\\':
589 OS << "'\\\\'";
590 break;
591 case '\'':
592 OS << "'\\''";
593 break;
594 case '\a':
595 // TODO: K&R: the meaning of '\\a' is different in traditional C
596 OS << "'\\a'";
597 break;
598 case '\b':
599 OS << "'\\b'";
600 break;
601 // Nonstandard escape sequence.
602 /*case '\e':
603 OS << "'\\e'";
604 break;*/
605 case '\f':
606 OS << "'\\f'";
607 break;
608 case '\n':
609 OS << "'\\n'";
610 break;
611 case '\r':
612 OS << "'\\r'";
613 break;
614 case '\t':
615 OS << "'\\t'";
616 break;
617 case '\v':
618 OS << "'\\v'";
619 break;
620 default:
Ted Kremenek652d18e2008-02-23 00:52:04 +0000621 if (value < 256 && isprint(value)) {
Chris Lattner666115c2007-07-13 23:58:20 +0000622 OS << "'" << (char)value << "'";
623 } else if (value < 256) {
Ted Kremenek2d470fc2008-09-13 05:16:45 +0000624 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner666115c2007-07-13 23:58:20 +0000625 } else {
626 // FIXME what to really do here?
627 OS << value;
628 }
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000629 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000630}
631
Steve Naroffdf7855b2007-02-21 23:46:25 +0000632void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000633 bool isSigned = Node->getType()->isSignedIntegerType();
634 OS << Node->getValue().toString(10, isSigned);
635
636 // Emit suffixes. Integer literals are always a builtin integer type.
Chris Lattner574dee62008-07-26 22:17:49 +0000637 switch (Node->getType()->getAsBuiltinType()->getKind()) {
Chris Lattner06430412007-05-21 05:45:03 +0000638 default: assert(0 && "Unexpected type for integer literal!");
639 case BuiltinType::Int: break; // no suffix.
640 case BuiltinType::UInt: OS << 'U'; break;
641 case BuiltinType::Long: OS << 'L'; break;
642 case BuiltinType::ULong: OS << "UL"; break;
643 case BuiltinType::LongLong: OS << "LL"; break;
644 case BuiltinType::ULongLong: OS << "ULL"; break;
645 }
Chris Lattner882f7882006-11-04 18:52:07 +0000646}
Steve Naroffab624882007-02-21 22:05:47 +0000647void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattnerbf2f3862007-08-01 00:23:58 +0000648 // FIXME: print value more precisely.
Chris Lattnera0173132008-06-07 22:13:43 +0000649 OS << Node->getValueAsApproximateDouble();
Chris Lattner882f7882006-11-04 18:52:07 +0000650}
Chris Lattner1c20a172007-08-26 03:42:43 +0000651
652void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
653 PrintExpr(Node->getSubExpr());
654 OS << "i";
655}
656
Steve Naroffdf7855b2007-02-21 23:46:25 +0000657void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Chris Lattner882f7882006-11-04 18:52:07 +0000658 if (Str->isWide()) OS << 'L';
Chris Lattner5d8f4942006-11-04 20:29:31 +0000659 OS << '"';
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000660
Chris Lattner5d8f4942006-11-04 20:29:31 +0000661 // FIXME: this doesn't print wstrings right.
662 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattnerbb96cc42009-01-16 19:25:18 +0000663 unsigned char Char = Str->getStrData()[i];
664
665 switch (Char) {
666 default:
667 if (isprint(Char))
668 OS << (char)Char;
669 else // Output anything hard as an octal escape.
670 OS << '\\'
671 << (char)('0'+ ((Char >> 6) & 7))
672 << (char)('0'+ ((Char >> 3) & 7))
673 << (char)('0'+ ((Char >> 0) & 7));
674 break;
675 // Handle some common non-printable cases to make dumps prettier.
Chris Lattner5d8f4942006-11-04 20:29:31 +0000676 case '\\': OS << "\\\\"; break;
677 case '"': OS << "\\\""; break;
678 case '\n': OS << "\\n"; break;
679 case '\t': OS << "\\t"; break;
680 case '\a': OS << "\\a"; break;
681 case '\b': OS << "\\b"; break;
682 }
683 }
684 OS << '"';
Chris Lattner882f7882006-11-04 18:52:07 +0000685}
686void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
687 OS << "(";
688 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000689 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000690}
691void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +0000692 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +0000693 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattnere5b60442007-08-23 21:46:40 +0000694
Sebastian Redl6f282892008-11-11 17:56:53 +0000695 // Print a space if this is an "identifier operator" like __real.
Chris Lattnere5b60442007-08-23 21:46:40 +0000696 switch (Node->getOpcode()) {
697 default: break;
Chris Lattnere5b60442007-08-23 21:46:40 +0000698 case UnaryOperator::Real:
699 case UnaryOperator::Imag:
700 case UnaryOperator::Extension:
701 OS << ' ';
702 break;
703 }
704 }
Chris Lattner882f7882006-11-04 18:52:07 +0000705 PrintExpr(Node->getSubExpr());
Chris Lattner15768702006-11-05 23:54:51 +0000706
707 if (Node->isPostfix())
708 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000709}
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000710
711bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
712 if (isa<CompoundLiteralExpr>(E)) {
713 // Base case, print the type and comma.
714 OS << E->getType().getAsString() << ", ";
715 return true;
716 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
717 PrintOffsetOfDesignator(ASE->getLHS());
718 OS << "[";
719 PrintExpr(ASE->getRHS());
720 OS << "]";
721 return false;
722 } else {
723 MemberExpr *ME = cast<MemberExpr>(E);
724 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000725 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getNameAsString();
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000726 return false;
727 }
728}
729
730void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
731 OS << "__builtin_offsetof(";
732 PrintOffsetOfDesignator(Node->getSubExpr());
733 OS << ")";
734}
735
Sebastian Redl6f282892008-11-11 17:56:53 +0000736void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
737 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
738 if (Node->isArgumentType())
739 OS << "(" << Node->getArgumentType().getAsString() << ")";
740 else {
741 OS << " ";
742 PrintExpr(Node->getArgumentExpr());
743 }
Chris Lattner882f7882006-11-04 18:52:07 +0000744}
745void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000746 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000747 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +0000748 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000749 OS << "]";
750}
751
752void StmtPrinter::VisitCallExpr(CallExpr *Call) {
753 PrintExpr(Call->getCallee());
754 OS << "(";
755 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000756 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
757 // Don't print any defaulted arguments
758 break;
759 }
760
Chris Lattner882f7882006-11-04 18:52:07 +0000761 if (i) OS << ", ";
762 PrintExpr(Call->getArg(i));
763 }
764 OS << ")";
765}
766void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregore4b414c2009-01-08 22:45:41 +0000767 // FIXME: Suppress printing implicit bases (like "this")
768 PrintExpr(Node->getBase());
769 OS << (Node->isArrow() ? "->" : ".");
770 // FIXME: Suppress printing references to unnamed objects
771 // representing anonymous unions/structs
Douglas Gregor2eedc3a2008-12-20 23:49:58 +0000772 OS << Node->getMemberDecl()->getNameAsString();
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000773}
Nate Begemance4d7fc2008-04-18 23:10:10 +0000774void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +0000775 PrintExpr(Node->getBase());
776 OS << ".";
777 OS << Node->getAccessor().getName();
778}
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +0000779void StmtPrinter::VisitCastExpr(CastExpr *) {
780 assert(0 && "CastExpr is an abstract class");
781}
Douglas Gregore200adc2008-10-27 19:41:14 +0000782void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *) {
783 assert(0 && "ExplicitCastExpr is an abstract class");
784}
Douglas Gregorf19b2312008-10-28 15:36:24 +0000785void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Chris Lattner51aff8b2007-07-15 23:54:50 +0000786 OS << "(" << Node->getType().getAsString() << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000787 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000788}
Steve Naroff57eb2c52007-07-19 21:32:11 +0000789void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
790 OS << "(" << Node->getType().getAsString() << ")";
791 PrintExpr(Node->getInitializer());
792}
Steve Naroff7a5af782007-07-13 16:58:59 +0000793void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000794 // No need to print anything, simply forward to the sub expression.
795 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +0000796}
Chris Lattner882f7882006-11-04 18:52:07 +0000797void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
798 PrintExpr(Node->getLHS());
799 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
800 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000801}
Chris Lattner86928112007-08-25 02:00:02 +0000802void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
803 PrintExpr(Node->getLHS());
804 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
805 PrintExpr(Node->getRHS());
806}
Chris Lattner882f7882006-11-04 18:52:07 +0000807void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
808 PrintExpr(Node->getCond());
Ted Kremenekebb1c0c2007-11-26 18:27:54 +0000809
810 if (Node->getLHS()) {
811 OS << " ? ";
812 PrintExpr(Node->getLHS());
813 OS << " : ";
814 }
815 else { // Handle GCC extention where LHS can be NULL.
816 OS << " ?: ";
817 }
818
Chris Lattner882f7882006-11-04 18:52:07 +0000819 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000820}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000821
Chris Lattnereefa10e2007-05-28 06:56:27 +0000822// GNU extensions.
823
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000824void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000825 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000826}
827
Chris Lattner366727f2007-07-24 16:58:17 +0000828void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
829 OS << "(";
830 PrintRawCompoundStmt(E->getSubStmt());
831 OS << ")";
832}
833
Steve Naroff78864672007-08-01 22:05:33 +0000834void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
835 OS << "__builtin_types_compatible_p(";
836 OS << Node->getArgType1().getAsString() << ",";
837 OS << Node->getArgType2().getAsString() << ")";
838}
839
Steve Naroff9efdabc2007-08-03 21:21:27 +0000840void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
841 OS << "__builtin_choose_expr(";
842 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +0000843 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000844 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +0000845 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000846 PrintExpr(Node->getRHS());
847 OS << ")";
848}
Chris Lattner366727f2007-07-24 16:58:17 +0000849
Douglas Gregor3be4b122008-11-29 04:51:27 +0000850void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
851 OS << "__null";
852}
853
Nate Begeman1e36a852008-01-17 17:46:27 +0000854void StmtPrinter::VisitOverloadExpr(OverloadExpr *Node) {
855 OS << "__builtin_overload(";
Nate Begeman936b2072008-01-30 20:50:20 +0000856 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
Nate Begeman1e36a852008-01-17 17:46:27 +0000857 if (i) OS << ", ";
Nate Begeman936b2072008-01-30 20:50:20 +0000858 PrintExpr(Node->getExpr(i));
Nate Begeman1e36a852008-01-17 17:46:27 +0000859 }
860 OS << ")";
861}
862
Eli Friedmana1b4ed82008-05-14 19:38:39 +0000863void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
864 OS << "__builtin_shufflevector(";
865 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
866 if (i) OS << ", ";
867 PrintExpr(Node->getExpr(i));
868 }
869 OS << ")";
870}
871
Anders Carlsson4692db02007-08-31 04:56:16 +0000872void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
873 OS << "{ ";
874 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
875 if (i) OS << ", ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000876 if (Node->getInit(i))
877 PrintExpr(Node->getInit(i));
878 else
879 OS << "0";
Anders Carlsson4692db02007-08-31 04:56:16 +0000880 }
881 OS << " }";
882}
883
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000884void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000885 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
886 DEnd = Node->designators_end();
887 D != DEnd; ++D) {
888 if (D->isFieldDesignator()) {
889 if (D->getDotLoc().isInvalid())
890 OS << D->getFieldName()->getName() << ":";
891 else
892 OS << "." << D->getFieldName()->getName();
893 } else {
894 OS << "[";
895 if (D->isArrayDesignator()) {
896 PrintExpr(Node->getArrayIndex(*D));
897 } else {
898 PrintExpr(Node->getArrayRangeStart(*D));
899 OS << " ... ";
900 PrintExpr(Node->getArrayRangeEnd(*D));
901 }
902 OS << "]";
903 }
904 }
905
906 OS << " = ";
907 PrintExpr(Node->getInit());
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000908}
909
Douglas Gregor0202cb42009-01-29 17:44:32 +0000910void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
911 OS << "/*implicit*/" << Node->getType().getAsString() << "()";
912}
913
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000914void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
915 OS << "va_arg(";
916 PrintExpr(Node->getSubExpr());
917 OS << ", ";
918 OS << Node->getType().getAsString();
919 OS << ")";
920}
921
Chris Lattnereefa10e2007-05-28 06:56:27 +0000922// C++
Douglas Gregor993603d2008-11-14 16:09:21 +0000923void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
924 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
925 "",
926#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
927 Spelling,
928#include "clang/Basic/OperatorKinds.def"
929 };
930
931 OverloadedOperatorKind Kind = Node->getOperator();
932 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
933 if (Node->getNumArgs() == 1) {
934 OS << OpStrings[Kind] << ' ';
935 PrintExpr(Node->getArg(0));
936 } else {
937 PrintExpr(Node->getArg(0));
938 OS << ' ' << OpStrings[Kind];
939 }
940 } else if (Kind == OO_Call) {
941 PrintExpr(Node->getArg(0));
942 OS << '(';
943 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
944 if (ArgIdx > 1)
945 OS << ", ";
946 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
947 PrintExpr(Node->getArg(ArgIdx));
948 }
949 OS << ')';
950 } else if (Kind == OO_Subscript) {
951 PrintExpr(Node->getArg(0));
952 OS << '[';
953 PrintExpr(Node->getArg(1));
954 OS << ']';
955 } else if (Node->getNumArgs() == 1) {
956 OS << OpStrings[Kind] << ' ';
957 PrintExpr(Node->getArg(0));
958 } else if (Node->getNumArgs() == 2) {
959 PrintExpr(Node->getArg(0));
960 OS << ' ' << OpStrings[Kind] << ' ';
961 PrintExpr(Node->getArg(1));
962 } else {
963 assert(false && "unknown overloaded operator");
964 }
965}
Chris Lattnereefa10e2007-05-28 06:56:27 +0000966
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000967void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
968 VisitCallExpr(cast<CallExpr>(Node));
969}
970
Douglas Gregore200adc2008-10-27 19:41:14 +0000971void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
972 OS << Node->getCastName() << '<';
973 OS << Node->getTypeAsWritten().getAsString() << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +0000974 PrintExpr(Node->getSubExpr());
975 OS << ")";
976}
977
Douglas Gregore200adc2008-10-27 19:41:14 +0000978void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
979 VisitCXXNamedCastExpr(Node);
980}
981
982void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
983 VisitCXXNamedCastExpr(Node);
984}
985
986void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
987 VisitCXXNamedCastExpr(Node);
988}
989
990void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
991 VisitCXXNamedCastExpr(Node);
992}
993
Sebastian Redlc4704762008-11-11 11:37:55 +0000994void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
995 OS << "typeid(";
996 if (Node->isTypeOperand()) {
997 OS << Node->getTypeOperand().getAsString();
998 } else {
999 PrintExpr(Node->getExprOperand());
1000 }
1001 OS << ")";
1002}
1003
Chris Lattnereefa10e2007-05-28 06:56:27 +00001004void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1005 OS << (Node->getValue() ? "true" : "false");
1006}
1007
Douglas Gregor97a9c812008-11-04 14:32:21 +00001008void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1009 OS << "this";
1010}
1011
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001012void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1013 if (Node->getSubExpr() == 0)
1014 OS << "throw";
1015 else {
1016 OS << "throw ";
1017 PrintExpr(Node->getSubExpr());
1018 }
1019}
1020
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001021void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1022 // Nothing to print: we picked up the default argument
1023}
1024
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001025void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1026 OS << Node->getType().getAsString();
1027 OS << "(";
1028 PrintExpr(Node->getSubExpr());
1029 OS << ")";
1030}
1031
Douglas Gregordd04d332009-01-16 18:33:17 +00001032void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1033 OS << Node->getType().getAsString();
1034 OS << "(";
1035 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1036 ArgEnd = Node->arg_end();
1037 Arg != ArgEnd; ++Arg) {
1038 if (Arg != Node->arg_begin())
1039 OS << ", ";
1040 PrintExpr(*Arg);
1041 }
1042 OS << ")";
1043}
1044
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001045void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
1046 OS << Node->getType().getAsString() << "()";
1047}
1048
Argyrios Kyrtzidisaa479132008-09-09 23:47:53 +00001049void
1050StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
1051 PrintRawDecl(E->getVarDecl());
1052}
1053
Sebastian Redlbd150f42008-11-21 19:14:01 +00001054void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1055 if (E->isGlobalNew())
1056 OS << "::";
1057 OS << "new ";
1058 unsigned NumPlace = E->getNumPlacementArgs();
1059 if (NumPlace > 0) {
1060 OS << "(";
1061 PrintExpr(E->getPlacementArg(0));
1062 for (unsigned i = 1; i < NumPlace; ++i) {
1063 OS << ", ";
1064 PrintExpr(E->getPlacementArg(i));
1065 }
1066 OS << ") ";
1067 }
1068 if (E->isParenTypeId())
1069 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001070 std::string TypeS;
1071 if (Expr *Size = E->getArraySize()) {
1072 llvm::raw_string_ostream s(TypeS);
1073 Size->printPretty(s);
1074 s.flush();
1075 TypeS = "[" + TypeS + "]";
1076 }
1077 E->getAllocatedType().getAsStringInternal(TypeS);
1078 OS << TypeS;
Sebastian Redlbd150f42008-11-21 19:14:01 +00001079 if (E->isParenTypeId())
1080 OS << ")";
1081
1082 if (E->hasInitializer()) {
1083 OS << "(";
1084 unsigned NumCons = E->getNumConstructorArgs();
1085 if (NumCons > 0) {
1086 PrintExpr(E->getConstructorArg(0));
1087 for (unsigned i = 1; i < NumCons; ++i) {
1088 OS << ", ";
1089 PrintExpr(E->getConstructorArg(i));
1090 }
1091 }
1092 OS << ")";
1093 }
1094}
1095
1096void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1097 if (E->isGlobalDelete())
1098 OS << "::";
1099 OS << "delete ";
1100 if (E->isArrayForm())
1101 OS << "[] ";
1102 PrintExpr(E->getArgument());
1103}
1104
Douglas Gregorb8a9a412009-02-04 15:01:18 +00001105void StmtPrinter::VisitUnresolvedFunctionNameExpr(UnresolvedFunctionNameExpr *E) {
1106 OS << E->getName().getAsString();
Douglas Gregorb0846b02008-12-06 00:22:45 +00001107}
1108
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001109static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1110 switch (UTT) {
1111 default: assert(false && "Unknown type trait");
1112 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1113 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1114 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1115 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1116 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1117 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1118 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1119 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1120 case UTT_IsAbstract: return "__is_abstract";
1121 case UTT_IsClass: return "__is_class";
1122 case UTT_IsEmpty: return "__is_empty";
1123 case UTT_IsEnum: return "__is_enum";
1124 case UTT_IsPOD: return "__is_pod";
1125 case UTT_IsPolymorphic: return "__is_polymorphic";
1126 case UTT_IsUnion: return "__is_union";
1127 }
1128}
1129
1130void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1131 OS << getTypeTraitName(E->getTrait()) << "("
1132 << E->getQueriedType().getAsString() << ")";
1133}
1134
Anders Carlsson76f4a902007-08-21 17:43:55 +00001135// Obj-C
1136
1137void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1138 OS << "@";
1139 VisitStringLiteral(Node->getString());
1140}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001141
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001142void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001143 OS << "@encode(" << Node->getEncodedType().getAsString() << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001144}
1145
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001146void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001147 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001148}
1149
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001150void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001151 OS << "@protocol(" << Node->getProtocol()->getNameAsString() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001152}
1153
Steve Naroffd54978b2007-09-18 23:55:05 +00001154void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1155 OS << "[";
Steve Naroffc6814ea2007-10-02 20:01:56 +00001156 Expr *receiver = Mess->getReceiver();
1157 if (receiver) PrintExpr(receiver);
1158 else OS << Mess->getClassName()->getName();
Ted Kremeneka06e7122008-05-02 17:32:38 +00001159 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00001160 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001161 if (selector.isUnarySelector()) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001162 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001163 } else {
1164 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001165 if (i < selector.getNumArgs()) {
1166 if (i > 0) OS << ' ';
1167 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001168 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00001169 else
1170 OS << ":";
1171 }
1172 else OS << ", "; // Handle variadic methods.
1173
Steve Naroffc6814ea2007-10-02 20:01:56 +00001174 PrintExpr(Mess->getArg(i));
1175 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001176 }
1177 OS << "]";
1178}
1179
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001180void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) {
1181 OS << "super";
1182}
1183
Steve Naroffc540d662008-09-03 18:15:37 +00001184void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001185 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00001186 OS << "^";
1187
1188 const FunctionType *AFT = Node->getFunctionType();
1189
1190 if (isa<FunctionTypeNoProto>(AFT)) {
1191 OS << "()";
Steve Naroff415d3d52008-10-08 17:01:13 +00001192 } else if (!BD->param_empty() || cast<FunctionTypeProto>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00001193 OS << '(';
1194 std::string ParamStr;
Steve Naroff415d3d52008-10-08 17:01:13 +00001195 for (BlockDecl::param_iterator AI = BD->param_begin(),
1196 E = BD->param_end(); AI != E; ++AI) {
1197 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001198 ParamStr = (*AI)->getNameAsString();
Steve Naroffc540d662008-09-03 18:15:37 +00001199 (*AI)->getType().getAsStringInternal(ParamStr);
1200 OS << ParamStr;
1201 }
1202
Steve Naroff415d3d52008-10-08 17:01:13 +00001203 const FunctionTypeProto *FT = cast<FunctionTypeProto>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00001204 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001205 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00001206 OS << "...";
1207 }
1208 OS << ')';
1209 }
1210}
1211
Steve Naroffc540d662008-09-03 18:15:37 +00001212void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001213 OS << Node->getDecl()->getNameAsString();
Steve Naroffc540d662008-09-03 18:15:37 +00001214}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001215//===----------------------------------------------------------------------===//
1216// Stmt method implementations
1217//===----------------------------------------------------------------------===//
1218
Chris Lattnercbe4f772007-08-08 22:51:59 +00001219void Stmt::dumpPretty() const {
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001220 llvm::raw_ostream &OS = llvm::errs();
1221 printPretty(OS);
1222 OS.flush();
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001223}
1224
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001225void Stmt::printPretty(llvm::raw_ostream &OS, PrinterHelper* Helper) const {
Chris Lattner882f7882006-11-04 18:52:07 +00001226 if (this == 0) {
1227 OS << "<NULL>";
1228 return;
1229 }
1230
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001231 StmtPrinter P(OS, Helper);
Chris Lattner62249a62007-08-21 04:04:25 +00001232 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001233}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001234
1235//===----------------------------------------------------------------------===//
1236// PrinterHelper
1237//===----------------------------------------------------------------------===//
1238
1239// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +00001240PrinterHelper::~PrinterHelper() {}