blob: b55bddfed09d74b90abe70ef3dad7de1aaeedbac [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)) {
120 switch (V->getStorageClass()) {
121 default: assert(0 && "Unknown storage class!");
Mike Stumpc5840c02009-02-10 23:49:50 +0000122 case VarDecl::None: break;
123 case VarDecl::Extern: OS << "extern "; break;
124 case VarDecl::Static: OS << "static "; break;
125 case VarDecl::Auto: OS << "auto "; break;
126 case VarDecl::Register: OS << "register "; break;
127 case VarDecl::PrivateExtern: OS << "__private_extern "; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 }
129 }
130
Chris Lattner39f34e92008-11-24 04:00:27 +0000131 std::string Name = VD->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 VD->getType().getAsStringInternal(Name);
133 OS << Name;
134
Chris Lattner24c39902007-07-12 00:36:32 +0000135 // If this is a vardecl with an initializer, emit it.
136 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
137 if (V->getInit()) {
138 OS << " = ";
139 PrintExpr(V->getInit());
140 }
141 }
Steve Naroff91578f32007-11-17 21:21:01 +0000142 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
143 // print a free standing tag decl (e.g. "struct x;").
144 OS << TD->getKindName();
145 OS << " ";
146 if (const IdentifierInfo *II = TD->getIdentifier())
147 OS << II->getName();
Mike Stump071e4da2009-02-10 20:16:46 +0000148 if (RecordDecl *RD = dyn_cast<RecordDecl>(TD)) {
149 OS << "{\n";
150 IndentLevel += 1;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000151 // FIXME: The context passed to field_begin/field_end should
152 // never be NULL!
153 ASTContext *Context = 0;
154 for (RecordDecl::field_iterator i = RD->field_begin(*Context);
155 i != RD->field_end(*Context); ++i) {
Mike Stump071e4da2009-02-10 20:16:46 +0000156 PrintFieldDecl(*i);
157 IndentLevel -= 1;
158 }
159 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 } else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 assert(0 && "Unexpected decl");
162 }
163}
164
Mike Stump071e4da2009-02-10 20:16:46 +0000165void StmtPrinter::PrintFieldDecl(FieldDecl *FD) {
166 Indent() << FD->getNameAsString() << "\n";
167}
168
Ted Kremenekecd64c52008-10-06 18:39:36 +0000169void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
Mike Stump071e4da2009-02-10 20:16:46 +0000170 bool isFirst = true;
Ted Kremenekecd64c52008-10-06 18:39:36 +0000171
172 for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();
173 I != E; ++I) {
174
175 if (!isFirst) OS << ", ";
176 else isFirst = false;
177
178 PrintRawDecl(*I);
179 }
180}
Reid Spencer5f016e22007-07-11 17:01:13 +0000181
182void StmtPrinter::VisitNullStmt(NullStmt *Node) {
183 Indent() << ";\n";
184}
185
186void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Ted Kremenekecd64c52008-10-06 18:39:36 +0000187 for (DeclStmt::decl_iterator I = Node->decl_begin(), E = Node->decl_end();
188 I!=E; ++I) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 Indent();
Ted Kremenekecd64c52008-10-06 18:39:36 +0000190 PrintRawDecl(*I);
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 OS << ";\n";
192 }
193}
194
195void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
196 Indent();
197 PrintRawCompoundStmt(Node);
198 OS << "\n";
199}
200
201void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
202 Indent(-1) << "case ";
203 PrintExpr(Node->getLHS());
204 if (Node->getRHS()) {
205 OS << " ... ";
206 PrintExpr(Node->getRHS());
207 }
208 OS << ":\n";
209
210 PrintStmt(Node->getSubStmt(), 0);
211}
212
213void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
214 Indent(-1) << "default:\n";
215 PrintStmt(Node->getSubStmt(), 0);
216}
217
218void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
219 Indent(-1) << Node->getName() << ":\n";
220 PrintStmt(Node->getSubStmt(), 0);
221}
222
223void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000224 OS << "if (";
Reid Spencer5f016e22007-07-11 17:01:13 +0000225 PrintExpr(If->getCond());
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000226 OS << ')';
Reid Spencer5f016e22007-07-11 17:01:13 +0000227
228 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
229 OS << ' ';
230 PrintRawCompoundStmt(CS);
231 OS << (If->getElse() ? ' ' : '\n');
232 } else {
233 OS << '\n';
234 PrintStmt(If->getThen());
235 if (If->getElse()) Indent();
236 }
237
238 if (Stmt *Else = If->getElse()) {
239 OS << "else";
240
241 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
242 OS << ' ';
243 PrintRawCompoundStmt(CS);
244 OS << '\n';
245 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
246 OS << ' ';
247 PrintRawIfStmt(ElseIf);
248 } else {
249 OS << '\n';
250 PrintStmt(If->getElse());
251 }
252 }
253}
254
255void StmtPrinter::VisitIfStmt(IfStmt *If) {
256 Indent();
257 PrintRawIfStmt(If);
258}
259
260void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
261 Indent() << "switch (";
262 PrintExpr(Node->getCond());
263 OS << ")";
264
265 // Pretty print compoundstmt bodies (very common).
266 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
267 OS << " ";
268 PrintRawCompoundStmt(CS);
269 OS << "\n";
270 } else {
271 OS << "\n";
272 PrintStmt(Node->getBody());
273 }
274}
275
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000276void StmtPrinter::VisitSwitchCase(SwitchCase*) {
277 assert(0 && "SwitchCase is an abstract class");
278}
279
Reid Spencer5f016e22007-07-11 17:01:13 +0000280void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
281 Indent() << "while (";
282 PrintExpr(Node->getCond());
283 OS << ")\n";
284 PrintStmt(Node->getBody());
285}
286
287void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner8bdcc472007-09-15 21:49:37 +0000288 Indent() << "do ";
289 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
290 PrintRawCompoundStmt(CS);
291 OS << " ";
292 } else {
293 OS << "\n";
294 PrintStmt(Node->getBody());
295 Indent();
296 }
297
298 OS << "while ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000299 PrintExpr(Node->getCond());
300 OS << ";\n";
301}
302
303void StmtPrinter::VisitForStmt(ForStmt *Node) {
304 Indent() << "for (";
305 if (Node->getInit()) {
306 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000307 PrintRawDeclStmt(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000308 else
309 PrintExpr(cast<Expr>(Node->getInit()));
310 }
Chris Lattner8bdcc472007-09-15 21:49:37 +0000311 OS << ";";
312 if (Node->getCond()) {
313 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 PrintExpr(Node->getCond());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000315 }
316 OS << ";";
317 if (Node->getInc()) {
318 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000319 PrintExpr(Node->getInc());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000320 }
321 OS << ") ";
322
323 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
324 PrintRawCompoundStmt(CS);
325 OS << "\n";
326 } else {
327 OS << "\n";
328 PrintStmt(Node->getBody());
329 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000330}
331
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000332void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000333 Indent() << "for (";
334 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000335 PrintRawDeclStmt(DS);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000336 else
337 PrintExpr(cast<Expr>(Node->getElement()));
338 OS << " in ";
339 PrintExpr(Node->getCollection());
340 OS << ") ";
341
342 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
343 PrintRawCompoundStmt(CS);
344 OS << "\n";
345 } else {
346 OS << "\n";
347 PrintStmt(Node->getBody());
348 }
349}
350
Reid Spencer5f016e22007-07-11 17:01:13 +0000351void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
352 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
353}
354
355void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
356 Indent() << "goto *";
357 PrintExpr(Node->getTarget());
358 OS << ";\n";
359}
360
361void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
362 Indent() << "continue;\n";
363}
364
365void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
366 Indent() << "break;\n";
367}
368
369
370void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
371 Indent() << "return";
372 if (Node->getRetValue()) {
373 OS << " ";
374 PrintExpr(Node->getRetValue());
375 }
376 OS << ";\n";
377}
378
Chris Lattnerfe795952007-10-29 04:04:16 +0000379
380void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson39c47b52007-11-23 23:12:25 +0000381 Indent() << "asm ";
382
383 if (Node->isVolatile())
384 OS << "volatile ";
385
386 OS << "(";
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000387 VisitStringLiteral(Node->getAsmString());
Anders Carlssonb235fc22007-11-22 01:36:19 +0000388
389 // Outputs
390 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
391 Node->getNumClobbers() != 0)
392 OS << " : ";
393
394 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
395 if (i != 0)
396 OS << ", ";
397
398 if (!Node->getOutputName(i).empty()) {
399 OS << '[';
400 OS << Node->getOutputName(i);
401 OS << "] ";
402 }
403
Chris Lattnerb3277932009-03-10 04:59:06 +0000404 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000405 OS << " ";
406 Visit(Node->getOutputExpr(i));
407 }
408
409 // Inputs
410 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
411 OS << " : ";
412
413 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
414 if (i != 0)
415 OS << ", ";
416
417 if (!Node->getInputName(i).empty()) {
418 OS << '[';
419 OS << Node->getInputName(i);
420 OS << "] ";
421 }
422
Chris Lattnerb3277932009-03-10 04:59:06 +0000423 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000424 OS << " ";
425 Visit(Node->getInputExpr(i));
426 }
427
428 // Clobbers
429 if (Node->getNumClobbers() != 0)
430 OS << " : ";
431
432 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
433 if (i != 0)
434 OS << ", ";
435
436 VisitStringLiteral(Node->getClobber(i));
437 }
438
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000439 OS << ");\n";
Chris Lattnerfe795952007-10-29 04:04:16 +0000440}
441
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000442void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000443 Indent() << "@try";
444 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
445 PrintRawCompoundStmt(TS);
446 OS << "\n";
447 }
448
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000449 for (ObjCAtCatchStmt *catchStmt =
450 static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts());
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000451 catchStmt;
452 catchStmt =
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000453 static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000454 Indent() << "@catch(";
Steve Naroff7ba138a2009-03-03 19:52:17 +0000455 if (catchStmt->getCatchParamDecl()) {
456 if (Decl *DS = catchStmt->getCatchParamDecl())
457 PrintRawDecl(DS);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000458 }
459 OS << ")";
460 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody()))
461 {
462 PrintRawCompoundStmt(CS);
463 OS << "\n";
464 }
465 }
466
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000467 if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>(
Fariborz Jahanian1e7eab42007-11-07 00:46:42 +0000468 Node->getFinallyStmt())) {
469 Indent() << "@finally";
470 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000471 OS << "\n";
472 }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000473}
474
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000475void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000476}
477
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000478void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000479 Indent() << "@catch (...) { /* todo */ } \n";
480}
481
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000482void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000483 Indent() << "@throw";
484 if (Node->getThrowExpr()) {
485 OS << " ";
486 PrintExpr(Node->getThrowExpr());
487 }
488 OS << ";\n";
489}
490
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000491void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000492 Indent() << "@synchronized (";
493 PrintExpr(Node->getSynchExpr());
494 OS << ")";
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000495 PrintRawCompoundStmt(Node->getSynchBody());
496 OS << "\n";
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000497}
498
Sebastian Redl8351da02008-12-22 21:35:02 +0000499void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
500 OS << "catch (";
Sebastian Redl4b07b292008-12-22 19:15:10 +0000501 if (Decl *ExDecl = Node->getExceptionDecl())
502 PrintRawDecl(ExDecl);
503 else
504 OS << "...";
505 OS << ") ";
506 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl8351da02008-12-22 21:35:02 +0000507}
508
509void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
510 Indent();
511 PrintRawCXXCatchStmt(Node);
512 OS << "\n";
513}
514
515void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
516 Indent() << "try ";
517 PrintRawCompoundStmt(Node->getTryBlock());
518 for(unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
519 OS << " ";
520 PrintRawCXXCatchStmt(Node->getHandler(i));
521 }
Sebastian Redl4b07b292008-12-22 19:15:10 +0000522 OS << "\n";
523}
524
Reid Spencer5f016e22007-07-11 17:01:13 +0000525//===----------------------------------------------------------------------===//
526// Expr printing methods.
527//===----------------------------------------------------------------------===//
528
529void StmtPrinter::VisitExpr(Expr *Node) {
530 OS << "<<unknown expr type>>";
531}
532
533void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +0000534 OS << Node->getDecl()->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000535}
536
Douglas Gregorbad35182009-03-19 03:51:16 +0000537void StmtPrinter::VisitQualifiedDeclRefExpr(QualifiedDeclRefExpr *Node) {
Douglas Gregor1a49af92009-01-06 05:10:23 +0000538 NamedDecl *D = Node->getDecl();
539
Douglas Gregor9bde7732009-03-31 20:22:05 +0000540 Node->getQualifier()->print(OS);
Douglas Gregor1a49af92009-01-06 05:10:23 +0000541 OS << D->getNameAsString();
542}
543
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000544void StmtPrinter::VisitUnresolvedDeclRefExpr(UnresolvedDeclRefExpr *Node) {
Douglas Gregor9bde7732009-03-31 20:22:05 +0000545 Node->getQualifier()->print(OS);
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000546 OS << Node->getDeclName().getAsString();
547}
548
Steve Naroff7779db42007-11-12 14:29:37 +0000549void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000550 if (Node->getBase()) {
551 PrintExpr(Node->getBase());
552 OS << (Node->isArrow() ? "->" : ".");
553 }
Chris Lattner39f34e92008-11-24 04:00:27 +0000554 OS << Node->getDecl()->getNameAsString();
Steve Naroff7779db42007-11-12 14:29:37 +0000555}
556
Steve Naroffae784072008-05-30 00:40:33 +0000557void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
558 if (Node->getBase()) {
559 PrintExpr(Node->getBase());
560 OS << ".";
561 }
Steve Naroffc77a6362008-12-04 16:24:46 +0000562 OS << Node->getProperty()->getNameAsCString();
Steve Naroffae784072008-05-30 00:40:33 +0000563}
564
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000565void StmtPrinter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node) {
566 if (Node->getBase()) {
567 PrintExpr(Node->getBase());
568 OS << ".";
569 }
570 // FIXME: Setter/Getter names
571}
572
Chris Lattnerd9f69102008-08-10 01:53:14 +0000573void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson22742662007-07-21 05:21:51 +0000574 switch (Node->getIdentType()) {
575 default:
576 assert(0 && "unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000577 case PredefinedExpr::Func:
Anders Carlsson22742662007-07-21 05:21:51 +0000578 OS << "__func__";
579 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000580 case PredefinedExpr::Function:
Anders Carlsson22742662007-07-21 05:21:51 +0000581 OS << "__FUNCTION__";
582 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000583 case PredefinedExpr::PrettyFunction:
Anders Carlsson22742662007-07-21 05:21:51 +0000584 OS << "__PRETTY_FUNCTION__";
585 break;
586 }
587}
588
Reid Spencer5f016e22007-07-11 17:01:13 +0000589void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000590 unsigned value = Node->getValue();
Chris Lattnerc250aae2008-06-07 22:35:38 +0000591 if (Node->isWide())
592 OS << "L";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000593 switch (value) {
594 case '\\':
595 OS << "'\\\\'";
596 break;
597 case '\'':
598 OS << "'\\''";
599 break;
600 case '\a':
601 // TODO: K&R: the meaning of '\\a' is different in traditional C
602 OS << "'\\a'";
603 break;
604 case '\b':
605 OS << "'\\b'";
606 break;
607 // Nonstandard escape sequence.
608 /*case '\e':
609 OS << "'\\e'";
610 break;*/
611 case '\f':
612 OS << "'\\f'";
613 break;
614 case '\n':
615 OS << "'\\n'";
616 break;
617 case '\r':
618 OS << "'\\r'";
619 break;
620 case '\t':
621 OS << "'\\t'";
622 break;
623 case '\v':
624 OS << "'\\v'";
625 break;
626 default:
Ted Kremenek471733d2008-02-23 00:52:04 +0000627 if (value < 256 && isprint(value)) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000628 OS << "'" << (char)value << "'";
629 } else if (value < 256) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000630 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000631 } else {
632 // FIXME what to really do here?
633 OS << value;
634 }
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000635 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000636}
637
638void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
639 bool isSigned = Node->getType()->isSignedIntegerType();
640 OS << Node->getValue().toString(10, isSigned);
641
642 // Emit suffixes. Integer literals are always a builtin integer type.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000643 switch (Node->getType()->getAsBuiltinType()->getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000644 default: assert(0 && "Unexpected type for integer literal!");
645 case BuiltinType::Int: break; // no suffix.
646 case BuiltinType::UInt: OS << 'U'; break;
647 case BuiltinType::Long: OS << 'L'; break;
648 case BuiltinType::ULong: OS << "UL"; break;
649 case BuiltinType::LongLong: OS << "LL"; break;
650 case BuiltinType::ULongLong: OS << "ULL"; break;
651 }
652}
653void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner86e499d2007-08-01 00:23:58 +0000654 // FIXME: print value more precisely.
Chris Lattnerda8249e2008-06-07 22:13:43 +0000655 OS << Node->getValueAsApproximateDouble();
Reid Spencer5f016e22007-07-11 17:01:13 +0000656}
Chris Lattner5d661452007-08-26 03:42:43 +0000657
658void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
659 PrintExpr(Node->getSubExpr());
660 OS << "i";
661}
662
Reid Spencer5f016e22007-07-11 17:01:13 +0000663void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
664 if (Str->isWide()) OS << 'L';
665 OS << '"';
Anders Carlssonee98ac52007-10-15 02:50:23 +0000666
Reid Spencer5f016e22007-07-11 17:01:13 +0000667 // FIXME: this doesn't print wstrings right.
668 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattner9a81c872009-01-16 19:25:18 +0000669 unsigned char Char = Str->getStrData()[i];
670
671 switch (Char) {
672 default:
673 if (isprint(Char))
674 OS << (char)Char;
675 else // Output anything hard as an octal escape.
676 OS << '\\'
677 << (char)('0'+ ((Char >> 6) & 7))
678 << (char)('0'+ ((Char >> 3) & 7))
679 << (char)('0'+ ((Char >> 0) & 7));
680 break;
681 // Handle some common non-printable cases to make dumps prettier.
Reid Spencer5f016e22007-07-11 17:01:13 +0000682 case '\\': OS << "\\\\"; break;
683 case '"': OS << "\\\""; break;
684 case '\n': OS << "\\n"; break;
685 case '\t': OS << "\\t"; break;
686 case '\a': OS << "\\a"; break;
687 case '\b': OS << "\\b"; break;
688 }
689 }
690 OS << '"';
691}
692void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
693 OS << "(";
694 PrintExpr(Node->getSubExpr());
695 OS << ")";
696}
697void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +0000698 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000699 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner296bf192007-08-23 21:46:40 +0000700
Sebastian Redl05189992008-11-11 17:56:53 +0000701 // Print a space if this is an "identifier operator" like __real.
Chris Lattner296bf192007-08-23 21:46:40 +0000702 switch (Node->getOpcode()) {
703 default: break;
Chris Lattner296bf192007-08-23 21:46:40 +0000704 case UnaryOperator::Real:
705 case UnaryOperator::Imag:
706 case UnaryOperator::Extension:
707 OS << ' ';
708 break;
709 }
710 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000711 PrintExpr(Node->getSubExpr());
712
713 if (Node->isPostfix())
714 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +0000715}
Chris Lattner704fe352007-08-30 17:59:59 +0000716
717bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
Eli Friedman35183ac2009-02-27 06:44:11 +0000718 if (isa<UnaryOperator>(E)) {
Chris Lattner704fe352007-08-30 17:59:59 +0000719 // Base case, print the type and comma.
720 OS << E->getType().getAsString() << ", ";
721 return true;
722 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
723 PrintOffsetOfDesignator(ASE->getLHS());
724 OS << "[";
725 PrintExpr(ASE->getRHS());
726 OS << "]";
727 return false;
728 } else {
729 MemberExpr *ME = cast<MemberExpr>(E);
730 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
Chris Lattner39f34e92008-11-24 04:00:27 +0000731 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getNameAsString();
Chris Lattner704fe352007-08-30 17:59:59 +0000732 return false;
733 }
734}
735
736void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
737 OS << "__builtin_offsetof(";
738 PrintOffsetOfDesignator(Node->getSubExpr());
739 OS << ")";
740}
741
Sebastian Redl05189992008-11-11 17:56:53 +0000742void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
743 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
744 if (Node->isArgumentType())
745 OS << "(" << Node->getArgumentType().getAsString() << ")";
746 else {
747 OS << " ";
748 PrintExpr(Node->getArgumentExpr());
749 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000750}
751void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +0000752 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000753 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +0000754 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000755 OS << "]";
756}
757
758void StmtPrinter::VisitCallExpr(CallExpr *Call) {
759 PrintExpr(Call->getCallee());
760 OS << "(";
761 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattner04421082008-04-08 04:40:51 +0000762 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
763 // Don't print any defaulted arguments
764 break;
765 }
766
Reid Spencer5f016e22007-07-11 17:01:13 +0000767 if (i) OS << ", ";
768 PrintExpr(Call->getArg(i));
769 }
770 OS << ")";
771}
772void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregorb3eef682009-01-08 22:45:41 +0000773 // FIXME: Suppress printing implicit bases (like "this")
774 PrintExpr(Node->getBase());
775 OS << (Node->isArrow() ? "->" : ".");
776 // FIXME: Suppress printing references to unnamed objects
777 // representing anonymous unions/structs
Douglas Gregor86f19402008-12-20 23:49:58 +0000778 OS << Node->getMemberDecl()->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000779}
Nate Begeman213541a2008-04-18 23:10:10 +0000780void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +0000781 PrintExpr(Node->getBase());
782 OS << ".";
783 OS << Node->getAccessor().getName();
784}
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000785void StmtPrinter::VisitCastExpr(CastExpr *) {
786 assert(0 && "CastExpr is an abstract class");
787}
Douglas Gregor49badde2008-10-27 19:41:14 +0000788void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *) {
789 assert(0 && "ExplicitCastExpr is an abstract class");
790}
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000791void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000792 OS << "(" << Node->getType().getAsString() << ")";
Reid Spencer5f016e22007-07-11 17:01:13 +0000793 PrintExpr(Node->getSubExpr());
794}
Steve Naroffaff1edd2007-07-19 21:32:11 +0000795void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
796 OS << "(" << Node->getType().getAsString() << ")";
797 PrintExpr(Node->getInitializer());
798}
Steve Naroff49b45262007-07-13 16:58:59 +0000799void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroff90045e82007-07-13 23:32:42 +0000800 // No need to print anything, simply forward to the sub expression.
801 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +0000802}
Reid Spencer5f016e22007-07-11 17:01:13 +0000803void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
804 PrintExpr(Node->getLHS());
805 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
806 PrintExpr(Node->getRHS());
807}
Chris Lattnereb14fe82007-08-25 02:00:02 +0000808void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
809 PrintExpr(Node->getLHS());
810 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
811 PrintExpr(Node->getRHS());
812}
Reid Spencer5f016e22007-07-11 17:01:13 +0000813void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
814 PrintExpr(Node->getCond());
Ted Kremenek8e911c42007-11-26 18:27:54 +0000815
816 if (Node->getLHS()) {
817 OS << " ? ";
818 PrintExpr(Node->getLHS());
819 OS << " : ";
820 }
821 else { // Handle GCC extention where LHS can be NULL.
822 OS << " ?: ";
823 }
824
Reid Spencer5f016e22007-07-11 17:01:13 +0000825 PrintExpr(Node->getRHS());
826}
827
828// GNU extensions.
829
Chris Lattner6481a572007-08-03 17:31:20 +0000830void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000831 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000832}
833
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000834void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
835 OS << "(";
836 PrintRawCompoundStmt(E->getSubStmt());
837 OS << ")";
838}
839
Steve Naroffd34e9152007-08-01 22:05:33 +0000840void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
841 OS << "__builtin_types_compatible_p(";
842 OS << Node->getArgType1().getAsString() << ",";
843 OS << Node->getArgType2().getAsString() << ")";
844}
845
Steve Naroffd04fdd52007-08-03 21:21:27 +0000846void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
847 OS << "__builtin_choose_expr(";
848 PrintExpr(Node->getCond());
Chris Lattner94f05e32007-08-04 00:20:15 +0000849 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000850 PrintExpr(Node->getLHS());
Chris Lattner94f05e32007-08-04 00:20:15 +0000851 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000852 PrintExpr(Node->getRHS());
853 OS << ")";
854}
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000855
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000856void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
857 OS << "__null";
858}
859
Eli Friedmand38617c2008-05-14 19:38:39 +0000860void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
861 OS << "__builtin_shufflevector(";
862 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
863 if (i) OS << ", ";
864 PrintExpr(Node->getExpr(i));
865 }
866 OS << ")";
867}
868
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000869void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
870 OS << "{ ";
871 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
872 if (i) OS << ", ";
Douglas Gregor4c678342009-01-28 21:54:33 +0000873 if (Node->getInit(i))
874 PrintExpr(Node->getInit(i));
875 else
876 OS << "0";
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000877 }
878 OS << " }";
879}
880
Douglas Gregor05c13a32009-01-22 00:58:24 +0000881void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000882 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
883 DEnd = Node->designators_end();
884 D != DEnd; ++D) {
885 if (D->isFieldDesignator()) {
886 if (D->getDotLoc().isInvalid())
887 OS << D->getFieldName()->getName() << ":";
888 else
889 OS << "." << D->getFieldName()->getName();
890 } else {
891 OS << "[";
892 if (D->isArrayDesignator()) {
893 PrintExpr(Node->getArrayIndex(*D));
894 } else {
895 PrintExpr(Node->getArrayRangeStart(*D));
896 OS << " ... ";
897 PrintExpr(Node->getArrayRangeEnd(*D));
898 }
899 OS << "]";
900 }
901 }
902
903 OS << " = ";
904 PrintExpr(Node->getInit());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000905}
906
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000907void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
908 OS << "/*implicit*/" << Node->getType().getAsString() << "()";
909}
910
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000911void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
912 OS << "va_arg(";
913 PrintExpr(Node->getSubExpr());
914 OS << ", ";
915 OS << Node->getType().getAsString();
916 OS << ")";
917}
918
Reid Spencer5f016e22007-07-11 17:01:13 +0000919// C++
Douglas Gregorb4609802008-11-14 16:09:21 +0000920void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
921 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
922 "",
923#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
924 Spelling,
925#include "clang/Basic/OperatorKinds.def"
926 };
927
928 OverloadedOperatorKind Kind = Node->getOperator();
929 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
930 if (Node->getNumArgs() == 1) {
931 OS << OpStrings[Kind] << ' ';
932 PrintExpr(Node->getArg(0));
933 } else {
934 PrintExpr(Node->getArg(0));
935 OS << ' ' << OpStrings[Kind];
936 }
937 } else if (Kind == OO_Call) {
938 PrintExpr(Node->getArg(0));
939 OS << '(';
940 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
941 if (ArgIdx > 1)
942 OS << ", ";
943 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
944 PrintExpr(Node->getArg(ArgIdx));
945 }
946 OS << ')';
947 } else if (Kind == OO_Subscript) {
948 PrintExpr(Node->getArg(0));
949 OS << '[';
950 PrintExpr(Node->getArg(1));
951 OS << ']';
952 } else if (Node->getNumArgs() == 1) {
953 OS << OpStrings[Kind] << ' ';
954 PrintExpr(Node->getArg(0));
955 } else if (Node->getNumArgs() == 2) {
956 PrintExpr(Node->getArg(0));
957 OS << ' ' << OpStrings[Kind] << ' ';
958 PrintExpr(Node->getArg(1));
959 } else {
960 assert(false && "unknown overloaded operator");
961 }
962}
Reid Spencer5f016e22007-07-11 17:01:13 +0000963
Douglas Gregor88a35142008-12-22 05:46:06 +0000964void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
965 VisitCallExpr(cast<CallExpr>(Node));
966}
967
Douglas Gregor49badde2008-10-27 19:41:14 +0000968void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
969 OS << Node->getCastName() << '<';
970 OS << Node->getTypeAsWritten().getAsString() << ">(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000971 PrintExpr(Node->getSubExpr());
972 OS << ")";
973}
974
Douglas Gregor49badde2008-10-27 19:41:14 +0000975void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
976 VisitCXXNamedCastExpr(Node);
977}
978
979void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
980 VisitCXXNamedCastExpr(Node);
981}
982
983void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
984 VisitCXXNamedCastExpr(Node);
985}
986
987void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
988 VisitCXXNamedCastExpr(Node);
989}
990
Sebastian Redlc42e1182008-11-11 11:37:55 +0000991void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
992 OS << "typeid(";
993 if (Node->isTypeOperand()) {
994 OS << Node->getTypeOperand().getAsString();
995 } else {
996 PrintExpr(Node->getExprOperand());
997 }
998 OS << ")";
999}
1000
Reid Spencer5f016e22007-07-11 17:01:13 +00001001void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1002 OS << (Node->getValue() ? "true" : "false");
1003}
1004
Douglas Gregor796da182008-11-04 14:32:21 +00001005void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1006 OS << "this";
1007}
1008
Chris Lattner50dd2892008-02-26 00:51:44 +00001009void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1010 if (Node->getSubExpr() == 0)
1011 OS << "throw";
1012 else {
1013 OS << "throw ";
1014 PrintExpr(Node->getSubExpr());
1015 }
1016}
1017
Chris Lattner04421082008-04-08 04:40:51 +00001018void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1019 // Nothing to print: we picked up the default argument
1020}
1021
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001022void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1023 OS << Node->getType().getAsString();
1024 OS << "(";
1025 PrintExpr(Node->getSubExpr());
1026 OS << ")";
1027}
1028
Douglas Gregor506ae412009-01-16 18:33:17 +00001029void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1030 OS << Node->getType().getAsString();
1031 OS << "(";
1032 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1033 ArgEnd = Node->arg_end();
1034 Arg != ArgEnd; ++Arg) {
1035 if (Arg != Node->arg_begin())
1036 OS << ", ";
1037 PrintExpr(*Arg);
1038 }
1039 OS << ")";
1040}
1041
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001042void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
1043 OS << Node->getType().getAsString() << "()";
1044}
1045
Argyrios Kyrtzidis9e922b12008-09-09 23:47:53 +00001046void
1047StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
1048 PrintRawDecl(E->getVarDecl());
1049}
1050
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001051void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1052 if (E->isGlobalNew())
1053 OS << "::";
1054 OS << "new ";
1055 unsigned NumPlace = E->getNumPlacementArgs();
1056 if (NumPlace > 0) {
1057 OS << "(";
1058 PrintExpr(E->getPlacementArg(0));
1059 for (unsigned i = 1; i < NumPlace; ++i) {
1060 OS << ", ";
1061 PrintExpr(E->getPlacementArg(i));
1062 }
1063 OS << ") ";
1064 }
1065 if (E->isParenTypeId())
1066 OS << "(";
Sebastian Redl6fec6482008-12-02 22:08:59 +00001067 std::string TypeS;
1068 if (Expr *Size = E->getArraySize()) {
1069 llvm::raw_string_ostream s(TypeS);
1070 Size->printPretty(s);
1071 s.flush();
1072 TypeS = "[" + TypeS + "]";
1073 }
1074 E->getAllocatedType().getAsStringInternal(TypeS);
1075 OS << TypeS;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001076 if (E->isParenTypeId())
1077 OS << ")";
1078
1079 if (E->hasInitializer()) {
1080 OS << "(";
1081 unsigned NumCons = E->getNumConstructorArgs();
1082 if (NumCons > 0) {
1083 PrintExpr(E->getConstructorArg(0));
1084 for (unsigned i = 1; i < NumCons; ++i) {
1085 OS << ", ";
1086 PrintExpr(E->getConstructorArg(i));
1087 }
1088 }
1089 OS << ")";
1090 }
1091}
1092
1093void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1094 if (E->isGlobalDelete())
1095 OS << "::";
1096 OS << "delete ";
1097 if (E->isArrayForm())
1098 OS << "[] ";
1099 PrintExpr(E->getArgument());
1100}
1101
Douglas Gregor17330012009-02-04 15:01:18 +00001102void StmtPrinter::VisitUnresolvedFunctionNameExpr(UnresolvedFunctionNameExpr *E) {
1103 OS << E->getName().getAsString();
Douglas Gregor5c37de72008-12-06 00:22:45 +00001104}
1105
Sebastian Redl64b45f72009-01-05 20:52:13 +00001106static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1107 switch (UTT) {
1108 default: assert(false && "Unknown type trait");
1109 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1110 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1111 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1112 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1113 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1114 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1115 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1116 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1117 case UTT_IsAbstract: return "__is_abstract";
1118 case UTT_IsClass: return "__is_class";
1119 case UTT_IsEmpty: return "__is_empty";
1120 case UTT_IsEnum: return "__is_enum";
1121 case UTT_IsPOD: return "__is_pod";
1122 case UTT_IsPolymorphic: return "__is_polymorphic";
1123 case UTT_IsUnion: return "__is_union";
1124 }
1125}
1126
1127void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1128 OS << getTypeTraitName(E->getTrait()) << "("
1129 << E->getQueriedType().getAsString() << ")";
1130}
1131
Anders Carlsson55085182007-08-21 17:43:55 +00001132// Obj-C
1133
1134void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1135 OS << "@";
1136 VisitStringLiteral(Node->getString());
1137}
Reid Spencer5f016e22007-07-11 17:01:13 +00001138
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001139void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001140 OS << "@encode(" << Node->getEncodedType().getAsString() << ')';
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001141}
1142
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001143void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001144 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001145}
1146
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001147void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001148 OS << "@protocol(" << Node->getProtocol()->getNameAsString() << ')';
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001149}
1150
Steve Naroff563477d2007-09-18 23:55:05 +00001151void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1152 OS << "[";
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001153 Expr *receiver = Mess->getReceiver();
1154 if (receiver) PrintExpr(receiver);
1155 else OS << Mess->getClassName()->getName();
Ted Kremenekc29efd82008-05-02 17:32:38 +00001156 OS << ' ';
Ted Kremenek97b7f262008-04-16 04:30:16 +00001157 Selector selector = Mess->getSelector();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001158 if (selector.isUnarySelector()) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001159 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001160 } else {
1161 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001162 if (i < selector.getNumArgs()) {
1163 if (i > 0) OS << ' ';
1164 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner39f34e92008-11-24 04:00:27 +00001165 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremenekc29efd82008-05-02 17:32:38 +00001166 else
1167 OS << ":";
1168 }
1169 else OS << ", "; // Handle variadic methods.
1170
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001171 PrintExpr(Mess->getArg(i));
1172 }
Steve Naroff563477d2007-09-18 23:55:05 +00001173 }
1174 OS << "]";
1175}
1176
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001177void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) {
1178 OS << "super";
1179}
1180
Steve Naroff4eb206b2008-09-03 18:15:37 +00001181void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001182 BlockDecl *BD = Node->getBlockDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001183 OS << "^";
1184
1185 const FunctionType *AFT = Node->getFunctionType();
1186
Douglas Gregor72564e72009-02-26 23:50:07 +00001187 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001188 OS << "()";
Douglas Gregor72564e72009-02-26 23:50:07 +00001189 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001190 OS << '(';
1191 std::string ParamStr;
Steve Naroff56ee6892008-10-08 17:01:13 +00001192 for (BlockDecl::param_iterator AI = BD->param_begin(),
1193 E = BD->param_end(); AI != E; ++AI) {
1194 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner39f34e92008-11-24 04:00:27 +00001195 ParamStr = (*AI)->getNameAsString();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001196 (*AI)->getType().getAsStringInternal(ParamStr);
1197 OS << ParamStr;
1198 }
1199
Douglas Gregor72564e72009-02-26 23:50:07 +00001200 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001201 if (FT->isVariadic()) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001202 if (!BD->param_empty()) OS << ", ";
Steve Naroff4eb206b2008-09-03 18:15:37 +00001203 OS << "...";
1204 }
1205 OS << ')';
1206 }
1207}
1208
Steve Naroff4eb206b2008-09-03 18:15:37 +00001209void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001210 OS << Node->getDecl()->getNameAsString();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001211}
Reid Spencer5f016e22007-07-11 17:01:13 +00001212//===----------------------------------------------------------------------===//
1213// Stmt method implementations
1214//===----------------------------------------------------------------------===//
1215
Chris Lattner6000dac2007-08-08 22:51:59 +00001216void Stmt::dumpPretty() const {
Daniel Dunbar4a77edb2009-03-10 18:00:19 +00001217 printPretty(llvm::errs());
Reid Spencer5f016e22007-07-11 17:01:13 +00001218}
1219
Mike Stump071e4da2009-02-10 20:16:46 +00001220void Stmt::printPretty(llvm::raw_ostream &OS, PrinterHelper* Helper,
1221 unsigned I, bool NoIndent) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001222 if (this == 0) {
1223 OS << "<NULL>";
1224 return;
1225 }
1226
Mike Stump071e4da2009-02-10 20:16:46 +00001227 StmtPrinter P(OS, Helper, I, NoIndent);
Chris Lattnerc5598cb2007-08-21 04:04:25 +00001228 P.Visit(const_cast<Stmt*>(this));
Reid Spencer5f016e22007-07-11 17:01:13 +00001229}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001230
1231//===----------------------------------------------------------------------===//
1232// PrinterHelper
1233//===----------------------------------------------------------------------===//
1234
1235// Implement virtual destructor.
Gabor Greif84675832007-09-11 15:32:40 +00001236PrinterHelper::~PrinterHelper() {}