blob: 49292afe67e3479ae1d43dd5705f5873ab3a87ad [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) {
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000205 OS << "if (";
Reid Spencer5f016e22007-07-11 17:01:13 +0000206 PrintExpr(If->getCond());
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000207 OS << ')';
Reid Spencer5f016e22007-07-11 17:01:13 +0000208
209 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
210 OS << ' ';
211 PrintRawCompoundStmt(CS);
212 OS << (If->getElse() ? ' ' : '\n');
213 } else {
214 OS << '\n';
215 PrintStmt(If->getThen());
216 if (If->getElse()) Indent();
217 }
218
219 if (Stmt *Else = If->getElse()) {
220 OS << "else";
221
222 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
223 OS << ' ';
224 PrintRawCompoundStmt(CS);
225 OS << '\n';
226 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
227 OS << ' ';
228 PrintRawIfStmt(ElseIf);
229 } else {
230 OS << '\n';
231 PrintStmt(If->getElse());
232 }
233 }
234}
235
236void StmtPrinter::VisitIfStmt(IfStmt *If) {
237 Indent();
238 PrintRawIfStmt(If);
239}
240
241void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
242 Indent() << "switch (";
243 PrintExpr(Node->getCond());
244 OS << ")";
245
246 // Pretty print compoundstmt bodies (very common).
247 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
248 OS << " ";
249 PrintRawCompoundStmt(CS);
250 OS << "\n";
251 } else {
252 OS << "\n";
253 PrintStmt(Node->getBody());
254 }
255}
256
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000257void StmtPrinter::VisitSwitchCase(SwitchCase*) {
258 assert(0 && "SwitchCase is an abstract class");
259}
260
Reid Spencer5f016e22007-07-11 17:01:13 +0000261void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
262 Indent() << "while (";
263 PrintExpr(Node->getCond());
264 OS << ")\n";
265 PrintStmt(Node->getBody());
266}
267
268void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner8bdcc472007-09-15 21:49:37 +0000269 Indent() << "do ";
270 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
271 PrintRawCompoundStmt(CS);
272 OS << " ";
273 } else {
274 OS << "\n";
275 PrintStmt(Node->getBody());
276 Indent();
277 }
278
279 OS << "while ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 PrintExpr(Node->getCond());
281 OS << ";\n";
282}
283
284void StmtPrinter::VisitForStmt(ForStmt *Node) {
285 Indent() << "for (";
286 if (Node->getInit()) {
287 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000288 PrintRawDeclStmt(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000289 else
290 PrintExpr(cast<Expr>(Node->getInit()));
291 }
Chris Lattner8bdcc472007-09-15 21:49:37 +0000292 OS << ";";
293 if (Node->getCond()) {
294 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 PrintExpr(Node->getCond());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000296 }
297 OS << ";";
298 if (Node->getInc()) {
299 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 PrintExpr(Node->getInc());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000301 }
302 OS << ") ";
303
304 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
305 PrintRawCompoundStmt(CS);
306 OS << "\n";
307 } else {
308 OS << "\n";
309 PrintStmt(Node->getBody());
310 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000311}
312
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000313void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000314 Indent() << "for (";
315 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000316 PrintRawDeclStmt(DS);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000317 else
318 PrintExpr(cast<Expr>(Node->getElement()));
319 OS << " in ";
320 PrintExpr(Node->getCollection());
321 OS << ") ";
322
323 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
324 PrintRawCompoundStmt(CS);
325 OS << "\n";
326 } else {
327 OS << "\n";
328 PrintStmt(Node->getBody());
329 }
330}
331
Reid Spencer5f016e22007-07-11 17:01:13 +0000332void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
333 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
334}
335
336void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
337 Indent() << "goto *";
338 PrintExpr(Node->getTarget());
339 OS << ";\n";
340}
341
342void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
343 Indent() << "continue;\n";
344}
345
346void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
347 Indent() << "break;\n";
348}
349
350
351void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
352 Indent() << "return";
353 if (Node->getRetValue()) {
354 OS << " ";
355 PrintExpr(Node->getRetValue());
356 }
357 OS << ";\n";
358}
359
Chris Lattnerfe795952007-10-29 04:04:16 +0000360
361void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson39c47b52007-11-23 23:12:25 +0000362 Indent() << "asm ";
363
364 if (Node->isVolatile())
365 OS << "volatile ";
366
367 OS << "(";
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000368 VisitStringLiteral(Node->getAsmString());
Anders Carlssonb235fc22007-11-22 01:36:19 +0000369
370 // Outputs
371 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
372 Node->getNumClobbers() != 0)
373 OS << " : ";
374
375 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
376 if (i != 0)
377 OS << ", ";
378
379 if (!Node->getOutputName(i).empty()) {
380 OS << '[';
381 OS << Node->getOutputName(i);
382 OS << "] ";
383 }
384
385 VisitStringLiteral(Node->getOutputConstraint(i));
386 OS << " ";
387 Visit(Node->getOutputExpr(i));
388 }
389
390 // Inputs
391 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
392 OS << " : ";
393
394 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
395 if (i != 0)
396 OS << ", ";
397
398 if (!Node->getInputName(i).empty()) {
399 OS << '[';
400 OS << Node->getInputName(i);
401 OS << "] ";
402 }
403
404 VisitStringLiteral(Node->getInputConstraint(i));
405 OS << " ";
406 Visit(Node->getInputExpr(i));
407 }
408
409 // Clobbers
410 if (Node->getNumClobbers() != 0)
411 OS << " : ";
412
413 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
414 if (i != 0)
415 OS << ", ";
416
417 VisitStringLiteral(Node->getClobber(i));
418 }
419
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000420 OS << ");\n";
Chris Lattnerfe795952007-10-29 04:04:16 +0000421}
422
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000423void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000424 Indent() << "@try";
425 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
426 PrintRawCompoundStmt(TS);
427 OS << "\n";
428 }
429
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000430 for (ObjCAtCatchStmt *catchStmt =
431 static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts());
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000432 catchStmt;
433 catchStmt =
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000434 static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000435 Indent() << "@catch(";
436 if (catchStmt->getCatchParamStmt()) {
437 if (DeclStmt *DS = dyn_cast<DeclStmt>(catchStmt->getCatchParamStmt()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000438 PrintRawDeclStmt(DS);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000439 }
440 OS << ")";
441 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody()))
442 {
443 PrintRawCompoundStmt(CS);
444 OS << "\n";
445 }
446 }
447
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000448 if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>(
Fariborz Jahanian1e7eab42007-11-07 00:46:42 +0000449 Node->getFinallyStmt())) {
450 Indent() << "@finally";
451 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000452 OS << "\n";
453 }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000454}
455
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000456void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000457}
458
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000459void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000460 Indent() << "@catch (...) { /* todo */ } \n";
461}
462
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000463void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000464 Indent() << "@throw";
465 if (Node->getThrowExpr()) {
466 OS << " ";
467 PrintExpr(Node->getThrowExpr());
468 }
469 OS << ";\n";
470}
471
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000472void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000473 Indent() << "@synchronized (";
474 PrintExpr(Node->getSynchExpr());
475 OS << ")";
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000476 PrintRawCompoundStmt(Node->getSynchBody());
477 OS << "\n";
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000478}
479
Sebastian Redl8351da02008-12-22 21:35:02 +0000480void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
481 OS << "catch (";
Sebastian Redl4b07b292008-12-22 19:15:10 +0000482 if (Decl *ExDecl = Node->getExceptionDecl())
483 PrintRawDecl(ExDecl);
484 else
485 OS << "...";
486 OS << ") ";
487 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl8351da02008-12-22 21:35:02 +0000488}
489
490void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
491 Indent();
492 PrintRawCXXCatchStmt(Node);
493 OS << "\n";
494}
495
496void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
497 Indent() << "try ";
498 PrintRawCompoundStmt(Node->getTryBlock());
499 for(unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
500 OS << " ";
501 PrintRawCXXCatchStmt(Node->getHandler(i));
502 }
Sebastian Redl4b07b292008-12-22 19:15:10 +0000503 OS << "\n";
504}
505
Reid Spencer5f016e22007-07-11 17:01:13 +0000506//===----------------------------------------------------------------------===//
507// Expr printing methods.
508//===----------------------------------------------------------------------===//
509
510void StmtPrinter::VisitExpr(Expr *Node) {
511 OS << "<<unknown expr type>>";
512}
513
514void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +0000515 OS << Node->getDecl()->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000516}
517
Douglas Gregor1a49af92009-01-06 05:10:23 +0000518void StmtPrinter::VisitQualifiedDeclRefExpr(QualifiedDeclRefExpr *Node) {
519 // FIXME: Should we keep enough information in QualifiedDeclRefExpr
520 // to produce the same qualification that the user wrote?
521 llvm::SmallVector<DeclContext *, 4> Contexts;
522
523 NamedDecl *D = Node->getDecl();
524
525 // Build up a stack of contexts.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000526 DeclContext *Ctx = D->getDeclContext();
Douglas Gregor1a49af92009-01-06 05:10:23 +0000527 for (; Ctx; Ctx = Ctx->getParent())
528 if (!Ctx->isTransparentContext())
529 Contexts.push_back(Ctx);
530
531 while (!Contexts.empty()) {
532 DeclContext *Ctx = Contexts.back();
533 if (isa<TranslationUnitDecl>(Ctx))
534 OS << "::";
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000535 else if (NamedDecl *ND = dyn_cast<NamedDecl>(Ctx))
536 OS << ND->getNameAsString() << "::";
Douglas Gregor1a49af92009-01-06 05:10:23 +0000537 Contexts.pop_back();
538 }
539
540 OS << D->getNameAsString();
541}
542
Steve Naroff7779db42007-11-12 14:29:37 +0000543void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000544 if (Node->getBase()) {
545 PrintExpr(Node->getBase());
546 OS << (Node->isArrow() ? "->" : ".");
547 }
Chris Lattner39f34e92008-11-24 04:00:27 +0000548 OS << Node->getDecl()->getNameAsString();
Steve Naroff7779db42007-11-12 14:29:37 +0000549}
550
Steve Naroffae784072008-05-30 00:40:33 +0000551void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
552 if (Node->getBase()) {
553 PrintExpr(Node->getBase());
554 OS << ".";
555 }
Steve Naroffc77a6362008-12-04 16:24:46 +0000556 OS << Node->getProperty()->getNameAsCString();
Steve Naroffae784072008-05-30 00:40:33 +0000557}
558
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000559void StmtPrinter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node) {
560 if (Node->getBase()) {
561 PrintExpr(Node->getBase());
562 OS << ".";
563 }
564 // FIXME: Setter/Getter names
565}
566
Chris Lattnerd9f69102008-08-10 01:53:14 +0000567void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson22742662007-07-21 05:21:51 +0000568 switch (Node->getIdentType()) {
569 default:
570 assert(0 && "unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000571 case PredefinedExpr::Func:
Anders Carlsson22742662007-07-21 05:21:51 +0000572 OS << "__func__";
573 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000574 case PredefinedExpr::Function:
Anders Carlsson22742662007-07-21 05:21:51 +0000575 OS << "__FUNCTION__";
576 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000577 case PredefinedExpr::PrettyFunction:
Anders Carlsson22742662007-07-21 05:21:51 +0000578 OS << "__PRETTY_FUNCTION__";
579 break;
580 }
581}
582
Reid Spencer5f016e22007-07-11 17:01:13 +0000583void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000584 unsigned value = Node->getValue();
Chris Lattnerc250aae2008-06-07 22:35:38 +0000585 if (Node->isWide())
586 OS << "L";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000587 switch (value) {
588 case '\\':
589 OS << "'\\\\'";
590 break;
591 case '\'':
592 OS << "'\\''";
593 break;
594 case '\a':
595 // TODO: K&R: the meaning of '\\a' is different in traditional C
596 OS << "'\\a'";
597 break;
598 case '\b':
599 OS << "'\\b'";
600 break;
601 // Nonstandard escape sequence.
602 /*case '\e':
603 OS << "'\\e'";
604 break;*/
605 case '\f':
606 OS << "'\\f'";
607 break;
608 case '\n':
609 OS << "'\\n'";
610 break;
611 case '\r':
612 OS << "'\\r'";
613 break;
614 case '\t':
615 OS << "'\\t'";
616 break;
617 case '\v':
618 OS << "'\\v'";
619 break;
620 default:
Ted Kremenek471733d2008-02-23 00:52:04 +0000621 if (value < 256 && isprint(value)) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000622 OS << "'" << (char)value << "'";
623 } else if (value < 256) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000624 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000625 } else {
626 // FIXME what to really do here?
627 OS << value;
628 }
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000629 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000630}
631
632void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
633 bool isSigned = Node->getType()->isSignedIntegerType();
634 OS << Node->getValue().toString(10, isSigned);
635
636 // Emit suffixes. Integer literals are always a builtin integer type.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000637 switch (Node->getType()->getAsBuiltinType()->getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000638 default: assert(0 && "Unexpected type for integer literal!");
639 case BuiltinType::Int: break; // no suffix.
640 case BuiltinType::UInt: OS << 'U'; break;
641 case BuiltinType::Long: OS << 'L'; break;
642 case BuiltinType::ULong: OS << "UL"; break;
643 case BuiltinType::LongLong: OS << "LL"; break;
644 case BuiltinType::ULongLong: OS << "ULL"; break;
645 }
646}
647void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner86e499d2007-08-01 00:23:58 +0000648 // FIXME: print value more precisely.
Chris Lattnerda8249e2008-06-07 22:13:43 +0000649 OS << Node->getValueAsApproximateDouble();
Reid Spencer5f016e22007-07-11 17:01:13 +0000650}
Chris Lattner5d661452007-08-26 03:42:43 +0000651
652void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
653 PrintExpr(Node->getSubExpr());
654 OS << "i";
655}
656
Reid Spencer5f016e22007-07-11 17:01:13 +0000657void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
658 if (Str->isWide()) OS << 'L';
659 OS << '"';
Anders Carlssonee98ac52007-10-15 02:50:23 +0000660
Reid Spencer5f016e22007-07-11 17:01:13 +0000661 // FIXME: this doesn't print wstrings right.
662 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattner9a81c872009-01-16 19:25:18 +0000663 unsigned char Char = Str->getStrData()[i];
664
665 switch (Char) {
666 default:
667 if (isprint(Char))
668 OS << (char)Char;
669 else // Output anything hard as an octal escape.
670 OS << '\\'
671 << (char)('0'+ ((Char >> 6) & 7))
672 << (char)('0'+ ((Char >> 3) & 7))
673 << (char)('0'+ ((Char >> 0) & 7));
674 break;
675 // Handle some common non-printable cases to make dumps prettier.
Reid Spencer5f016e22007-07-11 17:01:13 +0000676 case '\\': OS << "\\\\"; break;
677 case '"': OS << "\\\""; break;
678 case '\n': OS << "\\n"; break;
679 case '\t': OS << "\\t"; break;
680 case '\a': OS << "\\a"; break;
681 case '\b': OS << "\\b"; break;
682 }
683 }
684 OS << '"';
685}
686void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
687 OS << "(";
688 PrintExpr(Node->getSubExpr());
689 OS << ")";
690}
691void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +0000692 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000693 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner296bf192007-08-23 21:46:40 +0000694
Sebastian Redl05189992008-11-11 17:56:53 +0000695 // Print a space if this is an "identifier operator" like __real.
Chris Lattner296bf192007-08-23 21:46:40 +0000696 switch (Node->getOpcode()) {
697 default: break;
Chris Lattner296bf192007-08-23 21:46:40 +0000698 case UnaryOperator::Real:
699 case UnaryOperator::Imag:
700 case UnaryOperator::Extension:
701 OS << ' ';
702 break;
703 }
704 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000705 PrintExpr(Node->getSubExpr());
706
707 if (Node->isPostfix())
708 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +0000709}
Chris Lattner704fe352007-08-30 17:59:59 +0000710
711bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
712 if (isa<CompoundLiteralExpr>(E)) {
713 // Base case, print the type and comma.
714 OS << E->getType().getAsString() << ", ";
715 return true;
716 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
717 PrintOffsetOfDesignator(ASE->getLHS());
718 OS << "[";
719 PrintExpr(ASE->getRHS());
720 OS << "]";
721 return false;
722 } else {
723 MemberExpr *ME = cast<MemberExpr>(E);
724 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
Chris Lattner39f34e92008-11-24 04:00:27 +0000725 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getNameAsString();
Chris Lattner704fe352007-08-30 17:59:59 +0000726 return false;
727 }
728}
729
730void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
731 OS << "__builtin_offsetof(";
732 PrintOffsetOfDesignator(Node->getSubExpr());
733 OS << ")";
734}
735
Sebastian Redl05189992008-11-11 17:56:53 +0000736void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
737 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
738 if (Node->isArgumentType())
739 OS << "(" << Node->getArgumentType().getAsString() << ")";
740 else {
741 OS << " ";
742 PrintExpr(Node->getArgumentExpr());
743 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000744}
745void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +0000746 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000747 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +0000748 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000749 OS << "]";
750}
751
752void StmtPrinter::VisitCallExpr(CallExpr *Call) {
753 PrintExpr(Call->getCallee());
754 OS << "(";
755 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattner04421082008-04-08 04:40:51 +0000756 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
757 // Don't print any defaulted arguments
758 break;
759 }
760
Reid Spencer5f016e22007-07-11 17:01:13 +0000761 if (i) OS << ", ";
762 PrintExpr(Call->getArg(i));
763 }
764 OS << ")";
765}
766void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregorb3eef682009-01-08 22:45:41 +0000767 // FIXME: Suppress printing implicit bases (like "this")
768 PrintExpr(Node->getBase());
769 OS << (Node->isArrow() ? "->" : ".");
770 // FIXME: Suppress printing references to unnamed objects
771 // representing anonymous unions/structs
Douglas Gregor86f19402008-12-20 23:49:58 +0000772 OS << Node->getMemberDecl()->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000773}
Nate Begeman213541a2008-04-18 23:10:10 +0000774void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +0000775 PrintExpr(Node->getBase());
776 OS << ".";
777 OS << Node->getAccessor().getName();
778}
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000779void StmtPrinter::VisitCastExpr(CastExpr *) {
780 assert(0 && "CastExpr is an abstract class");
781}
Douglas Gregor49badde2008-10-27 19:41:14 +0000782void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *) {
783 assert(0 && "ExplicitCastExpr is an abstract class");
784}
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000785void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000786 OS << "(" << Node->getType().getAsString() << ")";
Reid Spencer5f016e22007-07-11 17:01:13 +0000787 PrintExpr(Node->getSubExpr());
788}
Steve Naroffaff1edd2007-07-19 21:32:11 +0000789void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
790 OS << "(" << Node->getType().getAsString() << ")";
791 PrintExpr(Node->getInitializer());
792}
Steve Naroff49b45262007-07-13 16:58:59 +0000793void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroff90045e82007-07-13 23:32:42 +0000794 // No need to print anything, simply forward to the sub expression.
795 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +0000796}
Reid Spencer5f016e22007-07-11 17:01:13 +0000797void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
798 PrintExpr(Node->getLHS());
799 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
800 PrintExpr(Node->getRHS());
801}
Chris Lattnereb14fe82007-08-25 02:00:02 +0000802void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
803 PrintExpr(Node->getLHS());
804 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
805 PrintExpr(Node->getRHS());
806}
Reid Spencer5f016e22007-07-11 17:01:13 +0000807void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
808 PrintExpr(Node->getCond());
Ted Kremenek8e911c42007-11-26 18:27:54 +0000809
810 if (Node->getLHS()) {
811 OS << " ? ";
812 PrintExpr(Node->getLHS());
813 OS << " : ";
814 }
815 else { // Handle GCC extention where LHS can be NULL.
816 OS << " ?: ";
817 }
818
Reid Spencer5f016e22007-07-11 17:01:13 +0000819 PrintExpr(Node->getRHS());
820}
821
822// GNU extensions.
823
Chris Lattner6481a572007-08-03 17:31:20 +0000824void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000825 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000826}
827
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000828void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
829 OS << "(";
830 PrintRawCompoundStmt(E->getSubStmt());
831 OS << ")";
832}
833
Steve Naroffd34e9152007-08-01 22:05:33 +0000834void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
835 OS << "__builtin_types_compatible_p(";
836 OS << Node->getArgType1().getAsString() << ",";
837 OS << Node->getArgType2().getAsString() << ")";
838}
839
Steve Naroffd04fdd52007-08-03 21:21:27 +0000840void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
841 OS << "__builtin_choose_expr(";
842 PrintExpr(Node->getCond());
Chris Lattner94f05e32007-08-04 00:20:15 +0000843 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000844 PrintExpr(Node->getLHS());
Chris Lattner94f05e32007-08-04 00:20:15 +0000845 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000846 PrintExpr(Node->getRHS());
847 OS << ")";
848}
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000849
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000850void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
851 OS << "__null";
852}
853
Nate Begemane2ce1d92008-01-17 17:46:27 +0000854void StmtPrinter::VisitOverloadExpr(OverloadExpr *Node) {
855 OS << "__builtin_overload(";
Nate Begeman67295d02008-01-30 20:50:20 +0000856 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
Nate Begemane2ce1d92008-01-17 17:46:27 +0000857 if (i) OS << ", ";
Nate Begeman67295d02008-01-30 20:50:20 +0000858 PrintExpr(Node->getExpr(i));
Nate Begemane2ce1d92008-01-17 17:46:27 +0000859 }
860 OS << ")";
861}
862
Eli Friedmand38617c2008-05-14 19:38:39 +0000863void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
864 OS << "__builtin_shufflevector(";
865 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
866 if (i) OS << ", ";
867 PrintExpr(Node->getExpr(i));
868 }
869 OS << ")";
870}
871
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000872void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
873 OS << "{ ";
874 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
875 if (i) OS << ", ";
Douglas Gregor4c678342009-01-28 21:54:33 +0000876 if (Node->getInit(i))
877 PrintExpr(Node->getInit(i));
878 else
879 OS << "0";
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000880 }
881 OS << " }";
882}
883
Douglas Gregor05c13a32009-01-22 00:58:24 +0000884void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000885 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
886 DEnd = Node->designators_end();
887 D != DEnd; ++D) {
888 if (D->isFieldDesignator()) {
889 if (D->getDotLoc().isInvalid())
890 OS << D->getFieldName()->getName() << ":";
891 else
892 OS << "." << D->getFieldName()->getName();
893 } else {
894 OS << "[";
895 if (D->isArrayDesignator()) {
896 PrintExpr(Node->getArrayIndex(*D));
897 } else {
898 PrintExpr(Node->getArrayRangeStart(*D));
899 OS << " ... ";
900 PrintExpr(Node->getArrayRangeEnd(*D));
901 }
902 OS << "]";
903 }
904 }
905
906 OS << " = ";
907 PrintExpr(Node->getInit());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000908}
909
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000910void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
911 OS << "/*implicit*/" << Node->getType().getAsString() << "()";
912}
913
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000914void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
915 OS << "va_arg(";
916 PrintExpr(Node->getSubExpr());
917 OS << ", ";
918 OS << Node->getType().getAsString();
919 OS << ")";
920}
921
Reid Spencer5f016e22007-07-11 17:01:13 +0000922// C++
Douglas Gregorb4609802008-11-14 16:09:21 +0000923void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
924 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
925 "",
926#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
927 Spelling,
928#include "clang/Basic/OperatorKinds.def"
929 };
930
931 OverloadedOperatorKind Kind = Node->getOperator();
932 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
933 if (Node->getNumArgs() == 1) {
934 OS << OpStrings[Kind] << ' ';
935 PrintExpr(Node->getArg(0));
936 } else {
937 PrintExpr(Node->getArg(0));
938 OS << ' ' << OpStrings[Kind];
939 }
940 } else if (Kind == OO_Call) {
941 PrintExpr(Node->getArg(0));
942 OS << '(';
943 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
944 if (ArgIdx > 1)
945 OS << ", ";
946 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
947 PrintExpr(Node->getArg(ArgIdx));
948 }
949 OS << ')';
950 } else if (Kind == OO_Subscript) {
951 PrintExpr(Node->getArg(0));
952 OS << '[';
953 PrintExpr(Node->getArg(1));
954 OS << ']';
955 } else if (Node->getNumArgs() == 1) {
956 OS << OpStrings[Kind] << ' ';
957 PrintExpr(Node->getArg(0));
958 } else if (Node->getNumArgs() == 2) {
959 PrintExpr(Node->getArg(0));
960 OS << ' ' << OpStrings[Kind] << ' ';
961 PrintExpr(Node->getArg(1));
962 } else {
963 assert(false && "unknown overloaded operator");
964 }
965}
Reid Spencer5f016e22007-07-11 17:01:13 +0000966
Douglas Gregor88a35142008-12-22 05:46:06 +0000967void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
968 VisitCallExpr(cast<CallExpr>(Node));
969}
970
Douglas Gregor49badde2008-10-27 19:41:14 +0000971void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
972 OS << Node->getCastName() << '<';
973 OS << Node->getTypeAsWritten().getAsString() << ">(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000974 PrintExpr(Node->getSubExpr());
975 OS << ")";
976}
977
Douglas Gregor49badde2008-10-27 19:41:14 +0000978void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
979 VisitCXXNamedCastExpr(Node);
980}
981
982void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
983 VisitCXXNamedCastExpr(Node);
984}
985
986void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
987 VisitCXXNamedCastExpr(Node);
988}
989
990void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
991 VisitCXXNamedCastExpr(Node);
992}
993
Sebastian Redlc42e1182008-11-11 11:37:55 +0000994void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
995 OS << "typeid(";
996 if (Node->isTypeOperand()) {
997 OS << Node->getTypeOperand().getAsString();
998 } else {
999 PrintExpr(Node->getExprOperand());
1000 }
1001 OS << ")";
1002}
1003
Reid Spencer5f016e22007-07-11 17:01:13 +00001004void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1005 OS << (Node->getValue() ? "true" : "false");
1006}
1007
Douglas Gregor796da182008-11-04 14:32:21 +00001008void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1009 OS << "this";
1010}
1011
Chris Lattner50dd2892008-02-26 00:51:44 +00001012void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1013 if (Node->getSubExpr() == 0)
1014 OS << "throw";
1015 else {
1016 OS << "throw ";
1017 PrintExpr(Node->getSubExpr());
1018 }
1019}
1020
Chris Lattner04421082008-04-08 04:40:51 +00001021void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1022 // Nothing to print: we picked up the default argument
1023}
1024
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001025void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1026 OS << Node->getType().getAsString();
1027 OS << "(";
1028 PrintExpr(Node->getSubExpr());
1029 OS << ")";
1030}
1031
Douglas Gregor506ae412009-01-16 18:33:17 +00001032void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1033 OS << Node->getType().getAsString();
1034 OS << "(";
1035 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1036 ArgEnd = Node->arg_end();
1037 Arg != ArgEnd; ++Arg) {
1038 if (Arg != Node->arg_begin())
1039 OS << ", ";
1040 PrintExpr(*Arg);
1041 }
1042 OS << ")";
1043}
1044
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001045void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
1046 OS << Node->getType().getAsString() << "()";
1047}
1048
Argyrios Kyrtzidis9e922b12008-09-09 23:47:53 +00001049void
1050StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
1051 PrintRawDecl(E->getVarDecl());
1052}
1053
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001054void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1055 if (E->isGlobalNew())
1056 OS << "::";
1057 OS << "new ";
1058 unsigned NumPlace = E->getNumPlacementArgs();
1059 if (NumPlace > 0) {
1060 OS << "(";
1061 PrintExpr(E->getPlacementArg(0));
1062 for (unsigned i = 1; i < NumPlace; ++i) {
1063 OS << ", ";
1064 PrintExpr(E->getPlacementArg(i));
1065 }
1066 OS << ") ";
1067 }
1068 if (E->isParenTypeId())
1069 OS << "(";
Sebastian Redl6fec6482008-12-02 22:08:59 +00001070 std::string TypeS;
1071 if (Expr *Size = E->getArraySize()) {
1072 llvm::raw_string_ostream s(TypeS);
1073 Size->printPretty(s);
1074 s.flush();
1075 TypeS = "[" + TypeS + "]";
1076 }
1077 E->getAllocatedType().getAsStringInternal(TypeS);
1078 OS << TypeS;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001079 if (E->isParenTypeId())
1080 OS << ")";
1081
1082 if (E->hasInitializer()) {
1083 OS << "(";
1084 unsigned NumCons = E->getNumConstructorArgs();
1085 if (NumCons > 0) {
1086 PrintExpr(E->getConstructorArg(0));
1087 for (unsigned i = 1; i < NumCons; ++i) {
1088 OS << ", ";
1089 PrintExpr(E->getConstructorArg(i));
1090 }
1091 }
1092 OS << ")";
1093 }
1094}
1095
1096void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1097 if (E->isGlobalDelete())
1098 OS << "::";
1099 OS << "delete ";
1100 if (E->isArrayForm())
1101 OS << "[] ";
1102 PrintExpr(E->getArgument());
1103}
1104
Douglas Gregor17330012009-02-04 15:01:18 +00001105void StmtPrinter::VisitUnresolvedFunctionNameExpr(UnresolvedFunctionNameExpr *E) {
1106 OS << E->getName().getAsString();
Douglas Gregor5c37de72008-12-06 00:22:45 +00001107}
1108
Sebastian Redl64b45f72009-01-05 20:52:13 +00001109static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1110 switch (UTT) {
1111 default: assert(false && "Unknown type trait");
1112 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1113 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1114 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1115 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1116 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1117 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1118 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1119 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1120 case UTT_IsAbstract: return "__is_abstract";
1121 case UTT_IsClass: return "__is_class";
1122 case UTT_IsEmpty: return "__is_empty";
1123 case UTT_IsEnum: return "__is_enum";
1124 case UTT_IsPOD: return "__is_pod";
1125 case UTT_IsPolymorphic: return "__is_polymorphic";
1126 case UTT_IsUnion: return "__is_union";
1127 }
1128}
1129
1130void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1131 OS << getTypeTraitName(E->getTrait()) << "("
1132 << E->getQueriedType().getAsString() << ")";
1133}
1134
Anders Carlsson55085182007-08-21 17:43:55 +00001135// Obj-C
1136
1137void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1138 OS << "@";
1139 VisitStringLiteral(Node->getString());
1140}
Reid Spencer5f016e22007-07-11 17:01:13 +00001141
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001142void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001143 OS << "@encode(" << Node->getEncodedType().getAsString() << ')';
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001144}
1145
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001146void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001147 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001148}
1149
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001150void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001151 OS << "@protocol(" << Node->getProtocol()->getNameAsString() << ')';
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001152}
1153
Steve Naroff563477d2007-09-18 23:55:05 +00001154void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1155 OS << "[";
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001156 Expr *receiver = Mess->getReceiver();
1157 if (receiver) PrintExpr(receiver);
1158 else OS << Mess->getClassName()->getName();
Ted Kremenekc29efd82008-05-02 17:32:38 +00001159 OS << ' ';
Ted Kremenek97b7f262008-04-16 04:30:16 +00001160 Selector selector = Mess->getSelector();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001161 if (selector.isUnarySelector()) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001162 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001163 } else {
1164 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001165 if (i < selector.getNumArgs()) {
1166 if (i > 0) OS << ' ';
1167 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner39f34e92008-11-24 04:00:27 +00001168 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremenekc29efd82008-05-02 17:32:38 +00001169 else
1170 OS << ":";
1171 }
1172 else OS << ", "; // Handle variadic methods.
1173
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001174 PrintExpr(Mess->getArg(i));
1175 }
Steve Naroff563477d2007-09-18 23:55:05 +00001176 }
1177 OS << "]";
1178}
1179
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001180void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) {
1181 OS << "super";
1182}
1183
Steve Naroff4eb206b2008-09-03 18:15:37 +00001184void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001185 BlockDecl *BD = Node->getBlockDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001186 OS << "^";
1187
1188 const FunctionType *AFT = Node->getFunctionType();
1189
1190 if (isa<FunctionTypeNoProto>(AFT)) {
1191 OS << "()";
Steve Naroff56ee6892008-10-08 17:01:13 +00001192 } else if (!BD->param_empty() || cast<FunctionTypeProto>(AFT)->isVariadic()) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001193 OS << '(';
1194 std::string ParamStr;
Steve Naroff56ee6892008-10-08 17:01:13 +00001195 for (BlockDecl::param_iterator AI = BD->param_begin(),
1196 E = BD->param_end(); AI != E; ++AI) {
1197 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner39f34e92008-11-24 04:00:27 +00001198 ParamStr = (*AI)->getNameAsString();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001199 (*AI)->getType().getAsStringInternal(ParamStr);
1200 OS << ParamStr;
1201 }
1202
Steve Naroff56ee6892008-10-08 17:01:13 +00001203 const FunctionTypeProto *FT = cast<FunctionTypeProto>(AFT);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001204 if (FT->isVariadic()) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001205 if (!BD->param_empty()) OS << ", ";
Steve Naroff4eb206b2008-09-03 18:15:37 +00001206 OS << "...";
1207 }
1208 OS << ')';
1209 }
1210}
1211
Steve Naroff4eb206b2008-09-03 18:15:37 +00001212void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001213 OS << Node->getDecl()->getNameAsString();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001214}
Reid Spencer5f016e22007-07-11 17:01:13 +00001215//===----------------------------------------------------------------------===//
1216// Stmt method implementations
1217//===----------------------------------------------------------------------===//
1218
Chris Lattner6000dac2007-08-08 22:51:59 +00001219void Stmt::dumpPretty() const {
Douglas Gregor4c678342009-01-28 21:54:33 +00001220 llvm::raw_ostream &OS = llvm::errs();
1221 printPretty(OS);
1222 OS.flush();
Reid Spencer5f016e22007-07-11 17:01:13 +00001223}
1224
Ted Kremeneka95d3752008-09-13 05:16:45 +00001225void Stmt::printPretty(llvm::raw_ostream &OS, PrinterHelper* Helper) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001226 if (this == 0) {
1227 OS << "<NULL>";
1228 return;
1229 }
1230
Ted Kremenek42a509f2007-08-31 21:30:12 +00001231 StmtPrinter P(OS, Helper);
Chris Lattnerc5598cb2007-08-21 04:04:25 +00001232 P.Visit(const_cast<Stmt*>(this));
Reid Spencer5f016e22007-07-11 17:01:13 +00001233}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001234
1235//===----------------------------------------------------------------------===//
1236// PrinterHelper
1237//===----------------------------------------------------------------------===//
1238
1239// Implement virtual destructor.
Gabor Greif84675832007-09-11 15:32:40 +00001240PrinterHelper::~PrinterHelper() {}