blob: 94bca6386632080d3d85df4a007f45e249a58e13 [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"
Ted Kremenek5c84c012007-10-17 18:36:42 +000016#include "clang/AST/DeclObjC.h"
Ted Kremenek04f3cee2007-08-31 21:30:12 +000017#include "clang/AST/PrettyPrinter.h"
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000018#include "llvm/Support/Compiler.h"
Ted Kremenek871422e2007-11-26 22:50:46 +000019#include "llvm/Support/Streams.h"
Ted Kremenek2d470fc2008-09-13 05:16:45 +000020#include "llvm/Support/Format.h"
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000021using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// StmtPrinter Visitor
25//===----------------------------------------------------------------------===//
26
27namespace {
Chris Lattner62249a62007-08-21 04:04:25 +000028 class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor<StmtPrinter> {
Ted Kremenek2d470fc2008-09-13 05:16:45 +000029 llvm::raw_ostream &OS;
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000030 unsigned IndentLevel;
Ted Kremenek04f3cee2007-08-31 21:30:12 +000031 clang::PrinterHelper* Helper;
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000032 public:
Ted Kremenek2d470fc2008-09-13 05:16:45 +000033 StmtPrinter(llvm::raw_ostream &os, PrinterHelper* helper) :
Ted Kremenek04f3cee2007-08-31 21:30:12 +000034 OS(os), IndentLevel(0), Helper(helper) {}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000035
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000036 void PrintStmt(Stmt *S, int SubIndent = 1) {
37 IndentLevel += SubIndent;
Chris Lattnera076fde2007-05-31 18:21:33 +000038 if (S && isa<Expr>(S)) {
Chris Lattner882f7882006-11-04 18:52:07 +000039 // If this is an expr used in a stmt context, indent and newline it.
40 Indent();
Chris Lattner62249a62007-08-21 04:04:25 +000041 Visit(S);
Chris Lattnerfc068c12007-05-30 17:57:36 +000042 OS << ";\n";
Chris Lattner882f7882006-11-04 18:52:07 +000043 } else if (S) {
Chris Lattner62249a62007-08-21 04:04:25 +000044 Visit(S);
Chris Lattner882f7882006-11-04 18:52:07 +000045 } else {
Chris Lattner2f6ac262007-05-28 01:47:47 +000046 Indent() << "<<<NULL STATEMENT>>>\n";
Chris Lattner882f7882006-11-04 18:52:07 +000047 }
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000048 IndentLevel -= SubIndent;
Chris Lattner882f7882006-11-04 18:52:07 +000049 }
Chris Lattner073926e2007-05-20 23:04:55 +000050
51 void PrintRawCompoundStmt(CompoundStmt *S);
Chris Lattnerfdc195a2007-06-05 20:52:47 +000052 void PrintRawDecl(Decl *D);
Ted Kremenek15e6b402008-10-06 18:39:36 +000053 void PrintRawDeclStmt(DeclStmt *S);
Chris Lattnerc0a38dd2007-06-11 22:26:23 +000054 void PrintRawIfStmt(IfStmt *If);
55
Chris Lattner882f7882006-11-04 18:52:07 +000056 void PrintExpr(Expr *E) {
57 if (E)
Chris Lattner62249a62007-08-21 04:04:25 +000058 Visit(E);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000059 else
Chris Lattner882f7882006-11-04 18:52:07 +000060 OS << "<null expr>";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000061 }
62
Ted Kremenek2d470fc2008-09-13 05:16:45 +000063 llvm::raw_ostream &Indent(int Delta = 0) const {
Chris Lattnerd5322cd2007-05-29 23:49:07 +000064 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000065 OS << " ";
66 return OS;
67 }
68
Chris Lattner98dbf0a2007-08-30 17:59:59 +000069 bool PrintOffsetOfDesignator(Expr *E);
70 void VisitUnaryOffsetOf(UnaryOperator *Node);
71
Ted Kremenek04f3cee2007-08-31 21:30:12 +000072 void Visit(Stmt* S) {
73 if (Helper && Helper->handledStmt(S,OS))
74 return;
75 else StmtVisitor<StmtPrinter>::Visit(S);
76 }
77
Chris Lattner62249a62007-08-21 04:04:25 +000078 void VisitStmt(Stmt *Node);
Douglas Gregorbe35ce92008-11-14 12:46:07 +000079#define STMT(CLASS, PARENT) \
Chris Lattner62249a62007-08-21 04:04:25 +000080 void Visit##CLASS(CLASS *Node);
Chris Lattnerf2174b62006-11-04 20:59:27 +000081#include "clang/AST/StmtNodes.def"
Chris Lattner71e23ce2006-11-04 20:18:38 +000082 };
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000083}
84
Chris Lattner71e23ce2006-11-04 20:18:38 +000085//===----------------------------------------------------------------------===//
86// Stmt printing methods.
87//===----------------------------------------------------------------------===//
88
Chris Lattner882f7882006-11-04 18:52:07 +000089void StmtPrinter::VisitStmt(Stmt *Node) {
90 Indent() << "<<unknown stmt type>>\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000091}
92
Chris Lattner073926e2007-05-20 23:04:55 +000093/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
94/// with no newline after the }.
95void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
96 OS << "{\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000097 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
Chris Lattner882f7882006-11-04 18:52:07 +000098 I != E; ++I)
99 PrintStmt(*I);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000100
Chris Lattner073926e2007-05-20 23:04:55 +0000101 Indent() << "}";
102}
103
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000104void StmtPrinter::PrintRawDecl(Decl *D) {
105 // FIXME: Need to complete/beautify this... this code simply shows the
Steve Naroff2a8ad182007-05-29 22:59:26 +0000106 // nodes are where they need to be.
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000107 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
108 OS << "typedef " << localType->getUnderlyingType().getAsString();
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000109 OS << " " << localType->getNameAsString();
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000110 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattnerb4522b42007-06-02 05:27:01 +0000111 // Emit storage class for vardecls.
112 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
113 switch (V->getStorageClass()) {
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000114 default: assert(0 && "Unknown storage class!");
115 case VarDecl::None: break;
116 case VarDecl::Extern: OS << "extern "; break;
117 case VarDecl::Static: OS << "static "; break;
118 case VarDecl::Auto: OS << "auto "; break;
119 case VarDecl::Register: OS << "register "; break;
Chris Lattnerb4522b42007-06-02 05:27:01 +0000120 }
121 }
122
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000123 std::string Name = VD->getNameAsString();
Chris Lattnere0c4ae12007-06-02 03:38:08 +0000124 VD->getType().getAsStringInternal(Name);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000125 OS << Name;
126
Chris Lattner79c57592007-07-12 00:36:32 +0000127 // If this is a vardecl with an initializer, emit it.
128 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
129 if (V->getInit()) {
130 OS << " = ";
131 PrintExpr(V->getInit());
132 }
133 }
Steve Naroffc1e6d602007-11-17 21:21:01 +0000134 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
135 // print a free standing tag decl (e.g. "struct x;").
136 OS << TD->getKindName();
137 OS << " ";
138 if (const IdentifierInfo *II = TD->getIdentifier())
139 OS << II->getName();
140 else
141 OS << "<anonymous>";
142 // FIXME: print tag bodies.
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000143 } else {
Chris Lattnere0c4ae12007-06-02 03:38:08 +0000144 assert(0 && "Unexpected decl");
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000145 }
146}
147
Ted Kremenek15e6b402008-10-06 18:39:36 +0000148void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
149 bool isFirst = false;
150
151 for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();
152 I != E; ++I) {
153
154 if (!isFirst) OS << ", ";
155 else isFirst = false;
156
157 PrintRawDecl(*I);
158 }
159}
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000160
161void StmtPrinter::VisitNullStmt(NullStmt *Node) {
162 Indent() << ";\n";
163}
164
165void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Ted Kremenek15e6b402008-10-06 18:39:36 +0000166 for (DeclStmt::decl_iterator I = Node->decl_begin(), E = Node->decl_end();
167 I!=E; ++I) {
Chris Lattner776fac82007-06-09 00:53:06 +0000168 Indent();
Ted Kremenek15e6b402008-10-06 18:39:36 +0000169 PrintRawDecl(*I);
Chris Lattner776fac82007-06-09 00:53:06 +0000170 OS << ";\n";
171 }
Steve Naroff2a8ad182007-05-29 22:59:26 +0000172}
173
Chris Lattner073926e2007-05-20 23:04:55 +0000174void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
175 Indent();
176 PrintRawCompoundStmt(Node);
Chris Lattnerdf3cafb2007-05-31 05:08:56 +0000177 OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000178}
179
Chris Lattner6c0ff132006-11-05 00:19:50 +0000180void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000181 Indent(-1) << "case ";
Chris Lattner6c0ff132006-11-05 00:19:50 +0000182 PrintExpr(Node->getLHS());
183 if (Node->getRHS()) {
184 OS << " ... ";
185 PrintExpr(Node->getRHS());
186 }
187 OS << ":\n";
188
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000189 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000190}
191
192void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000193 Indent(-1) << "default:\n";
194 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000195}
196
197void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000198 Indent(-1) << Node->getName() << ":\n";
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000199 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000200}
201
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000202void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
203 OS << "if ";
Chris Lattner882f7882006-11-04 18:52:07 +0000204 PrintExpr(If->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000205
206 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
207 OS << ' ';
208 PrintRawCompoundStmt(CS);
209 OS << (If->getElse() ? ' ' : '\n');
210 } else {
211 OS << '\n';
212 PrintStmt(If->getThen());
213 if (If->getElse()) Indent();
214 }
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000215
Chris Lattner073926e2007-05-20 23:04:55 +0000216 if (Stmt *Else = If->getElse()) {
217 OS << "else";
218
219 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
220 OS << ' ';
221 PrintRawCompoundStmt(CS);
222 OS << '\n';
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000223 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
224 OS << ' ';
225 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000226 } else {
227 OS << '\n';
228 PrintStmt(If->getElse());
229 }
Chris Lattner882f7882006-11-04 18:52:07 +0000230 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000231}
232
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000233void StmtPrinter::VisitIfStmt(IfStmt *If) {
234 Indent();
235 PrintRawIfStmt(If);
236}
237
Chris Lattnerf2174b62006-11-04 20:59:27 +0000238void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
239 Indent() << "switch (";
240 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000241 OS << ")";
242
243 // Pretty print compoundstmt bodies (very common).
244 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
245 OS << " ";
246 PrintRawCompoundStmt(CS);
247 OS << "\n";
248 } else {
249 OS << "\n";
250 PrintStmt(Node->getBody());
251 }
Chris Lattnerf2174b62006-11-04 20:59:27 +0000252}
253
Anders Carlsson51873c22007-07-22 07:07:56 +0000254void StmtPrinter::VisitSwitchCase(SwitchCase*) {
255 assert(0 && "SwitchCase is an abstract class");
256}
257
Chris Lattner85ed8732006-11-04 20:40:44 +0000258void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
259 Indent() << "while (";
260 PrintExpr(Node->getCond());
261 OS << ")\n";
262 PrintStmt(Node->getBody());
263}
264
265void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000266 Indent() << "do ";
267 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
268 PrintRawCompoundStmt(CS);
269 OS << " ";
270 } else {
271 OS << "\n";
272 PrintStmt(Node->getBody());
273 Indent();
274 }
275
276 OS << "while ";
Chris Lattner85ed8732006-11-04 20:40:44 +0000277 PrintExpr(Node->getCond());
Chris Lattnera076fde2007-05-31 18:21:33 +0000278 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000279}
280
Chris Lattner71e23ce2006-11-04 20:18:38 +0000281void StmtPrinter::VisitForStmt(ForStmt *Node) {
282 Indent() << "for (";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000283 if (Node->getInit()) {
284 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000285 PrintRawDeclStmt(DS);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000286 else
287 PrintExpr(cast<Expr>(Node->getInit()));
288 }
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000289 OS << ";";
290 if (Node->getCond()) {
291 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000292 PrintExpr(Node->getCond());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000293 }
294 OS << ";";
295 if (Node->getInc()) {
296 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000297 PrintExpr(Node->getInc());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000298 }
299 OS << ") ";
300
301 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
302 PrintRawCompoundStmt(CS);
303 OS << "\n";
304 } else {
305 OS << "\n";
306 PrintStmt(Node->getBody());
307 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000308}
309
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000310void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian83615522008-01-02 22:54:34 +0000311 Indent() << "for (";
312 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000313 PrintRawDeclStmt(DS);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000314 else
315 PrintExpr(cast<Expr>(Node->getElement()));
316 OS << " in ";
317 PrintExpr(Node->getCollection());
318 OS << ") ";
319
320 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
321 PrintRawCompoundStmt(CS);
322 OS << "\n";
323 } else {
324 OS << "\n";
325 PrintStmt(Node->getBody());
326 }
327}
328
Chris Lattner16976d32006-11-05 01:46:01 +0000329void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000330 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000331}
332
333void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000334 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000335 PrintExpr(Node->getTarget());
Chris Lattnerb4619482007-05-31 06:00:14 +0000336 OS << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000337}
338
339void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000340 Indent() << "continue;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000341}
342
343void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000344 Indent() << "break;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000345}
346
347
Chris Lattner882f7882006-11-04 18:52:07 +0000348void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
349 Indent() << "return";
350 if (Node->getRetValue()) {
351 OS << " ";
352 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000353 }
Chris Lattnerb4619482007-05-31 06:00:14 +0000354 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000355}
356
Chris Lattner73c56c02007-10-29 04:04:16 +0000357
358void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson660bdd12007-11-23 23:12:25 +0000359 Indent() << "asm ";
360
361 if (Node->isVolatile())
362 OS << "volatile ";
363
364 OS << "(";
Anders Carlsson81a5a692007-11-20 19:21:03 +0000365 VisitStringLiteral(Node->getAsmString());
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000366
367 // Outputs
368 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
369 Node->getNumClobbers() != 0)
370 OS << " : ";
371
372 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
373 if (i != 0)
374 OS << ", ";
375
376 if (!Node->getOutputName(i).empty()) {
377 OS << '[';
378 OS << Node->getOutputName(i);
379 OS << "] ";
380 }
381
382 VisitStringLiteral(Node->getOutputConstraint(i));
383 OS << " ";
384 Visit(Node->getOutputExpr(i));
385 }
386
387 // Inputs
388 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
389 OS << " : ";
390
391 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
392 if (i != 0)
393 OS << ", ";
394
395 if (!Node->getInputName(i).empty()) {
396 OS << '[';
397 OS << Node->getInputName(i);
398 OS << "] ";
399 }
400
401 VisitStringLiteral(Node->getInputConstraint(i));
402 OS << " ";
403 Visit(Node->getInputExpr(i));
404 }
405
406 // Clobbers
407 if (Node->getNumClobbers() != 0)
408 OS << " : ";
409
410 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
411 if (i != 0)
412 OS << ", ";
413
414 VisitStringLiteral(Node->getClobber(i));
415 }
416
Anders Carlsson81a5a692007-11-20 19:21:03 +0000417 OS << ");\n";
Chris Lattner73c56c02007-10-29 04:04:16 +0000418}
419
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000420void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000421 Indent() << "@try";
422 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
423 PrintRawCompoundStmt(TS);
424 OS << "\n";
425 }
426
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000427 for (ObjCAtCatchStmt *catchStmt =
428 static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts());
Fariborz Jahanian88157952007-11-02 18:16:07 +0000429 catchStmt;
430 catchStmt =
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000431 static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000432 Indent() << "@catch(";
433 if (catchStmt->getCatchParamStmt()) {
434 if (DeclStmt *DS = dyn_cast<DeclStmt>(catchStmt->getCatchParamStmt()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000435 PrintRawDeclStmt(DS);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000436 }
437 OS << ")";
438 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody()))
439 {
440 PrintRawCompoundStmt(CS);
441 OS << "\n";
442 }
443 }
444
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000445 if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>(
Fariborz Jahaniandefbf9a2007-11-07 00:46:42 +0000446 Node->getFinallyStmt())) {
447 Indent() << "@finally";
448 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian88157952007-11-02 18:16:07 +0000449 OS << "\n";
450 }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000451}
452
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000453void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000454}
455
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000456void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000457 Indent() << "@catch (...) { /* todo */ } \n";
458}
459
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000460void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000461 Indent() << "@throw";
462 if (Node->getThrowExpr()) {
463 OS << " ";
464 PrintExpr(Node->getThrowExpr());
465 }
466 OS << ";\n";
467}
468
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000469void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000470 Indent() << "@synchronized (";
471 PrintExpr(Node->getSynchExpr());
472 OS << ")";
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000473 PrintRawCompoundStmt(Node->getSynchBody());
474 OS << "\n";
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000475}
476
Chris Lattner71e23ce2006-11-04 20:18:38 +0000477//===----------------------------------------------------------------------===//
478// Expr printing methods.
479//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000480
Chris Lattner882f7882006-11-04 18:52:07 +0000481void StmtPrinter::VisitExpr(Expr *Node) {
482 OS << "<<unknown expr type>>";
483}
484
485void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000486 OS << Node->getDecl()->getNameAsString();
Chris Lattner882f7882006-11-04 18:52:07 +0000487}
488
Steve Naroffe46504b2007-11-12 14:29:37 +0000489void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000490 if (Node->getBase()) {
491 PrintExpr(Node->getBase());
492 OS << (Node->isArrow() ? "->" : ".");
493 }
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000494 OS << Node->getDecl()->getNameAsString();
Steve Naroffe46504b2007-11-12 14:29:37 +0000495}
496
Steve Naroffec944032008-05-30 00:40:33 +0000497void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
498 if (Node->getBase()) {
499 PrintExpr(Node->getBase());
500 OS << ".";
501 }
Steve Naroff4588d0f2008-12-04 16:24:46 +0000502 OS << Node->getProperty()->getNameAsCString();
Steve Naroffec944032008-05-30 00:40:33 +0000503}
504
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000505void StmtPrinter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node) {
506 if (Node->getBase()) {
507 PrintExpr(Node->getBase());
508 OS << ".";
509 }
510 // FIXME: Setter/Getter names
511}
512
Chris Lattner6307f192008-08-10 01:53:14 +0000513void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson625bfc82007-07-21 05:21:51 +0000514 switch (Node->getIdentType()) {
515 default:
516 assert(0 && "unknown case");
Chris Lattner6307f192008-08-10 01:53:14 +0000517 case PredefinedExpr::Func:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000518 OS << "__func__";
519 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000520 case PredefinedExpr::Function:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000521 OS << "__FUNCTION__";
522 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000523 case PredefinedExpr::PrettyFunction:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000524 OS << "__PRETTY_FUNCTION__";
525 break;
526 }
527}
528
Steve Naroffae4143e2007-04-26 20:39:23 +0000529void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000530 unsigned value = Node->getValue();
Chris Lattnera5678cc2008-06-07 22:35:38 +0000531 if (Node->isWide())
532 OS << "L";
Chris Lattner666115c2007-07-13 23:58:20 +0000533 switch (value) {
534 case '\\':
535 OS << "'\\\\'";
536 break;
537 case '\'':
538 OS << "'\\''";
539 break;
540 case '\a':
541 // TODO: K&R: the meaning of '\\a' is different in traditional C
542 OS << "'\\a'";
543 break;
544 case '\b':
545 OS << "'\\b'";
546 break;
547 // Nonstandard escape sequence.
548 /*case '\e':
549 OS << "'\\e'";
550 break;*/
551 case '\f':
552 OS << "'\\f'";
553 break;
554 case '\n':
555 OS << "'\\n'";
556 break;
557 case '\r':
558 OS << "'\\r'";
559 break;
560 case '\t':
561 OS << "'\\t'";
562 break;
563 case '\v':
564 OS << "'\\v'";
565 break;
566 default:
Ted Kremenek652d18e2008-02-23 00:52:04 +0000567 if (value < 256 && isprint(value)) {
Chris Lattner666115c2007-07-13 23:58:20 +0000568 OS << "'" << (char)value << "'";
569 } else if (value < 256) {
Ted Kremenek2d470fc2008-09-13 05:16:45 +0000570 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner666115c2007-07-13 23:58:20 +0000571 } else {
572 // FIXME what to really do here?
573 OS << value;
574 }
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000575 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000576}
577
Steve Naroffdf7855b2007-02-21 23:46:25 +0000578void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000579 bool isSigned = Node->getType()->isSignedIntegerType();
580 OS << Node->getValue().toString(10, isSigned);
581
582 // Emit suffixes. Integer literals are always a builtin integer type.
Chris Lattner574dee62008-07-26 22:17:49 +0000583 switch (Node->getType()->getAsBuiltinType()->getKind()) {
Chris Lattner06430412007-05-21 05:45:03 +0000584 default: assert(0 && "Unexpected type for integer literal!");
585 case BuiltinType::Int: break; // no suffix.
586 case BuiltinType::UInt: OS << 'U'; break;
587 case BuiltinType::Long: OS << 'L'; break;
588 case BuiltinType::ULong: OS << "UL"; break;
589 case BuiltinType::LongLong: OS << "LL"; break;
590 case BuiltinType::ULongLong: OS << "ULL"; break;
591 }
Chris Lattner882f7882006-11-04 18:52:07 +0000592}
Steve Naroffab624882007-02-21 22:05:47 +0000593void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattnerbf2f3862007-08-01 00:23:58 +0000594 // FIXME: print value more precisely.
Chris Lattnera0173132008-06-07 22:13:43 +0000595 OS << Node->getValueAsApproximateDouble();
Chris Lattner882f7882006-11-04 18:52:07 +0000596}
Chris Lattner1c20a172007-08-26 03:42:43 +0000597
598void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
599 PrintExpr(Node->getSubExpr());
600 OS << "i";
601}
602
Steve Naroffdf7855b2007-02-21 23:46:25 +0000603void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Chris Lattner882f7882006-11-04 18:52:07 +0000604 if (Str->isWide()) OS << 'L';
Chris Lattner5d8f4942006-11-04 20:29:31 +0000605 OS << '"';
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000606
Chris Lattner5d8f4942006-11-04 20:29:31 +0000607 // FIXME: this doesn't print wstrings right.
608 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
609 switch (Str->getStrData()[i]) {
610 default: OS << Str->getStrData()[i]; break;
611 // Handle some common ones to make dumps prettier.
612 case '\\': OS << "\\\\"; break;
613 case '"': OS << "\\\""; break;
614 case '\n': OS << "\\n"; break;
615 case '\t': OS << "\\t"; break;
616 case '\a': OS << "\\a"; break;
617 case '\b': OS << "\\b"; break;
618 }
619 }
620 OS << '"';
Chris Lattner882f7882006-11-04 18:52:07 +0000621}
622void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
623 OS << "(";
624 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000625 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000626}
627void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +0000628 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +0000629 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattnere5b60442007-08-23 21:46:40 +0000630
Sebastian Redl6f282892008-11-11 17:56:53 +0000631 // Print a space if this is an "identifier operator" like __real.
Chris Lattnere5b60442007-08-23 21:46:40 +0000632 switch (Node->getOpcode()) {
633 default: break;
Chris Lattnere5b60442007-08-23 21:46:40 +0000634 case UnaryOperator::Real:
635 case UnaryOperator::Imag:
636 case UnaryOperator::Extension:
637 OS << ' ';
638 break;
639 }
640 }
Chris Lattner882f7882006-11-04 18:52:07 +0000641 PrintExpr(Node->getSubExpr());
Chris Lattner15768702006-11-05 23:54:51 +0000642
643 if (Node->isPostfix())
644 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000645}
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000646
647bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
648 if (isa<CompoundLiteralExpr>(E)) {
649 // Base case, print the type and comma.
650 OS << E->getType().getAsString() << ", ";
651 return true;
652 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
653 PrintOffsetOfDesignator(ASE->getLHS());
654 OS << "[";
655 PrintExpr(ASE->getRHS());
656 OS << "]";
657 return false;
658 } else {
659 MemberExpr *ME = cast<MemberExpr>(E);
660 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000661 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getNameAsString();
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000662 return false;
663 }
664}
665
666void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
667 OS << "__builtin_offsetof(";
668 PrintOffsetOfDesignator(Node->getSubExpr());
669 OS << ")";
670}
671
Sebastian Redl6f282892008-11-11 17:56:53 +0000672void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
673 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
674 if (Node->isArgumentType())
675 OS << "(" << Node->getArgumentType().getAsString() << ")";
676 else {
677 OS << " ";
678 PrintExpr(Node->getArgumentExpr());
679 }
Chris Lattner882f7882006-11-04 18:52:07 +0000680}
681void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000682 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000683 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +0000684 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000685 OS << "]";
686}
687
688void StmtPrinter::VisitCallExpr(CallExpr *Call) {
689 PrintExpr(Call->getCallee());
690 OS << "(";
691 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000692 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
693 // Don't print any defaulted arguments
694 break;
695 }
696
Chris Lattner882f7882006-11-04 18:52:07 +0000697 if (i) OS << ", ";
698 PrintExpr(Call->getArg(i));
699 }
700 OS << ")";
701}
702void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
703 PrintExpr(Node->getBase());
704 OS << (Node->isArrow() ? "->" : ".");
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000705
Chris Lattner24e0d6c2007-05-24 00:47:01 +0000706 FieldDecl *Field = Node->getMemberDecl();
707 assert(Field && "MemberExpr should alway reference a field!");
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000708 OS << Field->getNameAsString();
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000709}
Nate Begemance4d7fc2008-04-18 23:10:10 +0000710void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +0000711 PrintExpr(Node->getBase());
712 OS << ".";
713 OS << Node->getAccessor().getName();
714}
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +0000715void StmtPrinter::VisitCastExpr(CastExpr *) {
716 assert(0 && "CastExpr is an abstract class");
717}
Douglas Gregore200adc2008-10-27 19:41:14 +0000718void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *) {
719 assert(0 && "ExplicitCastExpr is an abstract class");
720}
Douglas Gregorf19b2312008-10-28 15:36:24 +0000721void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Chris Lattner51aff8b2007-07-15 23:54:50 +0000722 OS << "(" << Node->getType().getAsString() << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000723 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000724}
Steve Naroff57eb2c52007-07-19 21:32:11 +0000725void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
726 OS << "(" << Node->getType().getAsString() << ")";
727 PrintExpr(Node->getInitializer());
728}
Steve Naroff7a5af782007-07-13 16:58:59 +0000729void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000730 // No need to print anything, simply forward to the sub expression.
731 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +0000732}
Chris Lattner882f7882006-11-04 18:52:07 +0000733void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
734 PrintExpr(Node->getLHS());
735 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
736 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000737}
Chris Lattner86928112007-08-25 02:00:02 +0000738void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
739 PrintExpr(Node->getLHS());
740 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
741 PrintExpr(Node->getRHS());
742}
Chris Lattner882f7882006-11-04 18:52:07 +0000743void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
744 PrintExpr(Node->getCond());
Ted Kremenekebb1c0c2007-11-26 18:27:54 +0000745
746 if (Node->getLHS()) {
747 OS << " ? ";
748 PrintExpr(Node->getLHS());
749 OS << " : ";
750 }
751 else { // Handle GCC extention where LHS can be NULL.
752 OS << " ?: ";
753 }
754
Chris Lattner882f7882006-11-04 18:52:07 +0000755 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000756}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000757
Chris Lattnereefa10e2007-05-28 06:56:27 +0000758// GNU extensions.
759
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000760void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000761 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000762}
763
Chris Lattner366727f2007-07-24 16:58:17 +0000764void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
765 OS << "(";
766 PrintRawCompoundStmt(E->getSubStmt());
767 OS << ")";
768}
769
Steve Naroff78864672007-08-01 22:05:33 +0000770void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
771 OS << "__builtin_types_compatible_p(";
772 OS << Node->getArgType1().getAsString() << ",";
773 OS << Node->getArgType2().getAsString() << ")";
774}
775
Steve Naroff9efdabc2007-08-03 21:21:27 +0000776void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
777 OS << "__builtin_choose_expr(";
778 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +0000779 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000780 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +0000781 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000782 PrintExpr(Node->getRHS());
783 OS << ")";
784}
Chris Lattner366727f2007-07-24 16:58:17 +0000785
Douglas Gregor3be4b122008-11-29 04:51:27 +0000786void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
787 OS << "__null";
788}
789
Nate Begeman1e36a852008-01-17 17:46:27 +0000790void StmtPrinter::VisitOverloadExpr(OverloadExpr *Node) {
791 OS << "__builtin_overload(";
Nate Begeman936b2072008-01-30 20:50:20 +0000792 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
Nate Begeman1e36a852008-01-17 17:46:27 +0000793 if (i) OS << ", ";
Nate Begeman936b2072008-01-30 20:50:20 +0000794 PrintExpr(Node->getExpr(i));
Nate Begeman1e36a852008-01-17 17:46:27 +0000795 }
796 OS << ")";
797}
798
Eli Friedmana1b4ed82008-05-14 19:38:39 +0000799void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
800 OS << "__builtin_shufflevector(";
801 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
802 if (i) OS << ", ";
803 PrintExpr(Node->getExpr(i));
804 }
805 OS << ")";
806}
807
Anders Carlsson4692db02007-08-31 04:56:16 +0000808void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
809 OS << "{ ";
810 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
811 if (i) OS << ", ";
812 PrintExpr(Node->getInit(i));
813 }
814 OS << " }";
815}
816
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000817void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
818 OS << "va_arg(";
819 PrintExpr(Node->getSubExpr());
820 OS << ", ";
821 OS << Node->getType().getAsString();
822 OS << ")";
823}
824
Chris Lattnereefa10e2007-05-28 06:56:27 +0000825// C++
Douglas Gregor993603d2008-11-14 16:09:21 +0000826void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
827 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
828 "",
829#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
830 Spelling,
831#include "clang/Basic/OperatorKinds.def"
832 };
833
834 OverloadedOperatorKind Kind = Node->getOperator();
835 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
836 if (Node->getNumArgs() == 1) {
837 OS << OpStrings[Kind] << ' ';
838 PrintExpr(Node->getArg(0));
839 } else {
840 PrintExpr(Node->getArg(0));
841 OS << ' ' << OpStrings[Kind];
842 }
843 } else if (Kind == OO_Call) {
844 PrintExpr(Node->getArg(0));
845 OS << '(';
846 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
847 if (ArgIdx > 1)
848 OS << ", ";
849 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
850 PrintExpr(Node->getArg(ArgIdx));
851 }
852 OS << ')';
853 } else if (Kind == OO_Subscript) {
854 PrintExpr(Node->getArg(0));
855 OS << '[';
856 PrintExpr(Node->getArg(1));
857 OS << ']';
858 } else if (Node->getNumArgs() == 1) {
859 OS << OpStrings[Kind] << ' ';
860 PrintExpr(Node->getArg(0));
861 } else if (Node->getNumArgs() == 2) {
862 PrintExpr(Node->getArg(0));
863 OS << ' ' << OpStrings[Kind] << ' ';
864 PrintExpr(Node->getArg(1));
865 } else {
866 assert(false && "unknown overloaded operator");
867 }
868}
Chris Lattnereefa10e2007-05-28 06:56:27 +0000869
Douglas Gregore200adc2008-10-27 19:41:14 +0000870void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
871 OS << Node->getCastName() << '<';
872 OS << Node->getTypeAsWritten().getAsString() << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +0000873 PrintExpr(Node->getSubExpr());
874 OS << ")";
875}
876
Douglas Gregore200adc2008-10-27 19:41:14 +0000877void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
878 VisitCXXNamedCastExpr(Node);
879}
880
881void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
882 VisitCXXNamedCastExpr(Node);
883}
884
885void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
886 VisitCXXNamedCastExpr(Node);
887}
888
889void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
890 VisitCXXNamedCastExpr(Node);
891}
892
Sebastian Redlc4704762008-11-11 11:37:55 +0000893void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
894 OS << "typeid(";
895 if (Node->isTypeOperand()) {
896 OS << Node->getTypeOperand().getAsString();
897 } else {
898 PrintExpr(Node->getExprOperand());
899 }
900 OS << ")";
901}
902
Chris Lattnereefa10e2007-05-28 06:56:27 +0000903void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
904 OS << (Node->getValue() ? "true" : "false");
905}
906
Douglas Gregor97a9c812008-11-04 14:32:21 +0000907void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
908 OS << "this";
909}
910
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000911void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
912 if (Node->getSubExpr() == 0)
913 OS << "throw";
914 else {
915 OS << "throw ";
916 PrintExpr(Node->getSubExpr());
917 }
918}
919
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000920void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
921 // Nothing to print: we picked up the default argument
922}
923
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000924void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
925 OS << Node->getType().getAsString();
926 OS << "(";
927 PrintExpr(Node->getSubExpr());
928 OS << ")";
929}
930
931void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
932 OS << Node->getType().getAsString() << "()";
933}
934
Argyrios Kyrtzidisaa479132008-09-09 23:47:53 +0000935void
936StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
937 PrintRawDecl(E->getVarDecl());
938}
939
Sebastian Redlbd150f42008-11-21 19:14:01 +0000940void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
941 if (E->isGlobalNew())
942 OS << "::";
943 OS << "new ";
944 unsigned NumPlace = E->getNumPlacementArgs();
945 if (NumPlace > 0) {
946 OS << "(";
947 PrintExpr(E->getPlacementArg(0));
948 for (unsigned i = 1; i < NumPlace; ++i) {
949 OS << ", ";
950 PrintExpr(E->getPlacementArg(i));
951 }
952 OS << ") ";
953 }
954 if (E->isParenTypeId())
955 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +0000956 std::string TypeS;
957 if (Expr *Size = E->getArraySize()) {
958 llvm::raw_string_ostream s(TypeS);
959 Size->printPretty(s);
960 s.flush();
961 TypeS = "[" + TypeS + "]";
962 }
963 E->getAllocatedType().getAsStringInternal(TypeS);
964 OS << TypeS;
Sebastian Redlbd150f42008-11-21 19:14:01 +0000965 if (E->isParenTypeId())
966 OS << ")";
967
968 if (E->hasInitializer()) {
969 OS << "(";
970 unsigned NumCons = E->getNumConstructorArgs();
971 if (NumCons > 0) {
972 PrintExpr(E->getConstructorArg(0));
973 for (unsigned i = 1; i < NumCons; ++i) {
974 OS << ", ";
975 PrintExpr(E->getConstructorArg(i));
976 }
977 }
978 OS << ")";
979 }
980}
981
982void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
983 if (E->isGlobalDelete())
984 OS << "::";
985 OS << "delete ";
986 if (E->isArrayForm())
987 OS << "[] ";
988 PrintExpr(E->getArgument());
989}
990
Douglas Gregorb0846b02008-12-06 00:22:45 +0000991void StmtPrinter::VisitCXXDependentNameExpr(CXXDependentNameExpr *E) {
992 OS << E->getName()->getName();
993}
994
Anders Carlsson76f4a902007-08-21 17:43:55 +0000995// Obj-C
996
997void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
998 OS << "@";
999 VisitStringLiteral(Node->getString());
1000}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001001
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001002void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001003 OS << "@encode(" << Node->getEncodedType().getAsString() << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001004}
1005
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001006void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001007 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001008}
1009
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001010void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001011 OS << "@protocol(" << Node->getProtocol()->getNameAsString() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001012}
1013
Steve Naroffd54978b2007-09-18 23:55:05 +00001014void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1015 OS << "[";
Steve Naroffc6814ea2007-10-02 20:01:56 +00001016 Expr *receiver = Mess->getReceiver();
1017 if (receiver) PrintExpr(receiver);
1018 else OS << Mess->getClassName()->getName();
Ted Kremeneka06e7122008-05-02 17:32:38 +00001019 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00001020 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001021 if (selector.isUnarySelector()) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001022 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001023 } else {
1024 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001025 if (i < selector.getNumArgs()) {
1026 if (i > 0) OS << ' ';
1027 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001028 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00001029 else
1030 OS << ":";
1031 }
1032 else OS << ", "; // Handle variadic methods.
1033
Steve Naroffc6814ea2007-10-02 20:01:56 +00001034 PrintExpr(Mess->getArg(i));
1035 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001036 }
1037 OS << "]";
1038}
1039
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001040void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) {
1041 OS << "super";
1042}
1043
Steve Naroffc540d662008-09-03 18:15:37 +00001044void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001045 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00001046 OS << "^";
1047
1048 const FunctionType *AFT = Node->getFunctionType();
1049
1050 if (isa<FunctionTypeNoProto>(AFT)) {
1051 OS << "()";
Steve Naroff415d3d52008-10-08 17:01:13 +00001052 } else if (!BD->param_empty() || cast<FunctionTypeProto>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00001053 OS << '(';
1054 std::string ParamStr;
Steve Naroff415d3d52008-10-08 17:01:13 +00001055 for (BlockDecl::param_iterator AI = BD->param_begin(),
1056 E = BD->param_end(); AI != E; ++AI) {
1057 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001058 ParamStr = (*AI)->getNameAsString();
Steve Naroffc540d662008-09-03 18:15:37 +00001059 (*AI)->getType().getAsStringInternal(ParamStr);
1060 OS << ParamStr;
1061 }
1062
Steve Naroff415d3d52008-10-08 17:01:13 +00001063 const FunctionTypeProto *FT = cast<FunctionTypeProto>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00001064 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001065 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00001066 OS << "...";
1067 }
1068 OS << ')';
1069 }
1070}
1071
Steve Naroffc540d662008-09-03 18:15:37 +00001072void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001073 OS << Node->getDecl()->getNameAsString();
Steve Naroffc540d662008-09-03 18:15:37 +00001074}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001075//===----------------------------------------------------------------------===//
1076// Stmt method implementations
1077//===----------------------------------------------------------------------===//
1078
Chris Lattnercbe4f772007-08-08 22:51:59 +00001079void Stmt::dumpPretty() const {
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001080 printPretty(llvm::errs());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001081}
1082
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001083void Stmt::printPretty(llvm::raw_ostream &OS, PrinterHelper* Helper) const {
Chris Lattner882f7882006-11-04 18:52:07 +00001084 if (this == 0) {
1085 OS << "<NULL>";
1086 return;
1087 }
1088
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001089 StmtPrinter P(OS, Helper);
Chris Lattner62249a62007-08-21 04:04:25 +00001090 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001091}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001092
1093//===----------------------------------------------------------------------===//
1094// PrinterHelper
1095//===----------------------------------------------------------------------===//
1096
1097// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +00001098PrinterHelper::~PrinterHelper() {}