blob: 7a558ab8dd7b8907b6167e07585de60baaf6d5da [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;
Ted Kremenek42a509f2007-08-31 21:30:12 +000032 clang::PrinterHelper* Helper;
Reid Spencer5f016e22007-07-11 17:01:13 +000033 public:
Ted Kremeneka95d3752008-09-13 05:16:45 +000034 StmtPrinter(llvm::raw_ostream &os, PrinterHelper* helper) :
Ted Kremenek42a509f2007-08-31 21:30:12 +000035 OS(os), IndentLevel(0), Helper(helper) {}
Reid Spencer5f016e22007-07-11 17:01:13 +000036
37 void PrintStmt(Stmt *S, int SubIndent = 1) {
38 IndentLevel += SubIndent;
39 if (S && isa<Expr>(S)) {
40 // If this is an expr used in a stmt context, indent and newline it.
41 Indent();
Chris Lattnerc5598cb2007-08-21 04:04:25 +000042 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000043 OS << ";\n";
44 } else if (S) {
Chris Lattnerc5598cb2007-08-21 04:04:25 +000045 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000046 } else {
47 Indent() << "<<<NULL STATEMENT>>>\n";
48 }
49 IndentLevel -= SubIndent;
50 }
51
52 void PrintRawCompoundStmt(CompoundStmt *S);
53 void PrintRawDecl(Decl *D);
Ted Kremenekecd64c52008-10-06 18:39:36 +000054 void PrintRawDeclStmt(DeclStmt *S);
Reid Spencer5f016e22007-07-11 17:01:13 +000055 void PrintRawIfStmt(IfStmt *If);
Sebastian Redl8351da02008-12-22 21:35:02 +000056 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
Reid Spencer5f016e22007-07-11 17:01:13 +000057
58 void PrintExpr(Expr *E) {
59 if (E)
Chris Lattnerc5598cb2007-08-21 04:04:25 +000060 Visit(E);
Reid Spencer5f016e22007-07-11 17:01:13 +000061 else
62 OS << "<null expr>";
63 }
64
Ted Kremeneka95d3752008-09-13 05:16:45 +000065 llvm::raw_ostream &Indent(int Delta = 0) const {
Reid Spencer5f016e22007-07-11 17:01:13 +000066 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
67 OS << " ";
68 return OS;
69 }
70
Chris Lattner704fe352007-08-30 17:59:59 +000071 bool PrintOffsetOfDesignator(Expr *E);
72 void VisitUnaryOffsetOf(UnaryOperator *Node);
73
Ted Kremenek42a509f2007-08-31 21:30:12 +000074 void Visit(Stmt* S) {
75 if (Helper && Helper->handledStmt(S,OS))
76 return;
77 else StmtVisitor<StmtPrinter>::Visit(S);
78 }
79
Chris Lattnerc5598cb2007-08-21 04:04:25 +000080 void VisitStmt(Stmt *Node);
Douglas Gregorf2cad862008-11-14 12:46:07 +000081#define STMT(CLASS, PARENT) \
Chris Lattnerc5598cb2007-08-21 04:04:25 +000082 void Visit##CLASS(CLASS *Node);
Reid Spencer5f016e22007-07-11 17:01:13 +000083#include "clang/AST/StmtNodes.def"
84 };
85}
86
87//===----------------------------------------------------------------------===//
88// Stmt printing methods.
89//===----------------------------------------------------------------------===//
90
91void StmtPrinter::VisitStmt(Stmt *Node) {
92 Indent() << "<<unknown stmt type>>\n";
93}
94
95/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
96/// with no newline after the }.
97void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
98 OS << "{\n";
99 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
100 I != E; ++I)
101 PrintStmt(*I);
102
103 Indent() << "}";
104}
105
106void StmtPrinter::PrintRawDecl(Decl *D) {
107 // FIXME: Need to complete/beautify this... this code simply shows the
108 // nodes are where they need to be.
109 if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
110 OS << "typedef " << localType->getUnderlyingType().getAsString();
Chris Lattner39f34e92008-11-24 04:00:27 +0000111 OS << " " << localType->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
113 // Emit storage class for vardecls.
114 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
115 switch (V->getStorageClass()) {
116 default: assert(0 && "Unknown storage class!");
117 case VarDecl::None: break;
118 case VarDecl::Extern: OS << "extern "; break;
119 case VarDecl::Static: OS << "static "; break;
120 case VarDecl::Auto: OS << "auto "; break;
121 case VarDecl::Register: OS << "register "; break;
122 }
123 }
124
Chris Lattner39f34e92008-11-24 04:00:27 +0000125 std::string Name = VD->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 VD->getType().getAsStringInternal(Name);
127 OS << Name;
128
Chris Lattner24c39902007-07-12 00:36:32 +0000129 // If this is a vardecl with an initializer, emit it.
130 if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
131 if (V->getInit()) {
132 OS << " = ";
133 PrintExpr(V->getInit());
134 }
135 }
Steve Naroff91578f32007-11-17 21:21:01 +0000136 } else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
137 // print a free standing tag decl (e.g. "struct x;").
138 OS << TD->getKindName();
139 OS << " ";
140 if (const IdentifierInfo *II = TD->getIdentifier())
141 OS << II->getName();
142 else
143 OS << "<anonymous>";
144 // FIXME: print tag bodies.
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 } else {
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 assert(0 && "Unexpected decl");
147 }
148}
149
Ted Kremenekecd64c52008-10-06 18:39:36 +0000150void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
151 bool isFirst = false;
152
153 for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();
154 I != E; ++I) {
155
156 if (!isFirst) OS << ", ";
157 else isFirst = false;
158
159 PrintRawDecl(*I);
160 }
161}
Reid Spencer5f016e22007-07-11 17:01:13 +0000162
163void StmtPrinter::VisitNullStmt(NullStmt *Node) {
164 Indent() << ";\n";
165}
166
167void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Ted Kremenekecd64c52008-10-06 18:39:36 +0000168 for (DeclStmt::decl_iterator I = Node->decl_begin(), E = Node->decl_end();
169 I!=E; ++I) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 Indent();
Ted Kremenekecd64c52008-10-06 18:39:36 +0000171 PrintRawDecl(*I);
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 OS << ";\n";
173 }
174}
175
176void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
177 Indent();
178 PrintRawCompoundStmt(Node);
179 OS << "\n";
180}
181
182void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
183 Indent(-1) << "case ";
184 PrintExpr(Node->getLHS());
185 if (Node->getRHS()) {
186 OS << " ... ";
187 PrintExpr(Node->getRHS());
188 }
189 OS << ":\n";
190
191 PrintStmt(Node->getSubStmt(), 0);
192}
193
194void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
195 Indent(-1) << "default:\n";
196 PrintStmt(Node->getSubStmt(), 0);
197}
198
199void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
200 Indent(-1) << Node->getName() << ":\n";
201 PrintStmt(Node->getSubStmt(), 0);
202}
203
204void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
205 OS << "if ";
206 PrintExpr(If->getCond());
207
208 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
209 OS << ' ';
210 PrintRawCompoundStmt(CS);
211 OS << (If->getElse() ? ' ' : '\n');
212 } else {
213 OS << '\n';
214 PrintStmt(If->getThen());
215 if (If->getElse()) Indent();
216 }
217
218 if (Stmt *Else = If->getElse()) {
219 OS << "else";
220
221 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
222 OS << ' ';
223 PrintRawCompoundStmt(CS);
224 OS << '\n';
225 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
226 OS << ' ';
227 PrintRawIfStmt(ElseIf);
228 } else {
229 OS << '\n';
230 PrintStmt(If->getElse());
231 }
232 }
233}
234
235void StmtPrinter::VisitIfStmt(IfStmt *If) {
236 Indent();
237 PrintRawIfStmt(If);
238}
239
240void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
241 Indent() << "switch (";
242 PrintExpr(Node->getCond());
243 OS << ")";
244
245 // Pretty print compoundstmt bodies (very common).
246 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
247 OS << " ";
248 PrintRawCompoundStmt(CS);
249 OS << "\n";
250 } else {
251 OS << "\n";
252 PrintStmt(Node->getBody());
253 }
254}
255
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000256void StmtPrinter::VisitSwitchCase(SwitchCase*) {
257 assert(0 && "SwitchCase is an abstract class");
258}
259
Reid Spencer5f016e22007-07-11 17:01:13 +0000260void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
261 Indent() << "while (";
262 PrintExpr(Node->getCond());
263 OS << ")\n";
264 PrintStmt(Node->getBody());
265}
266
267void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner8bdcc472007-09-15 21:49:37 +0000268 Indent() << "do ";
269 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
270 PrintRawCompoundStmt(CS);
271 OS << " ";
272 } else {
273 OS << "\n";
274 PrintStmt(Node->getBody());
275 Indent();
276 }
277
278 OS << "while ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000279 PrintExpr(Node->getCond());
280 OS << ";\n";
281}
282
283void StmtPrinter::VisitForStmt(ForStmt *Node) {
284 Indent() << "for (";
285 if (Node->getInit()) {
286 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000287 PrintRawDeclStmt(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000288 else
289 PrintExpr(cast<Expr>(Node->getInit()));
290 }
Chris Lattner8bdcc472007-09-15 21:49:37 +0000291 OS << ";";
292 if (Node->getCond()) {
293 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000294 PrintExpr(Node->getCond());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000295 }
296 OS << ";";
297 if (Node->getInc()) {
298 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000299 PrintExpr(Node->getInc());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000300 }
301 OS << ") ";
302
303 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
304 PrintRawCompoundStmt(CS);
305 OS << "\n";
306 } else {
307 OS << "\n";
308 PrintStmt(Node->getBody());
309 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000310}
311
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000312void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000313 Indent() << "for (";
314 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000315 PrintRawDeclStmt(DS);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000316 else
317 PrintExpr(cast<Expr>(Node->getElement()));
318 OS << " in ";
319 PrintExpr(Node->getCollection());
320 OS << ") ";
321
322 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
323 PrintRawCompoundStmt(CS);
324 OS << "\n";
325 } else {
326 OS << "\n";
327 PrintStmt(Node->getBody());
328 }
329}
330
Reid Spencer5f016e22007-07-11 17:01:13 +0000331void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
332 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
333}
334
335void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
336 Indent() << "goto *";
337 PrintExpr(Node->getTarget());
338 OS << ";\n";
339}
340
341void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
342 Indent() << "continue;\n";
343}
344
345void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
346 Indent() << "break;\n";
347}
348
349
350void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
351 Indent() << "return";
352 if (Node->getRetValue()) {
353 OS << " ";
354 PrintExpr(Node->getRetValue());
355 }
356 OS << ";\n";
357}
358
Chris Lattnerfe795952007-10-29 04:04:16 +0000359
360void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson39c47b52007-11-23 23:12:25 +0000361 Indent() << "asm ";
362
363 if (Node->isVolatile())
364 OS << "volatile ";
365
366 OS << "(";
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000367 VisitStringLiteral(Node->getAsmString());
Anders Carlssonb235fc22007-11-22 01:36:19 +0000368
369 // Outputs
370 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
371 Node->getNumClobbers() != 0)
372 OS << " : ";
373
374 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
375 if (i != 0)
376 OS << ", ";
377
378 if (!Node->getOutputName(i).empty()) {
379 OS << '[';
380 OS << Node->getOutputName(i);
381 OS << "] ";
382 }
383
384 VisitStringLiteral(Node->getOutputConstraint(i));
385 OS << " ";
386 Visit(Node->getOutputExpr(i));
387 }
388
389 // Inputs
390 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
391 OS << " : ";
392
393 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
394 if (i != 0)
395 OS << ", ";
396
397 if (!Node->getInputName(i).empty()) {
398 OS << '[';
399 OS << Node->getInputName(i);
400 OS << "] ";
401 }
402
403 VisitStringLiteral(Node->getInputConstraint(i));
404 OS << " ";
405 Visit(Node->getInputExpr(i));
406 }
407
408 // Clobbers
409 if (Node->getNumClobbers() != 0)
410 OS << " : ";
411
412 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
413 if (i != 0)
414 OS << ", ";
415
416 VisitStringLiteral(Node->getClobber(i));
417 }
418
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000419 OS << ");\n";
Chris Lattnerfe795952007-10-29 04:04:16 +0000420}
421
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000422void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000423 Indent() << "@try";
424 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
425 PrintRawCompoundStmt(TS);
426 OS << "\n";
427 }
428
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000429 for (ObjCAtCatchStmt *catchStmt =
430 static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts());
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000431 catchStmt;
432 catchStmt =
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000433 static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000434 Indent() << "@catch(";
435 if (catchStmt->getCatchParamStmt()) {
436 if (DeclStmt *DS = dyn_cast<DeclStmt>(catchStmt->getCatchParamStmt()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000437 PrintRawDeclStmt(DS);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000438 }
439 OS << ")";
440 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody()))
441 {
442 PrintRawCompoundStmt(CS);
443 OS << "\n";
444 }
445 }
446
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000447 if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>(
Fariborz Jahanian1e7eab42007-11-07 00:46:42 +0000448 Node->getFinallyStmt())) {
449 Indent() << "@finally";
450 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000451 OS << "\n";
452 }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000453}
454
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000455void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000456}
457
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000458void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000459 Indent() << "@catch (...) { /* todo */ } \n";
460}
461
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000462void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000463 Indent() << "@throw";
464 if (Node->getThrowExpr()) {
465 OS << " ";
466 PrintExpr(Node->getThrowExpr());
467 }
468 OS << ";\n";
469}
470
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000471void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000472 Indent() << "@synchronized (";
473 PrintExpr(Node->getSynchExpr());
474 OS << ")";
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000475 PrintRawCompoundStmt(Node->getSynchBody());
476 OS << "\n";
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000477}
478
Sebastian Redl8351da02008-12-22 21:35:02 +0000479void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
480 OS << "catch (";
Sebastian Redl4b07b292008-12-22 19:15:10 +0000481 if (Decl *ExDecl = Node->getExceptionDecl())
482 PrintRawDecl(ExDecl);
483 else
484 OS << "...";
485 OS << ") ";
486 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl8351da02008-12-22 21:35:02 +0000487}
488
489void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
490 Indent();
491 PrintRawCXXCatchStmt(Node);
492 OS << "\n";
493}
494
495void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
496 Indent() << "try ";
497 PrintRawCompoundStmt(Node->getTryBlock());
498 for(unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
499 OS << " ";
500 PrintRawCXXCatchStmt(Node->getHandler(i));
501 }
Sebastian Redl4b07b292008-12-22 19:15:10 +0000502 OS << "\n";
503}
504
Reid Spencer5f016e22007-07-11 17:01:13 +0000505//===----------------------------------------------------------------------===//
506// Expr printing methods.
507//===----------------------------------------------------------------------===//
508
509void StmtPrinter::VisitExpr(Expr *Node) {
510 OS << "<<unknown expr type>>";
511}
512
513void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +0000514 OS << Node->getDecl()->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000515}
516
Douglas Gregor1a49af92009-01-06 05:10:23 +0000517void StmtPrinter::VisitQualifiedDeclRefExpr(QualifiedDeclRefExpr *Node) {
518 // FIXME: Should we keep enough information in QualifiedDeclRefExpr
519 // to produce the same qualification that the user wrote?
520 llvm::SmallVector<DeclContext *, 4> Contexts;
521
522 NamedDecl *D = Node->getDecl();
523
524 // Build up a stack of contexts.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000525 DeclContext *Ctx = D->getDeclContext();
Douglas Gregor1a49af92009-01-06 05:10:23 +0000526 for (; Ctx; Ctx = Ctx->getParent())
527 if (!Ctx->isTransparentContext())
528 Contexts.push_back(Ctx);
529
530 while (!Contexts.empty()) {
531 DeclContext *Ctx = Contexts.back();
532 if (isa<TranslationUnitDecl>(Ctx))
533 OS << "::";
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000534 else if (NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
535 OS << ND->getNameAsString() << "::";
Douglas Gregor1a49af92009-01-06 05:10:23 +0000536 Contexts.pop_back();
537 }
538
539 OS << D->getNameAsString();
540}
541
Steve Naroff7779db42007-11-12 14:29:37 +0000542void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000543 if (Node->getBase()) {
544 PrintExpr(Node->getBase());
545 OS << (Node->isArrow() ? "->" : ".");
546 }
Chris Lattner39f34e92008-11-24 04:00:27 +0000547 OS << Node->getDecl()->getNameAsString();
Steve Naroff7779db42007-11-12 14:29:37 +0000548}
549
Steve Naroffae784072008-05-30 00:40:33 +0000550void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
551 if (Node->getBase()) {
552 PrintExpr(Node->getBase());
553 OS << ".";
554 }
Steve Naroffc77a6362008-12-04 16:24:46 +0000555 OS << Node->getProperty()->getNameAsCString();
Steve Naroffae784072008-05-30 00:40:33 +0000556}
557
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000558void StmtPrinter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node) {
559 if (Node->getBase()) {
560 PrintExpr(Node->getBase());
561 OS << ".";
562 }
563 // FIXME: Setter/Getter names
564}
565
Chris Lattnerd9f69102008-08-10 01:53:14 +0000566void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson22742662007-07-21 05:21:51 +0000567 switch (Node->getIdentType()) {
568 default:
569 assert(0 && "unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000570 case PredefinedExpr::Func:
Anders Carlsson22742662007-07-21 05:21:51 +0000571 OS << "__func__";
572 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000573 case PredefinedExpr::Function:
Anders Carlsson22742662007-07-21 05:21:51 +0000574 OS << "__FUNCTION__";
575 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000576 case PredefinedExpr::PrettyFunction:
Anders Carlsson22742662007-07-21 05:21:51 +0000577 OS << "__PRETTY_FUNCTION__";
578 break;
579 }
580}
581
Reid Spencer5f016e22007-07-11 17:01:13 +0000582void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000583 unsigned value = Node->getValue();
Chris Lattnerc250aae2008-06-07 22:35:38 +0000584 if (Node->isWide())
585 OS << "L";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000586 switch (value) {
587 case '\\':
588 OS << "'\\\\'";
589 break;
590 case '\'':
591 OS << "'\\''";
592 break;
593 case '\a':
594 // TODO: K&R: the meaning of '\\a' is different in traditional C
595 OS << "'\\a'";
596 break;
597 case '\b':
598 OS << "'\\b'";
599 break;
600 // Nonstandard escape sequence.
601 /*case '\e':
602 OS << "'\\e'";
603 break;*/
604 case '\f':
605 OS << "'\\f'";
606 break;
607 case '\n':
608 OS << "'\\n'";
609 break;
610 case '\r':
611 OS << "'\\r'";
612 break;
613 case '\t':
614 OS << "'\\t'";
615 break;
616 case '\v':
617 OS << "'\\v'";
618 break;
619 default:
Ted Kremenek471733d2008-02-23 00:52:04 +0000620 if (value < 256 && isprint(value)) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000621 OS << "'" << (char)value << "'";
622 } else if (value < 256) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000623 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000624 } else {
625 // FIXME what to really do here?
626 OS << value;
627 }
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000628 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000629}
630
631void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
632 bool isSigned = Node->getType()->isSignedIntegerType();
633 OS << Node->getValue().toString(10, isSigned);
634
635 // Emit suffixes. Integer literals are always a builtin integer type.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000636 switch (Node->getType()->getAsBuiltinType()->getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000637 default: assert(0 && "Unexpected type for integer literal!");
638 case BuiltinType::Int: break; // no suffix.
639 case BuiltinType::UInt: OS << 'U'; break;
640 case BuiltinType::Long: OS << 'L'; break;
641 case BuiltinType::ULong: OS << "UL"; break;
642 case BuiltinType::LongLong: OS << "LL"; break;
643 case BuiltinType::ULongLong: OS << "ULL"; break;
644 }
645}
646void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner86e499d2007-08-01 00:23:58 +0000647 // FIXME: print value more precisely.
Chris Lattnerda8249e2008-06-07 22:13:43 +0000648 OS << Node->getValueAsApproximateDouble();
Reid Spencer5f016e22007-07-11 17:01:13 +0000649}
Chris Lattner5d661452007-08-26 03:42:43 +0000650
651void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
652 PrintExpr(Node->getSubExpr());
653 OS << "i";
654}
655
Reid Spencer5f016e22007-07-11 17:01:13 +0000656void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
657 if (Str->isWide()) OS << 'L';
658 OS << '"';
Anders Carlssonee98ac52007-10-15 02:50:23 +0000659
Reid Spencer5f016e22007-07-11 17:01:13 +0000660 // FIXME: this doesn't print wstrings right.
661 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattner9a81c872009-01-16 19:25:18 +0000662 unsigned char Char = Str->getStrData()[i];
663
664 switch (Char) {
665 default:
666 if (isprint(Char))
667 OS << (char)Char;
668 else // Output anything hard as an octal escape.
669 OS << '\\'
670 << (char)('0'+ ((Char >> 6) & 7))
671 << (char)('0'+ ((Char >> 3) & 7))
672 << (char)('0'+ ((Char >> 0) & 7));
673 break;
674 // Handle some common non-printable cases to make dumps prettier.
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 case '\\': OS << "\\\\"; break;
676 case '"': OS << "\\\""; break;
677 case '\n': OS << "\\n"; break;
678 case '\t': OS << "\\t"; break;
679 case '\a': OS << "\\a"; break;
680 case '\b': OS << "\\b"; break;
681 }
682 }
683 OS << '"';
684}
685void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
686 OS << "(";
687 PrintExpr(Node->getSubExpr());
688 OS << ")";
689}
690void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +0000691 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000692 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner296bf192007-08-23 21:46:40 +0000693
Sebastian Redl05189992008-11-11 17:56:53 +0000694 // Print a space if this is an "identifier operator" like __real.
Chris Lattner296bf192007-08-23 21:46:40 +0000695 switch (Node->getOpcode()) {
696 default: break;
Chris Lattner296bf192007-08-23 21:46:40 +0000697 case UnaryOperator::Real:
698 case UnaryOperator::Imag:
699 case UnaryOperator::Extension:
700 OS << ' ';
701 break;
702 }
703 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000704 PrintExpr(Node->getSubExpr());
705
706 if (Node->isPostfix())
707 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +0000708}
Chris Lattner704fe352007-08-30 17:59:59 +0000709
710bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
711 if (isa<CompoundLiteralExpr>(E)) {
712 // Base case, print the type and comma.
713 OS << E->getType().getAsString() << ", ";
714 return true;
715 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
716 PrintOffsetOfDesignator(ASE->getLHS());
717 OS << "[";
718 PrintExpr(ASE->getRHS());
719 OS << "]";
720 return false;
721 } else {
722 MemberExpr *ME = cast<MemberExpr>(E);
723 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
Chris Lattner39f34e92008-11-24 04:00:27 +0000724 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getNameAsString();
Chris Lattner704fe352007-08-30 17:59:59 +0000725 return false;
726 }
727}
728
729void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
730 OS << "__builtin_offsetof(";
731 PrintOffsetOfDesignator(Node->getSubExpr());
732 OS << ")";
733}
734
Sebastian Redl05189992008-11-11 17:56:53 +0000735void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
736 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
737 if (Node->isArgumentType())
738 OS << "(" << Node->getArgumentType().getAsString() << ")";
739 else {
740 OS << " ";
741 PrintExpr(Node->getArgumentExpr());
742 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000743}
744void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +0000745 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000746 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +0000747 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000748 OS << "]";
749}
750
751void StmtPrinter::VisitCallExpr(CallExpr *Call) {
752 PrintExpr(Call->getCallee());
753 OS << "(";
754 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattner04421082008-04-08 04:40:51 +0000755 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
756 // Don't print any defaulted arguments
757 break;
758 }
759
Reid Spencer5f016e22007-07-11 17:01:13 +0000760 if (i) OS << ", ";
761 PrintExpr(Call->getArg(i));
762 }
763 OS << ")";
764}
765void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregorb3eef682009-01-08 22:45:41 +0000766 // FIXME: Suppress printing implicit bases (like "this")
767 PrintExpr(Node->getBase());
768 OS << (Node->isArrow() ? "->" : ".");
769 // FIXME: Suppress printing references to unnamed objects
770 // representing anonymous unions/structs
Douglas Gregor86f19402008-12-20 23:49:58 +0000771 OS << Node->getMemberDecl()->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000772}
Nate Begeman213541a2008-04-18 23:10:10 +0000773void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +0000774 PrintExpr(Node->getBase());
775 OS << ".";
776 OS << Node->getAccessor().getName();
777}
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000778void StmtPrinter::VisitCastExpr(CastExpr *) {
779 assert(0 && "CastExpr is an abstract class");
780}
Douglas Gregor49badde2008-10-27 19:41:14 +0000781void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *) {
782 assert(0 && "ExplicitCastExpr is an abstract class");
783}
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000784void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000785 OS << "(" << Node->getType().getAsString() << ")";
Reid Spencer5f016e22007-07-11 17:01:13 +0000786 PrintExpr(Node->getSubExpr());
787}
Steve Naroffaff1edd2007-07-19 21:32:11 +0000788void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
789 OS << "(" << Node->getType().getAsString() << ")";
790 PrintExpr(Node->getInitializer());
791}
Steve Naroff49b45262007-07-13 16:58:59 +0000792void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroff90045e82007-07-13 23:32:42 +0000793 // No need to print anything, simply forward to the sub expression.
794 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +0000795}
Reid Spencer5f016e22007-07-11 17:01:13 +0000796void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
797 PrintExpr(Node->getLHS());
798 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
799 PrintExpr(Node->getRHS());
800}
Chris Lattnereb14fe82007-08-25 02:00:02 +0000801void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
802 PrintExpr(Node->getLHS());
803 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
804 PrintExpr(Node->getRHS());
805}
Reid Spencer5f016e22007-07-11 17:01:13 +0000806void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
807 PrintExpr(Node->getCond());
Ted Kremenek8e911c42007-11-26 18:27:54 +0000808
809 if (Node->getLHS()) {
810 OS << " ? ";
811 PrintExpr(Node->getLHS());
812 OS << " : ";
813 }
814 else { // Handle GCC extention where LHS can be NULL.
815 OS << " ?: ";
816 }
817
Reid Spencer5f016e22007-07-11 17:01:13 +0000818 PrintExpr(Node->getRHS());
819}
820
821// GNU extensions.
822
Chris Lattner6481a572007-08-03 17:31:20 +0000823void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000824 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000825}
826
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000827void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
828 OS << "(";
829 PrintRawCompoundStmt(E->getSubStmt());
830 OS << ")";
831}
832
Steve Naroffd34e9152007-08-01 22:05:33 +0000833void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
834 OS << "__builtin_types_compatible_p(";
835 OS << Node->getArgType1().getAsString() << ",";
836 OS << Node->getArgType2().getAsString() << ")";
837}
838
Steve Naroffd04fdd52007-08-03 21:21:27 +0000839void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
840 OS << "__builtin_choose_expr(";
841 PrintExpr(Node->getCond());
Chris Lattner94f05e32007-08-04 00:20:15 +0000842 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000843 PrintExpr(Node->getLHS());
Chris Lattner94f05e32007-08-04 00:20:15 +0000844 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000845 PrintExpr(Node->getRHS());
846 OS << ")";
847}
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000848
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000849void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
850 OS << "__null";
851}
852
Nate Begemane2ce1d92008-01-17 17:46:27 +0000853void StmtPrinter::VisitOverloadExpr(OverloadExpr *Node) {
854 OS << "__builtin_overload(";
Nate Begeman67295d02008-01-30 20:50:20 +0000855 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
Nate Begemane2ce1d92008-01-17 17:46:27 +0000856 if (i) OS << ", ";
Nate Begeman67295d02008-01-30 20:50:20 +0000857 PrintExpr(Node->getExpr(i));
Nate Begemane2ce1d92008-01-17 17:46:27 +0000858 }
859 OS << ")";
860}
861
Eli Friedmand38617c2008-05-14 19:38:39 +0000862void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
863 OS << "__builtin_shufflevector(";
864 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
865 if (i) OS << ", ";
866 PrintExpr(Node->getExpr(i));
867 }
868 OS << ")";
869}
870
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000871void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
872 OS << "{ ";
873 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
874 if (i) OS << ", ";
Douglas Gregor4c678342009-01-28 21:54:33 +0000875 if (Node->getInit(i))
876 PrintExpr(Node->getInit(i));
877 else
878 OS << "0";
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000879 }
880 OS << " }";
881}
882
Douglas Gregor05c13a32009-01-22 00:58:24 +0000883void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000884 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
885 DEnd = Node->designators_end();
886 D != DEnd; ++D) {
887 if (D->isFieldDesignator()) {
888 if (D->getDotLoc().isInvalid())
889 OS << D->getFieldName()->getName() << ":";
890 else
891 OS << "." << D->getFieldName()->getName();
892 } else {
893 OS << "[";
894 if (D->isArrayDesignator()) {
895 PrintExpr(Node->getArrayIndex(*D));
896 } else {
897 PrintExpr(Node->getArrayRangeStart(*D));
898 OS << " ... ";
899 PrintExpr(Node->getArrayRangeEnd(*D));
900 }
901 OS << "]";
902 }
903 }
904
905 OS << " = ";
906 PrintExpr(Node->getInit());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000907}
908
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000909void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
910 OS << "va_arg(";
911 PrintExpr(Node->getSubExpr());
912 OS << ", ";
913 OS << Node->getType().getAsString();
914 OS << ")";
915}
916
Reid Spencer5f016e22007-07-11 17:01:13 +0000917// C++
Douglas Gregorb4609802008-11-14 16:09:21 +0000918void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
919 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
920 "",
921#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
922 Spelling,
923#include "clang/Basic/OperatorKinds.def"
924 };
925
926 OverloadedOperatorKind Kind = Node->getOperator();
927 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
928 if (Node->getNumArgs() == 1) {
929 OS << OpStrings[Kind] << ' ';
930 PrintExpr(Node->getArg(0));
931 } else {
932 PrintExpr(Node->getArg(0));
933 OS << ' ' << OpStrings[Kind];
934 }
935 } else if (Kind == OO_Call) {
936 PrintExpr(Node->getArg(0));
937 OS << '(';
938 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
939 if (ArgIdx > 1)
940 OS << ", ";
941 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
942 PrintExpr(Node->getArg(ArgIdx));
943 }
944 OS << ')';
945 } else if (Kind == OO_Subscript) {
946 PrintExpr(Node->getArg(0));
947 OS << '[';
948 PrintExpr(Node->getArg(1));
949 OS << ']';
950 } else if (Node->getNumArgs() == 1) {
951 OS << OpStrings[Kind] << ' ';
952 PrintExpr(Node->getArg(0));
953 } else if (Node->getNumArgs() == 2) {
954 PrintExpr(Node->getArg(0));
955 OS << ' ' << OpStrings[Kind] << ' ';
956 PrintExpr(Node->getArg(1));
957 } else {
958 assert(false && "unknown overloaded operator");
959 }
960}
Reid Spencer5f016e22007-07-11 17:01:13 +0000961
Douglas Gregor88a35142008-12-22 05:46:06 +0000962void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
963 VisitCallExpr(cast<CallExpr>(Node));
964}
965
Douglas Gregor49badde2008-10-27 19:41:14 +0000966void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
967 OS << Node->getCastName() << '<';
968 OS << Node->getTypeAsWritten().getAsString() << ">(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000969 PrintExpr(Node->getSubExpr());
970 OS << ")";
971}
972
Douglas Gregor49badde2008-10-27 19:41:14 +0000973void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
974 VisitCXXNamedCastExpr(Node);
975}
976
977void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
978 VisitCXXNamedCastExpr(Node);
979}
980
981void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
982 VisitCXXNamedCastExpr(Node);
983}
984
985void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
986 VisitCXXNamedCastExpr(Node);
987}
988
Sebastian Redlc42e1182008-11-11 11:37:55 +0000989void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
990 OS << "typeid(";
991 if (Node->isTypeOperand()) {
992 OS << Node->getTypeOperand().getAsString();
993 } else {
994 PrintExpr(Node->getExprOperand());
995 }
996 OS << ")";
997}
998
Reid Spencer5f016e22007-07-11 17:01:13 +0000999void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1000 OS << (Node->getValue() ? "true" : "false");
1001}
1002
Douglas Gregor796da182008-11-04 14:32:21 +00001003void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1004 OS << "this";
1005}
1006
Chris Lattner50dd2892008-02-26 00:51:44 +00001007void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1008 if (Node->getSubExpr() == 0)
1009 OS << "throw";
1010 else {
1011 OS << "throw ";
1012 PrintExpr(Node->getSubExpr());
1013 }
1014}
1015
Chris Lattner04421082008-04-08 04:40:51 +00001016void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1017 // Nothing to print: we picked up the default argument
1018}
1019
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001020void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1021 OS << Node->getType().getAsString();
1022 OS << "(";
1023 PrintExpr(Node->getSubExpr());
1024 OS << ")";
1025}
1026
Douglas Gregor506ae412009-01-16 18:33:17 +00001027void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1028 OS << Node->getType().getAsString();
1029 OS << "(";
1030 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1031 ArgEnd = Node->arg_end();
1032 Arg != ArgEnd; ++Arg) {
1033 if (Arg != Node->arg_begin())
1034 OS << ", ";
1035 PrintExpr(*Arg);
1036 }
1037 OS << ")";
1038}
1039
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001040void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
1041 OS << Node->getType().getAsString() << "()";
1042}
1043
Argyrios Kyrtzidis9e922b12008-09-09 23:47:53 +00001044void
1045StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
1046 PrintRawDecl(E->getVarDecl());
1047}
1048
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001049void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1050 if (E->isGlobalNew())
1051 OS << "::";
1052 OS << "new ";
1053 unsigned NumPlace = E->getNumPlacementArgs();
1054 if (NumPlace > 0) {
1055 OS << "(";
1056 PrintExpr(E->getPlacementArg(0));
1057 for (unsigned i = 1; i < NumPlace; ++i) {
1058 OS << ", ";
1059 PrintExpr(E->getPlacementArg(i));
1060 }
1061 OS << ") ";
1062 }
1063 if (E->isParenTypeId())
1064 OS << "(";
Sebastian Redl6fec6482008-12-02 22:08:59 +00001065 std::string TypeS;
1066 if (Expr *Size = E->getArraySize()) {
1067 llvm::raw_string_ostream s(TypeS);
1068 Size->printPretty(s);
1069 s.flush();
1070 TypeS = "[" + TypeS + "]";
1071 }
1072 E->getAllocatedType().getAsStringInternal(TypeS);
1073 OS << TypeS;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001074 if (E->isParenTypeId())
1075 OS << ")";
1076
1077 if (E->hasInitializer()) {
1078 OS << "(";
1079 unsigned NumCons = E->getNumConstructorArgs();
1080 if (NumCons > 0) {
1081 PrintExpr(E->getConstructorArg(0));
1082 for (unsigned i = 1; i < NumCons; ++i) {
1083 OS << ", ";
1084 PrintExpr(E->getConstructorArg(i));
1085 }
1086 }
1087 OS << ")";
1088 }
1089}
1090
1091void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1092 if (E->isGlobalDelete())
1093 OS << "::";
1094 OS << "delete ";
1095 if (E->isArrayForm())
1096 OS << "[] ";
1097 PrintExpr(E->getArgument());
1098}
1099
Douglas Gregor5c37de72008-12-06 00:22:45 +00001100void StmtPrinter::VisitCXXDependentNameExpr(CXXDependentNameExpr *E) {
1101 OS << E->getName()->getName();
1102}
1103
Sebastian Redl64b45f72009-01-05 20:52:13 +00001104static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1105 switch (UTT) {
1106 default: assert(false && "Unknown type trait");
1107 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1108 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1109 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1110 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1111 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1112 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1113 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1114 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1115 case UTT_IsAbstract: return "__is_abstract";
1116 case UTT_IsClass: return "__is_class";
1117 case UTT_IsEmpty: return "__is_empty";
1118 case UTT_IsEnum: return "__is_enum";
1119 case UTT_IsPOD: return "__is_pod";
1120 case UTT_IsPolymorphic: return "__is_polymorphic";
1121 case UTT_IsUnion: return "__is_union";
1122 }
1123}
1124
1125void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1126 OS << getTypeTraitName(E->getTrait()) << "("
1127 << E->getQueriedType().getAsString() << ")";
1128}
1129
Anders Carlsson55085182007-08-21 17:43:55 +00001130// Obj-C
1131
1132void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1133 OS << "@";
1134 VisitStringLiteral(Node->getString());
1135}
Reid Spencer5f016e22007-07-11 17:01:13 +00001136
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001137void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001138 OS << "@encode(" << Node->getEncodedType().getAsString() << ')';
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001139}
1140
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001141void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001142 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001143}
1144
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001145void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001146 OS << "@protocol(" << Node->getProtocol()->getNameAsString() << ')';
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001147}
1148
Steve Naroff563477d2007-09-18 23:55:05 +00001149void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1150 OS << "[";
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001151 Expr *receiver = Mess->getReceiver();
1152 if (receiver) PrintExpr(receiver);
1153 else OS << Mess->getClassName()->getName();
Ted Kremenekc29efd82008-05-02 17:32:38 +00001154 OS << ' ';
Ted Kremenek97b7f262008-04-16 04:30:16 +00001155 Selector selector = Mess->getSelector();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001156 if (selector.isUnarySelector()) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001157 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001158 } else {
1159 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001160 if (i < selector.getNumArgs()) {
1161 if (i > 0) OS << ' ';
1162 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner39f34e92008-11-24 04:00:27 +00001163 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremenekc29efd82008-05-02 17:32:38 +00001164 else
1165 OS << ":";
1166 }
1167 else OS << ", "; // Handle variadic methods.
1168
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001169 PrintExpr(Mess->getArg(i));
1170 }
Steve Naroff563477d2007-09-18 23:55:05 +00001171 }
1172 OS << "]";
1173}
1174
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001175void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) {
1176 OS << "super";
1177}
1178
Steve Naroff4eb206b2008-09-03 18:15:37 +00001179void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001180 BlockDecl *BD = Node->getBlockDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001181 OS << "^";
1182
1183 const FunctionType *AFT = Node->getFunctionType();
1184
1185 if (isa<FunctionTypeNoProto>(AFT)) {
1186 OS << "()";
Steve Naroff56ee6892008-10-08 17:01:13 +00001187 } else if (!BD->param_empty() || cast<FunctionTypeProto>(AFT)->isVariadic()) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001188 OS << '(';
1189 std::string ParamStr;
Steve Naroff56ee6892008-10-08 17:01:13 +00001190 for (BlockDecl::param_iterator AI = BD->param_begin(),
1191 E = BD->param_end(); AI != E; ++AI) {
1192 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner39f34e92008-11-24 04:00:27 +00001193 ParamStr = (*AI)->getNameAsString();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001194 (*AI)->getType().getAsStringInternal(ParamStr);
1195 OS << ParamStr;
1196 }
1197
Steve Naroff56ee6892008-10-08 17:01:13 +00001198 const FunctionTypeProto *FT = cast<FunctionTypeProto>(AFT);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001199 if (FT->isVariadic()) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001200 if (!BD->param_empty()) OS << ", ";
Steve Naroff4eb206b2008-09-03 18:15:37 +00001201 OS << "...";
1202 }
1203 OS << ')';
1204 }
1205}
1206
Steve Naroff4eb206b2008-09-03 18:15:37 +00001207void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001208 OS << Node->getDecl()->getNameAsString();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001209}
Reid Spencer5f016e22007-07-11 17:01:13 +00001210//===----------------------------------------------------------------------===//
1211// Stmt method implementations
1212//===----------------------------------------------------------------------===//
1213
Chris Lattner6000dac2007-08-08 22:51:59 +00001214void Stmt::dumpPretty() const {
Douglas Gregor4c678342009-01-28 21:54:33 +00001215 llvm::raw_ostream &OS = llvm::errs();
1216 printPretty(OS);
1217 OS.flush();
Reid Spencer5f016e22007-07-11 17:01:13 +00001218}
1219
Ted Kremeneka95d3752008-09-13 05:16:45 +00001220void Stmt::printPretty(llvm::raw_ostream &OS, PrinterHelper* Helper) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001221 if (this == 0) {
1222 OS << "<NULL>";
1223 return;
1224 }
1225
Ted Kremenek42a509f2007-08-31 21:30:12 +00001226 StmtPrinter P(OS, Helper);
Chris Lattnerc5598cb2007-08-21 04:04:25 +00001227 P.Visit(const_cast<Stmt*>(this));
Reid Spencer5f016e22007-07-11 17:01:13 +00001228}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001229
1230//===----------------------------------------------------------------------===//
1231// PrinterHelper
1232//===----------------------------------------------------------------------===//
1233
1234// Implement virtual destructor.
Gabor Greif84675832007-09-11 15:32:40 +00001235PrinterHelper::~PrinterHelper() {}