blob: cb5c44f9269f05550e97838ba6376e96329ad18f [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- StmtPrinter.cpp - Printing implementation for Stmt ASTs ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner95578782007-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 Lattner4b009652007-07-25 00:24:17 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "clang/AST/StmtVisitor.h"
Ted Kremenek10364dd2007-10-17 18:36:42 +000016#include "clang/AST/DeclObjC.h"
Ted Kremenek08176a52007-08-31 21:30:12 +000017#include "clang/AST/PrettyPrinter.h"
Chris Lattner4b009652007-07-25 00:24:17 +000018#include "llvm/Support/Compiler.h"
Ted Kremenekdf409eb2007-11-26 22:50:46 +000019#include "llvm/Support/Streams.h"
Ted Kremenek7b6f67b2008-09-13 05:16:45 +000020#include "llvm/Support/Format.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// StmtPrinter Visitor
25//===----------------------------------------------------------------------===//
26
27namespace {
Chris Lattner7ba21fa2007-08-21 04:04:25 +000028 class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor<StmtPrinter> {
Ted Kremenek7b6f67b2008-09-13 05:16:45 +000029 llvm::raw_ostream &OS;
Chris Lattner4b009652007-07-25 00:24:17 +000030 unsigned IndentLevel;
Ted Kremenek08176a52007-08-31 21:30:12 +000031 clang::PrinterHelper* Helper;
Chris Lattner4b009652007-07-25 00:24:17 +000032 public:
Ted Kremenek7b6f67b2008-09-13 05:16:45 +000033 StmtPrinter(llvm::raw_ostream &os, PrinterHelper* helper) :
Ted Kremenek08176a52007-08-31 21:30:12 +000034 OS(os), IndentLevel(0), Helper(helper) {}
Chris Lattner4b009652007-07-25 00:24:17 +000035
36 void PrintStmt(Stmt *S, int SubIndent = 1) {
37 IndentLevel += SubIndent;
38 if (S && isa<Expr>(S)) {
39 // If this is an expr used in a stmt context, indent and newline it.
40 Indent();
Chris Lattner7ba21fa2007-08-21 04:04:25 +000041 Visit(S);
Chris Lattner4b009652007-07-25 00:24:17 +000042 OS << ";\n";
43 } else if (S) {
Chris Lattner7ba21fa2007-08-21 04:04:25 +000044 Visit(S);
Chris Lattner4b009652007-07-25 00:24:17 +000045 } else {
46 Indent() << "<<<NULL STATEMENT>>>\n";
47 }
48 IndentLevel -= SubIndent;
49 }
50
51 void PrintRawCompoundStmt(CompoundStmt *S);
52 void PrintRawDecl(Decl *D);
Ted Kremenek388b2842008-10-06 18:39:36 +000053 void PrintRawDeclStmt(DeclStmt *S);
Chris Lattner4b009652007-07-25 00:24:17 +000054 void PrintRawIfStmt(IfStmt *If);
55
56 void PrintExpr(Expr *E) {
57 if (E)
Chris Lattner7ba21fa2007-08-21 04:04:25 +000058 Visit(E);
Chris Lattner4b009652007-07-25 00:24:17 +000059 else
60 OS << "<null expr>";
61 }
62
Ted Kremenek7b6f67b2008-09-13 05:16:45 +000063 llvm::raw_ostream &Indent(int Delta = 0) const {
Chris Lattner4b009652007-07-25 00:24:17 +000064 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
65 OS << " ";
66 return OS;
67 }
68
Chris Lattner2af6a802007-08-30 17:59:59 +000069 bool PrintOffsetOfDesignator(Expr *E);
70 void VisitUnaryOffsetOf(UnaryOperator *Node);
71
Ted Kremenek08176a52007-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 Lattner7ba21fa2007-08-21 04:04:25 +000078 void VisitStmt(Stmt *Node);
Douglas Gregor19330152008-11-14 12:46:07 +000079#define STMT(CLASS, PARENT) \
Chris Lattner7ba21fa2007-08-21 04:04:25 +000080 void Visit##CLASS(CLASS *Node);
Chris Lattner4b009652007-07-25 00:24:17 +000081#include "clang/AST/StmtNodes.def"
82 };
83}
84
85//===----------------------------------------------------------------------===//
86// Stmt printing methods.
87//===----------------------------------------------------------------------===//
88
89void StmtPrinter::VisitStmt(Stmt *Node) {
90 Indent() << "<<unknown stmt type>>\n";
91}
92
93/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
94/// with no newline after the }.
95void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
96 OS << "{\n";
97 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
98 I != E; ++I)
99 PrintStmt(*I);
100
101 Indent() << "}";
102}
103
104void StmtPrinter::PrintRawDecl(Decl *D) {
105 // FIXME: Need to complete/beautify this... this code simply shows the
106 // nodes are where they need to be.
107 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
108 OS << "typedef " << localType->getUnderlyingType().getAsString();
109 OS << " " << localType->getName();
110 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
111 // Emit storage class for vardecls.
112 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
113 switch (V->getStorageClass()) {
114 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;
120 }
121 }
122
123 std::string Name = VD->getName();
124 VD->getType().getAsStringInternal(Name);
125 OS << Name;
126
127 // 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 Naroff3340c232007-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 Lattner4b009652007-07-25 00:24:17 +0000143 } else {
Chris Lattner4b009652007-07-25 00:24:17 +0000144 assert(0 && "Unexpected decl");
145 }
146}
147
Ted Kremenek388b2842008-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 Lattner4b009652007-07-25 00:24:17 +0000160
161void StmtPrinter::VisitNullStmt(NullStmt *Node) {
162 Indent() << ";\n";
163}
164
165void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Ted Kremenek388b2842008-10-06 18:39:36 +0000166 for (DeclStmt::decl_iterator I = Node->decl_begin(), E = Node->decl_end();
167 I!=E; ++I) {
Chris Lattner4b009652007-07-25 00:24:17 +0000168 Indent();
Ted Kremenek388b2842008-10-06 18:39:36 +0000169 PrintRawDecl(*I);
Chris Lattner4b009652007-07-25 00:24:17 +0000170 OS << ";\n";
171 }
172}
173
174void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
175 Indent();
176 PrintRawCompoundStmt(Node);
177 OS << "\n";
178}
179
180void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
181 Indent(-1) << "case ";
182 PrintExpr(Node->getLHS());
183 if (Node->getRHS()) {
184 OS << " ... ";
185 PrintExpr(Node->getRHS());
186 }
187 OS << ":\n";
188
189 PrintStmt(Node->getSubStmt(), 0);
190}
191
192void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
193 Indent(-1) << "default:\n";
194 PrintStmt(Node->getSubStmt(), 0);
195}
196
197void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
198 Indent(-1) << Node->getName() << ":\n";
199 PrintStmt(Node->getSubStmt(), 0);
200}
201
202void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
203 OS << "if ";
204 PrintExpr(If->getCond());
205
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 }
215
216 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';
223 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
224 OS << ' ';
225 PrintRawIfStmt(ElseIf);
226 } else {
227 OS << '\n';
228 PrintStmt(If->getElse());
229 }
230 }
231}
232
233void StmtPrinter::VisitIfStmt(IfStmt *If) {
234 Indent();
235 PrintRawIfStmt(If);
236}
237
238void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
239 Indent() << "switch (";
240 PrintExpr(Node->getCond());
241 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 }
252}
253
254void StmtPrinter::VisitSwitchCase(SwitchCase*) {
255 assert(0 && "SwitchCase is an abstract class");
256}
257
258void 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 Lattnercfe0ff02007-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 Lattner4b009652007-07-25 00:24:17 +0000277 PrintExpr(Node->getCond());
278 OS << ";\n";
279}
280
281void StmtPrinter::VisitForStmt(ForStmt *Node) {
282 Indent() << "for (";
283 if (Node->getInit()) {
284 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenek388b2842008-10-06 18:39:36 +0000285 PrintRawDeclStmt(DS);
Chris Lattner4b009652007-07-25 00:24:17 +0000286 else
287 PrintExpr(cast<Expr>(Node->getInit()));
288 }
Chris Lattnercfe0ff02007-09-15 21:49:37 +0000289 OS << ";";
290 if (Node->getCond()) {
291 OS << " ";
Chris Lattner4b009652007-07-25 00:24:17 +0000292 PrintExpr(Node->getCond());
Chris Lattnercfe0ff02007-09-15 21:49:37 +0000293 }
294 OS << ";";
295 if (Node->getInc()) {
296 OS << " ";
Chris Lattner4b009652007-07-25 00:24:17 +0000297 PrintExpr(Node->getInc());
Chris Lattnercfe0ff02007-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 Lattner4b009652007-07-25 00:24:17 +0000308}
309
Ted Kremenek42730c52008-01-07 19:49:32 +0000310void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000311 Indent() << "for (";
312 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenek388b2842008-10-06 18:39:36 +0000313 PrintRawDeclStmt(DS);
Fariborz Jahanian9e920f32008-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 Lattner4b009652007-07-25 00:24:17 +0000329void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
330 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
331}
332
333void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
334 Indent() << "goto *";
335 PrintExpr(Node->getTarget());
336 OS << ";\n";
337}
338
339void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
340 Indent() << "continue;\n";
341}
342
343void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
344 Indent() << "break;\n";
345}
346
347
348void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
349 Indent() << "return";
350 if (Node->getRetValue()) {
351 OS << " ";
352 PrintExpr(Node->getRetValue());
353 }
354 OS << ";\n";
355}
356
Chris Lattner8a40a832007-10-29 04:04:16 +0000357
358void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson759f45d2007-11-23 23:12:25 +0000359 Indent() << "asm ";
360
361 if (Node->isVolatile())
362 OS << "volatile ";
363
364 OS << "(";
Anders Carlsson076c1112007-11-20 19:21:03 +0000365 VisitStringLiteral(Node->getAsmString());
Anders Carlsson965d5202007-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 Carlsson076c1112007-11-20 19:21:03 +0000417 OS << ");\n";
Chris Lattner8a40a832007-10-29 04:04:16 +0000418}
419
Ted Kremenek42730c52008-01-07 19:49:32 +0000420void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian59e9e042007-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 Kremenek42730c52008-01-07 19:49:32 +0000427 for (ObjCAtCatchStmt *catchStmt =
428 static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts());
Fariborz Jahanian59e9e042007-11-02 18:16:07 +0000429 catchStmt;
430 catchStmt =
Ted Kremenek42730c52008-01-07 19:49:32 +0000431 static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
Fariborz Jahanian59e9e042007-11-02 18:16:07 +0000432 Indent() << "@catch(";
433 if (catchStmt->getCatchParamStmt()) {
434 if (DeclStmt *DS = dyn_cast<DeclStmt>(catchStmt->getCatchParamStmt()))
Ted Kremenek388b2842008-10-06 18:39:36 +0000435 PrintRawDeclStmt(DS);
Fariborz Jahanian59e9e042007-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 Kremenek42730c52008-01-07 19:49:32 +0000445 if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>(
Fariborz Jahanian50b47922007-11-07 00:46:42 +0000446 Node->getFinallyStmt())) {
447 Indent() << "@finally";
448 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian59e9e042007-11-02 18:16:07 +0000449 OS << "\n";
450 }
Fariborz Jahanian70952482007-11-01 21:12:44 +0000451}
452
Ted Kremenek42730c52008-01-07 19:49:32 +0000453void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian70952482007-11-01 21:12:44 +0000454}
455
Ted Kremenek42730c52008-01-07 19:49:32 +0000456void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian70952482007-11-01 21:12:44 +0000457 Indent() << "@catch (...) { /* todo */ } \n";
458}
459
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +0000460void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanian08df2c62007-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 Jahanian5f5d6222008-01-30 17:38:29 +0000469void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanian993360a2008-01-29 18:21:32 +0000470 Indent() << "@synchronized (";
471 PrintExpr(Node->getSynchExpr());
472 OS << ")";
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +0000473 PrintRawCompoundStmt(Node->getSynchBody());
474 OS << "\n";
Fariborz Jahanian993360a2008-01-29 18:21:32 +0000475}
476
Chris Lattner4b009652007-07-25 00:24:17 +0000477//===----------------------------------------------------------------------===//
478// Expr printing methods.
479//===----------------------------------------------------------------------===//
480
481void StmtPrinter::VisitExpr(Expr *Node) {
482 OS << "<<unknown expr type>>";
483}
484
485void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
486 OS << Node->getDecl()->getName();
487}
488
Steve Naroff5eb2a4a2007-11-12 14:29:37 +0000489void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian4af72492007-11-12 22:29:28 +0000490 if (Node->getBase()) {
491 PrintExpr(Node->getBase());
492 OS << (Node->isArrow() ? "->" : ".");
493 }
Steve Naroff5eb2a4a2007-11-12 14:29:37 +0000494 OS << Node->getDecl()->getName();
495}
496
Steve Naroff05391d22008-05-30 00:40:33 +0000497void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
498 if (Node->getBase()) {
499 PrintExpr(Node->getBase());
500 OS << ".";
501 }
502 // FIXME: OS << Node->getDecl()->getName();
503}
504
Chris Lattner69909292008-08-10 01:53:14 +0000505void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Chris Lattner4b009652007-07-25 00:24:17 +0000506 switch (Node->getIdentType()) {
507 default:
508 assert(0 && "unknown case");
Chris Lattner69909292008-08-10 01:53:14 +0000509 case PredefinedExpr::Func:
Chris Lattner4b009652007-07-25 00:24:17 +0000510 OS << "__func__";
511 break;
Chris Lattner69909292008-08-10 01:53:14 +0000512 case PredefinedExpr::Function:
Chris Lattner4b009652007-07-25 00:24:17 +0000513 OS << "__FUNCTION__";
514 break;
Chris Lattner69909292008-08-10 01:53:14 +0000515 case PredefinedExpr::PrettyFunction:
Chris Lattner4b009652007-07-25 00:24:17 +0000516 OS << "__PRETTY_FUNCTION__";
517 break;
518 }
519}
520
521void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner4b009652007-07-25 00:24:17 +0000522 unsigned value = Node->getValue();
Chris Lattner1aaf71c2008-06-07 22:35:38 +0000523 if (Node->isWide())
524 OS << "L";
Chris Lattner4b009652007-07-25 00:24:17 +0000525 switch (value) {
526 case '\\':
527 OS << "'\\\\'";
528 break;
529 case '\'':
530 OS << "'\\''";
531 break;
532 case '\a':
533 // TODO: K&R: the meaning of '\\a' is different in traditional C
534 OS << "'\\a'";
535 break;
536 case '\b':
537 OS << "'\\b'";
538 break;
539 // Nonstandard escape sequence.
540 /*case '\e':
541 OS << "'\\e'";
542 break;*/
543 case '\f':
544 OS << "'\\f'";
545 break;
546 case '\n':
547 OS << "'\\n'";
548 break;
549 case '\r':
550 OS << "'\\r'";
551 break;
552 case '\t':
553 OS << "'\\t'";
554 break;
555 case '\v':
556 OS << "'\\v'";
557 break;
558 default:
Ted Kremenekf91bc1d2008-02-23 00:52:04 +0000559 if (value < 256 && isprint(value)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000560 OS << "'" << (char)value << "'";
561 } else if (value < 256) {
Ted Kremenek7b6f67b2008-09-13 05:16:45 +0000562 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner4b009652007-07-25 00:24:17 +0000563 } else {
564 // FIXME what to really do here?
565 OS << value;
566 }
567 }
568}
569
570void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
571 bool isSigned = Node->getType()->isSignedIntegerType();
572 OS << Node->getValue().toString(10, isSigned);
573
574 // Emit suffixes. Integer literals are always a builtin integer type.
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000575 switch (Node->getType()->getAsBuiltinType()->getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000576 default: assert(0 && "Unexpected type for integer literal!");
577 case BuiltinType::Int: break; // no suffix.
578 case BuiltinType::UInt: OS << 'U'; break;
579 case BuiltinType::Long: OS << 'L'; break;
580 case BuiltinType::ULong: OS << "UL"; break;
581 case BuiltinType::LongLong: OS << "LL"; break;
582 case BuiltinType::ULongLong: OS << "ULL"; break;
583 }
584}
585void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner6a390402007-08-01 00:23:58 +0000586 // FIXME: print value more precisely.
Chris Lattnere0391b22008-06-07 22:13:43 +0000587 OS << Node->getValueAsApproximateDouble();
Chris Lattner4b009652007-07-25 00:24:17 +0000588}
Chris Lattner1de66eb2007-08-26 03:42:43 +0000589
590void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
591 PrintExpr(Node->getSubExpr());
592 OS << "i";
593}
594
Chris Lattner4b009652007-07-25 00:24:17 +0000595void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
596 if (Str->isWide()) OS << 'L';
597 OS << '"';
Anders Carlsson55bfe0d2007-10-15 02:50:23 +0000598
Chris Lattner4b009652007-07-25 00:24:17 +0000599 // FIXME: this doesn't print wstrings right.
600 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
601 switch (Str->getStrData()[i]) {
602 default: OS << Str->getStrData()[i]; break;
603 // Handle some common ones to make dumps prettier.
604 case '\\': OS << "\\\\"; break;
605 case '"': OS << "\\\""; break;
606 case '\n': OS << "\\n"; break;
607 case '\t': OS << "\\t"; break;
608 case '\a': OS << "\\a"; break;
609 case '\b': OS << "\\b"; break;
610 }
611 }
612 OS << '"';
613}
614void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
615 OS << "(";
616 PrintExpr(Node->getSubExpr());
617 OS << ")";
618}
619void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner2b97b092007-08-23 21:46:40 +0000620 if (!Node->isPostfix()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000621 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner2b97b092007-08-23 21:46:40 +0000622
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000623 // Print a space if this is an "identifier operator" like __real.
Chris Lattner2b97b092007-08-23 21:46:40 +0000624 switch (Node->getOpcode()) {
625 default: break;
Chris Lattner2b97b092007-08-23 21:46:40 +0000626 case UnaryOperator::Real:
627 case UnaryOperator::Imag:
628 case UnaryOperator::Extension:
629 OS << ' ';
630 break;
631 }
632 }
Chris Lattner4b009652007-07-25 00:24:17 +0000633 PrintExpr(Node->getSubExpr());
634
635 if (Node->isPostfix())
636 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner4b009652007-07-25 00:24:17 +0000637}
Chris Lattner2af6a802007-08-30 17:59:59 +0000638
639bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
640 if (isa<CompoundLiteralExpr>(E)) {
641 // Base case, print the type and comma.
642 OS << E->getType().getAsString() << ", ";
643 return true;
644 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
645 PrintOffsetOfDesignator(ASE->getLHS());
646 OS << "[";
647 PrintExpr(ASE->getRHS());
648 OS << "]";
649 return false;
650 } else {
651 MemberExpr *ME = cast<MemberExpr>(E);
652 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
653 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getName();
654 return false;
655 }
656}
657
658void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
659 OS << "__builtin_offsetof(";
660 PrintOffsetOfDesignator(Node->getSubExpr());
661 OS << ")";
662}
663
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000664void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
665 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
666 if (Node->isArgumentType())
667 OS << "(" << Node->getArgumentType().getAsString() << ")";
668 else {
669 OS << " ";
670 PrintExpr(Node->getArgumentExpr());
671 }
Chris Lattner4b009652007-07-25 00:24:17 +0000672}
673void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000674 PrintExpr(Node->getLHS());
Chris Lattner4b009652007-07-25 00:24:17 +0000675 OS << "[";
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000676 PrintExpr(Node->getRHS());
Chris Lattner4b009652007-07-25 00:24:17 +0000677 OS << "]";
678}
679
680void StmtPrinter::VisitCallExpr(CallExpr *Call) {
681 PrintExpr(Call->getCallee());
682 OS << "(";
683 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattner3e254fb2008-04-08 04:40:51 +0000684 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
685 // Don't print any defaulted arguments
686 break;
687 }
688
Chris Lattner4b009652007-07-25 00:24:17 +0000689 if (i) OS << ", ";
690 PrintExpr(Call->getArg(i));
691 }
692 OS << ")";
693}
694void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
695 PrintExpr(Node->getBase());
696 OS << (Node->isArrow() ? "->" : ".");
697
698 FieldDecl *Field = Node->getMemberDecl();
699 assert(Field && "MemberExpr should alway reference a field!");
700 OS << Field->getName();
701}
Nate Begemanaf6ed502008-04-18 23:10:10 +0000702void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Naroffc11705f2007-07-28 23:10:27 +0000703 PrintExpr(Node->getBase());
704 OS << ".";
705 OS << Node->getAccessor().getName();
706}
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +0000707void StmtPrinter::VisitCastExpr(CastExpr *) {
708 assert(0 && "CastExpr is an abstract class");
709}
Douglas Gregor21a04f32008-10-27 19:41:14 +0000710void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *) {
711 assert(0 && "ExplicitCastExpr is an abstract class");
712}
Douglas Gregor035d0882008-10-28 15:36:24 +0000713void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Chris Lattner4b009652007-07-25 00:24:17 +0000714 OS << "(" << Node->getType().getAsString() << ")";
715 PrintExpr(Node->getSubExpr());
716}
717void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
718 OS << "(" << Node->getType().getAsString() << ")";
719 PrintExpr(Node->getInitializer());
720}
721void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
722 // No need to print anything, simply forward to the sub expression.
723 PrintExpr(Node->getSubExpr());
724}
725void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
726 PrintExpr(Node->getLHS());
727 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
728 PrintExpr(Node->getRHS());
729}
Chris Lattner06078d22007-08-25 02:00:02 +0000730void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
731 PrintExpr(Node->getLHS());
732 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
733 PrintExpr(Node->getRHS());
734}
Chris Lattner4b009652007-07-25 00:24:17 +0000735void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
736 PrintExpr(Node->getCond());
Ted Kremenekeecd9e82007-11-26 18:27:54 +0000737
738 if (Node->getLHS()) {
739 OS << " ? ";
740 PrintExpr(Node->getLHS());
741 OS << " : ";
742 }
743 else { // Handle GCC extention where LHS can be NULL.
744 OS << " ?: ";
745 }
746
Chris Lattner4b009652007-07-25 00:24:17 +0000747 PrintExpr(Node->getRHS());
748}
749
750// GNU extensions.
751
Chris Lattnera0d03a72007-08-03 17:31:20 +0000752void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattner4b009652007-07-25 00:24:17 +0000753 OS << "&&" << Node->getLabel()->getName();
754}
755
756void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
757 OS << "(";
758 PrintRawCompoundStmt(E->getSubStmt());
759 OS << ")";
760}
761
Steve Naroff63bad2d2007-08-01 22:05:33 +0000762void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
763 OS << "__builtin_types_compatible_p(";
764 OS << Node->getArgType1().getAsString() << ",";
765 OS << Node->getArgType2().getAsString() << ")";
766}
767
Steve Naroff93c53012007-08-03 21:21:27 +0000768void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
769 OS << "__builtin_choose_expr(";
770 PrintExpr(Node->getCond());
Chris Lattner44fcf4f2007-08-04 00:20:15 +0000771 OS << ", ";
Steve Naroff93c53012007-08-03 21:21:27 +0000772 PrintExpr(Node->getLHS());
Chris Lattner44fcf4f2007-08-04 00:20:15 +0000773 OS << ", ";
Steve Naroff93c53012007-08-03 21:21:27 +0000774 PrintExpr(Node->getRHS());
775 OS << ")";
776}
Chris Lattner4b009652007-07-25 00:24:17 +0000777
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000778void StmtPrinter::VisitOverloadExpr(OverloadExpr *Node) {
779 OS << "__builtin_overload(";
Nate Begemanbd881ef2008-01-30 20:50:20 +0000780 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000781 if (i) OS << ", ";
Nate Begemanbd881ef2008-01-30 20:50:20 +0000782 PrintExpr(Node->getExpr(i));
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000783 }
784 OS << ")";
785}
786
Eli Friedmand0e9d092008-05-14 19:38:39 +0000787void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
788 OS << "__builtin_shufflevector(";
789 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
790 if (i) OS << ", ";
791 PrintExpr(Node->getExpr(i));
792 }
793 OS << ")";
794}
795
Anders Carlsson762b7c72007-08-31 04:56:16 +0000796void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
797 OS << "{ ";
798 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
799 if (i) OS << ", ";
800 PrintExpr(Node->getInit(i));
801 }
802 OS << " }";
803}
804
Anders Carlsson36760332007-10-15 20:28:48 +0000805void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
806 OS << "va_arg(";
807 PrintExpr(Node->getSubExpr());
808 OS << ", ";
809 OS << Node->getType().getAsString();
810 OS << ")";
811}
812
Chris Lattner4b009652007-07-25 00:24:17 +0000813// C++
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000814void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
815 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
816 "",
817#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
818 Spelling,
819#include "clang/Basic/OperatorKinds.def"
820 };
821
822 OverloadedOperatorKind Kind = Node->getOperator();
823 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
824 if (Node->getNumArgs() == 1) {
825 OS << OpStrings[Kind] << ' ';
826 PrintExpr(Node->getArg(0));
827 } else {
828 PrintExpr(Node->getArg(0));
829 OS << ' ' << OpStrings[Kind];
830 }
831 } else if (Kind == OO_Call) {
832 PrintExpr(Node->getArg(0));
833 OS << '(';
834 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
835 if (ArgIdx > 1)
836 OS << ", ";
837 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
838 PrintExpr(Node->getArg(ArgIdx));
839 }
840 OS << ')';
841 } else if (Kind == OO_Subscript) {
842 PrintExpr(Node->getArg(0));
843 OS << '[';
844 PrintExpr(Node->getArg(1));
845 OS << ']';
846 } else if (Node->getNumArgs() == 1) {
847 OS << OpStrings[Kind] << ' ';
848 PrintExpr(Node->getArg(0));
849 } else if (Node->getNumArgs() == 2) {
850 PrintExpr(Node->getArg(0));
851 OS << ' ' << OpStrings[Kind] << ' ';
852 PrintExpr(Node->getArg(1));
853 } else {
854 assert(false && "unknown overloaded operator");
855 }
856}
Chris Lattner4b009652007-07-25 00:24:17 +0000857
Douglas Gregor21a04f32008-10-27 19:41:14 +0000858void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
859 OS << Node->getCastName() << '<';
860 OS << Node->getTypeAsWritten().getAsString() << ">(";
Chris Lattner4b009652007-07-25 00:24:17 +0000861 PrintExpr(Node->getSubExpr());
862 OS << ")";
863}
864
Douglas Gregor21a04f32008-10-27 19:41:14 +0000865void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
866 VisitCXXNamedCastExpr(Node);
867}
868
869void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
870 VisitCXXNamedCastExpr(Node);
871}
872
873void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
874 VisitCXXNamedCastExpr(Node);
875}
876
877void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
878 VisitCXXNamedCastExpr(Node);
879}
880
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000881void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
882 OS << "typeid(";
883 if (Node->isTypeOperand()) {
884 OS << Node->getTypeOperand().getAsString();
885 } else {
886 PrintExpr(Node->getExprOperand());
887 }
888 OS << ")";
889}
890
Chris Lattner4b009652007-07-25 00:24:17 +0000891void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
892 OS << (Node->getValue() ? "true" : "false");
893}
894
Douglas Gregora5b022a2008-11-04 14:32:21 +0000895void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
896 OS << "this";
897}
898
Chris Lattnera7447ba2008-02-26 00:51:44 +0000899void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
900 if (Node->getSubExpr() == 0)
901 OS << "throw";
902 else {
903 OS << "throw ";
904 PrintExpr(Node->getSubExpr());
905 }
906}
907
Chris Lattner3e254fb2008-04-08 04:40:51 +0000908void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
909 // Nothing to print: we picked up the default argument
910}
911
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000912void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
913 OS << Node->getType().getAsString();
914 OS << "(";
915 PrintExpr(Node->getSubExpr());
916 OS << ")";
917}
918
919void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
920 OS << Node->getType().getAsString() << "()";
921}
922
Argiris Kirtzidisdbce6c12008-09-09 23:47:53 +0000923void
924StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
925 PrintRawDecl(E->getVarDecl());
926}
927
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000928void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
929 if (E->isGlobalNew())
930 OS << "::";
931 OS << "new ";
932 unsigned NumPlace = E->getNumPlacementArgs();
933 if (NumPlace > 0) {
934 OS << "(";
935 PrintExpr(E->getPlacementArg(0));
936 for (unsigned i = 1; i < NumPlace; ++i) {
937 OS << ", ";
938 PrintExpr(E->getPlacementArg(i));
939 }
940 OS << ") ";
941 }
942 if (E->isParenTypeId())
943 OS << "(";
944 OS << E->getAllocatedType().getAsString();
945 if (E->isParenTypeId())
946 OS << ")";
947
948 if (E->hasInitializer()) {
949 OS << "(";
950 unsigned NumCons = E->getNumConstructorArgs();
951 if (NumCons > 0) {
952 PrintExpr(E->getConstructorArg(0));
953 for (unsigned i = 1; i < NumCons; ++i) {
954 OS << ", ";
955 PrintExpr(E->getConstructorArg(i));
956 }
957 }
958 OS << ")";
959 }
960}
961
962void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
963 if (E->isGlobalDelete())
964 OS << "::";
965 OS << "delete ";
966 if (E->isArrayForm())
967 OS << "[] ";
968 PrintExpr(E->getArgument());
969}
970
Anders Carlssona66cad42007-08-21 17:43:55 +0000971// Obj-C
972
973void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
974 OS << "@";
975 VisitStringLiteral(Node->getString());
976}
Chris Lattner4b009652007-07-25 00:24:17 +0000977
Anders Carlsson8be1d402007-08-22 15:14:15 +0000978void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattnerc17ccab2007-10-18 00:39:29 +0000979 OS << "@encode(" << Node->getEncodedType().getAsString() << ")";
Anders Carlsson8be1d402007-08-22 15:14:15 +0000980}
981
Fariborz Jahanianf807c202007-10-16 20:40:23 +0000982void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattnerc17ccab2007-10-18 00:39:29 +0000983 OS << "@selector(" << Node->getSelector().getName() << ")";
Fariborz Jahanianf807c202007-10-16 20:40:23 +0000984}
985
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +0000986void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattnerc17ccab2007-10-18 00:39:29 +0000987 OS << "@protocol(" << Node->getProtocol()->getName() << ")";
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +0000988}
989
Steve Naroffc39ca262007-09-18 23:55:05 +0000990void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
991 OS << "[";
Steve Narofffa465d12007-10-02 20:01:56 +0000992 Expr *receiver = Mess->getReceiver();
993 if (receiver) PrintExpr(receiver);
994 else OS << Mess->getClassName()->getName();
Ted Kremenekbfec9492008-05-02 17:32:38 +0000995 OS << ' ';
Ted Kremenek2287cee2008-04-16 04:30:16 +0000996 Selector selector = Mess->getSelector();
Steve Narofffa465d12007-10-02 20:01:56 +0000997 if (selector.isUnarySelector()) {
Ted Kremenekbfec9492008-05-02 17:32:38 +0000998 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Narofffa465d12007-10-02 20:01:56 +0000999 } else {
1000 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremenekbfec9492008-05-02 17:32:38 +00001001 if (i < selector.getNumArgs()) {
1002 if (i > 0) OS << ' ';
1003 if (selector.getIdentifierInfoForSlot(i))
1004 OS << selector.getIdentifierInfoForSlot(i)->getName() << ":";
1005 else
1006 OS << ":";
1007 }
1008 else OS << ", "; // Handle variadic methods.
1009
Steve Narofffa465d12007-10-02 20:01:56 +00001010 PrintExpr(Mess->getArg(i));
1011 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001012 }
1013 OS << "]";
1014}
1015
Douglas Gregord8606632008-11-04 14:56:14 +00001016void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) {
1017 OS << "super";
1018}
1019
Steve Naroff52a81c02008-09-03 18:15:37 +00001020void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff9ac456d2008-10-08 17:01:13 +00001021 BlockDecl *BD = Node->getBlockDecl();
Steve Naroff52a81c02008-09-03 18:15:37 +00001022 OS << "^";
1023
1024 const FunctionType *AFT = Node->getFunctionType();
1025
1026 if (isa<FunctionTypeNoProto>(AFT)) {
1027 OS << "()";
Steve Naroff9ac456d2008-10-08 17:01:13 +00001028 } else if (!BD->param_empty() || cast<FunctionTypeProto>(AFT)->isVariadic()) {
Steve Naroff52a81c02008-09-03 18:15:37 +00001029 OS << '(';
1030 std::string ParamStr;
Steve Naroff9ac456d2008-10-08 17:01:13 +00001031 for (BlockDecl::param_iterator AI = BD->param_begin(),
1032 E = BD->param_end(); AI != E; ++AI) {
1033 if (AI != BD->param_begin()) OS << ", ";
Steve Naroff52a81c02008-09-03 18:15:37 +00001034 ParamStr = (*AI)->getName();
1035 (*AI)->getType().getAsStringInternal(ParamStr);
1036 OS << ParamStr;
1037 }
1038
Steve Naroff9ac456d2008-10-08 17:01:13 +00001039 const FunctionTypeProto *FT = cast<FunctionTypeProto>(AFT);
Steve Naroff52a81c02008-09-03 18:15:37 +00001040 if (FT->isVariadic()) {
Steve Naroff9ac456d2008-10-08 17:01:13 +00001041 if (!BD->param_empty()) OS << ", ";
Steve Naroff52a81c02008-09-03 18:15:37 +00001042 OS << "...";
1043 }
1044 OS << ')';
1045 }
1046}
1047
Steve Naroff52a81c02008-09-03 18:15:37 +00001048void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
1049 OS << Node->getDecl()->getName();
1050}
Chris Lattner4b009652007-07-25 00:24:17 +00001051//===----------------------------------------------------------------------===//
1052// Stmt method implementations
1053//===----------------------------------------------------------------------===//
1054
Chris Lattner95578782007-08-08 22:51:59 +00001055void Stmt::dumpPretty() const {
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001056 printPretty(llvm::errs());
Chris Lattner4b009652007-07-25 00:24:17 +00001057}
1058
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001059void Stmt::printPretty(llvm::raw_ostream &OS, PrinterHelper* Helper) const {
Chris Lattner4b009652007-07-25 00:24:17 +00001060 if (this == 0) {
1061 OS << "<NULL>";
1062 return;
1063 }
1064
Ted Kremenek08176a52007-08-31 21:30:12 +00001065 StmtPrinter P(OS, Helper);
Chris Lattner7ba21fa2007-08-21 04:04:25 +00001066 P.Visit(const_cast<Stmt*>(this));
Chris Lattner4b009652007-07-25 00:24:17 +00001067}
Ted Kremenek08176a52007-08-31 21:30:12 +00001068
1069//===----------------------------------------------------------------------===//
1070// PrinterHelper
1071//===----------------------------------------------------------------------===//
1072
1073// Implement virtual destructor.
Gabor Greif61ce98c2007-09-11 15:32:40 +00001074PrinterHelper::~PrinterHelper() {}