blob: a698688e6cee775c08768175180b791fca4e8f98 [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;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000033 PrintingPolicy Policy;
34
Reid Spencer5f016e22007-07-11 17:01:13 +000035 public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000036 StmtPrinter(llvm::raw_ostream &os, PrinterHelper* helper,
37 const PrintingPolicy &Policy = PrintingPolicy(),
38 unsigned Indentation = 0)
39 : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy) {}
Reid Spencer5f016e22007-07-11 17:01:13 +000040
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000041 void PrintStmt(Stmt *S) {
42 PrintStmt(S, Policy.Indentation);
43 }
44
45 void PrintStmt(Stmt *S, int SubIndent) {
Reid Spencer5f016e22007-07-11 17:01:13 +000046 IndentLevel += SubIndent;
47 if (S && isa<Expr>(S)) {
48 // If this is an expr used in a stmt context, indent and newline it.
49 Indent();
Chris Lattnerc5598cb2007-08-21 04:04:25 +000050 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000051 OS << ";\n";
52 } else if (S) {
Chris Lattnerc5598cb2007-08-21 04:04:25 +000053 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000054 } else {
55 Indent() << "<<<NULL STATEMENT>>>\n";
56 }
57 IndentLevel -= SubIndent;
58 }
Eli Friedmandb23b152009-05-30 00:19:54 +000059
Reid Spencer5f016e22007-07-11 17:01:13 +000060 void PrintRawCompoundStmt(CompoundStmt *S);
61 void PrintRawDecl(Decl *D);
Ted Kremenekecd64c52008-10-06 18:39:36 +000062 void PrintRawDeclStmt(DeclStmt *S);
Reid Spencer5f016e22007-07-11 17:01:13 +000063 void PrintRawIfStmt(IfStmt *If);
Sebastian Redl8351da02008-12-22 21:35:02 +000064 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
Reid Spencer5f016e22007-07-11 17:01:13 +000065
66 void PrintExpr(Expr *E) {
67 if (E)
Chris Lattnerc5598cb2007-08-21 04:04:25 +000068 Visit(E);
Reid Spencer5f016e22007-07-11 17:01:13 +000069 else
70 OS << "<null expr>";
71 }
72
Mike Stump071e4da2009-02-10 20:16:46 +000073 llvm::raw_ostream &Indent(int Delta = 0) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000074 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
75 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +000076 return OS;
77 }
78
Chris Lattner704fe352007-08-30 17:59:59 +000079 bool PrintOffsetOfDesignator(Expr *E);
80 void VisitUnaryOffsetOf(UnaryOperator *Node);
81
Ted Kremenek42a509f2007-08-31 21:30:12 +000082 void Visit(Stmt* S) {
83 if (Helper && Helper->handledStmt(S,OS))
84 return;
85 else StmtVisitor<StmtPrinter>::Visit(S);
86 }
87
Chris Lattnerc5598cb2007-08-21 04:04:25 +000088 void VisitStmt(Stmt *Node);
Douglas Gregorf2cad862008-11-14 12:46:07 +000089#define STMT(CLASS, PARENT) \
Chris Lattnerc5598cb2007-08-21 04:04:25 +000090 void Visit##CLASS(CLASS *Node);
Reid Spencer5f016e22007-07-11 17:01:13 +000091#include "clang/AST/StmtNodes.def"
92 };
93}
94
95//===----------------------------------------------------------------------===//
96// Stmt printing methods.
97//===----------------------------------------------------------------------===//
98
99void StmtPrinter::VisitStmt(Stmt *Node) {
100 Indent() << "<<unknown stmt type>>\n";
101}
102
103/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
104/// with no newline after the }.
105void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
106 OS << "{\n";
107 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
108 I != E; ++I)
109 PrintStmt(*I);
110
111 Indent() << "}";
112}
113
114void StmtPrinter::PrintRawDecl(Decl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000115 D->print(OS, *(ASTContext*)0, Policy, IndentLevel);
Mike Stump071e4da2009-02-10 20:16:46 +0000116}
117
Ted Kremenekecd64c52008-10-06 18:39:36 +0000118void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
Eli Friedmandb23b152009-05-30 00:19:54 +0000119 DeclStmt::decl_iterator Begin = S->decl_begin(), End = S->decl_end();
Eli Friedman42f42c02009-05-30 04:20:30 +0000120 llvm::SmallVector<Decl*, 2> Decls;
121 for ( ; Begin != End; ++Begin)
122 Decls.push_back(*Begin);
Eli Friedmandb23b152009-05-30 00:19:54 +0000123
Eli Friedman42f42c02009-05-30 04:20:30 +0000124 Decl::printGroup(Decls.data(), Decls.size(), OS, *(ASTContext*)0, Policy,
125 IndentLevel);
Ted Kremenekecd64c52008-10-06 18:39:36 +0000126}
Reid Spencer5f016e22007-07-11 17:01:13 +0000127
128void StmtPrinter::VisitNullStmt(NullStmt *Node) {
129 Indent() << ";\n";
130}
131
132void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Eli Friedmandb23b152009-05-30 00:19:54 +0000133 Indent();
134 PrintRawDeclStmt(Node);
135 OS << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000136}
137
138void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
139 Indent();
140 PrintRawCompoundStmt(Node);
141 OS << "\n";
142}
143
144void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
145 Indent(-1) << "case ";
146 PrintExpr(Node->getLHS());
147 if (Node->getRHS()) {
148 OS << " ... ";
149 PrintExpr(Node->getRHS());
150 }
151 OS << ":\n";
152
153 PrintStmt(Node->getSubStmt(), 0);
154}
155
156void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
157 Indent(-1) << "default:\n";
158 PrintStmt(Node->getSubStmt(), 0);
159}
160
161void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
162 Indent(-1) << Node->getName() << ":\n";
163 PrintStmt(Node->getSubStmt(), 0);
164}
165
166void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000167 OS << "if (";
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 PrintExpr(If->getCond());
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000169 OS << ')';
Reid Spencer5f016e22007-07-11 17:01:13 +0000170
171 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
172 OS << ' ';
173 PrintRawCompoundStmt(CS);
174 OS << (If->getElse() ? ' ' : '\n');
175 } else {
176 OS << '\n';
177 PrintStmt(If->getThen());
178 if (If->getElse()) Indent();
179 }
180
181 if (Stmt *Else = If->getElse()) {
182 OS << "else";
183
184 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
185 OS << ' ';
186 PrintRawCompoundStmt(CS);
187 OS << '\n';
188 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
189 OS << ' ';
190 PrintRawIfStmt(ElseIf);
191 } else {
192 OS << '\n';
193 PrintStmt(If->getElse());
194 }
195 }
196}
197
198void StmtPrinter::VisitIfStmt(IfStmt *If) {
199 Indent();
200 PrintRawIfStmt(If);
201}
202
203void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
204 Indent() << "switch (";
205 PrintExpr(Node->getCond());
206 OS << ")";
207
208 // Pretty print compoundstmt bodies (very common).
209 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
210 OS << " ";
211 PrintRawCompoundStmt(CS);
212 OS << "\n";
213 } else {
214 OS << "\n";
215 PrintStmt(Node->getBody());
216 }
217}
218
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000219void StmtPrinter::VisitSwitchCase(SwitchCase*) {
220 assert(0 && "SwitchCase is an abstract class");
221}
222
Reid Spencer5f016e22007-07-11 17:01:13 +0000223void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
224 Indent() << "while (";
225 PrintExpr(Node->getCond());
226 OS << ")\n";
227 PrintStmt(Node->getBody());
228}
229
230void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner8bdcc472007-09-15 21:49:37 +0000231 Indent() << "do ";
232 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
233 PrintRawCompoundStmt(CS);
234 OS << " ";
235 } else {
236 OS << "\n";
237 PrintStmt(Node->getBody());
238 Indent();
239 }
240
Eli Friedmanb3e22962009-05-17 01:05:34 +0000241 OS << "while (";
Reid Spencer5f016e22007-07-11 17:01:13 +0000242 PrintExpr(Node->getCond());
Eli Friedmanb3e22962009-05-17 01:05:34 +0000243 OS << ");\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000244}
245
246void StmtPrinter::VisitForStmt(ForStmt *Node) {
247 Indent() << "for (";
248 if (Node->getInit()) {
249 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000250 PrintRawDeclStmt(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000251 else
252 PrintExpr(cast<Expr>(Node->getInit()));
253 }
Chris Lattner8bdcc472007-09-15 21:49:37 +0000254 OS << ";";
255 if (Node->getCond()) {
256 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 PrintExpr(Node->getCond());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000258 }
259 OS << ";";
260 if (Node->getInc()) {
261 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000262 PrintExpr(Node->getInc());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000263 }
264 OS << ") ";
265
266 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
267 PrintRawCompoundStmt(CS);
268 OS << "\n";
269 } else {
270 OS << "\n";
271 PrintStmt(Node->getBody());
272 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000273}
274
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000275void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000276 Indent() << "for (";
277 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000278 PrintRawDeclStmt(DS);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000279 else
280 PrintExpr(cast<Expr>(Node->getElement()));
281 OS << " in ";
282 PrintExpr(Node->getCollection());
283 OS << ") ";
284
285 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
286 PrintRawCompoundStmt(CS);
287 OS << "\n";
288 } else {
289 OS << "\n";
290 PrintStmt(Node->getBody());
291 }
292}
293
Reid Spencer5f016e22007-07-11 17:01:13 +0000294void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
295 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
296}
297
298void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
299 Indent() << "goto *";
300 PrintExpr(Node->getTarget());
301 OS << ";\n";
302}
303
304void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
305 Indent() << "continue;\n";
306}
307
308void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
309 Indent() << "break;\n";
310}
311
312
313void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
314 Indent() << "return";
315 if (Node->getRetValue()) {
316 OS << " ";
317 PrintExpr(Node->getRetValue());
318 }
319 OS << ";\n";
320}
321
Chris Lattnerfe795952007-10-29 04:04:16 +0000322
323void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson39c47b52007-11-23 23:12:25 +0000324 Indent() << "asm ";
325
326 if (Node->isVolatile())
327 OS << "volatile ";
328
329 OS << "(";
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000330 VisitStringLiteral(Node->getAsmString());
Anders Carlssonb235fc22007-11-22 01:36:19 +0000331
332 // Outputs
333 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
334 Node->getNumClobbers() != 0)
335 OS << " : ";
336
337 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
338 if (i != 0)
339 OS << ", ";
340
341 if (!Node->getOutputName(i).empty()) {
342 OS << '[';
343 OS << Node->getOutputName(i);
344 OS << "] ";
345 }
346
Chris Lattnerb3277932009-03-10 04:59:06 +0000347 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000348 OS << " ";
349 Visit(Node->getOutputExpr(i));
350 }
351
352 // Inputs
353 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
354 OS << " : ";
355
356 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
357 if (i != 0)
358 OS << ", ";
359
360 if (!Node->getInputName(i).empty()) {
361 OS << '[';
362 OS << Node->getInputName(i);
363 OS << "] ";
364 }
365
Chris Lattnerb3277932009-03-10 04:59:06 +0000366 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000367 OS << " ";
368 Visit(Node->getInputExpr(i));
369 }
370
371 // Clobbers
372 if (Node->getNumClobbers() != 0)
373 OS << " : ";
374
375 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
376 if (i != 0)
377 OS << ", ";
378
379 VisitStringLiteral(Node->getClobber(i));
380 }
381
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000382 OS << ");\n";
Chris Lattnerfe795952007-10-29 04:04:16 +0000383}
384
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000385void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000386 Indent() << "@try";
387 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
388 PrintRawCompoundStmt(TS);
389 OS << "\n";
390 }
391
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000392 for (ObjCAtCatchStmt *catchStmt =
393 static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts());
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000394 catchStmt;
395 catchStmt =
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000396 static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000397 Indent() << "@catch(";
Steve Naroff7ba138a2009-03-03 19:52:17 +0000398 if (catchStmt->getCatchParamDecl()) {
399 if (Decl *DS = catchStmt->getCatchParamDecl())
400 PrintRawDecl(DS);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000401 }
402 OS << ")";
403 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody()))
404 {
405 PrintRawCompoundStmt(CS);
406 OS << "\n";
407 }
408 }
409
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000410 if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>(
Fariborz Jahanian1e7eab42007-11-07 00:46:42 +0000411 Node->getFinallyStmt())) {
412 Indent() << "@finally";
413 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000414 OS << "\n";
415 }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000416}
417
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000418void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000419}
420
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000421void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000422 Indent() << "@catch (...) { /* todo */ } \n";
423}
424
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000425void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000426 Indent() << "@throw";
427 if (Node->getThrowExpr()) {
428 OS << " ";
429 PrintExpr(Node->getThrowExpr());
430 }
431 OS << ";\n";
432}
433
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000434void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000435 Indent() << "@synchronized (";
436 PrintExpr(Node->getSynchExpr());
437 OS << ")";
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000438 PrintRawCompoundStmt(Node->getSynchBody());
439 OS << "\n";
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000440}
441
Sebastian Redl8351da02008-12-22 21:35:02 +0000442void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
443 OS << "catch (";
Sebastian Redl4b07b292008-12-22 19:15:10 +0000444 if (Decl *ExDecl = Node->getExceptionDecl())
445 PrintRawDecl(ExDecl);
446 else
447 OS << "...";
448 OS << ") ";
449 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl8351da02008-12-22 21:35:02 +0000450}
451
452void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
453 Indent();
454 PrintRawCXXCatchStmt(Node);
455 OS << "\n";
456}
457
458void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
459 Indent() << "try ";
460 PrintRawCompoundStmt(Node->getTryBlock());
461 for(unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
462 OS << " ";
463 PrintRawCXXCatchStmt(Node->getHandler(i));
464 }
Sebastian Redl4b07b292008-12-22 19:15:10 +0000465 OS << "\n";
466}
467
Reid Spencer5f016e22007-07-11 17:01:13 +0000468//===----------------------------------------------------------------------===//
469// Expr printing methods.
470//===----------------------------------------------------------------------===//
471
472void StmtPrinter::VisitExpr(Expr *Node) {
473 OS << "<<unknown expr type>>";
474}
475
476void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +0000477 OS << Node->getDecl()->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000478}
479
Douglas Gregorbad35182009-03-19 03:51:16 +0000480void StmtPrinter::VisitQualifiedDeclRefExpr(QualifiedDeclRefExpr *Node) {
Douglas Gregor1a49af92009-01-06 05:10:23 +0000481 NamedDecl *D = Node->getDecl();
482
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000483 Node->getQualifier()->print(OS, Policy);
Douglas Gregor1a49af92009-01-06 05:10:23 +0000484 OS << D->getNameAsString();
485}
486
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000487void StmtPrinter::VisitUnresolvedDeclRefExpr(UnresolvedDeclRefExpr *Node) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000488 Node->getQualifier()->print(OS, Policy);
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000489 OS << Node->getDeclName().getAsString();
490}
491
Steve Naroff7779db42007-11-12 14:29:37 +0000492void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000493 if (Node->getBase()) {
494 PrintExpr(Node->getBase());
495 OS << (Node->isArrow() ? "->" : ".");
496 }
Chris Lattner39f34e92008-11-24 04:00:27 +0000497 OS << Node->getDecl()->getNameAsString();
Steve Naroff7779db42007-11-12 14:29:37 +0000498}
499
Steve Naroffae784072008-05-30 00:40:33 +0000500void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
501 if (Node->getBase()) {
502 PrintExpr(Node->getBase());
503 OS << ".";
504 }
Steve Naroffc77a6362008-12-04 16:24:46 +0000505 OS << Node->getProperty()->getNameAsCString();
Steve Naroffae784072008-05-30 00:40:33 +0000506}
507
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000508void StmtPrinter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node) {
509 if (Node->getBase()) {
510 PrintExpr(Node->getBase());
511 OS << ".";
512 }
513 // FIXME: Setter/Getter names
514}
515
Chris Lattnerd9f69102008-08-10 01:53:14 +0000516void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson22742662007-07-21 05:21:51 +0000517 switch (Node->getIdentType()) {
518 default:
519 assert(0 && "unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000520 case PredefinedExpr::Func:
Anders Carlsson22742662007-07-21 05:21:51 +0000521 OS << "__func__";
522 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000523 case PredefinedExpr::Function:
Anders Carlsson22742662007-07-21 05:21:51 +0000524 OS << "__FUNCTION__";
525 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000526 case PredefinedExpr::PrettyFunction:
Anders Carlsson22742662007-07-21 05:21:51 +0000527 OS << "__PRETTY_FUNCTION__";
528 break;
529 }
530}
531
Reid Spencer5f016e22007-07-11 17:01:13 +0000532void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000533 unsigned value = Node->getValue();
Chris Lattnerc250aae2008-06-07 22:35:38 +0000534 if (Node->isWide())
535 OS << "L";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000536 switch (value) {
537 case '\\':
538 OS << "'\\\\'";
539 break;
540 case '\'':
541 OS << "'\\''";
542 break;
543 case '\a':
544 // TODO: K&R: the meaning of '\\a' is different in traditional C
545 OS << "'\\a'";
546 break;
547 case '\b':
548 OS << "'\\b'";
549 break;
550 // Nonstandard escape sequence.
551 /*case '\e':
552 OS << "'\\e'";
553 break;*/
554 case '\f':
555 OS << "'\\f'";
556 break;
557 case '\n':
558 OS << "'\\n'";
559 break;
560 case '\r':
561 OS << "'\\r'";
562 break;
563 case '\t':
564 OS << "'\\t'";
565 break;
566 case '\v':
567 OS << "'\\v'";
568 break;
569 default:
Ted Kremenek471733d2008-02-23 00:52:04 +0000570 if (value < 256 && isprint(value)) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000571 OS << "'" << (char)value << "'";
572 } else if (value < 256) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000573 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000574 } else {
575 // FIXME what to really do here?
576 OS << value;
577 }
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000578 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000579}
580
581void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
582 bool isSigned = Node->getType()->isSignedIntegerType();
583 OS << Node->getValue().toString(10, isSigned);
584
585 // Emit suffixes. Integer literals are always a builtin integer type.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000586 switch (Node->getType()->getAsBuiltinType()->getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000587 default: assert(0 && "Unexpected type for integer literal!");
588 case BuiltinType::Int: break; // no suffix.
589 case BuiltinType::UInt: OS << 'U'; break;
590 case BuiltinType::Long: OS << 'L'; break;
591 case BuiltinType::ULong: OS << "UL"; break;
592 case BuiltinType::LongLong: OS << "LL"; break;
593 case BuiltinType::ULongLong: OS << "ULL"; break;
594 }
595}
596void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner86e499d2007-08-01 00:23:58 +0000597 // FIXME: print value more precisely.
Chris Lattnerda8249e2008-06-07 22:13:43 +0000598 OS << Node->getValueAsApproximateDouble();
Reid Spencer5f016e22007-07-11 17:01:13 +0000599}
Chris Lattner5d661452007-08-26 03:42:43 +0000600
601void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
602 PrintExpr(Node->getSubExpr());
603 OS << "i";
604}
605
Reid Spencer5f016e22007-07-11 17:01:13 +0000606void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
607 if (Str->isWide()) OS << 'L';
608 OS << '"';
Anders Carlssonee98ac52007-10-15 02:50:23 +0000609
Reid Spencer5f016e22007-07-11 17:01:13 +0000610 // FIXME: this doesn't print wstrings right.
611 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattner9a81c872009-01-16 19:25:18 +0000612 unsigned char Char = Str->getStrData()[i];
613
614 switch (Char) {
615 default:
616 if (isprint(Char))
617 OS << (char)Char;
618 else // Output anything hard as an octal escape.
619 OS << '\\'
620 << (char)('0'+ ((Char >> 6) & 7))
621 << (char)('0'+ ((Char >> 3) & 7))
622 << (char)('0'+ ((Char >> 0) & 7));
623 break;
624 // Handle some common non-printable cases to make dumps prettier.
Reid Spencer5f016e22007-07-11 17:01:13 +0000625 case '\\': OS << "\\\\"; break;
626 case '"': OS << "\\\""; break;
627 case '\n': OS << "\\n"; break;
628 case '\t': OS << "\\t"; break;
629 case '\a': OS << "\\a"; break;
630 case '\b': OS << "\\b"; break;
631 }
632 }
633 OS << '"';
634}
635void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
636 OS << "(";
637 PrintExpr(Node->getSubExpr());
638 OS << ")";
639}
640void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +0000641 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000642 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner296bf192007-08-23 21:46:40 +0000643
Sebastian Redl05189992008-11-11 17:56:53 +0000644 // Print a space if this is an "identifier operator" like __real.
Chris Lattner296bf192007-08-23 21:46:40 +0000645 switch (Node->getOpcode()) {
646 default: break;
Chris Lattner296bf192007-08-23 21:46:40 +0000647 case UnaryOperator::Real:
648 case UnaryOperator::Imag:
649 case UnaryOperator::Extension:
650 OS << ' ';
651 break;
652 }
653 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 PrintExpr(Node->getSubExpr());
655
656 if (Node->isPostfix())
657 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +0000658}
Chris Lattner704fe352007-08-30 17:59:59 +0000659
660bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
Eli Friedman35183ac2009-02-27 06:44:11 +0000661 if (isa<UnaryOperator>(E)) {
Chris Lattner704fe352007-08-30 17:59:59 +0000662 // Base case, print the type and comma.
663 OS << E->getType().getAsString() << ", ";
664 return true;
665 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
666 PrintOffsetOfDesignator(ASE->getLHS());
667 OS << "[";
668 PrintExpr(ASE->getRHS());
669 OS << "]";
670 return false;
671 } else {
672 MemberExpr *ME = cast<MemberExpr>(E);
673 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
Chris Lattner39f34e92008-11-24 04:00:27 +0000674 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getNameAsString();
Chris Lattner704fe352007-08-30 17:59:59 +0000675 return false;
676 }
677}
678
679void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
680 OS << "__builtin_offsetof(";
681 PrintOffsetOfDesignator(Node->getSubExpr());
682 OS << ")";
683}
684
Sebastian Redl05189992008-11-11 17:56:53 +0000685void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
686 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
687 if (Node->isArgumentType())
688 OS << "(" << Node->getArgumentType().getAsString() << ")";
689 else {
690 OS << " ";
691 PrintExpr(Node->getArgumentExpr());
692 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000693}
694void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +0000695 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000696 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +0000697 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000698 OS << "]";
699}
700
701void StmtPrinter::VisitCallExpr(CallExpr *Call) {
702 PrintExpr(Call->getCallee());
703 OS << "(";
704 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattner04421082008-04-08 04:40:51 +0000705 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
706 // Don't print any defaulted arguments
707 break;
708 }
709
Reid Spencer5f016e22007-07-11 17:01:13 +0000710 if (i) OS << ", ";
711 PrintExpr(Call->getArg(i));
712 }
713 OS << ")";
714}
715void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregorb3eef682009-01-08 22:45:41 +0000716 // FIXME: Suppress printing implicit bases (like "this")
717 PrintExpr(Node->getBase());
718 OS << (Node->isArrow() ? "->" : ".");
719 // FIXME: Suppress printing references to unnamed objects
720 // representing anonymous unions/structs
Douglas Gregor86f19402008-12-20 23:49:58 +0000721 OS << Node->getMemberDecl()->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000722}
Nate Begeman213541a2008-04-18 23:10:10 +0000723void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +0000724 PrintExpr(Node->getBase());
725 OS << ".";
726 OS << Node->getAccessor().getName();
727}
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000728void StmtPrinter::VisitCastExpr(CastExpr *) {
729 assert(0 && "CastExpr is an abstract class");
730}
Douglas Gregor49badde2008-10-27 19:41:14 +0000731void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *) {
732 assert(0 && "ExplicitCastExpr is an abstract class");
733}
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000734void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000735 OS << "(" << Node->getType().getAsString() << ")";
Reid Spencer5f016e22007-07-11 17:01:13 +0000736 PrintExpr(Node->getSubExpr());
737}
Steve Naroffaff1edd2007-07-19 21:32:11 +0000738void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
739 OS << "(" << Node->getType().getAsString() << ")";
740 PrintExpr(Node->getInitializer());
741}
Steve Naroff49b45262007-07-13 16:58:59 +0000742void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroff90045e82007-07-13 23:32:42 +0000743 // No need to print anything, simply forward to the sub expression.
744 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +0000745}
Reid Spencer5f016e22007-07-11 17:01:13 +0000746void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
747 PrintExpr(Node->getLHS());
748 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
749 PrintExpr(Node->getRHS());
750}
Chris Lattnereb14fe82007-08-25 02:00:02 +0000751void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
752 PrintExpr(Node->getLHS());
753 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
754 PrintExpr(Node->getRHS());
755}
Reid Spencer5f016e22007-07-11 17:01:13 +0000756void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
757 PrintExpr(Node->getCond());
Ted Kremenek8e911c42007-11-26 18:27:54 +0000758
759 if (Node->getLHS()) {
760 OS << " ? ";
761 PrintExpr(Node->getLHS());
762 OS << " : ";
763 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000764 else { // Handle GCC extension where LHS can be NULL.
Ted Kremenek8e911c42007-11-26 18:27:54 +0000765 OS << " ?: ";
766 }
767
Reid Spencer5f016e22007-07-11 17:01:13 +0000768 PrintExpr(Node->getRHS());
769}
770
771// GNU extensions.
772
Chris Lattner6481a572007-08-03 17:31:20 +0000773void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000774 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000775}
776
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000777void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
778 OS << "(";
779 PrintRawCompoundStmt(E->getSubStmt());
780 OS << ")";
781}
782
Steve Naroffd34e9152007-08-01 22:05:33 +0000783void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
784 OS << "__builtin_types_compatible_p(";
785 OS << Node->getArgType1().getAsString() << ",";
786 OS << Node->getArgType2().getAsString() << ")";
787}
788
Steve Naroffd04fdd52007-08-03 21:21:27 +0000789void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
790 OS << "__builtin_choose_expr(";
791 PrintExpr(Node->getCond());
Chris Lattner94f05e32007-08-04 00:20:15 +0000792 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000793 PrintExpr(Node->getLHS());
Chris Lattner94f05e32007-08-04 00:20:15 +0000794 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000795 PrintExpr(Node->getRHS());
796 OS << ")";
797}
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000798
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000799void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
800 OS << "__null";
801}
802
Eli Friedmand38617c2008-05-14 19:38:39 +0000803void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
804 OS << "__builtin_shufflevector(";
805 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
806 if (i) OS << ", ";
807 PrintExpr(Node->getExpr(i));
808 }
809 OS << ")";
810}
811
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000812void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000813 if (Node->getSyntacticForm()) {
814 Visit(Node->getSyntacticForm());
815 return;
816 }
817
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000818 OS << "{ ";
819 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
820 if (i) OS << ", ";
Douglas Gregor4c678342009-01-28 21:54:33 +0000821 if (Node->getInit(i))
822 PrintExpr(Node->getInit(i));
823 else
824 OS << "0";
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000825 }
826 OS << " }";
827}
828
Douglas Gregor05c13a32009-01-22 00:58:24 +0000829void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000830 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
831 DEnd = Node->designators_end();
832 D != DEnd; ++D) {
833 if (D->isFieldDesignator()) {
834 if (D->getDotLoc().isInvalid())
835 OS << D->getFieldName()->getName() << ":";
836 else
837 OS << "." << D->getFieldName()->getName();
838 } else {
839 OS << "[";
840 if (D->isArrayDesignator()) {
841 PrintExpr(Node->getArrayIndex(*D));
842 } else {
843 PrintExpr(Node->getArrayRangeStart(*D));
844 OS << " ... ";
845 PrintExpr(Node->getArrayRangeEnd(*D));
846 }
847 OS << "]";
848 }
849 }
850
851 OS << " = ";
852 PrintExpr(Node->getInit());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000853}
854
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000855void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000856 if (Policy.CPlusPlus)
857 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()";
858 else {
859 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")";
860 if (Node->getType()->isRecordType())
861 OS << "{}";
862 else
863 OS << 0;
864 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000865}
866
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000867void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000868 OS << "__builtin_va_arg(";
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000869 PrintExpr(Node->getSubExpr());
870 OS << ", ";
871 OS << Node->getType().getAsString();
872 OS << ")";
873}
874
Reid Spencer5f016e22007-07-11 17:01:13 +0000875// C++
Douglas Gregorb4609802008-11-14 16:09:21 +0000876void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
877 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
878 "",
879#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
880 Spelling,
881#include "clang/Basic/OperatorKinds.def"
882 };
883
884 OverloadedOperatorKind Kind = Node->getOperator();
885 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
886 if (Node->getNumArgs() == 1) {
887 OS << OpStrings[Kind] << ' ';
888 PrintExpr(Node->getArg(0));
889 } else {
890 PrintExpr(Node->getArg(0));
891 OS << ' ' << OpStrings[Kind];
892 }
893 } else if (Kind == OO_Call) {
894 PrintExpr(Node->getArg(0));
895 OS << '(';
896 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
897 if (ArgIdx > 1)
898 OS << ", ";
899 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
900 PrintExpr(Node->getArg(ArgIdx));
901 }
902 OS << ')';
903 } else if (Kind == OO_Subscript) {
904 PrintExpr(Node->getArg(0));
905 OS << '[';
906 PrintExpr(Node->getArg(1));
907 OS << ']';
908 } else if (Node->getNumArgs() == 1) {
909 OS << OpStrings[Kind] << ' ';
910 PrintExpr(Node->getArg(0));
911 } else if (Node->getNumArgs() == 2) {
912 PrintExpr(Node->getArg(0));
913 OS << ' ' << OpStrings[Kind] << ' ';
914 PrintExpr(Node->getArg(1));
915 } else {
916 assert(false && "unknown overloaded operator");
917 }
918}
Reid Spencer5f016e22007-07-11 17:01:13 +0000919
Douglas Gregor88a35142008-12-22 05:46:06 +0000920void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
921 VisitCallExpr(cast<CallExpr>(Node));
922}
923
Douglas Gregor49badde2008-10-27 19:41:14 +0000924void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
925 OS << Node->getCastName() << '<';
926 OS << Node->getTypeAsWritten().getAsString() << ">(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000927 PrintExpr(Node->getSubExpr());
928 OS << ")";
929}
930
Douglas Gregor49badde2008-10-27 19:41:14 +0000931void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
932 VisitCXXNamedCastExpr(Node);
933}
934
935void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
936 VisitCXXNamedCastExpr(Node);
937}
938
939void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
940 VisitCXXNamedCastExpr(Node);
941}
942
943void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
944 VisitCXXNamedCastExpr(Node);
945}
946
Sebastian Redlc42e1182008-11-11 11:37:55 +0000947void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
948 OS << "typeid(";
949 if (Node->isTypeOperand()) {
950 OS << Node->getTypeOperand().getAsString();
951 } else {
952 PrintExpr(Node->getExprOperand());
953 }
954 OS << ")";
955}
956
Reid Spencer5f016e22007-07-11 17:01:13 +0000957void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
958 OS << (Node->getValue() ? "true" : "false");
959}
960
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000961void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
962 OS << "nullptr";
963}
964
Douglas Gregor796da182008-11-04 14:32:21 +0000965void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
966 OS << "this";
967}
968
Chris Lattner50dd2892008-02-26 00:51:44 +0000969void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
970 if (Node->getSubExpr() == 0)
971 OS << "throw";
972 else {
973 OS << "throw ";
974 PrintExpr(Node->getSubExpr());
975 }
976}
977
Chris Lattner04421082008-04-08 04:40:51 +0000978void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
979 // Nothing to print: we picked up the default argument
980}
981
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000982void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
983 OS << Node->getType().getAsString();
984 OS << "(";
985 PrintExpr(Node->getSubExpr());
986 OS << ")";
987}
988
Douglas Gregor506ae412009-01-16 18:33:17 +0000989void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
990 OS << Node->getType().getAsString();
991 OS << "(";
992 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
993 ArgEnd = Node->arg_end();
994 Arg != ArgEnd; ++Arg) {
995 if (Arg != Node->arg_begin())
996 OS << ", ";
997 PrintExpr(*Arg);
998 }
999 OS << ")";
1000}
1001
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001002void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
1003 OS << Node->getType().getAsString() << "()";
1004}
1005
Argyrios Kyrtzidis9e922b12008-09-09 23:47:53 +00001006void
1007StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
1008 PrintRawDecl(E->getVarDecl());
1009}
1010
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001011void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1012 if (E->isGlobalNew())
1013 OS << "::";
1014 OS << "new ";
1015 unsigned NumPlace = E->getNumPlacementArgs();
1016 if (NumPlace > 0) {
1017 OS << "(";
1018 PrintExpr(E->getPlacementArg(0));
1019 for (unsigned i = 1; i < NumPlace; ++i) {
1020 OS << ", ";
1021 PrintExpr(E->getPlacementArg(i));
1022 }
1023 OS << ") ";
1024 }
1025 if (E->isParenTypeId())
1026 OS << "(";
Sebastian Redl6fec6482008-12-02 22:08:59 +00001027 std::string TypeS;
1028 if (Expr *Size = E->getArraySize()) {
1029 llvm::raw_string_ostream s(TypeS);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001030 Size->printPretty(s, Helper, Policy);
Sebastian Redl6fec6482008-12-02 22:08:59 +00001031 s.flush();
1032 TypeS = "[" + TypeS + "]";
1033 }
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001034 E->getAllocatedType().getAsStringInternal(TypeS, Policy);
Sebastian Redl6fec6482008-12-02 22:08:59 +00001035 OS << TypeS;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001036 if (E->isParenTypeId())
1037 OS << ")";
1038
1039 if (E->hasInitializer()) {
1040 OS << "(";
1041 unsigned NumCons = E->getNumConstructorArgs();
1042 if (NumCons > 0) {
1043 PrintExpr(E->getConstructorArg(0));
1044 for (unsigned i = 1; i < NumCons; ++i) {
1045 OS << ", ";
1046 PrintExpr(E->getConstructorArg(i));
1047 }
1048 }
1049 OS << ")";
1050 }
1051}
1052
1053void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1054 if (E->isGlobalDelete())
1055 OS << "::";
1056 OS << "delete ";
1057 if (E->isArrayForm())
1058 OS << "[] ";
1059 PrintExpr(E->getArgument());
1060}
1061
Douglas Gregor17330012009-02-04 15:01:18 +00001062void StmtPrinter::VisitUnresolvedFunctionNameExpr(UnresolvedFunctionNameExpr *E) {
1063 OS << E->getName().getAsString();
Douglas Gregor5c37de72008-12-06 00:22:45 +00001064}
1065
Anders Carlssone349bea2009-04-23 02:32:43 +00001066void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
1067 // Nothing to print.
1068}
1069
Anders Carlsson2d44e8a2009-05-01 22:18:43 +00001070void StmtPrinter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
Anders Carlsson02bbfa32009-04-24 22:47:04 +00001071 // Just forward to the sub expression.
1072 PrintExpr(E->getSubExpr());
1073}
1074
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001075void
1076StmtPrinter::VisitCXXUnresolvedConstructExpr(
1077 CXXUnresolvedConstructExpr *Node) {
1078 OS << Node->getTypeAsWritten().getAsString();
1079 OS << "(";
1080 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
1081 ArgEnd = Node->arg_end();
1082 Arg != ArgEnd; ++Arg) {
1083 if (Arg != Node->arg_begin())
1084 OS << ", ";
1085 PrintExpr(*Arg);
1086 }
1087 OS << ")";
1088}
1089
Douglas Gregor1c0ca592009-05-22 21:13:27 +00001090void StmtPrinter::VisitCXXUnresolvedMemberExpr(CXXUnresolvedMemberExpr *Node) {
1091 PrintExpr(Node->getBase());
1092 OS << (Node->isArrow() ? "->" : ".");
1093 OS << Node->getMember().getAsString();
1094}
1095
Sebastian Redl64b45f72009-01-05 20:52:13 +00001096static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1097 switch (UTT) {
1098 default: assert(false && "Unknown type trait");
1099 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1100 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1101 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1102 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1103 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1104 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1105 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1106 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1107 case UTT_IsAbstract: return "__is_abstract";
1108 case UTT_IsClass: return "__is_class";
1109 case UTT_IsEmpty: return "__is_empty";
1110 case UTT_IsEnum: return "__is_enum";
1111 case UTT_IsPOD: return "__is_pod";
1112 case UTT_IsPolymorphic: return "__is_polymorphic";
1113 case UTT_IsUnion: return "__is_union";
1114 }
1115}
1116
1117void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1118 OS << getTypeTraitName(E->getTrait()) << "("
1119 << E->getQueriedType().getAsString() << ")";
1120}
1121
Anders Carlsson55085182007-08-21 17:43:55 +00001122// Obj-C
1123
1124void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1125 OS << "@";
1126 VisitStringLiteral(Node->getString());
1127}
Reid Spencer5f016e22007-07-11 17:01:13 +00001128
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001129void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001130 OS << "@encode(" << Node->getEncodedType().getAsString() << ')';
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001131}
1132
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001133void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001134 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001135}
1136
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001137void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001138 OS << "@protocol(" << Node->getProtocol()->getNameAsString() << ')';
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001139}
1140
Steve Naroff563477d2007-09-18 23:55:05 +00001141void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1142 OS << "[";
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001143 Expr *receiver = Mess->getReceiver();
1144 if (receiver) PrintExpr(receiver);
1145 else OS << Mess->getClassName()->getName();
Ted Kremenekc29efd82008-05-02 17:32:38 +00001146 OS << ' ';
Ted Kremenek97b7f262008-04-16 04:30:16 +00001147 Selector selector = Mess->getSelector();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001148 if (selector.isUnarySelector()) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001149 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001150 } else {
1151 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001152 if (i < selector.getNumArgs()) {
1153 if (i > 0) OS << ' ';
1154 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner39f34e92008-11-24 04:00:27 +00001155 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremenekc29efd82008-05-02 17:32:38 +00001156 else
1157 OS << ":";
1158 }
1159 else OS << ", "; // Handle variadic methods.
1160
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001161 PrintExpr(Mess->getArg(i));
1162 }
Steve Naroff563477d2007-09-18 23:55:05 +00001163 }
1164 OS << "]";
1165}
1166
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001167void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) {
1168 OS << "super";
1169}
1170
Steve Naroff4eb206b2008-09-03 18:15:37 +00001171void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001172 BlockDecl *BD = Node->getBlockDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001173 OS << "^";
1174
1175 const FunctionType *AFT = Node->getFunctionType();
1176
Douglas Gregor72564e72009-02-26 23:50:07 +00001177 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001178 OS << "()";
Douglas Gregor72564e72009-02-26 23:50:07 +00001179 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001180 OS << '(';
1181 std::string ParamStr;
Steve Naroff56ee6892008-10-08 17:01:13 +00001182 for (BlockDecl::param_iterator AI = BD->param_begin(),
1183 E = BD->param_end(); AI != E; ++AI) {
1184 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner39f34e92008-11-24 04:00:27 +00001185 ParamStr = (*AI)->getNameAsString();
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001186 (*AI)->getType().getAsStringInternal(ParamStr, Policy);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001187 OS << ParamStr;
1188 }
1189
Douglas Gregor72564e72009-02-26 23:50:07 +00001190 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001191 if (FT->isVariadic()) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001192 if (!BD->param_empty()) OS << ", ";
Steve Naroff4eb206b2008-09-03 18:15:37 +00001193 OS << "...";
1194 }
1195 OS << ')';
1196 }
1197}
1198
Steve Naroff4eb206b2008-09-03 18:15:37 +00001199void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001200 OS << Node->getDecl()->getNameAsString();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001201}
Reid Spencer5f016e22007-07-11 17:01:13 +00001202//===----------------------------------------------------------------------===//
1203// Stmt method implementations
1204//===----------------------------------------------------------------------===//
1205
Chris Lattner6000dac2007-08-08 22:51:59 +00001206void Stmt::dumpPretty() const {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001207 printPretty(llvm::errs(), 0, PrintingPolicy());
Reid Spencer5f016e22007-07-11 17:01:13 +00001208}
1209
Mike Stump071e4da2009-02-10 20:16:46 +00001210void Stmt::printPretty(llvm::raw_ostream &OS, PrinterHelper* Helper,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001211 const PrintingPolicy &Policy,
1212 unsigned Indentation) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001213 if (this == 0) {
1214 OS << "<NULL>";
1215 return;
1216 }
1217
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001218 if (Policy.Dump) {
1219 dump();
1220 return;
1221 }
1222
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001223 StmtPrinter P(OS, Helper, Policy, Indentation);
Chris Lattnerc5598cb2007-08-21 04:04:25 +00001224 P.Visit(const_cast<Stmt*>(this));
Reid Spencer5f016e22007-07-11 17:01:13 +00001225}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001226
1227//===----------------------------------------------------------------------===//
1228// PrinterHelper
1229//===----------------------------------------------------------------------===//
1230
1231// Implement virtual destructor.
Gabor Greif84675832007-09-11 15:32:40 +00001232PrinterHelper::~PrinterHelper() {}