blob: a2c04978b030ba619cd829afd5ed785b281b8ccb [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 Jahanian5daf5702008-11-22 18:39:36 +0000521void StmtPrinter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *Node) {
522 if (Node->getBase()) {
523 PrintExpr(Node->getBase());
524 OS << ".";
525 }
526 // FIXME: Setter/Getter names
527}
528
Chris Lattnerd9f69102008-08-10 01:53:14 +0000529void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson22742662007-07-21 05:21:51 +0000530 switch (Node->getIdentType()) {
531 default:
532 assert(0 && "unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000533 case PredefinedExpr::Func:
Anders Carlsson22742662007-07-21 05:21:51 +0000534 OS << "__func__";
535 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000536 case PredefinedExpr::Function:
Anders Carlsson22742662007-07-21 05:21:51 +0000537 OS << "__FUNCTION__";
538 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000539 case PredefinedExpr::PrettyFunction:
Anders Carlsson22742662007-07-21 05:21:51 +0000540 OS << "__PRETTY_FUNCTION__";
541 break;
542 }
543}
544
Reid Spencer5f016e22007-07-11 17:01:13 +0000545void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000546 unsigned value = Node->getValue();
Chris Lattnerc250aae2008-06-07 22:35:38 +0000547 if (Node->isWide())
548 OS << "L";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000549 switch (value) {
550 case '\\':
551 OS << "'\\\\'";
552 break;
553 case '\'':
554 OS << "'\\''";
555 break;
556 case '\a':
557 // TODO: K&R: the meaning of '\\a' is different in traditional C
558 OS << "'\\a'";
559 break;
560 case '\b':
561 OS << "'\\b'";
562 break;
563 // Nonstandard escape sequence.
564 /*case '\e':
565 OS << "'\\e'";
566 break;*/
567 case '\f':
568 OS << "'\\f'";
569 break;
570 case '\n':
571 OS << "'\\n'";
572 break;
573 case '\r':
574 OS << "'\\r'";
575 break;
576 case '\t':
577 OS << "'\\t'";
578 break;
579 case '\v':
580 OS << "'\\v'";
581 break;
582 default:
Ted Kremenek471733d2008-02-23 00:52:04 +0000583 if (value < 256 && isprint(value)) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000584 OS << "'" << (char)value << "'";
585 } else if (value < 256) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000586 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000587 } else {
588 // FIXME what to really do here?
589 OS << value;
590 }
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000591 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000592}
593
594void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
595 bool isSigned = Node->getType()->isSignedIntegerType();
596 OS << Node->getValue().toString(10, isSigned);
597
598 // Emit suffixes. Integer literals are always a builtin integer type.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000599 switch (Node->getType()->getAsBuiltinType()->getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000600 default: assert(0 && "Unexpected type for integer literal!");
601 case BuiltinType::Int: break; // no suffix.
602 case BuiltinType::UInt: OS << 'U'; break;
603 case BuiltinType::Long: OS << 'L'; break;
604 case BuiltinType::ULong: OS << "UL"; break;
605 case BuiltinType::LongLong: OS << "LL"; break;
606 case BuiltinType::ULongLong: OS << "ULL"; break;
607 }
608}
609void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner86e499d2007-08-01 00:23:58 +0000610 // FIXME: print value more precisely.
Chris Lattnerda8249e2008-06-07 22:13:43 +0000611 OS << Node->getValueAsApproximateDouble();
Reid Spencer5f016e22007-07-11 17:01:13 +0000612}
Chris Lattner5d661452007-08-26 03:42:43 +0000613
614void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
615 PrintExpr(Node->getSubExpr());
616 OS << "i";
617}
618
Reid Spencer5f016e22007-07-11 17:01:13 +0000619void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
620 if (Str->isWide()) OS << 'L';
621 OS << '"';
Anders Carlssonee98ac52007-10-15 02:50:23 +0000622
Reid Spencer5f016e22007-07-11 17:01:13 +0000623 // FIXME: this doesn't print wstrings right.
624 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattner9a81c872009-01-16 19:25:18 +0000625 unsigned char Char = Str->getStrData()[i];
626
627 switch (Char) {
628 default:
629 if (isprint(Char))
630 OS << (char)Char;
631 else // Output anything hard as an octal escape.
632 OS << '\\'
633 << (char)('0'+ ((Char >> 6) & 7))
634 << (char)('0'+ ((Char >> 3) & 7))
635 << (char)('0'+ ((Char >> 0) & 7));
636 break;
637 // Handle some common non-printable cases to make dumps prettier.
Reid Spencer5f016e22007-07-11 17:01:13 +0000638 case '\\': OS << "\\\\"; break;
639 case '"': OS << "\\\""; break;
640 case '\n': OS << "\\n"; break;
641 case '\t': OS << "\\t"; break;
642 case '\a': OS << "\\a"; break;
643 case '\b': OS << "\\b"; break;
644 }
645 }
646 OS << '"';
647}
648void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
649 OS << "(";
650 PrintExpr(Node->getSubExpr());
651 OS << ")";
652}
653void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +0000654 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000655 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner296bf192007-08-23 21:46:40 +0000656
Eli Friedman7df71ac2009-06-14 22:39:26 +0000657 // Print a space if this is an "identifier operator" like __real, or if
658 // it might be concatenated incorrectly like '+'.
Chris Lattner296bf192007-08-23 21:46:40 +0000659 switch (Node->getOpcode()) {
660 default: break;
Chris Lattner296bf192007-08-23 21:46:40 +0000661 case UnaryOperator::Real:
662 case UnaryOperator::Imag:
663 case UnaryOperator::Extension:
664 OS << ' ';
665 break;
Eli Friedman7df71ac2009-06-14 22:39:26 +0000666 case UnaryOperator::Plus:
667 case UnaryOperator::Minus:
668 if (isa<UnaryOperator>(Node->getSubExpr()))
669 OS << ' ';
670 break;
Chris Lattner296bf192007-08-23 21:46:40 +0000671 }
672 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000673 PrintExpr(Node->getSubExpr());
674
675 if (Node->isPostfix())
676 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +0000677}
Chris Lattner704fe352007-08-30 17:59:59 +0000678
679bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
Eli Friedman35183ac2009-02-27 06:44:11 +0000680 if (isa<UnaryOperator>(E)) {
Chris Lattner704fe352007-08-30 17:59:59 +0000681 // Base case, print the type and comma.
682 OS << E->getType().getAsString() << ", ";
683 return true;
684 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
685 PrintOffsetOfDesignator(ASE->getLHS());
686 OS << "[";
687 PrintExpr(ASE->getRHS());
688 OS << "]";
689 return false;
690 } else {
691 MemberExpr *ME = cast<MemberExpr>(E);
692 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
Chris Lattner39f34e92008-11-24 04:00:27 +0000693 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getNameAsString();
Chris Lattner704fe352007-08-30 17:59:59 +0000694 return false;
695 }
696}
697
698void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
699 OS << "__builtin_offsetof(";
700 PrintOffsetOfDesignator(Node->getSubExpr());
701 OS << ")";
702}
703
Sebastian Redl05189992008-11-11 17:56:53 +0000704void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
705 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
706 if (Node->isArgumentType())
707 OS << "(" << Node->getArgumentType().getAsString() << ")";
708 else {
709 OS << " ";
710 PrintExpr(Node->getArgumentExpr());
711 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000712}
713void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +0000714 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000715 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +0000716 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000717 OS << "]";
718}
719
720void StmtPrinter::VisitCallExpr(CallExpr *Call) {
721 PrintExpr(Call->getCallee());
722 OS << "(";
723 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattner04421082008-04-08 04:40:51 +0000724 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
725 // Don't print any defaulted arguments
726 break;
727 }
728
Reid Spencer5f016e22007-07-11 17:01:13 +0000729 if (i) OS << ", ";
730 PrintExpr(Call->getArg(i));
731 }
732 OS << ")";
733}
734void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregorb3eef682009-01-08 22:45:41 +0000735 // FIXME: Suppress printing implicit bases (like "this")
736 PrintExpr(Node->getBase());
737 OS << (Node->isArrow() ? "->" : ".");
738 // FIXME: Suppress printing references to unnamed objects
739 // representing anonymous unions/structs
Douglas Gregor86f19402008-12-20 23:49:58 +0000740 OS << Node->getMemberDecl()->getNameAsString();
Reid Spencer5f016e22007-07-11 17:01:13 +0000741}
Steve Narofff242b1b2009-07-24 17:54:45 +0000742void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
743 PrintExpr(Node->getBase());
744 OS << (Node->isArrow() ? "->isa" : ".isa");
745}
746
Nate Begeman213541a2008-04-18 23:10:10 +0000747void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +0000748 PrintExpr(Node->getBase());
749 OS << ".";
750 OS << Node->getAccessor().getName();
751}
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000752void StmtPrinter::VisitCastExpr(CastExpr *) {
753 assert(0 && "CastExpr is an abstract class");
754}
Douglas Gregor49badde2008-10-27 19:41:14 +0000755void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *) {
756 assert(0 && "ExplicitCastExpr is an abstract class");
757}
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000758void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000759 OS << "(" << Node->getType().getAsString() << ")";
Reid Spencer5f016e22007-07-11 17:01:13 +0000760 PrintExpr(Node->getSubExpr());
761}
Steve Naroffaff1edd2007-07-19 21:32:11 +0000762void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
763 OS << "(" << Node->getType().getAsString() << ")";
764 PrintExpr(Node->getInitializer());
765}
Steve Naroff49b45262007-07-13 16:58:59 +0000766void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroff90045e82007-07-13 23:32:42 +0000767 // No need to print anything, simply forward to the sub expression.
768 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +0000769}
Reid Spencer5f016e22007-07-11 17:01:13 +0000770void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
771 PrintExpr(Node->getLHS());
772 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
773 PrintExpr(Node->getRHS());
774}
Chris Lattnereb14fe82007-08-25 02:00:02 +0000775void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
776 PrintExpr(Node->getLHS());
777 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
778 PrintExpr(Node->getRHS());
779}
Reid Spencer5f016e22007-07-11 17:01:13 +0000780void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
781 PrintExpr(Node->getCond());
Ted Kremenek8e911c42007-11-26 18:27:54 +0000782
783 if (Node->getLHS()) {
784 OS << " ? ";
785 PrintExpr(Node->getLHS());
786 OS << " : ";
787 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000788 else { // Handle GCC extension where LHS can be NULL.
Ted Kremenek8e911c42007-11-26 18:27:54 +0000789 OS << " ?: ";
790 }
791
Reid Spencer5f016e22007-07-11 17:01:13 +0000792 PrintExpr(Node->getRHS());
793}
794
795// GNU extensions.
796
Chris Lattner6481a572007-08-03 17:31:20 +0000797void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000798 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000799}
800
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000801void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
802 OS << "(";
803 PrintRawCompoundStmt(E->getSubStmt());
804 OS << ")";
805}
806
Steve Naroffd34e9152007-08-01 22:05:33 +0000807void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
808 OS << "__builtin_types_compatible_p(";
809 OS << Node->getArgType1().getAsString() << ",";
810 OS << Node->getArgType2().getAsString() << ")";
811}
812
Steve Naroffd04fdd52007-08-03 21:21:27 +0000813void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
814 OS << "__builtin_choose_expr(";
815 PrintExpr(Node->getCond());
Chris Lattner94f05e32007-08-04 00:20:15 +0000816 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000817 PrintExpr(Node->getLHS());
Chris Lattner94f05e32007-08-04 00:20:15 +0000818 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000819 PrintExpr(Node->getRHS());
820 OS << ")";
821}
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000822
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000823void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
824 OS << "__null";
825}
826
Eli Friedmand38617c2008-05-14 19:38:39 +0000827void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
828 OS << "__builtin_shufflevector(";
829 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
830 if (i) OS << ", ";
831 PrintExpr(Node->getExpr(i));
832 }
833 OS << ")";
834}
835
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000836void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000837 if (Node->getSyntacticForm()) {
838 Visit(Node->getSyntacticForm());
839 return;
840 }
841
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000842 OS << "{ ";
843 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
844 if (i) OS << ", ";
Douglas Gregor4c678342009-01-28 21:54:33 +0000845 if (Node->getInit(i))
846 PrintExpr(Node->getInit(i));
847 else
848 OS << "0";
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000849 }
850 OS << " }";
851}
852
Nate Begeman2ef13e52009-08-10 23:49:36 +0000853void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
854 OS << "( ";
855 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
856 if (i) OS << ", ";
857 PrintExpr(Node->getExpr(i));
858 }
859 OS << " )";
860}
861
Douglas Gregor05c13a32009-01-22 00:58:24 +0000862void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000863 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
864 DEnd = Node->designators_end();
865 D != DEnd; ++D) {
866 if (D->isFieldDesignator()) {
867 if (D->getDotLoc().isInvalid())
868 OS << D->getFieldName()->getName() << ":";
869 else
870 OS << "." << D->getFieldName()->getName();
871 } else {
872 OS << "[";
873 if (D->isArrayDesignator()) {
874 PrintExpr(Node->getArrayIndex(*D));
875 } else {
876 PrintExpr(Node->getArrayRangeStart(*D));
877 OS << " ... ";
878 PrintExpr(Node->getArrayRangeEnd(*D));
879 }
880 OS << "]";
881 }
882 }
883
884 OS << " = ";
885 PrintExpr(Node->getInit());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000886}
887
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000888void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Chris Lattnere4f21422009-06-30 01:26:17 +0000889 if (Policy.LangOpts.CPlusPlus)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000890 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()";
891 else {
892 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")";
893 if (Node->getType()->isRecordType())
894 OS << "{}";
895 else
896 OS << 0;
897 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000898}
899
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000900void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000901 OS << "__builtin_va_arg(";
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000902 PrintExpr(Node->getSubExpr());
903 OS << ", ";
904 OS << Node->getType().getAsString();
905 OS << ")";
906}
907
Reid Spencer5f016e22007-07-11 17:01:13 +0000908// C++
Douglas Gregorb4609802008-11-14 16:09:21 +0000909void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
910 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
911 "",
912#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
913 Spelling,
914#include "clang/Basic/OperatorKinds.def"
915 };
916
917 OverloadedOperatorKind Kind = Node->getOperator();
918 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
919 if (Node->getNumArgs() == 1) {
920 OS << OpStrings[Kind] << ' ';
921 PrintExpr(Node->getArg(0));
922 } else {
923 PrintExpr(Node->getArg(0));
924 OS << ' ' << OpStrings[Kind];
925 }
926 } else if (Kind == OO_Call) {
927 PrintExpr(Node->getArg(0));
928 OS << '(';
929 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
930 if (ArgIdx > 1)
931 OS << ", ";
932 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
933 PrintExpr(Node->getArg(ArgIdx));
934 }
935 OS << ')';
936 } else if (Kind == OO_Subscript) {
937 PrintExpr(Node->getArg(0));
938 OS << '[';
939 PrintExpr(Node->getArg(1));
940 OS << ']';
941 } else if (Node->getNumArgs() == 1) {
942 OS << OpStrings[Kind] << ' ';
943 PrintExpr(Node->getArg(0));
944 } else if (Node->getNumArgs() == 2) {
945 PrintExpr(Node->getArg(0));
946 OS << ' ' << OpStrings[Kind] << ' ';
947 PrintExpr(Node->getArg(1));
948 } else {
949 assert(false && "unknown overloaded operator");
950 }
951}
Reid Spencer5f016e22007-07-11 17:01:13 +0000952
Douglas Gregor88a35142008-12-22 05:46:06 +0000953void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
954 VisitCallExpr(cast<CallExpr>(Node));
955}
956
Douglas Gregor49badde2008-10-27 19:41:14 +0000957void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
958 OS << Node->getCastName() << '<';
959 OS << Node->getTypeAsWritten().getAsString() << ">(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000960 PrintExpr(Node->getSubExpr());
961 OS << ")";
962}
963
Douglas Gregor49badde2008-10-27 19:41:14 +0000964void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
965 VisitCXXNamedCastExpr(Node);
966}
967
968void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
969 VisitCXXNamedCastExpr(Node);
970}
971
972void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
973 VisitCXXNamedCastExpr(Node);
974}
975
976void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
977 VisitCXXNamedCastExpr(Node);
978}
979
Sebastian Redlc42e1182008-11-11 11:37:55 +0000980void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
981 OS << "typeid(";
982 if (Node->isTypeOperand()) {
983 OS << Node->getTypeOperand().getAsString();
984 } else {
985 PrintExpr(Node->getExprOperand());
986 }
987 OS << ")";
988}
989
Reid Spencer5f016e22007-07-11 17:01:13 +0000990void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
991 OS << (Node->getValue() ? "true" : "false");
992}
993
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000994void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
995 OS << "nullptr";
996}
997
Douglas Gregor796da182008-11-04 14:32:21 +0000998void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
999 OS << "this";
1000}
1001
Chris Lattner50dd2892008-02-26 00:51:44 +00001002void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1003 if (Node->getSubExpr() == 0)
1004 OS << "throw";
1005 else {
1006 OS << "throw ";
1007 PrintExpr(Node->getSubExpr());
1008 }
1009}
1010
Chris Lattner04421082008-04-08 04:40:51 +00001011void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1012 // Nothing to print: we picked up the default argument
1013}
1014
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001015void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1016 OS << Node->getType().getAsString();
1017 OS << "(";
1018 PrintExpr(Node->getSubExpr());
1019 OS << ")";
1020}
1021
Anders Carlssonfceb0a82009-05-30 20:03:25 +00001022void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1023 PrintExpr(Node->getSubExpr());
1024}
1025
Douglas Gregor506ae412009-01-16 18:33:17 +00001026void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1027 OS << Node->getType().getAsString();
1028 OS << "(";
1029 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
1030 ArgEnd = Node->arg_end();
1031 Arg != ArgEnd; ++Arg) {
1032 if (Arg != Node->arg_begin())
1033 OS << ", ";
1034 PrintExpr(*Arg);
1035 }
1036 OS << ")";
1037}
1038
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001039void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
1040 OS << Node->getType().getAsString() << "()";
1041}
1042
Argyrios Kyrtzidis9e922b12008-09-09 23:47:53 +00001043void
1044StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
1045 PrintRawDecl(E->getVarDecl());
1046}
1047
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001048void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1049 if (E->isGlobalNew())
1050 OS << "::";
1051 OS << "new ";
1052 unsigned NumPlace = E->getNumPlacementArgs();
1053 if (NumPlace > 0) {
1054 OS << "(";
1055 PrintExpr(E->getPlacementArg(0));
1056 for (unsigned i = 1; i < NumPlace; ++i) {
1057 OS << ", ";
1058 PrintExpr(E->getPlacementArg(i));
1059 }
1060 OS << ") ";
1061 }
1062 if (E->isParenTypeId())
1063 OS << "(";
Sebastian Redl6fec6482008-12-02 22:08:59 +00001064 std::string TypeS;
1065 if (Expr *Size = E->getArraySize()) {
1066 llvm::raw_string_ostream s(TypeS);
Eli Friedman6e1a3452009-05-30 05:32:46 +00001067 Size->printPretty(s, Context, Helper, Policy);
Sebastian Redl6fec6482008-12-02 22:08:59 +00001068 s.flush();
1069 TypeS = "[" + TypeS + "]";
1070 }
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001071 E->getAllocatedType().getAsStringInternal(TypeS, Policy);
Sebastian Redl6fec6482008-12-02 22:08:59 +00001072 OS << TypeS;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001073 if (E->isParenTypeId())
1074 OS << ")";
1075
1076 if (E->hasInitializer()) {
1077 OS << "(";
1078 unsigned NumCons = E->getNumConstructorArgs();
1079 if (NumCons > 0) {
1080 PrintExpr(E->getConstructorArg(0));
1081 for (unsigned i = 1; i < NumCons; ++i) {
1082 OS << ", ";
1083 PrintExpr(E->getConstructorArg(i));
1084 }
1085 }
1086 OS << ")";
1087 }
1088}
1089
1090void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1091 if (E->isGlobalDelete())
1092 OS << "::";
1093 OS << "delete ";
1094 if (E->isArrayForm())
1095 OS << "[] ";
1096 PrintExpr(E->getArgument());
1097}
1098
Douglas Gregor17330012009-02-04 15:01:18 +00001099void StmtPrinter::VisitUnresolvedFunctionNameExpr(UnresolvedFunctionNameExpr *E) {
1100 OS << E->getName().getAsString();
Douglas Gregor5c37de72008-12-06 00:22:45 +00001101}
1102
Anders Carlssone349bea2009-04-23 02:32:43 +00001103void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
1104 // Nothing to print.
1105}
1106
Anders Carlsson2d44e8a2009-05-01 22:18:43 +00001107void StmtPrinter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
Anders Carlsson02bbfa32009-04-24 22:47:04 +00001108 // Just forward to the sub expression.
1109 PrintExpr(E->getSubExpr());
1110}
1111
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001112void
1113StmtPrinter::VisitCXXUnresolvedConstructExpr(
1114 CXXUnresolvedConstructExpr *Node) {
1115 OS << Node->getTypeAsWritten().getAsString();
1116 OS << "(";
1117 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
1118 ArgEnd = Node->arg_end();
1119 Arg != ArgEnd; ++Arg) {
1120 if (Arg != Node->arg_begin())
1121 OS << ", ";
1122 PrintExpr(*Arg);
1123 }
1124 OS << ")";
1125}
1126
Douglas Gregor1c0ca592009-05-22 21:13:27 +00001127void StmtPrinter::VisitCXXUnresolvedMemberExpr(CXXUnresolvedMemberExpr *Node) {
1128 PrintExpr(Node->getBase());
1129 OS << (Node->isArrow() ? "->" : ".");
1130 OS << Node->getMember().getAsString();
1131}
1132
Sebastian Redl64b45f72009-01-05 20:52:13 +00001133static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1134 switch (UTT) {
1135 default: assert(false && "Unknown type trait");
1136 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1137 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1138 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1139 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1140 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1141 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1142 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1143 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1144 case UTT_IsAbstract: return "__is_abstract";
1145 case UTT_IsClass: return "__is_class";
1146 case UTT_IsEmpty: return "__is_empty";
1147 case UTT_IsEnum: return "__is_enum";
1148 case UTT_IsPOD: return "__is_pod";
1149 case UTT_IsPolymorphic: return "__is_polymorphic";
1150 case UTT_IsUnion: return "__is_union";
1151 }
1152}
1153
1154void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1155 OS << getTypeTraitName(E->getTrait()) << "("
1156 << E->getQueriedType().getAsString() << ")";
1157}
1158
Anders Carlsson55085182007-08-21 17:43:55 +00001159// Obj-C
1160
1161void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1162 OS << "@";
1163 VisitStringLiteral(Node->getString());
1164}
Reid Spencer5f016e22007-07-11 17:01:13 +00001165
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001166void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001167 OS << "@encode(" << Node->getEncodedType().getAsString() << ')';
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001168}
1169
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001170void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001171 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001172}
1173
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001174void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001175 OS << "@protocol(" << Node->getProtocol()->getNameAsString() << ')';
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001176}
1177
Steve Naroff563477d2007-09-18 23:55:05 +00001178void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1179 OS << "[";
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001180 Expr *receiver = Mess->getReceiver();
1181 if (receiver) PrintExpr(receiver);
1182 else OS << Mess->getClassName()->getName();
Ted Kremenekc29efd82008-05-02 17:32:38 +00001183 OS << ' ';
Ted Kremenek97b7f262008-04-16 04:30:16 +00001184 Selector selector = Mess->getSelector();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001185 if (selector.isUnarySelector()) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001186 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001187 } else {
1188 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001189 if (i < selector.getNumArgs()) {
1190 if (i > 0) OS << ' ';
1191 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner39f34e92008-11-24 04:00:27 +00001192 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremenekc29efd82008-05-02 17:32:38 +00001193 else
1194 OS << ":";
1195 }
1196 else OS << ", "; // Handle variadic methods.
1197
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001198 PrintExpr(Mess->getArg(i));
1199 }
Steve Naroff563477d2007-09-18 23:55:05 +00001200 }
1201 OS << "]";
1202}
1203
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001204void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) {
1205 OS << "super";
1206}
1207
Steve Naroff4eb206b2008-09-03 18:15:37 +00001208void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001209 BlockDecl *BD = Node->getBlockDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001210 OS << "^";
1211
1212 const FunctionType *AFT = Node->getFunctionType();
1213
Douglas Gregor72564e72009-02-26 23:50:07 +00001214 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001215 OS << "()";
Douglas Gregor72564e72009-02-26 23:50:07 +00001216 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001217 OS << '(';
1218 std::string ParamStr;
Steve Naroff56ee6892008-10-08 17:01:13 +00001219 for (BlockDecl::param_iterator AI = BD->param_begin(),
1220 E = BD->param_end(); AI != E; ++AI) {
1221 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner39f34e92008-11-24 04:00:27 +00001222 ParamStr = (*AI)->getNameAsString();
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001223 (*AI)->getType().getAsStringInternal(ParamStr, Policy);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001224 OS << ParamStr;
1225 }
1226
Douglas Gregor72564e72009-02-26 23:50:07 +00001227 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001228 if (FT->isVariadic()) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001229 if (!BD->param_empty()) OS << ", ";
Steve Naroff4eb206b2008-09-03 18:15:37 +00001230 OS << "...";
1231 }
1232 OS << ')';
1233 }
1234}
1235
Steve Naroff4eb206b2008-09-03 18:15:37 +00001236void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001237 OS << Node->getDecl()->getNameAsString();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001238}
Reid Spencer5f016e22007-07-11 17:01:13 +00001239//===----------------------------------------------------------------------===//
1240// Stmt method implementations
1241//===----------------------------------------------------------------------===//
1242
Eli Friedman48d14a22009-05-30 05:03:24 +00001243void Stmt::dumpPretty(ASTContext& Context) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00001244 printPretty(llvm::errs(), Context, 0,
1245 PrintingPolicy(Context.getLangOptions()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001246}
1247
Eli Friedman48d14a22009-05-30 05:03:24 +00001248void Stmt::printPretty(llvm::raw_ostream &OS, ASTContext& Context,
1249 PrinterHelper* Helper,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001250 const PrintingPolicy &Policy,
1251 unsigned Indentation) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001252 if (this == 0) {
1253 OS << "<NULL>";
1254 return;
1255 }
1256
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001257 if (Policy.Dump) {
Argyrios Kyrtzidisad42f062009-07-14 03:20:38 +00001258 dump(Context.getSourceManager());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001259 return;
1260 }
1261
Eli Friedman48d14a22009-05-30 05:03:24 +00001262 StmtPrinter P(OS, Context, Helper, Policy, Indentation);
Chris Lattnerc5598cb2007-08-21 04:04:25 +00001263 P.Visit(const_cast<Stmt*>(this));
Reid Spencer5f016e22007-07-11 17:01:13 +00001264}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001265
1266//===----------------------------------------------------------------------===//
1267// PrinterHelper
1268//===----------------------------------------------------------------------===//
1269
1270// Implement virtual destructor.
Gabor Greif84675832007-09-11 15:32:40 +00001271PrinterHelper::~PrinterHelper() {}