blob: 9b8dfce00e7da307fe4a40473d3dbff8af9b0d7b [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,
Chris Lattnere4f21422009-06-30 01:26:17 +000038 const PrintingPolicy &Policy,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000039 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) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000117 D->print(OS, 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
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000126 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
Ted Kremenekecd64c52008-10-06 18:39:36 +0000127}
Reid Spencer5f016e22007-07-11 17:01:13 +0000128
129void StmtPrinter::VisitNullStmt(NullStmt *Node) {
130 Indent() << ";\n";
131}
132
133void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Eli Friedmandb23b152009-05-30 00:19:54 +0000134 Indent();
135 PrintRawDeclStmt(Node);
136 OS << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000137}
138
139void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
140 Indent();
141 PrintRawCompoundStmt(Node);
142 OS << "\n";
143}
144
145void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
146 Indent(-1) << "case ";
147 PrintExpr(Node->getLHS());
148 if (Node->getRHS()) {
149 OS << " ... ";
150 PrintExpr(Node->getRHS());
151 }
152 OS << ":\n";
153
154 PrintStmt(Node->getSubStmt(), 0);
155}
156
157void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
158 Indent(-1) << "default:\n";
159 PrintStmt(Node->getSubStmt(), 0);
160}
161
162void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
163 Indent(-1) << Node->getName() << ":\n";
164 PrintStmt(Node->getSubStmt(), 0);
165}
166
167void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000168 OS << "if (";
Reid Spencer5f016e22007-07-11 17:01:13 +0000169 PrintExpr(If->getCond());
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000170 OS << ')';
Reid Spencer5f016e22007-07-11 17:01:13 +0000171
172 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
173 OS << ' ';
174 PrintRawCompoundStmt(CS);
175 OS << (If->getElse() ? ' ' : '\n');
176 } else {
177 OS << '\n';
178 PrintStmt(If->getThen());
179 if (If->getElse()) Indent();
180 }
181
182 if (Stmt *Else = If->getElse()) {
183 OS << "else";
184
185 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
186 OS << ' ';
187 PrintRawCompoundStmt(CS);
188 OS << '\n';
189 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
190 OS << ' ';
191 PrintRawIfStmt(ElseIf);
192 } else {
193 OS << '\n';
194 PrintStmt(If->getElse());
195 }
196 }
197}
198
199void StmtPrinter::VisitIfStmt(IfStmt *If) {
200 Indent();
201 PrintRawIfStmt(If);
202}
203
204void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
205 Indent() << "switch (";
206 PrintExpr(Node->getCond());
207 OS << ")";
208
209 // Pretty print compoundstmt bodies (very common).
210 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
211 OS << " ";
212 PrintRawCompoundStmt(CS);
213 OS << "\n";
214 } else {
215 OS << "\n";
216 PrintStmt(Node->getBody());
217 }
218}
219
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000220void StmtPrinter::VisitSwitchCase(SwitchCase*) {
221 assert(0 && "SwitchCase is an abstract class");
222}
223
Reid Spencer5f016e22007-07-11 17:01:13 +0000224void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
225 Indent() << "while (";
226 PrintExpr(Node->getCond());
227 OS << ")\n";
228 PrintStmt(Node->getBody());
229}
230
231void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner8bdcc472007-09-15 21:49:37 +0000232 Indent() << "do ";
233 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
234 PrintRawCompoundStmt(CS);
235 OS << " ";
236 } else {
237 OS << "\n";
238 PrintStmt(Node->getBody());
239 Indent();
240 }
241
Eli Friedmanb3e22962009-05-17 01:05:34 +0000242 OS << "while (";
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 PrintExpr(Node->getCond());
Eli Friedmanb3e22962009-05-17 01:05:34 +0000244 OS << ");\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000245}
246
247void StmtPrinter::VisitForStmt(ForStmt *Node) {
248 Indent() << "for (";
249 if (Node->getInit()) {
250 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000251 PrintRawDeclStmt(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 else
253 PrintExpr(cast<Expr>(Node->getInit()));
254 }
Chris Lattner8bdcc472007-09-15 21:49:37 +0000255 OS << ";";
256 if (Node->getCond()) {
257 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000258 PrintExpr(Node->getCond());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000259 }
260 OS << ";";
261 if (Node->getInc()) {
262 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000263 PrintExpr(Node->getInc());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000264 }
265 OS << ") ";
266
267 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
268 PrintRawCompoundStmt(CS);
269 OS << "\n";
270 } else {
271 OS << "\n";
272 PrintStmt(Node->getBody());
273 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000274}
275
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000276void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000277 Indent() << "for (";
278 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000279 PrintRawDeclStmt(DS);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000280 else
281 PrintExpr(cast<Expr>(Node->getElement()));
282 OS << " in ";
283 PrintExpr(Node->getCollection());
284 OS << ") ";
285
286 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
287 PrintRawCompoundStmt(CS);
288 OS << "\n";
289 } else {
290 OS << "\n";
291 PrintStmt(Node->getBody());
292 }
293}
294
Reid Spencer5f016e22007-07-11 17:01:13 +0000295void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
296 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
297}
298
299void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
300 Indent() << "goto *";
301 PrintExpr(Node->getTarget());
302 OS << ";\n";
303}
304
305void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
306 Indent() << "continue;\n";
307}
308
309void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
310 Indent() << "break;\n";
311}
312
313
314void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
315 Indent() << "return";
316 if (Node->getRetValue()) {
317 OS << " ";
318 PrintExpr(Node->getRetValue());
319 }
320 OS << ";\n";
321}
322
Chris Lattnerfe795952007-10-29 04:04:16 +0000323
324void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson39c47b52007-11-23 23:12:25 +0000325 Indent() << "asm ";
326
327 if (Node->isVolatile())
328 OS << "volatile ";
329
330 OS << "(";
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000331 VisitStringLiteral(Node->getAsmString());
Anders Carlssonb235fc22007-11-22 01:36:19 +0000332
333 // Outputs
334 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
335 Node->getNumClobbers() != 0)
336 OS << " : ";
337
338 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
339 if (i != 0)
340 OS << ", ";
341
342 if (!Node->getOutputName(i).empty()) {
343 OS << '[';
344 OS << Node->getOutputName(i);
345 OS << "] ";
346 }
347
Chris Lattnerb3277932009-03-10 04:59:06 +0000348 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000349 OS << " ";
350 Visit(Node->getOutputExpr(i));
351 }
352
353 // Inputs
354 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
355 OS << " : ";
356
357 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
358 if (i != 0)
359 OS << ", ";
360
361 if (!Node->getInputName(i).empty()) {
362 OS << '[';
363 OS << Node->getInputName(i);
364 OS << "] ";
365 }
366
Chris Lattnerb3277932009-03-10 04:59:06 +0000367 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000368 OS << " ";
369 Visit(Node->getInputExpr(i));
370 }
371
372 // Clobbers
373 if (Node->getNumClobbers() != 0)
374 OS << " : ";
375
376 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
377 if (i != 0)
378 OS << ", ";
379
380 VisitStringLiteral(Node->getClobber(i));
381 }
382
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000383 OS << ");\n";
Chris Lattnerfe795952007-10-29 04:04:16 +0000384}
385
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000386void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000387 Indent() << "@try";
388 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
389 PrintRawCompoundStmt(TS);
390 OS << "\n";
391 }
392
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000393 for (ObjCAtCatchStmt *catchStmt =
394 static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts());
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000395 catchStmt;
396 catchStmt =
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000397 static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000398 Indent() << "@catch(";
Steve Naroff7ba138a2009-03-03 19:52:17 +0000399 if (catchStmt->getCatchParamDecl()) {
400 if (Decl *DS = catchStmt->getCatchParamDecl())
401 PrintRawDecl(DS);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000402 }
403 OS << ")";
404 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody()))
405 {
406 PrintRawCompoundStmt(CS);
407 OS << "\n";
408 }
409 }
410
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000411 if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>(
Fariborz Jahanian1e7eab42007-11-07 00:46:42 +0000412 Node->getFinallyStmt())) {
413 Indent() << "@finally";
414 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000415 OS << "\n";
416 }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000417}
418
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000419void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000420}
421
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000422void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000423 Indent() << "@catch (...) { /* todo */ } \n";
424}
425
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000426void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000427 Indent() << "@throw";
428 if (Node->getThrowExpr()) {
429 OS << " ";
430 PrintExpr(Node->getThrowExpr());
431 }
432 OS << ";\n";
433}
434
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000435void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000436 Indent() << "@synchronized (";
437 PrintExpr(Node->getSynchExpr());
438 OS << ")";
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000439 PrintRawCompoundStmt(Node->getSynchBody());
440 OS << "\n";
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000441}
442
Sebastian Redl8351da02008-12-22 21:35:02 +0000443void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
444 OS << "catch (";
Sebastian Redl4b07b292008-12-22 19:15:10 +0000445 if (Decl *ExDecl = Node->getExceptionDecl())
446 PrintRawDecl(ExDecl);
447 else
448 OS << "...";
449 OS << ") ";
450 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl8351da02008-12-22 21:35:02 +0000451}
452
453void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
454 Indent();
455 PrintRawCXXCatchStmt(Node);
456 OS << "\n";
457}
458
459void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
460 Indent() << "try ";
461 PrintRawCompoundStmt(Node->getTryBlock());
462 for(unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
463 OS << " ";
464 PrintRawCXXCatchStmt(Node->getHandler(i));
465 }
Sebastian Redl4b07b292008-12-22 19:15:10 +0000466 OS << "\n";
467}
468
Reid Spencer5f016e22007-07-11 17:01:13 +0000469//===----------------------------------------------------------------------===//
470// Expr printing methods.
471//===----------------------------------------------------------------------===//
472
473void StmtPrinter::VisitExpr(Expr *Node) {
474 OS << "<<unknown expr type>>";
475}
476
477void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +0000478 OS << Node->getDecl()->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000479}
480
Douglas Gregorbad35182009-03-19 03:51:16 +0000481void StmtPrinter::VisitQualifiedDeclRefExpr(QualifiedDeclRefExpr *Node) {
Douglas Gregor1a49af92009-01-06 05:10:23 +0000482 NamedDecl *D = Node->getDecl();
483
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000484 Node->getQualifier()->print(OS, Policy);
Douglas Gregor1a49af92009-01-06 05:10:23 +0000485 OS << D->getNameAsString();
486}
487
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000488void StmtPrinter::VisitUnresolvedDeclRefExpr(UnresolvedDeclRefExpr *Node) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000489 Node->getQualifier()->print(OS, Policy);
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000490 OS << Node->getDeclName().getAsString();
491}
492
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000493void StmtPrinter::VisitTemplateIdRefExpr(TemplateIdRefExpr *Node) {
494 if (Node->getQualifier())
495 Node->getQualifier()->print(OS, Policy);
496 Node->getTemplateName().print(OS, Policy, true);
497 OS << '<';
498 OS << TemplateSpecializationType::PrintTemplateArgumentList(
499 Node->getTemplateArgs(),
500 Node->getNumTemplateArgs(),
501 Policy);
502 OS << '>';
503}
504
Steve Naroff7779db42007-11-12 14:29:37 +0000505void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000506 if (Node->getBase()) {
507 PrintExpr(Node->getBase());
508 OS << (Node->isArrow() ? "->" : ".");
509 }
Chris Lattner39f34e92008-11-24 04:00:27 +0000510 OS << Node->getDecl()->getNameAsString();
Steve Naroff7779db42007-11-12 14:29:37 +0000511}
512
Steve Naroffae784072008-05-30 00:40:33 +0000513void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
514 if (Node->getBase()) {
515 PrintExpr(Node->getBase());
516 OS << ".";
517 }
Steve Naroffc77a6362008-12-04 16:24:46 +0000518 OS << Node->getProperty()->getNameAsCString();
Steve Naroffae784072008-05-30 00:40:33 +0000519}
520
Fariborz Jahanian154440e2009-08-18 20:50:23 +0000521void StmtPrinter::VisitObjCImplctSetterGetterRefExpr(
522 ObjCImplctSetterGetterRefExpr *Node) {
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000523 if (Node->getBase()) {
524 PrintExpr(Node->getBase());
525 OS << ".";
526 }
Fariborz Jahanian154440e2009-08-18 20:50:23 +0000527 if (Node->getGetterMethod())
528 OS << Node->getGetterMethod()->getNameAsString();
529
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000530}
531
Chris Lattnerd9f69102008-08-10 01:53:14 +0000532void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson22742662007-07-21 05:21:51 +0000533 switch (Node->getIdentType()) {
534 default:
535 assert(0 && "unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000536 case PredefinedExpr::Func:
Anders Carlsson22742662007-07-21 05:21:51 +0000537 OS << "__func__";
538 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000539 case PredefinedExpr::Function:
Anders Carlsson22742662007-07-21 05:21:51 +0000540 OS << "__FUNCTION__";
541 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000542 case PredefinedExpr::PrettyFunction:
Anders Carlsson22742662007-07-21 05:21:51 +0000543 OS << "__PRETTY_FUNCTION__";
544 break;
545 }
546}
547
Reid Spencer5f016e22007-07-11 17:01:13 +0000548void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000549 unsigned value = Node->getValue();
Chris Lattnerc250aae2008-06-07 22:35:38 +0000550 if (Node->isWide())
551 OS << "L";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000552 switch (value) {
553 case '\\':
554 OS << "'\\\\'";
555 break;
556 case '\'':
557 OS << "'\\''";
558 break;
559 case '\a':
560 // TODO: K&R: the meaning of '\\a' is different in traditional C
561 OS << "'\\a'";
562 break;
563 case '\b':
564 OS << "'\\b'";
565 break;
566 // Nonstandard escape sequence.
567 /*case '\e':
568 OS << "'\\e'";
569 break;*/
570 case '\f':
571 OS << "'\\f'";
572 break;
573 case '\n':
574 OS << "'\\n'";
575 break;
576 case '\r':
577 OS << "'\\r'";
578 break;
579 case '\t':
580 OS << "'\\t'";
581 break;
582 case '\v':
583 OS << "'\\v'";
584 break;
585 default:
Ted Kremenek471733d2008-02-23 00:52:04 +0000586 if (value < 256 && isprint(value)) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000587 OS << "'" << (char)value << "'";
588 } else if (value < 256) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000589 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000590 } else {
591 // FIXME what to really do here?
592 OS << value;
593 }
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000594 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000595}
596
597void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
598 bool isSigned = Node->getType()->isSignedIntegerType();
599 OS << Node->getValue().toString(10, isSigned);
600
601 // Emit suffixes. Integer literals are always a builtin integer type.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000602 switch (Node->getType()->getAsBuiltinType()->getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000603 default: assert(0 && "Unexpected type for integer literal!");
604 case BuiltinType::Int: break; // no suffix.
605 case BuiltinType::UInt: OS << 'U'; break;
606 case BuiltinType::Long: OS << 'L'; break;
607 case BuiltinType::ULong: OS << "UL"; break;
608 case BuiltinType::LongLong: OS << "LL"; break;
609 case BuiltinType::ULongLong: OS << "ULL"; break;
610 }
611}
612void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner86e499d2007-08-01 00:23:58 +0000613 // FIXME: print value more precisely.
Chris Lattnerda8249e2008-06-07 22:13:43 +0000614 OS << Node->getValueAsApproximateDouble();
Reid Spencer5f016e22007-07-11 17:01:13 +0000615}
Chris Lattner5d661452007-08-26 03:42:43 +0000616
617void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
618 PrintExpr(Node->getSubExpr());
619 OS << "i";
620}
621
Reid Spencer5f016e22007-07-11 17:01:13 +0000622void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
623 if (Str->isWide()) OS << 'L';
624 OS << '"';
Anders Carlssonee98ac52007-10-15 02:50:23 +0000625
Reid Spencer5f016e22007-07-11 17:01:13 +0000626 // FIXME: this doesn't print wstrings right.
627 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattner9a81c872009-01-16 19:25:18 +0000628 unsigned char Char = Str->getStrData()[i];
629
630 switch (Char) {
631 default:
632 if (isprint(Char))
633 OS << (char)Char;
634 else // Output anything hard as an octal escape.
635 OS << '\\'
636 << (char)('0'+ ((Char >> 6) & 7))
637 << (char)('0'+ ((Char >> 3) & 7))
638 << (char)('0'+ ((Char >> 0) & 7));
639 break;
640 // Handle some common non-printable cases to make dumps prettier.
Reid Spencer5f016e22007-07-11 17:01:13 +0000641 case '\\': OS << "\\\\"; break;
642 case '"': OS << "\\\""; break;
643 case '\n': OS << "\\n"; break;
644 case '\t': OS << "\\t"; break;
645 case '\a': OS << "\\a"; break;
646 case '\b': OS << "\\b"; break;
647 }
648 }
649 OS << '"';
650}
651void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
652 OS << "(";
653 PrintExpr(Node->getSubExpr());
654 OS << ")";
655}
656void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +0000657 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000658 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner296bf192007-08-23 21:46:40 +0000659
Eli Friedman7df71ac2009-06-14 22:39:26 +0000660 // Print a space if this is an "identifier operator" like __real, or if
661 // it might be concatenated incorrectly like '+'.
Chris Lattner296bf192007-08-23 21:46:40 +0000662 switch (Node->getOpcode()) {
663 default: break;
Chris Lattner296bf192007-08-23 21:46:40 +0000664 case UnaryOperator::Real:
665 case UnaryOperator::Imag:
666 case UnaryOperator::Extension:
667 OS << ' ';
668 break;
Eli Friedman7df71ac2009-06-14 22:39:26 +0000669 case UnaryOperator::Plus:
670 case UnaryOperator::Minus:
671 if (isa<UnaryOperator>(Node->getSubExpr()))
672 OS << ' ';
673 break;
Chris Lattner296bf192007-08-23 21:46:40 +0000674 }
675 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000676 PrintExpr(Node->getSubExpr());
677
678 if (Node->isPostfix())
679 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +0000680}
Chris Lattner704fe352007-08-30 17:59:59 +0000681
682bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
Eli Friedman35183ac2009-02-27 06:44:11 +0000683 if (isa<UnaryOperator>(E)) {
Chris Lattner704fe352007-08-30 17:59:59 +0000684 // Base case, print the type and comma.
685 OS << E->getType().getAsString() << ", ";
686 return true;
687 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
688 PrintOffsetOfDesignator(ASE->getLHS());
689 OS << "[";
690 PrintExpr(ASE->getRHS());
691 OS << "]";
692 return false;
693 } else {
694 MemberExpr *ME = cast<MemberExpr>(E);
695 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
Chris Lattner39f34e92008-11-24 04:00:27 +0000696 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getNameAsString();
Chris Lattner704fe352007-08-30 17:59:59 +0000697 return false;
698 }
699}
700
701void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
702 OS << "__builtin_offsetof(";
703 PrintOffsetOfDesignator(Node->getSubExpr());
704 OS << ")";
705}
706
Sebastian Redl05189992008-11-11 17:56:53 +0000707void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
708 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
709 if (Node->isArgumentType())
710 OS << "(" << Node->getArgumentType().getAsString() << ")";
711 else {
712 OS << " ";
713 PrintExpr(Node->getArgumentExpr());
714 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000715}
716void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +0000717 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000718 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +0000719 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000720 OS << "]";
721}
722
723void StmtPrinter::VisitCallExpr(CallExpr *Call) {
724 PrintExpr(Call->getCallee());
725 OS << "(";
726 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattner04421082008-04-08 04:40:51 +0000727 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
728 // Don't print any defaulted arguments
729 break;
730 }
731
Reid Spencer5f016e22007-07-11 17:01:13 +0000732 if (i) OS << ", ";
733 PrintExpr(Call->getArg(i));
734 }
735 OS << ")";
736}
737void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregorb3eef682009-01-08 22:45:41 +0000738 // FIXME: Suppress printing implicit bases (like "this")
739 PrintExpr(Node->getBase());
740 OS << (Node->isArrow() ? "->" : ".");
741 // FIXME: Suppress printing references to unnamed objects
742 // representing anonymous unions/structs
Douglas Gregor86f19402008-12-20 23:49:58 +0000743 OS << Node->getMemberDecl()->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000744}
Steve Narofff242b1b2009-07-24 17:54:45 +0000745void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
746 PrintExpr(Node->getBase());
747 OS << (Node->isArrow() ? "->isa" : ".isa");
748}
749
Nate Begeman213541a2008-04-18 23:10:10 +0000750void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +0000751 PrintExpr(Node->getBase());
752 OS << ".";
753 OS << Node->getAccessor().getName();
754}
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000755void StmtPrinter::VisitCastExpr(CastExpr *) {
756 assert(0 && "CastExpr is an abstract class");
757}
Douglas Gregor49badde2008-10-27 19:41:14 +0000758void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *) {
759 assert(0 && "ExplicitCastExpr is an abstract class");
760}
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000761void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000762 OS << "(" << Node->getType().getAsString() << ")";
Reid Spencer5f016e22007-07-11 17:01:13 +0000763 PrintExpr(Node->getSubExpr());
764}
Steve Naroffaff1edd2007-07-19 21:32:11 +0000765void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
766 OS << "(" << Node->getType().getAsString() << ")";
767 PrintExpr(Node->getInitializer());
768}
Steve Naroff49b45262007-07-13 16:58:59 +0000769void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroff90045e82007-07-13 23:32:42 +0000770 // No need to print anything, simply forward to the sub expression.
771 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +0000772}
Reid Spencer5f016e22007-07-11 17:01:13 +0000773void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
774 PrintExpr(Node->getLHS());
775 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
776 PrintExpr(Node->getRHS());
777}
Chris Lattnereb14fe82007-08-25 02:00:02 +0000778void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
779 PrintExpr(Node->getLHS());
780 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
781 PrintExpr(Node->getRHS());
782}
Reid Spencer5f016e22007-07-11 17:01:13 +0000783void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
784 PrintExpr(Node->getCond());
Ted Kremenek8e911c42007-11-26 18:27:54 +0000785
786 if (Node->getLHS()) {
787 OS << " ? ";
788 PrintExpr(Node->getLHS());
789 OS << " : ";
790 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000791 else { // Handle GCC extension where LHS can be NULL.
Ted Kremenek8e911c42007-11-26 18:27:54 +0000792 OS << " ?: ";
793 }
794
Reid Spencer5f016e22007-07-11 17:01:13 +0000795 PrintExpr(Node->getRHS());
796}
797
798// GNU extensions.
799
Chris Lattner6481a572007-08-03 17:31:20 +0000800void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000801 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000802}
803
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000804void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
805 OS << "(";
806 PrintRawCompoundStmt(E->getSubStmt());
807 OS << ")";
808}
809
Steve Naroffd34e9152007-08-01 22:05:33 +0000810void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
811 OS << "__builtin_types_compatible_p(";
812 OS << Node->getArgType1().getAsString() << ",";
813 OS << Node->getArgType2().getAsString() << ")";
814}
815
Steve Naroffd04fdd52007-08-03 21:21:27 +0000816void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
817 OS << "__builtin_choose_expr(";
818 PrintExpr(Node->getCond());
Chris Lattner94f05e32007-08-04 00:20:15 +0000819 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000820 PrintExpr(Node->getLHS());
Chris Lattner94f05e32007-08-04 00:20:15 +0000821 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000822 PrintExpr(Node->getRHS());
823 OS << ")";
824}
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000825
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000826void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
827 OS << "__null";
828}
829
Eli Friedmand38617c2008-05-14 19:38:39 +0000830void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
831 OS << "__builtin_shufflevector(";
832 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
833 if (i) OS << ", ";
834 PrintExpr(Node->getExpr(i));
835 }
836 OS << ")";
837}
838
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000839void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000840 if (Node->getSyntacticForm()) {
841 Visit(Node->getSyntacticForm());
842 return;
843 }
844
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000845 OS << "{ ";
846 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
847 if (i) OS << ", ";
Douglas Gregor4c678342009-01-28 21:54:33 +0000848 if (Node->getInit(i))
849 PrintExpr(Node->getInit(i));
850 else
851 OS << "0";
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000852 }
853 OS << " }";
854}
855
Nate Begeman2ef13e52009-08-10 23:49:36 +0000856void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
857 OS << "( ";
858 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
859 if (i) OS << ", ";
860 PrintExpr(Node->getExpr(i));
861 }
862 OS << " )";
863}
864
Douglas Gregor05c13a32009-01-22 00:58:24 +0000865void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000866 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
867 DEnd = Node->designators_end();
868 D != DEnd; ++D) {
869 if (D->isFieldDesignator()) {
870 if (D->getDotLoc().isInvalid())
871 OS << D->getFieldName()->getName() << ":";
872 else
873 OS << "." << D->getFieldName()->getName();
874 } else {
875 OS << "[";
876 if (D->isArrayDesignator()) {
877 PrintExpr(Node->getArrayIndex(*D));
878 } else {
879 PrintExpr(Node->getArrayRangeStart(*D));
880 OS << " ... ";
881 PrintExpr(Node->getArrayRangeEnd(*D));
882 }
883 OS << "]";
884 }
885 }
886
887 OS << " = ";
888 PrintExpr(Node->getInit());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000889}
890
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000891void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Chris Lattnere4f21422009-06-30 01:26:17 +0000892 if (Policy.LangOpts.CPlusPlus)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000893 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()";
894 else {
895 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")";
896 if (Node->getType()->isRecordType())
897 OS << "{}";
898 else
899 OS << 0;
900 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000901}
902
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000903void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000904 OS << "__builtin_va_arg(";
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000905 PrintExpr(Node->getSubExpr());
906 OS << ", ";
907 OS << Node->getType().getAsString();
908 OS << ")";
909}
910
Reid Spencer5f016e22007-07-11 17:01:13 +0000911// C++
Douglas Gregorb4609802008-11-14 16:09:21 +0000912void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
913 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
914 "",
915#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
916 Spelling,
917#include "clang/Basic/OperatorKinds.def"
918 };
919
920 OverloadedOperatorKind Kind = Node->getOperator();
921 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
922 if (Node->getNumArgs() == 1) {
923 OS << OpStrings[Kind] << ' ';
924 PrintExpr(Node->getArg(0));
925 } else {
926 PrintExpr(Node->getArg(0));
927 OS << ' ' << OpStrings[Kind];
928 }
929 } else if (Kind == OO_Call) {
930 PrintExpr(Node->getArg(0));
931 OS << '(';
932 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
933 if (ArgIdx > 1)
934 OS << ", ";
935 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
936 PrintExpr(Node->getArg(ArgIdx));
937 }
938 OS << ')';
939 } else if (Kind == OO_Subscript) {
940 PrintExpr(Node->getArg(0));
941 OS << '[';
942 PrintExpr(Node->getArg(1));
943 OS << ']';
944 } else if (Node->getNumArgs() == 1) {
945 OS << OpStrings[Kind] << ' ';
946 PrintExpr(Node->getArg(0));
947 } else if (Node->getNumArgs() == 2) {
948 PrintExpr(Node->getArg(0));
949 OS << ' ' << OpStrings[Kind] << ' ';
950 PrintExpr(Node->getArg(1));
951 } else {
952 assert(false && "unknown overloaded operator");
953 }
954}
Reid Spencer5f016e22007-07-11 17:01:13 +0000955
Douglas Gregor88a35142008-12-22 05:46:06 +0000956void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
957 VisitCallExpr(cast<CallExpr>(Node));
958}
959
Douglas Gregor49badde2008-10-27 19:41:14 +0000960void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
961 OS << Node->getCastName() << '<';
962 OS << Node->getTypeAsWritten().getAsString() << ">(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000963 PrintExpr(Node->getSubExpr());
964 OS << ")";
965}
966
Douglas Gregor49badde2008-10-27 19:41:14 +0000967void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
968 VisitCXXNamedCastExpr(Node);
969}
970
971void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
972 VisitCXXNamedCastExpr(Node);
973}
974
975void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
976 VisitCXXNamedCastExpr(Node);
977}
978
979void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
980 VisitCXXNamedCastExpr(Node);
981}
982
Sebastian Redlc42e1182008-11-11 11:37:55 +0000983void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
984 OS << "typeid(";
985 if (Node->isTypeOperand()) {
986 OS << Node->getTypeOperand().getAsString();
987 } else {
988 PrintExpr(Node->getExprOperand());
989 }
990 OS << ")";
991}
992
Reid Spencer5f016e22007-07-11 17:01:13 +0000993void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
994 OS << (Node->getValue() ? "true" : "false");
995}
996
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000997void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
998 OS << "nullptr";
999}
1000
Douglas Gregor796da182008-11-04 14:32:21 +00001001void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1002 OS << "this";
1003}
1004
Chris Lattner50dd2892008-02-26 00:51:44 +00001005void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1006 if (Node->getSubExpr() == 0)
1007 OS << "throw";
1008 else {
1009 OS << "throw ";
1010 PrintExpr(Node->getSubExpr());
1011 }
1012}
1013
Chris Lattner04421082008-04-08 04:40:51 +00001014void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1015 // Nothing to print: we picked up the default argument
1016}
1017
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001018void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1019 OS << Node->getType().getAsString();
1020 OS << "(";
1021 PrintExpr(Node->getSubExpr());
1022 OS << ")";
1023}
1024
Anders Carlssonfceb0a82009-05-30 20:03:25 +00001025void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1026 PrintExpr(Node->getSubExpr());
1027}
1028
Douglas Gregor506ae412009-01-16 18:33:17 +00001029void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1030 OS << Node->getType().getAsString();
1031 OS << "(";
1032 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1033 ArgEnd = Node->arg_end();
1034 Arg != ArgEnd; ++Arg) {
1035 if (Arg != Node->arg_begin())
1036 OS << ", ";
1037 PrintExpr(*Arg);
1038 }
1039 OS << ")";
1040}
1041
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001042void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
1043 OS << Node->getType().getAsString() << "()";
1044}
1045
Argyrios Kyrtzidis9e922b12008-09-09 23:47:53 +00001046void
1047StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
1048 PrintRawDecl(E->getVarDecl());
1049}
1050
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001051void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1052 if (E->isGlobalNew())
1053 OS << "::";
1054 OS << "new ";
1055 unsigned NumPlace = E->getNumPlacementArgs();
1056 if (NumPlace > 0) {
1057 OS << "(";
1058 PrintExpr(E->getPlacementArg(0));
1059 for (unsigned i = 1; i < NumPlace; ++i) {
1060 OS << ", ";
1061 PrintExpr(E->getPlacementArg(i));
1062 }
1063 OS << ") ";
1064 }
1065 if (E->isParenTypeId())
1066 OS << "(";
Sebastian Redl6fec6482008-12-02 22:08:59 +00001067 std::string TypeS;
1068 if (Expr *Size = E->getArraySize()) {
1069 llvm::raw_string_ostream s(TypeS);
Eli Friedman6e1a3452009-05-30 05:32:46 +00001070 Size->printPretty(s, Context, Helper, Policy);
Sebastian Redl6fec6482008-12-02 22:08:59 +00001071 s.flush();
1072 TypeS = "[" + TypeS + "]";
1073 }
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001074 E->getAllocatedType().getAsStringInternal(TypeS, Policy);
Sebastian Redl6fec6482008-12-02 22:08:59 +00001075 OS << TypeS;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001076 if (E->isParenTypeId())
1077 OS << ")";
1078
1079 if (E->hasInitializer()) {
1080 OS << "(";
1081 unsigned NumCons = E->getNumConstructorArgs();
1082 if (NumCons > 0) {
1083 PrintExpr(E->getConstructorArg(0));
1084 for (unsigned i = 1; i < NumCons; ++i) {
1085 OS << ", ";
1086 PrintExpr(E->getConstructorArg(i));
1087 }
1088 }
1089 OS << ")";
1090 }
1091}
1092
1093void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1094 if (E->isGlobalDelete())
1095 OS << "::";
1096 OS << "delete ";
1097 if (E->isArrayForm())
1098 OS << "[] ";
1099 PrintExpr(E->getArgument());
1100}
1101
Douglas Gregor17330012009-02-04 15:01:18 +00001102void StmtPrinter::VisitUnresolvedFunctionNameExpr(UnresolvedFunctionNameExpr *E) {
1103 OS << E->getName().getAsString();
Douglas Gregor5c37de72008-12-06 00:22:45 +00001104}
1105
Anders Carlssone349bea2009-04-23 02:32:43 +00001106void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
1107 // Nothing to print.
1108}
1109
Anders Carlsson2d44e8a2009-05-01 22:18:43 +00001110void StmtPrinter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
Anders Carlsson02bbfa32009-04-24 22:47:04 +00001111 // Just forward to the sub expression.
1112 PrintExpr(E->getSubExpr());
1113}
1114
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001115void
1116StmtPrinter::VisitCXXUnresolvedConstructExpr(
1117 CXXUnresolvedConstructExpr *Node) {
1118 OS << Node->getTypeAsWritten().getAsString();
1119 OS << "(";
1120 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
1121 ArgEnd = Node->arg_end();
1122 Arg != ArgEnd; ++Arg) {
1123 if (Arg != Node->arg_begin())
1124 OS << ", ";
1125 PrintExpr(*Arg);
1126 }
1127 OS << ")";
1128}
1129
Douglas Gregor1c0ca592009-05-22 21:13:27 +00001130void StmtPrinter::VisitCXXUnresolvedMemberExpr(CXXUnresolvedMemberExpr *Node) {
1131 PrintExpr(Node->getBase());
1132 OS << (Node->isArrow() ? "->" : ".");
1133 OS << Node->getMember().getAsString();
1134}
1135
Sebastian Redl64b45f72009-01-05 20:52:13 +00001136static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1137 switch (UTT) {
1138 default: assert(false && "Unknown type trait");
1139 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1140 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1141 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1142 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1143 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1144 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1145 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1146 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1147 case UTT_IsAbstract: return "__is_abstract";
1148 case UTT_IsClass: return "__is_class";
1149 case UTT_IsEmpty: return "__is_empty";
1150 case UTT_IsEnum: return "__is_enum";
1151 case UTT_IsPOD: return "__is_pod";
1152 case UTT_IsPolymorphic: return "__is_polymorphic";
1153 case UTT_IsUnion: return "__is_union";
1154 }
1155}
1156
1157void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1158 OS << getTypeTraitName(E->getTrait()) << "("
1159 << E->getQueriedType().getAsString() << ")";
1160}
1161
Anders Carlsson55085182007-08-21 17:43:55 +00001162// Obj-C
1163
1164void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1165 OS << "@";
1166 VisitStringLiteral(Node->getString());
1167}
Reid Spencer5f016e22007-07-11 17:01:13 +00001168
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001169void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001170 OS << "@encode(" << Node->getEncodedType().getAsString() << ')';
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001171}
1172
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001173void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001174 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001175}
1176
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001177void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001178 OS << "@protocol(" << Node->getProtocol()->getNameAsString() << ')';
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001179}
1180
Steve Naroff563477d2007-09-18 23:55:05 +00001181void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1182 OS << "[";
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001183 Expr *receiver = Mess->getReceiver();
1184 if (receiver) PrintExpr(receiver);
1185 else OS << Mess->getClassName()->getName();
Ted Kremenekc29efd82008-05-02 17:32:38 +00001186 OS << ' ';
Ted Kremenek97b7f262008-04-16 04:30:16 +00001187 Selector selector = Mess->getSelector();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001188 if (selector.isUnarySelector()) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001189 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001190 } else {
1191 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001192 if (i < selector.getNumArgs()) {
1193 if (i > 0) OS << ' ';
1194 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner39f34e92008-11-24 04:00:27 +00001195 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremenekc29efd82008-05-02 17:32:38 +00001196 else
1197 OS << ":";
1198 }
1199 else OS << ", "; // Handle variadic methods.
1200
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001201 PrintExpr(Mess->getArg(i));
1202 }
Steve Naroff563477d2007-09-18 23:55:05 +00001203 }
1204 OS << "]";
1205}
1206
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001207void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) {
1208 OS << "super";
1209}
1210
Steve Naroff4eb206b2008-09-03 18:15:37 +00001211void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001212 BlockDecl *BD = Node->getBlockDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001213 OS << "^";
1214
1215 const FunctionType *AFT = Node->getFunctionType();
1216
Douglas Gregor72564e72009-02-26 23:50:07 +00001217 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001218 OS << "()";
Douglas Gregor72564e72009-02-26 23:50:07 +00001219 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001220 OS << '(';
1221 std::string ParamStr;
Steve Naroff56ee6892008-10-08 17:01:13 +00001222 for (BlockDecl::param_iterator AI = BD->param_begin(),
1223 E = BD->param_end(); AI != E; ++AI) {
1224 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner39f34e92008-11-24 04:00:27 +00001225 ParamStr = (*AI)->getNameAsString();
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001226 (*AI)->getType().getAsStringInternal(ParamStr, Policy);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001227 OS << ParamStr;
1228 }
1229
Douglas Gregor72564e72009-02-26 23:50:07 +00001230 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001231 if (FT->isVariadic()) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001232 if (!BD->param_empty()) OS << ", ";
Steve Naroff4eb206b2008-09-03 18:15:37 +00001233 OS << "...";
1234 }
1235 OS << ')';
1236 }
1237}
1238
Steve Naroff4eb206b2008-09-03 18:15:37 +00001239void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001240 OS << Node->getDecl()->getNameAsString();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001241}
Reid Spencer5f016e22007-07-11 17:01:13 +00001242//===----------------------------------------------------------------------===//
1243// Stmt method implementations
1244//===----------------------------------------------------------------------===//
1245
Eli Friedman48d14a22009-05-30 05:03:24 +00001246void Stmt::dumpPretty(ASTContext& Context) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00001247 printPretty(llvm::errs(), Context, 0,
1248 PrintingPolicy(Context.getLangOptions()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001249}
1250
Eli Friedman48d14a22009-05-30 05:03:24 +00001251void Stmt::printPretty(llvm::raw_ostream &OS, ASTContext& Context,
1252 PrinterHelper* Helper,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001253 const PrintingPolicy &Policy,
1254 unsigned Indentation) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001255 if (this == 0) {
1256 OS << "<NULL>";
1257 return;
1258 }
1259
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001260 if (Policy.Dump) {
Argyrios Kyrtzidisad42f062009-07-14 03:20:38 +00001261 dump(Context.getSourceManager());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001262 return;
1263 }
1264
Eli Friedman48d14a22009-05-30 05:03:24 +00001265 StmtPrinter P(OS, Context, Helper, Policy, Indentation);
Chris Lattnerc5598cb2007-08-21 04:04:25 +00001266 P.Visit(const_cast<Stmt*>(this));
Reid Spencer5f016e22007-07-11 17:01:13 +00001267}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001268
1269//===----------------------------------------------------------------------===//
1270// PrinterHelper
1271//===----------------------------------------------------------------------===//
1272
1273// Implement virtual destructor.
Gabor Greif84675832007-09-11 15:32:40 +00001274PrinterHelper::~PrinterHelper() {}