blob: 3a6a01c56861ab6f3efc62548dd9092852abb6e2 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- StmtPrinter.cpp - Printing implementation for Stmt ASTs ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner95578782007-08-08 22:51:59 +000010// This file implements the Stmt::dumpPretty/Stmt::printPretty methods, which
11// pretty print the AST back out to C code.
Chris Lattner4b009652007-07-25 00:24:17 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "clang/AST/StmtVisitor.h"
Douglas Gregor566782a2009-01-06 05:10:23 +000016#include "clang/AST/DeclCXX.h"
Ted Kremenek10364dd2007-10-17 18:36:42 +000017#include "clang/AST/DeclObjC.h"
Ted Kremenek08176a52007-08-31 21:30:12 +000018#include "clang/AST/PrettyPrinter.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include "llvm/Support/Compiler.h"
Ted Kremenekdf409eb2007-11-26 22:50:46 +000020#include "llvm/Support/Streams.h"
Ted Kremenek7b6f67b2008-09-13 05:16:45 +000021#include "llvm/Support/Format.h"
Chris Lattner4b009652007-07-25 00:24:17 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// StmtPrinter Visitor
26//===----------------------------------------------------------------------===//
27
28namespace {
Chris Lattner7ba21fa2007-08-21 04:04:25 +000029 class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor<StmtPrinter> {
Ted Kremenek7b6f67b2008-09-13 05:16:45 +000030 llvm::raw_ostream &OS;
Chris Lattner4b009652007-07-25 00:24:17 +000031 unsigned IndentLevel;
Mike Stumpc43f4fc2009-02-10 20:16:46 +000032 bool NoIndent;
Ted Kremenek08176a52007-08-31 21:30:12 +000033 clang::PrinterHelper* Helper;
Chris Lattner4b009652007-07-25 00:24:17 +000034 public:
Mike Stumpc43f4fc2009-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) {}
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner7ba21fa2007-08-21 04:04:25 +000044 Visit(S);
Chris Lattner4b009652007-07-25 00:24:17 +000045 OS << ";\n";
46 } else if (S) {
Chris Lattner7ba21fa2007-08-21 04:04:25 +000047 Visit(S);
Chris Lattner4b009652007-07-25 00:24:17 +000048 } else {
49 Indent() << "<<<NULL STATEMENT>>>\n";
50 }
51 IndentLevel -= SubIndent;
52 }
53
54 void PrintRawCompoundStmt(CompoundStmt *S);
55 void PrintRawDecl(Decl *D);
Ted Kremenek388b2842008-10-06 18:39:36 +000056 void PrintRawDeclStmt(DeclStmt *S);
Mike Stumpc43f4fc2009-02-10 20:16:46 +000057 void PrintFieldDecl(FieldDecl *FD);
Chris Lattner4b009652007-07-25 00:24:17 +000058 void PrintRawIfStmt(IfStmt *If);
Sebastian Redl237116b2008-12-22 21:35:02 +000059 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
Chris Lattner4b009652007-07-25 00:24:17 +000060
61 void PrintExpr(Expr *E) {
62 if (E)
Chris Lattner7ba21fa2007-08-21 04:04:25 +000063 Visit(E);
Chris Lattner4b009652007-07-25 00:24:17 +000064 else
65 OS << "<null expr>";
66 }
67
Mike Stumpc43f4fc2009-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;
Chris Lattner4b009652007-07-25 00:24:17 +000073 return OS;
74 }
75
Chris Lattner2af6a802007-08-30 17:59:59 +000076 bool PrintOffsetOfDesignator(Expr *E);
77 void VisitUnaryOffsetOf(UnaryOperator *Node);
78
Ted Kremenek08176a52007-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 Lattner7ba21fa2007-08-21 04:04:25 +000085 void VisitStmt(Stmt *Node);
Douglas Gregor19330152008-11-14 12:46:07 +000086#define STMT(CLASS, PARENT) \
Chris Lattner7ba21fa2007-08-21 04:04:25 +000087 void Visit##CLASS(CLASS *Node);
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner6c5ec622008-11-24 04:00:27 +0000116 OS << " " << localType->getNameAsString();
Chris Lattner4b009652007-07-25 00:24:17 +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 Stumpb42ab332009-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;
Chris Lattner4b009652007-07-25 00:24:17 +0000128 }
129 }
130
Chris Lattner6c5ec622008-11-24 04:00:27 +0000131 std::string Name = VD->getNameAsString();
Chris Lattner4b009652007-07-25 00:24:17 +0000132 VD->getType().getAsStringInternal(Name);
133 OS << Name;
134
135 // 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 Naroff3340c232007-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 Stumpc43f4fc2009-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 }
Chris Lattner4b009652007-07-25 00:24:17 +0000156 } else {
Chris Lattner4b009652007-07-25 00:24:17 +0000157 assert(0 && "Unexpected decl");
158 }
159}
160
Mike Stumpc43f4fc2009-02-10 20:16:46 +0000161void StmtPrinter::PrintFieldDecl(FieldDecl *FD) {
162 Indent() << FD->getNameAsString() << "\n";
163}
164
Ted Kremenek388b2842008-10-06 18:39:36 +0000165void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
Mike Stumpc43f4fc2009-02-10 20:16:46 +0000166 bool isFirst = true;
Ted Kremenek388b2842008-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}
Chris Lattner4b009652007-07-25 00:24:17 +0000177
178void StmtPrinter::VisitNullStmt(NullStmt *Node) {
179 Indent() << ";\n";
180}
181
182void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Ted Kremenek388b2842008-10-06 18:39:36 +0000183 for (DeclStmt::decl_iterator I = Node->decl_begin(), E = Node->decl_end();
184 I!=E; ++I) {
Chris Lattner4b009652007-07-25 00:24:17 +0000185 Indent();
Ted Kremenek388b2842008-10-06 18:39:36 +0000186 PrintRawDecl(*I);
Chris Lattner4b009652007-07-25 00:24:17 +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 Redlfd835a22009-02-07 20:05:48 +0000220 OS << "if (";
Chris Lattner4b009652007-07-25 00:24:17 +0000221 PrintExpr(If->getCond());
Sebastian Redlfd835a22009-02-07 20:05:48 +0000222 OS << ')';
Chris Lattner4b009652007-07-25 00:24:17 +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
272void StmtPrinter::VisitSwitchCase(SwitchCase*) {
273 assert(0 && "SwitchCase is an abstract class");
274}
275
276void 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 Lattnercfe0ff02007-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 ";
Chris Lattner4b009652007-07-25 00:24:17 +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 Kremenek388b2842008-10-06 18:39:36 +0000303 PrintRawDeclStmt(DS);
Chris Lattner4b009652007-07-25 00:24:17 +0000304 else
305 PrintExpr(cast<Expr>(Node->getInit()));
306 }
Chris Lattnercfe0ff02007-09-15 21:49:37 +0000307 OS << ";";
308 if (Node->getCond()) {
309 OS << " ";
Chris Lattner4b009652007-07-25 00:24:17 +0000310 PrintExpr(Node->getCond());
Chris Lattnercfe0ff02007-09-15 21:49:37 +0000311 }
312 OS << ";";
313 if (Node->getInc()) {
314 OS << " ";
Chris Lattner4b009652007-07-25 00:24:17 +0000315 PrintExpr(Node->getInc());
Chris Lattnercfe0ff02007-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 }
Chris Lattner4b009652007-07-25 00:24:17 +0000326}
327
Ted Kremenek42730c52008-01-07 19:49:32 +0000328void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000329 Indent() << "for (";
330 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenek388b2842008-10-06 18:39:36 +0000331 PrintRawDeclStmt(DS);
Fariborz Jahanian9e920f32008-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
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner8a40a832007-10-29 04:04:16 +0000375
376void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson759f45d2007-11-23 23:12:25 +0000377 Indent() << "asm ";
378
379 if (Node->isVolatile())
380 OS << "volatile ";
381
382 OS << "(";
Anders Carlsson076c1112007-11-20 19:21:03 +0000383 VisitStringLiteral(Node->getAsmString());
Anders Carlsson965d5202007-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
Chris Lattnere8625112009-03-10 04:59:06 +0000400 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlsson965d5202007-11-22 01:36:19 +0000401 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
Chris Lattnere8625112009-03-10 04:59:06 +0000419 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlsson965d5202007-11-22 01:36:19 +0000420 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 Carlsson076c1112007-11-20 19:21:03 +0000435 OS << ");\n";
Chris Lattner8a40a832007-10-29 04:04:16 +0000436}
437
Ted Kremenek42730c52008-01-07 19:49:32 +0000438void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian59e9e042007-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 Kremenek42730c52008-01-07 19:49:32 +0000445 for (ObjCAtCatchStmt *catchStmt =
446 static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts());
Fariborz Jahanian59e9e042007-11-02 18:16:07 +0000447 catchStmt;
448 catchStmt =
Ted Kremenek42730c52008-01-07 19:49:32 +0000449 static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
Fariborz Jahanian59e9e042007-11-02 18:16:07 +0000450 Indent() << "@catch(";
Steve Naroff0e8b96a2009-03-03 19:52:17 +0000451 if (catchStmt->getCatchParamDecl()) {
452 if (Decl *DS = catchStmt->getCatchParamDecl())
453 PrintRawDecl(DS);
Fariborz Jahanian59e9e042007-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 Kremenek42730c52008-01-07 19:49:32 +0000463 if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>(
Fariborz Jahanian50b47922007-11-07 00:46:42 +0000464 Node->getFinallyStmt())) {
465 Indent() << "@finally";
466 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian59e9e042007-11-02 18:16:07 +0000467 OS << "\n";
468 }
Fariborz Jahanian70952482007-11-01 21:12:44 +0000469}
470
Ted Kremenek42730c52008-01-07 19:49:32 +0000471void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian70952482007-11-01 21:12:44 +0000472}
473
Ted Kremenek42730c52008-01-07 19:49:32 +0000474void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian70952482007-11-01 21:12:44 +0000475 Indent() << "@catch (...) { /* todo */ } \n";
476}
477
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +0000478void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanian08df2c62007-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 Jahanian5f5d6222008-01-30 17:38:29 +0000487void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanian993360a2008-01-29 18:21:32 +0000488 Indent() << "@synchronized (";
489 PrintExpr(Node->getSynchExpr());
490 OS << ")";
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +0000491 PrintRawCompoundStmt(Node->getSynchBody());
492 OS << "\n";
Fariborz Jahanian993360a2008-01-29 18:21:32 +0000493}
494
Sebastian Redl237116b2008-12-22 21:35:02 +0000495void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
496 OS << "catch (";
Sebastian Redl743c8162008-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 Redl237116b2008-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 Redl743c8162008-12-22 19:15:10 +0000518 OS << "\n";
519}
520
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner6c5ec622008-11-24 04:00:27 +0000530 OS << Node->getDecl()->getNameAsString();
Chris Lattner4b009652007-07-25 00:24:17 +0000531}
532
Douglas Gregor7e508262009-03-19 03:51:16 +0000533void StmtPrinter::VisitQualifiedDeclRefExpr(QualifiedDeclRefExpr *Node) {
Douglas Gregor566782a2009-01-06 05:10:23 +0000534 NamedDecl *D = Node->getDecl();
535
Douglas Gregor7e508262009-03-19 03:51:16 +0000536 NestedNameSpecifier::Print(OS, Node->begin(), Node->end());
Douglas Gregor566782a2009-01-06 05:10:23 +0000537 OS << D->getNameAsString();
538}
539
Steve Naroff5eb2a4a2007-11-12 14:29:37 +0000540void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian4af72492007-11-12 22:29:28 +0000541 if (Node->getBase()) {
542 PrintExpr(Node->getBase());
543 OS << (Node->isArrow() ? "->" : ".");
544 }
Chris Lattner6c5ec622008-11-24 04:00:27 +0000545 OS << Node->getDecl()->getNameAsString();
Steve Naroff5eb2a4a2007-11-12 14:29:37 +0000546}
547
Steve Naroff05391d22008-05-30 00:40:33 +0000548void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
549 if (Node->getBase()) {
550 PrintExpr(Node->getBase());
551 OS << ".";
552 }
Steve Naroff0e948412008-12-04 16:24:46 +0000553 OS << Node->getProperty()->getNameAsCString();
Steve Naroff05391d22008-05-30 00:40:33 +0000554}
555
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000556void StmtPrinter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node) {
557 if (Node->getBase()) {
558 PrintExpr(Node->getBase());
559 OS << ".";
560 }
561 // FIXME: Setter/Getter names
562}
563
Chris Lattner69909292008-08-10 01:53:14 +0000564void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Chris Lattner4b009652007-07-25 00:24:17 +0000565 switch (Node->getIdentType()) {
566 default:
567 assert(0 && "unknown case");
Chris Lattner69909292008-08-10 01:53:14 +0000568 case PredefinedExpr::Func:
Chris Lattner4b009652007-07-25 00:24:17 +0000569 OS << "__func__";
570 break;
Chris Lattner69909292008-08-10 01:53:14 +0000571 case PredefinedExpr::Function:
Chris Lattner4b009652007-07-25 00:24:17 +0000572 OS << "__FUNCTION__";
573 break;
Chris Lattner69909292008-08-10 01:53:14 +0000574 case PredefinedExpr::PrettyFunction:
Chris Lattner4b009652007-07-25 00:24:17 +0000575 OS << "__PRETTY_FUNCTION__";
576 break;
577 }
578}
579
580void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner4b009652007-07-25 00:24:17 +0000581 unsigned value = Node->getValue();
Chris Lattner1aaf71c2008-06-07 22:35:38 +0000582 if (Node->isWide())
583 OS << "L";
Chris Lattner4b009652007-07-25 00:24:17 +0000584 switch (value) {
585 case '\\':
586 OS << "'\\\\'";
587 break;
588 case '\'':
589 OS << "'\\''";
590 break;
591 case '\a':
592 // TODO: K&R: the meaning of '\\a' is different in traditional C
593 OS << "'\\a'";
594 break;
595 case '\b':
596 OS << "'\\b'";
597 break;
598 // Nonstandard escape sequence.
599 /*case '\e':
600 OS << "'\\e'";
601 break;*/
602 case '\f':
603 OS << "'\\f'";
604 break;
605 case '\n':
606 OS << "'\\n'";
607 break;
608 case '\r':
609 OS << "'\\r'";
610 break;
611 case '\t':
612 OS << "'\\t'";
613 break;
614 case '\v':
615 OS << "'\\v'";
616 break;
617 default:
Ted Kremenekf91bc1d2008-02-23 00:52:04 +0000618 if (value < 256 && isprint(value)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000619 OS << "'" << (char)value << "'";
620 } else if (value < 256) {
Ted Kremenek7b6f67b2008-09-13 05:16:45 +0000621 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner4b009652007-07-25 00:24:17 +0000622 } else {
623 // FIXME what to really do here?
624 OS << value;
625 }
626 }
627}
628
629void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
630 bool isSigned = Node->getType()->isSignedIntegerType();
631 OS << Node->getValue().toString(10, isSigned);
632
633 // Emit suffixes. Integer literals are always a builtin integer type.
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000634 switch (Node->getType()->getAsBuiltinType()->getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000635 default: assert(0 && "Unexpected type for integer literal!");
636 case BuiltinType::Int: break; // no suffix.
637 case BuiltinType::UInt: OS << 'U'; break;
638 case BuiltinType::Long: OS << 'L'; break;
639 case BuiltinType::ULong: OS << "UL"; break;
640 case BuiltinType::LongLong: OS << "LL"; break;
641 case BuiltinType::ULongLong: OS << "ULL"; break;
642 }
643}
644void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner6a390402007-08-01 00:23:58 +0000645 // FIXME: print value more precisely.
Chris Lattnere0391b22008-06-07 22:13:43 +0000646 OS << Node->getValueAsApproximateDouble();
Chris Lattner4b009652007-07-25 00:24:17 +0000647}
Chris Lattner1de66eb2007-08-26 03:42:43 +0000648
649void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
650 PrintExpr(Node->getSubExpr());
651 OS << "i";
652}
653
Chris Lattner4b009652007-07-25 00:24:17 +0000654void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
655 if (Str->isWide()) OS << 'L';
656 OS << '"';
Anders Carlsson55bfe0d2007-10-15 02:50:23 +0000657
Chris Lattner4b009652007-07-25 00:24:17 +0000658 // FIXME: this doesn't print wstrings right.
659 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattner5919a852009-01-16 19:25:18 +0000660 unsigned char Char = Str->getStrData()[i];
661
662 switch (Char) {
663 default:
664 if (isprint(Char))
665 OS << (char)Char;
666 else // Output anything hard as an octal escape.
667 OS << '\\'
668 << (char)('0'+ ((Char >> 6) & 7))
669 << (char)('0'+ ((Char >> 3) & 7))
670 << (char)('0'+ ((Char >> 0) & 7));
671 break;
672 // Handle some common non-printable cases to make dumps prettier.
Chris Lattner4b009652007-07-25 00:24:17 +0000673 case '\\': OS << "\\\\"; break;
674 case '"': OS << "\\\""; break;
675 case '\n': OS << "\\n"; break;
676 case '\t': OS << "\\t"; break;
677 case '\a': OS << "\\a"; break;
678 case '\b': OS << "\\b"; break;
679 }
680 }
681 OS << '"';
682}
683void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
684 OS << "(";
685 PrintExpr(Node->getSubExpr());
686 OS << ")";
687}
688void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner2b97b092007-08-23 21:46:40 +0000689 if (!Node->isPostfix()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000690 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner2b97b092007-08-23 21:46:40 +0000691
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000692 // Print a space if this is an "identifier operator" like __real.
Chris Lattner2b97b092007-08-23 21:46:40 +0000693 switch (Node->getOpcode()) {
694 default: break;
Chris Lattner2b97b092007-08-23 21:46:40 +0000695 case UnaryOperator::Real:
696 case UnaryOperator::Imag:
697 case UnaryOperator::Extension:
698 OS << ' ';
699 break;
700 }
701 }
Chris Lattner4b009652007-07-25 00:24:17 +0000702 PrintExpr(Node->getSubExpr());
703
704 if (Node->isPostfix())
705 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner4b009652007-07-25 00:24:17 +0000706}
Chris Lattner2af6a802007-08-30 17:59:59 +0000707
708bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
Eli Friedman342d9432009-02-27 06:44:11 +0000709 if (isa<UnaryOperator>(E)) {
Chris Lattner2af6a802007-08-30 17:59:59 +0000710 // Base case, print the type and comma.
711 OS << E->getType().getAsString() << ", ";
712 return true;
713 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
714 PrintOffsetOfDesignator(ASE->getLHS());
715 OS << "[";
716 PrintExpr(ASE->getRHS());
717 OS << "]";
718 return false;
719 } else {
720 MemberExpr *ME = cast<MemberExpr>(E);
721 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
Chris Lattner6c5ec622008-11-24 04:00:27 +0000722 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getNameAsString();
Chris Lattner2af6a802007-08-30 17:59:59 +0000723 return false;
724 }
725}
726
727void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
728 OS << "__builtin_offsetof(";
729 PrintOffsetOfDesignator(Node->getSubExpr());
730 OS << ")";
731}
732
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000733void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
734 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
735 if (Node->isArgumentType())
736 OS << "(" << Node->getArgumentType().getAsString() << ")";
737 else {
738 OS << " ";
739 PrintExpr(Node->getArgumentExpr());
740 }
Chris Lattner4b009652007-07-25 00:24:17 +0000741}
742void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000743 PrintExpr(Node->getLHS());
Chris Lattner4b009652007-07-25 00:24:17 +0000744 OS << "[";
Ted Kremenek1c1700f2007-08-20 16:18:38 +0000745 PrintExpr(Node->getRHS());
Chris Lattner4b009652007-07-25 00:24:17 +0000746 OS << "]";
747}
748
749void StmtPrinter::VisitCallExpr(CallExpr *Call) {
750 PrintExpr(Call->getCallee());
751 OS << "(";
752 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattner3e254fb2008-04-08 04:40:51 +0000753 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
754 // Don't print any defaulted arguments
755 break;
756 }
757
Chris Lattner4b009652007-07-25 00:24:17 +0000758 if (i) OS << ", ";
759 PrintExpr(Call->getArg(i));
760 }
761 OS << ")";
762}
763void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregorbdbbc2d2009-01-08 22:45:41 +0000764 // FIXME: Suppress printing implicit bases (like "this")
765 PrintExpr(Node->getBase());
766 OS << (Node->isArrow() ? "->" : ".");
767 // FIXME: Suppress printing references to unnamed objects
768 // representing anonymous unions/structs
Douglas Gregor82d44772008-12-20 23:49:58 +0000769 OS << Node->getMemberDecl()->getNameAsString();
Chris Lattner4b009652007-07-25 00:24:17 +0000770}
Nate Begemanaf6ed502008-04-18 23:10:10 +0000771void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Naroffc11705f2007-07-28 23:10:27 +0000772 PrintExpr(Node->getBase());
773 OS << ".";
774 OS << Node->getAccessor().getName();
775}
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +0000776void StmtPrinter::VisitCastExpr(CastExpr *) {
777 assert(0 && "CastExpr is an abstract class");
778}
Douglas Gregor21a04f32008-10-27 19:41:14 +0000779void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *) {
780 assert(0 && "ExplicitCastExpr is an abstract class");
781}
Douglas Gregor035d0882008-10-28 15:36:24 +0000782void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Chris Lattner4b009652007-07-25 00:24:17 +0000783 OS << "(" << Node->getType().getAsString() << ")";
784 PrintExpr(Node->getSubExpr());
785}
786void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
787 OS << "(" << Node->getType().getAsString() << ")";
788 PrintExpr(Node->getInitializer());
789}
790void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
791 // No need to print anything, simply forward to the sub expression.
792 PrintExpr(Node->getSubExpr());
793}
794void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
795 PrintExpr(Node->getLHS());
796 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
797 PrintExpr(Node->getRHS());
798}
Chris Lattner06078d22007-08-25 02:00:02 +0000799void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
800 PrintExpr(Node->getLHS());
801 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
802 PrintExpr(Node->getRHS());
803}
Chris Lattner4b009652007-07-25 00:24:17 +0000804void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
805 PrintExpr(Node->getCond());
Ted Kremenekeecd9e82007-11-26 18:27:54 +0000806
807 if (Node->getLHS()) {
808 OS << " ? ";
809 PrintExpr(Node->getLHS());
810 OS << " : ";
811 }
812 else { // Handle GCC extention where LHS can be NULL.
813 OS << " ?: ";
814 }
815
Chris Lattner4b009652007-07-25 00:24:17 +0000816 PrintExpr(Node->getRHS());
817}
818
819// GNU extensions.
820
Chris Lattnera0d03a72007-08-03 17:31:20 +0000821void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattner4b009652007-07-25 00:24:17 +0000822 OS << "&&" << Node->getLabel()->getName();
823}
824
825void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
826 OS << "(";
827 PrintRawCompoundStmt(E->getSubStmt());
828 OS << ")";
829}
830
Steve Naroff63bad2d2007-08-01 22:05:33 +0000831void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
832 OS << "__builtin_types_compatible_p(";
833 OS << Node->getArgType1().getAsString() << ",";
834 OS << Node->getArgType2().getAsString() << ")";
835}
836
Steve Naroff93c53012007-08-03 21:21:27 +0000837void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
838 OS << "__builtin_choose_expr(";
839 PrintExpr(Node->getCond());
Chris Lattner44fcf4f2007-08-04 00:20:15 +0000840 OS << ", ";
Steve Naroff93c53012007-08-03 21:21:27 +0000841 PrintExpr(Node->getLHS());
Chris Lattner44fcf4f2007-08-04 00:20:15 +0000842 OS << ", ";
Steve Naroff93c53012007-08-03 21:21:27 +0000843 PrintExpr(Node->getRHS());
844 OS << ")";
845}
Chris Lattner4b009652007-07-25 00:24:17 +0000846
Douglas Gregorad4b3792008-11-29 04:51:27 +0000847void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
848 OS << "__null";
849}
850
Eli Friedmand0e9d092008-05-14 19:38:39 +0000851void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
852 OS << "__builtin_shufflevector(";
853 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
854 if (i) OS << ", ";
855 PrintExpr(Node->getExpr(i));
856 }
857 OS << ")";
858}
859
Anders Carlsson762b7c72007-08-31 04:56:16 +0000860void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
861 OS << "{ ";
862 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
863 if (i) OS << ", ";
Douglas Gregorf603b472009-01-28 21:54:33 +0000864 if (Node->getInit(i))
865 PrintExpr(Node->getInit(i));
866 else
867 OS << "0";
Anders Carlsson762b7c72007-08-31 04:56:16 +0000868 }
869 OS << " }";
870}
871
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000872void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregorf603b472009-01-28 21:54:33 +0000873 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
874 DEnd = Node->designators_end();
875 D != DEnd; ++D) {
876 if (D->isFieldDesignator()) {
877 if (D->getDotLoc().isInvalid())
878 OS << D->getFieldName()->getName() << ":";
879 else
880 OS << "." << D->getFieldName()->getName();
881 } else {
882 OS << "[";
883 if (D->isArrayDesignator()) {
884 PrintExpr(Node->getArrayIndex(*D));
885 } else {
886 PrintExpr(Node->getArrayRangeStart(*D));
887 OS << " ... ";
888 PrintExpr(Node->getArrayRangeEnd(*D));
889 }
890 OS << "]";
891 }
892 }
893
894 OS << " = ";
895 PrintExpr(Node->getInit());
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +0000896}
897
Douglas Gregorc9e012a2009-01-29 17:44:32 +0000898void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
899 OS << "/*implicit*/" << Node->getType().getAsString() << "()";
900}
901
Anders Carlsson36760332007-10-15 20:28:48 +0000902void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
903 OS << "va_arg(";
904 PrintExpr(Node->getSubExpr());
905 OS << ", ";
906 OS << Node->getType().getAsString();
907 OS << ")";
908}
909
Chris Lattner4b009652007-07-25 00:24:17 +0000910// C++
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000911void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
912 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
913 "",
914#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
915 Spelling,
916#include "clang/Basic/OperatorKinds.def"
917 };
918
919 OverloadedOperatorKind Kind = Node->getOperator();
920 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
921 if (Node->getNumArgs() == 1) {
922 OS << OpStrings[Kind] << ' ';
923 PrintExpr(Node->getArg(0));
924 } else {
925 PrintExpr(Node->getArg(0));
926 OS << ' ' << OpStrings[Kind];
927 }
928 } else if (Kind == OO_Call) {
929 PrintExpr(Node->getArg(0));
930 OS << '(';
931 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
932 if (ArgIdx > 1)
933 OS << ", ";
934 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
935 PrintExpr(Node->getArg(ArgIdx));
936 }
937 OS << ')';
938 } else if (Kind == OO_Subscript) {
939 PrintExpr(Node->getArg(0));
940 OS << '[';
941 PrintExpr(Node->getArg(1));
942 OS << ']';
943 } else if (Node->getNumArgs() == 1) {
944 OS << OpStrings[Kind] << ' ';
945 PrintExpr(Node->getArg(0));
946 } else if (Node->getNumArgs() == 2) {
947 PrintExpr(Node->getArg(0));
948 OS << ' ' << OpStrings[Kind] << ' ';
949 PrintExpr(Node->getArg(1));
950 } else {
951 assert(false && "unknown overloaded operator");
952 }
953}
Chris Lattner4b009652007-07-25 00:24:17 +0000954
Douglas Gregor3257fb52008-12-22 05:46:06 +0000955void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
956 VisitCallExpr(cast<CallExpr>(Node));
957}
958
Douglas Gregor21a04f32008-10-27 19:41:14 +0000959void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
960 OS << Node->getCastName() << '<';
961 OS << Node->getTypeAsWritten().getAsString() << ">(";
Chris Lattner4b009652007-07-25 00:24:17 +0000962 PrintExpr(Node->getSubExpr());
963 OS << ")";
964}
965
Douglas Gregor21a04f32008-10-27 19:41:14 +0000966void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
967 VisitCXXNamedCastExpr(Node);
968}
969
970void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
971 VisitCXXNamedCastExpr(Node);
972}
973
974void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
975 VisitCXXNamedCastExpr(Node);
976}
977
978void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
979 VisitCXXNamedCastExpr(Node);
980}
981
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000982void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
983 OS << "typeid(";
984 if (Node->isTypeOperand()) {
985 OS << Node->getTypeOperand().getAsString();
986 } else {
987 PrintExpr(Node->getExprOperand());
988 }
989 OS << ")";
990}
991
Chris Lattner4b009652007-07-25 00:24:17 +0000992void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
993 OS << (Node->getValue() ? "true" : "false");
994}
995
Douglas Gregora5b022a2008-11-04 14:32:21 +0000996void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
997 OS << "this";
998}
999
Chris Lattnera7447ba2008-02-26 00:51:44 +00001000void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1001 if (Node->getSubExpr() == 0)
1002 OS << "throw";
1003 else {
1004 OS << "throw ";
1005 PrintExpr(Node->getSubExpr());
1006 }
1007}
1008
Chris Lattner3e254fb2008-04-08 04:40:51 +00001009void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1010 // Nothing to print: we picked up the default argument
1011}
1012
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +00001013void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1014 OS << Node->getType().getAsString();
1015 OS << "(";
1016 PrintExpr(Node->getSubExpr());
1017 OS << ")";
1018}
1019
Douglas Gregor861e7902009-01-16 18:33:17 +00001020void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1021 OS << Node->getType().getAsString();
1022 OS << "(";
1023 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1024 ArgEnd = Node->arg_end();
1025 Arg != ArgEnd; ++Arg) {
1026 if (Arg != Node->arg_begin())
1027 OS << ", ";
1028 PrintExpr(*Arg);
1029 }
1030 OS << ")";
1031}
1032
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +00001033void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
1034 OS << Node->getType().getAsString() << "()";
1035}
1036
Argiris Kirtzidisdbce6c12008-09-09 23:47:53 +00001037void
1038StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
1039 PrintRawDecl(E->getVarDecl());
1040}
1041
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001042void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1043 if (E->isGlobalNew())
1044 OS << "::";
1045 OS << "new ";
1046 unsigned NumPlace = E->getNumPlacementArgs();
1047 if (NumPlace > 0) {
1048 OS << "(";
1049 PrintExpr(E->getPlacementArg(0));
1050 for (unsigned i = 1; i < NumPlace; ++i) {
1051 OS << ", ";
1052 PrintExpr(E->getPlacementArg(i));
1053 }
1054 OS << ") ";
1055 }
1056 if (E->isParenTypeId())
1057 OS << "(";
Sebastian Redl516432a2008-12-02 22:08:59 +00001058 std::string TypeS;
1059 if (Expr *Size = E->getArraySize()) {
1060 llvm::raw_string_ostream s(TypeS);
1061 Size->printPretty(s);
1062 s.flush();
1063 TypeS = "[" + TypeS + "]";
1064 }
1065 E->getAllocatedType().getAsStringInternal(TypeS);
1066 OS << TypeS;
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001067 if (E->isParenTypeId())
1068 OS << ")";
1069
1070 if (E->hasInitializer()) {
1071 OS << "(";
1072 unsigned NumCons = E->getNumConstructorArgs();
1073 if (NumCons > 0) {
1074 PrintExpr(E->getConstructorArg(0));
1075 for (unsigned i = 1; i < NumCons; ++i) {
1076 OS << ", ";
1077 PrintExpr(E->getConstructorArg(i));
1078 }
1079 }
1080 OS << ")";
1081 }
1082}
1083
1084void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1085 if (E->isGlobalDelete())
1086 OS << "::";
1087 OS << "delete ";
1088 if (E->isArrayForm())
1089 OS << "[] ";
1090 PrintExpr(E->getArgument());
1091}
1092
Douglas Gregor4646f9c2009-02-04 15:01:18 +00001093void StmtPrinter::VisitUnresolvedFunctionNameExpr(UnresolvedFunctionNameExpr *E) {
1094 OS << E->getName().getAsString();
Douglas Gregora133e262008-12-06 00:22:45 +00001095}
1096
Sebastian Redl39c0f6f2009-01-05 20:52:13 +00001097static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1098 switch (UTT) {
1099 default: assert(false && "Unknown type trait");
1100 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1101 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1102 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1103 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1104 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1105 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1106 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1107 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1108 case UTT_IsAbstract: return "__is_abstract";
1109 case UTT_IsClass: return "__is_class";
1110 case UTT_IsEmpty: return "__is_empty";
1111 case UTT_IsEnum: return "__is_enum";
1112 case UTT_IsPOD: return "__is_pod";
1113 case UTT_IsPolymorphic: return "__is_polymorphic";
1114 case UTT_IsUnion: return "__is_union";
1115 }
1116}
1117
1118void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1119 OS << getTypeTraitName(E->getTrait()) << "("
1120 << E->getQueriedType().getAsString() << ")";
1121}
1122
Anders Carlssona66cad42007-08-21 17:43:55 +00001123// Obj-C
1124
1125void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1126 OS << "@";
1127 VisitStringLiteral(Node->getString());
1128}
Chris Lattner4b009652007-07-25 00:24:17 +00001129
Anders Carlsson8be1d402007-08-22 15:14:15 +00001130void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner6c5ec622008-11-24 04:00:27 +00001131 OS << "@encode(" << Node->getEncodedType().getAsString() << ')';
Anders Carlsson8be1d402007-08-22 15:14:15 +00001132}
1133
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001134void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner6c5ec622008-11-24 04:00:27 +00001135 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001136}
1137
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001138void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner6c5ec622008-11-24 04:00:27 +00001139 OS << "@protocol(" << Node->getProtocol()->getNameAsString() << ')';
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001140}
1141
Steve Naroffc39ca262007-09-18 23:55:05 +00001142void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1143 OS << "[";
Steve Narofffa465d12007-10-02 20:01:56 +00001144 Expr *receiver = Mess->getReceiver();
1145 if (receiver) PrintExpr(receiver);
1146 else OS << Mess->getClassName()->getName();
Ted Kremenekbfec9492008-05-02 17:32:38 +00001147 OS << ' ';
Ted Kremenek2287cee2008-04-16 04:30:16 +00001148 Selector selector = Mess->getSelector();
Steve Narofffa465d12007-10-02 20:01:56 +00001149 if (selector.isUnarySelector()) {
Ted Kremenekbfec9492008-05-02 17:32:38 +00001150 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Narofffa465d12007-10-02 20:01:56 +00001151 } else {
1152 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremenekbfec9492008-05-02 17:32:38 +00001153 if (i < selector.getNumArgs()) {
1154 if (i > 0) OS << ' ';
1155 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner6c5ec622008-11-24 04:00:27 +00001156 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremenekbfec9492008-05-02 17:32:38 +00001157 else
1158 OS << ":";
1159 }
1160 else OS << ", "; // Handle variadic methods.
1161
Steve Narofffa465d12007-10-02 20:01:56 +00001162 PrintExpr(Mess->getArg(i));
1163 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001164 }
1165 OS << "]";
1166}
1167
Douglas Gregord8606632008-11-04 14:56:14 +00001168void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) {
1169 OS << "super";
1170}
1171
Steve Naroff52a81c02008-09-03 18:15:37 +00001172void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff9ac456d2008-10-08 17:01:13 +00001173 BlockDecl *BD = Node->getBlockDecl();
Steve Naroff52a81c02008-09-03 18:15:37 +00001174 OS << "^";
1175
1176 const FunctionType *AFT = Node->getFunctionType();
1177
Douglas Gregor4fa58902009-02-26 23:50:07 +00001178 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroff52a81c02008-09-03 18:15:37 +00001179 OS << "()";
Douglas Gregor4fa58902009-02-26 23:50:07 +00001180 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroff52a81c02008-09-03 18:15:37 +00001181 OS << '(';
1182 std::string ParamStr;
Steve Naroff9ac456d2008-10-08 17:01:13 +00001183 for (BlockDecl::param_iterator AI = BD->param_begin(),
1184 E = BD->param_end(); AI != E; ++AI) {
1185 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner6c5ec622008-11-24 04:00:27 +00001186 ParamStr = (*AI)->getNameAsString();
Steve Naroff52a81c02008-09-03 18:15:37 +00001187 (*AI)->getType().getAsStringInternal(ParamStr);
1188 OS << ParamStr;
1189 }
1190
Douglas Gregor4fa58902009-02-26 23:50:07 +00001191 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroff52a81c02008-09-03 18:15:37 +00001192 if (FT->isVariadic()) {
Steve Naroff9ac456d2008-10-08 17:01:13 +00001193 if (!BD->param_empty()) OS << ", ";
Steve Naroff52a81c02008-09-03 18:15:37 +00001194 OS << "...";
1195 }
1196 OS << ')';
1197 }
1198}
1199
Steve Naroff52a81c02008-09-03 18:15:37 +00001200void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Chris Lattner6c5ec622008-11-24 04:00:27 +00001201 OS << Node->getDecl()->getNameAsString();
Steve Naroff52a81c02008-09-03 18:15:37 +00001202}
Chris Lattner4b009652007-07-25 00:24:17 +00001203//===----------------------------------------------------------------------===//
1204// Stmt method implementations
1205//===----------------------------------------------------------------------===//
1206
Chris Lattner95578782007-08-08 22:51:59 +00001207void Stmt::dumpPretty() const {
Daniel Dunbar24c169d2009-03-10 18:00:19 +00001208 printPretty(llvm::errs());
Chris Lattner4b009652007-07-25 00:24:17 +00001209}
1210
Mike Stumpc43f4fc2009-02-10 20:16:46 +00001211void Stmt::printPretty(llvm::raw_ostream &OS, PrinterHelper* Helper,
1212 unsigned I, bool NoIndent) const {
Chris Lattner4b009652007-07-25 00:24:17 +00001213 if (this == 0) {
1214 OS << "<NULL>";
1215 return;
1216 }
1217
Mike Stumpc43f4fc2009-02-10 20:16:46 +00001218 StmtPrinter P(OS, Helper, I, NoIndent);
Chris Lattner7ba21fa2007-08-21 04:04:25 +00001219 P.Visit(const_cast<Stmt*>(this));
Chris Lattner4b009652007-07-25 00:24:17 +00001220}
Ted Kremenek08176a52007-08-31 21:30:12 +00001221
1222//===----------------------------------------------------------------------===//
1223// PrinterHelper
1224//===----------------------------------------------------------------------===//
1225
1226// Implement virtual destructor.
Gabor Greif61ce98c2007-09-11 15:32:40 +00001227PrinterHelper::~PrinterHelper() {}