blob: e95b8c84096dd34d8ced1535b5adc2771e67b082 [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);
Chris Lattnerc0a38dd2007-06-11 22:26:23 +000053 void PrintRawIfStmt(IfStmt *If);
54
Chris Lattner882f7882006-11-04 18:52:07 +000055 void PrintExpr(Expr *E) {
56 if (E)
Chris Lattner62249a62007-08-21 04:04:25 +000057 Visit(E);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000058 else
Chris Lattner882f7882006-11-04 18:52:07 +000059 OS << "<null expr>";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000060 }
61
Ted Kremenek2d470fc2008-09-13 05:16:45 +000062 llvm::raw_ostream &Indent(int Delta = 0) const {
Chris Lattnerd5322cd2007-05-29 23:49:07 +000063 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000064 OS << " ";
65 return OS;
66 }
67
Chris Lattner98dbf0a2007-08-30 17:59:59 +000068 bool PrintOffsetOfDesignator(Expr *E);
69 void VisitUnaryOffsetOf(UnaryOperator *Node);
70
Ted Kremenek04f3cee2007-08-31 21:30:12 +000071 void Visit(Stmt* S) {
72 if (Helper && Helper->handledStmt(S,OS))
73 return;
74 else StmtVisitor<StmtPrinter>::Visit(S);
75 }
76
Chris Lattner62249a62007-08-21 04:04:25 +000077 void VisitStmt(Stmt *Node);
Steve Naroff7f890eb2007-02-27 02:53:10 +000078#define STMT(N, CLASS, PARENT) \
Chris Lattner62249a62007-08-21 04:04:25 +000079 void Visit##CLASS(CLASS *Node);
Chris Lattnerf2174b62006-11-04 20:59:27 +000080#include "clang/AST/StmtNodes.def"
Chris Lattner71e23ce2006-11-04 20:18:38 +000081 };
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000082}
83
Chris Lattner71e23ce2006-11-04 20:18:38 +000084//===----------------------------------------------------------------------===//
85// Stmt printing methods.
86//===----------------------------------------------------------------------===//
87
Chris Lattner882f7882006-11-04 18:52:07 +000088void StmtPrinter::VisitStmt(Stmt *Node) {
89 Indent() << "<<unknown stmt type>>\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000090}
91
Chris Lattner073926e2007-05-20 23:04:55 +000092/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
93/// with no newline after the }.
94void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
95 OS << "{\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000096 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
Chris Lattner882f7882006-11-04 18:52:07 +000097 I != E; ++I)
98 PrintStmt(*I);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000099
Chris Lattner073926e2007-05-20 23:04:55 +0000100 Indent() << "}";
101}
102
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000103void StmtPrinter::PrintRawDecl(Decl *D) {
104 // FIXME: Need to complete/beautify this... this code simply shows the
Steve Naroff2a8ad182007-05-29 22:59:26 +0000105 // nodes are where they need to be.
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000106 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
107 OS << "typedef " << localType->getUnderlyingType().getAsString();
108 OS << " " << localType->getName();
109 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattnerb4522b42007-06-02 05:27:01 +0000110 // Emit storage class for vardecls.
111 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
112 switch (V->getStorageClass()) {
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000113 default: assert(0 && "Unknown storage class!");
114 case VarDecl::None: break;
115 case VarDecl::Extern: OS << "extern "; break;
116 case VarDecl::Static: OS << "static "; break;
117 case VarDecl::Auto: OS << "auto "; break;
118 case VarDecl::Register: OS << "register "; break;
Chris Lattnerb4522b42007-06-02 05:27:01 +0000119 }
120 }
121
Chris Lattnere0c4ae12007-06-02 03:38:08 +0000122 std::string Name = VD->getName();
123 VD->getType().getAsStringInternal(Name);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000124 OS << Name;
125
Chris Lattner79c57592007-07-12 00:36:32 +0000126 // If this is a vardecl with an initializer, emit it.
127 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
128 if (V->getInit()) {
129 OS << " = ";
130 PrintExpr(V->getInit());
131 }
132 }
Steve Naroffc1e6d602007-11-17 21:21:01 +0000133 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
134 // print a free standing tag decl (e.g. "struct x;").
135 OS << TD->getKindName();
136 OS << " ";
137 if (const IdentifierInfo *II = TD->getIdentifier())
138 OS << II->getName();
139 else
140 OS << "<anonymous>";
141 // FIXME: print tag bodies.
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000142 } else {
Chris Lattnere0c4ae12007-06-02 03:38:08 +0000143 assert(0 && "Unexpected decl");
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000144 }
145}
146
147
148void StmtPrinter::VisitNullStmt(NullStmt *Node) {
149 Indent() << ";\n";
150}
151
152void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Steve Naroffa23cc792007-09-13 23:52:58 +0000153 for (ScopedDecl *D = Node->getDecl(); D; D = D->getNextDeclarator()) {
Chris Lattner776fac82007-06-09 00:53:06 +0000154 Indent();
155 PrintRawDecl(D);
156 OS << ";\n";
157 }
Steve Naroff2a8ad182007-05-29 22:59:26 +0000158}
159
Chris Lattner073926e2007-05-20 23:04:55 +0000160void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
161 Indent();
162 PrintRawCompoundStmt(Node);
Chris Lattnerdf3cafb2007-05-31 05:08:56 +0000163 OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000164}
165
Chris Lattner6c0ff132006-11-05 00:19:50 +0000166void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000167 Indent(-1) << "case ";
Chris Lattner6c0ff132006-11-05 00:19:50 +0000168 PrintExpr(Node->getLHS());
169 if (Node->getRHS()) {
170 OS << " ... ";
171 PrintExpr(Node->getRHS());
172 }
173 OS << ":\n";
174
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000175 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000176}
177
178void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000179 Indent(-1) << "default:\n";
180 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000181}
182
183void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000184 Indent(-1) << Node->getName() << ":\n";
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000185 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000186}
187
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000188void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
189 OS << "if ";
Chris Lattner882f7882006-11-04 18:52:07 +0000190 PrintExpr(If->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000191
192 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
193 OS << ' ';
194 PrintRawCompoundStmt(CS);
195 OS << (If->getElse() ? ' ' : '\n');
196 } else {
197 OS << '\n';
198 PrintStmt(If->getThen());
199 if (If->getElse()) Indent();
200 }
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000201
Chris Lattner073926e2007-05-20 23:04:55 +0000202 if (Stmt *Else = If->getElse()) {
203 OS << "else";
204
205 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
206 OS << ' ';
207 PrintRawCompoundStmt(CS);
208 OS << '\n';
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000209 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
210 OS << ' ';
211 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000212 } else {
213 OS << '\n';
214 PrintStmt(If->getElse());
215 }
Chris Lattner882f7882006-11-04 18:52:07 +0000216 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000217}
218
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000219void StmtPrinter::VisitIfStmt(IfStmt *If) {
220 Indent();
221 PrintRawIfStmt(If);
222}
223
Chris Lattnerf2174b62006-11-04 20:59:27 +0000224void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
225 Indent() << "switch (";
226 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000227 OS << ")";
228
229 // Pretty print compoundstmt bodies (very common).
230 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
231 OS << " ";
232 PrintRawCompoundStmt(CS);
233 OS << "\n";
234 } else {
235 OS << "\n";
236 PrintStmt(Node->getBody());
237 }
Chris Lattnerf2174b62006-11-04 20:59:27 +0000238}
239
Anders Carlsson51873c22007-07-22 07:07:56 +0000240void StmtPrinter::VisitSwitchCase(SwitchCase*) {
241 assert(0 && "SwitchCase is an abstract class");
242}
243
Chris Lattner85ed8732006-11-04 20:40:44 +0000244void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
245 Indent() << "while (";
246 PrintExpr(Node->getCond());
247 OS << ")\n";
248 PrintStmt(Node->getBody());
249}
250
251void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000252 Indent() << "do ";
253 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
254 PrintRawCompoundStmt(CS);
255 OS << " ";
256 } else {
257 OS << "\n";
258 PrintStmt(Node->getBody());
259 Indent();
260 }
261
262 OS << "while ";
Chris Lattner85ed8732006-11-04 20:40:44 +0000263 PrintExpr(Node->getCond());
Chris Lattnera076fde2007-05-31 18:21:33 +0000264 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000265}
266
Chris Lattner71e23ce2006-11-04 20:18:38 +0000267void StmtPrinter::VisitForStmt(ForStmt *Node) {
268 Indent() << "for (";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000269 if (Node->getInit()) {
270 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
271 PrintRawDecl(DS->getDecl());
272 else
273 PrintExpr(cast<Expr>(Node->getInit()));
274 }
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000275 OS << ";";
276 if (Node->getCond()) {
277 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000278 PrintExpr(Node->getCond());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000279 }
280 OS << ";";
281 if (Node->getInc()) {
282 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000283 PrintExpr(Node->getInc());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000284 }
285 OS << ") ";
286
287 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
288 PrintRawCompoundStmt(CS);
289 OS << "\n";
290 } else {
291 OS << "\n";
292 PrintStmt(Node->getBody());
293 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000294}
295
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000296void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian83615522008-01-02 22:54:34 +0000297 Indent() << "for (";
298 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
299 PrintRawDecl(DS->getDecl());
300 else
301 PrintExpr(cast<Expr>(Node->getElement()));
302 OS << " in ";
303 PrintExpr(Node->getCollection());
304 OS << ") ";
305
306 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
307 PrintRawCompoundStmt(CS);
308 OS << "\n";
309 } else {
310 OS << "\n";
311 PrintStmt(Node->getBody());
312 }
313}
314
Chris Lattner16976d32006-11-05 01:46:01 +0000315void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000316 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000317}
318
319void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000320 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000321 PrintExpr(Node->getTarget());
Chris Lattnerb4619482007-05-31 06:00:14 +0000322 OS << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000323}
324
325void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000326 Indent() << "continue;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000327}
328
329void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000330 Indent() << "break;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000331}
332
333
Chris Lattner882f7882006-11-04 18:52:07 +0000334void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
335 Indent() << "return";
336 if (Node->getRetValue()) {
337 OS << " ";
338 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000339 }
Chris Lattnerb4619482007-05-31 06:00:14 +0000340 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000341}
342
Chris Lattner73c56c02007-10-29 04:04:16 +0000343
344void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson660bdd12007-11-23 23:12:25 +0000345 Indent() << "asm ";
346
347 if (Node->isVolatile())
348 OS << "volatile ";
349
350 OS << "(";
Anders Carlsson81a5a692007-11-20 19:21:03 +0000351 VisitStringLiteral(Node->getAsmString());
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000352
353 // Outputs
354 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
355 Node->getNumClobbers() != 0)
356 OS << " : ";
357
358 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
359 if (i != 0)
360 OS << ", ";
361
362 if (!Node->getOutputName(i).empty()) {
363 OS << '[';
364 OS << Node->getOutputName(i);
365 OS << "] ";
366 }
367
368 VisitStringLiteral(Node->getOutputConstraint(i));
369 OS << " ";
370 Visit(Node->getOutputExpr(i));
371 }
372
373 // Inputs
374 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
375 OS << " : ";
376
377 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
378 if (i != 0)
379 OS << ", ";
380
381 if (!Node->getInputName(i).empty()) {
382 OS << '[';
383 OS << Node->getInputName(i);
384 OS << "] ";
385 }
386
387 VisitStringLiteral(Node->getInputConstraint(i));
388 OS << " ";
389 Visit(Node->getInputExpr(i));
390 }
391
392 // Clobbers
393 if (Node->getNumClobbers() != 0)
394 OS << " : ";
395
396 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
397 if (i != 0)
398 OS << ", ";
399
400 VisitStringLiteral(Node->getClobber(i));
401 }
402
Anders Carlsson81a5a692007-11-20 19:21:03 +0000403 OS << ");\n";
Chris Lattner73c56c02007-10-29 04:04:16 +0000404}
405
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000406void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000407 Indent() << "@try";
408 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
409 PrintRawCompoundStmt(TS);
410 OS << "\n";
411 }
412
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000413 for (ObjCAtCatchStmt *catchStmt =
414 static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts());
Fariborz Jahanian88157952007-11-02 18:16:07 +0000415 catchStmt;
416 catchStmt =
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000417 static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000418 Indent() << "@catch(";
419 if (catchStmt->getCatchParamStmt()) {
420 if (DeclStmt *DS = dyn_cast<DeclStmt>(catchStmt->getCatchParamStmt()))
421 PrintRawDecl(DS->getDecl());
422 }
423 OS << ")";
424 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody()))
425 {
426 PrintRawCompoundStmt(CS);
427 OS << "\n";
428 }
429 }
430
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000431 if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>(
Fariborz Jahaniandefbf9a2007-11-07 00:46:42 +0000432 Node->getFinallyStmt())) {
433 Indent() << "@finally";
434 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian88157952007-11-02 18:16:07 +0000435 OS << "\n";
436 }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000437}
438
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000439void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000440}
441
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000442void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000443 Indent() << "@catch (...) { /* todo */ } \n";
444}
445
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000446void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000447 Indent() << "@throw";
448 if (Node->getThrowExpr()) {
449 OS << " ";
450 PrintExpr(Node->getThrowExpr());
451 }
452 OS << ";\n";
453}
454
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000455void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000456 Indent() << "@synchronized (";
457 PrintExpr(Node->getSynchExpr());
458 OS << ")";
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000459 PrintRawCompoundStmt(Node->getSynchBody());
460 OS << "\n";
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000461}
462
Chris Lattner71e23ce2006-11-04 20:18:38 +0000463//===----------------------------------------------------------------------===//
464// Expr printing methods.
465//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000466
Chris Lattner882f7882006-11-04 18:52:07 +0000467void StmtPrinter::VisitExpr(Expr *Node) {
468 OS << "<<unknown expr type>>";
469}
470
471void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Chris Lattner5efbb332006-11-20 05:01:40 +0000472 OS << Node->getDecl()->getName();
Chris Lattner882f7882006-11-04 18:52:07 +0000473}
474
Steve Naroffe46504b2007-11-12 14:29:37 +0000475void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000476 if (Node->getBase()) {
477 PrintExpr(Node->getBase());
478 OS << (Node->isArrow() ? "->" : ".");
479 }
Steve Naroffe46504b2007-11-12 14:29:37 +0000480 OS << Node->getDecl()->getName();
481}
482
Steve Naroffec944032008-05-30 00:40:33 +0000483void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
484 if (Node->getBase()) {
485 PrintExpr(Node->getBase());
486 OS << ".";
487 }
488 // FIXME: OS << Node->getDecl()->getName();
489}
490
Chris Lattner6307f192008-08-10 01:53:14 +0000491void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson625bfc82007-07-21 05:21:51 +0000492 switch (Node->getIdentType()) {
493 default:
494 assert(0 && "unknown case");
Chris Lattner6307f192008-08-10 01:53:14 +0000495 case PredefinedExpr::Func:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000496 OS << "__func__";
497 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000498 case PredefinedExpr::Function:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000499 OS << "__FUNCTION__";
500 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000501 case PredefinedExpr::PrettyFunction:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000502 OS << "__PRETTY_FUNCTION__";
503 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000504 case PredefinedExpr::ObjCSuper:
Chris Lattnera9b3cae2008-06-21 18:04:54 +0000505 OS << "super";
506 break;
Anders Carlsson625bfc82007-07-21 05:21:51 +0000507 }
508}
509
Steve Naroffae4143e2007-04-26 20:39:23 +0000510void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000511 unsigned value = Node->getValue();
Chris Lattnera5678cc2008-06-07 22:35:38 +0000512 if (Node->isWide())
513 OS << "L";
Chris Lattner666115c2007-07-13 23:58:20 +0000514 switch (value) {
515 case '\\':
516 OS << "'\\\\'";
517 break;
518 case '\'':
519 OS << "'\\''";
520 break;
521 case '\a':
522 // TODO: K&R: the meaning of '\\a' is different in traditional C
523 OS << "'\\a'";
524 break;
525 case '\b':
526 OS << "'\\b'";
527 break;
528 // Nonstandard escape sequence.
529 /*case '\e':
530 OS << "'\\e'";
531 break;*/
532 case '\f':
533 OS << "'\\f'";
534 break;
535 case '\n':
536 OS << "'\\n'";
537 break;
538 case '\r':
539 OS << "'\\r'";
540 break;
541 case '\t':
542 OS << "'\\t'";
543 break;
544 case '\v':
545 OS << "'\\v'";
546 break;
547 default:
Ted Kremenek652d18e2008-02-23 00:52:04 +0000548 if (value < 256 && isprint(value)) {
Chris Lattner666115c2007-07-13 23:58:20 +0000549 OS << "'" << (char)value << "'";
550 } else if (value < 256) {
Ted Kremenek2d470fc2008-09-13 05:16:45 +0000551 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner666115c2007-07-13 23:58:20 +0000552 } else {
553 // FIXME what to really do here?
554 OS << value;
555 }
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000556 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000557}
558
Steve Naroffdf7855b2007-02-21 23:46:25 +0000559void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000560 bool isSigned = Node->getType()->isSignedIntegerType();
561 OS << Node->getValue().toString(10, isSigned);
562
563 // Emit suffixes. Integer literals are always a builtin integer type.
Chris Lattner574dee62008-07-26 22:17:49 +0000564 switch (Node->getType()->getAsBuiltinType()->getKind()) {
Chris Lattner06430412007-05-21 05:45:03 +0000565 default: assert(0 && "Unexpected type for integer literal!");
566 case BuiltinType::Int: break; // no suffix.
567 case BuiltinType::UInt: OS << 'U'; break;
568 case BuiltinType::Long: OS << 'L'; break;
569 case BuiltinType::ULong: OS << "UL"; break;
570 case BuiltinType::LongLong: OS << "LL"; break;
571 case BuiltinType::ULongLong: OS << "ULL"; break;
572 }
Chris Lattner882f7882006-11-04 18:52:07 +0000573}
Steve Naroffab624882007-02-21 22:05:47 +0000574void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattnerbf2f3862007-08-01 00:23:58 +0000575 // FIXME: print value more precisely.
Chris Lattnera0173132008-06-07 22:13:43 +0000576 OS << Node->getValueAsApproximateDouble();
Chris Lattner882f7882006-11-04 18:52:07 +0000577}
Chris Lattner1c20a172007-08-26 03:42:43 +0000578
579void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
580 PrintExpr(Node->getSubExpr());
581 OS << "i";
582}
583
Steve Naroffdf7855b2007-02-21 23:46:25 +0000584void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Chris Lattner882f7882006-11-04 18:52:07 +0000585 if (Str->isWide()) OS << 'L';
Chris Lattner5d8f4942006-11-04 20:29:31 +0000586 OS << '"';
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000587
Chris Lattner5d8f4942006-11-04 20:29:31 +0000588 // FIXME: this doesn't print wstrings right.
589 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
590 switch (Str->getStrData()[i]) {
591 default: OS << Str->getStrData()[i]; break;
592 // Handle some common ones to make dumps prettier.
593 case '\\': OS << "\\\\"; break;
594 case '"': OS << "\\\""; break;
595 case '\n': OS << "\\n"; break;
596 case '\t': OS << "\\t"; break;
597 case '\a': OS << "\\a"; break;
598 case '\b': OS << "\\b"; break;
599 }
600 }
601 OS << '"';
Chris Lattner882f7882006-11-04 18:52:07 +0000602}
603void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
604 OS << "(";
605 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000606 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000607}
608void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +0000609 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +0000610 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattnere5b60442007-08-23 21:46:40 +0000611
612 // Print a space if this is an "identifier operator" like sizeof or __real.
613 switch (Node->getOpcode()) {
614 default: break;
615 case UnaryOperator::SizeOf:
616 case UnaryOperator::AlignOf:
617 case UnaryOperator::Real:
618 case UnaryOperator::Imag:
619 case UnaryOperator::Extension:
620 OS << ' ';
621 break;
622 }
623 }
Chris Lattner882f7882006-11-04 18:52:07 +0000624 PrintExpr(Node->getSubExpr());
Chris Lattner15768702006-11-05 23:54:51 +0000625
626 if (Node->isPostfix())
627 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000628}
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000629
630bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
631 if (isa<CompoundLiteralExpr>(E)) {
632 // Base case, print the type and comma.
633 OS << E->getType().getAsString() << ", ";
634 return true;
635 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
636 PrintOffsetOfDesignator(ASE->getLHS());
637 OS << "[";
638 PrintExpr(ASE->getRHS());
639 OS << "]";
640 return false;
641 } else {
642 MemberExpr *ME = cast<MemberExpr>(E);
643 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
644 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getName();
645 return false;
646 }
647}
648
649void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
650 OS << "__builtin_offsetof(";
651 PrintOffsetOfDesignator(Node->getSubExpr());
652 OS << ")";
653}
654
Chris Lattner882f7882006-11-04 18:52:07 +0000655void StmtPrinter::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
Chris Lattner33e8a552006-11-19 01:48:02 +0000656 OS << (Node->isSizeOf() ? "sizeof(" : "__alignof(");
Chris Lattner3dc3d772007-05-16 18:07:12 +0000657 OS << Node->getArgumentType().getAsString() << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000658}
659void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000660 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000661 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +0000662 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000663 OS << "]";
664}
665
666void StmtPrinter::VisitCallExpr(CallExpr *Call) {
667 PrintExpr(Call->getCallee());
668 OS << "(";
669 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000670 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
671 // Don't print any defaulted arguments
672 break;
673 }
674
Chris Lattner882f7882006-11-04 18:52:07 +0000675 if (i) OS << ", ";
676 PrintExpr(Call->getArg(i));
677 }
678 OS << ")";
679}
680void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
681 PrintExpr(Node->getBase());
682 OS << (Node->isArrow() ? "->" : ".");
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000683
Chris Lattner24e0d6c2007-05-24 00:47:01 +0000684 FieldDecl *Field = Node->getMemberDecl();
685 assert(Field && "MemberExpr should alway reference a field!");
686 OS << Field->getName();
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000687}
Nate Begemance4d7fc2008-04-18 23:10:10 +0000688void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +0000689 PrintExpr(Node->getBase());
690 OS << ".";
691 OS << Node->getAccessor().getName();
692}
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +0000693void StmtPrinter::VisitCastExpr(CastExpr *) {
694 assert(0 && "CastExpr is an abstract class");
695}
696void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *Node) {
Chris Lattner51aff8b2007-07-15 23:54:50 +0000697 OS << "(" << Node->getType().getAsString() << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000698 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000699}
Steve Naroff57eb2c52007-07-19 21:32:11 +0000700void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
701 OS << "(" << Node->getType().getAsString() << ")";
702 PrintExpr(Node->getInitializer());
703}
Steve Naroff7a5af782007-07-13 16:58:59 +0000704void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000705 // No need to print anything, simply forward to the sub expression.
706 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +0000707}
Chris Lattner882f7882006-11-04 18:52:07 +0000708void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
709 PrintExpr(Node->getLHS());
710 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
711 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000712}
Chris Lattner86928112007-08-25 02:00:02 +0000713void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
714 PrintExpr(Node->getLHS());
715 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
716 PrintExpr(Node->getRHS());
717}
Chris Lattner882f7882006-11-04 18:52:07 +0000718void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
719 PrintExpr(Node->getCond());
Ted Kremenekebb1c0c2007-11-26 18:27:54 +0000720
721 if (Node->getLHS()) {
722 OS << " ? ";
723 PrintExpr(Node->getLHS());
724 OS << " : ";
725 }
726 else { // Handle GCC extention where LHS can be NULL.
727 OS << " ?: ";
728 }
729
Chris Lattner882f7882006-11-04 18:52:07 +0000730 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000731}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000732
Chris Lattnereefa10e2007-05-28 06:56:27 +0000733// GNU extensions.
734
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000735void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000736 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000737}
738
Chris Lattner366727f2007-07-24 16:58:17 +0000739void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
740 OS << "(";
741 PrintRawCompoundStmt(E->getSubStmt());
742 OS << ")";
743}
744
Steve Naroff78864672007-08-01 22:05:33 +0000745void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
746 OS << "__builtin_types_compatible_p(";
747 OS << Node->getArgType1().getAsString() << ",";
748 OS << Node->getArgType2().getAsString() << ")";
749}
750
Steve Naroff9efdabc2007-08-03 21:21:27 +0000751void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
752 OS << "__builtin_choose_expr(";
753 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +0000754 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000755 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +0000756 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000757 PrintExpr(Node->getRHS());
758 OS << ")";
759}
Chris Lattner366727f2007-07-24 16:58:17 +0000760
Nate Begeman1e36a852008-01-17 17:46:27 +0000761void StmtPrinter::VisitOverloadExpr(OverloadExpr *Node) {
762 OS << "__builtin_overload(";
Nate Begeman936b2072008-01-30 20:50:20 +0000763 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
Nate Begeman1e36a852008-01-17 17:46:27 +0000764 if (i) OS << ", ";
Nate Begeman936b2072008-01-30 20:50:20 +0000765 PrintExpr(Node->getExpr(i));
Nate Begeman1e36a852008-01-17 17:46:27 +0000766 }
767 OS << ")";
768}
769
Eli Friedmana1b4ed82008-05-14 19:38:39 +0000770void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
771 OS << "__builtin_shufflevector(";
772 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
773 if (i) OS << ", ";
774 PrintExpr(Node->getExpr(i));
775 }
776 OS << ")";
777}
778
Anders Carlsson4692db02007-08-31 04:56:16 +0000779void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
780 OS << "{ ";
781 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
782 if (i) OS << ", ";
783 PrintExpr(Node->getInit(i));
784 }
785 OS << " }";
786}
787
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000788void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
789 OS << "va_arg(";
790 PrintExpr(Node->getSubExpr());
791 OS << ", ";
792 OS << Node->getType().getAsString();
793 OS << ")";
794}
795
Chris Lattnereefa10e2007-05-28 06:56:27 +0000796// C++
797
798void StmtPrinter::VisitCXXCastExpr(CXXCastExpr *Node) {
Chris Lattner3f5e4682007-08-09 17:34:19 +0000799 OS << CXXCastExpr::getOpcodeStr(Node->getOpcode()) << '<';
Chris Lattnereefa10e2007-05-28 06:56:27 +0000800 OS << Node->getDestType().getAsString() << ">(";
801 PrintExpr(Node->getSubExpr());
802 OS << ")";
803}
804
805void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
806 OS << (Node->getValue() ? "true" : "false");
807}
808
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000809void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
810 if (Node->getSubExpr() == 0)
811 OS << "throw";
812 else {
813 OS << "throw ";
814 PrintExpr(Node->getSubExpr());
815 }
816}
817
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000818void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
819 // Nothing to print: we picked up the default argument
820}
821
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000822void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
823 OS << Node->getType().getAsString();
824 OS << "(";
825 PrintExpr(Node->getSubExpr());
826 OS << ")";
827}
828
829void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
830 OS << Node->getType().getAsString() << "()";
831}
832
Argyrios Kyrtzidisaa479132008-09-09 23:47:53 +0000833void
834StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
835 PrintRawDecl(E->getVarDecl());
836}
837
Anders Carlsson76f4a902007-08-21 17:43:55 +0000838// Obj-C
839
840void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
841 OS << "@";
842 VisitStringLiteral(Node->getString());
843}
Chris Lattnereefa10e2007-05-28 06:56:27 +0000844
Anders Carlssonc5a81eb2007-08-22 15:14:15 +0000845void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattnerc7f39812007-10-18 00:39:29 +0000846 OS << "@encode(" << Node->getEncodedType().getAsString() << ")";
Anders Carlssonc5a81eb2007-08-22 15:14:15 +0000847}
848
Fariborz Jahanian4bef4622007-10-16 20:40:23 +0000849void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattnerc7f39812007-10-18 00:39:29 +0000850 OS << "@selector(" << Node->getSelector().getName() << ")";
Fariborz Jahanian4bef4622007-10-16 20:40:23 +0000851}
852
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +0000853void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattnerc7f39812007-10-18 00:39:29 +0000854 OS << "@protocol(" << Node->getProtocol()->getName() << ")";
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +0000855}
856
Steve Naroffd54978b2007-09-18 23:55:05 +0000857void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
858 OS << "[";
Steve Naroffc6814ea2007-10-02 20:01:56 +0000859 Expr *receiver = Mess->getReceiver();
860 if (receiver) PrintExpr(receiver);
861 else OS << Mess->getClassName()->getName();
Ted Kremeneka06e7122008-05-02 17:32:38 +0000862 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +0000863 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +0000864 if (selector.isUnarySelector()) {
Ted Kremeneka06e7122008-05-02 17:32:38 +0000865 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroffc6814ea2007-10-02 20:01:56 +0000866 } else {
867 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +0000868 if (i < selector.getNumArgs()) {
869 if (i > 0) OS << ' ';
870 if (selector.getIdentifierInfoForSlot(i))
871 OS << selector.getIdentifierInfoForSlot(i)->getName() << ":";
872 else
873 OS << ":";
874 }
875 else OS << ", "; // Handle variadic methods.
876
Steve Naroffc6814ea2007-10-02 20:01:56 +0000877 PrintExpr(Mess->getArg(i));
878 }
Steve Naroffd54978b2007-09-18 23:55:05 +0000879 }
880 OS << "]";
881}
882
Steve Naroffc540d662008-09-03 18:15:37 +0000883void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
884 OS << "^";
885
886 const FunctionType *AFT = Node->getFunctionType();
887
888 if (isa<FunctionTypeNoProto>(AFT)) {
889 OS << "()";
890 } else if (!Node->arg_empty() || cast<FunctionTypeProto>(AFT)->isVariadic()) {
891 const FunctionTypeProto *FT = cast<FunctionTypeProto>(AFT);
892 OS << '(';
893 std::string ParamStr;
Steve Naroff43bafa72008-09-17 18:37:59 +0000894 for (BlockExpr::arg_iterator AI = Node->arg_begin(),
Steve Naroffc540d662008-09-03 18:15:37 +0000895 E = Node->arg_end(); AI != E; ++AI) {
896 if (AI != Node->arg_begin()) OS << ", ";
897 ParamStr = (*AI)->getName();
898 (*AI)->getType().getAsStringInternal(ParamStr);
899 OS << ParamStr;
900 }
901
902 if (FT->isVariadic()) {
903 if (!Node->arg_empty()) OS << ", ";
904 OS << "...";
905 }
906 OS << ')';
907 }
908}
909
Steve Naroffc540d662008-09-03 18:15:37 +0000910void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
911 OS << Node->getDecl()->getName();
912}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000913//===----------------------------------------------------------------------===//
914// Stmt method implementations
915//===----------------------------------------------------------------------===//
916
Chris Lattnercbe4f772007-08-08 22:51:59 +0000917void Stmt::dumpPretty() const {
Ted Kremenek2d470fc2008-09-13 05:16:45 +0000918 printPretty(llvm::errs());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000919}
920
Ted Kremenek2d470fc2008-09-13 05:16:45 +0000921void Stmt::printPretty(llvm::raw_ostream &OS, PrinterHelper* Helper) const {
Chris Lattner882f7882006-11-04 18:52:07 +0000922 if (this == 0) {
923 OS << "<NULL>";
924 return;
925 }
926
Ted Kremenek04f3cee2007-08-31 21:30:12 +0000927 StmtPrinter P(OS, Helper);
Chris Lattner62249a62007-08-21 04:04:25 +0000928 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000929}
Ted Kremenek04f3cee2007-08-31 21:30:12 +0000930
931//===----------------------------------------------------------------------===//
932// PrinterHelper
933//===----------------------------------------------------------------------===//
934
935// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +0000936PrinterHelper::~PrinterHelper() {}