blob: 583a7a95a49adaf3127869b7ecc96fa0f31fe1bf [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"
Douglas Gregor8ecdb652010-04-28 22:16:22 +000020#include "clang/AST/Expr.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// StmtPrinter Visitor
25//===----------------------------------------------------------------------===//
26
27namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +000028 class StmtPrinter : public StmtVisitor<StmtPrinter> {
Ted Kremeneka95d3752008-09-13 05:16:45 +000029 llvm::raw_ostream &OS;
Eli Friedman48d14a22009-05-30 05:03:24 +000030 ASTContext &Context;
Reid Spencer5f016e22007-07-11 17:01:13 +000031 unsigned IndentLevel;
Ted Kremenek42a509f2007-08-31 21:30:12 +000032 clang::PrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000033 PrintingPolicy Policy;
34
Reid Spencer5f016e22007-07-11 17:01:13 +000035 public:
Mike Stump1eb44332009-09-09 15:08:12 +000036 StmtPrinter(llvm::raw_ostream &os, ASTContext &C, PrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +000037 const PrintingPolicy &Policy,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000038 unsigned Indentation = 0)
Eli Friedman48d14a22009-05-30 05:03:24 +000039 : OS(os), Context(C), IndentLevel(Indentation), Helper(helper),
40 Policy(Policy) {}
Mike Stump1eb44332009-09-09 15:08:12 +000041
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000042 void PrintStmt(Stmt *S) {
43 PrintStmt(S, Policy.Indentation);
44 }
45
46 void PrintStmt(Stmt *S, int SubIndent) {
Reid Spencer5f016e22007-07-11 17:01:13 +000047 IndentLevel += SubIndent;
48 if (S && isa<Expr>(S)) {
49 // If this is an expr used in a stmt context, indent and newline it.
50 Indent();
Chris Lattnerc5598cb2007-08-21 04:04:25 +000051 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000052 OS << ";\n";
53 } else if (S) {
Chris Lattnerc5598cb2007-08-21 04:04:25 +000054 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000055 } else {
56 Indent() << "<<<NULL STATEMENT>>>\n";
57 }
58 IndentLevel -= SubIndent;
59 }
Eli Friedmandb23b152009-05-30 00:19:54 +000060
Reid Spencer5f016e22007-07-11 17:01:13 +000061 void PrintRawCompoundStmt(CompoundStmt *S);
62 void PrintRawDecl(Decl *D);
Ted Kremenekecd64c52008-10-06 18:39:36 +000063 void PrintRawDeclStmt(DeclStmt *S);
Reid Spencer5f016e22007-07-11 17:01:13 +000064 void PrintRawIfStmt(IfStmt *If);
Sebastian Redl8351da02008-12-22 21:35:02 +000065 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
Mike Stump1eb44332009-09-09 15:08:12 +000066
Reid Spencer5f016e22007-07-11 17:01:13 +000067 void PrintExpr(Expr *E) {
68 if (E)
Chris Lattnerc5598cb2007-08-21 04:04:25 +000069 Visit(E);
Reid Spencer5f016e22007-07-11 17:01:13 +000070 else
71 OS << "<null expr>";
72 }
Mike Stump1eb44332009-09-09 15:08:12 +000073
Mike Stump071e4da2009-02-10 20:16:46 +000074 llvm::raw_ostream &Indent(int Delta = 0) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000075 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
76 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +000077 return OS;
78 }
Mike Stump1eb44332009-09-09 15:08:12 +000079
Mike Stump1eb44332009-09-09 15:08:12 +000080 void Visit(Stmt* S) {
Ted Kremenek42a509f2007-08-31 21:30:12 +000081 if (Helper && Helper->handledStmt(S,OS))
82 return;
83 else StmtVisitor<StmtPrinter>::Visit(S);
84 }
Argyrios Kyrtzidis78e4cc72010-08-15 01:15:33 +000085
86 void VisitStmt(Stmt *Node) ATTRIBUTE_UNUSED {
87 Indent() << "<<unknown stmt type>>\n";
88 }
89 void VisitExpr(Expr *Node) ATTRIBUTE_UNUSED {
90 OS << "<<unknown expr type>>";
91 }
92 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Mike Stump1eb44332009-09-09 15:08:12 +000093
Argyrios Kyrtzidis78e4cc72010-08-15 01:15:33 +000094#define ABSTRACT_STMT(CLASS)
Douglas Gregorf2cad862008-11-14 12:46:07 +000095#define STMT(CLASS, PARENT) \
Chris Lattnerc5598cb2007-08-21 04:04:25 +000096 void Visit##CLASS(CLASS *Node);
Sean Hunt4bfe1962010-05-05 15:24:00 +000097#include "clang/AST/StmtNodes.inc"
Reid Spencer5f016e22007-07-11 17:01:13 +000098 };
99}
100
101//===----------------------------------------------------------------------===//
102// Stmt printing methods.
103//===----------------------------------------------------------------------===//
104
Reid Spencer5f016e22007-07-11 17:01:13 +0000105/// 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);
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 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;
Mike Stump1eb44332009-09-09 15:08:12 +0000123 for ( ; Begin != End; ++Begin)
Eli Friedman42f42c02009-05-30 04:20:30 +0000124 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";
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 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 << ')';
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 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 }
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 if (Stmt *Else = If->getElse()) {
183 OS << "else";
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 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 << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 // 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 }
Mike Stump1eb44332009-09-09 15:08:12 +0000241
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 << ") ";
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Chris Lattner8bdcc472007-09-15 21:49:37 +0000267 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 << ") ";
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000286 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 ";
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Anders Carlsson39c47b52007-11-23 23:12:25 +0000327 if (Node->isVolatile())
328 OS << "volatile ";
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Anders Carlsson39c47b52007-11-23 23:12:25 +0000330 OS << "(";
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000331 VisitStringLiteral(Node->getAsmString());
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Anders Carlssonb235fc22007-11-22 01:36:19 +0000333 // Outputs
334 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
335 Node->getNumClobbers() != 0)
336 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Anders Carlssonb235fc22007-11-22 01:36:19 +0000338 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
339 if (i != 0)
340 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Anders Carlssonb235fc22007-11-22 01:36:19 +0000342 if (!Node->getOutputName(i).empty()) {
343 OS << '[';
344 OS << Node->getOutputName(i);
345 OS << "] ";
346 }
Mike Stump1eb44332009-09-09 15:08:12 +0000347
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 }
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Anders Carlssonb235fc22007-11-22 01:36:19 +0000353 // Inputs
354 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
355 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Anders Carlssonb235fc22007-11-22 01:36:19 +0000357 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
358 if (i != 0)
359 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Anders Carlssonb235fc22007-11-22 01:36:19 +0000361 if (!Node->getInputName(i).empty()) {
362 OS << '[';
363 OS << Node->getInputName(i);
364 OS << "] ";
365 }
Mike Stump1eb44332009-09-09 15:08:12 +0000366
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 }
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Anders Carlssonb235fc22007-11-22 01:36:19 +0000372 // Clobbers
373 if (Node->getNumClobbers() != 0)
374 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Anders Carlssonb235fc22007-11-22 01:36:19 +0000376 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
377 if (i != 0)
378 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Anders Carlssonb235fc22007-11-22 01:36:19 +0000380 VisitStringLiteral(Node->getClobber(i));
381 }
Mike Stump1eb44332009-09-09 15:08:12 +0000382
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 }
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000393 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
394 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000395 Indent() << "@catch(";
Steve Naroff7ba138a2009-03-03 19:52:17 +0000396 if (catchStmt->getCatchParamDecl()) {
397 if (Decl *DS = catchStmt->getCatchParamDecl())
398 PrintRawDecl(DS);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000399 }
400 OS << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000401 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
402 PrintRawCompoundStmt(CS);
403 OS << "\n";
404 }
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000405 }
Mike Stump1eb44332009-09-09 15:08:12 +0000406
407 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
408 Node->getFinallyStmt())) {
Fariborz Jahanian1e7eab42007-11-07 00:46:42 +0000409 Indent() << "@finally";
410 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000411 OS << "\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000412 }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000413}
414
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000415void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000416}
417
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000418void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000419 Indent() << "@catch (...) { /* todo */ } \n";
420}
421
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000422void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000423 Indent() << "@throw";
424 if (Node->getThrowExpr()) {
425 OS << " ";
426 PrintExpr(Node->getThrowExpr());
427 }
428 OS << ";\n";
429}
430
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000431void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000432 Indent() << "@synchronized (";
433 PrintExpr(Node->getSynchExpr());
434 OS << ")";
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000435 PrintRawCompoundStmt(Node->getSynchBody());
436 OS << "\n";
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000437}
438
Sebastian Redl8351da02008-12-22 21:35:02 +0000439void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
440 OS << "catch (";
Sebastian Redl4b07b292008-12-22 19:15:10 +0000441 if (Decl *ExDecl = Node->getExceptionDecl())
442 PrintRawDecl(ExDecl);
443 else
444 OS << "...";
445 OS << ") ";
446 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl8351da02008-12-22 21:35:02 +0000447}
448
449void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
450 Indent();
451 PrintRawCXXCatchStmt(Node);
452 OS << "\n";
453}
454
455void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
456 Indent() << "try ";
457 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump1eb44332009-09-09 15:08:12 +0000458 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl8351da02008-12-22 21:35:02 +0000459 OS << " ";
460 PrintRawCXXCatchStmt(Node->getHandler(i));
461 }
Sebastian Redl4b07b292008-12-22 19:15:10 +0000462 OS << "\n";
463}
464
Reid Spencer5f016e22007-07-11 17:01:13 +0000465//===----------------------------------------------------------------------===//
466// Expr printing methods.
467//===----------------------------------------------------------------------===//
468
Reid Spencer5f016e22007-07-11 17:01:13 +0000469void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000470 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
471 Qualifier->print(OS, Policy);
Abramo Bagnara25777432010-08-11 22:01:17 +0000472 OS << Node->getNameInfo();
John McCall096832c2010-08-19 23:49:38 +0000473 if (Node->hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000474 OS << TemplateSpecializationType::PrintTemplateArgumentList(
475 Node->getTemplateArgs(),
476 Node->getNumTemplateArgs(),
Sean Huntc3021132010-05-05 15:23:54 +0000477 Policy);
Douglas Gregor1a49af92009-01-06 05:10:23 +0000478}
479
John McCall865d4472009-11-19 22:55:06 +0000480void StmtPrinter::VisitDependentScopeDeclRefExpr(
481 DependentScopeDeclRefExpr *Node) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000482 Node->getQualifier()->print(OS, Policy);
Abramo Bagnara25777432010-08-11 22:01:17 +0000483 OS << Node->getNameInfo();
John McCallf7a1a742009-11-24 19:00:30 +0000484 if (Node->hasExplicitTemplateArgs())
485 OS << TemplateSpecializationType::PrintTemplateArgumentList(
486 Node->getTemplateArgs(),
487 Node->getNumTemplateArgs(),
488 Policy);
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000489}
490
John McCallba135432009-11-21 08:51:07 +0000491void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000492 if (Node->getQualifier())
493 Node->getQualifier()->print(OS, Policy);
Abramo Bagnara25777432010-08-11 22:01:17 +0000494 OS << Node->getNameInfo();
John McCallf7a1a742009-11-24 19:00:30 +0000495 if (Node->hasExplicitTemplateArgs())
496 OS << TemplateSpecializationType::PrintTemplateArgumentList(
497 Node->getTemplateArgs(),
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000498 Node->getNumTemplateArgs(),
John McCallf7a1a742009-11-24 19:00:30 +0000499 Policy);
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000500}
501
Steve Naroff7779db42007-11-12 14:29:37 +0000502void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000503 if (Node->getBase()) {
504 PrintExpr(Node->getBase());
505 OS << (Node->isArrow() ? "->" : ".");
506 }
Benjamin Kramer900fc632010-04-17 09:33:03 +0000507 OS << Node->getDecl();
Steve Naroff7779db42007-11-12 14:29:37 +0000508}
509
Steve Naroffae784072008-05-30 00:40:33 +0000510void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
511 if (Node->getBase()) {
512 PrintExpr(Node->getBase());
513 OS << ".";
514 }
Daniel Dunbar4087f272010-08-17 22:39:59 +0000515 OS << Node->getProperty()->getName();
Steve Naroffae784072008-05-30 00:40:33 +0000516}
517
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000518void StmtPrinter::VisitObjCImplicitSetterGetterRefExpr(
519 ObjCImplicitSetterGetterRefExpr *Node) {
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000520 if (Node->getBase()) {
521 PrintExpr(Node->getBase());
522 OS << ".";
523 }
Fariborz Jahanian154440e2009-08-18 20:50:23 +0000524 if (Node->getGetterMethod())
Benjamin Kramer900fc632010-04-17 09:33:03 +0000525 OS << Node->getGetterMethod();
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000527}
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);
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Reid Spencer5f016e22007-07-11 17:01:13 +0000598 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall183700f2009-09-21 23:43:11 +0000599 switch (Node->getType()->getAs<BuiltinType>()->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.
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +0000624 llvm::StringRef StrData = Str->getString();
625 for (llvm::StringRef::iterator I = StrData.begin(), E = StrData.end();
626 I != E; ++I) {
627 unsigned char Char = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000628
Chris Lattner9a81c872009-01-16 19:25:18 +0000629 switch (Char) {
630 default:
631 if (isprint(Char))
632 OS << (char)Char;
633 else // Output anything hard as an octal escape.
634 OS << '\\'
635 << (char)('0'+ ((Char >> 6) & 7))
636 << (char)('0'+ ((Char >> 3) & 7))
637 << (char)('0'+ ((Char >> 0) & 7));
638 break;
639 // Handle some common non-printable cases to make dumps prettier.
Reid Spencer5f016e22007-07-11 17:01:13 +0000640 case '\\': OS << "\\\\"; break;
641 case '"': OS << "\\\""; break;
642 case '\n': OS << "\\n"; break;
643 case '\t': OS << "\\t"; break;
644 case '\a': OS << "\\a"; break;
645 case '\b': OS << "\\b"; break;
646 }
647 }
648 OS << '"';
649}
650void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
651 OS << "(";
652 PrintExpr(Node->getSubExpr());
653 OS << ")";
654}
655void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +0000656 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000657 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Eli Friedman7df71ac2009-06-14 22:39:26 +0000659 // Print a space if this is an "identifier operator" like __real, or if
660 // it might be concatenated incorrectly like '+'.
Chris Lattner296bf192007-08-23 21:46:40 +0000661 switch (Node->getOpcode()) {
662 default: break;
Chris Lattner296bf192007-08-23 21:46:40 +0000663 case UnaryOperator::Real:
664 case UnaryOperator::Imag:
665 case UnaryOperator::Extension:
666 OS << ' ';
667 break;
Eli Friedman7df71ac2009-06-14 22:39:26 +0000668 case UnaryOperator::Plus:
669 case UnaryOperator::Minus:
670 if (isa<UnaryOperator>(Node->getSubExpr()))
671 OS << ' ';
672 break;
Chris Lattner296bf192007-08-23 21:46:40 +0000673 }
674 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 PrintExpr(Node->getSubExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Reid Spencer5f016e22007-07-11 17:01:13 +0000677 if (Node->isPostfix())
678 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +0000679}
Chris Lattner704fe352007-08-30 17:59:59 +0000680
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000681void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
682 OS << "__builtin_offsetof(";
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000683 OS << Node->getTypeSourceInfo()->getType().getAsString(Policy) << ", ";
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000684 bool PrintedSomething = false;
685 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
686 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
687 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
688 // Array node
689 OS << "[";
690 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
691 OS << "]";
692 PrintedSomething = true;
693 continue;
694 }
Douglas Gregorcc8a5d52010-04-29 00:18:15 +0000695
696 // Skip implicit base indirections.
697 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
698 continue;
699
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000700 // Field or identifier node.
701 IdentifierInfo *Id = ON.getFieldName();
702 if (!Id)
703 continue;
Sean Huntc3021132010-05-05 15:23:54 +0000704
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000705 if (PrintedSomething)
706 OS << ".";
707 else
708 PrintedSomething = true;
Sean Huntc3021132010-05-05 15:23:54 +0000709 OS << Id->getName();
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000710 }
711 OS << ")";
712}
713
Sebastian Redl05189992008-11-11 17:56:53 +0000714void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
715 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
716 if (Node->isArgumentType())
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000717 OS << "(" << Node->getArgumentType().getAsString(Policy) << ")";
Sebastian Redl05189992008-11-11 17:56:53 +0000718 else {
719 OS << " ";
720 PrintExpr(Node->getArgumentExpr());
721 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000722}
723void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +0000724 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000725 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +0000726 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000727 OS << "]";
728}
729
730void StmtPrinter::VisitCallExpr(CallExpr *Call) {
731 PrintExpr(Call->getCallee());
732 OS << "(";
733 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattner04421082008-04-08 04:40:51 +0000734 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
735 // Don't print any defaulted arguments
736 break;
737 }
738
Reid Spencer5f016e22007-07-11 17:01:13 +0000739 if (i) OS << ", ";
740 PrintExpr(Call->getArg(i));
741 }
742 OS << ")";
743}
744void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregorb3eef682009-01-08 22:45:41 +0000745 // FIXME: Suppress printing implicit bases (like "this")
746 PrintExpr(Node->getBase());
Fariborz Jahanian97fd83a2010-01-11 21:17:32 +0000747 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
748 if (FD->isAnonymousStructOrUnion())
749 return;
Douglas Gregorb3eef682009-01-08 22:45:41 +0000750 OS << (Node->isArrow() ? "->" : ".");
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000751 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
752 Qualifier->print(OS, Policy);
753
Abramo Bagnara25777432010-08-11 22:01:17 +0000754 OS << Node->getMemberNameInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000755
John McCall096832c2010-08-19 23:49:38 +0000756 if (Node->hasExplicitTemplateArgs())
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000757 OS << TemplateSpecializationType::PrintTemplateArgumentList(
758 Node->getTemplateArgs(),
759 Node->getNumTemplateArgs(),
760 Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000761}
Steve Narofff242b1b2009-07-24 17:54:45 +0000762void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
763 PrintExpr(Node->getBase());
764 OS << (Node->isArrow() ? "->isa" : ".isa");
765}
766
Nate Begeman213541a2008-04-18 23:10:10 +0000767void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +0000768 PrintExpr(Node->getBase());
769 OS << ".";
770 OS << Node->getAccessor().getName();
771}
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000772void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Fariborz Jahanian03e80e42010-06-30 16:31:08 +0000773 OS << "(" << Node->getType().getAsString(Policy) << ")";
Reid Spencer5f016e22007-07-11 17:01:13 +0000774 PrintExpr(Node->getSubExpr());
775}
Steve Naroffaff1edd2007-07-19 21:32:11 +0000776void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000777 OS << "(" << Node->getType().getAsString(Policy) << ")";
Steve Naroffaff1edd2007-07-19 21:32:11 +0000778 PrintExpr(Node->getInitializer());
779}
Steve Naroff49b45262007-07-13 16:58:59 +0000780void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroff90045e82007-07-13 23:32:42 +0000781 // No need to print anything, simply forward to the sub expression.
782 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +0000783}
Reid Spencer5f016e22007-07-11 17:01:13 +0000784void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
785 PrintExpr(Node->getLHS());
786 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
787 PrintExpr(Node->getRHS());
788}
Chris Lattnereb14fe82007-08-25 02:00:02 +0000789void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
790 PrintExpr(Node->getLHS());
791 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
792 PrintExpr(Node->getRHS());
793}
Reid Spencer5f016e22007-07-11 17:01:13 +0000794void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
795 PrintExpr(Node->getCond());
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Ted Kremenek8e911c42007-11-26 18:27:54 +0000797 if (Node->getLHS()) {
798 OS << " ? ";
799 PrintExpr(Node->getLHS());
800 OS << " : ";
801 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000802 else { // Handle GCC extension where LHS can be NULL.
Ted Kremenek8e911c42007-11-26 18:27:54 +0000803 OS << " ?: ";
804 }
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Reid Spencer5f016e22007-07-11 17:01:13 +0000806 PrintExpr(Node->getRHS());
807}
808
809// GNU extensions.
810
Chris Lattner6481a572007-08-03 17:31:20 +0000811void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000812 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000813}
814
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000815void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
816 OS << "(";
817 PrintRawCompoundStmt(E->getSubStmt());
818 OS << ")";
819}
820
Steve Naroffd34e9152007-08-01 22:05:33 +0000821void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
822 OS << "__builtin_types_compatible_p(";
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000823 OS << Node->getArgType1().getAsString(Policy) << ",";
824 OS << Node->getArgType2().getAsString(Policy) << ")";
Steve Naroffd34e9152007-08-01 22:05:33 +0000825}
826
Steve Naroffd04fdd52007-08-03 21:21:27 +0000827void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
828 OS << "__builtin_choose_expr(";
829 PrintExpr(Node->getCond());
Chris Lattner94f05e32007-08-04 00:20:15 +0000830 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000831 PrintExpr(Node->getLHS());
Chris Lattner94f05e32007-08-04 00:20:15 +0000832 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000833 PrintExpr(Node->getRHS());
834 OS << ")";
835}
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000836
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000837void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
838 OS << "__null";
839}
840
Eli Friedmand38617c2008-05-14 19:38:39 +0000841void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
842 OS << "__builtin_shufflevector(";
843 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
844 if (i) OS << ", ";
845 PrintExpr(Node->getExpr(i));
846 }
847 OS << ")";
848}
849
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000850void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000851 if (Node->getSyntacticForm()) {
852 Visit(Node->getSyntacticForm());
853 return;
854 }
855
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000856 OS << "{ ";
857 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
858 if (i) OS << ", ";
Douglas Gregor4c678342009-01-28 21:54:33 +0000859 if (Node->getInit(i))
860 PrintExpr(Node->getInit(i));
861 else
862 OS << "0";
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000863 }
864 OS << " }";
865}
866
Nate Begeman2ef13e52009-08-10 23:49:36 +0000867void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
868 OS << "( ";
869 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
870 if (i) OS << ", ";
871 PrintExpr(Node->getExpr(i));
872 }
873 OS << " )";
874}
875
Douglas Gregor05c13a32009-01-22 00:58:24 +0000876void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000877 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
878 DEnd = Node->designators_end();
879 D != DEnd; ++D) {
880 if (D->isFieldDesignator()) {
881 if (D->getDotLoc().isInvalid())
882 OS << D->getFieldName()->getName() << ":";
883 else
884 OS << "." << D->getFieldName()->getName();
885 } else {
886 OS << "[";
887 if (D->isArrayDesignator()) {
888 PrintExpr(Node->getArrayIndex(*D));
889 } else {
890 PrintExpr(Node->getArrayRangeStart(*D));
891 OS << " ... ";
Mike Stump1eb44332009-09-09 15:08:12 +0000892 PrintExpr(Node->getArrayRangeEnd(*D));
Douglas Gregor4c678342009-01-28 21:54:33 +0000893 }
894 OS << "]";
895 }
896 }
897
898 OS << " = ";
899 PrintExpr(Node->getInit());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000900}
901
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000902void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Chris Lattnere4f21422009-06-30 01:26:17 +0000903 if (Policy.LangOpts.CPlusPlus)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000904 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()";
905 else {
906 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")";
907 if (Node->getType()->isRecordType())
908 OS << "{}";
909 else
910 OS << 0;
911 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000912}
913
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000914void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000915 OS << "__builtin_va_arg(";
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000916 PrintExpr(Node->getSubExpr());
917 OS << ", ";
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000918 OS << Node->getType().getAsString(Policy);
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000919 OS << ")";
920}
921
Reid Spencer5f016e22007-07-11 17:01:13 +0000922// C++
Douglas Gregorb4609802008-11-14 16:09:21 +0000923void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
924 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
925 "",
926#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
927 Spelling,
928#include "clang/Basic/OperatorKinds.def"
929 };
930
931 OverloadedOperatorKind Kind = Node->getOperator();
932 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
933 if (Node->getNumArgs() == 1) {
934 OS << OpStrings[Kind] << ' ';
935 PrintExpr(Node->getArg(0));
936 } else {
937 PrintExpr(Node->getArg(0));
938 OS << ' ' << OpStrings[Kind];
939 }
940 } else if (Kind == OO_Call) {
941 PrintExpr(Node->getArg(0));
942 OS << '(';
943 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
944 if (ArgIdx > 1)
945 OS << ", ";
946 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
947 PrintExpr(Node->getArg(ArgIdx));
948 }
949 OS << ')';
950 } else if (Kind == OO_Subscript) {
951 PrintExpr(Node->getArg(0));
952 OS << '[';
953 PrintExpr(Node->getArg(1));
954 OS << ']';
955 } else if (Node->getNumArgs() == 1) {
956 OS << OpStrings[Kind] << ' ';
957 PrintExpr(Node->getArg(0));
958 } else if (Node->getNumArgs() == 2) {
959 PrintExpr(Node->getArg(0));
960 OS << ' ' << OpStrings[Kind] << ' ';
961 PrintExpr(Node->getArg(1));
962 } else {
963 assert(false && "unknown overloaded operator");
964 }
965}
Reid Spencer5f016e22007-07-11 17:01:13 +0000966
Douglas Gregor88a35142008-12-22 05:46:06 +0000967void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
968 VisitCallExpr(cast<CallExpr>(Node));
969}
970
Douglas Gregor49badde2008-10-27 19:41:14 +0000971void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
972 OS << Node->getCastName() << '<';
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000973 OS << Node->getTypeAsWritten().getAsString(Policy) << ">(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000974 PrintExpr(Node->getSubExpr());
975 OS << ")";
976}
977
Douglas Gregor49badde2008-10-27 19:41:14 +0000978void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
979 VisitCXXNamedCastExpr(Node);
980}
981
982void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
983 VisitCXXNamedCastExpr(Node);
984}
985
986void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
987 VisitCXXNamedCastExpr(Node);
988}
989
990void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
991 VisitCXXNamedCastExpr(Node);
992}
993
Sebastian Redlc42e1182008-11-11 11:37:55 +0000994void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
995 OS << "typeid(";
996 if (Node->isTypeOperand()) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000997 OS << Node->getTypeOperand().getAsString(Policy);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000998 } else {
999 PrintExpr(Node->getExprOperand());
1000 }
1001 OS << ")";
1002}
1003
Reid Spencer5f016e22007-07-11 17:01:13 +00001004void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1005 OS << (Node->getValue() ? "true" : "false");
1006}
1007
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001008void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1009 OS << "nullptr";
1010}
1011
Douglas Gregor796da182008-11-04 14:32:21 +00001012void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1013 OS << "this";
1014}
1015
Chris Lattner50dd2892008-02-26 00:51:44 +00001016void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1017 if (Node->getSubExpr() == 0)
1018 OS << "throw";
1019 else {
1020 OS << "throw ";
1021 PrintExpr(Node->getSubExpr());
1022 }
1023}
1024
Chris Lattner04421082008-04-08 04:40:51 +00001025void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1026 // Nothing to print: we picked up the default argument
1027}
1028
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001029void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001030 OS << Node->getType().getAsString(Policy);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001031 OS << "(";
1032 PrintExpr(Node->getSubExpr());
1033 OS << ")";
1034}
1035
Anders Carlssonfceb0a82009-05-30 20:03:25 +00001036void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1037 PrintExpr(Node->getSubExpr());
1038}
1039
Anders Carlssoneb60edf2010-01-29 02:39:32 +00001040void StmtPrinter::VisitCXXBindReferenceExpr(CXXBindReferenceExpr *Node) {
1041 PrintExpr(Node->getSubExpr());
1042}
1043
Douglas Gregor506ae412009-01-16 18:33:17 +00001044void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001045 OS << Node->getType().getAsString(Policy);
Douglas Gregor506ae412009-01-16 18:33:17 +00001046 OS << "(";
1047 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001048 ArgEnd = Node->arg_end();
Douglas Gregor506ae412009-01-16 18:33:17 +00001049 Arg != ArgEnd; ++Arg) {
1050 if (Arg != Node->arg_begin())
1051 OS << ", ";
1052 PrintExpr(*Arg);
1053 }
1054 OS << ")";
1055}
1056
Douglas Gregored8abf12010-07-08 06:14:04 +00001057void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001058 OS << Node->getType().getAsString(Policy) << "()";
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001059}
1060
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001061void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1062 if (E->isGlobalNew())
1063 OS << "::";
1064 OS << "new ";
1065 unsigned NumPlace = E->getNumPlacementArgs();
1066 if (NumPlace > 0) {
1067 OS << "(";
1068 PrintExpr(E->getPlacementArg(0));
1069 for (unsigned i = 1; i < NumPlace; ++i) {
1070 OS << ", ";
1071 PrintExpr(E->getPlacementArg(i));
1072 }
1073 OS << ") ";
1074 }
1075 if (E->isParenTypeId())
1076 OS << "(";
Sebastian Redl6fec6482008-12-02 22:08:59 +00001077 std::string TypeS;
1078 if (Expr *Size = E->getArraySize()) {
1079 llvm::raw_string_ostream s(TypeS);
Eli Friedman6e1a3452009-05-30 05:32:46 +00001080 Size->printPretty(s, Context, Helper, Policy);
Sebastian Redl6fec6482008-12-02 22:08:59 +00001081 s.flush();
1082 TypeS = "[" + TypeS + "]";
1083 }
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001084 E->getAllocatedType().getAsStringInternal(TypeS, Policy);
Sebastian Redl6fec6482008-12-02 22:08:59 +00001085 OS << TypeS;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001086 if (E->isParenTypeId())
1087 OS << ")";
1088
1089 if (E->hasInitializer()) {
1090 OS << "(";
1091 unsigned NumCons = E->getNumConstructorArgs();
1092 if (NumCons > 0) {
1093 PrintExpr(E->getConstructorArg(0));
1094 for (unsigned i = 1; i < NumCons; ++i) {
1095 OS << ", ";
1096 PrintExpr(E->getConstructorArg(i));
1097 }
1098 }
1099 OS << ")";
1100 }
1101}
1102
1103void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1104 if (E->isGlobalDelete())
1105 OS << "::";
1106 OS << "delete ";
1107 if (E->isArrayForm())
1108 OS << "[] ";
1109 PrintExpr(E->getArgument());
1110}
1111
Douglas Gregora71d8192009-09-04 17:36:40 +00001112void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1113 PrintExpr(E->getBase());
1114 if (E->isArrow())
1115 OS << "->";
1116 else
1117 OS << '.';
1118 if (E->getQualifier())
1119 E->getQualifier()->print(OS, Policy);
Mike Stump1eb44332009-09-09 15:08:12 +00001120
Douglas Gregora71d8192009-09-04 17:36:40 +00001121 std::string TypeS;
Douglas Gregora2e7dd22010-02-25 01:56:36 +00001122 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1123 OS << II->getName();
1124 else
1125 E->getDestroyedType().getAsStringInternal(TypeS, Policy);
Douglas Gregora71d8192009-09-04 17:36:40 +00001126 OS << TypeS;
1127}
1128
Anders Carlssone349bea2009-04-23 02:32:43 +00001129void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Fariborz Jahanianc75da512010-01-13 21:41:11 +00001130 // FIXME. For now we just print a trivial constructor call expression,
1131 // constructing its first argument object.
1132 if (E->getNumArgs() == 1) {
1133 CXXConstructorDecl *CD = E->getConstructor();
1134 if (CD->isTrivial())
1135 PrintExpr(E->getArg(0));
1136 }
Anders Carlssone349bea2009-04-23 02:32:43 +00001137 // Nothing to print.
1138}
1139
Anders Carlsson2d44e8a2009-05-01 22:18:43 +00001140void StmtPrinter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
Anders Carlsson02bbfa32009-04-24 22:47:04 +00001141 // Just forward to the sub expression.
1142 PrintExpr(E->getSubExpr());
1143}
1144
Mike Stump1eb44332009-09-09 15:08:12 +00001145void
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001146StmtPrinter::VisitCXXUnresolvedConstructExpr(
1147 CXXUnresolvedConstructExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001148 OS << Node->getTypeAsWritten().getAsString(Policy);
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001149 OS << "(";
1150 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001151 ArgEnd = Node->arg_end();
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001152 Arg != ArgEnd; ++Arg) {
1153 if (Arg != Node->arg_begin())
1154 OS << ", ";
1155 PrintExpr(*Arg);
1156 }
1157 OS << ")";
1158}
1159
John McCall865d4472009-11-19 22:55:06 +00001160void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1161 CXXDependentScopeMemberExpr *Node) {
John McCallaa81e162009-12-01 22:10:20 +00001162 if (!Node->isImplicitAccess()) {
1163 PrintExpr(Node->getBase());
1164 OS << (Node->isArrow() ? "->" : ".");
1165 }
Douglas Gregora38c6872009-09-03 16:14:30 +00001166 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1167 Qualifier->print(OS, Policy);
John McCallaa81e162009-12-01 22:10:20 +00001168 else if (Node->hasExplicitTemplateArgs())
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001169 // FIXME: Track use of "template" keyword explicitly?
1170 OS << "template ";
Mike Stump1eb44332009-09-09 15:08:12 +00001171
Abramo Bagnara25777432010-08-11 22:01:17 +00001172 OS << Node->getMemberNameInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00001173
John McCallaa81e162009-12-01 22:10:20 +00001174 if (Node->hasExplicitTemplateArgs()) {
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001175 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1176 Node->getTemplateArgs(),
1177 Node->getNumTemplateArgs(),
1178 Policy);
1179 }
Douglas Gregor1c0ca592009-05-22 21:13:27 +00001180}
1181
John McCall129e2df2009-11-30 22:42:35 +00001182void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCallaa81e162009-12-01 22:10:20 +00001183 if (!Node->isImplicitAccess()) {
1184 PrintExpr(Node->getBase());
1185 OS << (Node->isArrow() ? "->" : ".");
1186 }
John McCall129e2df2009-11-30 22:42:35 +00001187 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1188 Qualifier->print(OS, Policy);
1189
1190 // FIXME: this might originally have been written with 'template'
1191
Abramo Bagnara25777432010-08-11 22:01:17 +00001192 OS << Node->getMemberNameInfo();
John McCall129e2df2009-11-30 22:42:35 +00001193
1194 if (Node->hasExplicitTemplateArgs()) {
1195 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1196 Node->getTemplateArgs(),
1197 Node->getNumTemplateArgs(),
1198 Policy);
1199 }
1200}
1201
Sebastian Redl64b45f72009-01-05 20:52:13 +00001202static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1203 switch (UTT) {
1204 default: assert(false && "Unknown type trait");
1205 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1206 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1207 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1208 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1209 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1210 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1211 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1212 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1213 case UTT_IsAbstract: return "__is_abstract";
1214 case UTT_IsClass: return "__is_class";
1215 case UTT_IsEmpty: return "__is_empty";
1216 case UTT_IsEnum: return "__is_enum";
1217 case UTT_IsPOD: return "__is_pod";
1218 case UTT_IsPolymorphic: return "__is_polymorphic";
1219 case UTT_IsUnion: return "__is_union";
1220 }
1221}
1222
1223void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1224 OS << getTypeTraitName(E->getTrait()) << "("
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001225 << E->getQueriedType().getAsString(Policy) << ")";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001226}
1227
Mike Stump1eb44332009-09-09 15:08:12 +00001228// Obj-C
Anders Carlsson55085182007-08-21 17:43:55 +00001229
1230void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1231 OS << "@";
1232 VisitStringLiteral(Node->getString());
1233}
Reid Spencer5f016e22007-07-11 17:01:13 +00001234
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001235void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001236 OS << "@encode(" << Node->getEncodedType().getAsString(Policy) << ')';
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001237}
1238
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001239void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001240 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001241}
1242
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001243void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramer900fc632010-04-17 09:33:03 +00001244 OS << "@protocol(" << Node->getProtocol() << ')';
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001245}
1246
Steve Naroff563477d2007-09-18 23:55:05 +00001247void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1248 OS << "[";
Douglas Gregor04badcf2010-04-21 00:45:42 +00001249 switch (Mess->getReceiverKind()) {
1250 case ObjCMessageExpr::Instance:
1251 PrintExpr(Mess->getInstanceReceiver());
1252 break;
1253
1254 case ObjCMessageExpr::Class:
1255 OS << Mess->getClassReceiver().getAsString(Policy);
1256 break;
1257
1258 case ObjCMessageExpr::SuperInstance:
1259 case ObjCMessageExpr::SuperClass:
1260 OS << "Super";
1261 break;
1262 }
1263
Ted Kremenekc29efd82008-05-02 17:32:38 +00001264 OS << ' ';
Ted Kremenek97b7f262008-04-16 04:30:16 +00001265 Selector selector = Mess->getSelector();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001266 if (selector.isUnarySelector()) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001267 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001268 } else {
1269 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001270 if (i < selector.getNumArgs()) {
1271 if (i > 0) OS << ' ';
1272 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner39f34e92008-11-24 04:00:27 +00001273 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremenekc29efd82008-05-02 17:32:38 +00001274 else
1275 OS << ":";
1276 }
1277 else OS << ", "; // Handle variadic methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001278
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001279 PrintExpr(Mess->getArg(i));
1280 }
Steve Naroff563477d2007-09-18 23:55:05 +00001281 }
1282 OS << "]";
1283}
1284
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001285void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) {
1286 OS << "super";
1287}
1288
Steve Naroff4eb206b2008-09-03 18:15:37 +00001289void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001290 BlockDecl *BD = Node->getBlockDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001291 OS << "^";
Mike Stump1eb44332009-09-09 15:08:12 +00001292
Steve Naroff4eb206b2008-09-03 18:15:37 +00001293 const FunctionType *AFT = Node->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +00001294
Douglas Gregor72564e72009-02-26 23:50:07 +00001295 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001296 OS << "()";
Douglas Gregor72564e72009-02-26 23:50:07 +00001297 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001298 OS << '(';
1299 std::string ParamStr;
Steve Naroff56ee6892008-10-08 17:01:13 +00001300 for (BlockDecl::param_iterator AI = BD->param_begin(),
1301 E = BD->param_end(); AI != E; ++AI) {
1302 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner39f34e92008-11-24 04:00:27 +00001303 ParamStr = (*AI)->getNameAsString();
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001304 (*AI)->getType().getAsStringInternal(ParamStr, Policy);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001305 OS << ParamStr;
1306 }
Mike Stump1eb44332009-09-09 15:08:12 +00001307
Douglas Gregor72564e72009-02-26 23:50:07 +00001308 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001309 if (FT->isVariadic()) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001310 if (!BD->param_empty()) OS << ", ";
Steve Naroff4eb206b2008-09-03 18:15:37 +00001311 OS << "...";
1312 }
1313 OS << ')';
1314 }
1315}
1316
Steve Naroff4eb206b2008-09-03 18:15:37 +00001317void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Benjamin Kramer900fc632010-04-17 09:33:03 +00001318 OS << Node->getDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001319}
Reid Spencer5f016e22007-07-11 17:01:13 +00001320//===----------------------------------------------------------------------===//
1321// Stmt method implementations
1322//===----------------------------------------------------------------------===//
1323
Eli Friedman48d14a22009-05-30 05:03:24 +00001324void Stmt::dumpPretty(ASTContext& Context) const {
Argyrios Kyrtzidis96680332010-08-09 10:54:31 +00001325 printPretty(llvm::errs(), Context, 0,
Chris Lattnere4f21422009-06-30 01:26:17 +00001326 PrintingPolicy(Context.getLangOptions()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001327}
1328
Eli Friedman48d14a22009-05-30 05:03:24 +00001329void Stmt::printPretty(llvm::raw_ostream &OS, ASTContext& Context,
1330 PrinterHelper* Helper,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001331 const PrintingPolicy &Policy,
1332 unsigned Indentation) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001333 if (this == 0) {
1334 OS << "<NULL>";
1335 return;
1336 }
1337
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001338 if (Policy.Dump && &Context) {
Argyrios Kyrtzidis96680332010-08-09 10:54:31 +00001339 dump(OS, Context.getSourceManager());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001340 return;
1341 }
Mike Stump1eb44332009-09-09 15:08:12 +00001342
Eli Friedman48d14a22009-05-30 05:03:24 +00001343 StmtPrinter P(OS, Context, Helper, Policy, Indentation);
Chris Lattnerc5598cb2007-08-21 04:04:25 +00001344 P.Visit(const_cast<Stmt*>(this));
Reid Spencer5f016e22007-07-11 17:01:13 +00001345}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001346
1347//===----------------------------------------------------------------------===//
1348// PrinterHelper
1349//===----------------------------------------------------------------------===//
1350
1351// Implement virtual destructor.
Gabor Greif84675832007-09-11 15:32:40 +00001352PrinterHelper::~PrinterHelper() {}