blob: 7e3180d22814c9e4597a2daa076d7e93793a8e7b [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;
151 for (RecordDecl::field_iterator i = RD->field_begin(); i != RD->field_end(); ++i) {
152 PrintFieldDecl(*i);
153 IndentLevel -= 1;
154 }
155 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 } else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 assert(0 && "Unexpected decl");
158 }
159}
160
Mike Stump071e4da2009-02-10 20:16:46 +0000161void StmtPrinter::PrintFieldDecl(FieldDecl *FD) {
162 Indent() << FD->getNameAsString() << "\n";
163}
164
Ted Kremenekecd64c52008-10-06 18:39:36 +0000165void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
Mike Stump071e4da2009-02-10 20:16:46 +0000166 bool isFirst = true;
Ted Kremenekecd64c52008-10-06 18:39:36 +0000167
168 for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();
169 I != E; ++I) {
170
171 if (!isFirst) OS << ", ";
172 else isFirst = false;
173
174 PrintRawDecl(*I);
175 }
176}
Reid Spencer5f016e22007-07-11 17:01:13 +0000177
178void StmtPrinter::VisitNullStmt(NullStmt *Node) {
179 Indent() << ";\n";
180}
181
182void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Ted Kremenekecd64c52008-10-06 18:39:36 +0000183 for (DeclStmt::decl_iterator I = Node->decl_begin(), E = Node->decl_end();
184 I!=E; ++I) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 Indent();
Ted Kremenekecd64c52008-10-06 18:39:36 +0000186 PrintRawDecl(*I);
Reid Spencer5f016e22007-07-11 17:01:13 +0000187 OS << ";\n";
188 }
189}
190
191void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
192 Indent();
193 PrintRawCompoundStmt(Node);
194 OS << "\n";
195}
196
197void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
198 Indent(-1) << "case ";
199 PrintExpr(Node->getLHS());
200 if (Node->getRHS()) {
201 OS << " ... ";
202 PrintExpr(Node->getRHS());
203 }
204 OS << ":\n";
205
206 PrintStmt(Node->getSubStmt(), 0);
207}
208
209void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
210 Indent(-1) << "default:\n";
211 PrintStmt(Node->getSubStmt(), 0);
212}
213
214void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
215 Indent(-1) << Node->getName() << ":\n";
216 PrintStmt(Node->getSubStmt(), 0);
217}
218
219void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000220 OS << "if (";
Reid Spencer5f016e22007-07-11 17:01:13 +0000221 PrintExpr(If->getCond());
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000222 OS << ')';
Reid Spencer5f016e22007-07-11 17:01:13 +0000223
224 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
225 OS << ' ';
226 PrintRawCompoundStmt(CS);
227 OS << (If->getElse() ? ' ' : '\n');
228 } else {
229 OS << '\n';
230 PrintStmt(If->getThen());
231 if (If->getElse()) Indent();
232 }
233
234 if (Stmt *Else = If->getElse()) {
235 OS << "else";
236
237 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
238 OS << ' ';
239 PrintRawCompoundStmt(CS);
240 OS << '\n';
241 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
242 OS << ' ';
243 PrintRawIfStmt(ElseIf);
244 } else {
245 OS << '\n';
246 PrintStmt(If->getElse());
247 }
248 }
249}
250
251void StmtPrinter::VisitIfStmt(IfStmt *If) {
252 Indent();
253 PrintRawIfStmt(If);
254}
255
256void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
257 Indent() << "switch (";
258 PrintExpr(Node->getCond());
259 OS << ")";
260
261 // Pretty print compoundstmt bodies (very common).
262 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
263 OS << " ";
264 PrintRawCompoundStmt(CS);
265 OS << "\n";
266 } else {
267 OS << "\n";
268 PrintStmt(Node->getBody());
269 }
270}
271
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000272void StmtPrinter::VisitSwitchCase(SwitchCase*) {
273 assert(0 && "SwitchCase is an abstract class");
274}
275
Reid Spencer5f016e22007-07-11 17:01:13 +0000276void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
277 Indent() << "while (";
278 PrintExpr(Node->getCond());
279 OS << ")\n";
280 PrintStmt(Node->getBody());
281}
282
283void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner8bdcc472007-09-15 21:49:37 +0000284 Indent() << "do ";
285 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
286 PrintRawCompoundStmt(CS);
287 OS << " ";
288 } else {
289 OS << "\n";
290 PrintStmt(Node->getBody());
291 Indent();
292 }
293
294 OS << "while ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 PrintExpr(Node->getCond());
296 OS << ";\n";
297}
298
299void StmtPrinter::VisitForStmt(ForStmt *Node) {
300 Indent() << "for (";
301 if (Node->getInit()) {
302 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000303 PrintRawDeclStmt(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000304 else
305 PrintExpr(cast<Expr>(Node->getInit()));
306 }
Chris Lattner8bdcc472007-09-15 21:49:37 +0000307 OS << ";";
308 if (Node->getCond()) {
309 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000310 PrintExpr(Node->getCond());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000311 }
312 OS << ";";
313 if (Node->getInc()) {
314 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000315 PrintExpr(Node->getInc());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000316 }
317 OS << ") ";
318
319 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
320 PrintRawCompoundStmt(CS);
321 OS << "\n";
322 } else {
323 OS << "\n";
324 PrintStmt(Node->getBody());
325 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000326}
327
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000328void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000329 Indent() << "for (";
330 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000331 PrintRawDeclStmt(DS);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000332 else
333 PrintExpr(cast<Expr>(Node->getElement()));
334 OS << " in ";
335 PrintExpr(Node->getCollection());
336 OS << ") ";
337
338 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
339 PrintRawCompoundStmt(CS);
340 OS << "\n";
341 } else {
342 OS << "\n";
343 PrintStmt(Node->getBody());
344 }
345}
346
Reid Spencer5f016e22007-07-11 17:01:13 +0000347void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
348 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
349}
350
351void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
352 Indent() << "goto *";
353 PrintExpr(Node->getTarget());
354 OS << ";\n";
355}
356
357void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
358 Indent() << "continue;\n";
359}
360
361void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
362 Indent() << "break;\n";
363}
364
365
366void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
367 Indent() << "return";
368 if (Node->getRetValue()) {
369 OS << " ";
370 PrintExpr(Node->getRetValue());
371 }
372 OS << ";\n";
373}
374
Chris Lattnerfe795952007-10-29 04:04:16 +0000375
376void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson39c47b52007-11-23 23:12:25 +0000377 Indent() << "asm ";
378
379 if (Node->isVolatile())
380 OS << "volatile ";
381
382 OS << "(";
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000383 VisitStringLiteral(Node->getAsmString());
Anders Carlssonb235fc22007-11-22 01:36:19 +0000384
385 // Outputs
386 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
387 Node->getNumClobbers() != 0)
388 OS << " : ";
389
390 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
391 if (i != 0)
392 OS << ", ";
393
394 if (!Node->getOutputName(i).empty()) {
395 OS << '[';
396 OS << Node->getOutputName(i);
397 OS << "] ";
398 }
399
400 VisitStringLiteral(Node->getOutputConstraint(i));
401 OS << " ";
402 Visit(Node->getOutputExpr(i));
403 }
404
405 // Inputs
406 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
407 OS << " : ";
408
409 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
410 if (i != 0)
411 OS << ", ";
412
413 if (!Node->getInputName(i).empty()) {
414 OS << '[';
415 OS << Node->getInputName(i);
416 OS << "] ";
417 }
418
419 VisitStringLiteral(Node->getInputConstraint(i));
420 OS << " ";
421 Visit(Node->getInputExpr(i));
422 }
423
424 // Clobbers
425 if (Node->getNumClobbers() != 0)
426 OS << " : ";
427
428 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
429 if (i != 0)
430 OS << ", ";
431
432 VisitStringLiteral(Node->getClobber(i));
433 }
434
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000435 OS << ");\n";
Chris Lattnerfe795952007-10-29 04:04:16 +0000436}
437
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000438void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000439 Indent() << "@try";
440 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
441 PrintRawCompoundStmt(TS);
442 OS << "\n";
443 }
444
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000445 for (ObjCAtCatchStmt *catchStmt =
446 static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts());
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000447 catchStmt;
448 catchStmt =
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000449 static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000450 Indent() << "@catch(";
451 if (catchStmt->getCatchParamStmt()) {
452 if (DeclStmt *DS = dyn_cast<DeclStmt>(catchStmt->getCatchParamStmt()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000453 PrintRawDeclStmt(DS);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000454 }
455 OS << ")";
456 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody()))
457 {
458 PrintRawCompoundStmt(CS);
459 OS << "\n";
460 }
461 }
462
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000463 if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>(
Fariborz Jahanian1e7eab42007-11-07 00:46:42 +0000464 Node->getFinallyStmt())) {
465 Indent() << "@finally";
466 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000467 OS << "\n";
468 }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000469}
470
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000471void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000472}
473
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000474void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000475 Indent() << "@catch (...) { /* todo */ } \n";
476}
477
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000478void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000479 Indent() << "@throw";
480 if (Node->getThrowExpr()) {
481 OS << " ";
482 PrintExpr(Node->getThrowExpr());
483 }
484 OS << ";\n";
485}
486
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000487void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000488 Indent() << "@synchronized (";
489 PrintExpr(Node->getSynchExpr());
490 OS << ")";
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000491 PrintRawCompoundStmt(Node->getSynchBody());
492 OS << "\n";
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000493}
494
Sebastian Redl8351da02008-12-22 21:35:02 +0000495void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
496 OS << "catch (";
Sebastian Redl4b07b292008-12-22 19:15:10 +0000497 if (Decl *ExDecl = Node->getExceptionDecl())
498 PrintRawDecl(ExDecl);
499 else
500 OS << "...";
501 OS << ") ";
502 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl8351da02008-12-22 21:35:02 +0000503}
504
505void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
506 Indent();
507 PrintRawCXXCatchStmt(Node);
508 OS << "\n";
509}
510
511void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
512 Indent() << "try ";
513 PrintRawCompoundStmt(Node->getTryBlock());
514 for(unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
515 OS << " ";
516 PrintRawCXXCatchStmt(Node->getHandler(i));
517 }
Sebastian Redl4b07b292008-12-22 19:15:10 +0000518 OS << "\n";
519}
520
Reid Spencer5f016e22007-07-11 17:01:13 +0000521//===----------------------------------------------------------------------===//
522// Expr printing methods.
523//===----------------------------------------------------------------------===//
524
525void StmtPrinter::VisitExpr(Expr *Node) {
526 OS << "<<unknown expr type>>";
527}
528
529void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +0000530 OS << Node->getDecl()->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000531}
532
Douglas Gregor1a49af92009-01-06 05:10:23 +0000533void StmtPrinter::VisitQualifiedDeclRefExpr(QualifiedDeclRefExpr *Node) {
534 // FIXME: Should we keep enough information in QualifiedDeclRefExpr
535 // to produce the same qualification that the user wrote?
536 llvm::SmallVector<DeclContext *, 4> Contexts;
537
538 NamedDecl *D = Node->getDecl();
539
540 // Build up a stack of contexts.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000541 DeclContext *Ctx = D->getDeclContext();
Douglas Gregor1a49af92009-01-06 05:10:23 +0000542 for (; Ctx; Ctx = Ctx->getParent())
543 if (!Ctx->isTransparentContext())
544 Contexts.push_back(Ctx);
545
546 while (!Contexts.empty()) {
547 DeclContext *Ctx = Contexts.back();
548 if (isa<TranslationUnitDecl>(Ctx))
549 OS << "::";
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000550 else if (NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
551 OS << ND->getNameAsString() << "::";
Douglas Gregor1a49af92009-01-06 05:10:23 +0000552 Contexts.pop_back();
553 }
554
555 OS << D->getNameAsString();
556}
557
Steve Naroff7779db42007-11-12 14:29:37 +0000558void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000559 if (Node->getBase()) {
560 PrintExpr(Node->getBase());
561 OS << (Node->isArrow() ? "->" : ".");
562 }
Chris Lattner39f34e92008-11-24 04:00:27 +0000563 OS << Node->getDecl()->getNameAsString();
Steve Naroff7779db42007-11-12 14:29:37 +0000564}
565
Steve Naroffae784072008-05-30 00:40:33 +0000566void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
567 if (Node->getBase()) {
568 PrintExpr(Node->getBase());
569 OS << ".";
570 }
Steve Naroffc77a6362008-12-04 16:24:46 +0000571 OS << Node->getProperty()->getNameAsCString();
Steve Naroffae784072008-05-30 00:40:33 +0000572}
573
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000574void StmtPrinter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node) {
575 if (Node->getBase()) {
576 PrintExpr(Node->getBase());
577 OS << ".";
578 }
579 // FIXME: Setter/Getter names
580}
581
Chris Lattnerd9f69102008-08-10 01:53:14 +0000582void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson22742662007-07-21 05:21:51 +0000583 switch (Node->getIdentType()) {
584 default:
585 assert(0 && "unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000586 case PredefinedExpr::Func:
Anders Carlsson22742662007-07-21 05:21:51 +0000587 OS << "__func__";
588 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000589 case PredefinedExpr::Function:
Anders Carlsson22742662007-07-21 05:21:51 +0000590 OS << "__FUNCTION__";
591 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000592 case PredefinedExpr::PrettyFunction:
Anders Carlsson22742662007-07-21 05:21:51 +0000593 OS << "__PRETTY_FUNCTION__";
594 break;
595 }
596}
597
Reid Spencer5f016e22007-07-11 17:01:13 +0000598void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000599 unsigned value = Node->getValue();
Chris Lattnerc250aae2008-06-07 22:35:38 +0000600 if (Node->isWide())
601 OS << "L";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000602 switch (value) {
603 case '\\':
604 OS << "'\\\\'";
605 break;
606 case '\'':
607 OS << "'\\''";
608 break;
609 case '\a':
610 // TODO: K&R: the meaning of '\\a' is different in traditional C
611 OS << "'\\a'";
612 break;
613 case '\b':
614 OS << "'\\b'";
615 break;
616 // Nonstandard escape sequence.
617 /*case '\e':
618 OS << "'\\e'";
619 break;*/
620 case '\f':
621 OS << "'\\f'";
622 break;
623 case '\n':
624 OS << "'\\n'";
625 break;
626 case '\r':
627 OS << "'\\r'";
628 break;
629 case '\t':
630 OS << "'\\t'";
631 break;
632 case '\v':
633 OS << "'\\v'";
634 break;
635 default:
Ted Kremenek471733d2008-02-23 00:52:04 +0000636 if (value < 256 && isprint(value)) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000637 OS << "'" << (char)value << "'";
638 } else if (value < 256) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000639 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000640 } else {
641 // FIXME what to really do here?
642 OS << value;
643 }
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000644 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000645}
646
647void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
648 bool isSigned = Node->getType()->isSignedIntegerType();
649 OS << Node->getValue().toString(10, isSigned);
650
651 // Emit suffixes. Integer literals are always a builtin integer type.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000652 switch (Node->getType()->getAsBuiltinType()->getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000653 default: assert(0 && "Unexpected type for integer literal!");
654 case BuiltinType::Int: break; // no suffix.
655 case BuiltinType::UInt: OS << 'U'; break;
656 case BuiltinType::Long: OS << 'L'; break;
657 case BuiltinType::ULong: OS << "UL"; break;
658 case BuiltinType::LongLong: OS << "LL"; break;
659 case BuiltinType::ULongLong: OS << "ULL"; break;
660 }
661}
662void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner86e499d2007-08-01 00:23:58 +0000663 // FIXME: print value more precisely.
Chris Lattnerda8249e2008-06-07 22:13:43 +0000664 OS << Node->getValueAsApproximateDouble();
Reid Spencer5f016e22007-07-11 17:01:13 +0000665}
Chris Lattner5d661452007-08-26 03:42:43 +0000666
667void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
668 PrintExpr(Node->getSubExpr());
669 OS << "i";
670}
671
Reid Spencer5f016e22007-07-11 17:01:13 +0000672void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
673 if (Str->isWide()) OS << 'L';
674 OS << '"';
Anders Carlssonee98ac52007-10-15 02:50:23 +0000675
Reid Spencer5f016e22007-07-11 17:01:13 +0000676 // FIXME: this doesn't print wstrings right.
677 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattner9a81c872009-01-16 19:25:18 +0000678 unsigned char Char = Str->getStrData()[i];
679
680 switch (Char) {
681 default:
682 if (isprint(Char))
683 OS << (char)Char;
684 else // Output anything hard as an octal escape.
685 OS << '\\'
686 << (char)('0'+ ((Char >> 6) & 7))
687 << (char)('0'+ ((Char >> 3) & 7))
688 << (char)('0'+ ((Char >> 0) & 7));
689 break;
690 // Handle some common non-printable cases to make dumps prettier.
Reid Spencer5f016e22007-07-11 17:01:13 +0000691 case '\\': OS << "\\\\"; break;
692 case '"': OS << "\\\""; break;
693 case '\n': OS << "\\n"; break;
694 case '\t': OS << "\\t"; break;
695 case '\a': OS << "\\a"; break;
696 case '\b': OS << "\\b"; break;
697 }
698 }
699 OS << '"';
700}
701void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
702 OS << "(";
703 PrintExpr(Node->getSubExpr());
704 OS << ")";
705}
706void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +0000707 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000708 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner296bf192007-08-23 21:46:40 +0000709
Sebastian Redl05189992008-11-11 17:56:53 +0000710 // Print a space if this is an "identifier operator" like __real.
Chris Lattner296bf192007-08-23 21:46:40 +0000711 switch (Node->getOpcode()) {
712 default: break;
Chris Lattner296bf192007-08-23 21:46:40 +0000713 case UnaryOperator::Real:
714 case UnaryOperator::Imag:
715 case UnaryOperator::Extension:
716 OS << ' ';
717 break;
718 }
719 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000720 PrintExpr(Node->getSubExpr());
721
722 if (Node->isPostfix())
723 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +0000724}
Chris Lattner704fe352007-08-30 17:59:59 +0000725
726bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
727 if (isa<CompoundLiteralExpr>(E)) {
728 // Base case, print the type and comma.
729 OS << E->getType().getAsString() << ", ";
730 return true;
731 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
732 PrintOffsetOfDesignator(ASE->getLHS());
733 OS << "[";
734 PrintExpr(ASE->getRHS());
735 OS << "]";
736 return false;
737 } else {
738 MemberExpr *ME = cast<MemberExpr>(E);
739 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
Chris Lattner39f34e92008-11-24 04:00:27 +0000740 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getNameAsString();
Chris Lattner704fe352007-08-30 17:59:59 +0000741 return false;
742 }
743}
744
745void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
746 OS << "__builtin_offsetof(";
747 PrintOffsetOfDesignator(Node->getSubExpr());
748 OS << ")";
749}
750
Sebastian Redl05189992008-11-11 17:56:53 +0000751void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
752 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
753 if (Node->isArgumentType())
754 OS << "(" << Node->getArgumentType().getAsString() << ")";
755 else {
756 OS << " ";
757 PrintExpr(Node->getArgumentExpr());
758 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000759}
760void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +0000761 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000762 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +0000763 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000764 OS << "]";
765}
766
767void StmtPrinter::VisitCallExpr(CallExpr *Call) {
768 PrintExpr(Call->getCallee());
769 OS << "(";
770 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattner04421082008-04-08 04:40:51 +0000771 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
772 // Don't print any defaulted arguments
773 break;
774 }
775
Reid Spencer5f016e22007-07-11 17:01:13 +0000776 if (i) OS << ", ";
777 PrintExpr(Call->getArg(i));
778 }
779 OS << ")";
780}
781void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregorb3eef682009-01-08 22:45:41 +0000782 // FIXME: Suppress printing implicit bases (like "this")
783 PrintExpr(Node->getBase());
784 OS << (Node->isArrow() ? "->" : ".");
785 // FIXME: Suppress printing references to unnamed objects
786 // representing anonymous unions/structs
Douglas Gregor86f19402008-12-20 23:49:58 +0000787 OS << Node->getMemberDecl()->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000788}
Nate Begeman213541a2008-04-18 23:10:10 +0000789void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +0000790 PrintExpr(Node->getBase());
791 OS << ".";
792 OS << Node->getAccessor().getName();
793}
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000794void StmtPrinter::VisitCastExpr(CastExpr *) {
795 assert(0 && "CastExpr is an abstract class");
796}
Douglas Gregor49badde2008-10-27 19:41:14 +0000797void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *) {
798 assert(0 && "ExplicitCastExpr is an abstract class");
799}
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000800void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000801 OS << "(" << Node->getType().getAsString() << ")";
Reid Spencer5f016e22007-07-11 17:01:13 +0000802 PrintExpr(Node->getSubExpr());
803}
Steve Naroffaff1edd2007-07-19 21:32:11 +0000804void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
805 OS << "(" << Node->getType().getAsString() << ")";
806 PrintExpr(Node->getInitializer());
807}
Steve Naroff49b45262007-07-13 16:58:59 +0000808void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroff90045e82007-07-13 23:32:42 +0000809 // No need to print anything, simply forward to the sub expression.
810 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +0000811}
Reid Spencer5f016e22007-07-11 17:01:13 +0000812void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
813 PrintExpr(Node->getLHS());
814 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
815 PrintExpr(Node->getRHS());
816}
Chris Lattnereb14fe82007-08-25 02:00:02 +0000817void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
818 PrintExpr(Node->getLHS());
819 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
820 PrintExpr(Node->getRHS());
821}
Reid Spencer5f016e22007-07-11 17:01:13 +0000822void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
823 PrintExpr(Node->getCond());
Ted Kremenek8e911c42007-11-26 18:27:54 +0000824
825 if (Node->getLHS()) {
826 OS << " ? ";
827 PrintExpr(Node->getLHS());
828 OS << " : ";
829 }
830 else { // Handle GCC extention where LHS can be NULL.
831 OS << " ?: ";
832 }
833
Reid Spencer5f016e22007-07-11 17:01:13 +0000834 PrintExpr(Node->getRHS());
835}
836
837// GNU extensions.
838
Chris Lattner6481a572007-08-03 17:31:20 +0000839void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000840 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000841}
842
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000843void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
844 OS << "(";
845 PrintRawCompoundStmt(E->getSubStmt());
846 OS << ")";
847}
848
Steve Naroffd34e9152007-08-01 22:05:33 +0000849void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
850 OS << "__builtin_types_compatible_p(";
851 OS << Node->getArgType1().getAsString() << ",";
852 OS << Node->getArgType2().getAsString() << ")";
853}
854
Steve Naroffd04fdd52007-08-03 21:21:27 +0000855void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
856 OS << "__builtin_choose_expr(";
857 PrintExpr(Node->getCond());
Chris Lattner94f05e32007-08-04 00:20:15 +0000858 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000859 PrintExpr(Node->getLHS());
Chris Lattner94f05e32007-08-04 00:20:15 +0000860 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000861 PrintExpr(Node->getRHS());
862 OS << ")";
863}
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000864
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000865void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
866 OS << "__null";
867}
868
Nate Begemane2ce1d92008-01-17 17:46:27 +0000869void StmtPrinter::VisitOverloadExpr(OverloadExpr *Node) {
870 OS << "__builtin_overload(";
Nate Begeman67295d02008-01-30 20:50:20 +0000871 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
Nate Begemane2ce1d92008-01-17 17:46:27 +0000872 if (i) OS << ", ";
Nate Begeman67295d02008-01-30 20:50:20 +0000873 PrintExpr(Node->getExpr(i));
Nate Begemane2ce1d92008-01-17 17:46:27 +0000874 }
875 OS << ")";
876}
877
Eli Friedmand38617c2008-05-14 19:38:39 +0000878void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
879 OS << "__builtin_shufflevector(";
880 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
881 if (i) OS << ", ";
882 PrintExpr(Node->getExpr(i));
883 }
884 OS << ")";
885}
886
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000887void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
888 OS << "{ ";
889 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
890 if (i) OS << ", ";
Douglas Gregor4c678342009-01-28 21:54:33 +0000891 if (Node->getInit(i))
892 PrintExpr(Node->getInit(i));
893 else
894 OS << "0";
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000895 }
896 OS << " }";
897}
898
Douglas Gregor05c13a32009-01-22 00:58:24 +0000899void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000900 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
901 DEnd = Node->designators_end();
902 D != DEnd; ++D) {
903 if (D->isFieldDesignator()) {
904 if (D->getDotLoc().isInvalid())
905 OS << D->getFieldName()->getName() << ":";
906 else
907 OS << "." << D->getFieldName()->getName();
908 } else {
909 OS << "[";
910 if (D->isArrayDesignator()) {
911 PrintExpr(Node->getArrayIndex(*D));
912 } else {
913 PrintExpr(Node->getArrayRangeStart(*D));
914 OS << " ... ";
915 PrintExpr(Node->getArrayRangeEnd(*D));
916 }
917 OS << "]";
918 }
919 }
920
921 OS << " = ";
922 PrintExpr(Node->getInit());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000923}
924
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000925void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
926 OS << "/*implicit*/" << Node->getType().getAsString() << "()";
927}
928
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000929void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
930 OS << "va_arg(";
931 PrintExpr(Node->getSubExpr());
932 OS << ", ";
933 OS << Node->getType().getAsString();
934 OS << ")";
935}
936
Reid Spencer5f016e22007-07-11 17:01:13 +0000937// C++
Douglas Gregorb4609802008-11-14 16:09:21 +0000938void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
939 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
940 "",
941#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
942 Spelling,
943#include "clang/Basic/OperatorKinds.def"
944 };
945
946 OverloadedOperatorKind Kind = Node->getOperator();
947 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
948 if (Node->getNumArgs() == 1) {
949 OS << OpStrings[Kind] << ' ';
950 PrintExpr(Node->getArg(0));
951 } else {
952 PrintExpr(Node->getArg(0));
953 OS << ' ' << OpStrings[Kind];
954 }
955 } else if (Kind == OO_Call) {
956 PrintExpr(Node->getArg(0));
957 OS << '(';
958 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
959 if (ArgIdx > 1)
960 OS << ", ";
961 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
962 PrintExpr(Node->getArg(ArgIdx));
963 }
964 OS << ')';
965 } else if (Kind == OO_Subscript) {
966 PrintExpr(Node->getArg(0));
967 OS << '[';
968 PrintExpr(Node->getArg(1));
969 OS << ']';
970 } else if (Node->getNumArgs() == 1) {
971 OS << OpStrings[Kind] << ' ';
972 PrintExpr(Node->getArg(0));
973 } else if (Node->getNumArgs() == 2) {
974 PrintExpr(Node->getArg(0));
975 OS << ' ' << OpStrings[Kind] << ' ';
976 PrintExpr(Node->getArg(1));
977 } else {
978 assert(false && "unknown overloaded operator");
979 }
980}
Reid Spencer5f016e22007-07-11 17:01:13 +0000981
Douglas Gregor88a35142008-12-22 05:46:06 +0000982void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
983 VisitCallExpr(cast<CallExpr>(Node));
984}
985
Douglas Gregor49badde2008-10-27 19:41:14 +0000986void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
987 OS << Node->getCastName() << '<';
988 OS << Node->getTypeAsWritten().getAsString() << ">(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000989 PrintExpr(Node->getSubExpr());
990 OS << ")";
991}
992
Douglas Gregor49badde2008-10-27 19:41:14 +0000993void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
994 VisitCXXNamedCastExpr(Node);
995}
996
997void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
998 VisitCXXNamedCastExpr(Node);
999}
1000
1001void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1002 VisitCXXNamedCastExpr(Node);
1003}
1004
1005void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1006 VisitCXXNamedCastExpr(Node);
1007}
1008
Sebastian Redlc42e1182008-11-11 11:37:55 +00001009void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1010 OS << "typeid(";
1011 if (Node->isTypeOperand()) {
1012 OS << Node->getTypeOperand().getAsString();
1013 } else {
1014 PrintExpr(Node->getExprOperand());
1015 }
1016 OS << ")";
1017}
1018
Reid Spencer5f016e22007-07-11 17:01:13 +00001019void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1020 OS << (Node->getValue() ? "true" : "false");
1021}
1022
Douglas Gregor796da182008-11-04 14:32:21 +00001023void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1024 OS << "this";
1025}
1026
Chris Lattner50dd2892008-02-26 00:51:44 +00001027void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1028 if (Node->getSubExpr() == 0)
1029 OS << "throw";
1030 else {
1031 OS << "throw ";
1032 PrintExpr(Node->getSubExpr());
1033 }
1034}
1035
Chris Lattner04421082008-04-08 04:40:51 +00001036void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1037 // Nothing to print: we picked up the default argument
1038}
1039
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001040void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1041 OS << Node->getType().getAsString();
1042 OS << "(";
1043 PrintExpr(Node->getSubExpr());
1044 OS << ")";
1045}
1046
Douglas Gregor506ae412009-01-16 18:33:17 +00001047void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1048 OS << Node->getType().getAsString();
1049 OS << "(";
1050 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1051 ArgEnd = Node->arg_end();
1052 Arg != ArgEnd; ++Arg) {
1053 if (Arg != Node->arg_begin())
1054 OS << ", ";
1055 PrintExpr(*Arg);
1056 }
1057 OS << ")";
1058}
1059
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001060void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
1061 OS << Node->getType().getAsString() << "()";
1062}
1063
Argyrios Kyrtzidis9e922b12008-09-09 23:47:53 +00001064void
1065StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
1066 PrintRawDecl(E->getVarDecl());
1067}
1068
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001069void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1070 if (E->isGlobalNew())
1071 OS << "::";
1072 OS << "new ";
1073 unsigned NumPlace = E->getNumPlacementArgs();
1074 if (NumPlace > 0) {
1075 OS << "(";
1076 PrintExpr(E->getPlacementArg(0));
1077 for (unsigned i = 1; i < NumPlace; ++i) {
1078 OS << ", ";
1079 PrintExpr(E->getPlacementArg(i));
1080 }
1081 OS << ") ";
1082 }
1083 if (E->isParenTypeId())
1084 OS << "(";
Sebastian Redl6fec6482008-12-02 22:08:59 +00001085 std::string TypeS;
1086 if (Expr *Size = E->getArraySize()) {
1087 llvm::raw_string_ostream s(TypeS);
1088 Size->printPretty(s);
1089 s.flush();
1090 TypeS = "[" + TypeS + "]";
1091 }
1092 E->getAllocatedType().getAsStringInternal(TypeS);
1093 OS << TypeS;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001094 if (E->isParenTypeId())
1095 OS << ")";
1096
1097 if (E->hasInitializer()) {
1098 OS << "(";
1099 unsigned NumCons = E->getNumConstructorArgs();
1100 if (NumCons > 0) {
1101 PrintExpr(E->getConstructorArg(0));
1102 for (unsigned i = 1; i < NumCons; ++i) {
1103 OS << ", ";
1104 PrintExpr(E->getConstructorArg(i));
1105 }
1106 }
1107 OS << ")";
1108 }
1109}
1110
1111void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1112 if (E->isGlobalDelete())
1113 OS << "::";
1114 OS << "delete ";
1115 if (E->isArrayForm())
1116 OS << "[] ";
1117 PrintExpr(E->getArgument());
1118}
1119
Douglas Gregor17330012009-02-04 15:01:18 +00001120void StmtPrinter::VisitUnresolvedFunctionNameExpr(UnresolvedFunctionNameExpr *E) {
1121 OS << E->getName().getAsString();
Douglas Gregor5c37de72008-12-06 00:22:45 +00001122}
1123
Sebastian Redl64b45f72009-01-05 20:52:13 +00001124static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1125 switch (UTT) {
1126 default: assert(false && "Unknown type trait");
1127 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1128 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1129 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1130 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1131 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1132 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1133 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1134 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1135 case UTT_IsAbstract: return "__is_abstract";
1136 case UTT_IsClass: return "__is_class";
1137 case UTT_IsEmpty: return "__is_empty";
1138 case UTT_IsEnum: return "__is_enum";
1139 case UTT_IsPOD: return "__is_pod";
1140 case UTT_IsPolymorphic: return "__is_polymorphic";
1141 case UTT_IsUnion: return "__is_union";
1142 }
1143}
1144
1145void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1146 OS << getTypeTraitName(E->getTrait()) << "("
1147 << E->getQueriedType().getAsString() << ")";
1148}
1149
Anders Carlsson55085182007-08-21 17:43:55 +00001150// Obj-C
1151
1152void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1153 OS << "@";
1154 VisitStringLiteral(Node->getString());
1155}
Reid Spencer5f016e22007-07-11 17:01:13 +00001156
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001157void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001158 OS << "@encode(" << Node->getEncodedType().getAsString() << ')';
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001159}
1160
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001161void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001162 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001163}
1164
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001165void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001166 OS << "@protocol(" << Node->getProtocol()->getNameAsString() << ')';
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001167}
1168
Steve Naroff563477d2007-09-18 23:55:05 +00001169void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1170 OS << "[";
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001171 Expr *receiver = Mess->getReceiver();
1172 if (receiver) PrintExpr(receiver);
1173 else OS << Mess->getClassName()->getName();
Ted Kremenekc29efd82008-05-02 17:32:38 +00001174 OS << ' ';
Ted Kremenek97b7f262008-04-16 04:30:16 +00001175 Selector selector = Mess->getSelector();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001176 if (selector.isUnarySelector()) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001177 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001178 } else {
1179 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001180 if (i < selector.getNumArgs()) {
1181 if (i > 0) OS << ' ';
1182 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner39f34e92008-11-24 04:00:27 +00001183 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremenekc29efd82008-05-02 17:32:38 +00001184 else
1185 OS << ":";
1186 }
1187 else OS << ", "; // Handle variadic methods.
1188
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001189 PrintExpr(Mess->getArg(i));
1190 }
Steve Naroff563477d2007-09-18 23:55:05 +00001191 }
1192 OS << "]";
1193}
1194
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001195void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) {
1196 OS << "super";
1197}
1198
Steve Naroff4eb206b2008-09-03 18:15:37 +00001199void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001200 BlockDecl *BD = Node->getBlockDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001201 OS << "^";
1202
1203 const FunctionType *AFT = Node->getFunctionType();
1204
1205 if (isa<FunctionTypeNoProto>(AFT)) {
1206 OS << "()";
Steve Naroff56ee6892008-10-08 17:01:13 +00001207 } else if (!BD->param_empty() || cast<FunctionTypeProto>(AFT)->isVariadic()) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001208 OS << '(';
1209 std::string ParamStr;
Steve Naroff56ee6892008-10-08 17:01:13 +00001210 for (BlockDecl::param_iterator AI = BD->param_begin(),
1211 E = BD->param_end(); AI != E; ++AI) {
1212 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner39f34e92008-11-24 04:00:27 +00001213 ParamStr = (*AI)->getNameAsString();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001214 (*AI)->getType().getAsStringInternal(ParamStr);
1215 OS << ParamStr;
1216 }
1217
Steve Naroff56ee6892008-10-08 17:01:13 +00001218 const FunctionTypeProto *FT = cast<FunctionTypeProto>(AFT);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001219 if (FT->isVariadic()) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001220 if (!BD->param_empty()) OS << ", ";
Steve Naroff4eb206b2008-09-03 18:15:37 +00001221 OS << "...";
1222 }
1223 OS << ')';
1224 }
1225}
1226
Steve Naroff4eb206b2008-09-03 18:15:37 +00001227void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001228 OS << Node->getDecl()->getNameAsString();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001229}
Reid Spencer5f016e22007-07-11 17:01:13 +00001230//===----------------------------------------------------------------------===//
1231// Stmt method implementations
1232//===----------------------------------------------------------------------===//
1233
Chris Lattner6000dac2007-08-08 22:51:59 +00001234void Stmt::dumpPretty() const {
Douglas Gregor4c678342009-01-28 21:54:33 +00001235 llvm::raw_ostream &OS = llvm::errs();
1236 printPretty(OS);
1237 OS.flush();
Reid Spencer5f016e22007-07-11 17:01:13 +00001238}
1239
Mike Stump071e4da2009-02-10 20:16:46 +00001240void Stmt::printPretty(llvm::raw_ostream &OS, PrinterHelper* Helper,
1241 unsigned I, bool NoIndent) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001242 if (this == 0) {
1243 OS << "<NULL>";
1244 return;
1245 }
1246
Mike Stump071e4da2009-02-10 20:16:46 +00001247 StmtPrinter P(OS, Helper, I, NoIndent);
Chris Lattnerc5598cb2007-08-21 04:04:25 +00001248 P.Visit(const_cast<Stmt*>(this));
Reid Spencer5f016e22007-07-11 17:01:13 +00001249}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001250
1251//===----------------------------------------------------------------------===//
1252// PrinterHelper
1253//===----------------------------------------------------------------------===//
1254
1255// Implement virtual destructor.
Gabor Greif84675832007-09-11 15:32:40 +00001256PrinterHelper::~PrinterHelper() {}