blob: bbb904de79b32b4ac40543f81cd76d8235bfc97e [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"
Ted Kremeneka95d3752008-09-13 05:16:45 +000019#include "llvm/Support/Format.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// StmtPrinter Visitor
24//===----------------------------------------------------------------------===//
25
26namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +000027 class StmtPrinter : public StmtVisitor<StmtPrinter> {
Ted Kremeneka95d3752008-09-13 05:16:45 +000028 llvm::raw_ostream &OS;
Eli Friedman48d14a22009-05-30 05:03:24 +000029 ASTContext &Context;
Reid Spencer5f016e22007-07-11 17:01:13 +000030 unsigned IndentLevel;
Ted Kremenek42a509f2007-08-31 21:30:12 +000031 clang::PrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000032 PrintingPolicy Policy;
33
Reid Spencer5f016e22007-07-11 17:01:13 +000034 public:
Mike Stump1eb44332009-09-09 15:08:12 +000035 StmtPrinter(llvm::raw_ostream &os, ASTContext &C, PrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +000036 const PrintingPolicy &Policy,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000037 unsigned Indentation = 0)
Eli Friedman48d14a22009-05-30 05:03:24 +000038 : OS(os), Context(C), IndentLevel(Indentation), Helper(helper),
39 Policy(Policy) {}
Mike Stump1eb44332009-09-09 15:08:12 +000040
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000041 void PrintStmt(Stmt *S) {
42 PrintStmt(S, Policy.Indentation);
43 }
44
45 void PrintStmt(Stmt *S, int SubIndent) {
Reid Spencer5f016e22007-07-11 17:01:13 +000046 IndentLevel += SubIndent;
47 if (S && isa<Expr>(S)) {
48 // If this is an expr used in a stmt context, indent and newline it.
49 Indent();
Chris Lattnerc5598cb2007-08-21 04:04:25 +000050 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000051 OS << ";\n";
52 } else if (S) {
Chris Lattnerc5598cb2007-08-21 04:04:25 +000053 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000054 } else {
55 Indent() << "<<<NULL STATEMENT>>>\n";
56 }
57 IndentLevel -= SubIndent;
58 }
Eli Friedmandb23b152009-05-30 00:19:54 +000059
Reid Spencer5f016e22007-07-11 17:01:13 +000060 void PrintRawCompoundStmt(CompoundStmt *S);
61 void PrintRawDecl(Decl *D);
Ted Kremenekecd64c52008-10-06 18:39:36 +000062 void PrintRawDeclStmt(DeclStmt *S);
Reid Spencer5f016e22007-07-11 17:01:13 +000063 void PrintRawIfStmt(IfStmt *If);
Sebastian Redl8351da02008-12-22 21:35:02 +000064 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
Mike Stump1eb44332009-09-09 15:08:12 +000065
Reid Spencer5f016e22007-07-11 17:01:13 +000066 void PrintExpr(Expr *E) {
67 if (E)
Chris Lattnerc5598cb2007-08-21 04:04:25 +000068 Visit(E);
Reid Spencer5f016e22007-07-11 17:01:13 +000069 else
70 OS << "<null expr>";
71 }
Mike Stump1eb44332009-09-09 15:08:12 +000072
Mike Stump071e4da2009-02-10 20:16:46 +000073 llvm::raw_ostream &Indent(int Delta = 0) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000074 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
75 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +000076 return OS;
77 }
Mike Stump1eb44332009-09-09 15:08:12 +000078
Chris Lattner704fe352007-08-30 17:59:59 +000079 bool PrintOffsetOfDesignator(Expr *E);
80 void VisitUnaryOffsetOf(UnaryOperator *Node);
Mike Stump1eb44332009-09-09 15:08:12 +000081
82 void Visit(Stmt* S) {
Ted Kremenek42a509f2007-08-31 21:30:12 +000083 if (Helper && Helper->handledStmt(S,OS))
84 return;
85 else StmtVisitor<StmtPrinter>::Visit(S);
86 }
Mike Stump1eb44332009-09-09 15:08:12 +000087
Chris Lattnerc5598cb2007-08-21 04:04:25 +000088 void VisitStmt(Stmt *Node);
Douglas Gregorf2cad862008-11-14 12:46:07 +000089#define STMT(CLASS, PARENT) \
Chris Lattnerc5598cb2007-08-21 04:04:25 +000090 void Visit##CLASS(CLASS *Node);
Reid Spencer5f016e22007-07-11 17:01:13 +000091#include "clang/AST/StmtNodes.def"
92 };
93}
94
95//===----------------------------------------------------------------------===//
96// Stmt printing methods.
97//===----------------------------------------------------------------------===//
98
99void StmtPrinter::VisitStmt(Stmt *Node) {
100 Indent() << "<<unknown stmt type>>\n";
101}
102
103/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
104/// with no newline after the }.
105void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
106 OS << "{\n";
107 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
108 I != E; ++I)
109 PrintStmt(*I);
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Reid Spencer5f016e22007-07-11 17:01:13 +0000111 Indent() << "}";
112}
113
114void StmtPrinter::PrintRawDecl(Decl *D) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000115 D->print(OS, Policy, IndentLevel);
Mike Stump071e4da2009-02-10 20:16:46 +0000116}
117
Ted Kremenekecd64c52008-10-06 18:39:36 +0000118void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
Eli Friedmandb23b152009-05-30 00:19:54 +0000119 DeclStmt::decl_iterator Begin = S->decl_begin(), End = S->decl_end();
Eli Friedman42f42c02009-05-30 04:20:30 +0000120 llvm::SmallVector<Decl*, 2> Decls;
Mike Stump1eb44332009-09-09 15:08:12 +0000121 for ( ; Begin != End; ++Begin)
Eli Friedman42f42c02009-05-30 04:20:30 +0000122 Decls.push_back(*Begin);
Eli Friedmandb23b152009-05-30 00:19:54 +0000123
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000124 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
Ted Kremenekecd64c52008-10-06 18:39:36 +0000125}
Reid Spencer5f016e22007-07-11 17:01:13 +0000126
127void StmtPrinter::VisitNullStmt(NullStmt *Node) {
128 Indent() << ";\n";
129}
130
131void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Eli Friedmandb23b152009-05-30 00:19:54 +0000132 Indent();
133 PrintRawDeclStmt(Node);
134 OS << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000135}
136
137void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
138 Indent();
139 PrintRawCompoundStmt(Node);
140 OS << "\n";
141}
142
143void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
144 Indent(-1) << "case ";
145 PrintExpr(Node->getLHS());
146 if (Node->getRHS()) {
147 OS << " ... ";
148 PrintExpr(Node->getRHS());
149 }
150 OS << ":\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 PrintStmt(Node->getSubStmt(), 0);
153}
154
155void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
156 Indent(-1) << "default:\n";
157 PrintStmt(Node->getSubStmt(), 0);
158}
159
160void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
161 Indent(-1) << Node->getName() << ":\n";
162 PrintStmt(Node->getSubStmt(), 0);
163}
164
165void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000166 OS << "if (";
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 PrintExpr(If->getCond());
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000168 OS << ')';
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
171 OS << ' ';
172 PrintRawCompoundStmt(CS);
173 OS << (If->getElse() ? ' ' : '\n');
174 } else {
175 OS << '\n';
176 PrintStmt(If->getThen());
177 if (If->getElse()) Indent();
178 }
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 if (Stmt *Else = If->getElse()) {
181 OS << "else";
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Reid Spencer5f016e22007-07-11 17:01:13 +0000183 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
184 OS << ' ';
185 PrintRawCompoundStmt(CS);
186 OS << '\n';
187 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
188 OS << ' ';
189 PrintRawIfStmt(ElseIf);
190 } else {
191 OS << '\n';
192 PrintStmt(If->getElse());
193 }
194 }
195}
196
197void StmtPrinter::VisitIfStmt(IfStmt *If) {
198 Indent();
199 PrintRawIfStmt(If);
200}
201
202void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
203 Indent() << "switch (";
204 PrintExpr(Node->getCond());
205 OS << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Reid Spencer5f016e22007-07-11 17:01:13 +0000207 // Pretty print compoundstmt bodies (very common).
208 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
209 OS << " ";
210 PrintRawCompoundStmt(CS);
211 OS << "\n";
212 } else {
213 OS << "\n";
214 PrintStmt(Node->getBody());
215 }
216}
217
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000218void StmtPrinter::VisitSwitchCase(SwitchCase*) {
219 assert(0 && "SwitchCase is an abstract class");
220}
221
Reid Spencer5f016e22007-07-11 17:01:13 +0000222void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
223 Indent() << "while (";
224 PrintExpr(Node->getCond());
225 OS << ")\n";
226 PrintStmt(Node->getBody());
227}
228
229void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner8bdcc472007-09-15 21:49:37 +0000230 Indent() << "do ";
231 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
232 PrintRawCompoundStmt(CS);
233 OS << " ";
234 } else {
235 OS << "\n";
236 PrintStmt(Node->getBody());
237 Indent();
238 }
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Eli Friedmanb3e22962009-05-17 01:05:34 +0000240 OS << "while (";
Reid Spencer5f016e22007-07-11 17:01:13 +0000241 PrintExpr(Node->getCond());
Eli Friedmanb3e22962009-05-17 01:05:34 +0000242 OS << ");\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000243}
244
245void StmtPrinter::VisitForStmt(ForStmt *Node) {
246 Indent() << "for (";
247 if (Node->getInit()) {
248 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000249 PrintRawDeclStmt(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000250 else
251 PrintExpr(cast<Expr>(Node->getInit()));
252 }
Chris Lattner8bdcc472007-09-15 21:49:37 +0000253 OS << ";";
254 if (Node->getCond()) {
255 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000256 PrintExpr(Node->getCond());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000257 }
258 OS << ";";
259 if (Node->getInc()) {
260 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000261 PrintExpr(Node->getInc());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000262 }
263 OS << ") ";
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Chris Lattner8bdcc472007-09-15 21:49:37 +0000265 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
266 PrintRawCompoundStmt(CS);
267 OS << "\n";
268 } else {
269 OS << "\n";
270 PrintStmt(Node->getBody());
271 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000272}
273
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000274void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000275 Indent() << "for (";
276 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000277 PrintRawDeclStmt(DS);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000278 else
279 PrintExpr(cast<Expr>(Node->getElement()));
280 OS << " in ";
281 PrintExpr(Node->getCollection());
282 OS << ") ";
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000284 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
285 PrintRawCompoundStmt(CS);
286 OS << "\n";
287 } else {
288 OS << "\n";
289 PrintStmt(Node->getBody());
290 }
291}
292
Reid Spencer5f016e22007-07-11 17:01:13 +0000293void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
294 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
295}
296
297void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
298 Indent() << "goto *";
299 PrintExpr(Node->getTarget());
300 OS << ";\n";
301}
302
303void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
304 Indent() << "continue;\n";
305}
306
307void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
308 Indent() << "break;\n";
309}
310
311
312void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
313 Indent() << "return";
314 if (Node->getRetValue()) {
315 OS << " ";
316 PrintExpr(Node->getRetValue());
317 }
318 OS << ";\n";
319}
320
Chris Lattnerfe795952007-10-29 04:04:16 +0000321
322void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson39c47b52007-11-23 23:12:25 +0000323 Indent() << "asm ";
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Anders Carlsson39c47b52007-11-23 23:12:25 +0000325 if (Node->isVolatile())
326 OS << "volatile ";
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Anders Carlsson39c47b52007-11-23 23:12:25 +0000328 OS << "(";
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000329 VisitStringLiteral(Node->getAsmString());
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Anders Carlssonb235fc22007-11-22 01:36:19 +0000331 // Outputs
332 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
333 Node->getNumClobbers() != 0)
334 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Anders Carlssonb235fc22007-11-22 01:36:19 +0000336 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
337 if (i != 0)
338 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Anders Carlssonb235fc22007-11-22 01:36:19 +0000340 if (!Node->getOutputName(i).empty()) {
341 OS << '[';
342 OS << Node->getOutputName(i);
343 OS << "] ";
344 }
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Chris Lattnerb3277932009-03-10 04:59:06 +0000346 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000347 OS << " ";
348 Visit(Node->getOutputExpr(i));
349 }
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Anders Carlssonb235fc22007-11-22 01:36:19 +0000351 // Inputs
352 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
353 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Anders Carlssonb235fc22007-11-22 01:36:19 +0000355 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
356 if (i != 0)
357 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Anders Carlssonb235fc22007-11-22 01:36:19 +0000359 if (!Node->getInputName(i).empty()) {
360 OS << '[';
361 OS << Node->getInputName(i);
362 OS << "] ";
363 }
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Chris Lattnerb3277932009-03-10 04:59:06 +0000365 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000366 OS << " ";
367 Visit(Node->getInputExpr(i));
368 }
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Anders Carlssonb235fc22007-11-22 01:36:19 +0000370 // Clobbers
371 if (Node->getNumClobbers() != 0)
372 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Anders Carlssonb235fc22007-11-22 01:36:19 +0000374 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
375 if (i != 0)
376 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Anders Carlssonb235fc22007-11-22 01:36:19 +0000378 VisitStringLiteral(Node->getClobber(i));
379 }
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000381 OS << ");\n";
Chris Lattnerfe795952007-10-29 04:04:16 +0000382}
383
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000384void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000385 Indent() << "@try";
386 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
387 PrintRawCompoundStmt(TS);
388 OS << "\n";
389 }
Mike Stump1eb44332009-09-09 15:08:12 +0000390
391 for (ObjCAtCatchStmt *catchStmt =
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000392 static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts());
Mike Stump1eb44332009-09-09 15:08:12 +0000393 catchStmt;
394 catchStmt =
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000395 static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000396 Indent() << "@catch(";
Steve Naroff7ba138a2009-03-03 19:52:17 +0000397 if (catchStmt->getCatchParamDecl()) {
398 if (Decl *DS = catchStmt->getCatchParamDecl())
399 PrintRawDecl(DS);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000400 }
401 OS << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000402 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
403 PrintRawCompoundStmt(CS);
404 OS << "\n";
405 }
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000406 }
Mike Stump1eb44332009-09-09 15:08:12 +0000407
408 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
409 Node->getFinallyStmt())) {
Fariborz Jahanian1e7eab42007-11-07 00:46:42 +0000410 Indent() << "@finally";
411 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000412 OS << "\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000413 }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000414}
415
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000416void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000417}
418
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000419void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000420 Indent() << "@catch (...) { /* todo */ } \n";
421}
422
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000423void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000424 Indent() << "@throw";
425 if (Node->getThrowExpr()) {
426 OS << " ";
427 PrintExpr(Node->getThrowExpr());
428 }
429 OS << ";\n";
430}
431
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000432void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000433 Indent() << "@synchronized (";
434 PrintExpr(Node->getSynchExpr());
435 OS << ")";
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000436 PrintRawCompoundStmt(Node->getSynchBody());
437 OS << "\n";
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000438}
439
Sebastian Redl8351da02008-12-22 21:35:02 +0000440void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
441 OS << "catch (";
Sebastian Redl4b07b292008-12-22 19:15:10 +0000442 if (Decl *ExDecl = Node->getExceptionDecl())
443 PrintRawDecl(ExDecl);
444 else
445 OS << "...";
446 OS << ") ";
447 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl8351da02008-12-22 21:35:02 +0000448}
449
450void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
451 Indent();
452 PrintRawCXXCatchStmt(Node);
453 OS << "\n";
454}
455
456void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
457 Indent() << "try ";
458 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump1eb44332009-09-09 15:08:12 +0000459 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl8351da02008-12-22 21:35:02 +0000460 OS << " ";
461 PrintRawCXXCatchStmt(Node->getHandler(i));
462 }
Sebastian Redl4b07b292008-12-22 19:15:10 +0000463 OS << "\n";
464}
465
Reid Spencer5f016e22007-07-11 17:01:13 +0000466//===----------------------------------------------------------------------===//
467// Expr printing methods.
468//===----------------------------------------------------------------------===//
469
470void StmtPrinter::VisitExpr(Expr *Node) {
471 OS << "<<unknown expr type>>";
472}
473
474void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000475 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
476 Qualifier->print(OS, Policy);
Chris Lattner39f34e92008-11-24 04:00:27 +0000477 OS << Node->getDecl()->getNameAsString();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000478 if (Node->hasExplicitTemplateArgumentList())
479 OS << TemplateSpecializationType::PrintTemplateArgumentList(
480 Node->getTemplateArgs(),
481 Node->getNumTemplateArgs(),
482 Policy);
Douglas Gregor1a49af92009-01-06 05:10:23 +0000483}
484
John McCall865d4472009-11-19 22:55:06 +0000485void StmtPrinter::VisitDependentScopeDeclRefExpr(
486 DependentScopeDeclRefExpr *Node) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000487 Node->getQualifier()->print(OS, Policy);
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000488 OS << Node->getDeclName().getAsString();
John McCallf7a1a742009-11-24 19:00:30 +0000489 if (Node->hasExplicitTemplateArgs())
490 OS << TemplateSpecializationType::PrintTemplateArgumentList(
491 Node->getTemplateArgs(),
492 Node->getNumTemplateArgs(),
493 Policy);
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000494}
495
John McCallba135432009-11-21 08:51:07 +0000496void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000497 if (Node->getQualifier())
498 Node->getQualifier()->print(OS, Policy);
John McCallf7a1a742009-11-24 19:00:30 +0000499 OS << Node->getName().getAsString();
500 if (Node->hasExplicitTemplateArgs())
501 OS << TemplateSpecializationType::PrintTemplateArgumentList(
502 Node->getTemplateArgs(),
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000503 Node->getNumTemplateArgs(),
John McCallf7a1a742009-11-24 19:00:30 +0000504 Policy);
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000505}
506
Steve Naroff7779db42007-11-12 14:29:37 +0000507void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000508 if (Node->getBase()) {
509 PrintExpr(Node->getBase());
510 OS << (Node->isArrow() ? "->" : ".");
511 }
Chris Lattner39f34e92008-11-24 04:00:27 +0000512 OS << Node->getDecl()->getNameAsString();
Steve Naroff7779db42007-11-12 14:29:37 +0000513}
514
Steve Naroffae784072008-05-30 00:40:33 +0000515void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
516 if (Node->getBase()) {
517 PrintExpr(Node->getBase());
518 OS << ".";
519 }
Steve Naroffc77a6362008-12-04 16:24:46 +0000520 OS << Node->getProperty()->getNameAsCString();
Steve Naroffae784072008-05-30 00:40:33 +0000521}
522
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000523void StmtPrinter::VisitObjCImplicitSetterGetterRefExpr(
524 ObjCImplicitSetterGetterRefExpr *Node) {
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000525 if (Node->getBase()) {
526 PrintExpr(Node->getBase());
527 OS << ".";
528 }
Fariborz Jahanian154440e2009-08-18 20:50:23 +0000529 if (Node->getGetterMethod())
530 OS << Node->getGetterMethod()->getNameAsString();
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000532}
533
Chris Lattnerd9f69102008-08-10 01:53:14 +0000534void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson22742662007-07-21 05:21:51 +0000535 switch (Node->getIdentType()) {
536 default:
537 assert(0 && "unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000538 case PredefinedExpr::Func:
Anders Carlsson22742662007-07-21 05:21:51 +0000539 OS << "__func__";
540 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000541 case PredefinedExpr::Function:
Anders Carlsson22742662007-07-21 05:21:51 +0000542 OS << "__FUNCTION__";
543 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000544 case PredefinedExpr::PrettyFunction:
Anders Carlsson22742662007-07-21 05:21:51 +0000545 OS << "__PRETTY_FUNCTION__";
546 break;
547 }
548}
549
Reid Spencer5f016e22007-07-11 17:01:13 +0000550void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000551 unsigned value = Node->getValue();
Chris Lattnerc250aae2008-06-07 22:35:38 +0000552 if (Node->isWide())
553 OS << "L";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000554 switch (value) {
555 case '\\':
556 OS << "'\\\\'";
557 break;
558 case '\'':
559 OS << "'\\''";
560 break;
561 case '\a':
562 // TODO: K&R: the meaning of '\\a' is different in traditional C
563 OS << "'\\a'";
564 break;
565 case '\b':
566 OS << "'\\b'";
567 break;
568 // Nonstandard escape sequence.
569 /*case '\e':
570 OS << "'\\e'";
571 break;*/
572 case '\f':
573 OS << "'\\f'";
574 break;
575 case '\n':
576 OS << "'\\n'";
577 break;
578 case '\r':
579 OS << "'\\r'";
580 break;
581 case '\t':
582 OS << "'\\t'";
583 break;
584 case '\v':
585 OS << "'\\v'";
586 break;
587 default:
Ted Kremenek471733d2008-02-23 00:52:04 +0000588 if (value < 256 && isprint(value)) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000589 OS << "'" << (char)value << "'";
590 } else if (value < 256) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000591 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000592 } else {
593 // FIXME what to really do here?
594 OS << value;
595 }
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000596 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000597}
598
599void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
600 bool isSigned = Node->getType()->isSignedIntegerType();
601 OS << Node->getValue().toString(10, isSigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Reid Spencer5f016e22007-07-11 17:01:13 +0000603 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall183700f2009-09-21 23:43:11 +0000604 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000605 default: assert(0 && "Unexpected type for integer literal!");
606 case BuiltinType::Int: break; // no suffix.
607 case BuiltinType::UInt: OS << 'U'; break;
608 case BuiltinType::Long: OS << 'L'; break;
609 case BuiltinType::ULong: OS << "UL"; break;
610 case BuiltinType::LongLong: OS << "LL"; break;
611 case BuiltinType::ULongLong: OS << "ULL"; break;
612 }
613}
614void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner86e499d2007-08-01 00:23:58 +0000615 // FIXME: print value more precisely.
Chris Lattnerda8249e2008-06-07 22:13:43 +0000616 OS << Node->getValueAsApproximateDouble();
Reid Spencer5f016e22007-07-11 17:01:13 +0000617}
Chris Lattner5d661452007-08-26 03:42:43 +0000618
619void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
620 PrintExpr(Node->getSubExpr());
621 OS << "i";
622}
623
Reid Spencer5f016e22007-07-11 17:01:13 +0000624void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
625 if (Str->isWide()) OS << 'L';
626 OS << '"';
Anders Carlssonee98ac52007-10-15 02:50:23 +0000627
Reid Spencer5f016e22007-07-11 17:01:13 +0000628 // FIXME: this doesn't print wstrings right.
629 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
Chris Lattner9a81c872009-01-16 19:25:18 +0000630 unsigned char Char = Str->getStrData()[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Chris Lattner9a81c872009-01-16 19:25:18 +0000632 switch (Char) {
633 default:
634 if (isprint(Char))
635 OS << (char)Char;
636 else // Output anything hard as an octal escape.
637 OS << '\\'
638 << (char)('0'+ ((Char >> 6) & 7))
639 << (char)('0'+ ((Char >> 3) & 7))
640 << (char)('0'+ ((Char >> 0) & 7));
641 break;
642 // Handle some common non-printable cases to make dumps prettier.
Reid Spencer5f016e22007-07-11 17:01:13 +0000643 case '\\': OS << "\\\\"; break;
644 case '"': OS << "\\\""; break;
645 case '\n': OS << "\\n"; break;
646 case '\t': OS << "\\t"; break;
647 case '\a': OS << "\\a"; break;
648 case '\b': OS << "\\b"; break;
649 }
650 }
651 OS << '"';
652}
653void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
654 OS << "(";
655 PrintExpr(Node->getSubExpr());
656 OS << ")";
657}
658void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +0000659 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000660 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Eli Friedman7df71ac2009-06-14 22:39:26 +0000662 // Print a space if this is an "identifier operator" like __real, or if
663 // it might be concatenated incorrectly like '+'.
Chris Lattner296bf192007-08-23 21:46:40 +0000664 switch (Node->getOpcode()) {
665 default: break;
Chris Lattner296bf192007-08-23 21:46:40 +0000666 case UnaryOperator::Real:
667 case UnaryOperator::Imag:
668 case UnaryOperator::Extension:
669 OS << ' ';
670 break;
Eli Friedman7df71ac2009-06-14 22:39:26 +0000671 case UnaryOperator::Plus:
672 case UnaryOperator::Minus:
673 if (isa<UnaryOperator>(Node->getSubExpr()))
674 OS << ' ';
675 break;
Chris Lattner296bf192007-08-23 21:46:40 +0000676 }
677 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000678 PrintExpr(Node->getSubExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Reid Spencer5f016e22007-07-11 17:01:13 +0000680 if (Node->isPostfix())
681 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +0000682}
Chris Lattner704fe352007-08-30 17:59:59 +0000683
684bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
Eli Friedman35183ac2009-02-27 06:44:11 +0000685 if (isa<UnaryOperator>(E)) {
Chris Lattner704fe352007-08-30 17:59:59 +0000686 // Base case, print the type and comma.
687 OS << E->getType().getAsString() << ", ";
688 return true;
689 } else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
690 PrintOffsetOfDesignator(ASE->getLHS());
691 OS << "[";
692 PrintExpr(ASE->getRHS());
693 OS << "]";
694 return false;
695 } else {
696 MemberExpr *ME = cast<MemberExpr>(E);
697 bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
Chris Lattner39f34e92008-11-24 04:00:27 +0000698 OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getNameAsString();
Chris Lattner704fe352007-08-30 17:59:59 +0000699 return false;
700 }
701}
702
703void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
704 OS << "__builtin_offsetof(";
705 PrintOffsetOfDesignator(Node->getSubExpr());
706 OS << ")";
707}
708
Sebastian Redl05189992008-11-11 17:56:53 +0000709void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
710 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
711 if (Node->isArgumentType())
712 OS << "(" << Node->getArgumentType().getAsString() << ")";
713 else {
714 OS << " ";
715 PrintExpr(Node->getArgumentExpr());
716 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000717}
718void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +0000719 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000720 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +0000721 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000722 OS << "]";
723}
724
725void StmtPrinter::VisitCallExpr(CallExpr *Call) {
726 PrintExpr(Call->getCallee());
727 OS << "(";
728 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattner04421082008-04-08 04:40:51 +0000729 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
730 // Don't print any defaulted arguments
731 break;
732 }
733
Reid Spencer5f016e22007-07-11 17:01:13 +0000734 if (i) OS << ", ";
735 PrintExpr(Call->getArg(i));
736 }
737 OS << ")";
738}
739void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregorb3eef682009-01-08 22:45:41 +0000740 // FIXME: Suppress printing implicit bases (like "this")
741 PrintExpr(Node->getBase());
Fariborz Jahanian97fd83a2010-01-11 21:17:32 +0000742 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
743 if (FD->isAnonymousStructOrUnion())
744 return;
Douglas Gregorb3eef682009-01-08 22:45:41 +0000745 OS << (Node->isArrow() ? "->" : ".");
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000746 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
747 Qualifier->print(OS, Policy);
748
Douglas Gregor86f19402008-12-20 23:49:58 +0000749 OS << Node->getMemberDecl()->getNameAsString();
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000751 if (Node->hasExplicitTemplateArgumentList())
752 OS << TemplateSpecializationType::PrintTemplateArgumentList(
753 Node->getTemplateArgs(),
754 Node->getNumTemplateArgs(),
755 Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000756}
Steve Narofff242b1b2009-07-24 17:54:45 +0000757void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
758 PrintExpr(Node->getBase());
759 OS << (Node->isArrow() ? "->isa" : ".isa");
760}
761
Nate Begeman213541a2008-04-18 23:10:10 +0000762void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +0000763 PrintExpr(Node->getBase());
764 OS << ".";
765 OS << Node->getAccessor().getName();
766}
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000767void StmtPrinter::VisitCastExpr(CastExpr *) {
768 assert(0 && "CastExpr is an abstract class");
769}
Douglas Gregor49badde2008-10-27 19:41:14 +0000770void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *) {
771 assert(0 && "ExplicitCastExpr is an abstract class");
772}
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000773void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000774 OS << "(" << Node->getType().getAsString() << ")";
Reid Spencer5f016e22007-07-11 17:01:13 +0000775 PrintExpr(Node->getSubExpr());
776}
Steve Naroffaff1edd2007-07-19 21:32:11 +0000777void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
778 OS << "(" << Node->getType().getAsString() << ")";
779 PrintExpr(Node->getInitializer());
780}
Steve Naroff49b45262007-07-13 16:58:59 +0000781void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroff90045e82007-07-13 23:32:42 +0000782 // No need to print anything, simply forward to the sub expression.
783 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +0000784}
Reid Spencer5f016e22007-07-11 17:01:13 +0000785void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
786 PrintExpr(Node->getLHS());
787 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
788 PrintExpr(Node->getRHS());
789}
Chris Lattnereb14fe82007-08-25 02:00:02 +0000790void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
791 PrintExpr(Node->getLHS());
792 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
793 PrintExpr(Node->getRHS());
794}
Reid Spencer5f016e22007-07-11 17:01:13 +0000795void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
796 PrintExpr(Node->getCond());
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Ted Kremenek8e911c42007-11-26 18:27:54 +0000798 if (Node->getLHS()) {
799 OS << " ? ";
800 PrintExpr(Node->getLHS());
801 OS << " : ";
802 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000803 else { // Handle GCC extension where LHS can be NULL.
Ted Kremenek8e911c42007-11-26 18:27:54 +0000804 OS << " ?: ";
805 }
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Reid Spencer5f016e22007-07-11 17:01:13 +0000807 PrintExpr(Node->getRHS());
808}
809
810// GNU extensions.
811
Chris Lattner6481a572007-08-03 17:31:20 +0000812void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000813 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000814}
815
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000816void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
817 OS << "(";
818 PrintRawCompoundStmt(E->getSubStmt());
819 OS << ")";
820}
821
Steve Naroffd34e9152007-08-01 22:05:33 +0000822void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
823 OS << "__builtin_types_compatible_p(";
824 OS << Node->getArgType1().getAsString() << ",";
825 OS << Node->getArgType2().getAsString() << ")";
826}
827
Steve Naroffd04fdd52007-08-03 21:21:27 +0000828void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
829 OS << "__builtin_choose_expr(";
830 PrintExpr(Node->getCond());
Chris Lattner94f05e32007-08-04 00:20:15 +0000831 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000832 PrintExpr(Node->getLHS());
Chris Lattner94f05e32007-08-04 00:20:15 +0000833 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000834 PrintExpr(Node->getRHS());
835 OS << ")";
836}
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000837
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000838void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
839 OS << "__null";
840}
841
Eli Friedmand38617c2008-05-14 19:38:39 +0000842void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
843 OS << "__builtin_shufflevector(";
844 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
845 if (i) OS << ", ";
846 PrintExpr(Node->getExpr(i));
847 }
848 OS << ")";
849}
850
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000851void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000852 if (Node->getSyntacticForm()) {
853 Visit(Node->getSyntacticForm());
854 return;
855 }
856
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000857 OS << "{ ";
858 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
859 if (i) OS << ", ";
Douglas Gregor4c678342009-01-28 21:54:33 +0000860 if (Node->getInit(i))
861 PrintExpr(Node->getInit(i));
862 else
863 OS << "0";
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000864 }
865 OS << " }";
866}
867
Nate Begeman2ef13e52009-08-10 23:49:36 +0000868void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
869 OS << "( ";
870 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
871 if (i) OS << ", ";
872 PrintExpr(Node->getExpr(i));
873 }
874 OS << " )";
875}
876
Douglas Gregor05c13a32009-01-22 00:58:24 +0000877void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000878 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
879 DEnd = Node->designators_end();
880 D != DEnd; ++D) {
881 if (D->isFieldDesignator()) {
882 if (D->getDotLoc().isInvalid())
883 OS << D->getFieldName()->getName() << ":";
884 else
885 OS << "." << D->getFieldName()->getName();
886 } else {
887 OS << "[";
888 if (D->isArrayDesignator()) {
889 PrintExpr(Node->getArrayIndex(*D));
890 } else {
891 PrintExpr(Node->getArrayRangeStart(*D));
892 OS << " ... ";
Mike Stump1eb44332009-09-09 15:08:12 +0000893 PrintExpr(Node->getArrayRangeEnd(*D));
Douglas Gregor4c678342009-01-28 21:54:33 +0000894 }
895 OS << "]";
896 }
897 }
898
899 OS << " = ";
900 PrintExpr(Node->getInit());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000901}
902
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000903void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Chris Lattnere4f21422009-06-30 01:26:17 +0000904 if (Policy.LangOpts.CPlusPlus)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000905 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()";
906 else {
907 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")";
908 if (Node->getType()->isRecordType())
909 OS << "{}";
910 else
911 OS << 0;
912 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000913}
914
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000915void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000916 OS << "__builtin_va_arg(";
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000917 PrintExpr(Node->getSubExpr());
918 OS << ", ";
919 OS << Node->getType().getAsString();
920 OS << ")";
921}
922
Reid Spencer5f016e22007-07-11 17:01:13 +0000923// C++
Douglas Gregorb4609802008-11-14 16:09:21 +0000924void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
925 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
926 "",
927#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
928 Spelling,
929#include "clang/Basic/OperatorKinds.def"
930 };
931
932 OverloadedOperatorKind Kind = Node->getOperator();
933 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
934 if (Node->getNumArgs() == 1) {
935 OS << OpStrings[Kind] << ' ';
936 PrintExpr(Node->getArg(0));
937 } else {
938 PrintExpr(Node->getArg(0));
939 OS << ' ' << OpStrings[Kind];
940 }
941 } else if (Kind == OO_Call) {
942 PrintExpr(Node->getArg(0));
943 OS << '(';
944 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
945 if (ArgIdx > 1)
946 OS << ", ";
947 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
948 PrintExpr(Node->getArg(ArgIdx));
949 }
950 OS << ')';
951 } else if (Kind == OO_Subscript) {
952 PrintExpr(Node->getArg(0));
953 OS << '[';
954 PrintExpr(Node->getArg(1));
955 OS << ']';
956 } else if (Node->getNumArgs() == 1) {
957 OS << OpStrings[Kind] << ' ';
958 PrintExpr(Node->getArg(0));
959 } else if (Node->getNumArgs() == 2) {
960 PrintExpr(Node->getArg(0));
961 OS << ' ' << OpStrings[Kind] << ' ';
962 PrintExpr(Node->getArg(1));
963 } else {
964 assert(false && "unknown overloaded operator");
965 }
966}
Reid Spencer5f016e22007-07-11 17:01:13 +0000967
Douglas Gregor88a35142008-12-22 05:46:06 +0000968void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
969 VisitCallExpr(cast<CallExpr>(Node));
970}
971
Douglas Gregor49badde2008-10-27 19:41:14 +0000972void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
973 OS << Node->getCastName() << '<';
974 OS << Node->getTypeAsWritten().getAsString() << ">(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000975 PrintExpr(Node->getSubExpr());
976 OS << ")";
977}
978
Douglas Gregor49badde2008-10-27 19:41:14 +0000979void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
980 VisitCXXNamedCastExpr(Node);
981}
982
983void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
984 VisitCXXNamedCastExpr(Node);
985}
986
987void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
988 VisitCXXNamedCastExpr(Node);
989}
990
991void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
992 VisitCXXNamedCastExpr(Node);
993}
994
Sebastian Redlc42e1182008-11-11 11:37:55 +0000995void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
996 OS << "typeid(";
997 if (Node->isTypeOperand()) {
998 OS << Node->getTypeOperand().getAsString();
999 } else {
1000 PrintExpr(Node->getExprOperand());
1001 }
1002 OS << ")";
1003}
1004
Reid Spencer5f016e22007-07-11 17:01:13 +00001005void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1006 OS << (Node->getValue() ? "true" : "false");
1007}
1008
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001009void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1010 OS << "nullptr";
1011}
1012
Douglas Gregor796da182008-11-04 14:32:21 +00001013void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1014 OS << "this";
1015}
1016
Chris Lattner50dd2892008-02-26 00:51:44 +00001017void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1018 if (Node->getSubExpr() == 0)
1019 OS << "throw";
1020 else {
1021 OS << "throw ";
1022 PrintExpr(Node->getSubExpr());
1023 }
1024}
1025
Chris Lattner04421082008-04-08 04:40:51 +00001026void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1027 // Nothing to print: we picked up the default argument
1028}
1029
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001030void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
1031 OS << Node->getType().getAsString();
1032 OS << "(";
1033 PrintExpr(Node->getSubExpr());
1034 OS << ")";
1035}
1036
Anders Carlssonfceb0a82009-05-30 20:03:25 +00001037void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1038 PrintExpr(Node->getSubExpr());
1039}
1040
Douglas Gregor506ae412009-01-16 18:33:17 +00001041void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
1042 OS << Node->getType().getAsString();
1043 OS << "(";
1044 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001045 ArgEnd = Node->arg_end();
Douglas Gregor506ae412009-01-16 18:33:17 +00001046 Arg != ArgEnd; ++Arg) {
1047 if (Arg != Node->arg_begin())
1048 OS << ", ";
1049 PrintExpr(*Arg);
1050 }
1051 OS << ")";
1052}
1053
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001054void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
1055 OS << Node->getType().getAsString() << "()";
1056}
1057
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001058void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1059 if (E->isGlobalNew())
1060 OS << "::";
1061 OS << "new ";
1062 unsigned NumPlace = E->getNumPlacementArgs();
1063 if (NumPlace > 0) {
1064 OS << "(";
1065 PrintExpr(E->getPlacementArg(0));
1066 for (unsigned i = 1; i < NumPlace; ++i) {
1067 OS << ", ";
1068 PrintExpr(E->getPlacementArg(i));
1069 }
1070 OS << ") ";
1071 }
1072 if (E->isParenTypeId())
1073 OS << "(";
Sebastian Redl6fec6482008-12-02 22:08:59 +00001074 std::string TypeS;
1075 if (Expr *Size = E->getArraySize()) {
1076 llvm::raw_string_ostream s(TypeS);
Eli Friedman6e1a3452009-05-30 05:32:46 +00001077 Size->printPretty(s, Context, Helper, Policy);
Sebastian Redl6fec6482008-12-02 22:08:59 +00001078 s.flush();
1079 TypeS = "[" + TypeS + "]";
1080 }
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001081 E->getAllocatedType().getAsStringInternal(TypeS, Policy);
Sebastian Redl6fec6482008-12-02 22:08:59 +00001082 OS << TypeS;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001083 if (E->isParenTypeId())
1084 OS << ")";
1085
1086 if (E->hasInitializer()) {
1087 OS << "(";
1088 unsigned NumCons = E->getNumConstructorArgs();
1089 if (NumCons > 0) {
1090 PrintExpr(E->getConstructorArg(0));
1091 for (unsigned i = 1; i < NumCons; ++i) {
1092 OS << ", ";
1093 PrintExpr(E->getConstructorArg(i));
1094 }
1095 }
1096 OS << ")";
1097 }
1098}
1099
1100void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1101 if (E->isGlobalDelete())
1102 OS << "::";
1103 OS << "delete ";
1104 if (E->isArrayForm())
1105 OS << "[] ";
1106 PrintExpr(E->getArgument());
1107}
1108
Douglas Gregora71d8192009-09-04 17:36:40 +00001109void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1110 PrintExpr(E->getBase());
1111 if (E->isArrow())
1112 OS << "->";
1113 else
1114 OS << '.';
1115 if (E->getQualifier())
1116 E->getQualifier()->print(OS, Policy);
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Douglas Gregora71d8192009-09-04 17:36:40 +00001118 std::string TypeS;
1119 E->getDestroyedType().getAsStringInternal(TypeS, Policy);
1120 OS << TypeS;
1121}
1122
Anders Carlssone349bea2009-04-23 02:32:43 +00001123void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Fariborz Jahanianc75da512010-01-13 21:41:11 +00001124 // FIXME. For now we just print a trivial constructor call expression,
1125 // constructing its first argument object.
1126 if (E->getNumArgs() == 1) {
1127 CXXConstructorDecl *CD = E->getConstructor();
1128 if (CD->isTrivial())
1129 PrintExpr(E->getArg(0));
1130 }
Anders Carlssone349bea2009-04-23 02:32:43 +00001131 // Nothing to print.
1132}
1133
Anders Carlsson2d44e8a2009-05-01 22:18:43 +00001134void StmtPrinter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
Anders Carlsson02bbfa32009-04-24 22:47:04 +00001135 // Just forward to the sub expression.
1136 PrintExpr(E->getSubExpr());
1137}
1138
Mike Stump1eb44332009-09-09 15:08:12 +00001139void
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001140StmtPrinter::VisitCXXUnresolvedConstructExpr(
1141 CXXUnresolvedConstructExpr *Node) {
1142 OS << Node->getTypeAsWritten().getAsString();
1143 OS << "(";
1144 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001145 ArgEnd = Node->arg_end();
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001146 Arg != ArgEnd; ++Arg) {
1147 if (Arg != Node->arg_begin())
1148 OS << ", ";
1149 PrintExpr(*Arg);
1150 }
1151 OS << ")";
1152}
1153
John McCall865d4472009-11-19 22:55:06 +00001154void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1155 CXXDependentScopeMemberExpr *Node) {
John McCallaa81e162009-12-01 22:10:20 +00001156 if (!Node->isImplicitAccess()) {
1157 PrintExpr(Node->getBase());
1158 OS << (Node->isArrow() ? "->" : ".");
1159 }
Douglas Gregora38c6872009-09-03 16:14:30 +00001160 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1161 Qualifier->print(OS, Policy);
John McCallaa81e162009-12-01 22:10:20 +00001162 else if (Node->hasExplicitTemplateArgs())
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001163 // FIXME: Track use of "template" keyword explicitly?
1164 OS << "template ";
Mike Stump1eb44332009-09-09 15:08:12 +00001165
Douglas Gregor1c0ca592009-05-22 21:13:27 +00001166 OS << Node->getMember().getAsString();
Mike Stump1eb44332009-09-09 15:08:12 +00001167
John McCallaa81e162009-12-01 22:10:20 +00001168 if (Node->hasExplicitTemplateArgs()) {
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001169 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1170 Node->getTemplateArgs(),
1171 Node->getNumTemplateArgs(),
1172 Policy);
1173 }
Douglas Gregor1c0ca592009-05-22 21:13:27 +00001174}
1175
John McCall129e2df2009-11-30 22:42:35 +00001176void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCallaa81e162009-12-01 22:10:20 +00001177 if (!Node->isImplicitAccess()) {
1178 PrintExpr(Node->getBase());
1179 OS << (Node->isArrow() ? "->" : ".");
1180 }
John McCall129e2df2009-11-30 22:42:35 +00001181 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1182 Qualifier->print(OS, Policy);
1183
1184 // FIXME: this might originally have been written with 'template'
1185
1186 OS << Node->getMemberName().getAsString();
1187
1188 if (Node->hasExplicitTemplateArgs()) {
1189 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1190 Node->getTemplateArgs(),
1191 Node->getNumTemplateArgs(),
1192 Policy);
1193 }
1194}
1195
Sebastian Redl64b45f72009-01-05 20:52:13 +00001196static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1197 switch (UTT) {
1198 default: assert(false && "Unknown type trait");
1199 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1200 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1201 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1202 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1203 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1204 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1205 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1206 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1207 case UTT_IsAbstract: return "__is_abstract";
1208 case UTT_IsClass: return "__is_class";
1209 case UTT_IsEmpty: return "__is_empty";
1210 case UTT_IsEnum: return "__is_enum";
1211 case UTT_IsPOD: return "__is_pod";
1212 case UTT_IsPolymorphic: return "__is_polymorphic";
1213 case UTT_IsUnion: return "__is_union";
1214 }
1215}
1216
1217void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1218 OS << getTypeTraitName(E->getTrait()) << "("
1219 << E->getQueriedType().getAsString() << ")";
1220}
1221
Mike Stump1eb44332009-09-09 15:08:12 +00001222// Obj-C
Anders Carlsson55085182007-08-21 17:43:55 +00001223
1224void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1225 OS << "@";
1226 VisitStringLiteral(Node->getString());
1227}
Reid Spencer5f016e22007-07-11 17:01:13 +00001228
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001229void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001230 OS << "@encode(" << Node->getEncodedType().getAsString() << ')';
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001231}
1232
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001233void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001234 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001235}
1236
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001237void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001238 OS << "@protocol(" << Node->getProtocol()->getNameAsString() << ')';
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001239}
1240
Steve Naroff563477d2007-09-18 23:55:05 +00001241void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1242 OS << "[";
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001243 Expr *receiver = Mess->getReceiver();
1244 if (receiver) PrintExpr(receiver);
1245 else OS << Mess->getClassName()->getName();
Ted Kremenekc29efd82008-05-02 17:32:38 +00001246 OS << ' ';
Ted Kremenek97b7f262008-04-16 04:30:16 +00001247 Selector selector = Mess->getSelector();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001248 if (selector.isUnarySelector()) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001249 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001250 } else {
1251 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001252 if (i < selector.getNumArgs()) {
1253 if (i > 0) OS << ' ';
1254 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner39f34e92008-11-24 04:00:27 +00001255 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremenekc29efd82008-05-02 17:32:38 +00001256 else
1257 OS << ":";
1258 }
1259 else OS << ", "; // Handle variadic methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001260
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001261 PrintExpr(Mess->getArg(i));
1262 }
Steve Naroff563477d2007-09-18 23:55:05 +00001263 }
1264 OS << "]";
1265}
1266
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001267void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) {
1268 OS << "super";
1269}
1270
Steve Naroff4eb206b2008-09-03 18:15:37 +00001271void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001272 BlockDecl *BD = Node->getBlockDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001273 OS << "^";
Mike Stump1eb44332009-09-09 15:08:12 +00001274
Steve Naroff4eb206b2008-09-03 18:15:37 +00001275 const FunctionType *AFT = Node->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +00001276
Douglas Gregor72564e72009-02-26 23:50:07 +00001277 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001278 OS << "()";
Douglas Gregor72564e72009-02-26 23:50:07 +00001279 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001280 OS << '(';
1281 std::string ParamStr;
Steve Naroff56ee6892008-10-08 17:01:13 +00001282 for (BlockDecl::param_iterator AI = BD->param_begin(),
1283 E = BD->param_end(); AI != E; ++AI) {
1284 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner39f34e92008-11-24 04:00:27 +00001285 ParamStr = (*AI)->getNameAsString();
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001286 (*AI)->getType().getAsStringInternal(ParamStr, Policy);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001287 OS << ParamStr;
1288 }
Mike Stump1eb44332009-09-09 15:08:12 +00001289
Douglas Gregor72564e72009-02-26 23:50:07 +00001290 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001291 if (FT->isVariadic()) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001292 if (!BD->param_empty()) OS << ", ";
Steve Naroff4eb206b2008-09-03 18:15:37 +00001293 OS << "...";
1294 }
1295 OS << ')';
1296 }
1297}
1298
Steve Naroff4eb206b2008-09-03 18:15:37 +00001299void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001300 OS << Node->getDecl()->getNameAsString();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001301}
Reid Spencer5f016e22007-07-11 17:01:13 +00001302//===----------------------------------------------------------------------===//
1303// Stmt method implementations
1304//===----------------------------------------------------------------------===//
1305
Eli Friedman48d14a22009-05-30 05:03:24 +00001306void Stmt::dumpPretty(ASTContext& Context) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00001307 printPretty(llvm::errs(), Context, 0,
1308 PrintingPolicy(Context.getLangOptions()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001309}
1310
Eli Friedman48d14a22009-05-30 05:03:24 +00001311void Stmt::printPretty(llvm::raw_ostream &OS, ASTContext& Context,
1312 PrinterHelper* Helper,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001313 const PrintingPolicy &Policy,
1314 unsigned Indentation) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001315 if (this == 0) {
1316 OS << "<NULL>";
1317 return;
1318 }
1319
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001320 if (Policy.Dump && &Context) {
Argyrios Kyrtzidisad42f062009-07-14 03:20:38 +00001321 dump(Context.getSourceManager());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001322 return;
1323 }
Mike Stump1eb44332009-09-09 15:08:12 +00001324
Eli Friedman48d14a22009-05-30 05:03:24 +00001325 StmtPrinter P(OS, Context, Helper, Policy, Indentation);
Chris Lattnerc5598cb2007-08-21 04:04:25 +00001326 P.Visit(const_cast<Stmt*>(this));
Reid Spencer5f016e22007-07-11 17:01:13 +00001327}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001328
1329//===----------------------------------------------------------------------===//
1330// PrinterHelper
1331//===----------------------------------------------------------------------===//
1332
1333// Implement virtual destructor.
Gabor Greif84675832007-09-11 15:32:40 +00001334PrinterHelper::~PrinterHelper() {}