blob: 1a74937be94e70ffe5b10cfd545677ca143bd02e [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- StmtPrinter.cpp - Printing implementation for Stmt ASTs ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner6000dac2007-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.
Reid Spencer5f016e22007-07-11 17:01:13 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "clang/AST/StmtVisitor.h"
Douglas Gregor1a49af92009-01-06 05:10:23 +000016#include "clang/AST/DeclCXX.h"
Ted Kremenek91d1d7a2007-10-17 18:36:42 +000017#include "clang/AST/DeclObjC.h"
Ted Kremenek42a509f2007-08-31 21:30:12 +000018#include "clang/AST/PrettyPrinter.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "llvm/Support/Compiler.h"
Ted Kremenek51221ec2007-11-26 22:50:46 +000020#include "llvm/Support/Streams.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000021#include "llvm/Support/Format.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// StmtPrinter Visitor
26//===----------------------------------------------------------------------===//
27
28namespace {
Chris Lattnerc5598cb2007-08-21 04:04:25 +000029 class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor<StmtPrinter> {
Ted Kremeneka95d3752008-09-13 05:16:45 +000030 llvm::raw_ostream &OS;
Reid Spencer5f016e22007-07-11 17:01:13 +000031 unsigned IndentLevel;
Mike Stump071e4da2009-02-10 20:16:46 +000032 bool NoIndent;
Ted Kremenek42a509f2007-08-31 21:30:12 +000033 clang::PrinterHelper* Helper;
Reid Spencer5f016e22007-07-11 17:01:13 +000034 public:
Mike Stump071e4da2009-02-10 20:16:46 +000035 StmtPrinter(llvm::raw_ostream &os, PrinterHelper* helper, unsigned I=0,
36 bool noIndent=false) :
37 OS(os), IndentLevel(I), NoIndent(noIndent), Helper(helper) {}
Reid Spencer5f016e22007-07-11 17:01:13 +000038
39 void PrintStmt(Stmt *S, int SubIndent = 1) {
40 IndentLevel += SubIndent;
41 if (S && isa<Expr>(S)) {
42 // If this is an expr used in a stmt context, indent and newline it.
43 Indent();
Chris Lattnerc5598cb2007-08-21 04:04:25 +000044 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000045 OS << ";\n";
46 } else if (S) {
Chris Lattnerc5598cb2007-08-21 04:04:25 +000047 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000048 } else {
49 Indent() << "<<<NULL STATEMENT>>>\n";
50 }
51 IndentLevel -= SubIndent;
52 }
53
54 void PrintRawCompoundStmt(CompoundStmt *S);
55 void PrintRawDecl(Decl *D);
Ted Kremenekecd64c52008-10-06 18:39:36 +000056 void PrintRawDeclStmt(DeclStmt *S);
Mike Stump071e4da2009-02-10 20:16:46 +000057 void PrintFieldDecl(FieldDecl *FD);
Reid Spencer5f016e22007-07-11 17:01:13 +000058 void PrintRawIfStmt(IfStmt *If);
Sebastian Redl8351da02008-12-22 21:35:02 +000059 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
Reid Spencer5f016e22007-07-11 17:01:13 +000060
61 void PrintExpr(Expr *E) {
62 if (E)
Chris Lattnerc5598cb2007-08-21 04:04:25 +000063 Visit(E);
Reid Spencer5f016e22007-07-11 17:01:13 +000064 else
65 OS << "<null expr>";
66 }
67
Mike Stump071e4da2009-02-10 20:16:46 +000068 llvm::raw_ostream &Indent(int Delta = 0) {
69 if (!NoIndent) {
70 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
71 OS << " ";
72 } else NoIndent = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000073 return OS;
74 }
75
Chris Lattner704fe352007-08-30 17:59:59 +000076 bool PrintOffsetOfDesignator(Expr *E);
77 void VisitUnaryOffsetOf(UnaryOperator *Node);
78
Ted Kremenek42a509f2007-08-31 21:30:12 +000079 void Visit(Stmt* S) {
80 if (Helper && Helper->handledStmt(S,OS))
81 return;
82 else StmtVisitor<StmtPrinter>::Visit(S);
83 }
84
Chris Lattnerc5598cb2007-08-21 04:04:25 +000085 void VisitStmt(Stmt *Node);
Douglas Gregorf2cad862008-11-14 12:46:07 +000086#define STMT(CLASS, PARENT) \
Chris Lattnerc5598cb2007-08-21 04:04:25 +000087 void Visit##CLASS(CLASS *Node);
Reid Spencer5f016e22007-07-11 17:01:13 +000088#include "clang/AST/StmtNodes.def"
89 };
90}
91
92//===----------------------------------------------------------------------===//
93// Stmt printing methods.
94//===----------------------------------------------------------------------===//
95
96void StmtPrinter::VisitStmt(Stmt *Node) {
97 Indent() << "<<unknown stmt type>>\n";
98}
99
100/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
101/// with no newline after the }.
102void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
103 OS << "{\n";
104 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
105 I != E; ++I)
106 PrintStmt(*I);
107
108 Indent() << "}";
109}
110
111void StmtPrinter::PrintRawDecl(Decl *D) {
112 // FIXME: Need to complete/beautify this... this code simply shows the
113 // nodes are where they need to be.
114 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
115 OS << "typedef " << localType->getUnderlyingType().getAsString();
Chris Lattner39f34e92008-11-24 04:00:27 +0000116 OS << " " << localType->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
118 // Emit storage class for vardecls.
119 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +0000120 if (V->getStorageClass() != VarDecl::None)
121 OS << VarDecl::getStorageClassSpecifierString(V->getStorageClass())
122 << ' ';
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 }
124
Chris Lattner39f34e92008-11-24 04:00:27 +0000125 std::string Name = VD->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 VD->getType().getAsStringInternal(Name);
127 OS << Name;
128
Chris Lattner24c39902007-07-12 00:36:32 +0000129 // If this is a vardecl with an initializer, emit it.
130 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
131 if (V->getInit()) {
132 OS << " = ";
133 PrintExpr(V->getInit());
134 }
135 }
Steve Naroff91578f32007-11-17 21:21:01 +0000136 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
137 // print a free standing tag decl (e.g. "struct x;").
138 OS << TD->getKindName();
139 OS << " ";
140 if (const IdentifierInfo *II = TD->getIdentifier())
141 OS << II->getName();
Mike Stump071e4da2009-02-10 20:16:46 +0000142 if (RecordDecl *RD = dyn_cast<RecordDecl>(TD)) {
143 OS << "{\n";
144 IndentLevel += 1;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000145 // FIXME: The context passed to field_begin/field_end should
146 // never be NULL!
147 ASTContext *Context = 0;
148 for (RecordDecl::field_iterator i = RD->field_begin(*Context);
149 i != RD->field_end(*Context); ++i) {
Mike Stump071e4da2009-02-10 20:16:46 +0000150 PrintFieldDecl(*i);
151 IndentLevel -= 1;
152 }
153 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 } else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000155 assert(0 && "Unexpected decl");
156 }
157}
158
Mike Stump071e4da2009-02-10 20:16:46 +0000159void StmtPrinter::PrintFieldDecl(FieldDecl *FD) {
160 Indent() << FD->getNameAsString() << "\n";
161}
162
Ted Kremenekecd64c52008-10-06 18:39:36 +0000163void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
Mike Stump071e4da2009-02-10 20:16:46 +0000164 bool isFirst = true;
Ted Kremenekecd64c52008-10-06 18:39:36 +0000165
166 for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();
167 I != E; ++I) {
168
169 if (!isFirst) OS << ", ";
170 else isFirst = false;
171
172 PrintRawDecl(*I);
173 }
174}
Reid Spencer5f016e22007-07-11 17:01:13 +0000175
176void StmtPrinter::VisitNullStmt(NullStmt *Node) {
177 Indent() << ";\n";
178}
179
180void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Ted Kremenekecd64c52008-10-06 18:39:36 +0000181 for (DeclStmt::decl_iterator I = Node->decl_begin(), E = Node->decl_end();
182 I!=E; ++I) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000183 Indent();
Ted Kremenekecd64c52008-10-06 18:39:36 +0000184 PrintRawDecl(*I);
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 OS << ";\n";
186 }
187}
188
189void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
190 Indent();
191 PrintRawCompoundStmt(Node);
192 OS << "\n";
193}
194
195void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
196 Indent(-1) << "case ";
197 PrintExpr(Node->getLHS());
198 if (Node->getRHS()) {
199 OS << " ... ";
200 PrintExpr(Node->getRHS());
201 }
202 OS << ":\n";
203
204 PrintStmt(Node->getSubStmt(), 0);
205}
206
207void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
208 Indent(-1) << "default:\n";
209 PrintStmt(Node->getSubStmt(), 0);
210}
211
212void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
213 Indent(-1) << Node->getName() << ":\n";
214 PrintStmt(Node->getSubStmt(), 0);
215}
216
217void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000218 OS << "if (";
Reid Spencer5f016e22007-07-11 17:01:13 +0000219 PrintExpr(If->getCond());
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000220 OS << ')';
Reid Spencer5f016e22007-07-11 17:01:13 +0000221
222 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
223 OS << ' ';
224 PrintRawCompoundStmt(CS);
225 OS << (If->getElse() ? ' ' : '\n');
226 } else {
227 OS << '\n';
228 PrintStmt(If->getThen());
229 if (If->getElse()) Indent();
230 }
231
232 if (Stmt *Else = If->getElse()) {
233 OS << "else";
234
235 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
236 OS << ' ';
237 PrintRawCompoundStmt(CS);
238 OS << '\n';
239 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
240 OS << ' ';
241 PrintRawIfStmt(ElseIf);
242 } else {
243 OS << '\n';
244 PrintStmt(If->getElse());
245 }
246 }
247}
248
249void StmtPrinter::VisitIfStmt(IfStmt *If) {
250 Indent();
251 PrintRawIfStmt(If);
252}
253
254void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
255 Indent() << "switch (";
256 PrintExpr(Node->getCond());
257 OS << ")";
258
259 // Pretty print compoundstmt bodies (very common).
260 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
261 OS << " ";
262 PrintRawCompoundStmt(CS);
263 OS << "\n";
264 } else {
265 OS << "\n";
266 PrintStmt(Node->getBody());
267 }
268}
269
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000270void StmtPrinter::VisitSwitchCase(SwitchCase*) {
271 assert(0 && "SwitchCase is an abstract class");
272}
273
Reid Spencer5f016e22007-07-11 17:01:13 +0000274void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
275 Indent() << "while (";
276 PrintExpr(Node->getCond());
277 OS << ")\n";
278 PrintStmt(Node->getBody());
279}
280
281void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner8bdcc472007-09-15 21:49:37 +0000282 Indent() << "do ";
283 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
284 PrintRawCompoundStmt(CS);
285 OS << " ";
286 } else {
287 OS << "\n";
288 PrintStmt(Node->getBody());
289 Indent();
290 }
291
292 OS << "while ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000293 PrintExpr(Node->getCond());
294 OS << ";\n";
295}
296
297void StmtPrinter::VisitForStmt(ForStmt *Node) {
298 Indent() << "for (";
299 if (Node->getInit()) {
300 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000301 PrintRawDeclStmt(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000302 else
303 PrintExpr(cast<Expr>(Node->getInit()));
304 }
Chris Lattner8bdcc472007-09-15 21:49:37 +0000305 OS << ";";
306 if (Node->getCond()) {
307 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000308 PrintExpr(Node->getCond());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000309 }
310 OS << ";";
311 if (Node->getInc()) {
312 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000313 PrintExpr(Node->getInc());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000314 }
315 OS << ") ";
316
317 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
318 PrintRawCompoundStmt(CS);
319 OS << "\n";
320 } else {
321 OS << "\n";
322 PrintStmt(Node->getBody());
323 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000324}
325
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000326void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000327 Indent() << "for (";
328 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000329 PrintRawDeclStmt(DS);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000330 else
331 PrintExpr(cast<Expr>(Node->getElement()));
332 OS << " in ";
333 PrintExpr(Node->getCollection());
334 OS << ") ";
335
336 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
337 PrintRawCompoundStmt(CS);
338 OS << "\n";
339 } else {
340 OS << "\n";
341 PrintStmt(Node->getBody());
342 }
343}
344
Reid Spencer5f016e22007-07-11 17:01:13 +0000345void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
346 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
347}
348
349void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
350 Indent() << "goto *";
351 PrintExpr(Node->getTarget());
352 OS << ";\n";
353}
354
355void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
356 Indent() << "continue;\n";
357}
358
359void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
360 Indent() << "break;\n";
361}
362
363
364void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
365 Indent() << "return";
366 if (Node->getRetValue()) {
367 OS << " ";
368 PrintExpr(Node->getRetValue());
369 }
370 OS << ";\n";
371}
372
Chris Lattnerfe795952007-10-29 04:04:16 +0000373
374void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson39c47b52007-11-23 23:12:25 +0000375 Indent() << "asm ";
376
377 if (Node->isVolatile())
378 OS << "volatile ";
379
380 OS << "(";
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000381 VisitStringLiteral(Node->getAsmString());
Anders Carlssonb235fc22007-11-22 01:36:19 +0000382
383 // Outputs
384 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
385 Node->getNumClobbers() != 0)
386 OS << " : ";
387
388 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
389 if (i != 0)
390 OS << ", ";
391
392 if (!Node->getOutputName(i).empty()) {
393 OS << '[';
394 OS << Node->getOutputName(i);
395 OS << "] ";
396 }
397
Chris Lattnerb3277932009-03-10 04:59:06 +0000398 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000399 OS << " ";
400 Visit(Node->getOutputExpr(i));
401 }
402
403 // Inputs
404 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
405 OS << " : ";
406
407 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
408 if (i != 0)
409 OS << ", ";
410
411 if (!Node->getInputName(i).empty()) {
412 OS << '[';
413 OS << Node->getInputName(i);
414 OS << "] ";
415 }
416
Chris Lattnerb3277932009-03-10 04:59:06 +0000417 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000418 OS << " ";
419 Visit(Node->getInputExpr(i));
420 }
421
422 // Clobbers
423 if (Node->getNumClobbers() != 0)
424 OS << " : ";
425
426 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
427 if (i != 0)
428 OS << ", ";
429
430 VisitStringLiteral(Node->getClobber(i));
431 }
432
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000433 OS << ");\n";
Chris Lattnerfe795952007-10-29 04:04:16 +0000434}
435
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000436void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000437 Indent() << "@try";
438 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
439 PrintRawCompoundStmt(TS);
440 OS << "\n";
441 }
442
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000443 for (ObjCAtCatchStmt *catchStmt =
444 static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts());
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000445 catchStmt;
446 catchStmt =
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000447 static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000448 Indent() << "@catch(";
Steve Naroff7ba138a2009-03-03 19:52:17 +0000449 if (catchStmt->getCatchParamDecl()) {
450 if (Decl *DS = catchStmt->getCatchParamDecl())
451 PrintRawDecl(DS);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000452 }
453 OS << ")";
454 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody()))
455 {
456 PrintRawCompoundStmt(CS);
457 OS << "\n";
458 }
459 }
460
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000461 if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>(
Fariborz Jahanian1e7eab42007-11-07 00:46:42 +0000462 Node->getFinallyStmt())) {
463 Indent() << "@finally";
464 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000465 OS << "\n";
466 }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000467}
468
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000469void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000470}
471
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000472void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000473 Indent() << "@catch (...) { /* todo */ } \n";
474}
475
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000476void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000477 Indent() << "@throw";
478 if (Node->getThrowExpr()) {
479 OS << " ";
480 PrintExpr(Node->getThrowExpr());
481 }
482 OS << ";\n";
483}
484
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000485void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000486 Indent() << "@synchronized (";
487 PrintExpr(Node->getSynchExpr());
488 OS << ")";
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000489 PrintRawCompoundStmt(Node->getSynchBody());
490 OS << "\n";
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000491}
492
Sebastian Redl8351da02008-12-22 21:35:02 +0000493void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
494 OS << "catch (";
Sebastian Redl4b07b292008-12-22 19:15:10 +0000495 if (Decl *ExDecl = Node->getExceptionDecl())
496 PrintRawDecl(ExDecl);
497 else
498 OS << "...";
499 OS << ") ";
500 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl8351da02008-12-22 21:35:02 +0000501}
502
503void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
504 Indent();
505 PrintRawCXXCatchStmt(Node);
506 OS << "\n";
507}
508
509void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
510 Indent() << "try ";
511 PrintRawCompoundStmt(Node->getTryBlock());
512 for(unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
513 OS << " ";
514 PrintRawCXXCatchStmt(Node->getHandler(i));
515 }
Sebastian Redl4b07b292008-12-22 19:15:10 +0000516 OS << "\n";
517}
518
Reid Spencer5f016e22007-07-11 17:01:13 +0000519//===----------------------------------------------------------------------===//
520// Expr printing methods.
521//===----------------------------------------------------------------------===//
522
523void StmtPrinter::VisitExpr(Expr *Node) {
524 OS << "<<unknown expr type>>";
525}
526
527void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +0000528 OS << Node->getDecl()->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000529}
530
Douglas Gregorbad35182009-03-19 03:51:16 +0000531void StmtPrinter::VisitQualifiedDeclRefExpr(QualifiedDeclRefExpr *Node) {
Douglas Gregor1a49af92009-01-06 05:10:23 +0000532 NamedDecl *D = Node->getDecl();
533
Douglas Gregor9bde7732009-03-31 20:22:05 +0000534 Node->getQualifier()->print(OS);
Douglas Gregor1a49af92009-01-06 05:10:23 +0000535 OS << D->getNameAsString();
536}
537
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000538void StmtPrinter::VisitUnresolvedDeclRefExpr(UnresolvedDeclRefExpr *Node) {
Douglas Gregor9bde7732009-03-31 20:22:05 +0000539 Node->getQualifier()->print(OS);
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000540 OS << Node->getDeclName().getAsString();
541}
542
Steve Naroff7779db42007-11-12 14:29:37 +0000543void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000544 if (Node->getBase()) {
545 PrintExpr(Node->getBase());
546 OS << (Node->isArrow() ? "->" : ".");
547 }
Chris Lattner39f34e92008-11-24 04:00:27 +0000548 OS << Node->getDecl()->getNameAsString();
Steve Naroff7779db42007-11-12 14:29:37 +0000549}
550
Steve Naroffae784072008-05-30 00:40:33 +0000551void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
552 if (Node->getBase()) {
553 PrintExpr(Node->getBase());
554 OS << ".";
555 }
Steve Naroffc77a6362008-12-04 16:24:46 +0000556 OS << Node->getProperty()->getNameAsCString();
Steve Naroffae784072008-05-30 00:40:33 +0000557}
558
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000559void StmtPrinter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node) {
560 if (Node->getBase()) {
561 PrintExpr(Node->getBase());
562 OS << ".";
563 }
564 // FIXME: Setter/Getter names
565}
566
Chris Lattnerd9f69102008-08-10 01:53:14 +0000567void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson22742662007-07-21 05:21:51 +0000568 switch (Node->getIdentType()) {
569 default:
570 assert(0 && "unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000571 case PredefinedExpr::Func:
Anders Carlsson22742662007-07-21 05:21:51 +0000572 OS << "__func__";
573 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000574 case PredefinedExpr::Function:
Anders Carlsson22742662007-07-21 05:21:51 +0000575 OS << "__FUNCTION__";
576 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000577 case PredefinedExpr::PrettyFunction:
Anders Carlsson22742662007-07-21 05:21:51 +0000578 OS << "__PRETTY_FUNCTION__";
579 break;
580 }
581}
582
Reid Spencer5f016e22007-07-11 17:01:13 +0000583void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000584 unsigned value = Node->getValue();
Chris Lattnerc250aae2008-06-07 22:35:38 +0000585 if (Node->isWide())
586 OS << "L";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000587 switch (value) {
588 case '\\':
589 OS << "'\\\\'";
590 break;
591 case '\'':
592 OS << "'\\''";
593 break;
594 case '\a':
595 // TODO: K&R: the meaning of '\\a' is different in traditional C
596 OS << "'\\a'";
597 break;
598 case '\b':
599 OS << "'\\b'";
600 break;
601 // Nonstandard escape sequence.
602 /*case '\e':
603 OS << "'\\e'";
604 break;*/
605 case '\f':
606 OS << "'\\f'";
607 break;
608 case '\n':
609 OS << "'\\n'";
610 break;
611 case '\r':
612 OS << "'\\r'";
613 break;
614 case '\t':
615 OS << "'\\t'";
616 break;
617 case '\v':
618 OS << "'\\v'";
619 break;
620 default:
Ted Kremenek471733d2008-02-23 00:52:04 +0000621 if (value < 256 && isprint(value)) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000622 OS << "'" << (char)value << "'";
623 } else if (value < 256) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000624 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000625 } else {
626 // FIXME what to really do here?
627 OS << value;
628 }
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000629 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000630}
631
632void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
633 bool isSigned = Node->getType()->isSignedIntegerType();
634 OS << Node->getValue().toString(10, isSigned);
635
636 // Emit suffixes. Integer literals are always a builtin integer type.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000637 switch (Node->getType()->getAsBuiltinType()->getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000638 default: assert(0 && "Unexpected type for integer literal!");
639 case BuiltinType::Int: break; // no suffix.
640 case BuiltinType::UInt: OS << 'U'; break;
641 case BuiltinType::Long: OS << 'L'; break;
642 case BuiltinType::ULong: OS << "UL"; break;
643 case BuiltinType::LongLong: OS << "LL"; break;
644 case BuiltinType::ULongLong: OS << "ULL"; break;
645 }
646}
647void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner86e499d2007-08-01 00:23:58 +0000648 // FIXME: print value more precisely.
Chris Lattnerda8249e2008-06-07 22:13:43 +0000649 OS << Node->getValueAsApproximateDouble();
Reid Spencer5f016e22007-07-11 17:01:13 +0000650}
Chris Lattner5d661452007-08-26 03:42:43 +0000651
652void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
653 PrintExpr(Node->getSubExpr());
654 OS << "i";
655}
656
Reid Spencer5f016e22007-07-11 17:01:13 +0000657void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
658 if (Str->isWide()) OS << 'L';
659 OS << '"';
Anders Carlssonee98ac52007-10-15 02:50:23 +0000660
Reid Spencer5f016e22007-07-11 17:01:13 +0000661 // FIXME: this doesn't print wstrings right.
662 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattner9a81c872009-01-16 19:25:18 +0000663 unsigned char Char = Str->getStrData()[i];
664
665 switch (Char) {
666 default:
667 if (isprint(Char))
668 OS << (char)Char;
669 else // Output anything hard as an octal escape.
670 OS << '\\'
671 << (char)('0'+ ((Char >> 6) & 7))
672 << (char)('0'+ ((Char >> 3) & 7))
673 << (char)('0'+ ((Char >> 0) & 7));
674 break;
675 // Handle some common non-printable cases to make dumps prettier.
Reid Spencer5f016e22007-07-11 17:01:13 +0000676 case '\\': OS << "\\\\"; break;
677 case '"': OS << "\\\""; break;
678 case '\n': OS << "\\n"; break;
679 case '\t': OS << "\\t"; break;
680 case '\a': OS << "\\a"; break;
681 case '\b': OS << "\\b"; break;
682 }
683 }
684 OS << '"';
685}
686void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
687 OS << "(";
688 PrintExpr(Node->getSubExpr());
689 OS << ")";
690}
691void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +0000692 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000693 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner296bf192007-08-23 21:46:40 +0000694
Sebastian Redl05189992008-11-11 17:56:53 +0000695 // Print a space if this is an "identifier operator" like __real.
Chris Lattner296bf192007-08-23 21:46:40 +0000696 switch (Node->getOpcode()) {
697 default: break;
Chris Lattner296bf192007-08-23 21:46:40 +0000698 case UnaryOperator::Real:
699 case UnaryOperator::Imag:
700 case UnaryOperator::Extension:
701 OS << ' ';
702 break;
703 }
704 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000705 PrintExpr(Node->getSubExpr());
706
707 if (Node->isPostfix())
708 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +0000709}
Chris Lattner704fe352007-08-30 17:59:59 +0000710
711bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
Eli Friedman35183ac2009-02-27 06:44:11 +0000712 if (isa<UnaryOperator>(E)) {
Chris Lattner704fe352007-08-30 17:59:59 +0000713 // Base case, print the type and comma.
714 OS << E->getType().getAsString() << ", ";
715 return true;
716 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
717 PrintOffsetOfDesignator(ASE->getLHS());
718 OS << "[";
719 PrintExpr(ASE->getRHS());
720 OS << "]";
721 return false;
722 } else {
723 MemberExpr *ME = cast<MemberExpr>(E);
724 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
Chris Lattner39f34e92008-11-24 04:00:27 +0000725 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getNameAsString();
Chris Lattner704fe352007-08-30 17:59:59 +0000726 return false;
727 }
728}
729
730void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
731 OS << "__builtin_offsetof(";
732 PrintOffsetOfDesignator(Node->getSubExpr());
733 OS << ")";
734}
735
Sebastian Redl05189992008-11-11 17:56:53 +0000736void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
737 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
738 if (Node->isArgumentType())
739 OS << "(" << Node->getArgumentType().getAsString() << ")";
740 else {
741 OS << " ";
742 PrintExpr(Node->getArgumentExpr());
743 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000744}
745void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +0000746 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000747 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +0000748 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000749 OS << "]";
750}
751
752void StmtPrinter::VisitCallExpr(CallExpr *Call) {
753 PrintExpr(Call->getCallee());
754 OS << "(";
755 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattner04421082008-04-08 04:40:51 +0000756 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
757 // Don't print any defaulted arguments
758 break;
759 }
760
Reid Spencer5f016e22007-07-11 17:01:13 +0000761 if (i) OS << ", ";
762 PrintExpr(Call->getArg(i));
763 }
764 OS << ")";
765}
766void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregorb3eef682009-01-08 22:45:41 +0000767 // FIXME: Suppress printing implicit bases (like "this")
768 PrintExpr(Node->getBase());
769 OS << (Node->isArrow() ? "->" : ".");
770 // FIXME: Suppress printing references to unnamed objects
771 // representing anonymous unions/structs
Douglas Gregor86f19402008-12-20 23:49:58 +0000772 OS << Node->getMemberDecl()->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000773}
Nate Begeman213541a2008-04-18 23:10:10 +0000774void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +0000775 PrintExpr(Node->getBase());
776 OS << ".";
777 OS << Node->getAccessor().getName();
778}
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000779void StmtPrinter::VisitCastExpr(CastExpr *) {
780 assert(0 && "CastExpr is an abstract class");
781}
Douglas Gregor49badde2008-10-27 19:41:14 +0000782void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *) {
783 assert(0 && "ExplicitCastExpr is an abstract class");
784}
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000785void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000786 OS << "(" << Node->getType().getAsString() << ")";
Reid Spencer5f016e22007-07-11 17:01:13 +0000787 PrintExpr(Node->getSubExpr());
788}
Steve Naroffaff1edd2007-07-19 21:32:11 +0000789void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
790 OS << "(" << Node->getType().getAsString() << ")";
791 PrintExpr(Node->getInitializer());
792}
Steve Naroff49b45262007-07-13 16:58:59 +0000793void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroff90045e82007-07-13 23:32:42 +0000794 // No need to print anything, simply forward to the sub expression.
795 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +0000796}
Reid Spencer5f016e22007-07-11 17:01:13 +0000797void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
798 PrintExpr(Node->getLHS());
799 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
800 PrintExpr(Node->getRHS());
801}
Chris Lattnereb14fe82007-08-25 02:00:02 +0000802void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
803 PrintExpr(Node->getLHS());
804 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
805 PrintExpr(Node->getRHS());
806}
Reid Spencer5f016e22007-07-11 17:01:13 +0000807void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
808 PrintExpr(Node->getCond());
Ted Kremenek8e911c42007-11-26 18:27:54 +0000809
810 if (Node->getLHS()) {
811 OS << " ? ";
812 PrintExpr(Node->getLHS());
813 OS << " : ";
814 }
815 else { // Handle GCC extention where LHS can be NULL.
816 OS << " ?: ";
817 }
818
Reid Spencer5f016e22007-07-11 17:01:13 +0000819 PrintExpr(Node->getRHS());
820}
821
822// GNU extensions.
823
Chris Lattner6481a572007-08-03 17:31:20 +0000824void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000825 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000826}
827
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000828void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
829 OS << "(";
830 PrintRawCompoundStmt(E->getSubStmt());
831 OS << ")";
832}
833
Steve Naroffd34e9152007-08-01 22:05:33 +0000834void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
835 OS << "__builtin_types_compatible_p(";
836 OS << Node->getArgType1().getAsString() << ",";
837 OS << Node->getArgType2().getAsString() << ")";
838}
839
Steve Naroffd04fdd52007-08-03 21:21:27 +0000840void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
841 OS << "__builtin_choose_expr(";
842 PrintExpr(Node->getCond());
Chris Lattner94f05e32007-08-04 00:20:15 +0000843 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000844 PrintExpr(Node->getLHS());
Chris Lattner94f05e32007-08-04 00:20:15 +0000845 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000846 PrintExpr(Node->getRHS());
847 OS << ")";
848}
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000849
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000850void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
851 OS << "__null";
852}
853
Eli Friedmand38617c2008-05-14 19:38:39 +0000854void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
855 OS << "__builtin_shufflevector(";
856 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
857 if (i) OS << ", ";
858 PrintExpr(Node->getExpr(i));
859 }
860 OS << ")";
861}
862
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000863void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
864 OS << "{ ";
865 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
866 if (i) OS << ", ";
Douglas Gregor4c678342009-01-28 21:54:33 +0000867 if (Node->getInit(i))
868 PrintExpr(Node->getInit(i));
869 else
870 OS << "0";
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000871 }
872 OS << " }";
873}
874
Douglas Gregor05c13a32009-01-22 00:58:24 +0000875void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000876 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
877 DEnd = Node->designators_end();
878 D != DEnd; ++D) {
879 if (D->isFieldDesignator()) {
880 if (D->getDotLoc().isInvalid())
881 OS << D->getFieldName()->getName() << ":";
882 else
883 OS << "." << D->getFieldName()->getName();
884 } else {
885 OS << "[";
886 if (D->isArrayDesignator()) {
887 PrintExpr(Node->getArrayIndex(*D));
888 } else {
889 PrintExpr(Node->getArrayRangeStart(*D));
890 OS << " ... ";
891 PrintExpr(Node->getArrayRangeEnd(*D));
892 }
893 OS << "]";
894 }
895 }
896
897 OS << " = ";
898 PrintExpr(Node->getInit());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000899}
900
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000901void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
902 OS << "/*implicit*/" << Node->getType().getAsString() << "()";
903}
904
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000905void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
906 OS << "va_arg(";
907 PrintExpr(Node->getSubExpr());
908 OS << ", ";
909 OS << Node->getType().getAsString();
910 OS << ")";
911}
912
Reid Spencer5f016e22007-07-11 17:01:13 +0000913// C++
Douglas Gregorb4609802008-11-14 16:09:21 +0000914void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
915 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
916 "",
917#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
918 Spelling,
919#include "clang/Basic/OperatorKinds.def"
920 };
921
922 OverloadedOperatorKind Kind = Node->getOperator();
923 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
924 if (Node->getNumArgs() == 1) {
925 OS << OpStrings[Kind] << ' ';
926 PrintExpr(Node->getArg(0));
927 } else {
928 PrintExpr(Node->getArg(0));
929 OS << ' ' << OpStrings[Kind];
930 }
931 } else if (Kind == OO_Call) {
932 PrintExpr(Node->getArg(0));
933 OS << '(';
934 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
935 if (ArgIdx > 1)
936 OS << ", ";
937 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
938 PrintExpr(Node->getArg(ArgIdx));
939 }
940 OS << ')';
941 } else if (Kind == OO_Subscript) {
942 PrintExpr(Node->getArg(0));
943 OS << '[';
944 PrintExpr(Node->getArg(1));
945 OS << ']';
946 } else if (Node->getNumArgs() == 1) {
947 OS << OpStrings[Kind] << ' ';
948 PrintExpr(Node->getArg(0));
949 } else if (Node->getNumArgs() == 2) {
950 PrintExpr(Node->getArg(0));
951 OS << ' ' << OpStrings[Kind] << ' ';
952 PrintExpr(Node->getArg(1));
953 } else {
954 assert(false && "unknown overloaded operator");
955 }
956}
Reid Spencer5f016e22007-07-11 17:01:13 +0000957
Douglas Gregor88a35142008-12-22 05:46:06 +0000958void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
959 VisitCallExpr(cast<CallExpr>(Node));
960}
961
Douglas Gregor49badde2008-10-27 19:41:14 +0000962void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
963 OS << Node->getCastName() << '<';
964 OS << Node->getTypeAsWritten().getAsString() << ">(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000965 PrintExpr(Node->getSubExpr());
966 OS << ")";
967}
968
Douglas Gregor49badde2008-10-27 19:41:14 +0000969void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
970 VisitCXXNamedCastExpr(Node);
971}
972
973void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
974 VisitCXXNamedCastExpr(Node);
975}
976
977void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
978 VisitCXXNamedCastExpr(Node);
979}
980
981void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
982 VisitCXXNamedCastExpr(Node);
983}
984
Sebastian Redlc42e1182008-11-11 11:37:55 +0000985void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
986 OS << "typeid(";
987 if (Node->isTypeOperand()) {
988 OS << Node->getTypeOperand().getAsString();
989 } else {
990 PrintExpr(Node->getExprOperand());
991 }
992 OS << ")";
993}
994
Reid Spencer5f016e22007-07-11 17:01:13 +0000995void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
996 OS << (Node->getValue() ? "true" : "false");
997}
998
Douglas Gregor796da182008-11-04 14:32:21 +0000999void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1000 OS << "this";
1001}
1002
Chris Lattner50dd2892008-02-26 00:51:44 +00001003void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1004 if (Node->getSubExpr() == 0)
1005 OS << "throw";
1006 else {
1007 OS << "throw ";
1008 PrintExpr(Node->getSubExpr());
1009 }
1010}
1011
Chris Lattner04421082008-04-08 04:40:51 +00001012void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1013 // Nothing to print: we picked up the default argument
1014}
1015
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001016void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1017 OS << Node->getType().getAsString();
1018 OS << "(";
1019 PrintExpr(Node->getSubExpr());
1020 OS << ")";
1021}
1022
Douglas Gregor506ae412009-01-16 18:33:17 +00001023void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1024 OS << Node->getType().getAsString();
1025 OS << "(";
1026 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1027 ArgEnd = Node->arg_end();
1028 Arg != ArgEnd; ++Arg) {
1029 if (Arg != Node->arg_begin())
1030 OS << ", ";
1031 PrintExpr(*Arg);
1032 }
1033 OS << ")";
1034}
1035
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001036void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
1037 OS << Node->getType().getAsString() << "()";
1038}
1039
Argyrios Kyrtzidis9e922b12008-09-09 23:47:53 +00001040void
1041StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
1042 PrintRawDecl(E->getVarDecl());
1043}
1044
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001045void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1046 if (E->isGlobalNew())
1047 OS << "::";
1048 OS << "new ";
1049 unsigned NumPlace = E->getNumPlacementArgs();
1050 if (NumPlace > 0) {
1051 OS << "(";
1052 PrintExpr(E->getPlacementArg(0));
1053 for (unsigned i = 1; i < NumPlace; ++i) {
1054 OS << ", ";
1055 PrintExpr(E->getPlacementArg(i));
1056 }
1057 OS << ") ";
1058 }
1059 if (E->isParenTypeId())
1060 OS << "(";
Sebastian Redl6fec6482008-12-02 22:08:59 +00001061 std::string TypeS;
1062 if (Expr *Size = E->getArraySize()) {
1063 llvm::raw_string_ostream s(TypeS);
1064 Size->printPretty(s);
1065 s.flush();
1066 TypeS = "[" + TypeS + "]";
1067 }
1068 E->getAllocatedType().getAsStringInternal(TypeS);
1069 OS << TypeS;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001070 if (E->isParenTypeId())
1071 OS << ")";
1072
1073 if (E->hasInitializer()) {
1074 OS << "(";
1075 unsigned NumCons = E->getNumConstructorArgs();
1076 if (NumCons > 0) {
1077 PrintExpr(E->getConstructorArg(0));
1078 for (unsigned i = 1; i < NumCons; ++i) {
1079 OS << ", ";
1080 PrintExpr(E->getConstructorArg(i));
1081 }
1082 }
1083 OS << ")";
1084 }
1085}
1086
1087void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1088 if (E->isGlobalDelete())
1089 OS << "::";
1090 OS << "delete ";
1091 if (E->isArrayForm())
1092 OS << "[] ";
1093 PrintExpr(E->getArgument());
1094}
1095
Douglas Gregor17330012009-02-04 15:01:18 +00001096void StmtPrinter::VisitUnresolvedFunctionNameExpr(UnresolvedFunctionNameExpr *E) {
1097 OS << E->getName().getAsString();
Douglas Gregor5c37de72008-12-06 00:22:45 +00001098}
1099
Anders Carlssone349bea2009-04-23 02:32:43 +00001100void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
1101 // Nothing to print.
1102}
1103
Anders Carlsson19d28a62009-04-21 02:22:11 +00001104void StmtPrinter::VisitCXXDestroyExpr(CXXDestroyExpr *E) {
1105 // Nothing to print.
1106}
1107
Anders Carlsson02bbfa32009-04-24 22:47:04 +00001108void StmtPrinter::VisitCXXExprWithCleanup(CXXExprWithCleanup *E) {
1109 // Just forward to the sub expression.
1110 PrintExpr(E->getSubExpr());
1111}
1112
Sebastian Redl64b45f72009-01-05 20:52:13 +00001113static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1114 switch (UTT) {
1115 default: assert(false && "Unknown type trait");
1116 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1117 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1118 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1119 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1120 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1121 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1122 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1123 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1124 case UTT_IsAbstract: return "__is_abstract";
1125 case UTT_IsClass: return "__is_class";
1126 case UTT_IsEmpty: return "__is_empty";
1127 case UTT_IsEnum: return "__is_enum";
1128 case UTT_IsPOD: return "__is_pod";
1129 case UTT_IsPolymorphic: return "__is_polymorphic";
1130 case UTT_IsUnion: return "__is_union";
1131 }
1132}
1133
1134void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1135 OS << getTypeTraitName(E->getTrait()) << "("
1136 << E->getQueriedType().getAsString() << ")";
1137}
1138
Anders Carlsson55085182007-08-21 17:43:55 +00001139// Obj-C
1140
1141void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1142 OS << "@";
1143 VisitStringLiteral(Node->getString());
1144}
Reid Spencer5f016e22007-07-11 17:01:13 +00001145
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001146void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001147 OS << "@encode(" << Node->getEncodedType().getAsString() << ')';
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001148}
1149
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001150void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001151 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001152}
1153
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001154void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001155 OS << "@protocol(" << Node->getProtocol()->getNameAsString() << ')';
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001156}
1157
Steve Naroff563477d2007-09-18 23:55:05 +00001158void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1159 OS << "[";
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001160 Expr *receiver = Mess->getReceiver();
1161 if (receiver) PrintExpr(receiver);
1162 else OS << Mess->getClassName()->getName();
Ted Kremenekc29efd82008-05-02 17:32:38 +00001163 OS << ' ';
Ted Kremenek97b7f262008-04-16 04:30:16 +00001164 Selector selector = Mess->getSelector();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001165 if (selector.isUnarySelector()) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001166 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001167 } else {
1168 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001169 if (i < selector.getNumArgs()) {
1170 if (i > 0) OS << ' ';
1171 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner39f34e92008-11-24 04:00:27 +00001172 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremenekc29efd82008-05-02 17:32:38 +00001173 else
1174 OS << ":";
1175 }
1176 else OS << ", "; // Handle variadic methods.
1177
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001178 PrintExpr(Mess->getArg(i));
1179 }
Steve Naroff563477d2007-09-18 23:55:05 +00001180 }
1181 OS << "]";
1182}
1183
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001184void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) {
1185 OS << "super";
1186}
1187
Steve Naroff4eb206b2008-09-03 18:15:37 +00001188void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001189 BlockDecl *BD = Node->getBlockDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001190 OS << "^";
1191
1192 const FunctionType *AFT = Node->getFunctionType();
1193
Douglas Gregor72564e72009-02-26 23:50:07 +00001194 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001195 OS << "()";
Douglas Gregor72564e72009-02-26 23:50:07 +00001196 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001197 OS << '(';
1198 std::string ParamStr;
Steve Naroff56ee6892008-10-08 17:01:13 +00001199 for (BlockDecl::param_iterator AI = BD->param_begin(),
1200 E = BD->param_end(); AI != E; ++AI) {
1201 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner39f34e92008-11-24 04:00:27 +00001202 ParamStr = (*AI)->getNameAsString();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001203 (*AI)->getType().getAsStringInternal(ParamStr);
1204 OS << ParamStr;
1205 }
1206
Douglas Gregor72564e72009-02-26 23:50:07 +00001207 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001208 if (FT->isVariadic()) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001209 if (!BD->param_empty()) OS << ", ";
Steve Naroff4eb206b2008-09-03 18:15:37 +00001210 OS << "...";
1211 }
1212 OS << ')';
1213 }
1214}
1215
Steve Naroff4eb206b2008-09-03 18:15:37 +00001216void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001217 OS << Node->getDecl()->getNameAsString();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001218}
Reid Spencer5f016e22007-07-11 17:01:13 +00001219//===----------------------------------------------------------------------===//
1220// Stmt method implementations
1221//===----------------------------------------------------------------------===//
1222
Chris Lattner6000dac2007-08-08 22:51:59 +00001223void Stmt::dumpPretty() const {
Daniel Dunbar4a77edb2009-03-10 18:00:19 +00001224 printPretty(llvm::errs());
Reid Spencer5f016e22007-07-11 17:01:13 +00001225}
1226
Mike Stump071e4da2009-02-10 20:16:46 +00001227void Stmt::printPretty(llvm::raw_ostream &OS, PrinterHelper* Helper,
1228 unsigned I, bool NoIndent) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001229 if (this == 0) {
1230 OS << "<NULL>";
1231 return;
1232 }
1233
Mike Stump071e4da2009-02-10 20:16:46 +00001234 StmtPrinter P(OS, Helper, I, NoIndent);
Chris Lattnerc5598cb2007-08-21 04:04:25 +00001235 P.Visit(const_cast<Stmt*>(this));
Reid Spencer5f016e22007-07-11 17:01:13 +00001236}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001237
1238//===----------------------------------------------------------------------===//
1239// PrinterHelper
1240//===----------------------------------------------------------------------===//
1241
1242// Implement virtual destructor.
Gabor Greif84675832007-09-11 15:32:40 +00001243PrinterHelper::~PrinterHelper() {}