blob: 6d641cf2d912fbd5d7c3d7c71795d45f5dcbee28 [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;
Eli Friedman48d14a22009-05-30 05:03:24 +000031 ASTContext &Context;
Reid Spencer5f016e22007-07-11 17:01:13 +000032 unsigned IndentLevel;
Ted Kremenek42a509f2007-08-31 21:30:12 +000033 clang::PrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000034 PrintingPolicy Policy;
35
Reid Spencer5f016e22007-07-11 17:01:13 +000036 public:
Eli Friedman48d14a22009-05-30 05:03:24 +000037 StmtPrinter(llvm::raw_ostream &os, ASTContext &C, PrinterHelper* helper,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000038 const PrintingPolicy &Policy = PrintingPolicy(),
39 unsigned Indentation = 0)
Eli Friedman48d14a22009-05-30 05:03:24 +000040 : OS(os), Context(C), IndentLevel(Indentation), Helper(helper),
41 Policy(Policy) {}
Reid Spencer5f016e22007-07-11 17:01:13 +000042
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000043 void PrintStmt(Stmt *S) {
44 PrintStmt(S, Policy.Indentation);
45 }
46
47 void PrintStmt(Stmt *S, int SubIndent) {
Reid Spencer5f016e22007-07-11 17:01:13 +000048 IndentLevel += SubIndent;
49 if (S && isa<Expr>(S)) {
50 // If this is an expr used in a stmt context, indent and newline it.
51 Indent();
Chris Lattnerc5598cb2007-08-21 04:04:25 +000052 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000053 OS << ";\n";
54 } else if (S) {
Chris Lattnerc5598cb2007-08-21 04:04:25 +000055 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000056 } else {
57 Indent() << "<<<NULL STATEMENT>>>\n";
58 }
59 IndentLevel -= SubIndent;
60 }
Eli Friedmandb23b152009-05-30 00:19:54 +000061
Reid Spencer5f016e22007-07-11 17:01:13 +000062 void PrintRawCompoundStmt(CompoundStmt *S);
63 void PrintRawDecl(Decl *D);
Ted Kremenekecd64c52008-10-06 18:39:36 +000064 void PrintRawDeclStmt(DeclStmt *S);
Reid Spencer5f016e22007-07-11 17:01:13 +000065 void PrintRawIfStmt(IfStmt *If);
Sebastian Redl8351da02008-12-22 21:35:02 +000066 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
Reid Spencer5f016e22007-07-11 17:01:13 +000067
68 void PrintExpr(Expr *E) {
69 if (E)
Chris Lattnerc5598cb2007-08-21 04:04:25 +000070 Visit(E);
Reid Spencer5f016e22007-07-11 17:01:13 +000071 else
72 OS << "<null expr>";
73 }
74
Mike Stump071e4da2009-02-10 20:16:46 +000075 llvm::raw_ostream &Indent(int Delta = 0) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000076 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
77 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +000078 return OS;
79 }
80
Chris Lattner704fe352007-08-30 17:59:59 +000081 bool PrintOffsetOfDesignator(Expr *E);
82 void VisitUnaryOffsetOf(UnaryOperator *Node);
83
Ted Kremenek42a509f2007-08-31 21:30:12 +000084 void Visit(Stmt* S) {
85 if (Helper && Helper->handledStmt(S,OS))
86 return;
87 else StmtVisitor<StmtPrinter>::Visit(S);
88 }
89
Chris Lattnerc5598cb2007-08-21 04:04:25 +000090 void VisitStmt(Stmt *Node);
Douglas Gregorf2cad862008-11-14 12:46:07 +000091#define STMT(CLASS, PARENT) \
Chris Lattnerc5598cb2007-08-21 04:04:25 +000092 void Visit##CLASS(CLASS *Node);
Reid Spencer5f016e22007-07-11 17:01:13 +000093#include "clang/AST/StmtNodes.def"
94 };
95}
96
97//===----------------------------------------------------------------------===//
98// Stmt printing methods.
99//===----------------------------------------------------------------------===//
100
101void StmtPrinter::VisitStmt(Stmt *Node) {
102 Indent() << "<<unknown stmt type>>\n";
103}
104
105/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
106/// with no newline after the }.
107void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
108 OS << "{\n";
109 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
110 I != E; ++I)
111 PrintStmt(*I);
112
113 Indent() << "}";
114}
115
116void StmtPrinter::PrintRawDecl(Decl *D) {
Eli Friedman48d14a22009-05-30 05:03:24 +0000117 D->print(OS, Context, Policy, IndentLevel);
Mike Stump071e4da2009-02-10 20:16:46 +0000118}
119
Ted Kremenekecd64c52008-10-06 18:39:36 +0000120void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
Eli Friedmandb23b152009-05-30 00:19:54 +0000121 DeclStmt::decl_iterator Begin = S->decl_begin(), End = S->decl_end();
Eli Friedman42f42c02009-05-30 04:20:30 +0000122 llvm::SmallVector<Decl*, 2> Decls;
123 for ( ; Begin != End; ++Begin)
124 Decls.push_back(*Begin);
Eli Friedmandb23b152009-05-30 00:19:54 +0000125
Eli Friedman48d14a22009-05-30 05:03:24 +0000126 Decl::printGroup(Decls.data(), Decls.size(), OS, Context, Policy,
Eli Friedman42f42c02009-05-30 04:20:30 +0000127 IndentLevel);
Ted Kremenekecd64c52008-10-06 18:39:36 +0000128}
Reid Spencer5f016e22007-07-11 17:01:13 +0000129
130void StmtPrinter::VisitNullStmt(NullStmt *Node) {
131 Indent() << ";\n";
132}
133
134void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Eli Friedmandb23b152009-05-30 00:19:54 +0000135 Indent();
136 PrintRawDeclStmt(Node);
137 OS << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000138}
139
140void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
141 Indent();
142 PrintRawCompoundStmt(Node);
143 OS << "\n";
144}
145
146void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
147 Indent(-1) << "case ";
148 PrintExpr(Node->getLHS());
149 if (Node->getRHS()) {
150 OS << " ... ";
151 PrintExpr(Node->getRHS());
152 }
153 OS << ":\n";
154
155 PrintStmt(Node->getSubStmt(), 0);
156}
157
158void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
159 Indent(-1) << "default:\n";
160 PrintStmt(Node->getSubStmt(), 0);
161}
162
163void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
164 Indent(-1) << Node->getName() << ":\n";
165 PrintStmt(Node->getSubStmt(), 0);
166}
167
168void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000169 OS << "if (";
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 PrintExpr(If->getCond());
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000171 OS << ')';
Reid Spencer5f016e22007-07-11 17:01:13 +0000172
173 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
174 OS << ' ';
175 PrintRawCompoundStmt(CS);
176 OS << (If->getElse() ? ' ' : '\n');
177 } else {
178 OS << '\n';
179 PrintStmt(If->getThen());
180 if (If->getElse()) Indent();
181 }
182
183 if (Stmt *Else = If->getElse()) {
184 OS << "else";
185
186 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
187 OS << ' ';
188 PrintRawCompoundStmt(CS);
189 OS << '\n';
190 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
191 OS << ' ';
192 PrintRawIfStmt(ElseIf);
193 } else {
194 OS << '\n';
195 PrintStmt(If->getElse());
196 }
197 }
198}
199
200void StmtPrinter::VisitIfStmt(IfStmt *If) {
201 Indent();
202 PrintRawIfStmt(If);
203}
204
205void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
206 Indent() << "switch (";
207 PrintExpr(Node->getCond());
208 OS << ")";
209
210 // Pretty print compoundstmt bodies (very common).
211 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
212 OS << " ";
213 PrintRawCompoundStmt(CS);
214 OS << "\n";
215 } else {
216 OS << "\n";
217 PrintStmt(Node->getBody());
218 }
219}
220
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000221void StmtPrinter::VisitSwitchCase(SwitchCase*) {
222 assert(0 && "SwitchCase is an abstract class");
223}
224
Reid Spencer5f016e22007-07-11 17:01:13 +0000225void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
226 Indent() << "while (";
227 PrintExpr(Node->getCond());
228 OS << ")\n";
229 PrintStmt(Node->getBody());
230}
231
232void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner8bdcc472007-09-15 21:49:37 +0000233 Indent() << "do ";
234 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
235 PrintRawCompoundStmt(CS);
236 OS << " ";
237 } else {
238 OS << "\n";
239 PrintStmt(Node->getBody());
240 Indent();
241 }
242
Eli Friedmanb3e22962009-05-17 01:05:34 +0000243 OS << "while (";
Reid Spencer5f016e22007-07-11 17:01:13 +0000244 PrintExpr(Node->getCond());
Eli Friedmanb3e22962009-05-17 01:05:34 +0000245 OS << ");\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000246}
247
248void StmtPrinter::VisitForStmt(ForStmt *Node) {
249 Indent() << "for (";
250 if (Node->getInit()) {
251 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000252 PrintRawDeclStmt(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 else
254 PrintExpr(cast<Expr>(Node->getInit()));
255 }
Chris Lattner8bdcc472007-09-15 21:49:37 +0000256 OS << ";";
257 if (Node->getCond()) {
258 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000259 PrintExpr(Node->getCond());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000260 }
261 OS << ";";
262 if (Node->getInc()) {
263 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 PrintExpr(Node->getInc());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000265 }
266 OS << ") ";
267
268 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
269 PrintRawCompoundStmt(CS);
270 OS << "\n";
271 } else {
272 OS << "\n";
273 PrintStmt(Node->getBody());
274 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000275}
276
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000277void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000278 Indent() << "for (";
279 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000280 PrintRawDeclStmt(DS);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000281 else
282 PrintExpr(cast<Expr>(Node->getElement()));
283 OS << " in ";
284 PrintExpr(Node->getCollection());
285 OS << ") ";
286
287 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
288 PrintRawCompoundStmt(CS);
289 OS << "\n";
290 } else {
291 OS << "\n";
292 PrintStmt(Node->getBody());
293 }
294}
295
Reid Spencer5f016e22007-07-11 17:01:13 +0000296void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
297 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
298}
299
300void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
301 Indent() << "goto *";
302 PrintExpr(Node->getTarget());
303 OS << ";\n";
304}
305
306void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
307 Indent() << "continue;\n";
308}
309
310void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
311 Indent() << "break;\n";
312}
313
314
315void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
316 Indent() << "return";
317 if (Node->getRetValue()) {
318 OS << " ";
319 PrintExpr(Node->getRetValue());
320 }
321 OS << ";\n";
322}
323
Chris Lattnerfe795952007-10-29 04:04:16 +0000324
325void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson39c47b52007-11-23 23:12:25 +0000326 Indent() << "asm ";
327
328 if (Node->isVolatile())
329 OS << "volatile ";
330
331 OS << "(";
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000332 VisitStringLiteral(Node->getAsmString());
Anders Carlssonb235fc22007-11-22 01:36:19 +0000333
334 // Outputs
335 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
336 Node->getNumClobbers() != 0)
337 OS << " : ";
338
339 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
340 if (i != 0)
341 OS << ", ";
342
343 if (!Node->getOutputName(i).empty()) {
344 OS << '[';
345 OS << Node->getOutputName(i);
346 OS << "] ";
347 }
348
Chris Lattnerb3277932009-03-10 04:59:06 +0000349 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000350 OS << " ";
351 Visit(Node->getOutputExpr(i));
352 }
353
354 // Inputs
355 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
356 OS << " : ";
357
358 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
359 if (i != 0)
360 OS << ", ";
361
362 if (!Node->getInputName(i).empty()) {
363 OS << '[';
364 OS << Node->getInputName(i);
365 OS << "] ";
366 }
367
Chris Lattnerb3277932009-03-10 04:59:06 +0000368 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000369 OS << " ";
370 Visit(Node->getInputExpr(i));
371 }
372
373 // Clobbers
374 if (Node->getNumClobbers() != 0)
375 OS << " : ";
376
377 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
378 if (i != 0)
379 OS << ", ";
380
381 VisitStringLiteral(Node->getClobber(i));
382 }
383
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000384 OS << ");\n";
Chris Lattnerfe795952007-10-29 04:04:16 +0000385}
386
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000387void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000388 Indent() << "@try";
389 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
390 PrintRawCompoundStmt(TS);
391 OS << "\n";
392 }
393
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000394 for (ObjCAtCatchStmt *catchStmt =
395 static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts());
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000396 catchStmt;
397 catchStmt =
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000398 static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000399 Indent() << "@catch(";
Steve Naroff7ba138a2009-03-03 19:52:17 +0000400 if (catchStmt->getCatchParamDecl()) {
401 if (Decl *DS = catchStmt->getCatchParamDecl())
402 PrintRawDecl(DS);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000403 }
404 OS << ")";
405 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody()))
406 {
407 PrintRawCompoundStmt(CS);
408 OS << "\n";
409 }
410 }
411
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000412 if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>(
Fariborz Jahanian1e7eab42007-11-07 00:46:42 +0000413 Node->getFinallyStmt())) {
414 Indent() << "@finally";
415 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000416 OS << "\n";
417 }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000418}
419
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000420void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000421}
422
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000423void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000424 Indent() << "@catch (...) { /* todo */ } \n";
425}
426
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000427void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000428 Indent() << "@throw";
429 if (Node->getThrowExpr()) {
430 OS << " ";
431 PrintExpr(Node->getThrowExpr());
432 }
433 OS << ";\n";
434}
435
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000436void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000437 Indent() << "@synchronized (";
438 PrintExpr(Node->getSynchExpr());
439 OS << ")";
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000440 PrintRawCompoundStmt(Node->getSynchBody());
441 OS << "\n";
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000442}
443
Sebastian Redl8351da02008-12-22 21:35:02 +0000444void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
445 OS << "catch (";
Sebastian Redl4b07b292008-12-22 19:15:10 +0000446 if (Decl *ExDecl = Node->getExceptionDecl())
447 PrintRawDecl(ExDecl);
448 else
449 OS << "...";
450 OS << ") ";
451 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl8351da02008-12-22 21:35:02 +0000452}
453
454void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
455 Indent();
456 PrintRawCXXCatchStmt(Node);
457 OS << "\n";
458}
459
460void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
461 Indent() << "try ";
462 PrintRawCompoundStmt(Node->getTryBlock());
463 for(unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
464 OS << " ";
465 PrintRawCXXCatchStmt(Node->getHandler(i));
466 }
Sebastian Redl4b07b292008-12-22 19:15:10 +0000467 OS << "\n";
468}
469
Reid Spencer5f016e22007-07-11 17:01:13 +0000470//===----------------------------------------------------------------------===//
471// Expr printing methods.
472//===----------------------------------------------------------------------===//
473
474void StmtPrinter::VisitExpr(Expr *Node) {
475 OS << "<<unknown expr type>>";
476}
477
478void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +0000479 OS << Node->getDecl()->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000480}
481
Douglas Gregorbad35182009-03-19 03:51:16 +0000482void StmtPrinter::VisitQualifiedDeclRefExpr(QualifiedDeclRefExpr *Node) {
Douglas Gregor1a49af92009-01-06 05:10:23 +0000483 NamedDecl *D = Node->getDecl();
484
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000485 Node->getQualifier()->print(OS, Policy);
Douglas Gregor1a49af92009-01-06 05:10:23 +0000486 OS << D->getNameAsString();
487}
488
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000489void StmtPrinter::VisitUnresolvedDeclRefExpr(UnresolvedDeclRefExpr *Node) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000490 Node->getQualifier()->print(OS, Policy);
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000491 OS << Node->getDeclName().getAsString();
492}
493
Steve Naroff7779db42007-11-12 14:29:37 +0000494void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000495 if (Node->getBase()) {
496 PrintExpr(Node->getBase());
497 OS << (Node->isArrow() ? "->" : ".");
498 }
Chris Lattner39f34e92008-11-24 04:00:27 +0000499 OS << Node->getDecl()->getNameAsString();
Steve Naroff7779db42007-11-12 14:29:37 +0000500}
501
Steve Naroffae784072008-05-30 00:40:33 +0000502void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
503 if (Node->getBase()) {
504 PrintExpr(Node->getBase());
505 OS << ".";
506 }
Steve Naroffc77a6362008-12-04 16:24:46 +0000507 OS << Node->getProperty()->getNameAsCString();
Steve Naroffae784072008-05-30 00:40:33 +0000508}
509
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000510void StmtPrinter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node) {
511 if (Node->getBase()) {
512 PrintExpr(Node->getBase());
513 OS << ".";
514 }
515 // FIXME: Setter/Getter names
516}
517
Chris Lattnerd9f69102008-08-10 01:53:14 +0000518void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson22742662007-07-21 05:21:51 +0000519 switch (Node->getIdentType()) {
520 default:
521 assert(0 && "unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000522 case PredefinedExpr::Func:
Anders Carlsson22742662007-07-21 05:21:51 +0000523 OS << "__func__";
524 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000525 case PredefinedExpr::Function:
Anders Carlsson22742662007-07-21 05:21:51 +0000526 OS << "__FUNCTION__";
527 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000528 case PredefinedExpr::PrettyFunction:
Anders Carlsson22742662007-07-21 05:21:51 +0000529 OS << "__PRETTY_FUNCTION__";
530 break;
531 }
532}
533
Reid Spencer5f016e22007-07-11 17:01:13 +0000534void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000535 unsigned value = Node->getValue();
Chris Lattnerc250aae2008-06-07 22:35:38 +0000536 if (Node->isWide())
537 OS << "L";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000538 switch (value) {
539 case '\\':
540 OS << "'\\\\'";
541 break;
542 case '\'':
543 OS << "'\\''";
544 break;
545 case '\a':
546 // TODO: K&R: the meaning of '\\a' is different in traditional C
547 OS << "'\\a'";
548 break;
549 case '\b':
550 OS << "'\\b'";
551 break;
552 // Nonstandard escape sequence.
553 /*case '\e':
554 OS << "'\\e'";
555 break;*/
556 case '\f':
557 OS << "'\\f'";
558 break;
559 case '\n':
560 OS << "'\\n'";
561 break;
562 case '\r':
563 OS << "'\\r'";
564 break;
565 case '\t':
566 OS << "'\\t'";
567 break;
568 case '\v':
569 OS << "'\\v'";
570 break;
571 default:
Ted Kremenek471733d2008-02-23 00:52:04 +0000572 if (value < 256 && isprint(value)) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000573 OS << "'" << (char)value << "'";
574 } else if (value < 256) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000575 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000576 } else {
577 // FIXME what to really do here?
578 OS << value;
579 }
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000580 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000581}
582
583void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
584 bool isSigned = Node->getType()->isSignedIntegerType();
585 OS << Node->getValue().toString(10, isSigned);
586
587 // Emit suffixes. Integer literals are always a builtin integer type.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000588 switch (Node->getType()->getAsBuiltinType()->getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000589 default: assert(0 && "Unexpected type for integer literal!");
590 case BuiltinType::Int: break; // no suffix.
591 case BuiltinType::UInt: OS << 'U'; break;
592 case BuiltinType::Long: OS << 'L'; break;
593 case BuiltinType::ULong: OS << "UL"; break;
594 case BuiltinType::LongLong: OS << "LL"; break;
595 case BuiltinType::ULongLong: OS << "ULL"; break;
596 }
597}
598void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner86e499d2007-08-01 00:23:58 +0000599 // FIXME: print value more precisely.
Chris Lattnerda8249e2008-06-07 22:13:43 +0000600 OS << Node->getValueAsApproximateDouble();
Reid Spencer5f016e22007-07-11 17:01:13 +0000601}
Chris Lattner5d661452007-08-26 03:42:43 +0000602
603void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
604 PrintExpr(Node->getSubExpr());
605 OS << "i";
606}
607
Reid Spencer5f016e22007-07-11 17:01:13 +0000608void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
609 if (Str->isWide()) OS << 'L';
610 OS << '"';
Anders Carlssonee98ac52007-10-15 02:50:23 +0000611
Reid Spencer5f016e22007-07-11 17:01:13 +0000612 // FIXME: this doesn't print wstrings right.
613 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattner9a81c872009-01-16 19:25:18 +0000614 unsigned char Char = Str->getStrData()[i];
615
616 switch (Char) {
617 default:
618 if (isprint(Char))
619 OS << (char)Char;
620 else // Output anything hard as an octal escape.
621 OS << '\\'
622 << (char)('0'+ ((Char >> 6) & 7))
623 << (char)('0'+ ((Char >> 3) & 7))
624 << (char)('0'+ ((Char >> 0) & 7));
625 break;
626 // Handle some common non-printable cases to make dumps prettier.
Reid Spencer5f016e22007-07-11 17:01:13 +0000627 case '\\': OS << "\\\\"; break;
628 case '"': OS << "\\\""; break;
629 case '\n': OS << "\\n"; break;
630 case '\t': OS << "\\t"; break;
631 case '\a': OS << "\\a"; break;
632 case '\b': OS << "\\b"; break;
633 }
634 }
635 OS << '"';
636}
637void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
638 OS << "(";
639 PrintExpr(Node->getSubExpr());
640 OS << ")";
641}
642void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +0000643 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000644 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner296bf192007-08-23 21:46:40 +0000645
Sebastian Redl05189992008-11-11 17:56:53 +0000646 // Print a space if this is an "identifier operator" like __real.
Chris Lattner296bf192007-08-23 21:46:40 +0000647 switch (Node->getOpcode()) {
648 default: break;
Chris Lattner296bf192007-08-23 21:46:40 +0000649 case UnaryOperator::Real:
650 case UnaryOperator::Imag:
651 case UnaryOperator::Extension:
652 OS << ' ';
653 break;
654 }
655 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000656 PrintExpr(Node->getSubExpr());
657
658 if (Node->isPostfix())
659 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +0000660}
Chris Lattner704fe352007-08-30 17:59:59 +0000661
662bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
Eli Friedman35183ac2009-02-27 06:44:11 +0000663 if (isa<UnaryOperator>(E)) {
Chris Lattner704fe352007-08-30 17:59:59 +0000664 // Base case, print the type and comma.
665 OS << E->getType().getAsString() << ", ";
666 return true;
667 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
668 PrintOffsetOfDesignator(ASE->getLHS());
669 OS << "[";
670 PrintExpr(ASE->getRHS());
671 OS << "]";
672 return false;
673 } else {
674 MemberExpr *ME = cast<MemberExpr>(E);
675 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
Chris Lattner39f34e92008-11-24 04:00:27 +0000676 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getNameAsString();
Chris Lattner704fe352007-08-30 17:59:59 +0000677 return false;
678 }
679}
680
681void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
682 OS << "__builtin_offsetof(";
683 PrintOffsetOfDesignator(Node->getSubExpr());
684 OS << ")";
685}
686
Sebastian Redl05189992008-11-11 17:56:53 +0000687void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
688 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
689 if (Node->isArgumentType())
690 OS << "(" << Node->getArgumentType().getAsString() << ")";
691 else {
692 OS << " ";
693 PrintExpr(Node->getArgumentExpr());
694 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000695}
696void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +0000697 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000698 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +0000699 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000700 OS << "]";
701}
702
703void StmtPrinter::VisitCallExpr(CallExpr *Call) {
704 PrintExpr(Call->getCallee());
705 OS << "(";
706 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattner04421082008-04-08 04:40:51 +0000707 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
708 // Don't print any defaulted arguments
709 break;
710 }
711
Reid Spencer5f016e22007-07-11 17:01:13 +0000712 if (i) OS << ", ";
713 PrintExpr(Call->getArg(i));
714 }
715 OS << ")";
716}
717void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregorb3eef682009-01-08 22:45:41 +0000718 // FIXME: Suppress printing implicit bases (like "this")
719 PrintExpr(Node->getBase());
720 OS << (Node->isArrow() ? "->" : ".");
721 // FIXME: Suppress printing references to unnamed objects
722 // representing anonymous unions/structs
Douglas Gregor86f19402008-12-20 23:49:58 +0000723 OS << Node->getMemberDecl()->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000724}
Nate Begeman213541a2008-04-18 23:10:10 +0000725void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +0000726 PrintExpr(Node->getBase());
727 OS << ".";
728 OS << Node->getAccessor().getName();
729}
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000730void StmtPrinter::VisitCastExpr(CastExpr *) {
731 assert(0 && "CastExpr is an abstract class");
732}
Douglas Gregor49badde2008-10-27 19:41:14 +0000733void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *) {
734 assert(0 && "ExplicitCastExpr is an abstract class");
735}
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000736void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000737 OS << "(" << Node->getType().getAsString() << ")";
Reid Spencer5f016e22007-07-11 17:01:13 +0000738 PrintExpr(Node->getSubExpr());
739}
Steve Naroffaff1edd2007-07-19 21:32:11 +0000740void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
741 OS << "(" << Node->getType().getAsString() << ")";
742 PrintExpr(Node->getInitializer());
743}
Steve Naroff49b45262007-07-13 16:58:59 +0000744void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroff90045e82007-07-13 23:32:42 +0000745 // No need to print anything, simply forward to the sub expression.
746 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +0000747}
Reid Spencer5f016e22007-07-11 17:01:13 +0000748void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
749 PrintExpr(Node->getLHS());
750 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
751 PrintExpr(Node->getRHS());
752}
Chris Lattnereb14fe82007-08-25 02:00:02 +0000753void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
754 PrintExpr(Node->getLHS());
755 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
756 PrintExpr(Node->getRHS());
757}
Reid Spencer5f016e22007-07-11 17:01:13 +0000758void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
759 PrintExpr(Node->getCond());
Ted Kremenek8e911c42007-11-26 18:27:54 +0000760
761 if (Node->getLHS()) {
762 OS << " ? ";
763 PrintExpr(Node->getLHS());
764 OS << " : ";
765 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000766 else { // Handle GCC extension where LHS can be NULL.
Ted Kremenek8e911c42007-11-26 18:27:54 +0000767 OS << " ?: ";
768 }
769
Reid Spencer5f016e22007-07-11 17:01:13 +0000770 PrintExpr(Node->getRHS());
771}
772
773// GNU extensions.
774
Chris Lattner6481a572007-08-03 17:31:20 +0000775void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000776 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000777}
778
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000779void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
780 OS << "(";
781 PrintRawCompoundStmt(E->getSubStmt());
782 OS << ")";
783}
784
Steve Naroffd34e9152007-08-01 22:05:33 +0000785void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
786 OS << "__builtin_types_compatible_p(";
787 OS << Node->getArgType1().getAsString() << ",";
788 OS << Node->getArgType2().getAsString() << ")";
789}
790
Steve Naroffd04fdd52007-08-03 21:21:27 +0000791void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
792 OS << "__builtin_choose_expr(";
793 PrintExpr(Node->getCond());
Chris Lattner94f05e32007-08-04 00:20:15 +0000794 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000795 PrintExpr(Node->getLHS());
Chris Lattner94f05e32007-08-04 00:20:15 +0000796 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000797 PrintExpr(Node->getRHS());
798 OS << ")";
799}
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000800
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000801void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
802 OS << "__null";
803}
804
Eli Friedmand38617c2008-05-14 19:38:39 +0000805void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
806 OS << "__builtin_shufflevector(";
807 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
808 if (i) OS << ", ";
809 PrintExpr(Node->getExpr(i));
810 }
811 OS << ")";
812}
813
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000814void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000815 if (Node->getSyntacticForm()) {
816 Visit(Node->getSyntacticForm());
817 return;
818 }
819
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000820 OS << "{ ";
821 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
822 if (i) OS << ", ";
Douglas Gregor4c678342009-01-28 21:54:33 +0000823 if (Node->getInit(i))
824 PrintExpr(Node->getInit(i));
825 else
826 OS << "0";
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000827 }
828 OS << " }";
829}
830
Douglas Gregor05c13a32009-01-22 00:58:24 +0000831void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000832 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
833 DEnd = Node->designators_end();
834 D != DEnd; ++D) {
835 if (D->isFieldDesignator()) {
836 if (D->getDotLoc().isInvalid())
837 OS << D->getFieldName()->getName() << ":";
838 else
839 OS << "." << D->getFieldName()->getName();
840 } else {
841 OS << "[";
842 if (D->isArrayDesignator()) {
843 PrintExpr(Node->getArrayIndex(*D));
844 } else {
845 PrintExpr(Node->getArrayRangeStart(*D));
846 OS << " ... ";
847 PrintExpr(Node->getArrayRangeEnd(*D));
848 }
849 OS << "]";
850 }
851 }
852
853 OS << " = ";
854 PrintExpr(Node->getInit());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000855}
856
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000857void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000858 if (Policy.CPlusPlus)
859 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()";
860 else {
861 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")";
862 if (Node->getType()->isRecordType())
863 OS << "{}";
864 else
865 OS << 0;
866 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000867}
868
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000869void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000870 OS << "__builtin_va_arg(";
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000871 PrintExpr(Node->getSubExpr());
872 OS << ", ";
873 OS << Node->getType().getAsString();
874 OS << ")";
875}
876
Reid Spencer5f016e22007-07-11 17:01:13 +0000877// C++
Douglas Gregorb4609802008-11-14 16:09:21 +0000878void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
879 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
880 "",
881#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
882 Spelling,
883#include "clang/Basic/OperatorKinds.def"
884 };
885
886 OverloadedOperatorKind Kind = Node->getOperator();
887 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
888 if (Node->getNumArgs() == 1) {
889 OS << OpStrings[Kind] << ' ';
890 PrintExpr(Node->getArg(0));
891 } else {
892 PrintExpr(Node->getArg(0));
893 OS << ' ' << OpStrings[Kind];
894 }
895 } else if (Kind == OO_Call) {
896 PrintExpr(Node->getArg(0));
897 OS << '(';
898 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
899 if (ArgIdx > 1)
900 OS << ", ";
901 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
902 PrintExpr(Node->getArg(ArgIdx));
903 }
904 OS << ')';
905 } else if (Kind == OO_Subscript) {
906 PrintExpr(Node->getArg(0));
907 OS << '[';
908 PrintExpr(Node->getArg(1));
909 OS << ']';
910 } else if (Node->getNumArgs() == 1) {
911 OS << OpStrings[Kind] << ' ';
912 PrintExpr(Node->getArg(0));
913 } else if (Node->getNumArgs() == 2) {
914 PrintExpr(Node->getArg(0));
915 OS << ' ' << OpStrings[Kind] << ' ';
916 PrintExpr(Node->getArg(1));
917 } else {
918 assert(false && "unknown overloaded operator");
919 }
920}
Reid Spencer5f016e22007-07-11 17:01:13 +0000921
Douglas Gregor88a35142008-12-22 05:46:06 +0000922void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
923 VisitCallExpr(cast<CallExpr>(Node));
924}
925
Douglas Gregor49badde2008-10-27 19:41:14 +0000926void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
927 OS << Node->getCastName() << '<';
928 OS << Node->getTypeAsWritten().getAsString() << ">(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000929 PrintExpr(Node->getSubExpr());
930 OS << ")";
931}
932
Douglas Gregor49badde2008-10-27 19:41:14 +0000933void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
934 VisitCXXNamedCastExpr(Node);
935}
936
937void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
938 VisitCXXNamedCastExpr(Node);
939}
940
941void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
942 VisitCXXNamedCastExpr(Node);
943}
944
945void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
946 VisitCXXNamedCastExpr(Node);
947}
948
Sebastian Redlc42e1182008-11-11 11:37:55 +0000949void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
950 OS << "typeid(";
951 if (Node->isTypeOperand()) {
952 OS << Node->getTypeOperand().getAsString();
953 } else {
954 PrintExpr(Node->getExprOperand());
955 }
956 OS << ")";
957}
958
Reid Spencer5f016e22007-07-11 17:01:13 +0000959void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
960 OS << (Node->getValue() ? "true" : "false");
961}
962
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000963void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
964 OS << "nullptr";
965}
966
Douglas Gregor796da182008-11-04 14:32:21 +0000967void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
968 OS << "this";
969}
970
Chris Lattner50dd2892008-02-26 00:51:44 +0000971void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
972 if (Node->getSubExpr() == 0)
973 OS << "throw";
974 else {
975 OS << "throw ";
976 PrintExpr(Node->getSubExpr());
977 }
978}
979
Chris Lattner04421082008-04-08 04:40:51 +0000980void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
981 // Nothing to print: we picked up the default argument
982}
983
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000984void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
985 OS << Node->getType().getAsString();
986 OS << "(";
987 PrintExpr(Node->getSubExpr());
988 OS << ")";
989}
990
Douglas Gregor506ae412009-01-16 18:33:17 +0000991void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
992 OS << Node->getType().getAsString();
993 OS << "(";
994 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
995 ArgEnd = Node->arg_end();
996 Arg != ArgEnd; ++Arg) {
997 if (Arg != Node->arg_begin())
998 OS << ", ";
999 PrintExpr(*Arg);
1000 }
1001 OS << ")";
1002}
1003
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001004void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
1005 OS << Node->getType().getAsString() << "()";
1006}
1007
Argyrios Kyrtzidis9e922b12008-09-09 23:47:53 +00001008void
1009StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
1010 PrintRawDecl(E->getVarDecl());
1011}
1012
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001013void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1014 if (E->isGlobalNew())
1015 OS << "::";
1016 OS << "new ";
1017 unsigned NumPlace = E->getNumPlacementArgs();
1018 if (NumPlace > 0) {
1019 OS << "(";
1020 PrintExpr(E->getPlacementArg(0));
1021 for (unsigned i = 1; i < NumPlace; ++i) {
1022 OS << ", ";
1023 PrintExpr(E->getPlacementArg(i));
1024 }
1025 OS << ") ";
1026 }
1027 if (E->isParenTypeId())
1028 OS << "(";
Sebastian Redl6fec6482008-12-02 22:08:59 +00001029 std::string TypeS;
1030 if (Expr *Size = E->getArraySize()) {
1031 llvm::raw_string_ostream s(TypeS);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001032 Size->printPretty(s, Helper, Policy);
Sebastian Redl6fec6482008-12-02 22:08:59 +00001033 s.flush();
1034 TypeS = "[" + TypeS + "]";
1035 }
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001036 E->getAllocatedType().getAsStringInternal(TypeS, Policy);
Sebastian Redl6fec6482008-12-02 22:08:59 +00001037 OS << TypeS;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001038 if (E->isParenTypeId())
1039 OS << ")";
1040
1041 if (E->hasInitializer()) {
1042 OS << "(";
1043 unsigned NumCons = E->getNumConstructorArgs();
1044 if (NumCons > 0) {
1045 PrintExpr(E->getConstructorArg(0));
1046 for (unsigned i = 1; i < NumCons; ++i) {
1047 OS << ", ";
1048 PrintExpr(E->getConstructorArg(i));
1049 }
1050 }
1051 OS << ")";
1052 }
1053}
1054
1055void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1056 if (E->isGlobalDelete())
1057 OS << "::";
1058 OS << "delete ";
1059 if (E->isArrayForm())
1060 OS << "[] ";
1061 PrintExpr(E->getArgument());
1062}
1063
Douglas Gregor17330012009-02-04 15:01:18 +00001064void StmtPrinter::VisitUnresolvedFunctionNameExpr(UnresolvedFunctionNameExpr *E) {
1065 OS << E->getName().getAsString();
Douglas Gregor5c37de72008-12-06 00:22:45 +00001066}
1067
Anders Carlssone349bea2009-04-23 02:32:43 +00001068void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
1069 // Nothing to print.
1070}
1071
Anders Carlsson2d44e8a2009-05-01 22:18:43 +00001072void StmtPrinter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
Anders Carlsson02bbfa32009-04-24 22:47:04 +00001073 // Just forward to the sub expression.
1074 PrintExpr(E->getSubExpr());
1075}
1076
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001077void
1078StmtPrinter::VisitCXXUnresolvedConstructExpr(
1079 CXXUnresolvedConstructExpr *Node) {
1080 OS << Node->getTypeAsWritten().getAsString();
1081 OS << "(";
1082 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
1083 ArgEnd = Node->arg_end();
1084 Arg != ArgEnd; ++Arg) {
1085 if (Arg != Node->arg_begin())
1086 OS << ", ";
1087 PrintExpr(*Arg);
1088 }
1089 OS << ")";
1090}
1091
Douglas Gregor1c0ca592009-05-22 21:13:27 +00001092void StmtPrinter::VisitCXXUnresolvedMemberExpr(CXXUnresolvedMemberExpr *Node) {
1093 PrintExpr(Node->getBase());
1094 OS << (Node->isArrow() ? "->" : ".");
1095 OS << Node->getMember().getAsString();
1096}
1097
Sebastian Redl64b45f72009-01-05 20:52:13 +00001098static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1099 switch (UTT) {
1100 default: assert(false && "Unknown type trait");
1101 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1102 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1103 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1104 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1105 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1106 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1107 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1108 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1109 case UTT_IsAbstract: return "__is_abstract";
1110 case UTT_IsClass: return "__is_class";
1111 case UTT_IsEmpty: return "__is_empty";
1112 case UTT_IsEnum: return "__is_enum";
1113 case UTT_IsPOD: return "__is_pod";
1114 case UTT_IsPolymorphic: return "__is_polymorphic";
1115 case UTT_IsUnion: return "__is_union";
1116 }
1117}
1118
1119void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1120 OS << getTypeTraitName(E->getTrait()) << "("
1121 << E->getQueriedType().getAsString() << ")";
1122}
1123
Anders Carlsson55085182007-08-21 17:43:55 +00001124// Obj-C
1125
1126void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1127 OS << "@";
1128 VisitStringLiteral(Node->getString());
1129}
Reid Spencer5f016e22007-07-11 17:01:13 +00001130
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001131void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001132 OS << "@encode(" << Node->getEncodedType().getAsString() << ')';
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001133}
1134
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001135void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001136 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001137}
1138
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001139void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001140 OS << "@protocol(" << Node->getProtocol()->getNameAsString() << ')';
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001141}
1142
Steve Naroff563477d2007-09-18 23:55:05 +00001143void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1144 OS << "[";
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001145 Expr *receiver = Mess->getReceiver();
1146 if (receiver) PrintExpr(receiver);
1147 else OS << Mess->getClassName()->getName();
Ted Kremenekc29efd82008-05-02 17:32:38 +00001148 OS << ' ';
Ted Kremenek97b7f262008-04-16 04:30:16 +00001149 Selector selector = Mess->getSelector();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001150 if (selector.isUnarySelector()) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001151 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001152 } else {
1153 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001154 if (i < selector.getNumArgs()) {
1155 if (i > 0) OS << ' ';
1156 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner39f34e92008-11-24 04:00:27 +00001157 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremenekc29efd82008-05-02 17:32:38 +00001158 else
1159 OS << ":";
1160 }
1161 else OS << ", "; // Handle variadic methods.
1162
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001163 PrintExpr(Mess->getArg(i));
1164 }
Steve Naroff563477d2007-09-18 23:55:05 +00001165 }
1166 OS << "]";
1167}
1168
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001169void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) {
1170 OS << "super";
1171}
1172
Steve Naroff4eb206b2008-09-03 18:15:37 +00001173void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001174 BlockDecl *BD = Node->getBlockDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001175 OS << "^";
1176
1177 const FunctionType *AFT = Node->getFunctionType();
1178
Douglas Gregor72564e72009-02-26 23:50:07 +00001179 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001180 OS << "()";
Douglas Gregor72564e72009-02-26 23:50:07 +00001181 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001182 OS << '(';
1183 std::string ParamStr;
Steve Naroff56ee6892008-10-08 17:01:13 +00001184 for (BlockDecl::param_iterator AI = BD->param_begin(),
1185 E = BD->param_end(); AI != E; ++AI) {
1186 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner39f34e92008-11-24 04:00:27 +00001187 ParamStr = (*AI)->getNameAsString();
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001188 (*AI)->getType().getAsStringInternal(ParamStr, Policy);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001189 OS << ParamStr;
1190 }
1191
Douglas Gregor72564e72009-02-26 23:50:07 +00001192 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001193 if (FT->isVariadic()) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001194 if (!BD->param_empty()) OS << ", ";
Steve Naroff4eb206b2008-09-03 18:15:37 +00001195 OS << "...";
1196 }
1197 OS << ')';
1198 }
1199}
1200
Steve Naroff4eb206b2008-09-03 18:15:37 +00001201void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001202 OS << Node->getDecl()->getNameAsString();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001203}
Reid Spencer5f016e22007-07-11 17:01:13 +00001204//===----------------------------------------------------------------------===//
1205// Stmt method implementations
1206//===----------------------------------------------------------------------===//
1207
Eli Friedman48d14a22009-05-30 05:03:24 +00001208void Stmt::dumpPretty(ASTContext& Context) const {
1209 printPretty(llvm::errs(), Context, 0, PrintingPolicy());
Reid Spencer5f016e22007-07-11 17:01:13 +00001210}
1211
Eli Friedman48d14a22009-05-30 05:03:24 +00001212void Stmt::printPretty(llvm::raw_ostream &OS, ASTContext& Context,
1213 PrinterHelper* Helper,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001214 const PrintingPolicy &Policy,
1215 unsigned Indentation) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001216 if (this == 0) {
1217 OS << "<NULL>";
1218 return;
1219 }
1220
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001221 if (Policy.Dump) {
1222 dump();
1223 return;
1224 }
1225
Eli Friedman48d14a22009-05-30 05:03:24 +00001226 StmtPrinter P(OS, Context, Helper, Policy, Indentation);
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() {}