blob: 2ed2c92750e6e7e628cd6d90a94d263b4847c964 [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"
Douglas Gregorc7793c72011-01-15 01:15:58 +000018#include "clang/AST/DeclTemplate.h"
Ted Kremenek42a509f2007-08-31 21:30:12 +000019#include "clang/AST/PrettyPrinter.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000020#include "llvm/Support/Format.h"
Douglas Gregor8ecdb652010-04-28 22:16:22 +000021#include "clang/AST/Expr.h"
Douglas Gregorab6677e2010-09-08 00:15:04 +000022#include "clang/AST/ExprCXX.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// StmtPrinter Visitor
27//===----------------------------------------------------------------------===//
28
29namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +000030 class StmtPrinter : public StmtVisitor<StmtPrinter> {
Ted Kremeneka95d3752008-09-13 05:16:45 +000031 llvm::raw_ostream &OS;
Eli Friedman48d14a22009-05-30 05:03:24 +000032 ASTContext &Context;
Reid Spencer5f016e22007-07-11 17:01:13 +000033 unsigned IndentLevel;
Ted Kremenek42a509f2007-08-31 21:30:12 +000034 clang::PrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000035 PrintingPolicy Policy;
36
Reid Spencer5f016e22007-07-11 17:01:13 +000037 public:
Mike Stump1eb44332009-09-09 15:08:12 +000038 StmtPrinter(llvm::raw_ostream &os, ASTContext &C, PrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +000039 const PrintingPolicy &Policy,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000040 unsigned Indentation = 0)
Eli Friedman48d14a22009-05-30 05:03:24 +000041 : OS(os), Context(C), IndentLevel(Indentation), Helper(helper),
42 Policy(Policy) {}
Mike Stump1eb44332009-09-09 15:08:12 +000043
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000044 void PrintStmt(Stmt *S) {
45 PrintStmt(S, Policy.Indentation);
46 }
47
48 void PrintStmt(Stmt *S, int SubIndent) {
Reid Spencer5f016e22007-07-11 17:01:13 +000049 IndentLevel += SubIndent;
50 if (S && isa<Expr>(S)) {
51 // If this is an expr used in a stmt context, indent and newline it.
52 Indent();
Chris Lattnerc5598cb2007-08-21 04:04:25 +000053 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000054 OS << ";\n";
55 } else if (S) {
Chris Lattnerc5598cb2007-08-21 04:04:25 +000056 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000057 } else {
58 Indent() << "<<<NULL STATEMENT>>>\n";
59 }
60 IndentLevel -= SubIndent;
61 }
Eli Friedmandb23b152009-05-30 00:19:54 +000062
Reid Spencer5f016e22007-07-11 17:01:13 +000063 void PrintRawCompoundStmt(CompoundStmt *S);
64 void PrintRawDecl(Decl *D);
Ted Kremenekecd64c52008-10-06 18:39:36 +000065 void PrintRawDeclStmt(DeclStmt *S);
Reid Spencer5f016e22007-07-11 17:01:13 +000066 void PrintRawIfStmt(IfStmt *If);
Sebastian Redl8351da02008-12-22 21:35:02 +000067 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
Peter Collingbourned64e2372011-02-08 21:17:54 +000068 void PrintCallArgs(CallExpr *E);
Mike Stump1eb44332009-09-09 15:08:12 +000069
Reid Spencer5f016e22007-07-11 17:01:13 +000070 void PrintExpr(Expr *E) {
71 if (E)
Chris Lattnerc5598cb2007-08-21 04:04:25 +000072 Visit(E);
Reid Spencer5f016e22007-07-11 17:01:13 +000073 else
74 OS << "<null expr>";
75 }
Mike Stump1eb44332009-09-09 15:08:12 +000076
Mike Stump071e4da2009-02-10 20:16:46 +000077 llvm::raw_ostream &Indent(int Delta = 0) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000078 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
79 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +000080 return OS;
81 }
Mike Stump1eb44332009-09-09 15:08:12 +000082
Mike Stump1eb44332009-09-09 15:08:12 +000083 void Visit(Stmt* S) {
Ted Kremenek42a509f2007-08-31 21:30:12 +000084 if (Helper && Helper->handledStmt(S,OS))
85 return;
86 else StmtVisitor<StmtPrinter>::Visit(S);
87 }
Argyrios Kyrtzidis78e4cc72010-08-15 01:15:33 +000088
Chandler Carruth61e38282010-10-23 08:21:37 +000089 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis78e4cc72010-08-15 01:15:33 +000090 Indent() << "<<unknown stmt type>>\n";
91 }
Chandler Carruth61e38282010-10-23 08:21:37 +000092 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis78e4cc72010-08-15 01:15:33 +000093 OS << "<<unknown expr type>>";
94 }
95 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Mike Stump1eb44332009-09-09 15:08:12 +000096
Argyrios Kyrtzidis78e4cc72010-08-15 01:15:33 +000097#define ABSTRACT_STMT(CLASS)
Douglas Gregorf2cad862008-11-14 12:46:07 +000098#define STMT(CLASS, PARENT) \
Chris Lattnerc5598cb2007-08-21 04:04:25 +000099 void Visit##CLASS(CLASS *Node);
Sean Hunt4bfe1962010-05-05 15:24:00 +0000100#include "clang/AST/StmtNodes.inc"
Reid Spencer5f016e22007-07-11 17:01:13 +0000101 };
102}
103
104//===----------------------------------------------------------------------===//
105// Stmt printing methods.
106//===----------------------------------------------------------------------===//
107
Reid Spencer5f016e22007-07-11 17:01:13 +0000108/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
109/// with no newline after the }.
110void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
111 OS << "{\n";
112 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
113 I != E; ++I)
114 PrintStmt(*I);
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 Indent() << "}";
117}
118
119void StmtPrinter::PrintRawDecl(Decl *D) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000120 D->print(OS, Policy, IndentLevel);
Mike Stump071e4da2009-02-10 20:16:46 +0000121}
122
Ted Kremenekecd64c52008-10-06 18:39:36 +0000123void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
Eli Friedmandb23b152009-05-30 00:19:54 +0000124 DeclStmt::decl_iterator Begin = S->decl_begin(), End = S->decl_end();
Eli Friedman42f42c02009-05-30 04:20:30 +0000125 llvm::SmallVector<Decl*, 2> Decls;
Mike Stump1eb44332009-09-09 15:08:12 +0000126 for ( ; Begin != End; ++Begin)
Eli Friedman42f42c02009-05-30 04:20:30 +0000127 Decls.push_back(*Begin);
Eli Friedmandb23b152009-05-30 00:19:54 +0000128
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000129 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
Ted Kremenekecd64c52008-10-06 18:39:36 +0000130}
Reid Spencer5f016e22007-07-11 17:01:13 +0000131
132void StmtPrinter::VisitNullStmt(NullStmt *Node) {
133 Indent() << ";\n";
134}
135
136void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Eli Friedmandb23b152009-05-30 00:19:54 +0000137 Indent();
138 PrintRawDeclStmt(Node);
139 OS << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000140}
141
142void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
143 Indent();
144 PrintRawCompoundStmt(Node);
145 OS << "\n";
146}
147
148void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
149 Indent(-1) << "case ";
150 PrintExpr(Node->getLHS());
151 if (Node->getRHS()) {
152 OS << " ... ";
153 PrintExpr(Node->getRHS());
154 }
155 OS << ":\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 PrintStmt(Node->getSubStmt(), 0);
158}
159
160void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
161 Indent(-1) << "default:\n";
162 PrintStmt(Node->getSubStmt(), 0);
163}
164
165void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
166 Indent(-1) << Node->getName() << ":\n";
167 PrintStmt(Node->getSubStmt(), 0);
168}
169
170void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000171 OS << "if (";
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 PrintExpr(If->getCond());
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000173 OS << ')';
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Reid Spencer5f016e22007-07-11 17:01:13 +0000175 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
176 OS << ' ';
177 PrintRawCompoundStmt(CS);
178 OS << (If->getElse() ? ' ' : '\n');
179 } else {
180 OS << '\n';
181 PrintStmt(If->getThen());
182 if (If->getElse()) Indent();
183 }
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 if (Stmt *Else = If->getElse()) {
186 OS << "else";
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
189 OS << ' ';
190 PrintRawCompoundStmt(CS);
191 OS << '\n';
192 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
193 OS << ' ';
194 PrintRawIfStmt(ElseIf);
195 } else {
196 OS << '\n';
197 PrintStmt(If->getElse());
198 }
199 }
200}
201
202void StmtPrinter::VisitIfStmt(IfStmt *If) {
203 Indent();
204 PrintRawIfStmt(If);
205}
206
207void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
208 Indent() << "switch (";
209 PrintExpr(Node->getCond());
210 OS << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Reid Spencer5f016e22007-07-11 17:01:13 +0000212 // Pretty print compoundstmt bodies (very common).
213 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
214 OS << " ";
215 PrintRawCompoundStmt(CS);
216 OS << "\n";
217 } else {
218 OS << "\n";
219 PrintStmt(Node->getBody());
220 }
221}
222
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000223void StmtPrinter::VisitSwitchCase(SwitchCase*) {
224 assert(0 && "SwitchCase is an abstract class");
225}
226
Reid Spencer5f016e22007-07-11 17:01:13 +0000227void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
228 Indent() << "while (";
229 PrintExpr(Node->getCond());
230 OS << ")\n";
231 PrintStmt(Node->getBody());
232}
233
234void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner8bdcc472007-09-15 21:49:37 +0000235 Indent() << "do ";
236 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
237 PrintRawCompoundStmt(CS);
238 OS << " ";
239 } else {
240 OS << "\n";
241 PrintStmt(Node->getBody());
242 Indent();
243 }
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Eli Friedmanb3e22962009-05-17 01:05:34 +0000245 OS << "while (";
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 PrintExpr(Node->getCond());
Eli Friedmanb3e22962009-05-17 01:05:34 +0000247 OS << ");\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000248}
249
250void StmtPrinter::VisitForStmt(ForStmt *Node) {
251 Indent() << "for (";
252 if (Node->getInit()) {
253 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000254 PrintRawDeclStmt(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000255 else
256 PrintExpr(cast<Expr>(Node->getInit()));
257 }
Chris Lattner8bdcc472007-09-15 21:49:37 +0000258 OS << ";";
259 if (Node->getCond()) {
260 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000261 PrintExpr(Node->getCond());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000262 }
263 OS << ";";
264 if (Node->getInc()) {
265 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 PrintExpr(Node->getInc());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000267 }
268 OS << ") ";
Mike Stump1eb44332009-09-09 15:08:12 +0000269
Chris Lattner8bdcc472007-09-15 21:49:37 +0000270 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
271 PrintRawCompoundStmt(CS);
272 OS << "\n";
273 } else {
274 OS << "\n";
275 PrintStmt(Node->getBody());
276 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000277}
278
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000279void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000280 Indent() << "for (";
281 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000282 PrintRawDeclStmt(DS);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000283 else
284 PrintExpr(cast<Expr>(Node->getElement()));
285 OS << " in ";
286 PrintExpr(Node->getCollection());
287 OS << ") ";
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000289 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
290 PrintRawCompoundStmt(CS);
291 OS << "\n";
292 } else {
293 OS << "\n";
294 PrintStmt(Node->getBody());
295 }
296}
297
Reid Spencer5f016e22007-07-11 17:01:13 +0000298void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
299 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
300}
301
302void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
303 Indent() << "goto *";
304 PrintExpr(Node->getTarget());
305 OS << ";\n";
306}
307
308void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
309 Indent() << "continue;\n";
310}
311
312void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
313 Indent() << "break;\n";
314}
315
316
317void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
318 Indent() << "return";
319 if (Node->getRetValue()) {
320 OS << " ";
321 PrintExpr(Node->getRetValue());
322 }
323 OS << ";\n";
324}
325
Chris Lattnerfe795952007-10-29 04:04:16 +0000326
327void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson39c47b52007-11-23 23:12:25 +0000328 Indent() << "asm ";
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Anders Carlsson39c47b52007-11-23 23:12:25 +0000330 if (Node->isVolatile())
331 OS << "volatile ";
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Anders Carlsson39c47b52007-11-23 23:12:25 +0000333 OS << "(";
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000334 VisitStringLiteral(Node->getAsmString());
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Anders Carlssonb235fc22007-11-22 01:36:19 +0000336 // Outputs
337 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
338 Node->getNumClobbers() != 0)
339 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Anders Carlssonb235fc22007-11-22 01:36:19 +0000341 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
342 if (i != 0)
343 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Anders Carlssonb235fc22007-11-22 01:36:19 +0000345 if (!Node->getOutputName(i).empty()) {
346 OS << '[';
347 OS << Node->getOutputName(i);
348 OS << "] ";
349 }
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Chris Lattnerb3277932009-03-10 04:59:06 +0000351 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000352 OS << " ";
353 Visit(Node->getOutputExpr(i));
354 }
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Anders Carlssonb235fc22007-11-22 01:36:19 +0000356 // Inputs
357 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
358 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Anders Carlssonb235fc22007-11-22 01:36:19 +0000360 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
361 if (i != 0)
362 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Anders Carlssonb235fc22007-11-22 01:36:19 +0000364 if (!Node->getInputName(i).empty()) {
365 OS << '[';
366 OS << Node->getInputName(i);
367 OS << "] ";
368 }
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Chris Lattnerb3277932009-03-10 04:59:06 +0000370 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000371 OS << " ";
372 Visit(Node->getInputExpr(i));
373 }
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Anders Carlssonb235fc22007-11-22 01:36:19 +0000375 // Clobbers
376 if (Node->getNumClobbers() != 0)
377 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Anders Carlssonb235fc22007-11-22 01:36:19 +0000379 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
380 if (i != 0)
381 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Anders Carlssonb235fc22007-11-22 01:36:19 +0000383 VisitStringLiteral(Node->getClobber(i));
384 }
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000386 OS << ");\n";
Chris Lattnerfe795952007-10-29 04:04:16 +0000387}
388
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000389void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000390 Indent() << "@try";
391 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
392 PrintRawCompoundStmt(TS);
393 OS << "\n";
394 }
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000396 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
397 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000398 Indent() << "@catch(";
Steve Naroff7ba138a2009-03-03 19:52:17 +0000399 if (catchStmt->getCatchParamDecl()) {
400 if (Decl *DS = catchStmt->getCatchParamDecl())
401 PrintRawDecl(DS);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000402 }
403 OS << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000404 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
405 PrintRawCompoundStmt(CS);
406 OS << "\n";
407 }
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000408 }
Mike Stump1eb44332009-09-09 15:08:12 +0000409
410 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
411 Node->getFinallyStmt())) {
Fariborz Jahanian1e7eab42007-11-07 00:46:42 +0000412 Indent() << "@finally";
413 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000414 OS << "\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000415 }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000416}
417
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000418void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000419}
420
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000421void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000422 Indent() << "@catch (...) { /* todo */ } \n";
423}
424
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000425void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000426 Indent() << "@throw";
427 if (Node->getThrowExpr()) {
428 OS << " ";
429 PrintExpr(Node->getThrowExpr());
430 }
431 OS << ";\n";
432}
433
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000434void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000435 Indent() << "@synchronized (";
436 PrintExpr(Node->getSynchExpr());
437 OS << ")";
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000438 PrintRawCompoundStmt(Node->getSynchBody());
439 OS << "\n";
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000440}
441
Sebastian Redl8351da02008-12-22 21:35:02 +0000442void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
443 OS << "catch (";
Sebastian Redl4b07b292008-12-22 19:15:10 +0000444 if (Decl *ExDecl = Node->getExceptionDecl())
445 PrintRawDecl(ExDecl);
446 else
447 OS << "...";
448 OS << ") ";
449 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl8351da02008-12-22 21:35:02 +0000450}
451
452void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
453 Indent();
454 PrintRawCXXCatchStmt(Node);
455 OS << "\n";
456}
457
458void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
459 Indent() << "try ";
460 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump1eb44332009-09-09 15:08:12 +0000461 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl8351da02008-12-22 21:35:02 +0000462 OS << " ";
463 PrintRawCXXCatchStmt(Node->getHandler(i));
464 }
Sebastian Redl4b07b292008-12-22 19:15:10 +0000465 OS << "\n";
466}
467
Reid Spencer5f016e22007-07-11 17:01:13 +0000468//===----------------------------------------------------------------------===//
469// Expr printing methods.
470//===----------------------------------------------------------------------===//
471
Reid Spencer5f016e22007-07-11 17:01:13 +0000472void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000473 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
474 Qualifier->print(OS, Policy);
Abramo Bagnara25777432010-08-11 22:01:17 +0000475 OS << Node->getNameInfo();
John McCall096832c2010-08-19 23:49:38 +0000476 if (Node->hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000477 OS << TemplateSpecializationType::PrintTemplateArgumentList(
478 Node->getTemplateArgs(),
479 Node->getNumTemplateArgs(),
Sean Huntc3021132010-05-05 15:23:54 +0000480 Policy);
Douglas Gregor1a49af92009-01-06 05:10:23 +0000481}
482
John McCall865d4472009-11-19 22:55:06 +0000483void StmtPrinter::VisitDependentScopeDeclRefExpr(
484 DependentScopeDeclRefExpr *Node) {
Axel Naumann274f83c2011-01-24 15:44:00 +0000485 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
486 Qualifier->print(OS, Policy);
Abramo Bagnara25777432010-08-11 22:01:17 +0000487 OS << Node->getNameInfo();
John McCallf7a1a742009-11-24 19:00:30 +0000488 if (Node->hasExplicitTemplateArgs())
489 OS << TemplateSpecializationType::PrintTemplateArgumentList(
490 Node->getTemplateArgs(),
491 Node->getNumTemplateArgs(),
492 Policy);
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000493}
494
John McCallba135432009-11-21 08:51:07 +0000495void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000496 if (Node->getQualifier())
497 Node->getQualifier()->print(OS, Policy);
Abramo Bagnara25777432010-08-11 22:01:17 +0000498 OS << Node->getNameInfo();
John McCallf7a1a742009-11-24 19:00:30 +0000499 if (Node->hasExplicitTemplateArgs())
500 OS << TemplateSpecializationType::PrintTemplateArgumentList(
501 Node->getTemplateArgs(),
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000502 Node->getNumTemplateArgs(),
John McCallf7a1a742009-11-24 19:00:30 +0000503 Policy);
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000504}
505
Steve Naroff7779db42007-11-12 14:29:37 +0000506void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000507 if (Node->getBase()) {
508 PrintExpr(Node->getBase());
509 OS << (Node->isArrow() ? "->" : ".");
510 }
Benjamin Kramer900fc632010-04-17 09:33:03 +0000511 OS << Node->getDecl();
Steve Naroff7779db42007-11-12 14:29:37 +0000512}
513
Steve Naroffae784072008-05-30 00:40:33 +0000514void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000515 if (Node->isSuperReceiver())
516 OS << "super.";
517 else if (Node->getBase()) {
Steve Naroffae784072008-05-30 00:40:33 +0000518 PrintExpr(Node->getBase());
519 OS << ".";
520 }
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000521
John McCall12f78a62010-12-02 01:19:52 +0000522 if (Node->isImplicitProperty())
523 OS << Node->getImplicitPropertyGetter()->getSelector().getAsString();
524 else
525 OS << Node->getExplicitProperty()->getName();
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000526}
527
Chris Lattnerd9f69102008-08-10 01:53:14 +0000528void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson22742662007-07-21 05:21:51 +0000529 switch (Node->getIdentType()) {
530 default:
531 assert(0 && "unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000532 case PredefinedExpr::Func:
Anders Carlsson22742662007-07-21 05:21:51 +0000533 OS << "__func__";
534 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000535 case PredefinedExpr::Function:
Anders Carlsson22742662007-07-21 05:21:51 +0000536 OS << "__FUNCTION__";
537 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000538 case PredefinedExpr::PrettyFunction:
Anders Carlsson22742662007-07-21 05:21:51 +0000539 OS << "__PRETTY_FUNCTION__";
540 break;
541 }
542}
543
Reid Spencer5f016e22007-07-11 17:01:13 +0000544void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000545 unsigned value = Node->getValue();
Chris Lattnerc250aae2008-06-07 22:35:38 +0000546 if (Node->isWide())
547 OS << "L";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000548 switch (value) {
549 case '\\':
550 OS << "'\\\\'";
551 break;
552 case '\'':
553 OS << "'\\''";
554 break;
555 case '\a':
556 // TODO: K&R: the meaning of '\\a' is different in traditional C
557 OS << "'\\a'";
558 break;
559 case '\b':
560 OS << "'\\b'";
561 break;
562 // Nonstandard escape sequence.
563 /*case '\e':
564 OS << "'\\e'";
565 break;*/
566 case '\f':
567 OS << "'\\f'";
568 break;
569 case '\n':
570 OS << "'\\n'";
571 break;
572 case '\r':
573 OS << "'\\r'";
574 break;
575 case '\t':
576 OS << "'\\t'";
577 break;
578 case '\v':
579 OS << "'\\v'";
580 break;
581 default:
Ted Kremenek471733d2008-02-23 00:52:04 +0000582 if (value < 256 && isprint(value)) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000583 OS << "'" << (char)value << "'";
584 } else if (value < 256) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000585 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000586 } else {
587 // FIXME what to really do here?
588 OS << value;
589 }
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000590 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000591}
592
593void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
594 bool isSigned = Node->getType()->isSignedIntegerType();
595 OS << Node->getValue().toString(10, isSigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000596
Reid Spencer5f016e22007-07-11 17:01:13 +0000597 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall183700f2009-09-21 23:43:11 +0000598 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000599 default: assert(0 && "Unexpected type for integer literal!");
600 case BuiltinType::Int: break; // no suffix.
601 case BuiltinType::UInt: OS << 'U'; break;
602 case BuiltinType::Long: OS << 'L'; break;
603 case BuiltinType::ULong: OS << "UL"; break;
604 case BuiltinType::LongLong: OS << "LL"; break;
605 case BuiltinType::ULongLong: OS << "ULL"; break;
606 }
607}
608void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner86e499d2007-08-01 00:23:58 +0000609 // FIXME: print value more precisely.
Chris Lattnerda8249e2008-06-07 22:13:43 +0000610 OS << Node->getValueAsApproximateDouble();
Reid Spencer5f016e22007-07-11 17:01:13 +0000611}
Chris Lattner5d661452007-08-26 03:42:43 +0000612
613void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
614 PrintExpr(Node->getSubExpr());
615 OS << "i";
616}
617
Reid Spencer5f016e22007-07-11 17:01:13 +0000618void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
619 if (Str->isWide()) OS << 'L';
620 OS << '"';
Anders Carlssonee98ac52007-10-15 02:50:23 +0000621
Reid Spencer5f016e22007-07-11 17:01:13 +0000622 // FIXME: this doesn't print wstrings right.
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +0000623 llvm::StringRef StrData = Str->getString();
624 for (llvm::StringRef::iterator I = StrData.begin(), E = StrData.end();
625 I != E; ++I) {
626 unsigned char Char = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Chris Lattner9a81c872009-01-16 19:25:18 +0000628 switch (Char) {
629 default:
630 if (isprint(Char))
631 OS << (char)Char;
632 else // Output anything hard as an octal escape.
633 OS << '\\'
634 << (char)('0'+ ((Char >> 6) & 7))
635 << (char)('0'+ ((Char >> 3) & 7))
636 << (char)('0'+ ((Char >> 0) & 7));
637 break;
638 // Handle some common non-printable cases to make dumps prettier.
Reid Spencer5f016e22007-07-11 17:01:13 +0000639 case '\\': OS << "\\\\"; break;
640 case '"': OS << "\\\""; break;
641 case '\n': OS << "\\n"; break;
642 case '\t': OS << "\\t"; break;
643 case '\a': OS << "\\a"; break;
644 case '\b': OS << "\\b"; break;
645 }
646 }
647 OS << '"';
648}
649void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
650 OS << "(";
651 PrintExpr(Node->getSubExpr());
652 OS << ")";
653}
654void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +0000655 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000656 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Eli Friedman7df71ac2009-06-14 22:39:26 +0000658 // Print a space if this is an "identifier operator" like __real, or if
659 // it might be concatenated incorrectly like '+'.
Chris Lattner296bf192007-08-23 21:46:40 +0000660 switch (Node->getOpcode()) {
661 default: break;
John McCall2de56d12010-08-25 11:45:40 +0000662 case UO_Real:
663 case UO_Imag:
664 case UO_Extension:
Chris Lattner296bf192007-08-23 21:46:40 +0000665 OS << ' ';
666 break;
John McCall2de56d12010-08-25 11:45:40 +0000667 case UO_Plus:
668 case UO_Minus:
Eli Friedman7df71ac2009-06-14 22:39:26 +0000669 if (isa<UnaryOperator>(Node->getSubExpr()))
670 OS << ' ';
671 break;
Chris Lattner296bf192007-08-23 21:46:40 +0000672 }
673 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000674 PrintExpr(Node->getSubExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Reid Spencer5f016e22007-07-11 17:01:13 +0000676 if (Node->isPostfix())
677 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +0000678}
Chris Lattner704fe352007-08-30 17:59:59 +0000679
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000680void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
681 OS << "__builtin_offsetof(";
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000682 OS << Node->getTypeSourceInfo()->getType().getAsString(Policy) << ", ";
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000683 bool PrintedSomething = false;
684 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
685 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
686 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
687 // Array node
688 OS << "[";
689 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
690 OS << "]";
691 PrintedSomething = true;
692 continue;
693 }
Douglas Gregorcc8a5d52010-04-29 00:18:15 +0000694
695 // Skip implicit base indirections.
696 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
697 continue;
698
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000699 // Field or identifier node.
700 IdentifierInfo *Id = ON.getFieldName();
701 if (!Id)
702 continue;
Sean Huntc3021132010-05-05 15:23:54 +0000703
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000704 if (PrintedSomething)
705 OS << ".";
706 else
707 PrintedSomething = true;
Sean Huntc3021132010-05-05 15:23:54 +0000708 OS << Id->getName();
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000709 }
710 OS << ")";
711}
712
Sebastian Redl05189992008-11-11 17:56:53 +0000713void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
714 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
715 if (Node->isArgumentType())
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000716 OS << "(" << Node->getArgumentType().getAsString(Policy) << ")";
Sebastian Redl05189992008-11-11 17:56:53 +0000717 else {
718 OS << " ";
719 PrintExpr(Node->getArgumentExpr());
720 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000721}
722void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +0000723 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000724 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +0000725 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000726 OS << "]";
727}
728
Peter Collingbourned64e2372011-02-08 21:17:54 +0000729void StmtPrinter::PrintCallArgs(CallExpr *Call) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000730 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattner04421082008-04-08 04:40:51 +0000731 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
732 // Don't print any defaulted arguments
733 break;
734 }
735
Reid Spencer5f016e22007-07-11 17:01:13 +0000736 if (i) OS << ", ";
737 PrintExpr(Call->getArg(i));
738 }
Peter Collingbourned64e2372011-02-08 21:17:54 +0000739}
740
741void StmtPrinter::VisitCallExpr(CallExpr *Call) {
742 PrintExpr(Call->getCallee());
743 OS << "(";
744 PrintCallArgs(Call);
Reid Spencer5f016e22007-07-11 17:01:13 +0000745 OS << ")";
746}
747void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregorb3eef682009-01-08 22:45:41 +0000748 // FIXME: Suppress printing implicit bases (like "this")
749 PrintExpr(Node->getBase());
Fariborz Jahanian97fd83a2010-01-11 21:17:32 +0000750 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
751 if (FD->isAnonymousStructOrUnion())
752 return;
Douglas Gregorb3eef682009-01-08 22:45:41 +0000753 OS << (Node->isArrow() ? "->" : ".");
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000754 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
755 Qualifier->print(OS, Policy);
756
Abramo Bagnara25777432010-08-11 22:01:17 +0000757 OS << Node->getMemberNameInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000758
John McCall096832c2010-08-19 23:49:38 +0000759 if (Node->hasExplicitTemplateArgs())
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000760 OS << TemplateSpecializationType::PrintTemplateArgumentList(
761 Node->getTemplateArgs(),
762 Node->getNumTemplateArgs(),
763 Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000764}
Steve Narofff242b1b2009-07-24 17:54:45 +0000765void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
766 PrintExpr(Node->getBase());
767 OS << (Node->isArrow() ? "->isa" : ".isa");
768}
769
Nate Begeman213541a2008-04-18 23:10:10 +0000770void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +0000771 PrintExpr(Node->getBase());
772 OS << ".";
773 OS << Node->getAccessor().getName();
774}
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000775void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Fariborz Jahanian03e80e42010-06-30 16:31:08 +0000776 OS << "(" << Node->getType().getAsString(Policy) << ")";
Reid Spencer5f016e22007-07-11 17:01:13 +0000777 PrintExpr(Node->getSubExpr());
778}
Steve Naroffaff1edd2007-07-19 21:32:11 +0000779void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000780 OS << "(" << Node->getType().getAsString(Policy) << ")";
Steve Naroffaff1edd2007-07-19 21:32:11 +0000781 PrintExpr(Node->getInitializer());
782}
Steve Naroff49b45262007-07-13 16:58:59 +0000783void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroff90045e82007-07-13 23:32:42 +0000784 // No need to print anything, simply forward to the sub expression.
785 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +0000786}
Reid Spencer5f016e22007-07-11 17:01:13 +0000787void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
788 PrintExpr(Node->getLHS());
789 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
790 PrintExpr(Node->getRHS());
791}
Chris Lattnereb14fe82007-08-25 02:00:02 +0000792void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
793 PrintExpr(Node->getLHS());
794 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
795 PrintExpr(Node->getRHS());
796}
Reid Spencer5f016e22007-07-11 17:01:13 +0000797void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
798 PrintExpr(Node->getCond());
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Ted Kremenek8e911c42007-11-26 18:27:54 +0000800 if (Node->getLHS()) {
801 OS << " ? ";
802 PrintExpr(Node->getLHS());
803 OS << " : ";
804 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000805 else { // Handle GCC extension where LHS can be NULL.
Ted Kremenek8e911c42007-11-26 18:27:54 +0000806 OS << " ?: ";
807 }
Mike Stump1eb44332009-09-09 15:08:12 +0000808
Reid Spencer5f016e22007-07-11 17:01:13 +0000809 PrintExpr(Node->getRHS());
810}
811
812// GNU extensions.
813
Chris Lattner6481a572007-08-03 17:31:20 +0000814void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000815 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000816}
817
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000818void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
819 OS << "(";
820 PrintRawCompoundStmt(E->getSubStmt());
821 OS << ")";
822}
823
Steve Naroffd04fdd52007-08-03 21:21:27 +0000824void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
825 OS << "__builtin_choose_expr(";
826 PrintExpr(Node->getCond());
Chris Lattner94f05e32007-08-04 00:20:15 +0000827 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000828 PrintExpr(Node->getLHS());
Chris Lattner94f05e32007-08-04 00:20:15 +0000829 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000830 PrintExpr(Node->getRHS());
831 OS << ")";
832}
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000833
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000834void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
835 OS << "__null";
836}
837
Eli Friedmand38617c2008-05-14 19:38:39 +0000838void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
839 OS << "__builtin_shufflevector(";
840 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
841 if (i) OS << ", ";
842 PrintExpr(Node->getExpr(i));
843 }
844 OS << ")";
845}
846
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000847void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000848 if (Node->getSyntacticForm()) {
849 Visit(Node->getSyntacticForm());
850 return;
851 }
852
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000853 OS << "{ ";
854 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
855 if (i) OS << ", ";
Douglas Gregor4c678342009-01-28 21:54:33 +0000856 if (Node->getInit(i))
857 PrintExpr(Node->getInit(i));
858 else
859 OS << "0";
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000860 }
861 OS << " }";
862}
863
Nate Begeman2ef13e52009-08-10 23:49:36 +0000864void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
865 OS << "( ";
866 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
867 if (i) OS << ", ";
868 PrintExpr(Node->getExpr(i));
869 }
870 OS << " )";
871}
872
Douglas Gregor05c13a32009-01-22 00:58:24 +0000873void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000874 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
875 DEnd = Node->designators_end();
876 D != DEnd; ++D) {
877 if (D->isFieldDesignator()) {
878 if (D->getDotLoc().isInvalid())
879 OS << D->getFieldName()->getName() << ":";
880 else
881 OS << "." << D->getFieldName()->getName();
882 } else {
883 OS << "[";
884 if (D->isArrayDesignator()) {
885 PrintExpr(Node->getArrayIndex(*D));
886 } else {
887 PrintExpr(Node->getArrayRangeStart(*D));
888 OS << " ... ";
Mike Stump1eb44332009-09-09 15:08:12 +0000889 PrintExpr(Node->getArrayRangeEnd(*D));
Douglas Gregor4c678342009-01-28 21:54:33 +0000890 }
891 OS << "]";
892 }
893 }
894
895 OS << " = ";
896 PrintExpr(Node->getInit());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000897}
898
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000899void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Chris Lattnere4f21422009-06-30 01:26:17 +0000900 if (Policy.LangOpts.CPlusPlus)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000901 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()";
902 else {
903 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")";
904 if (Node->getType()->isRecordType())
905 OS << "{}";
906 else
907 OS << 0;
908 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000909}
910
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000911void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000912 OS << "__builtin_va_arg(";
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000913 PrintExpr(Node->getSubExpr());
914 OS << ", ";
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000915 OS << Node->getType().getAsString(Policy);
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000916 OS << ")";
917}
918
Reid Spencer5f016e22007-07-11 17:01:13 +0000919// C++
Douglas Gregorb4609802008-11-14 16:09:21 +0000920void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
921 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
922 "",
923#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
924 Spelling,
925#include "clang/Basic/OperatorKinds.def"
926 };
927
928 OverloadedOperatorKind Kind = Node->getOperator();
929 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
930 if (Node->getNumArgs() == 1) {
931 OS << OpStrings[Kind] << ' ';
932 PrintExpr(Node->getArg(0));
933 } else {
934 PrintExpr(Node->getArg(0));
935 OS << ' ' << OpStrings[Kind];
936 }
937 } else if (Kind == OO_Call) {
938 PrintExpr(Node->getArg(0));
939 OS << '(';
940 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
941 if (ArgIdx > 1)
942 OS << ", ";
943 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
944 PrintExpr(Node->getArg(ArgIdx));
945 }
946 OS << ')';
947 } else if (Kind == OO_Subscript) {
948 PrintExpr(Node->getArg(0));
949 OS << '[';
950 PrintExpr(Node->getArg(1));
951 OS << ']';
952 } else if (Node->getNumArgs() == 1) {
953 OS << OpStrings[Kind] << ' ';
954 PrintExpr(Node->getArg(0));
955 } else if (Node->getNumArgs() == 2) {
956 PrintExpr(Node->getArg(0));
957 OS << ' ' << OpStrings[Kind] << ' ';
958 PrintExpr(Node->getArg(1));
959 } else {
960 assert(false && "unknown overloaded operator");
961 }
962}
Reid Spencer5f016e22007-07-11 17:01:13 +0000963
Douglas Gregor88a35142008-12-22 05:46:06 +0000964void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
965 VisitCallExpr(cast<CallExpr>(Node));
966}
967
Douglas Gregor49badde2008-10-27 19:41:14 +0000968void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
969 OS << Node->getCastName() << '<';
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000970 OS << Node->getTypeAsWritten().getAsString(Policy) << ">(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000971 PrintExpr(Node->getSubExpr());
972 OS << ")";
973}
974
Douglas Gregor49badde2008-10-27 19:41:14 +0000975void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
976 VisitCXXNamedCastExpr(Node);
977}
978
979void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
980 VisitCXXNamedCastExpr(Node);
981}
982
983void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
984 VisitCXXNamedCastExpr(Node);
985}
986
987void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
988 VisitCXXNamedCastExpr(Node);
989}
990
Sebastian Redlc42e1182008-11-11 11:37:55 +0000991void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
992 OS << "typeid(";
993 if (Node->isTypeOperand()) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000994 OS << Node->getTypeOperand().getAsString(Policy);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000995 } else {
996 PrintExpr(Node->getExprOperand());
997 }
998 OS << ")";
999}
1000
Francois Pichet01b7c302010-09-08 12:20:18 +00001001void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1002 OS << "__uuidof(";
1003 if (Node->isTypeOperand()) {
1004 OS << Node->getTypeOperand().getAsString(Policy);
1005 } else {
1006 PrintExpr(Node->getExprOperand());
1007 }
1008 OS << ")";
1009}
1010
Reid Spencer5f016e22007-07-11 17:01:13 +00001011void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1012 OS << (Node->getValue() ? "true" : "false");
1013}
1014
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001015void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1016 OS << "nullptr";
1017}
1018
Douglas Gregor796da182008-11-04 14:32:21 +00001019void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1020 OS << "this";
1021}
1022
Chris Lattner50dd2892008-02-26 00:51:44 +00001023void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1024 if (Node->getSubExpr() == 0)
1025 OS << "throw";
1026 else {
1027 OS << "throw ";
1028 PrintExpr(Node->getSubExpr());
1029 }
1030}
1031
Chris Lattner04421082008-04-08 04:40:51 +00001032void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1033 // Nothing to print: we picked up the default argument
1034}
1035
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001036void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001037 OS << Node->getType().getAsString(Policy);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001038 OS << "(";
1039 PrintExpr(Node->getSubExpr());
1040 OS << ")";
1041}
1042
Anders Carlssonfceb0a82009-05-30 20:03:25 +00001043void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1044 PrintExpr(Node->getSubExpr());
1045}
1046
Douglas Gregor506ae412009-01-16 18:33:17 +00001047void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001048 OS << Node->getType().getAsString(Policy);
Douglas Gregor506ae412009-01-16 18:33:17 +00001049 OS << "(";
1050 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001051 ArgEnd = Node->arg_end();
Douglas Gregor506ae412009-01-16 18:33:17 +00001052 Arg != ArgEnd; ++Arg) {
1053 if (Arg != Node->arg_begin())
1054 OS << ", ";
1055 PrintExpr(*Arg);
1056 }
1057 OS << ")";
1058}
1059
Douglas Gregored8abf12010-07-08 06:14:04 +00001060void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregorab6677e2010-09-08 00:15:04 +00001061 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
1062 OS << TSInfo->getType().getAsString(Policy) << "()";
1063 else
1064 OS << Node->getType().getAsString(Policy) << "()";
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001065}
1066
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001067void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1068 if (E->isGlobalNew())
1069 OS << "::";
1070 OS << "new ";
1071 unsigned NumPlace = E->getNumPlacementArgs();
1072 if (NumPlace > 0) {
1073 OS << "(";
1074 PrintExpr(E->getPlacementArg(0));
1075 for (unsigned i = 1; i < NumPlace; ++i) {
1076 OS << ", ";
1077 PrintExpr(E->getPlacementArg(i));
1078 }
1079 OS << ") ";
1080 }
1081 if (E->isParenTypeId())
1082 OS << "(";
Sebastian Redl6fec6482008-12-02 22:08:59 +00001083 std::string TypeS;
1084 if (Expr *Size = E->getArraySize()) {
1085 llvm::raw_string_ostream s(TypeS);
Eli Friedman6e1a3452009-05-30 05:32:46 +00001086 Size->printPretty(s, Context, Helper, Policy);
Sebastian Redl6fec6482008-12-02 22:08:59 +00001087 s.flush();
1088 TypeS = "[" + TypeS + "]";
1089 }
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001090 E->getAllocatedType().getAsStringInternal(TypeS, Policy);
Sebastian Redl6fec6482008-12-02 22:08:59 +00001091 OS << TypeS;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001092 if (E->isParenTypeId())
1093 OS << ")";
1094
1095 if (E->hasInitializer()) {
1096 OS << "(";
1097 unsigned NumCons = E->getNumConstructorArgs();
1098 if (NumCons > 0) {
1099 PrintExpr(E->getConstructorArg(0));
1100 for (unsigned i = 1; i < NumCons; ++i) {
1101 OS << ", ";
1102 PrintExpr(E->getConstructorArg(i));
1103 }
1104 }
1105 OS << ")";
1106 }
1107}
1108
1109void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1110 if (E->isGlobalDelete())
1111 OS << "::";
1112 OS << "delete ";
1113 if (E->isArrayForm())
1114 OS << "[] ";
1115 PrintExpr(E->getArgument());
1116}
1117
Douglas Gregora71d8192009-09-04 17:36:40 +00001118void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1119 PrintExpr(E->getBase());
1120 if (E->isArrow())
1121 OS << "->";
1122 else
1123 OS << '.';
1124 if (E->getQualifier())
1125 E->getQualifier()->print(OS, Policy);
Mike Stump1eb44332009-09-09 15:08:12 +00001126
Douglas Gregora71d8192009-09-04 17:36:40 +00001127 std::string TypeS;
Douglas Gregora2e7dd22010-02-25 01:56:36 +00001128 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1129 OS << II->getName();
1130 else
1131 E->getDestroyedType().getAsStringInternal(TypeS, Policy);
Douglas Gregora71d8192009-09-04 17:36:40 +00001132 OS << TypeS;
1133}
1134
Anders Carlssone349bea2009-04-23 02:32:43 +00001135void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Douglas Gregord0fb3ad2011-01-24 17:25:03 +00001136 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1137 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1138 // Don't print any defaulted arguments
1139 break;
1140 }
1141
1142 if (i) OS << ", ";
1143 PrintExpr(E->getArg(i));
Fariborz Jahanianc75da512010-01-13 21:41:11 +00001144 }
Anders Carlssone349bea2009-04-23 02:32:43 +00001145}
1146
John McCall4765fa02010-12-06 08:20:24 +00001147void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Anders Carlsson02bbfa32009-04-24 22:47:04 +00001148 // Just forward to the sub expression.
1149 PrintExpr(E->getSubExpr());
1150}
1151
Mike Stump1eb44332009-09-09 15:08:12 +00001152void
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001153StmtPrinter::VisitCXXUnresolvedConstructExpr(
1154 CXXUnresolvedConstructExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001155 OS << Node->getTypeAsWritten().getAsString(Policy);
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001156 OS << "(";
1157 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001158 ArgEnd = Node->arg_end();
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001159 Arg != ArgEnd; ++Arg) {
1160 if (Arg != Node->arg_begin())
1161 OS << ", ";
1162 PrintExpr(*Arg);
1163 }
1164 OS << ")";
1165}
1166
John McCall865d4472009-11-19 22:55:06 +00001167void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1168 CXXDependentScopeMemberExpr *Node) {
John McCallaa81e162009-12-01 22:10:20 +00001169 if (!Node->isImplicitAccess()) {
1170 PrintExpr(Node->getBase());
1171 OS << (Node->isArrow() ? "->" : ".");
1172 }
Douglas Gregora38c6872009-09-03 16:14:30 +00001173 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1174 Qualifier->print(OS, Policy);
John McCallaa81e162009-12-01 22:10:20 +00001175 else if (Node->hasExplicitTemplateArgs())
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001176 // FIXME: Track use of "template" keyword explicitly?
1177 OS << "template ";
Mike Stump1eb44332009-09-09 15:08:12 +00001178
Abramo Bagnara25777432010-08-11 22:01:17 +00001179 OS << Node->getMemberNameInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00001180
John McCallaa81e162009-12-01 22:10:20 +00001181 if (Node->hasExplicitTemplateArgs()) {
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001182 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1183 Node->getTemplateArgs(),
1184 Node->getNumTemplateArgs(),
1185 Policy);
1186 }
Douglas Gregor1c0ca592009-05-22 21:13:27 +00001187}
1188
John McCall129e2df2009-11-30 22:42:35 +00001189void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCallaa81e162009-12-01 22:10:20 +00001190 if (!Node->isImplicitAccess()) {
1191 PrintExpr(Node->getBase());
1192 OS << (Node->isArrow() ? "->" : ".");
1193 }
John McCall129e2df2009-11-30 22:42:35 +00001194 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1195 Qualifier->print(OS, Policy);
1196
1197 // FIXME: this might originally have been written with 'template'
1198
Abramo Bagnara25777432010-08-11 22:01:17 +00001199 OS << Node->getMemberNameInfo();
John McCall129e2df2009-11-30 22:42:35 +00001200
1201 if (Node->hasExplicitTemplateArgs()) {
1202 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1203 Node->getTemplateArgs(),
1204 Node->getNumTemplateArgs(),
1205 Policy);
1206 }
1207}
1208
Sebastian Redl64b45f72009-01-05 20:52:13 +00001209static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1210 switch (UTT) {
Francois Pichet38c2b732010-12-07 00:55:57 +00001211 default: llvm_unreachable("Unknown unary type trait");
Sebastian Redl64b45f72009-01-05 20:52:13 +00001212 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1213 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1214 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1215 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1216 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1217 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1218 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1219 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1220 case UTT_IsAbstract: return "__is_abstract";
1221 case UTT_IsClass: return "__is_class";
1222 case UTT_IsEmpty: return "__is_empty";
1223 case UTT_IsEnum: return "__is_enum";
1224 case UTT_IsPOD: return "__is_pod";
1225 case UTT_IsPolymorphic: return "__is_polymorphic";
1226 case UTT_IsUnion: return "__is_union";
1227 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00001228 return "";
1229}
1230
1231static const char *getTypeTraitName(BinaryTypeTrait BTT) {
1232 switch (BTT) {
Francois Pichetf1872372010-12-08 22:35:30 +00001233 case BTT_IsBaseOf: return "__is_base_of";
1234 case BTT_TypeCompatible: return "__builtin_types_compatible_p";
Douglas Gregor9f361132011-01-27 20:28:01 +00001235 case BTT_IsConvertibleTo: return "__is_convertible_to";
Francois Pichet6ad6f282010-12-07 00:08:36 +00001236 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00001237 return "";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001238}
1239
1240void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1241 OS << getTypeTraitName(E->getTrait()) << "("
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001242 << E->getQueriedType().getAsString(Policy) << ")";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001243}
1244
Francois Pichet6ad6f282010-12-07 00:08:36 +00001245void StmtPrinter::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
1246 OS << getTypeTraitName(E->getTrait()) << "("
1247 << E->getLhsType().getAsString(Policy) << ","
1248 << E->getRhsType().getAsString(Policy) << ")";
1249}
1250
Sebastian Redl2e156222010-09-10 20:55:43 +00001251void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1252 OS << "noexcept(";
1253 PrintExpr(E->getOperand());
1254 OS << ")";
1255}
1256
Douglas Gregoree8aff02011-01-04 17:33:58 +00001257void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregorbe230c32011-01-03 17:17:50 +00001258 PrintExpr(E->getPattern());
1259 OS << "...";
1260}
1261
Douglas Gregoree8aff02011-01-04 17:33:58 +00001262void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
1263 OS << "sizeof...(" << E->getPack()->getNameAsString() << ")";
1264}
1265
Douglas Gregorc7793c72011-01-15 01:15:58 +00001266void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
1267 SubstNonTypeTemplateParmPackExpr *Node) {
1268 OS << Node->getParameterPack()->getNameAsString();
1269}
1270
Mike Stump1eb44332009-09-09 15:08:12 +00001271// Obj-C
Anders Carlsson55085182007-08-21 17:43:55 +00001272
1273void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1274 OS << "@";
1275 VisitStringLiteral(Node->getString());
1276}
Reid Spencer5f016e22007-07-11 17:01:13 +00001277
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001278void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001279 OS << "@encode(" << Node->getEncodedType().getAsString(Policy) << ')';
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001280}
1281
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001282void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001283 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001284}
1285
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001286void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramer900fc632010-04-17 09:33:03 +00001287 OS << "@protocol(" << Node->getProtocol() << ')';
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001288}
1289
Steve Naroff563477d2007-09-18 23:55:05 +00001290void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1291 OS << "[";
Douglas Gregor04badcf2010-04-21 00:45:42 +00001292 switch (Mess->getReceiverKind()) {
1293 case ObjCMessageExpr::Instance:
1294 PrintExpr(Mess->getInstanceReceiver());
1295 break;
1296
1297 case ObjCMessageExpr::Class:
1298 OS << Mess->getClassReceiver().getAsString(Policy);
1299 break;
1300
1301 case ObjCMessageExpr::SuperInstance:
1302 case ObjCMessageExpr::SuperClass:
1303 OS << "Super";
1304 break;
1305 }
1306
Ted Kremenekc29efd82008-05-02 17:32:38 +00001307 OS << ' ';
Ted Kremenek97b7f262008-04-16 04:30:16 +00001308 Selector selector = Mess->getSelector();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001309 if (selector.isUnarySelector()) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001310 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001311 } else {
1312 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001313 if (i < selector.getNumArgs()) {
1314 if (i > 0) OS << ' ';
1315 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner39f34e92008-11-24 04:00:27 +00001316 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremenekc29efd82008-05-02 17:32:38 +00001317 else
1318 OS << ":";
1319 }
1320 else OS << ", "; // Handle variadic methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001321
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001322 PrintExpr(Mess->getArg(i));
1323 }
Steve Naroff563477d2007-09-18 23:55:05 +00001324 }
1325 OS << "]";
1326}
1327
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001328
Steve Naroff4eb206b2008-09-03 18:15:37 +00001329void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001330 BlockDecl *BD = Node->getBlockDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001331 OS << "^";
Mike Stump1eb44332009-09-09 15:08:12 +00001332
Steve Naroff4eb206b2008-09-03 18:15:37 +00001333 const FunctionType *AFT = Node->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +00001334
Douglas Gregor72564e72009-02-26 23:50:07 +00001335 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001336 OS << "()";
Douglas Gregor72564e72009-02-26 23:50:07 +00001337 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001338 OS << '(';
1339 std::string ParamStr;
Steve Naroff56ee6892008-10-08 17:01:13 +00001340 for (BlockDecl::param_iterator AI = BD->param_begin(),
1341 E = BD->param_end(); AI != E; ++AI) {
1342 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner39f34e92008-11-24 04:00:27 +00001343 ParamStr = (*AI)->getNameAsString();
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001344 (*AI)->getType().getAsStringInternal(ParamStr, Policy);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001345 OS << ParamStr;
1346 }
Mike Stump1eb44332009-09-09 15:08:12 +00001347
Douglas Gregor72564e72009-02-26 23:50:07 +00001348 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001349 if (FT->isVariadic()) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001350 if (!BD->param_empty()) OS << ", ";
Steve Naroff4eb206b2008-09-03 18:15:37 +00001351 OS << "...";
1352 }
1353 OS << ')';
1354 }
1355}
1356
Steve Naroff4eb206b2008-09-03 18:15:37 +00001357void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Benjamin Kramer900fc632010-04-17 09:33:03 +00001358 OS << Node->getDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001359}
John McCall7cd7d1a2010-11-15 23:31:06 +00001360
1361void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {}
1362
Reid Spencer5f016e22007-07-11 17:01:13 +00001363//===----------------------------------------------------------------------===//
1364// Stmt method implementations
1365//===----------------------------------------------------------------------===//
1366
Eli Friedman48d14a22009-05-30 05:03:24 +00001367void Stmt::dumpPretty(ASTContext& Context) const {
Argyrios Kyrtzidis96680332010-08-09 10:54:31 +00001368 printPretty(llvm::errs(), Context, 0,
Chris Lattnere4f21422009-06-30 01:26:17 +00001369 PrintingPolicy(Context.getLangOptions()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001370}
1371
Eli Friedman48d14a22009-05-30 05:03:24 +00001372void Stmt::printPretty(llvm::raw_ostream &OS, ASTContext& Context,
1373 PrinterHelper* Helper,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001374 const PrintingPolicy &Policy,
1375 unsigned Indentation) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001376 if (this == 0) {
1377 OS << "<NULL>";
1378 return;
1379 }
1380
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001381 if (Policy.Dump && &Context) {
Argyrios Kyrtzidis96680332010-08-09 10:54:31 +00001382 dump(OS, Context.getSourceManager());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001383 return;
1384 }
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Eli Friedman48d14a22009-05-30 05:03:24 +00001386 StmtPrinter P(OS, Context, Helper, Policy, Indentation);
Chris Lattnerc5598cb2007-08-21 04:04:25 +00001387 P.Visit(const_cast<Stmt*>(this));
Reid Spencer5f016e22007-07-11 17:01:13 +00001388}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001389
1390//===----------------------------------------------------------------------===//
1391// PrinterHelper
1392//===----------------------------------------------------------------------===//
1393
1394// Implement virtual destructor.
Gabor Greif84675832007-09-11 15:32:40 +00001395PrinterHelper::~PrinterHelper() {}