blob: 3c3e207a02db773cfa306a3b922a80f8ba702bd5 [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"
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000020using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// StmtPrinter Visitor
24//===----------------------------------------------------------------------===//
25
26namespace {
Chris Lattner62249a62007-08-21 04:04:25 +000027 class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor<StmtPrinter> {
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000028 std::ostream &OS;
29 unsigned IndentLevel;
Ted Kremenek04f3cee2007-08-31 21:30:12 +000030 clang::PrinterHelper* Helper;
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000031 public:
Ted Kremenek04f3cee2007-08-31 21:30:12 +000032 StmtPrinter(std::ostream &os, PrinterHelper* helper) :
33 OS(os), IndentLevel(0), Helper(helper) {}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000034
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000035 void PrintStmt(Stmt *S, int SubIndent = 1) {
36 IndentLevel += SubIndent;
Chris Lattnera076fde2007-05-31 18:21:33 +000037 if (S && isa<Expr>(S)) {
Chris Lattner882f7882006-11-04 18:52:07 +000038 // If this is an expr used in a stmt context, indent and newline it.
39 Indent();
Chris Lattner62249a62007-08-21 04:04:25 +000040 Visit(S);
Chris Lattnerfc068c12007-05-30 17:57:36 +000041 OS << ";\n";
Chris Lattner882f7882006-11-04 18:52:07 +000042 } else if (S) {
Chris Lattner62249a62007-08-21 04:04:25 +000043 Visit(S);
Chris Lattner882f7882006-11-04 18:52:07 +000044 } else {
Chris Lattner2f6ac262007-05-28 01:47:47 +000045 Indent() << "<<<NULL STATEMENT>>>\n";
Chris Lattner882f7882006-11-04 18:52:07 +000046 }
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000047 IndentLevel -= SubIndent;
Chris Lattner882f7882006-11-04 18:52:07 +000048 }
Chris Lattner073926e2007-05-20 23:04:55 +000049
50 void PrintRawCompoundStmt(CompoundStmt *S);
Chris Lattnerfdc195a2007-06-05 20:52:47 +000051 void PrintRawDecl(Decl *D);
Chris Lattnerc0a38dd2007-06-11 22:26:23 +000052 void PrintRawIfStmt(IfStmt *If);
53
Chris Lattner882f7882006-11-04 18:52:07 +000054 void PrintExpr(Expr *E) {
55 if (E)
Chris Lattner62249a62007-08-21 04:04:25 +000056 Visit(E);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000057 else
Chris Lattner882f7882006-11-04 18:52:07 +000058 OS << "<null expr>";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000059 }
60
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000061 std::ostream &Indent(int Delta = 0) const {
Chris Lattnerd5322cd2007-05-29 23:49:07 +000062 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000063 OS << " ";
64 return OS;
65 }
66
Chris Lattner98dbf0a2007-08-30 17:59:59 +000067 bool PrintOffsetOfDesignator(Expr *E);
68 void VisitUnaryOffsetOf(UnaryOperator *Node);
69
Ted Kremenek04f3cee2007-08-31 21:30:12 +000070 void Visit(Stmt* S) {
71 if (Helper && Helper->handledStmt(S,OS))
72 return;
73 else StmtVisitor<StmtPrinter>::Visit(S);
74 }
75
Chris Lattner62249a62007-08-21 04:04:25 +000076 void VisitStmt(Stmt *Node);
Steve Naroff7f890eb2007-02-27 02:53:10 +000077#define STMT(N, CLASS, PARENT) \
Chris Lattner62249a62007-08-21 04:04:25 +000078 void Visit##CLASS(CLASS *Node);
Chris Lattnerf2174b62006-11-04 20:59:27 +000079#include "clang/AST/StmtNodes.def"
Chris Lattner71e23ce2006-11-04 20:18:38 +000080 };
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000081}
82
Chris Lattner71e23ce2006-11-04 20:18:38 +000083//===----------------------------------------------------------------------===//
84// Stmt printing methods.
85//===----------------------------------------------------------------------===//
86
Chris Lattner882f7882006-11-04 18:52:07 +000087void StmtPrinter::VisitStmt(Stmt *Node) {
88 Indent() << "<<unknown stmt type>>\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000089}
90
Chris Lattner073926e2007-05-20 23:04:55 +000091/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
92/// with no newline after the }.
93void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
94 OS << "{\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000095 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
Chris Lattner882f7882006-11-04 18:52:07 +000096 I != E; ++I)
97 PrintStmt(*I);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000098
Chris Lattner073926e2007-05-20 23:04:55 +000099 Indent() << "}";
100}
101
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000102void StmtPrinter::PrintRawDecl(Decl *D) {
103 // FIXME: Need to complete/beautify this... this code simply shows the
Steve Naroff2a8ad182007-05-29 22:59:26 +0000104 // nodes are where they need to be.
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000105 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
106 OS << "typedef " << localType->getUnderlyingType().getAsString();
107 OS << " " << localType->getName();
108 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattnerb4522b42007-06-02 05:27:01 +0000109 // Emit storage class for vardecls.
110 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
111 switch (V->getStorageClass()) {
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000112 default: assert(0 && "Unknown storage class!");
113 case VarDecl::None: break;
114 case VarDecl::Extern: OS << "extern "; break;
115 case VarDecl::Static: OS << "static "; break;
116 case VarDecl::Auto: OS << "auto "; break;
117 case VarDecl::Register: OS << "register "; break;
Chris Lattnerb4522b42007-06-02 05:27:01 +0000118 }
119 }
120
Chris Lattnere0c4ae12007-06-02 03:38:08 +0000121 std::string Name = VD->getName();
122 VD->getType().getAsStringInternal(Name);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000123 OS << Name;
124
Chris Lattner79c57592007-07-12 00:36:32 +0000125 // If this is a vardecl with an initializer, emit it.
126 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
127 if (V->getInit()) {
128 OS << " = ";
129 PrintExpr(V->getInit());
130 }
131 }
Steve Naroffc1e6d602007-11-17 21:21:01 +0000132 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
133 // print a free standing tag decl (e.g. "struct x;").
134 OS << TD->getKindName();
135 OS << " ";
136 if (const IdentifierInfo *II = TD->getIdentifier())
137 OS << II->getName();
138 else
139 OS << "<anonymous>";
140 // FIXME: print tag bodies.
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000141 } else {
Chris Lattnere0c4ae12007-06-02 03:38:08 +0000142 assert(0 && "Unexpected decl");
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000143 }
144}
145
146
147void StmtPrinter::VisitNullStmt(NullStmt *Node) {
148 Indent() << ";\n";
149}
150
151void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Steve Naroffa23cc792007-09-13 23:52:58 +0000152 for (ScopedDecl *D = Node->getDecl(); D; D = D->getNextDeclarator()) {
Chris Lattner776fac82007-06-09 00:53:06 +0000153 Indent();
154 PrintRawDecl(D);
155 OS << ";\n";
156 }
Steve Naroff2a8ad182007-05-29 22:59:26 +0000157}
158
Chris Lattner073926e2007-05-20 23:04:55 +0000159void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
160 Indent();
161 PrintRawCompoundStmt(Node);
Chris Lattnerdf3cafb2007-05-31 05:08:56 +0000162 OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000163}
164
Chris Lattner6c0ff132006-11-05 00:19:50 +0000165void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000166 Indent(-1) << "case ";
Chris Lattner6c0ff132006-11-05 00:19:50 +0000167 PrintExpr(Node->getLHS());
168 if (Node->getRHS()) {
169 OS << " ... ";
170 PrintExpr(Node->getRHS());
171 }
172 OS << ":\n";
173
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000174 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000175}
176
177void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000178 Indent(-1) << "default:\n";
179 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000180}
181
182void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000183 Indent(-1) << Node->getName() << ":\n";
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000184 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000185}
186
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000187void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
188 OS << "if ";
Chris Lattner882f7882006-11-04 18:52:07 +0000189 PrintExpr(If->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000190
191 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
192 OS << ' ';
193 PrintRawCompoundStmt(CS);
194 OS << (If->getElse() ? ' ' : '\n');
195 } else {
196 OS << '\n';
197 PrintStmt(If->getThen());
198 if (If->getElse()) Indent();
199 }
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000200
Chris Lattner073926e2007-05-20 23:04:55 +0000201 if (Stmt *Else = If->getElse()) {
202 OS << "else";
203
204 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
205 OS << ' ';
206 PrintRawCompoundStmt(CS);
207 OS << '\n';
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000208 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
209 OS << ' ';
210 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000211 } else {
212 OS << '\n';
213 PrintStmt(If->getElse());
214 }
Chris Lattner882f7882006-11-04 18:52:07 +0000215 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000216}
217
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000218void StmtPrinter::VisitIfStmt(IfStmt *If) {
219 Indent();
220 PrintRawIfStmt(If);
221}
222
Chris Lattnerf2174b62006-11-04 20:59:27 +0000223void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
224 Indent() << "switch (";
225 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000226 OS << ")";
227
228 // Pretty print compoundstmt bodies (very common).
229 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
230 OS << " ";
231 PrintRawCompoundStmt(CS);
232 OS << "\n";
233 } else {
234 OS << "\n";
235 PrintStmt(Node->getBody());
236 }
Chris Lattnerf2174b62006-11-04 20:59:27 +0000237}
238
Anders Carlsson51873c22007-07-22 07:07:56 +0000239void StmtPrinter::VisitSwitchCase(SwitchCase*) {
240 assert(0 && "SwitchCase is an abstract class");
241}
242
Chris Lattner85ed8732006-11-04 20:40:44 +0000243void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
244 Indent() << "while (";
245 PrintExpr(Node->getCond());
246 OS << ")\n";
247 PrintStmt(Node->getBody());
248}
249
250void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000251 Indent() << "do ";
252 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
253 PrintRawCompoundStmt(CS);
254 OS << " ";
255 } else {
256 OS << "\n";
257 PrintStmt(Node->getBody());
258 Indent();
259 }
260
261 OS << "while ";
Chris Lattner85ed8732006-11-04 20:40:44 +0000262 PrintExpr(Node->getCond());
Chris Lattnera076fde2007-05-31 18:21:33 +0000263 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000264}
265
Chris Lattner71e23ce2006-11-04 20:18:38 +0000266void StmtPrinter::VisitForStmt(ForStmt *Node) {
267 Indent() << "for (";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000268 if (Node->getInit()) {
269 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
270 PrintRawDecl(DS->getDecl());
271 else
272 PrintExpr(cast<Expr>(Node->getInit()));
273 }
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000274 OS << ";";
275 if (Node->getCond()) {
276 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000277 PrintExpr(Node->getCond());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000278 }
279 OS << ";";
280 if (Node->getInc()) {
281 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000282 PrintExpr(Node->getInc());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000283 }
284 OS << ") ";
285
286 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
287 PrintRawCompoundStmt(CS);
288 OS << "\n";
289 } else {
290 OS << "\n";
291 PrintStmt(Node->getBody());
292 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000293}
294
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000295void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian83615522008-01-02 22:54:34 +0000296 Indent() << "for (";
297 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
298 PrintRawDecl(DS->getDecl());
299 else
300 PrintExpr(cast<Expr>(Node->getElement()));
301 OS << " in ";
302 PrintExpr(Node->getCollection());
303 OS << ") ";
304
305 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
306 PrintRawCompoundStmt(CS);
307 OS << "\n";
308 } else {
309 OS << "\n";
310 PrintStmt(Node->getBody());
311 }
312}
313
Chris Lattner16976d32006-11-05 01:46:01 +0000314void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000315 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000316}
317
318void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000319 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000320 PrintExpr(Node->getTarget());
Chris Lattnerb4619482007-05-31 06:00:14 +0000321 OS << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000322}
323
324void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000325 Indent() << "continue;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000326}
327
328void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000329 Indent() << "break;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000330}
331
332
Chris Lattner882f7882006-11-04 18:52:07 +0000333void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
334 Indent() << "return";
335 if (Node->getRetValue()) {
336 OS << " ";
337 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000338 }
Chris Lattnerb4619482007-05-31 06:00:14 +0000339 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000340}
341
Chris Lattner73c56c02007-10-29 04:04:16 +0000342
343void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson660bdd12007-11-23 23:12:25 +0000344 Indent() << "asm ";
345
346 if (Node->isVolatile())
347 OS << "volatile ";
348
349 OS << "(";
Anders Carlsson81a5a692007-11-20 19:21:03 +0000350 VisitStringLiteral(Node->getAsmString());
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000351
352 // Outputs
353 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
354 Node->getNumClobbers() != 0)
355 OS << " : ";
356
357 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
358 if (i != 0)
359 OS << ", ";
360
361 if (!Node->getOutputName(i).empty()) {
362 OS << '[';
363 OS << Node->getOutputName(i);
364 OS << "] ";
365 }
366
367 VisitStringLiteral(Node->getOutputConstraint(i));
368 OS << " ";
369 Visit(Node->getOutputExpr(i));
370 }
371
372 // Inputs
373 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
374 OS << " : ";
375
376 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
377 if (i != 0)
378 OS << ", ";
379
380 if (!Node->getInputName(i).empty()) {
381 OS << '[';
382 OS << Node->getInputName(i);
383 OS << "] ";
384 }
385
386 VisitStringLiteral(Node->getInputConstraint(i));
387 OS << " ";
388 Visit(Node->getInputExpr(i));
389 }
390
391 // Clobbers
392 if (Node->getNumClobbers() != 0)
393 OS << " : ";
394
395 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
396 if (i != 0)
397 OS << ", ";
398
399 VisitStringLiteral(Node->getClobber(i));
400 }
401
Anders Carlsson81a5a692007-11-20 19:21:03 +0000402 OS << ");\n";
Chris Lattner73c56c02007-10-29 04:04:16 +0000403}
404
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000405void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000406 Indent() << "@try";
407 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
408 PrintRawCompoundStmt(TS);
409 OS << "\n";
410 }
411
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000412 for (ObjCAtCatchStmt *catchStmt =
413 static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts());
Fariborz Jahanian88157952007-11-02 18:16:07 +0000414 catchStmt;
415 catchStmt =
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000416 static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000417 Indent() << "@catch(";
418 if (catchStmt->getCatchParamStmt()) {
419 if (DeclStmt *DS = dyn_cast<DeclStmt>(catchStmt->getCatchParamStmt()))
420 PrintRawDecl(DS->getDecl());
421 }
422 OS << ")";
423 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody()))
424 {
425 PrintRawCompoundStmt(CS);
426 OS << "\n";
427 }
428 }
429
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000430 if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>(
Fariborz Jahaniandefbf9a2007-11-07 00:46:42 +0000431 Node->getFinallyStmt())) {
432 Indent() << "@finally";
433 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian88157952007-11-02 18:16:07 +0000434 OS << "\n";
435 }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000436}
437
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000438void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000439}
440
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000441void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000442 Indent() << "@catch (...) { /* todo */ } \n";
443}
444
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000445void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000446 Indent() << "@throw";
447 if (Node->getThrowExpr()) {
448 OS << " ";
449 PrintExpr(Node->getThrowExpr());
450 }
451 OS << ";\n";
452}
453
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000454void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000455 Indent() << "@synchronized (";
456 PrintExpr(Node->getSynchExpr());
457 OS << ")";
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000458 PrintRawCompoundStmt(Node->getSynchBody());
459 OS << "\n";
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000460}
461
Chris Lattner71e23ce2006-11-04 20:18:38 +0000462//===----------------------------------------------------------------------===//
463// Expr printing methods.
464//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000465
Chris Lattner882f7882006-11-04 18:52:07 +0000466void StmtPrinter::VisitExpr(Expr *Node) {
467 OS << "<<unknown expr type>>";
468}
469
470void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Chris Lattner5efbb332006-11-20 05:01:40 +0000471 OS << Node->getDecl()->getName();
Chris Lattner882f7882006-11-04 18:52:07 +0000472}
473
Steve Naroffe46504b2007-11-12 14:29:37 +0000474void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000475 if (Node->getBase()) {
476 PrintExpr(Node->getBase());
477 OS << (Node->isArrow() ? "->" : ".");
478 }
Steve Naroffe46504b2007-11-12 14:29:37 +0000479 OS << Node->getDecl()->getName();
480}
481
Steve Naroffec944032008-05-30 00:40:33 +0000482void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
483 if (Node->getBase()) {
484 PrintExpr(Node->getBase());
485 OS << ".";
486 }
487 // FIXME: OS << Node->getDecl()->getName();
488}
489
Chris Lattner6307f192008-08-10 01:53:14 +0000490void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson625bfc82007-07-21 05:21:51 +0000491 switch (Node->getIdentType()) {
492 default:
493 assert(0 && "unknown case");
Chris Lattner6307f192008-08-10 01:53:14 +0000494 case PredefinedExpr::Func:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000495 OS << "__func__";
496 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000497 case PredefinedExpr::Function:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000498 OS << "__FUNCTION__";
499 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000500 case PredefinedExpr::PrettyFunction:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000501 OS << "__PRETTY_FUNCTION__";
502 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000503 case PredefinedExpr::ObjCSuper:
Chris Lattnera9b3cae2008-06-21 18:04:54 +0000504 OS << "super";
505 break;
Anders Carlsson625bfc82007-07-21 05:21:51 +0000506 }
507}
508
Steve Naroffae4143e2007-04-26 20:39:23 +0000509void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000510 unsigned value = Node->getValue();
Chris Lattnera5678cc2008-06-07 22:35:38 +0000511 if (Node->isWide())
512 OS << "L";
Chris Lattner666115c2007-07-13 23:58:20 +0000513 switch (value) {
514 case '\\':
515 OS << "'\\\\'";
516 break;
517 case '\'':
518 OS << "'\\''";
519 break;
520 case '\a':
521 // TODO: K&R: the meaning of '\\a' is different in traditional C
522 OS << "'\\a'";
523 break;
524 case '\b':
525 OS << "'\\b'";
526 break;
527 // Nonstandard escape sequence.
528 /*case '\e':
529 OS << "'\\e'";
530 break;*/
531 case '\f':
532 OS << "'\\f'";
533 break;
534 case '\n':
535 OS << "'\\n'";
536 break;
537 case '\r':
538 OS << "'\\r'";
539 break;
540 case '\t':
541 OS << "'\\t'";
542 break;
543 case '\v':
544 OS << "'\\v'";
545 break;
546 default:
Ted Kremenek652d18e2008-02-23 00:52:04 +0000547 if (value < 256 && isprint(value)) {
Chris Lattner666115c2007-07-13 23:58:20 +0000548 OS << "'" << (char)value << "'";
549 } else if (value < 256) {
550 OS << "'\\x" << std::hex << value << std::dec << "'";
551 } else {
552 // FIXME what to really do here?
553 OS << value;
554 }
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000555 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000556}
557
Steve Naroffdf7855b2007-02-21 23:46:25 +0000558void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000559 bool isSigned = Node->getType()->isSignedIntegerType();
560 OS << Node->getValue().toString(10, isSigned);
561
562 // Emit suffixes. Integer literals are always a builtin integer type.
Chris Lattner574dee62008-07-26 22:17:49 +0000563 switch (Node->getType()->getAsBuiltinType()->getKind()) {
Chris Lattner06430412007-05-21 05:45:03 +0000564 default: assert(0 && "Unexpected type for integer literal!");
565 case BuiltinType::Int: break; // no suffix.
566 case BuiltinType::UInt: OS << 'U'; break;
567 case BuiltinType::Long: OS << 'L'; break;
568 case BuiltinType::ULong: OS << "UL"; break;
569 case BuiltinType::LongLong: OS << "LL"; break;
570 case BuiltinType::ULongLong: OS << "ULL"; break;
571 }
Chris Lattner882f7882006-11-04 18:52:07 +0000572}
Steve Naroffab624882007-02-21 22:05:47 +0000573void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattnerbf2f3862007-08-01 00:23:58 +0000574 // FIXME: print value more precisely.
Chris Lattnera0173132008-06-07 22:13:43 +0000575 OS << Node->getValueAsApproximateDouble();
Chris Lattner882f7882006-11-04 18:52:07 +0000576}
Chris Lattner1c20a172007-08-26 03:42:43 +0000577
578void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
579 PrintExpr(Node->getSubExpr());
580 OS << "i";
581}
582
Steve Naroffdf7855b2007-02-21 23:46:25 +0000583void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Chris Lattner882f7882006-11-04 18:52:07 +0000584 if (Str->isWide()) OS << 'L';
Chris Lattner5d8f4942006-11-04 20:29:31 +0000585 OS << '"';
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000586
Chris Lattner5d8f4942006-11-04 20:29:31 +0000587 // FIXME: this doesn't print wstrings right.
588 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
589 switch (Str->getStrData()[i]) {
590 default: OS << Str->getStrData()[i]; break;
591 // Handle some common ones to make dumps prettier.
592 case '\\': OS << "\\\\"; break;
593 case '"': OS << "\\\""; break;
594 case '\n': OS << "\\n"; break;
595 case '\t': OS << "\\t"; break;
596 case '\a': OS << "\\a"; break;
597 case '\b': OS << "\\b"; break;
598 }
599 }
600 OS << '"';
Chris Lattner882f7882006-11-04 18:52:07 +0000601}
602void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
603 OS << "(";
604 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000605 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000606}
607void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +0000608 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +0000609 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattnere5b60442007-08-23 21:46:40 +0000610
611 // Print a space if this is an "identifier operator" like sizeof or __real.
612 switch (Node->getOpcode()) {
613 default: break;
614 case UnaryOperator::SizeOf:
615 case UnaryOperator::AlignOf:
616 case UnaryOperator::Real:
617 case UnaryOperator::Imag:
618 case UnaryOperator::Extension:
619 OS << ' ';
620 break;
621 }
622 }
Chris Lattner882f7882006-11-04 18:52:07 +0000623 PrintExpr(Node->getSubExpr());
Chris Lattner15768702006-11-05 23:54:51 +0000624
625 if (Node->isPostfix())
626 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000627}
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000628
629bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
630 if (isa<CompoundLiteralExpr>(E)) {
631 // Base case, print the type and comma.
632 OS << E->getType().getAsString() << ", ";
633 return true;
634 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
635 PrintOffsetOfDesignator(ASE->getLHS());
636 OS << "[";
637 PrintExpr(ASE->getRHS());
638 OS << "]";
639 return false;
640 } else {
641 MemberExpr *ME = cast<MemberExpr>(E);
642 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
643 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getName();
644 return false;
645 }
646}
647
648void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
649 OS << "__builtin_offsetof(";
650 PrintOffsetOfDesignator(Node->getSubExpr());
651 OS << ")";
652}
653
Chris Lattner882f7882006-11-04 18:52:07 +0000654void StmtPrinter::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
Chris Lattner33e8a552006-11-19 01:48:02 +0000655 OS << (Node->isSizeOf() ? "sizeof(" : "__alignof(");
Chris Lattner3dc3d772007-05-16 18:07:12 +0000656 OS << Node->getArgumentType().getAsString() << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000657}
658void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000659 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000660 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +0000661 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000662 OS << "]";
663}
664
665void StmtPrinter::VisitCallExpr(CallExpr *Call) {
666 PrintExpr(Call->getCallee());
667 OS << "(";
668 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000669 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
670 // Don't print any defaulted arguments
671 break;
672 }
673
Chris Lattner882f7882006-11-04 18:52:07 +0000674 if (i) OS << ", ";
675 PrintExpr(Call->getArg(i));
676 }
677 OS << ")";
678}
679void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
680 PrintExpr(Node->getBase());
681 OS << (Node->isArrow() ? "->" : ".");
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000682
Chris Lattner24e0d6c2007-05-24 00:47:01 +0000683 FieldDecl *Field = Node->getMemberDecl();
684 assert(Field && "MemberExpr should alway reference a field!");
685 OS << Field->getName();
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000686}
Nate Begemance4d7fc2008-04-18 23:10:10 +0000687void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +0000688 PrintExpr(Node->getBase());
689 OS << ".";
690 OS << Node->getAccessor().getName();
691}
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +0000692void StmtPrinter::VisitCastExpr(CastExpr *) {
693 assert(0 && "CastExpr is an abstract class");
694}
695void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *Node) {
Chris Lattner51aff8b2007-07-15 23:54:50 +0000696 OS << "(" << Node->getType().getAsString() << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000697 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000698}
Steve Naroff57eb2c52007-07-19 21:32:11 +0000699void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
700 OS << "(" << Node->getType().getAsString() << ")";
701 PrintExpr(Node->getInitializer());
702}
Steve Naroff7a5af782007-07-13 16:58:59 +0000703void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000704 // No need to print anything, simply forward to the sub expression.
705 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +0000706}
Chris Lattner882f7882006-11-04 18:52:07 +0000707void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
708 PrintExpr(Node->getLHS());
709 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
710 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000711}
Chris Lattner86928112007-08-25 02:00:02 +0000712void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
713 PrintExpr(Node->getLHS());
714 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
715 PrintExpr(Node->getRHS());
716}
Chris Lattner882f7882006-11-04 18:52:07 +0000717void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
718 PrintExpr(Node->getCond());
Ted Kremenekebb1c0c2007-11-26 18:27:54 +0000719
720 if (Node->getLHS()) {
721 OS << " ? ";
722 PrintExpr(Node->getLHS());
723 OS << " : ";
724 }
725 else { // Handle GCC extention where LHS can be NULL.
726 OS << " ?: ";
727 }
728
Chris Lattner882f7882006-11-04 18:52:07 +0000729 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000730}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000731
Chris Lattnereefa10e2007-05-28 06:56:27 +0000732// GNU extensions.
733
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000734void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000735 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000736}
737
Chris Lattner366727f2007-07-24 16:58:17 +0000738void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
739 OS << "(";
740 PrintRawCompoundStmt(E->getSubStmt());
741 OS << ")";
742}
743
Steve Naroff78864672007-08-01 22:05:33 +0000744void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
745 OS << "__builtin_types_compatible_p(";
746 OS << Node->getArgType1().getAsString() << ",";
747 OS << Node->getArgType2().getAsString() << ")";
748}
749
Steve Naroff9efdabc2007-08-03 21:21:27 +0000750void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
751 OS << "__builtin_choose_expr(";
752 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +0000753 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000754 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +0000755 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000756 PrintExpr(Node->getRHS());
757 OS << ")";
758}
Chris Lattner366727f2007-07-24 16:58:17 +0000759
Nate Begeman1e36a852008-01-17 17:46:27 +0000760void StmtPrinter::VisitOverloadExpr(OverloadExpr *Node) {
761 OS << "__builtin_overload(";
Nate Begeman936b2072008-01-30 20:50:20 +0000762 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
Nate Begeman1e36a852008-01-17 17:46:27 +0000763 if (i) OS << ", ";
Nate Begeman936b2072008-01-30 20:50:20 +0000764 PrintExpr(Node->getExpr(i));
Nate Begeman1e36a852008-01-17 17:46:27 +0000765 }
766 OS << ")";
767}
768
Eli Friedmana1b4ed82008-05-14 19:38:39 +0000769void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
770 OS << "__builtin_shufflevector(";
771 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
772 if (i) OS << ", ";
773 PrintExpr(Node->getExpr(i));
774 }
775 OS << ")";
776}
777
Anders Carlsson4692db02007-08-31 04:56:16 +0000778void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
779 OS << "{ ";
780 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
781 if (i) OS << ", ";
782 PrintExpr(Node->getInit(i));
783 }
784 OS << " }";
785}
786
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000787void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
788 OS << "va_arg(";
789 PrintExpr(Node->getSubExpr());
790 OS << ", ";
791 OS << Node->getType().getAsString();
792 OS << ")";
793}
794
Chris Lattnereefa10e2007-05-28 06:56:27 +0000795// C++
796
797void StmtPrinter::VisitCXXCastExpr(CXXCastExpr *Node) {
Chris Lattner3f5e4682007-08-09 17:34:19 +0000798 OS << CXXCastExpr::getOpcodeStr(Node->getOpcode()) << '<';
Chris Lattnereefa10e2007-05-28 06:56:27 +0000799 OS << Node->getDestType().getAsString() << ">(";
800 PrintExpr(Node->getSubExpr());
801 OS << ")";
802}
803
804void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
805 OS << (Node->getValue() ? "true" : "false");
806}
807
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000808void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
809 if (Node->getSubExpr() == 0)
810 OS << "throw";
811 else {
812 OS << "throw ";
813 PrintExpr(Node->getSubExpr());
814 }
815}
816
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000817void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
818 // Nothing to print: we picked up the default argument
819}
820
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000821void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
822 OS << Node->getType().getAsString();
823 OS << "(";
824 PrintExpr(Node->getSubExpr());
825 OS << ")";
826}
827
828void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
829 OS << Node->getType().getAsString() << "()";
830}
831
Argyrios Kyrtzidisaa479132008-09-09 23:47:53 +0000832void
833StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
834 PrintRawDecl(E->getVarDecl());
835}
836
Anders Carlsson76f4a902007-08-21 17:43:55 +0000837// Obj-C
838
839void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
840 OS << "@";
841 VisitStringLiteral(Node->getString());
842}
Chris Lattnereefa10e2007-05-28 06:56:27 +0000843
Anders Carlssonc5a81eb2007-08-22 15:14:15 +0000844void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattnerc7f39812007-10-18 00:39:29 +0000845 OS << "@encode(" << Node->getEncodedType().getAsString() << ")";
Anders Carlssonc5a81eb2007-08-22 15:14:15 +0000846}
847
Fariborz Jahanian4bef4622007-10-16 20:40:23 +0000848void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattnerc7f39812007-10-18 00:39:29 +0000849 OS << "@selector(" << Node->getSelector().getName() << ")";
Fariborz Jahanian4bef4622007-10-16 20:40:23 +0000850}
851
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +0000852void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattnerc7f39812007-10-18 00:39:29 +0000853 OS << "@protocol(" << Node->getProtocol()->getName() << ")";
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +0000854}
855
Steve Naroffd54978b2007-09-18 23:55:05 +0000856void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
857 OS << "[";
Steve Naroffc6814ea2007-10-02 20:01:56 +0000858 Expr *receiver = Mess->getReceiver();
859 if (receiver) PrintExpr(receiver);
860 else OS << Mess->getClassName()->getName();
Ted Kremeneka06e7122008-05-02 17:32:38 +0000861 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +0000862 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +0000863 if (selector.isUnarySelector()) {
Ted Kremeneka06e7122008-05-02 17:32:38 +0000864 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroffc6814ea2007-10-02 20:01:56 +0000865 } else {
866 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +0000867 if (i < selector.getNumArgs()) {
868 if (i > 0) OS << ' ';
869 if (selector.getIdentifierInfoForSlot(i))
870 OS << selector.getIdentifierInfoForSlot(i)->getName() << ":";
871 else
872 OS << ":";
873 }
874 else OS << ", "; // Handle variadic methods.
875
Steve Naroffc6814ea2007-10-02 20:01:56 +0000876 PrintExpr(Mess->getArg(i));
877 }
Steve Naroffd54978b2007-09-18 23:55:05 +0000878 }
879 OS << "]";
880}
881
Steve Naroffc540d662008-09-03 18:15:37 +0000882void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
883 OS << "^";
884
885 const FunctionType *AFT = Node->getFunctionType();
886
887 if (isa<FunctionTypeNoProto>(AFT)) {
888 OS << "()";
889 } else if (!Node->arg_empty() || cast<FunctionTypeProto>(AFT)->isVariadic()) {
890 const FunctionTypeProto *FT = cast<FunctionTypeProto>(AFT);
891 OS << '(';
892 std::string ParamStr;
893 for (BlockStmtExpr::arg_iterator AI = Node->arg_begin(),
894 E = Node->arg_end(); AI != E; ++AI) {
895 if (AI != Node->arg_begin()) OS << ", ";
896 ParamStr = (*AI)->getName();
897 (*AI)->getType().getAsStringInternal(ParamStr);
898 OS << ParamStr;
899 }
900
901 if (FT->isVariadic()) {
902 if (!Node->arg_empty()) OS << ", ";
903 OS << "...";
904 }
905 OS << ')';
906 }
907}
908
909void StmtPrinter::VisitBlockStmtExpr(BlockStmtExpr *Node) {
910 VisitBlockExpr(Node);
911 PrintRawCompoundStmt(Node->getBody());
912}
913
914void StmtPrinter::VisitBlockExprExpr(BlockExprExpr *Node) {
915 VisitBlockExpr(Node);
916 PrintExpr(Node->getExpr());
917}
918
919void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
920 OS << Node->getDecl()->getName();
921}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000922//===----------------------------------------------------------------------===//
923// Stmt method implementations
924//===----------------------------------------------------------------------===//
925
Chris Lattnercbe4f772007-08-08 22:51:59 +0000926void Stmt::dumpPretty() const {
Ted Kremenek871422e2007-11-26 22:50:46 +0000927 printPretty(*llvm::cerr.stream());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000928}
929
Ted Kremenek04f3cee2007-08-31 21:30:12 +0000930void Stmt::printPretty(std::ostream &OS, PrinterHelper* Helper) const {
Chris Lattner882f7882006-11-04 18:52:07 +0000931 if (this == 0) {
932 OS << "<NULL>";
933 return;
934 }
935
Ted Kremenek04f3cee2007-08-31 21:30:12 +0000936 StmtPrinter P(OS, Helper);
Chris Lattner62249a62007-08-21 04:04:25 +0000937 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000938}
Ted Kremenek04f3cee2007-08-31 21:30:12 +0000939
940//===----------------------------------------------------------------------===//
941// PrinterHelper
942//===----------------------------------------------------------------------===//
943
944// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +0000945PrinterHelper::~PrinterHelper() {}