blob: cc32fa23d9c223fa9c0db48b2e80234a786cf433 [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!");
122 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 }
128 }
129
Chris Lattner39f34e92008-11-24 04:00:27 +0000130 std::string Name = VD->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 VD->getType().getAsStringInternal(Name);
132 OS << Name;
133
Chris Lattner24c39902007-07-12 00:36:32 +0000134 // If this is a vardecl with an initializer, emit it.
135 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
136 if (V->getInit()) {
137 OS << " = ";
138 PrintExpr(V->getInit());
139 }
140 }
Steve Naroff91578f32007-11-17 21:21:01 +0000141 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
142 // print a free standing tag decl (e.g. "struct x;").
143 OS << TD->getKindName();
144 OS << " ";
145 if (const IdentifierInfo *II = TD->getIdentifier())
146 OS << II->getName();
Mike Stump071e4da2009-02-10 20:16:46 +0000147 if (RecordDecl *RD = dyn_cast<RecordDecl>(TD)) {
148 OS << "{\n";
149 IndentLevel += 1;
150 for (RecordDecl::field_iterator i = RD->field_begin(); i != RD->field_end(); ++i) {
151 PrintFieldDecl(*i);
152 IndentLevel -= 1;
153 }
154 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000155 } else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 assert(0 && "Unexpected decl");
157 }
158}
159
Mike Stump071e4da2009-02-10 20:16:46 +0000160void StmtPrinter::PrintFieldDecl(FieldDecl *FD) {
161 Indent() << FD->getNameAsString() << "\n";
162}
163
Ted Kremenekecd64c52008-10-06 18:39:36 +0000164void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
Mike Stump071e4da2009-02-10 20:16:46 +0000165 bool isFirst = true;
Ted Kremenekecd64c52008-10-06 18:39:36 +0000166
167 for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();
168 I != E; ++I) {
169
170 if (!isFirst) OS << ", ";
171 else isFirst = false;
172
173 PrintRawDecl(*I);
174 }
175}
Reid Spencer5f016e22007-07-11 17:01:13 +0000176
177void StmtPrinter::VisitNullStmt(NullStmt *Node) {
178 Indent() << ";\n";
179}
180
181void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Ted Kremenekecd64c52008-10-06 18:39:36 +0000182 for (DeclStmt::decl_iterator I = Node->decl_begin(), E = Node->decl_end();
183 I!=E; ++I) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000184 Indent();
Ted Kremenekecd64c52008-10-06 18:39:36 +0000185 PrintRawDecl(*I);
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 OS << ";\n";
187 }
188}
189
190void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
191 Indent();
192 PrintRawCompoundStmt(Node);
193 OS << "\n";
194}
195
196void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
197 Indent(-1) << "case ";
198 PrintExpr(Node->getLHS());
199 if (Node->getRHS()) {
200 OS << " ... ";
201 PrintExpr(Node->getRHS());
202 }
203 OS << ":\n";
204
205 PrintStmt(Node->getSubStmt(), 0);
206}
207
208void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
209 Indent(-1) << "default:\n";
210 PrintStmt(Node->getSubStmt(), 0);
211}
212
213void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
214 Indent(-1) << Node->getName() << ":\n";
215 PrintStmt(Node->getSubStmt(), 0);
216}
217
218void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000219 OS << "if (";
Reid Spencer5f016e22007-07-11 17:01:13 +0000220 PrintExpr(If->getCond());
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000221 OS << ')';
Reid Spencer5f016e22007-07-11 17:01:13 +0000222
223 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
224 OS << ' ';
225 PrintRawCompoundStmt(CS);
226 OS << (If->getElse() ? ' ' : '\n');
227 } else {
228 OS << '\n';
229 PrintStmt(If->getThen());
230 if (If->getElse()) Indent();
231 }
232
233 if (Stmt *Else = If->getElse()) {
234 OS << "else";
235
236 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
237 OS << ' ';
238 PrintRawCompoundStmt(CS);
239 OS << '\n';
240 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
241 OS << ' ';
242 PrintRawIfStmt(ElseIf);
243 } else {
244 OS << '\n';
245 PrintStmt(If->getElse());
246 }
247 }
248}
249
250void StmtPrinter::VisitIfStmt(IfStmt *If) {
251 Indent();
252 PrintRawIfStmt(If);
253}
254
255void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
256 Indent() << "switch (";
257 PrintExpr(Node->getCond());
258 OS << ")";
259
260 // Pretty print compoundstmt bodies (very common).
261 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
262 OS << " ";
263 PrintRawCompoundStmt(CS);
264 OS << "\n";
265 } else {
266 OS << "\n";
267 PrintStmt(Node->getBody());
268 }
269}
270
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000271void StmtPrinter::VisitSwitchCase(SwitchCase*) {
272 assert(0 && "SwitchCase is an abstract class");
273}
274
Reid Spencer5f016e22007-07-11 17:01:13 +0000275void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
276 Indent() << "while (";
277 PrintExpr(Node->getCond());
278 OS << ")\n";
279 PrintStmt(Node->getBody());
280}
281
282void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner8bdcc472007-09-15 21:49:37 +0000283 Indent() << "do ";
284 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
285 PrintRawCompoundStmt(CS);
286 OS << " ";
287 } else {
288 OS << "\n";
289 PrintStmt(Node->getBody());
290 Indent();
291 }
292
293 OS << "while ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000294 PrintExpr(Node->getCond());
295 OS << ";\n";
296}
297
298void StmtPrinter::VisitForStmt(ForStmt *Node) {
299 Indent() << "for (";
300 if (Node->getInit()) {
301 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000302 PrintRawDeclStmt(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000303 else
304 PrintExpr(cast<Expr>(Node->getInit()));
305 }
Chris Lattner8bdcc472007-09-15 21:49:37 +0000306 OS << ";";
307 if (Node->getCond()) {
308 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000309 PrintExpr(Node->getCond());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000310 }
311 OS << ";";
312 if (Node->getInc()) {
313 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 PrintExpr(Node->getInc());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000315 }
316 OS << ") ";
317
318 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
319 PrintRawCompoundStmt(CS);
320 OS << "\n";
321 } else {
322 OS << "\n";
323 PrintStmt(Node->getBody());
324 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000325}
326
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000327void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000328 Indent() << "for (";
329 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000330 PrintRawDeclStmt(DS);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000331 else
332 PrintExpr(cast<Expr>(Node->getElement()));
333 OS << " in ";
334 PrintExpr(Node->getCollection());
335 OS << ") ";
336
337 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
338 PrintRawCompoundStmt(CS);
339 OS << "\n";
340 } else {
341 OS << "\n";
342 PrintStmt(Node->getBody());
343 }
344}
345
Reid Spencer5f016e22007-07-11 17:01:13 +0000346void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
347 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
348}
349
350void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
351 Indent() << "goto *";
352 PrintExpr(Node->getTarget());
353 OS << ";\n";
354}
355
356void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
357 Indent() << "continue;\n";
358}
359
360void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
361 Indent() << "break;\n";
362}
363
364
365void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
366 Indent() << "return";
367 if (Node->getRetValue()) {
368 OS << " ";
369 PrintExpr(Node->getRetValue());
370 }
371 OS << ";\n";
372}
373
Chris Lattnerfe795952007-10-29 04:04:16 +0000374
375void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson39c47b52007-11-23 23:12:25 +0000376 Indent() << "asm ";
377
378 if (Node->isVolatile())
379 OS << "volatile ";
380
381 OS << "(";
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000382 VisitStringLiteral(Node->getAsmString());
Anders Carlssonb235fc22007-11-22 01:36:19 +0000383
384 // Outputs
385 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
386 Node->getNumClobbers() != 0)
387 OS << " : ";
388
389 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
390 if (i != 0)
391 OS << ", ";
392
393 if (!Node->getOutputName(i).empty()) {
394 OS << '[';
395 OS << Node->getOutputName(i);
396 OS << "] ";
397 }
398
399 VisitStringLiteral(Node->getOutputConstraint(i));
400 OS << " ";
401 Visit(Node->getOutputExpr(i));
402 }
403
404 // Inputs
405 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
406 OS << " : ";
407
408 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
409 if (i != 0)
410 OS << ", ";
411
412 if (!Node->getInputName(i).empty()) {
413 OS << '[';
414 OS << Node->getInputName(i);
415 OS << "] ";
416 }
417
418 VisitStringLiteral(Node->getInputConstraint(i));
419 OS << " ";
420 Visit(Node->getInputExpr(i));
421 }
422
423 // Clobbers
424 if (Node->getNumClobbers() != 0)
425 OS << " : ";
426
427 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
428 if (i != 0)
429 OS << ", ";
430
431 VisitStringLiteral(Node->getClobber(i));
432 }
433
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000434 OS << ");\n";
Chris Lattnerfe795952007-10-29 04:04:16 +0000435}
436
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000437void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000438 Indent() << "@try";
439 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
440 PrintRawCompoundStmt(TS);
441 OS << "\n";
442 }
443
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000444 for (ObjCAtCatchStmt *catchStmt =
445 static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts());
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000446 catchStmt;
447 catchStmt =
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000448 static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000449 Indent() << "@catch(";
450 if (catchStmt->getCatchParamStmt()) {
451 if (DeclStmt *DS = dyn_cast<DeclStmt>(catchStmt->getCatchParamStmt()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000452 PrintRawDeclStmt(DS);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000453 }
454 OS << ")";
455 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody()))
456 {
457 PrintRawCompoundStmt(CS);
458 OS << "\n";
459 }
460 }
461
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000462 if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>(
Fariborz Jahanian1e7eab42007-11-07 00:46:42 +0000463 Node->getFinallyStmt())) {
464 Indent() << "@finally";
465 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000466 OS << "\n";
467 }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000468}
469
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000470void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000471}
472
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000473void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000474 Indent() << "@catch (...) { /* todo */ } \n";
475}
476
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000477void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000478 Indent() << "@throw";
479 if (Node->getThrowExpr()) {
480 OS << " ";
481 PrintExpr(Node->getThrowExpr());
482 }
483 OS << ";\n";
484}
485
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000486void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000487 Indent() << "@synchronized (";
488 PrintExpr(Node->getSynchExpr());
489 OS << ")";
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000490 PrintRawCompoundStmt(Node->getSynchBody());
491 OS << "\n";
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000492}
493
Sebastian Redl8351da02008-12-22 21:35:02 +0000494void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
495 OS << "catch (";
Sebastian Redl4b07b292008-12-22 19:15:10 +0000496 if (Decl *ExDecl = Node->getExceptionDecl())
497 PrintRawDecl(ExDecl);
498 else
499 OS << "...";
500 OS << ") ";
501 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl8351da02008-12-22 21:35:02 +0000502}
503
504void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
505 Indent();
506 PrintRawCXXCatchStmt(Node);
507 OS << "\n";
508}
509
510void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
511 Indent() << "try ";
512 PrintRawCompoundStmt(Node->getTryBlock());
513 for(unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
514 OS << " ";
515 PrintRawCXXCatchStmt(Node->getHandler(i));
516 }
Sebastian Redl4b07b292008-12-22 19:15:10 +0000517 OS << "\n";
518}
519
Reid Spencer5f016e22007-07-11 17:01:13 +0000520//===----------------------------------------------------------------------===//
521// Expr printing methods.
522//===----------------------------------------------------------------------===//
523
524void StmtPrinter::VisitExpr(Expr *Node) {
525 OS << "<<unknown expr type>>";
526}
527
528void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +0000529 OS << Node->getDecl()->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000530}
531
Douglas Gregor1a49af92009-01-06 05:10:23 +0000532void StmtPrinter::VisitQualifiedDeclRefExpr(QualifiedDeclRefExpr *Node) {
533 // FIXME: Should we keep enough information in QualifiedDeclRefExpr
534 // to produce the same qualification that the user wrote?
535 llvm::SmallVector<DeclContext *, 4> Contexts;
536
537 NamedDecl *D = Node->getDecl();
538
539 // Build up a stack of contexts.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000540 DeclContext *Ctx = D->getDeclContext();
Douglas Gregor1a49af92009-01-06 05:10:23 +0000541 for (; Ctx; Ctx = Ctx->getParent())
542 if (!Ctx->isTransparentContext())
543 Contexts.push_back(Ctx);
544
545 while (!Contexts.empty()) {
546 DeclContext *Ctx = Contexts.back();
547 if (isa<TranslationUnitDecl>(Ctx))
548 OS << "::";
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000549 else if (NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
550 OS << ND->getNameAsString() << "::";
Douglas Gregor1a49af92009-01-06 05:10:23 +0000551 Contexts.pop_back();
552 }
553
554 OS << D->getNameAsString();
555}
556
Steve Naroff7779db42007-11-12 14:29:37 +0000557void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000558 if (Node->getBase()) {
559 PrintExpr(Node->getBase());
560 OS << (Node->isArrow() ? "->" : ".");
561 }
Chris Lattner39f34e92008-11-24 04:00:27 +0000562 OS << Node->getDecl()->getNameAsString();
Steve Naroff7779db42007-11-12 14:29:37 +0000563}
564
Steve Naroffae784072008-05-30 00:40:33 +0000565void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
566 if (Node->getBase()) {
567 PrintExpr(Node->getBase());
568 OS << ".";
569 }
Steve Naroffc77a6362008-12-04 16:24:46 +0000570 OS << Node->getProperty()->getNameAsCString();
Steve Naroffae784072008-05-30 00:40:33 +0000571}
572
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000573void StmtPrinter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node) {
574 if (Node->getBase()) {
575 PrintExpr(Node->getBase());
576 OS << ".";
577 }
578 // FIXME: Setter/Getter names
579}
580
Chris Lattnerd9f69102008-08-10 01:53:14 +0000581void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson22742662007-07-21 05:21:51 +0000582 switch (Node->getIdentType()) {
583 default:
584 assert(0 && "unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000585 case PredefinedExpr::Func:
Anders Carlsson22742662007-07-21 05:21:51 +0000586 OS << "__func__";
587 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000588 case PredefinedExpr::Function:
Anders Carlsson22742662007-07-21 05:21:51 +0000589 OS << "__FUNCTION__";
590 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000591 case PredefinedExpr::PrettyFunction:
Anders Carlsson22742662007-07-21 05:21:51 +0000592 OS << "__PRETTY_FUNCTION__";
593 break;
594 }
595}
596
Reid Spencer5f016e22007-07-11 17:01:13 +0000597void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000598 unsigned value = Node->getValue();
Chris Lattnerc250aae2008-06-07 22:35:38 +0000599 if (Node->isWide())
600 OS << "L";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000601 switch (value) {
602 case '\\':
603 OS << "'\\\\'";
604 break;
605 case '\'':
606 OS << "'\\''";
607 break;
608 case '\a':
609 // TODO: K&R: the meaning of '\\a' is different in traditional C
610 OS << "'\\a'";
611 break;
612 case '\b':
613 OS << "'\\b'";
614 break;
615 // Nonstandard escape sequence.
616 /*case '\e':
617 OS << "'\\e'";
618 break;*/
619 case '\f':
620 OS << "'\\f'";
621 break;
622 case '\n':
623 OS << "'\\n'";
624 break;
625 case '\r':
626 OS << "'\\r'";
627 break;
628 case '\t':
629 OS << "'\\t'";
630 break;
631 case '\v':
632 OS << "'\\v'";
633 break;
634 default:
Ted Kremenek471733d2008-02-23 00:52:04 +0000635 if (value < 256 && isprint(value)) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000636 OS << "'" << (char)value << "'";
637 } else if (value < 256) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000638 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000639 } else {
640 // FIXME what to really do here?
641 OS << value;
642 }
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000643 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000644}
645
646void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
647 bool isSigned = Node->getType()->isSignedIntegerType();
648 OS << Node->getValue().toString(10, isSigned);
649
650 // Emit suffixes. Integer literals are always a builtin integer type.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000651 switch (Node->getType()->getAsBuiltinType()->getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000652 default: assert(0 && "Unexpected type for integer literal!");
653 case BuiltinType::Int: break; // no suffix.
654 case BuiltinType::UInt: OS << 'U'; break;
655 case BuiltinType::Long: OS << 'L'; break;
656 case BuiltinType::ULong: OS << "UL"; break;
657 case BuiltinType::LongLong: OS << "LL"; break;
658 case BuiltinType::ULongLong: OS << "ULL"; break;
659 }
660}
661void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner86e499d2007-08-01 00:23:58 +0000662 // FIXME: print value more precisely.
Chris Lattnerda8249e2008-06-07 22:13:43 +0000663 OS << Node->getValueAsApproximateDouble();
Reid Spencer5f016e22007-07-11 17:01:13 +0000664}
Chris Lattner5d661452007-08-26 03:42:43 +0000665
666void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
667 PrintExpr(Node->getSubExpr());
668 OS << "i";
669}
670
Reid Spencer5f016e22007-07-11 17:01:13 +0000671void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
672 if (Str->isWide()) OS << 'L';
673 OS << '"';
Anders Carlssonee98ac52007-10-15 02:50:23 +0000674
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 // FIXME: this doesn't print wstrings right.
676 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattner9a81c872009-01-16 19:25:18 +0000677 unsigned char Char = Str->getStrData()[i];
678
679 switch (Char) {
680 default:
681 if (isprint(Char))
682 OS << (char)Char;
683 else // Output anything hard as an octal escape.
684 OS << '\\'
685 << (char)('0'+ ((Char >> 6) & 7))
686 << (char)('0'+ ((Char >> 3) & 7))
687 << (char)('0'+ ((Char >> 0) & 7));
688 break;
689 // Handle some common non-printable cases to make dumps prettier.
Reid Spencer5f016e22007-07-11 17:01:13 +0000690 case '\\': OS << "\\\\"; break;
691 case '"': OS << "\\\""; break;
692 case '\n': OS << "\\n"; break;
693 case '\t': OS << "\\t"; break;
694 case '\a': OS << "\\a"; break;
695 case '\b': OS << "\\b"; break;
696 }
697 }
698 OS << '"';
699}
700void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
701 OS << "(";
702 PrintExpr(Node->getSubExpr());
703 OS << ")";
704}
705void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +0000706 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000707 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner296bf192007-08-23 21:46:40 +0000708
Sebastian Redl05189992008-11-11 17:56:53 +0000709 // Print a space if this is an "identifier operator" like __real.
Chris Lattner296bf192007-08-23 21:46:40 +0000710 switch (Node->getOpcode()) {
711 default: break;
Chris Lattner296bf192007-08-23 21:46:40 +0000712 case UnaryOperator::Real:
713 case UnaryOperator::Imag:
714 case UnaryOperator::Extension:
715 OS << ' ';
716 break;
717 }
718 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000719 PrintExpr(Node->getSubExpr());
720
721 if (Node->isPostfix())
722 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +0000723}
Chris Lattner704fe352007-08-30 17:59:59 +0000724
725bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
726 if (isa<CompoundLiteralExpr>(E)) {
727 // Base case, print the type and comma.
728 OS << E->getType().getAsString() << ", ";
729 return true;
730 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
731 PrintOffsetOfDesignator(ASE->getLHS());
732 OS << "[";
733 PrintExpr(ASE->getRHS());
734 OS << "]";
735 return false;
736 } else {
737 MemberExpr *ME = cast<MemberExpr>(E);
738 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
Chris Lattner39f34e92008-11-24 04:00:27 +0000739 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getNameAsString();
Chris Lattner704fe352007-08-30 17:59:59 +0000740 return false;
741 }
742}
743
744void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
745 OS << "__builtin_offsetof(";
746 PrintOffsetOfDesignator(Node->getSubExpr());
747 OS << ")";
748}
749
Sebastian Redl05189992008-11-11 17:56:53 +0000750void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
751 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
752 if (Node->isArgumentType())
753 OS << "(" << Node->getArgumentType().getAsString() << ")";
754 else {
755 OS << " ";
756 PrintExpr(Node->getArgumentExpr());
757 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000758}
759void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +0000760 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000761 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +0000762 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000763 OS << "]";
764}
765
766void StmtPrinter::VisitCallExpr(CallExpr *Call) {
767 PrintExpr(Call->getCallee());
768 OS << "(";
769 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattner04421082008-04-08 04:40:51 +0000770 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
771 // Don't print any defaulted arguments
772 break;
773 }
774
Reid Spencer5f016e22007-07-11 17:01:13 +0000775 if (i) OS << ", ";
776 PrintExpr(Call->getArg(i));
777 }
778 OS << ")";
779}
780void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregorb3eef682009-01-08 22:45:41 +0000781 // FIXME: Suppress printing implicit bases (like "this")
782 PrintExpr(Node->getBase());
783 OS << (Node->isArrow() ? "->" : ".");
784 // FIXME: Suppress printing references to unnamed objects
785 // representing anonymous unions/structs
Douglas Gregor86f19402008-12-20 23:49:58 +0000786 OS << Node->getMemberDecl()->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000787}
Nate Begeman213541a2008-04-18 23:10:10 +0000788void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +0000789 PrintExpr(Node->getBase());
790 OS << ".";
791 OS << Node->getAccessor().getName();
792}
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000793void StmtPrinter::VisitCastExpr(CastExpr *) {
794 assert(0 && "CastExpr is an abstract class");
795}
Douglas Gregor49badde2008-10-27 19:41:14 +0000796void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *) {
797 assert(0 && "ExplicitCastExpr is an abstract class");
798}
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000799void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000800 OS << "(" << Node->getType().getAsString() << ")";
Reid Spencer5f016e22007-07-11 17:01:13 +0000801 PrintExpr(Node->getSubExpr());
802}
Steve Naroffaff1edd2007-07-19 21:32:11 +0000803void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
804 OS << "(" << Node->getType().getAsString() << ")";
805 PrintExpr(Node->getInitializer());
806}
Steve Naroff49b45262007-07-13 16:58:59 +0000807void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroff90045e82007-07-13 23:32:42 +0000808 // No need to print anything, simply forward to the sub expression.
809 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +0000810}
Reid Spencer5f016e22007-07-11 17:01:13 +0000811void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
812 PrintExpr(Node->getLHS());
813 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
814 PrintExpr(Node->getRHS());
815}
Chris Lattnereb14fe82007-08-25 02:00:02 +0000816void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
817 PrintExpr(Node->getLHS());
818 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
819 PrintExpr(Node->getRHS());
820}
Reid Spencer5f016e22007-07-11 17:01:13 +0000821void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
822 PrintExpr(Node->getCond());
Ted Kremenek8e911c42007-11-26 18:27:54 +0000823
824 if (Node->getLHS()) {
825 OS << " ? ";
826 PrintExpr(Node->getLHS());
827 OS << " : ";
828 }
829 else { // Handle GCC extention where LHS can be NULL.
830 OS << " ?: ";
831 }
832
Reid Spencer5f016e22007-07-11 17:01:13 +0000833 PrintExpr(Node->getRHS());
834}
835
836// GNU extensions.
837
Chris Lattner6481a572007-08-03 17:31:20 +0000838void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000839 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000840}
841
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000842void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
843 OS << "(";
844 PrintRawCompoundStmt(E->getSubStmt());
845 OS << ")";
846}
847
Steve Naroffd34e9152007-08-01 22:05:33 +0000848void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
849 OS << "__builtin_types_compatible_p(";
850 OS << Node->getArgType1().getAsString() << ",";
851 OS << Node->getArgType2().getAsString() << ")";
852}
853
Steve Naroffd04fdd52007-08-03 21:21:27 +0000854void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
855 OS << "__builtin_choose_expr(";
856 PrintExpr(Node->getCond());
Chris Lattner94f05e32007-08-04 00:20:15 +0000857 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000858 PrintExpr(Node->getLHS());
Chris Lattner94f05e32007-08-04 00:20:15 +0000859 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000860 PrintExpr(Node->getRHS());
861 OS << ")";
862}
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000863
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000864void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
865 OS << "__null";
866}
867
Nate Begemane2ce1d92008-01-17 17:46:27 +0000868void StmtPrinter::VisitOverloadExpr(OverloadExpr *Node) {
869 OS << "__builtin_overload(";
Nate Begeman67295d02008-01-30 20:50:20 +0000870 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
Nate Begemane2ce1d92008-01-17 17:46:27 +0000871 if (i) OS << ", ";
Nate Begeman67295d02008-01-30 20:50:20 +0000872 PrintExpr(Node->getExpr(i));
Nate Begemane2ce1d92008-01-17 17:46:27 +0000873 }
874 OS << ")";
875}
876
Eli Friedmand38617c2008-05-14 19:38:39 +0000877void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
878 OS << "__builtin_shufflevector(";
879 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
880 if (i) OS << ", ";
881 PrintExpr(Node->getExpr(i));
882 }
883 OS << ")";
884}
885
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000886void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
887 OS << "{ ";
888 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
889 if (i) OS << ", ";
Douglas Gregor4c678342009-01-28 21:54:33 +0000890 if (Node->getInit(i))
891 PrintExpr(Node->getInit(i));
892 else
893 OS << "0";
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000894 }
895 OS << " }";
896}
897
Douglas Gregor05c13a32009-01-22 00:58:24 +0000898void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000899 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
900 DEnd = Node->designators_end();
901 D != DEnd; ++D) {
902 if (D->isFieldDesignator()) {
903 if (D->getDotLoc().isInvalid())
904 OS << D->getFieldName()->getName() << ":";
905 else
906 OS << "." << D->getFieldName()->getName();
907 } else {
908 OS << "[";
909 if (D->isArrayDesignator()) {
910 PrintExpr(Node->getArrayIndex(*D));
911 } else {
912 PrintExpr(Node->getArrayRangeStart(*D));
913 OS << " ... ";
914 PrintExpr(Node->getArrayRangeEnd(*D));
915 }
916 OS << "]";
917 }
918 }
919
920 OS << " = ";
921 PrintExpr(Node->getInit());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000922}
923
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000924void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
925 OS << "/*implicit*/" << Node->getType().getAsString() << "()";
926}
927
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000928void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
929 OS << "va_arg(";
930 PrintExpr(Node->getSubExpr());
931 OS << ", ";
932 OS << Node->getType().getAsString();
933 OS << ")";
934}
935
Reid Spencer5f016e22007-07-11 17:01:13 +0000936// C++
Douglas Gregorb4609802008-11-14 16:09:21 +0000937void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
938 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
939 "",
940#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
941 Spelling,
942#include "clang/Basic/OperatorKinds.def"
943 };
944
945 OverloadedOperatorKind Kind = Node->getOperator();
946 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
947 if (Node->getNumArgs() == 1) {
948 OS << OpStrings[Kind] << ' ';
949 PrintExpr(Node->getArg(0));
950 } else {
951 PrintExpr(Node->getArg(0));
952 OS << ' ' << OpStrings[Kind];
953 }
954 } else if (Kind == OO_Call) {
955 PrintExpr(Node->getArg(0));
956 OS << '(';
957 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
958 if (ArgIdx > 1)
959 OS << ", ";
960 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
961 PrintExpr(Node->getArg(ArgIdx));
962 }
963 OS << ')';
964 } else if (Kind == OO_Subscript) {
965 PrintExpr(Node->getArg(0));
966 OS << '[';
967 PrintExpr(Node->getArg(1));
968 OS << ']';
969 } else if (Node->getNumArgs() == 1) {
970 OS << OpStrings[Kind] << ' ';
971 PrintExpr(Node->getArg(0));
972 } else if (Node->getNumArgs() == 2) {
973 PrintExpr(Node->getArg(0));
974 OS << ' ' << OpStrings[Kind] << ' ';
975 PrintExpr(Node->getArg(1));
976 } else {
977 assert(false && "unknown overloaded operator");
978 }
979}
Reid Spencer5f016e22007-07-11 17:01:13 +0000980
Douglas Gregor88a35142008-12-22 05:46:06 +0000981void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
982 VisitCallExpr(cast<CallExpr>(Node));
983}
984
Douglas Gregor49badde2008-10-27 19:41:14 +0000985void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
986 OS << Node->getCastName() << '<';
987 OS << Node->getTypeAsWritten().getAsString() << ">(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000988 PrintExpr(Node->getSubExpr());
989 OS << ")";
990}
991
Douglas Gregor49badde2008-10-27 19:41:14 +0000992void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
993 VisitCXXNamedCastExpr(Node);
994}
995
996void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
997 VisitCXXNamedCastExpr(Node);
998}
999
1000void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1001 VisitCXXNamedCastExpr(Node);
1002}
1003
1004void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1005 VisitCXXNamedCastExpr(Node);
1006}
1007
Sebastian Redlc42e1182008-11-11 11:37:55 +00001008void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1009 OS << "typeid(";
1010 if (Node->isTypeOperand()) {
1011 OS << Node->getTypeOperand().getAsString();
1012 } else {
1013 PrintExpr(Node->getExprOperand());
1014 }
1015 OS << ")";
1016}
1017
Reid Spencer5f016e22007-07-11 17:01:13 +00001018void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1019 OS << (Node->getValue() ? "true" : "false");
1020}
1021
Douglas Gregor796da182008-11-04 14:32:21 +00001022void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1023 OS << "this";
1024}
1025
Chris Lattner50dd2892008-02-26 00:51:44 +00001026void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1027 if (Node->getSubExpr() == 0)
1028 OS << "throw";
1029 else {
1030 OS << "throw ";
1031 PrintExpr(Node->getSubExpr());
1032 }
1033}
1034
Chris Lattner04421082008-04-08 04:40:51 +00001035void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1036 // Nothing to print: we picked up the default argument
1037}
1038
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001039void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1040 OS << Node->getType().getAsString();
1041 OS << "(";
1042 PrintExpr(Node->getSubExpr());
1043 OS << ")";
1044}
1045
Douglas Gregor506ae412009-01-16 18:33:17 +00001046void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1047 OS << Node->getType().getAsString();
1048 OS << "(";
1049 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1050 ArgEnd = Node->arg_end();
1051 Arg != ArgEnd; ++Arg) {
1052 if (Arg != Node->arg_begin())
1053 OS << ", ";
1054 PrintExpr(*Arg);
1055 }
1056 OS << ")";
1057}
1058
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001059void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
1060 OS << Node->getType().getAsString() << "()";
1061}
1062
Argyrios Kyrtzidis9e922b12008-09-09 23:47:53 +00001063void
1064StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
1065 PrintRawDecl(E->getVarDecl());
1066}
1067
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001068void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1069 if (E->isGlobalNew())
1070 OS << "::";
1071 OS << "new ";
1072 unsigned NumPlace = E->getNumPlacementArgs();
1073 if (NumPlace > 0) {
1074 OS << "(";
1075 PrintExpr(E->getPlacementArg(0));
1076 for (unsigned i = 1; i < NumPlace; ++i) {
1077 OS << ", ";
1078 PrintExpr(E->getPlacementArg(i));
1079 }
1080 OS << ") ";
1081 }
1082 if (E->isParenTypeId())
1083 OS << "(";
Sebastian Redl6fec6482008-12-02 22:08:59 +00001084 std::string TypeS;
1085 if (Expr *Size = E->getArraySize()) {
1086 llvm::raw_string_ostream s(TypeS);
1087 Size->printPretty(s);
1088 s.flush();
1089 TypeS = "[" + TypeS + "]";
1090 }
1091 E->getAllocatedType().getAsStringInternal(TypeS);
1092 OS << TypeS;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001093 if (E->isParenTypeId())
1094 OS << ")";
1095
1096 if (E->hasInitializer()) {
1097 OS << "(";
1098 unsigned NumCons = E->getNumConstructorArgs();
1099 if (NumCons > 0) {
1100 PrintExpr(E->getConstructorArg(0));
1101 for (unsigned i = 1; i < NumCons; ++i) {
1102 OS << ", ";
1103 PrintExpr(E->getConstructorArg(i));
1104 }
1105 }
1106 OS << ")";
1107 }
1108}
1109
1110void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1111 if (E->isGlobalDelete())
1112 OS << "::";
1113 OS << "delete ";
1114 if (E->isArrayForm())
1115 OS << "[] ";
1116 PrintExpr(E->getArgument());
1117}
1118
Douglas Gregor17330012009-02-04 15:01:18 +00001119void StmtPrinter::VisitUnresolvedFunctionNameExpr(UnresolvedFunctionNameExpr *E) {
1120 OS << E->getName().getAsString();
Douglas Gregor5c37de72008-12-06 00:22:45 +00001121}
1122
Sebastian Redl64b45f72009-01-05 20:52:13 +00001123static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1124 switch (UTT) {
1125 default: assert(false && "Unknown type trait");
1126 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1127 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1128 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1129 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1130 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1131 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1132 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1133 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1134 case UTT_IsAbstract: return "__is_abstract";
1135 case UTT_IsClass: return "__is_class";
1136 case UTT_IsEmpty: return "__is_empty";
1137 case UTT_IsEnum: return "__is_enum";
1138 case UTT_IsPOD: return "__is_pod";
1139 case UTT_IsPolymorphic: return "__is_polymorphic";
1140 case UTT_IsUnion: return "__is_union";
1141 }
1142}
1143
1144void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1145 OS << getTypeTraitName(E->getTrait()) << "("
1146 << E->getQueriedType().getAsString() << ")";
1147}
1148
Anders Carlsson55085182007-08-21 17:43:55 +00001149// Obj-C
1150
1151void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1152 OS << "@";
1153 VisitStringLiteral(Node->getString());
1154}
Reid Spencer5f016e22007-07-11 17:01:13 +00001155
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001156void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001157 OS << "@encode(" << Node->getEncodedType().getAsString() << ')';
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001158}
1159
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001160void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001161 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001162}
1163
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001164void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001165 OS << "@protocol(" << Node->getProtocol()->getNameAsString() << ')';
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001166}
1167
Steve Naroff563477d2007-09-18 23:55:05 +00001168void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1169 OS << "[";
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001170 Expr *receiver = Mess->getReceiver();
1171 if (receiver) PrintExpr(receiver);
1172 else OS << Mess->getClassName()->getName();
Ted Kremenekc29efd82008-05-02 17:32:38 +00001173 OS << ' ';
Ted Kremenek97b7f262008-04-16 04:30:16 +00001174 Selector selector = Mess->getSelector();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001175 if (selector.isUnarySelector()) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001176 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001177 } else {
1178 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001179 if (i < selector.getNumArgs()) {
1180 if (i > 0) OS << ' ';
1181 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner39f34e92008-11-24 04:00:27 +00001182 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremenekc29efd82008-05-02 17:32:38 +00001183 else
1184 OS << ":";
1185 }
1186 else OS << ", "; // Handle variadic methods.
1187
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001188 PrintExpr(Mess->getArg(i));
1189 }
Steve Naroff563477d2007-09-18 23:55:05 +00001190 }
1191 OS << "]";
1192}
1193
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001194void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) {
1195 OS << "super";
1196}
1197
Steve Naroff4eb206b2008-09-03 18:15:37 +00001198void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001199 BlockDecl *BD = Node->getBlockDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001200 OS << "^";
1201
1202 const FunctionType *AFT = Node->getFunctionType();
1203
1204 if (isa<FunctionTypeNoProto>(AFT)) {
1205 OS << "()";
Steve Naroff56ee6892008-10-08 17:01:13 +00001206 } else if (!BD->param_empty() || cast<FunctionTypeProto>(AFT)->isVariadic()) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001207 OS << '(';
1208 std::string ParamStr;
Steve Naroff56ee6892008-10-08 17:01:13 +00001209 for (BlockDecl::param_iterator AI = BD->param_begin(),
1210 E = BD->param_end(); AI != E; ++AI) {
1211 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner39f34e92008-11-24 04:00:27 +00001212 ParamStr = (*AI)->getNameAsString();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001213 (*AI)->getType().getAsStringInternal(ParamStr);
1214 OS << ParamStr;
1215 }
1216
Steve Naroff56ee6892008-10-08 17:01:13 +00001217 const FunctionTypeProto *FT = cast<FunctionTypeProto>(AFT);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001218 if (FT->isVariadic()) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001219 if (!BD->param_empty()) OS << ", ";
Steve Naroff4eb206b2008-09-03 18:15:37 +00001220 OS << "...";
1221 }
1222 OS << ')';
1223 }
1224}
1225
Steve Naroff4eb206b2008-09-03 18:15:37 +00001226void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001227 OS << Node->getDecl()->getNameAsString();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001228}
Reid Spencer5f016e22007-07-11 17:01:13 +00001229//===----------------------------------------------------------------------===//
1230// Stmt method implementations
1231//===----------------------------------------------------------------------===//
1232
Chris Lattner6000dac2007-08-08 22:51:59 +00001233void Stmt::dumpPretty() const {
Douglas Gregor4c678342009-01-28 21:54:33 +00001234 llvm::raw_ostream &OS = llvm::errs();
1235 printPretty(OS);
1236 OS.flush();
Reid Spencer5f016e22007-07-11 17:01:13 +00001237}
1238
Mike Stump071e4da2009-02-10 20:16:46 +00001239void Stmt::printPretty(llvm::raw_ostream &OS, PrinterHelper* Helper,
1240 unsigned I, bool NoIndent) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001241 if (this == 0) {
1242 OS << "<NULL>";
1243 return;
1244 }
1245
Mike Stump071e4da2009-02-10 20:16:46 +00001246 StmtPrinter P(OS, Helper, I, NoIndent);
Chris Lattnerc5598cb2007-08-21 04:04:25 +00001247 P.Visit(const_cast<Stmt*>(this));
Reid Spencer5f016e22007-07-11 17:01:13 +00001248}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001249
1250//===----------------------------------------------------------------------===//
1251// PrinterHelper
1252//===----------------------------------------------------------------------===//
1253
1254// Implement virtual destructor.
Gabor Greif84675832007-09-11 15:32:40 +00001255PrinterHelper::~PrinterHelper() {}