blob: 2fc08dabf883797c887f5b30107726f3eafe6b73 [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"
Douglas Gregorab6677e2010-09-08 00:15:04 +000021#include "clang/AST/ExprCXX.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// StmtPrinter Visitor
26//===----------------------------------------------------------------------===//
27
28namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +000029 class StmtPrinter : public StmtVisitor<StmtPrinter> {
Ted Kremeneka95d3752008-09-13 05:16:45 +000030 llvm::raw_ostream &OS;
Eli Friedman48d14a22009-05-30 05:03:24 +000031 ASTContext &Context;
Reid Spencer5f016e22007-07-11 17:01:13 +000032 unsigned IndentLevel;
Ted Kremenek42a509f2007-08-31 21:30:12 +000033 clang::PrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000034 PrintingPolicy Policy;
35
Reid Spencer5f016e22007-07-11 17:01:13 +000036 public:
Mike Stump1eb44332009-09-09 15:08:12 +000037 StmtPrinter(llvm::raw_ostream &os, ASTContext &C, PrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +000038 const PrintingPolicy &Policy,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000039 unsigned Indentation = 0)
Eli Friedman48d14a22009-05-30 05:03:24 +000040 : OS(os), Context(C), IndentLevel(Indentation), Helper(helper),
41 Policy(Policy) {}
Mike Stump1eb44332009-09-09 15:08:12 +000042
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000043 void PrintStmt(Stmt *S) {
44 PrintStmt(S, Policy.Indentation);
45 }
46
47 void PrintStmt(Stmt *S, int SubIndent) {
Reid Spencer5f016e22007-07-11 17:01:13 +000048 IndentLevel += SubIndent;
49 if (S && isa<Expr>(S)) {
50 // If this is an expr used in a stmt context, indent and newline it.
51 Indent();
Chris Lattnerc5598cb2007-08-21 04:04:25 +000052 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000053 OS << ";\n";
54 } else if (S) {
Chris Lattnerc5598cb2007-08-21 04:04:25 +000055 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000056 } else {
57 Indent() << "<<<NULL STATEMENT>>>\n";
58 }
59 IndentLevel -= SubIndent;
60 }
Eli Friedmandb23b152009-05-30 00:19:54 +000061
Reid Spencer5f016e22007-07-11 17:01:13 +000062 void PrintRawCompoundStmt(CompoundStmt *S);
63 void PrintRawDecl(Decl *D);
Ted Kremenekecd64c52008-10-06 18:39:36 +000064 void PrintRawDeclStmt(DeclStmt *S);
Reid Spencer5f016e22007-07-11 17:01:13 +000065 void PrintRawIfStmt(IfStmt *If);
Sebastian Redl8351da02008-12-22 21:35:02 +000066 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
Mike Stump1eb44332009-09-09 15:08:12 +000067
Reid Spencer5f016e22007-07-11 17:01:13 +000068 void PrintExpr(Expr *E) {
69 if (E)
Chris Lattnerc5598cb2007-08-21 04:04:25 +000070 Visit(E);
Reid Spencer5f016e22007-07-11 17:01:13 +000071 else
72 OS << "<null expr>";
73 }
Mike Stump1eb44332009-09-09 15:08:12 +000074
Mike Stump071e4da2009-02-10 20:16:46 +000075 llvm::raw_ostream &Indent(int Delta = 0) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000076 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
77 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +000078 return OS;
79 }
Mike Stump1eb44332009-09-09 15:08:12 +000080
Mike Stump1eb44332009-09-09 15:08:12 +000081 void Visit(Stmt* S) {
Ted Kremenek42a509f2007-08-31 21:30:12 +000082 if (Helper && Helper->handledStmt(S,OS))
83 return;
84 else StmtVisitor<StmtPrinter>::Visit(S);
85 }
Argyrios Kyrtzidis78e4cc72010-08-15 01:15:33 +000086
87 void VisitStmt(Stmt *Node) ATTRIBUTE_UNUSED {
88 Indent() << "<<unknown stmt type>>\n";
89 }
90 void VisitExpr(Expr *Node) ATTRIBUTE_UNUSED {
91 OS << "<<unknown expr type>>";
92 }
93 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Mike Stump1eb44332009-09-09 15:08:12 +000094
Argyrios Kyrtzidis78e4cc72010-08-15 01:15:33 +000095#define ABSTRACT_STMT(CLASS)
Douglas Gregorf2cad862008-11-14 12:46:07 +000096#define STMT(CLASS, PARENT) \
Chris Lattnerc5598cb2007-08-21 04:04:25 +000097 void Visit##CLASS(CLASS *Node);
Sean Hunt4bfe1962010-05-05 15:24:00 +000098#include "clang/AST/StmtNodes.inc"
Reid Spencer5f016e22007-07-11 17:01:13 +000099 };
100}
101
102//===----------------------------------------------------------------------===//
103// Stmt printing methods.
104//===----------------------------------------------------------------------===//
105
Reid Spencer5f016e22007-07-11 17:01:13 +0000106/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
107/// with no newline after the }.
108void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
109 OS << "{\n";
110 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
111 I != E; ++I)
112 PrintStmt(*I);
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 Indent() << "}";
115}
116
117void StmtPrinter::PrintRawDecl(Decl *D) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000118 D->print(OS, Policy, IndentLevel);
Mike Stump071e4da2009-02-10 20:16:46 +0000119}
120
Ted Kremenekecd64c52008-10-06 18:39:36 +0000121void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
Eli Friedmandb23b152009-05-30 00:19:54 +0000122 DeclStmt::decl_iterator Begin = S->decl_begin(), End = S->decl_end();
Eli Friedman42f42c02009-05-30 04:20:30 +0000123 llvm::SmallVector<Decl*, 2> Decls;
Mike Stump1eb44332009-09-09 15:08:12 +0000124 for ( ; Begin != End; ++Begin)
Eli Friedman42f42c02009-05-30 04:20:30 +0000125 Decls.push_back(*Begin);
Eli Friedmandb23b152009-05-30 00:19:54 +0000126
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000127 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
Ted Kremenekecd64c52008-10-06 18:39:36 +0000128}
Reid Spencer5f016e22007-07-11 17:01:13 +0000129
130void StmtPrinter::VisitNullStmt(NullStmt *Node) {
131 Indent() << ";\n";
132}
133
134void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Eli Friedmandb23b152009-05-30 00:19:54 +0000135 Indent();
136 PrintRawDeclStmt(Node);
137 OS << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000138}
139
140void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
141 Indent();
142 PrintRawCompoundStmt(Node);
143 OS << "\n";
144}
145
146void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
147 Indent(-1) << "case ";
148 PrintExpr(Node->getLHS());
149 if (Node->getRHS()) {
150 OS << " ... ";
151 PrintExpr(Node->getRHS());
152 }
153 OS << ":\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Reid Spencer5f016e22007-07-11 17:01:13 +0000155 PrintStmt(Node->getSubStmt(), 0);
156}
157
158void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
159 Indent(-1) << "default:\n";
160 PrintStmt(Node->getSubStmt(), 0);
161}
162
163void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
164 Indent(-1) << Node->getName() << ":\n";
165 PrintStmt(Node->getSubStmt(), 0);
166}
167
168void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000169 OS << "if (";
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 PrintExpr(If->getCond());
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000171 OS << ')';
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
174 OS << ' ';
175 PrintRawCompoundStmt(CS);
176 OS << (If->getElse() ? ' ' : '\n');
177 } else {
178 OS << '\n';
179 PrintStmt(If->getThen());
180 if (If->getElse()) Indent();
181 }
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Reid Spencer5f016e22007-07-11 17:01:13 +0000183 if (Stmt *Else = If->getElse()) {
184 OS << "else";
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
187 OS << ' ';
188 PrintRawCompoundStmt(CS);
189 OS << '\n';
190 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
191 OS << ' ';
192 PrintRawIfStmt(ElseIf);
193 } else {
194 OS << '\n';
195 PrintStmt(If->getElse());
196 }
197 }
198}
199
200void StmtPrinter::VisitIfStmt(IfStmt *If) {
201 Indent();
202 PrintRawIfStmt(If);
203}
204
205void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
206 Indent() << "switch (";
207 PrintExpr(Node->getCond());
208 OS << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 // Pretty print compoundstmt bodies (very common).
211 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
212 OS << " ";
213 PrintRawCompoundStmt(CS);
214 OS << "\n";
215 } else {
216 OS << "\n";
217 PrintStmt(Node->getBody());
218 }
219}
220
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000221void StmtPrinter::VisitSwitchCase(SwitchCase*) {
222 assert(0 && "SwitchCase is an abstract class");
223}
224
Reid Spencer5f016e22007-07-11 17:01:13 +0000225void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
226 Indent() << "while (";
227 PrintExpr(Node->getCond());
228 OS << ")\n";
229 PrintStmt(Node->getBody());
230}
231
232void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner8bdcc472007-09-15 21:49:37 +0000233 Indent() << "do ";
234 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
235 PrintRawCompoundStmt(CS);
236 OS << " ";
237 } else {
238 OS << "\n";
239 PrintStmt(Node->getBody());
240 Indent();
241 }
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Eli Friedmanb3e22962009-05-17 01:05:34 +0000243 OS << "while (";
Reid Spencer5f016e22007-07-11 17:01:13 +0000244 PrintExpr(Node->getCond());
Eli Friedmanb3e22962009-05-17 01:05:34 +0000245 OS << ");\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000246}
247
248void StmtPrinter::VisitForStmt(ForStmt *Node) {
249 Indent() << "for (";
250 if (Node->getInit()) {
251 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000252 PrintRawDeclStmt(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 else
254 PrintExpr(cast<Expr>(Node->getInit()));
255 }
Chris Lattner8bdcc472007-09-15 21:49:37 +0000256 OS << ";";
257 if (Node->getCond()) {
258 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000259 PrintExpr(Node->getCond());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000260 }
261 OS << ";";
262 if (Node->getInc()) {
263 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 PrintExpr(Node->getInc());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000265 }
266 OS << ") ";
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Chris Lattner8bdcc472007-09-15 21:49:37 +0000268 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
269 PrintRawCompoundStmt(CS);
270 OS << "\n";
271 } else {
272 OS << "\n";
273 PrintStmt(Node->getBody());
274 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000275}
276
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000277void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000278 Indent() << "for (";
279 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000280 PrintRawDeclStmt(DS);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000281 else
282 PrintExpr(cast<Expr>(Node->getElement()));
283 OS << " in ";
284 PrintExpr(Node->getCollection());
285 OS << ") ";
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000287 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
288 PrintRawCompoundStmt(CS);
289 OS << "\n";
290 } else {
291 OS << "\n";
292 PrintStmt(Node->getBody());
293 }
294}
295
Reid Spencer5f016e22007-07-11 17:01:13 +0000296void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
297 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
298}
299
300void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
301 Indent() << "goto *";
302 PrintExpr(Node->getTarget());
303 OS << ";\n";
304}
305
306void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
307 Indent() << "continue;\n";
308}
309
310void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
311 Indent() << "break;\n";
312}
313
314
315void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
316 Indent() << "return";
317 if (Node->getRetValue()) {
318 OS << " ";
319 PrintExpr(Node->getRetValue());
320 }
321 OS << ";\n";
322}
323
Chris Lattnerfe795952007-10-29 04:04:16 +0000324
325void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson39c47b52007-11-23 23:12:25 +0000326 Indent() << "asm ";
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Anders Carlsson39c47b52007-11-23 23:12:25 +0000328 if (Node->isVolatile())
329 OS << "volatile ";
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Anders Carlsson39c47b52007-11-23 23:12:25 +0000331 OS << "(";
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000332 VisitStringLiteral(Node->getAsmString());
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Anders Carlssonb235fc22007-11-22 01:36:19 +0000334 // Outputs
335 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
336 Node->getNumClobbers() != 0)
337 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Anders Carlssonb235fc22007-11-22 01:36:19 +0000339 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
340 if (i != 0)
341 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Anders Carlssonb235fc22007-11-22 01:36:19 +0000343 if (!Node->getOutputName(i).empty()) {
344 OS << '[';
345 OS << Node->getOutputName(i);
346 OS << "] ";
347 }
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Chris Lattnerb3277932009-03-10 04:59:06 +0000349 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000350 OS << " ";
351 Visit(Node->getOutputExpr(i));
352 }
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Anders Carlssonb235fc22007-11-22 01:36:19 +0000354 // Inputs
355 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
356 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Anders Carlssonb235fc22007-11-22 01:36:19 +0000358 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
359 if (i != 0)
360 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Anders Carlssonb235fc22007-11-22 01:36:19 +0000362 if (!Node->getInputName(i).empty()) {
363 OS << '[';
364 OS << Node->getInputName(i);
365 OS << "] ";
366 }
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Chris Lattnerb3277932009-03-10 04:59:06 +0000368 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000369 OS << " ";
370 Visit(Node->getInputExpr(i));
371 }
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Anders Carlssonb235fc22007-11-22 01:36:19 +0000373 // Clobbers
374 if (Node->getNumClobbers() != 0)
375 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Anders Carlssonb235fc22007-11-22 01:36:19 +0000377 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
378 if (i != 0)
379 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Anders Carlssonb235fc22007-11-22 01:36:19 +0000381 VisitStringLiteral(Node->getClobber(i));
382 }
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000384 OS << ");\n";
Chris Lattnerfe795952007-10-29 04:04:16 +0000385}
386
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000387void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000388 Indent() << "@try";
389 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
390 PrintRawCompoundStmt(TS);
391 OS << "\n";
392 }
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000394 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
395 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
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
Reid Spencer5f016e22007-07-11 17:01:13 +0000470void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000471 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
472 Qualifier->print(OS, Policy);
Abramo Bagnara25777432010-08-11 22:01:17 +0000473 OS << Node->getNameInfo();
John McCall096832c2010-08-19 23:49:38 +0000474 if (Node->hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000475 OS << TemplateSpecializationType::PrintTemplateArgumentList(
476 Node->getTemplateArgs(),
477 Node->getNumTemplateArgs(),
Sean Huntc3021132010-05-05 15:23:54 +0000478 Policy);
Douglas Gregor1a49af92009-01-06 05:10:23 +0000479}
480
John McCall865d4472009-11-19 22:55:06 +0000481void StmtPrinter::VisitDependentScopeDeclRefExpr(
482 DependentScopeDeclRefExpr *Node) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000483 Node->getQualifier()->print(OS, Policy);
Abramo Bagnara25777432010-08-11 22:01:17 +0000484 OS << Node->getNameInfo();
John McCallf7a1a742009-11-24 19:00:30 +0000485 if (Node->hasExplicitTemplateArgs())
486 OS << TemplateSpecializationType::PrintTemplateArgumentList(
487 Node->getTemplateArgs(),
488 Node->getNumTemplateArgs(),
489 Policy);
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000490}
491
John McCallba135432009-11-21 08:51:07 +0000492void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000493 if (Node->getQualifier())
494 Node->getQualifier()->print(OS, Policy);
Abramo Bagnara25777432010-08-11 22:01:17 +0000495 OS << Node->getNameInfo();
John McCallf7a1a742009-11-24 19:00:30 +0000496 if (Node->hasExplicitTemplateArgs())
497 OS << TemplateSpecializationType::PrintTemplateArgumentList(
498 Node->getTemplateArgs(),
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000499 Node->getNumTemplateArgs(),
John McCallf7a1a742009-11-24 19:00:30 +0000500 Policy);
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000501}
502
Steve Naroff7779db42007-11-12 14:29:37 +0000503void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000504 if (Node->getBase()) {
505 PrintExpr(Node->getBase());
506 OS << (Node->isArrow() ? "->" : ".");
507 }
Benjamin Kramer900fc632010-04-17 09:33:03 +0000508 OS << Node->getDecl();
Steve Naroff7779db42007-11-12 14:29:37 +0000509}
510
Steve Naroffae784072008-05-30 00:40:33 +0000511void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
512 if (Node->getBase()) {
513 PrintExpr(Node->getBase());
514 OS << ".";
515 }
Daniel Dunbar4087f272010-08-17 22:39:59 +0000516 OS << Node->getProperty()->getName();
Steve Naroffae784072008-05-30 00:40:33 +0000517}
518
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000519void StmtPrinter::VisitObjCImplicitSetterGetterRefExpr(
520 ObjCImplicitSetterGetterRefExpr *Node) {
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000521 if (Node->getBase()) {
522 PrintExpr(Node->getBase());
523 OS << ".";
524 }
Fariborz Jahanian154440e2009-08-18 20:50:23 +0000525 if (Node->getGetterMethod())
Benjamin Kramer900fc632010-04-17 09:33:03 +0000526 OS << Node->getGetterMethod();
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000528}
529
Chris Lattnerd9f69102008-08-10 01:53:14 +0000530void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson22742662007-07-21 05:21:51 +0000531 switch (Node->getIdentType()) {
532 default:
533 assert(0 && "unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000534 case PredefinedExpr::Func:
Anders Carlsson22742662007-07-21 05:21:51 +0000535 OS << "__func__";
536 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000537 case PredefinedExpr::Function:
Anders Carlsson22742662007-07-21 05:21:51 +0000538 OS << "__FUNCTION__";
539 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000540 case PredefinedExpr::PrettyFunction:
Anders Carlsson22742662007-07-21 05:21:51 +0000541 OS << "__PRETTY_FUNCTION__";
542 break;
543 }
544}
545
Reid Spencer5f016e22007-07-11 17:01:13 +0000546void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000547 unsigned value = Node->getValue();
Chris Lattnerc250aae2008-06-07 22:35:38 +0000548 if (Node->isWide())
549 OS << "L";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000550 switch (value) {
551 case '\\':
552 OS << "'\\\\'";
553 break;
554 case '\'':
555 OS << "'\\''";
556 break;
557 case '\a':
558 // TODO: K&R: the meaning of '\\a' is different in traditional C
559 OS << "'\\a'";
560 break;
561 case '\b':
562 OS << "'\\b'";
563 break;
564 // Nonstandard escape sequence.
565 /*case '\e':
566 OS << "'\\e'";
567 break;*/
568 case '\f':
569 OS << "'\\f'";
570 break;
571 case '\n':
572 OS << "'\\n'";
573 break;
574 case '\r':
575 OS << "'\\r'";
576 break;
577 case '\t':
578 OS << "'\\t'";
579 break;
580 case '\v':
581 OS << "'\\v'";
582 break;
583 default:
Ted Kremenek471733d2008-02-23 00:52:04 +0000584 if (value < 256 && isprint(value)) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000585 OS << "'" << (char)value << "'";
586 } else if (value < 256) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000587 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000588 } else {
589 // FIXME what to really do here?
590 OS << value;
591 }
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000592 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000593}
594
595void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
596 bool isSigned = Node->getType()->isSignedIntegerType();
597 OS << Node->getValue().toString(10, isSigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Reid Spencer5f016e22007-07-11 17:01:13 +0000599 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall183700f2009-09-21 23:43:11 +0000600 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000601 default: assert(0 && "Unexpected type for integer literal!");
602 case BuiltinType::Int: break; // no suffix.
603 case BuiltinType::UInt: OS << 'U'; break;
604 case BuiltinType::Long: OS << 'L'; break;
605 case BuiltinType::ULong: OS << "UL"; break;
606 case BuiltinType::LongLong: OS << "LL"; break;
607 case BuiltinType::ULongLong: OS << "ULL"; break;
608 }
609}
610void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner86e499d2007-08-01 00:23:58 +0000611 // FIXME: print value more precisely.
Chris Lattnerda8249e2008-06-07 22:13:43 +0000612 OS << Node->getValueAsApproximateDouble();
Reid Spencer5f016e22007-07-11 17:01:13 +0000613}
Chris Lattner5d661452007-08-26 03:42:43 +0000614
615void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
616 PrintExpr(Node->getSubExpr());
617 OS << "i";
618}
619
Reid Spencer5f016e22007-07-11 17:01:13 +0000620void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
621 if (Str->isWide()) OS << 'L';
622 OS << '"';
Anders Carlssonee98ac52007-10-15 02:50:23 +0000623
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 // FIXME: this doesn't print wstrings right.
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +0000625 llvm::StringRef StrData = Str->getString();
626 for (llvm::StringRef::iterator I = StrData.begin(), E = StrData.end();
627 I != E; ++I) {
628 unsigned char Char = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000629
Chris Lattner9a81c872009-01-16 19:25:18 +0000630 switch (Char) {
631 default:
632 if (isprint(Char))
633 OS << (char)Char;
634 else // Output anything hard as an octal escape.
635 OS << '\\'
636 << (char)('0'+ ((Char >> 6) & 7))
637 << (char)('0'+ ((Char >> 3) & 7))
638 << (char)('0'+ ((Char >> 0) & 7));
639 break;
640 // Handle some common non-printable cases to make dumps prettier.
Reid Spencer5f016e22007-07-11 17:01:13 +0000641 case '\\': OS << "\\\\"; break;
642 case '"': OS << "\\\""; break;
643 case '\n': OS << "\\n"; break;
644 case '\t': OS << "\\t"; break;
645 case '\a': OS << "\\a"; break;
646 case '\b': OS << "\\b"; break;
647 }
648 }
649 OS << '"';
650}
651void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
652 OS << "(";
653 PrintExpr(Node->getSubExpr());
654 OS << ")";
655}
656void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +0000657 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000658 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Eli Friedman7df71ac2009-06-14 22:39:26 +0000660 // Print a space if this is an "identifier operator" like __real, or if
661 // it might be concatenated incorrectly like '+'.
Chris Lattner296bf192007-08-23 21:46:40 +0000662 switch (Node->getOpcode()) {
663 default: break;
John McCall2de56d12010-08-25 11:45:40 +0000664 case UO_Real:
665 case UO_Imag:
666 case UO_Extension:
Chris Lattner296bf192007-08-23 21:46:40 +0000667 OS << ' ';
668 break;
John McCall2de56d12010-08-25 11:45:40 +0000669 case UO_Plus:
670 case UO_Minus:
Eli Friedman7df71ac2009-06-14 22:39:26 +0000671 if (isa<UnaryOperator>(Node->getSubExpr()))
672 OS << ' ';
673 break;
Chris Lattner296bf192007-08-23 21:46:40 +0000674 }
675 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000676 PrintExpr(Node->getSubExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000677
Reid Spencer5f016e22007-07-11 17:01:13 +0000678 if (Node->isPostfix())
679 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +0000680}
Chris Lattner704fe352007-08-30 17:59:59 +0000681
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000682void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
683 OS << "__builtin_offsetof(";
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000684 OS << Node->getTypeSourceInfo()->getType().getAsString(Policy) << ", ";
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000685 bool PrintedSomething = false;
686 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
687 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
688 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
689 // Array node
690 OS << "[";
691 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
692 OS << "]";
693 PrintedSomething = true;
694 continue;
695 }
Douglas Gregorcc8a5d52010-04-29 00:18:15 +0000696
697 // Skip implicit base indirections.
698 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
699 continue;
700
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000701 // Field or identifier node.
702 IdentifierInfo *Id = ON.getFieldName();
703 if (!Id)
704 continue;
Sean Huntc3021132010-05-05 15:23:54 +0000705
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000706 if (PrintedSomething)
707 OS << ".";
708 else
709 PrintedSomething = true;
Sean Huntc3021132010-05-05 15:23:54 +0000710 OS << Id->getName();
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000711 }
712 OS << ")";
713}
714
Sebastian Redl05189992008-11-11 17:56:53 +0000715void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
716 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
717 if (Node->isArgumentType())
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000718 OS << "(" << Node->getArgumentType().getAsString(Policy) << ")";
Sebastian Redl05189992008-11-11 17:56:53 +0000719 else {
720 OS << " ";
721 PrintExpr(Node->getArgumentExpr());
722 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000723}
724void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +0000725 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000726 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +0000727 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000728 OS << "]";
729}
730
731void StmtPrinter::VisitCallExpr(CallExpr *Call) {
732 PrintExpr(Call->getCallee());
733 OS << "(";
734 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattner04421082008-04-08 04:40:51 +0000735 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
736 // Don't print any defaulted arguments
737 break;
738 }
739
Reid Spencer5f016e22007-07-11 17:01:13 +0000740 if (i) OS << ", ";
741 PrintExpr(Call->getArg(i));
742 }
743 OS << ")";
744}
745void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregorb3eef682009-01-08 22:45:41 +0000746 // FIXME: Suppress printing implicit bases (like "this")
747 PrintExpr(Node->getBase());
Fariborz Jahanian97fd83a2010-01-11 21:17:32 +0000748 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
749 if (FD->isAnonymousStructOrUnion())
750 return;
Douglas Gregorb3eef682009-01-08 22:45:41 +0000751 OS << (Node->isArrow() ? "->" : ".");
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000752 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
753 Qualifier->print(OS, Policy);
754
Abramo Bagnara25777432010-08-11 22:01:17 +0000755 OS << Node->getMemberNameInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000756
John McCall096832c2010-08-19 23:49:38 +0000757 if (Node->hasExplicitTemplateArgs())
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000758 OS << TemplateSpecializationType::PrintTemplateArgumentList(
759 Node->getTemplateArgs(),
760 Node->getNumTemplateArgs(),
761 Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000762}
Steve Narofff242b1b2009-07-24 17:54:45 +0000763void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
764 PrintExpr(Node->getBase());
765 OS << (Node->isArrow() ? "->isa" : ".isa");
766}
767
Nate Begeman213541a2008-04-18 23:10:10 +0000768void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +0000769 PrintExpr(Node->getBase());
770 OS << ".";
771 OS << Node->getAccessor().getName();
772}
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000773void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Fariborz Jahanian03e80e42010-06-30 16:31:08 +0000774 OS << "(" << Node->getType().getAsString(Policy) << ")";
Reid Spencer5f016e22007-07-11 17:01:13 +0000775 PrintExpr(Node->getSubExpr());
776}
Steve Naroffaff1edd2007-07-19 21:32:11 +0000777void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000778 OS << "(" << Node->getType().getAsString(Policy) << ")";
Steve Naroffaff1edd2007-07-19 21:32:11 +0000779 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(";
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000824 OS << Node->getArgType1().getAsString(Policy) << ",";
825 OS << Node->getArgType2().getAsString(Policy) << ")";
Steve Naroffd34e9152007-08-01 22:05:33 +0000826}
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 << ", ";
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000919 OS << Node->getType().getAsString(Policy);
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000920 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() << '<';
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000974 OS << Node->getTypeAsWritten().getAsString(Policy) << ">(";
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()) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000998 OS << Node->getTypeOperand().getAsString(Policy);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000999 } else {
1000 PrintExpr(Node->getExprOperand());
1001 }
1002 OS << ")";
1003}
1004
Francois Pichet01b7c302010-09-08 12:20:18 +00001005void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1006 OS << "__uuidof(";
1007 if (Node->isTypeOperand()) {
1008 OS << Node->getTypeOperand().getAsString(Policy);
1009 } else {
1010 PrintExpr(Node->getExprOperand());
1011 }
1012 OS << ")";
1013}
1014
Reid Spencer5f016e22007-07-11 17:01:13 +00001015void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1016 OS << (Node->getValue() ? "true" : "false");
1017}
1018
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001019void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1020 OS << "nullptr";
1021}
1022
Douglas Gregor796da182008-11-04 14:32:21 +00001023void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1024 OS << "this";
1025}
1026
Chris Lattner50dd2892008-02-26 00:51:44 +00001027void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1028 if (Node->getSubExpr() == 0)
1029 OS << "throw";
1030 else {
1031 OS << "throw ";
1032 PrintExpr(Node->getSubExpr());
1033 }
1034}
1035
Chris Lattner04421082008-04-08 04:40:51 +00001036void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1037 // Nothing to print: we picked up the default argument
1038}
1039
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001040void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001041 OS << Node->getType().getAsString(Policy);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001042 OS << "(";
1043 PrintExpr(Node->getSubExpr());
1044 OS << ")";
1045}
1046
Anders Carlssonfceb0a82009-05-30 20:03:25 +00001047void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1048 PrintExpr(Node->getSubExpr());
1049}
1050
Douglas Gregor506ae412009-01-16 18:33:17 +00001051void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001052 OS << Node->getType().getAsString(Policy);
Douglas Gregor506ae412009-01-16 18:33:17 +00001053 OS << "(";
1054 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001055 ArgEnd = Node->arg_end();
Douglas Gregor506ae412009-01-16 18:33:17 +00001056 Arg != ArgEnd; ++Arg) {
1057 if (Arg != Node->arg_begin())
1058 OS << ", ";
1059 PrintExpr(*Arg);
1060 }
1061 OS << ")";
1062}
1063
Douglas Gregored8abf12010-07-08 06:14:04 +00001064void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregorab6677e2010-09-08 00:15:04 +00001065 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
1066 OS << TSInfo->getType().getAsString(Policy) << "()";
1067 else
1068 OS << Node->getType().getAsString(Policy) << "()";
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001069}
1070
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001071void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1072 if (E->isGlobalNew())
1073 OS << "::";
1074 OS << "new ";
1075 unsigned NumPlace = E->getNumPlacementArgs();
1076 if (NumPlace > 0) {
1077 OS << "(";
1078 PrintExpr(E->getPlacementArg(0));
1079 for (unsigned i = 1; i < NumPlace; ++i) {
1080 OS << ", ";
1081 PrintExpr(E->getPlacementArg(i));
1082 }
1083 OS << ") ";
1084 }
1085 if (E->isParenTypeId())
1086 OS << "(";
Sebastian Redl6fec6482008-12-02 22:08:59 +00001087 std::string TypeS;
1088 if (Expr *Size = E->getArraySize()) {
1089 llvm::raw_string_ostream s(TypeS);
Eli Friedman6e1a3452009-05-30 05:32:46 +00001090 Size->printPretty(s, Context, Helper, Policy);
Sebastian Redl6fec6482008-12-02 22:08:59 +00001091 s.flush();
1092 TypeS = "[" + TypeS + "]";
1093 }
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001094 E->getAllocatedType().getAsStringInternal(TypeS, Policy);
Sebastian Redl6fec6482008-12-02 22:08:59 +00001095 OS << TypeS;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001096 if (E->isParenTypeId())
1097 OS << ")";
1098
1099 if (E->hasInitializer()) {
1100 OS << "(";
1101 unsigned NumCons = E->getNumConstructorArgs();
1102 if (NumCons > 0) {
1103 PrintExpr(E->getConstructorArg(0));
1104 for (unsigned i = 1; i < NumCons; ++i) {
1105 OS << ", ";
1106 PrintExpr(E->getConstructorArg(i));
1107 }
1108 }
1109 OS << ")";
1110 }
1111}
1112
1113void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1114 if (E->isGlobalDelete())
1115 OS << "::";
1116 OS << "delete ";
1117 if (E->isArrayForm())
1118 OS << "[] ";
1119 PrintExpr(E->getArgument());
1120}
1121
Douglas Gregora71d8192009-09-04 17:36:40 +00001122void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1123 PrintExpr(E->getBase());
1124 if (E->isArrow())
1125 OS << "->";
1126 else
1127 OS << '.';
1128 if (E->getQualifier())
1129 E->getQualifier()->print(OS, Policy);
Mike Stump1eb44332009-09-09 15:08:12 +00001130
Douglas Gregora71d8192009-09-04 17:36:40 +00001131 std::string TypeS;
Douglas Gregora2e7dd22010-02-25 01:56:36 +00001132 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1133 OS << II->getName();
1134 else
1135 E->getDestroyedType().getAsStringInternal(TypeS, Policy);
Douglas Gregora71d8192009-09-04 17:36:40 +00001136 OS << TypeS;
1137}
1138
Anders Carlssone349bea2009-04-23 02:32:43 +00001139void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Fariborz Jahanianc75da512010-01-13 21:41:11 +00001140 // FIXME. For now we just print a trivial constructor call expression,
1141 // constructing its first argument object.
1142 if (E->getNumArgs() == 1) {
1143 CXXConstructorDecl *CD = E->getConstructor();
1144 if (CD->isTrivial())
1145 PrintExpr(E->getArg(0));
1146 }
Anders Carlssone349bea2009-04-23 02:32:43 +00001147 // Nothing to print.
1148}
1149
Anders Carlsson2d44e8a2009-05-01 22:18:43 +00001150void StmtPrinter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
Anders Carlsson02bbfa32009-04-24 22:47:04 +00001151 // Just forward to the sub expression.
1152 PrintExpr(E->getSubExpr());
1153}
1154
Mike Stump1eb44332009-09-09 15:08:12 +00001155void
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001156StmtPrinter::VisitCXXUnresolvedConstructExpr(
1157 CXXUnresolvedConstructExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001158 OS << Node->getTypeAsWritten().getAsString(Policy);
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001159 OS << "(";
1160 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001161 ArgEnd = Node->arg_end();
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001162 Arg != ArgEnd; ++Arg) {
1163 if (Arg != Node->arg_begin())
1164 OS << ", ";
1165 PrintExpr(*Arg);
1166 }
1167 OS << ")";
1168}
1169
John McCall865d4472009-11-19 22:55:06 +00001170void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1171 CXXDependentScopeMemberExpr *Node) {
John McCallaa81e162009-12-01 22:10:20 +00001172 if (!Node->isImplicitAccess()) {
1173 PrintExpr(Node->getBase());
1174 OS << (Node->isArrow() ? "->" : ".");
1175 }
Douglas Gregora38c6872009-09-03 16:14:30 +00001176 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1177 Qualifier->print(OS, Policy);
John McCallaa81e162009-12-01 22:10:20 +00001178 else if (Node->hasExplicitTemplateArgs())
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001179 // FIXME: Track use of "template" keyword explicitly?
1180 OS << "template ";
Mike Stump1eb44332009-09-09 15:08:12 +00001181
Abramo Bagnara25777432010-08-11 22:01:17 +00001182 OS << Node->getMemberNameInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00001183
John McCallaa81e162009-12-01 22:10:20 +00001184 if (Node->hasExplicitTemplateArgs()) {
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001185 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1186 Node->getTemplateArgs(),
1187 Node->getNumTemplateArgs(),
1188 Policy);
1189 }
Douglas Gregor1c0ca592009-05-22 21:13:27 +00001190}
1191
John McCall129e2df2009-11-30 22:42:35 +00001192void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCallaa81e162009-12-01 22:10:20 +00001193 if (!Node->isImplicitAccess()) {
1194 PrintExpr(Node->getBase());
1195 OS << (Node->isArrow() ? "->" : ".");
1196 }
John McCall129e2df2009-11-30 22:42:35 +00001197 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1198 Qualifier->print(OS, Policy);
1199
1200 // FIXME: this might originally have been written with 'template'
1201
Abramo Bagnara25777432010-08-11 22:01:17 +00001202 OS << Node->getMemberNameInfo();
John McCall129e2df2009-11-30 22:42:35 +00001203
1204 if (Node->hasExplicitTemplateArgs()) {
1205 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1206 Node->getTemplateArgs(),
1207 Node->getNumTemplateArgs(),
1208 Policy);
1209 }
1210}
1211
Sebastian Redl64b45f72009-01-05 20:52:13 +00001212static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1213 switch (UTT) {
1214 default: assert(false && "Unknown type trait");
1215 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1216 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1217 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1218 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1219 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1220 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1221 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1222 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1223 case UTT_IsAbstract: return "__is_abstract";
1224 case UTT_IsClass: return "__is_class";
1225 case UTT_IsEmpty: return "__is_empty";
1226 case UTT_IsEnum: return "__is_enum";
1227 case UTT_IsPOD: return "__is_pod";
1228 case UTT_IsPolymorphic: return "__is_polymorphic";
1229 case UTT_IsUnion: return "__is_union";
1230 }
1231}
1232
1233void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1234 OS << getTypeTraitName(E->getTrait()) << "("
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001235 << E->getQueriedType().getAsString(Policy) << ")";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001236}
1237
Sebastian Redl2e156222010-09-10 20:55:43 +00001238void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1239 OS << "noexcept(";
1240 PrintExpr(E->getOperand());
1241 OS << ")";
1242}
1243
Mike Stump1eb44332009-09-09 15:08:12 +00001244// Obj-C
Anders Carlsson55085182007-08-21 17:43:55 +00001245
1246void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1247 OS << "@";
1248 VisitStringLiteral(Node->getString());
1249}
Reid Spencer5f016e22007-07-11 17:01:13 +00001250
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001251void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001252 OS << "@encode(" << Node->getEncodedType().getAsString(Policy) << ')';
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001253}
1254
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001255void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001256 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001257}
1258
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001259void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramer900fc632010-04-17 09:33:03 +00001260 OS << "@protocol(" << Node->getProtocol() << ')';
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001261}
1262
Steve Naroff563477d2007-09-18 23:55:05 +00001263void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1264 OS << "[";
Douglas Gregor04badcf2010-04-21 00:45:42 +00001265 switch (Mess->getReceiverKind()) {
1266 case ObjCMessageExpr::Instance:
1267 PrintExpr(Mess->getInstanceReceiver());
1268 break;
1269
1270 case ObjCMessageExpr::Class:
1271 OS << Mess->getClassReceiver().getAsString(Policy);
1272 break;
1273
1274 case ObjCMessageExpr::SuperInstance:
1275 case ObjCMessageExpr::SuperClass:
1276 OS << "Super";
1277 break;
1278 }
1279
Ted Kremenekc29efd82008-05-02 17:32:38 +00001280 OS << ' ';
Ted Kremenek97b7f262008-04-16 04:30:16 +00001281 Selector selector = Mess->getSelector();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001282 if (selector.isUnarySelector()) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001283 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001284 } else {
1285 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001286 if (i < selector.getNumArgs()) {
1287 if (i > 0) OS << ' ';
1288 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner39f34e92008-11-24 04:00:27 +00001289 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremenekc29efd82008-05-02 17:32:38 +00001290 else
1291 OS << ":";
1292 }
1293 else OS << ", "; // Handle variadic methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001294
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001295 PrintExpr(Mess->getArg(i));
1296 }
Steve Naroff563477d2007-09-18 23:55:05 +00001297 }
1298 OS << "]";
1299}
1300
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001301void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) {
1302 OS << "super";
1303}
1304
Steve Naroff4eb206b2008-09-03 18:15:37 +00001305void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001306 BlockDecl *BD = Node->getBlockDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001307 OS << "^";
Mike Stump1eb44332009-09-09 15:08:12 +00001308
Steve Naroff4eb206b2008-09-03 18:15:37 +00001309 const FunctionType *AFT = Node->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +00001310
Douglas Gregor72564e72009-02-26 23:50:07 +00001311 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001312 OS << "()";
Douglas Gregor72564e72009-02-26 23:50:07 +00001313 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001314 OS << '(';
1315 std::string ParamStr;
Steve Naroff56ee6892008-10-08 17:01:13 +00001316 for (BlockDecl::param_iterator AI = BD->param_begin(),
1317 E = BD->param_end(); AI != E; ++AI) {
1318 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner39f34e92008-11-24 04:00:27 +00001319 ParamStr = (*AI)->getNameAsString();
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001320 (*AI)->getType().getAsStringInternal(ParamStr, Policy);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001321 OS << ParamStr;
1322 }
Mike Stump1eb44332009-09-09 15:08:12 +00001323
Douglas Gregor72564e72009-02-26 23:50:07 +00001324 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001325 if (FT->isVariadic()) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001326 if (!BD->param_empty()) OS << ", ";
Steve Naroff4eb206b2008-09-03 18:15:37 +00001327 OS << "...";
1328 }
1329 OS << ')';
1330 }
1331}
1332
Steve Naroff4eb206b2008-09-03 18:15:37 +00001333void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Benjamin Kramer900fc632010-04-17 09:33:03 +00001334 OS << Node->getDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001335}
Reid Spencer5f016e22007-07-11 17:01:13 +00001336//===----------------------------------------------------------------------===//
1337// Stmt method implementations
1338//===----------------------------------------------------------------------===//
1339
Eli Friedman48d14a22009-05-30 05:03:24 +00001340void Stmt::dumpPretty(ASTContext& Context) const {
Argyrios Kyrtzidis96680332010-08-09 10:54:31 +00001341 printPretty(llvm::errs(), Context, 0,
Chris Lattnere4f21422009-06-30 01:26:17 +00001342 PrintingPolicy(Context.getLangOptions()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001343}
1344
Eli Friedman48d14a22009-05-30 05:03:24 +00001345void Stmt::printPretty(llvm::raw_ostream &OS, ASTContext& Context,
1346 PrinterHelper* Helper,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001347 const PrintingPolicy &Policy,
1348 unsigned Indentation) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001349 if (this == 0) {
1350 OS << "<NULL>";
1351 return;
1352 }
1353
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001354 if (Policy.Dump && &Context) {
Argyrios Kyrtzidis96680332010-08-09 10:54:31 +00001355 dump(OS, Context.getSourceManager());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001356 return;
1357 }
Mike Stump1eb44332009-09-09 15:08:12 +00001358
Eli Friedman48d14a22009-05-30 05:03:24 +00001359 StmtPrinter P(OS, Context, Helper, Policy, Indentation);
Chris Lattnerc5598cb2007-08-21 04:04:25 +00001360 P.Visit(const_cast<Stmt*>(this));
Reid Spencer5f016e22007-07-11 17:01:13 +00001361}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001362
1363//===----------------------------------------------------------------------===//
1364// PrinterHelper
1365//===----------------------------------------------------------------------===//
1366
1367// Implement virtual destructor.
Gabor Greif84675832007-09-11 15:32:40 +00001368PrinterHelper::~PrinterHelper() {}