blob: fa1736f376823cb3e9811ad605530e30ed55f12d [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
223void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
224 Indent() << "while (";
225 PrintExpr(Node->getCond());
226 OS << ")\n";
227 PrintStmt(Node->getBody());
228}
229
230void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner8bdcc472007-09-15 21:49:37 +0000231 Indent() << "do ";
232 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
233 PrintRawCompoundStmt(CS);
234 OS << " ";
235 } else {
236 OS << "\n";
237 PrintStmt(Node->getBody());
238 Indent();
239 }
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Eli Friedmanb3e22962009-05-17 01:05:34 +0000241 OS << "while (";
Reid Spencer5f016e22007-07-11 17:01:13 +0000242 PrintExpr(Node->getCond());
Eli Friedmanb3e22962009-05-17 01:05:34 +0000243 OS << ");\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000244}
245
246void StmtPrinter::VisitForStmt(ForStmt *Node) {
247 Indent() << "for (";
248 if (Node->getInit()) {
249 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000250 PrintRawDeclStmt(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000251 else
252 PrintExpr(cast<Expr>(Node->getInit()));
253 }
Chris Lattner8bdcc472007-09-15 21:49:37 +0000254 OS << ";";
255 if (Node->getCond()) {
256 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 PrintExpr(Node->getCond());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000258 }
259 OS << ";";
260 if (Node->getInc()) {
261 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000262 PrintExpr(Node->getInc());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000263 }
264 OS << ") ";
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Chris Lattner8bdcc472007-09-15 21:49:37 +0000266 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
267 PrintRawCompoundStmt(CS);
268 OS << "\n";
269 } else {
270 OS << "\n";
271 PrintStmt(Node->getBody());
272 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000273}
274
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000275void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000276 Indent() << "for (";
277 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000278 PrintRawDeclStmt(DS);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000279 else
280 PrintExpr(cast<Expr>(Node->getElement()));
281 OS << " in ";
282 PrintExpr(Node->getCollection());
283 OS << ") ";
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000285 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
286 PrintRawCompoundStmt(CS);
287 OS << "\n";
288 } else {
289 OS << "\n";
290 PrintStmt(Node->getBody());
291 }
292}
293
Reid Spencer5f016e22007-07-11 17:01:13 +0000294void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
295 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
296}
297
298void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
299 Indent() << "goto *";
300 PrintExpr(Node->getTarget());
301 OS << ";\n";
302}
303
304void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
305 Indent() << "continue;\n";
306}
307
308void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
309 Indent() << "break;\n";
310}
311
312
313void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
314 Indent() << "return";
315 if (Node->getRetValue()) {
316 OS << " ";
317 PrintExpr(Node->getRetValue());
318 }
319 OS << ";\n";
320}
321
Chris Lattnerfe795952007-10-29 04:04:16 +0000322
323void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson39c47b52007-11-23 23:12:25 +0000324 Indent() << "asm ";
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Anders Carlsson39c47b52007-11-23 23:12:25 +0000326 if (Node->isVolatile())
327 OS << "volatile ";
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Anders Carlsson39c47b52007-11-23 23:12:25 +0000329 OS << "(";
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000330 VisitStringLiteral(Node->getAsmString());
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Anders Carlssonb235fc22007-11-22 01:36:19 +0000332 // Outputs
333 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
334 Node->getNumClobbers() != 0)
335 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Anders Carlssonb235fc22007-11-22 01:36:19 +0000337 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
338 if (i != 0)
339 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Anders Carlssonb235fc22007-11-22 01:36:19 +0000341 if (!Node->getOutputName(i).empty()) {
342 OS << '[';
343 OS << Node->getOutputName(i);
344 OS << "] ";
345 }
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Chris Lattnerb3277932009-03-10 04:59:06 +0000347 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000348 OS << " ";
349 Visit(Node->getOutputExpr(i));
350 }
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Anders Carlssonb235fc22007-11-22 01:36:19 +0000352 // Inputs
353 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
354 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Anders Carlssonb235fc22007-11-22 01:36:19 +0000356 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
357 if (i != 0)
358 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Anders Carlssonb235fc22007-11-22 01:36:19 +0000360 if (!Node->getInputName(i).empty()) {
361 OS << '[';
362 OS << Node->getInputName(i);
363 OS << "] ";
364 }
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Chris Lattnerb3277932009-03-10 04:59:06 +0000366 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000367 OS << " ";
368 Visit(Node->getInputExpr(i));
369 }
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Anders Carlssonb235fc22007-11-22 01:36:19 +0000371 // Clobbers
372 if (Node->getNumClobbers() != 0)
373 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Anders Carlssonb235fc22007-11-22 01:36:19 +0000375 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
376 if (i != 0)
377 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Anders Carlssonb235fc22007-11-22 01:36:19 +0000379 VisitStringLiteral(Node->getClobber(i));
380 }
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000382 OS << ");\n";
Chris Lattnerfe795952007-10-29 04:04:16 +0000383}
384
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000385void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000386 Indent() << "@try";
387 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
388 PrintRawCompoundStmt(TS);
389 OS << "\n";
390 }
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000392 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
393 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000394 Indent() << "@catch(";
Steve Naroff7ba138a2009-03-03 19:52:17 +0000395 if (catchStmt->getCatchParamDecl()) {
396 if (Decl *DS = catchStmt->getCatchParamDecl())
397 PrintRawDecl(DS);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000398 }
399 OS << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000400 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
401 PrintRawCompoundStmt(CS);
402 OS << "\n";
403 }
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000404 }
Mike Stump1eb44332009-09-09 15:08:12 +0000405
406 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
407 Node->getFinallyStmt())) {
Fariborz Jahanian1e7eab42007-11-07 00:46:42 +0000408 Indent() << "@finally";
409 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000410 OS << "\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000411 }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000412}
413
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000414void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000415}
416
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000417void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000418 Indent() << "@catch (...) { /* todo */ } \n";
419}
420
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000421void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000422 Indent() << "@throw";
423 if (Node->getThrowExpr()) {
424 OS << " ";
425 PrintExpr(Node->getThrowExpr());
426 }
427 OS << ";\n";
428}
429
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000430void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000431 Indent() << "@synchronized (";
432 PrintExpr(Node->getSynchExpr());
433 OS << ")";
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000434 PrintRawCompoundStmt(Node->getSynchBody());
435 OS << "\n";
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000436}
437
Sebastian Redl8351da02008-12-22 21:35:02 +0000438void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
439 OS << "catch (";
Sebastian Redl4b07b292008-12-22 19:15:10 +0000440 if (Decl *ExDecl = Node->getExceptionDecl())
441 PrintRawDecl(ExDecl);
442 else
443 OS << "...";
444 OS << ") ";
445 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl8351da02008-12-22 21:35:02 +0000446}
447
448void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
449 Indent();
450 PrintRawCXXCatchStmt(Node);
451 OS << "\n";
452}
453
454void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
455 Indent() << "try ";
456 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump1eb44332009-09-09 15:08:12 +0000457 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl8351da02008-12-22 21:35:02 +0000458 OS << " ";
459 PrintRawCXXCatchStmt(Node->getHandler(i));
460 }
Sebastian Redl4b07b292008-12-22 19:15:10 +0000461 OS << "\n";
462}
463
Reid Spencer5f016e22007-07-11 17:01:13 +0000464//===----------------------------------------------------------------------===//
465// Expr printing methods.
466//===----------------------------------------------------------------------===//
467
Reid Spencer5f016e22007-07-11 17:01:13 +0000468void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000469 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
470 Qualifier->print(OS, Policy);
Abramo Bagnara25777432010-08-11 22:01:17 +0000471 OS << Node->getNameInfo();
John McCall096832c2010-08-19 23:49:38 +0000472 if (Node->hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000473 OS << TemplateSpecializationType::PrintTemplateArgumentList(
474 Node->getTemplateArgs(),
475 Node->getNumTemplateArgs(),
Sean Huntc3021132010-05-05 15:23:54 +0000476 Policy);
Douglas Gregor1a49af92009-01-06 05:10:23 +0000477}
478
John McCall865d4472009-11-19 22:55:06 +0000479void StmtPrinter::VisitDependentScopeDeclRefExpr(
480 DependentScopeDeclRefExpr *Node) {
Axel Naumann274f83c2011-01-24 15:44:00 +0000481 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
482 Qualifier->print(OS, Policy);
Abramo Bagnara25777432010-08-11 22:01:17 +0000483 OS << Node->getNameInfo();
John McCallf7a1a742009-11-24 19:00:30 +0000484 if (Node->hasExplicitTemplateArgs())
485 OS << TemplateSpecializationType::PrintTemplateArgumentList(
486 Node->getTemplateArgs(),
487 Node->getNumTemplateArgs(),
488 Policy);
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000489}
490
John McCallba135432009-11-21 08:51:07 +0000491void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000492 if (Node->getQualifier())
493 Node->getQualifier()->print(OS, Policy);
Abramo Bagnara25777432010-08-11 22:01:17 +0000494 OS << Node->getNameInfo();
John McCallf7a1a742009-11-24 19:00:30 +0000495 if (Node->hasExplicitTemplateArgs())
496 OS << TemplateSpecializationType::PrintTemplateArgumentList(
497 Node->getTemplateArgs(),
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000498 Node->getNumTemplateArgs(),
John McCallf7a1a742009-11-24 19:00:30 +0000499 Policy);
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000500}
501
Steve Naroff7779db42007-11-12 14:29:37 +0000502void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000503 if (Node->getBase()) {
504 PrintExpr(Node->getBase());
505 OS << (Node->isArrow() ? "->" : ".");
506 }
Benjamin Kramer900fc632010-04-17 09:33:03 +0000507 OS << Node->getDecl();
Steve Naroff7779db42007-11-12 14:29:37 +0000508}
509
Steve Naroffae784072008-05-30 00:40:33 +0000510void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000511 if (Node->isSuperReceiver())
512 OS << "super.";
513 else if (Node->getBase()) {
Steve Naroffae784072008-05-30 00:40:33 +0000514 PrintExpr(Node->getBase());
515 OS << ".";
516 }
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000517
John McCall12f78a62010-12-02 01:19:52 +0000518 if (Node->isImplicitProperty())
519 OS << Node->getImplicitPropertyGetter()->getSelector().getAsString();
520 else
521 OS << Node->getExplicitProperty()->getName();
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000522}
523
Chris Lattnerd9f69102008-08-10 01:53:14 +0000524void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson22742662007-07-21 05:21:51 +0000525 switch (Node->getIdentType()) {
526 default:
527 assert(0 && "unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000528 case PredefinedExpr::Func:
Anders Carlsson22742662007-07-21 05:21:51 +0000529 OS << "__func__";
530 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000531 case PredefinedExpr::Function:
Anders Carlsson22742662007-07-21 05:21:51 +0000532 OS << "__FUNCTION__";
533 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000534 case PredefinedExpr::PrettyFunction:
Anders Carlsson22742662007-07-21 05:21:51 +0000535 OS << "__PRETTY_FUNCTION__";
536 break;
537 }
538}
539
Reid Spencer5f016e22007-07-11 17:01:13 +0000540void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000541 unsigned value = Node->getValue();
Chris Lattnerc250aae2008-06-07 22:35:38 +0000542 if (Node->isWide())
543 OS << "L";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000544 switch (value) {
545 case '\\':
546 OS << "'\\\\'";
547 break;
548 case '\'':
549 OS << "'\\''";
550 break;
551 case '\a':
552 // TODO: K&R: the meaning of '\\a' is different in traditional C
553 OS << "'\\a'";
554 break;
555 case '\b':
556 OS << "'\\b'";
557 break;
558 // Nonstandard escape sequence.
559 /*case '\e':
560 OS << "'\\e'";
561 break;*/
562 case '\f':
563 OS << "'\\f'";
564 break;
565 case '\n':
566 OS << "'\\n'";
567 break;
568 case '\r':
569 OS << "'\\r'";
570 break;
571 case '\t':
572 OS << "'\\t'";
573 break;
574 case '\v':
575 OS << "'\\v'";
576 break;
577 default:
Ted Kremenek471733d2008-02-23 00:52:04 +0000578 if (value < 256 && isprint(value)) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000579 OS << "'" << (char)value << "'";
580 } else if (value < 256) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000581 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000582 } else {
583 // FIXME what to really do here?
584 OS << value;
585 }
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000586 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000587}
588
589void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
590 bool isSigned = Node->getType()->isSignedIntegerType();
591 OS << Node->getValue().toString(10, isSigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000592
Reid Spencer5f016e22007-07-11 17:01:13 +0000593 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall183700f2009-09-21 23:43:11 +0000594 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000595 default: assert(0 && "Unexpected type for integer literal!");
596 case BuiltinType::Int: break; // no suffix.
597 case BuiltinType::UInt: OS << 'U'; break;
598 case BuiltinType::Long: OS << 'L'; break;
599 case BuiltinType::ULong: OS << "UL"; break;
600 case BuiltinType::LongLong: OS << "LL"; break;
601 case BuiltinType::ULongLong: OS << "ULL"; break;
602 }
603}
604void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner86e499d2007-08-01 00:23:58 +0000605 // FIXME: print value more precisely.
Chris Lattnerda8249e2008-06-07 22:13:43 +0000606 OS << Node->getValueAsApproximateDouble();
Reid Spencer5f016e22007-07-11 17:01:13 +0000607}
Chris Lattner5d661452007-08-26 03:42:43 +0000608
609void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
610 PrintExpr(Node->getSubExpr());
611 OS << "i";
612}
613
Reid Spencer5f016e22007-07-11 17:01:13 +0000614void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
615 if (Str->isWide()) OS << 'L';
616 OS << '"';
Anders Carlssonee98ac52007-10-15 02:50:23 +0000617
Reid Spencer5f016e22007-07-11 17:01:13 +0000618 // FIXME: this doesn't print wstrings right.
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +0000619 llvm::StringRef StrData = Str->getString();
620 for (llvm::StringRef::iterator I = StrData.begin(), E = StrData.end();
621 I != E; ++I) {
622 unsigned char Char = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Chris Lattner9a81c872009-01-16 19:25:18 +0000624 switch (Char) {
625 default:
626 if (isprint(Char))
627 OS << (char)Char;
628 else // Output anything hard as an octal escape.
629 OS << '\\'
630 << (char)('0'+ ((Char >> 6) & 7))
631 << (char)('0'+ ((Char >> 3) & 7))
632 << (char)('0'+ ((Char >> 0) & 7));
633 break;
634 // Handle some common non-printable cases to make dumps prettier.
Reid Spencer5f016e22007-07-11 17:01:13 +0000635 case '\\': OS << "\\\\"; break;
636 case '"': OS << "\\\""; break;
637 case '\n': OS << "\\n"; break;
638 case '\t': OS << "\\t"; break;
639 case '\a': OS << "\\a"; break;
640 case '\b': OS << "\\b"; break;
641 }
642 }
643 OS << '"';
644}
645void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
646 OS << "(";
647 PrintExpr(Node->getSubExpr());
648 OS << ")";
649}
650void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +0000651 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000652 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Eli Friedman7df71ac2009-06-14 22:39:26 +0000654 // Print a space if this is an "identifier operator" like __real, or if
655 // it might be concatenated incorrectly like '+'.
Chris Lattner296bf192007-08-23 21:46:40 +0000656 switch (Node->getOpcode()) {
657 default: break;
John McCall2de56d12010-08-25 11:45:40 +0000658 case UO_Real:
659 case UO_Imag:
660 case UO_Extension:
Chris Lattner296bf192007-08-23 21:46:40 +0000661 OS << ' ';
662 break;
John McCall2de56d12010-08-25 11:45:40 +0000663 case UO_Plus:
664 case UO_Minus:
Eli Friedman7df71ac2009-06-14 22:39:26 +0000665 if (isa<UnaryOperator>(Node->getSubExpr()))
666 OS << ' ';
667 break;
Chris Lattner296bf192007-08-23 21:46:40 +0000668 }
669 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000670 PrintExpr(Node->getSubExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Reid Spencer5f016e22007-07-11 17:01:13 +0000672 if (Node->isPostfix())
673 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +0000674}
Chris Lattner704fe352007-08-30 17:59:59 +0000675
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000676void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
677 OS << "__builtin_offsetof(";
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000678 OS << Node->getTypeSourceInfo()->getType().getAsString(Policy) << ", ";
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000679 bool PrintedSomething = false;
680 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
681 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
682 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
683 // Array node
684 OS << "[";
685 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
686 OS << "]";
687 PrintedSomething = true;
688 continue;
689 }
Douglas Gregorcc8a5d52010-04-29 00:18:15 +0000690
691 // Skip implicit base indirections.
692 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
693 continue;
694
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000695 // Field or identifier node.
696 IdentifierInfo *Id = ON.getFieldName();
697 if (!Id)
698 continue;
Sean Huntc3021132010-05-05 15:23:54 +0000699
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000700 if (PrintedSomething)
701 OS << ".";
702 else
703 PrintedSomething = true;
Sean Huntc3021132010-05-05 15:23:54 +0000704 OS << Id->getName();
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000705 }
706 OS << ")";
707}
708
Sebastian Redl05189992008-11-11 17:56:53 +0000709void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
710 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
711 if (Node->isArgumentType())
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000712 OS << "(" << Node->getArgumentType().getAsString(Policy) << ")";
Sebastian Redl05189992008-11-11 17:56:53 +0000713 else {
714 OS << " ";
715 PrintExpr(Node->getArgumentExpr());
716 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000717}
718void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +0000719 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000720 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +0000721 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000722 OS << "]";
723}
724
Peter Collingbourned64e2372011-02-08 21:17:54 +0000725void StmtPrinter::PrintCallArgs(CallExpr *Call) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000726 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattner04421082008-04-08 04:40:51 +0000727 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
728 // Don't print any defaulted arguments
729 break;
730 }
731
Reid Spencer5f016e22007-07-11 17:01:13 +0000732 if (i) OS << ", ";
733 PrintExpr(Call->getArg(i));
734 }
Peter Collingbourned64e2372011-02-08 21:17:54 +0000735}
736
737void StmtPrinter::VisitCallExpr(CallExpr *Call) {
738 PrintExpr(Call->getCallee());
739 OS << "(";
740 PrintCallArgs(Call);
Reid Spencer5f016e22007-07-11 17:01:13 +0000741 OS << ")";
742}
743void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregorb3eef682009-01-08 22:45:41 +0000744 // FIXME: Suppress printing implicit bases (like "this")
745 PrintExpr(Node->getBase());
Fariborz Jahanian97fd83a2010-01-11 21:17:32 +0000746 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
747 if (FD->isAnonymousStructOrUnion())
748 return;
Douglas Gregorb3eef682009-01-08 22:45:41 +0000749 OS << (Node->isArrow() ? "->" : ".");
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000750 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
751 Qualifier->print(OS, Policy);
752
Abramo Bagnara25777432010-08-11 22:01:17 +0000753 OS << Node->getMemberNameInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000754
John McCall096832c2010-08-19 23:49:38 +0000755 if (Node->hasExplicitTemplateArgs())
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000756 OS << TemplateSpecializationType::PrintTemplateArgumentList(
757 Node->getTemplateArgs(),
758 Node->getNumTemplateArgs(),
759 Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000760}
Steve Narofff242b1b2009-07-24 17:54:45 +0000761void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
762 PrintExpr(Node->getBase());
763 OS << (Node->isArrow() ? "->isa" : ".isa");
764}
765
Nate Begeman213541a2008-04-18 23:10:10 +0000766void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +0000767 PrintExpr(Node->getBase());
768 OS << ".";
769 OS << Node->getAccessor().getName();
770}
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000771void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Fariborz Jahanian03e80e42010-06-30 16:31:08 +0000772 OS << "(" << Node->getType().getAsString(Policy) << ")";
Reid Spencer5f016e22007-07-11 17:01:13 +0000773 PrintExpr(Node->getSubExpr());
774}
Steve Naroffaff1edd2007-07-19 21:32:11 +0000775void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000776 OS << "(" << Node->getType().getAsString(Policy) << ")";
Steve Naroffaff1edd2007-07-19 21:32:11 +0000777 PrintExpr(Node->getInitializer());
778}
Steve Naroff49b45262007-07-13 16:58:59 +0000779void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroff90045e82007-07-13 23:32:42 +0000780 // No need to print anything, simply forward to the sub expression.
781 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +0000782}
Reid Spencer5f016e22007-07-11 17:01:13 +0000783void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
784 PrintExpr(Node->getLHS());
785 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
786 PrintExpr(Node->getRHS());
787}
Chris Lattnereb14fe82007-08-25 02:00:02 +0000788void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
789 PrintExpr(Node->getLHS());
790 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
791 PrintExpr(Node->getRHS());
792}
Reid Spencer5f016e22007-07-11 17:01:13 +0000793void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
794 PrintExpr(Node->getCond());
Mike Stump1eb44332009-09-09 15:08:12 +0000795
Ted Kremenek8e911c42007-11-26 18:27:54 +0000796 if (Node->getLHS()) {
797 OS << " ? ";
798 PrintExpr(Node->getLHS());
799 OS << " : ";
800 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000801 else { // Handle GCC extension where LHS can be NULL.
Ted Kremenek8e911c42007-11-26 18:27:54 +0000802 OS << " ?: ";
803 }
Mike Stump1eb44332009-09-09 15:08:12 +0000804
Reid Spencer5f016e22007-07-11 17:01:13 +0000805 PrintExpr(Node->getRHS());
806}
807
808// GNU extensions.
809
Chris Lattner6481a572007-08-03 17:31:20 +0000810void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000811 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000812}
813
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000814void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
815 OS << "(";
816 PrintRawCompoundStmt(E->getSubStmt());
817 OS << ")";
818}
819
Steve Naroffd04fdd52007-08-03 21:21:27 +0000820void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
821 OS << "__builtin_choose_expr(";
822 PrintExpr(Node->getCond());
Chris Lattner94f05e32007-08-04 00:20:15 +0000823 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000824 PrintExpr(Node->getLHS());
Chris Lattner94f05e32007-08-04 00:20:15 +0000825 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000826 PrintExpr(Node->getRHS());
827 OS << ")";
828}
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000829
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000830void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
831 OS << "__null";
832}
833
Eli Friedmand38617c2008-05-14 19:38:39 +0000834void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
835 OS << "__builtin_shufflevector(";
836 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
837 if (i) OS << ", ";
838 PrintExpr(Node->getExpr(i));
839 }
840 OS << ")";
841}
842
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000843void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000844 if (Node->getSyntacticForm()) {
845 Visit(Node->getSyntacticForm());
846 return;
847 }
848
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000849 OS << "{ ";
850 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
851 if (i) OS << ", ";
Douglas Gregor4c678342009-01-28 21:54:33 +0000852 if (Node->getInit(i))
853 PrintExpr(Node->getInit(i));
854 else
855 OS << "0";
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000856 }
857 OS << " }";
858}
859
Nate Begeman2ef13e52009-08-10 23:49:36 +0000860void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
861 OS << "( ";
862 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
863 if (i) OS << ", ";
864 PrintExpr(Node->getExpr(i));
865 }
866 OS << " )";
867}
868
Douglas Gregor05c13a32009-01-22 00:58:24 +0000869void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000870 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
871 DEnd = Node->designators_end();
872 D != DEnd; ++D) {
873 if (D->isFieldDesignator()) {
874 if (D->getDotLoc().isInvalid())
875 OS << D->getFieldName()->getName() << ":";
876 else
877 OS << "." << D->getFieldName()->getName();
878 } else {
879 OS << "[";
880 if (D->isArrayDesignator()) {
881 PrintExpr(Node->getArrayIndex(*D));
882 } else {
883 PrintExpr(Node->getArrayRangeStart(*D));
884 OS << " ... ";
Mike Stump1eb44332009-09-09 15:08:12 +0000885 PrintExpr(Node->getArrayRangeEnd(*D));
Douglas Gregor4c678342009-01-28 21:54:33 +0000886 }
887 OS << "]";
888 }
889 }
890
891 OS << " = ";
892 PrintExpr(Node->getInit());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000893}
894
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000895void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Chris Lattnere4f21422009-06-30 01:26:17 +0000896 if (Policy.LangOpts.CPlusPlus)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000897 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()";
898 else {
899 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")";
900 if (Node->getType()->isRecordType())
901 OS << "{}";
902 else
903 OS << 0;
904 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000905}
906
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000907void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000908 OS << "__builtin_va_arg(";
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000909 PrintExpr(Node->getSubExpr());
910 OS << ", ";
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000911 OS << Node->getType().getAsString(Policy);
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000912 OS << ")";
913}
914
Reid Spencer5f016e22007-07-11 17:01:13 +0000915// C++
Douglas Gregorb4609802008-11-14 16:09:21 +0000916void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
917 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
918 "",
919#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
920 Spelling,
921#include "clang/Basic/OperatorKinds.def"
922 };
923
924 OverloadedOperatorKind Kind = Node->getOperator();
925 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
926 if (Node->getNumArgs() == 1) {
927 OS << OpStrings[Kind] << ' ';
928 PrintExpr(Node->getArg(0));
929 } else {
930 PrintExpr(Node->getArg(0));
931 OS << ' ' << OpStrings[Kind];
932 }
933 } else if (Kind == OO_Call) {
934 PrintExpr(Node->getArg(0));
935 OS << '(';
936 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
937 if (ArgIdx > 1)
938 OS << ", ";
939 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
940 PrintExpr(Node->getArg(ArgIdx));
941 }
942 OS << ')';
943 } else if (Kind == OO_Subscript) {
944 PrintExpr(Node->getArg(0));
945 OS << '[';
946 PrintExpr(Node->getArg(1));
947 OS << ']';
948 } else if (Node->getNumArgs() == 1) {
949 OS << OpStrings[Kind] << ' ';
950 PrintExpr(Node->getArg(0));
951 } else if (Node->getNumArgs() == 2) {
952 PrintExpr(Node->getArg(0));
953 OS << ' ' << OpStrings[Kind] << ' ';
954 PrintExpr(Node->getArg(1));
955 } else {
956 assert(false && "unknown overloaded operator");
957 }
958}
Reid Spencer5f016e22007-07-11 17:01:13 +0000959
Douglas Gregor88a35142008-12-22 05:46:06 +0000960void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
961 VisitCallExpr(cast<CallExpr>(Node));
962}
963
Peter Collingbournee08ce652011-02-09 21:07:24 +0000964void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
965 PrintExpr(Node->getCallee());
966 OS << "<<<";
967 PrintCallArgs(Node->getConfig());
968 OS << ">>>(";
969 PrintCallArgs(Node);
970 OS << ")";
971}
972
Douglas Gregor49badde2008-10-27 19:41:14 +0000973void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
974 OS << Node->getCastName() << '<';
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000975 OS << Node->getTypeAsWritten().getAsString(Policy) << ">(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000976 PrintExpr(Node->getSubExpr());
977 OS << ")";
978}
979
Douglas Gregor49badde2008-10-27 19:41:14 +0000980void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
981 VisitCXXNamedCastExpr(Node);
982}
983
984void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
985 VisitCXXNamedCastExpr(Node);
986}
987
988void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
989 VisitCXXNamedCastExpr(Node);
990}
991
992void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
993 VisitCXXNamedCastExpr(Node);
994}
995
Sebastian Redlc42e1182008-11-11 11:37:55 +0000996void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
997 OS << "typeid(";
998 if (Node->isTypeOperand()) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000999 OS << Node->getTypeOperand().getAsString(Policy);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001000 } else {
1001 PrintExpr(Node->getExprOperand());
1002 }
1003 OS << ")";
1004}
1005
Francois Pichet01b7c302010-09-08 12:20:18 +00001006void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1007 OS << "__uuidof(";
1008 if (Node->isTypeOperand()) {
1009 OS << Node->getTypeOperand().getAsString(Policy);
1010 } else {
1011 PrintExpr(Node->getExprOperand());
1012 }
1013 OS << ")";
1014}
1015
Reid Spencer5f016e22007-07-11 17:01:13 +00001016void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1017 OS << (Node->getValue() ? "true" : "false");
1018}
1019
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001020void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1021 OS << "nullptr";
1022}
1023
Douglas Gregor796da182008-11-04 14:32:21 +00001024void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1025 OS << "this";
1026}
1027
Chris Lattner50dd2892008-02-26 00:51:44 +00001028void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1029 if (Node->getSubExpr() == 0)
1030 OS << "throw";
1031 else {
1032 OS << "throw ";
1033 PrintExpr(Node->getSubExpr());
1034 }
1035}
1036
Chris Lattner04421082008-04-08 04:40:51 +00001037void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1038 // Nothing to print: we picked up the default argument
1039}
1040
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001041void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001042 OS << Node->getType().getAsString(Policy);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001043 OS << "(";
1044 PrintExpr(Node->getSubExpr());
1045 OS << ")";
1046}
1047
Anders Carlssonfceb0a82009-05-30 20:03:25 +00001048void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1049 PrintExpr(Node->getSubExpr());
1050}
1051
Douglas Gregor506ae412009-01-16 18:33:17 +00001052void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001053 OS << Node->getType().getAsString(Policy);
Douglas Gregor506ae412009-01-16 18:33:17 +00001054 OS << "(";
1055 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001056 ArgEnd = Node->arg_end();
Douglas Gregor506ae412009-01-16 18:33:17 +00001057 Arg != ArgEnd; ++Arg) {
1058 if (Arg != Node->arg_begin())
1059 OS << ", ";
1060 PrintExpr(*Arg);
1061 }
1062 OS << ")";
1063}
1064
Douglas Gregored8abf12010-07-08 06:14:04 +00001065void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregorab6677e2010-09-08 00:15:04 +00001066 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
1067 OS << TSInfo->getType().getAsString(Policy) << "()";
1068 else
1069 OS << Node->getType().getAsString(Policy) << "()";
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001070}
1071
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001072void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1073 if (E->isGlobalNew())
1074 OS << "::";
1075 OS << "new ";
1076 unsigned NumPlace = E->getNumPlacementArgs();
1077 if (NumPlace > 0) {
1078 OS << "(";
1079 PrintExpr(E->getPlacementArg(0));
1080 for (unsigned i = 1; i < NumPlace; ++i) {
1081 OS << ", ";
1082 PrintExpr(E->getPlacementArg(i));
1083 }
1084 OS << ") ";
1085 }
1086 if (E->isParenTypeId())
1087 OS << "(";
Sebastian Redl6fec6482008-12-02 22:08:59 +00001088 std::string TypeS;
1089 if (Expr *Size = E->getArraySize()) {
1090 llvm::raw_string_ostream s(TypeS);
Eli Friedman6e1a3452009-05-30 05:32:46 +00001091 Size->printPretty(s, Context, Helper, Policy);
Sebastian Redl6fec6482008-12-02 22:08:59 +00001092 s.flush();
1093 TypeS = "[" + TypeS + "]";
1094 }
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001095 E->getAllocatedType().getAsStringInternal(TypeS, Policy);
Sebastian Redl6fec6482008-12-02 22:08:59 +00001096 OS << TypeS;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001097 if (E->isParenTypeId())
1098 OS << ")";
1099
1100 if (E->hasInitializer()) {
1101 OS << "(";
1102 unsigned NumCons = E->getNumConstructorArgs();
1103 if (NumCons > 0) {
1104 PrintExpr(E->getConstructorArg(0));
1105 for (unsigned i = 1; i < NumCons; ++i) {
1106 OS << ", ";
1107 PrintExpr(E->getConstructorArg(i));
1108 }
1109 }
1110 OS << ")";
1111 }
1112}
1113
1114void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1115 if (E->isGlobalDelete())
1116 OS << "::";
1117 OS << "delete ";
1118 if (E->isArrayForm())
1119 OS << "[] ";
1120 PrintExpr(E->getArgument());
1121}
1122
Douglas Gregora71d8192009-09-04 17:36:40 +00001123void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1124 PrintExpr(E->getBase());
1125 if (E->isArrow())
1126 OS << "->";
1127 else
1128 OS << '.';
1129 if (E->getQualifier())
1130 E->getQualifier()->print(OS, Policy);
Mike Stump1eb44332009-09-09 15:08:12 +00001131
Douglas Gregora71d8192009-09-04 17:36:40 +00001132 std::string TypeS;
Douglas Gregora2e7dd22010-02-25 01:56:36 +00001133 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1134 OS << II->getName();
1135 else
1136 E->getDestroyedType().getAsStringInternal(TypeS, Policy);
Douglas Gregora71d8192009-09-04 17:36:40 +00001137 OS << TypeS;
1138}
1139
Anders Carlssone349bea2009-04-23 02:32:43 +00001140void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Douglas Gregord0fb3ad2011-01-24 17:25:03 +00001141 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1142 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1143 // Don't print any defaulted arguments
1144 break;
1145 }
1146
1147 if (i) OS << ", ";
1148 PrintExpr(E->getArg(i));
Fariborz Jahanianc75da512010-01-13 21:41:11 +00001149 }
Anders Carlssone349bea2009-04-23 02:32:43 +00001150}
1151
John McCall4765fa02010-12-06 08:20:24 +00001152void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Anders Carlsson02bbfa32009-04-24 22:47:04 +00001153 // Just forward to the sub expression.
1154 PrintExpr(E->getSubExpr());
1155}
1156
Mike Stump1eb44332009-09-09 15:08:12 +00001157void
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001158StmtPrinter::VisitCXXUnresolvedConstructExpr(
1159 CXXUnresolvedConstructExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001160 OS << Node->getTypeAsWritten().getAsString(Policy);
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001161 OS << "(";
1162 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001163 ArgEnd = Node->arg_end();
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001164 Arg != ArgEnd; ++Arg) {
1165 if (Arg != Node->arg_begin())
1166 OS << ", ";
1167 PrintExpr(*Arg);
1168 }
1169 OS << ")";
1170}
1171
John McCall865d4472009-11-19 22:55:06 +00001172void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1173 CXXDependentScopeMemberExpr *Node) {
John McCallaa81e162009-12-01 22:10:20 +00001174 if (!Node->isImplicitAccess()) {
1175 PrintExpr(Node->getBase());
1176 OS << (Node->isArrow() ? "->" : ".");
1177 }
Douglas Gregora38c6872009-09-03 16:14:30 +00001178 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1179 Qualifier->print(OS, Policy);
John McCallaa81e162009-12-01 22:10:20 +00001180 else if (Node->hasExplicitTemplateArgs())
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001181 // FIXME: Track use of "template" keyword explicitly?
1182 OS << "template ";
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Abramo Bagnara25777432010-08-11 22:01:17 +00001184 OS << Node->getMemberNameInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00001185
John McCallaa81e162009-12-01 22:10:20 +00001186 if (Node->hasExplicitTemplateArgs()) {
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001187 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1188 Node->getTemplateArgs(),
1189 Node->getNumTemplateArgs(),
1190 Policy);
1191 }
Douglas Gregor1c0ca592009-05-22 21:13:27 +00001192}
1193
John McCall129e2df2009-11-30 22:42:35 +00001194void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCallaa81e162009-12-01 22:10:20 +00001195 if (!Node->isImplicitAccess()) {
1196 PrintExpr(Node->getBase());
1197 OS << (Node->isArrow() ? "->" : ".");
1198 }
John McCall129e2df2009-11-30 22:42:35 +00001199 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1200 Qualifier->print(OS, Policy);
1201
1202 // FIXME: this might originally have been written with 'template'
1203
Abramo Bagnara25777432010-08-11 22:01:17 +00001204 OS << Node->getMemberNameInfo();
John McCall129e2df2009-11-30 22:42:35 +00001205
1206 if (Node->hasExplicitTemplateArgs()) {
1207 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1208 Node->getTemplateArgs(),
1209 Node->getNumTemplateArgs(),
1210 Policy);
1211 }
1212}
1213
Sebastian Redl64b45f72009-01-05 20:52:13 +00001214static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1215 switch (UTT) {
Francois Pichet38c2b732010-12-07 00:55:57 +00001216 default: llvm_unreachable("Unknown unary type trait");
Sebastian Redl64b45f72009-01-05 20:52:13 +00001217 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1218 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1219 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1220 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1221 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1222 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1223 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1224 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1225 case UTT_IsAbstract: return "__is_abstract";
1226 case UTT_IsClass: return "__is_class";
1227 case UTT_IsEmpty: return "__is_empty";
1228 case UTT_IsEnum: return "__is_enum";
1229 case UTT_IsPOD: return "__is_pod";
1230 case UTT_IsPolymorphic: return "__is_polymorphic";
1231 case UTT_IsUnion: return "__is_union";
1232 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00001233 return "";
1234}
1235
1236static const char *getTypeTraitName(BinaryTypeTrait BTT) {
1237 switch (BTT) {
Francois Pichetf1872372010-12-08 22:35:30 +00001238 case BTT_IsBaseOf: return "__is_base_of";
1239 case BTT_TypeCompatible: return "__builtin_types_compatible_p";
Douglas Gregor9f361132011-01-27 20:28:01 +00001240 case BTT_IsConvertibleTo: return "__is_convertible_to";
Francois Pichet6ad6f282010-12-07 00:08:36 +00001241 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00001242 return "";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001243}
1244
1245void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1246 OS << getTypeTraitName(E->getTrait()) << "("
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001247 << E->getQueriedType().getAsString(Policy) << ")";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001248}
1249
Francois Pichet6ad6f282010-12-07 00:08:36 +00001250void StmtPrinter::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
1251 OS << getTypeTraitName(E->getTrait()) << "("
1252 << E->getLhsType().getAsString(Policy) << ","
1253 << E->getRhsType().getAsString(Policy) << ")";
1254}
1255
Sebastian Redl2e156222010-09-10 20:55:43 +00001256void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1257 OS << "noexcept(";
1258 PrintExpr(E->getOperand());
1259 OS << ")";
1260}
1261
Douglas Gregoree8aff02011-01-04 17:33:58 +00001262void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregorbe230c32011-01-03 17:17:50 +00001263 PrintExpr(E->getPattern());
1264 OS << "...";
1265}
1266
Douglas Gregoree8aff02011-01-04 17:33:58 +00001267void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
1268 OS << "sizeof...(" << E->getPack()->getNameAsString() << ")";
1269}
1270
Douglas Gregorc7793c72011-01-15 01:15:58 +00001271void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
1272 SubstNonTypeTemplateParmPackExpr *Node) {
1273 OS << Node->getParameterPack()->getNameAsString();
1274}
1275
Mike Stump1eb44332009-09-09 15:08:12 +00001276// Obj-C
Anders Carlsson55085182007-08-21 17:43:55 +00001277
1278void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1279 OS << "@";
1280 VisitStringLiteral(Node->getString());
1281}
Reid Spencer5f016e22007-07-11 17:01:13 +00001282
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001283void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001284 OS << "@encode(" << Node->getEncodedType().getAsString(Policy) << ')';
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001285}
1286
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001287void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001288 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001289}
1290
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001291void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramer900fc632010-04-17 09:33:03 +00001292 OS << "@protocol(" << Node->getProtocol() << ')';
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001293}
1294
Steve Naroff563477d2007-09-18 23:55:05 +00001295void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1296 OS << "[";
Douglas Gregor04badcf2010-04-21 00:45:42 +00001297 switch (Mess->getReceiverKind()) {
1298 case ObjCMessageExpr::Instance:
1299 PrintExpr(Mess->getInstanceReceiver());
1300 break;
1301
1302 case ObjCMessageExpr::Class:
1303 OS << Mess->getClassReceiver().getAsString(Policy);
1304 break;
1305
1306 case ObjCMessageExpr::SuperInstance:
1307 case ObjCMessageExpr::SuperClass:
1308 OS << "Super";
1309 break;
1310 }
1311
Ted Kremenekc29efd82008-05-02 17:32:38 +00001312 OS << ' ';
Ted Kremenek97b7f262008-04-16 04:30:16 +00001313 Selector selector = Mess->getSelector();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001314 if (selector.isUnarySelector()) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001315 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001316 } else {
1317 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001318 if (i < selector.getNumArgs()) {
1319 if (i > 0) OS << ' ';
1320 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner39f34e92008-11-24 04:00:27 +00001321 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremenekc29efd82008-05-02 17:32:38 +00001322 else
1323 OS << ":";
1324 }
1325 else OS << ", "; // Handle variadic methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001326
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001327 PrintExpr(Mess->getArg(i));
1328 }
Steve Naroff563477d2007-09-18 23:55:05 +00001329 }
1330 OS << "]";
1331}
1332
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001333
Steve Naroff4eb206b2008-09-03 18:15:37 +00001334void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001335 BlockDecl *BD = Node->getBlockDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001336 OS << "^";
Mike Stump1eb44332009-09-09 15:08:12 +00001337
Steve Naroff4eb206b2008-09-03 18:15:37 +00001338 const FunctionType *AFT = Node->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +00001339
Douglas Gregor72564e72009-02-26 23:50:07 +00001340 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001341 OS << "()";
Douglas Gregor72564e72009-02-26 23:50:07 +00001342 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001343 OS << '(';
1344 std::string ParamStr;
Steve Naroff56ee6892008-10-08 17:01:13 +00001345 for (BlockDecl::param_iterator AI = BD->param_begin(),
1346 E = BD->param_end(); AI != E; ++AI) {
1347 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner39f34e92008-11-24 04:00:27 +00001348 ParamStr = (*AI)->getNameAsString();
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001349 (*AI)->getType().getAsStringInternal(ParamStr, Policy);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001350 OS << ParamStr;
1351 }
Mike Stump1eb44332009-09-09 15:08:12 +00001352
Douglas Gregor72564e72009-02-26 23:50:07 +00001353 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001354 if (FT->isVariadic()) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001355 if (!BD->param_empty()) OS << ", ";
Steve Naroff4eb206b2008-09-03 18:15:37 +00001356 OS << "...";
1357 }
1358 OS << ')';
1359 }
1360}
1361
Steve Naroff4eb206b2008-09-03 18:15:37 +00001362void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Benjamin Kramer900fc632010-04-17 09:33:03 +00001363 OS << Node->getDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001364}
John McCall7cd7d1a2010-11-15 23:31:06 +00001365
1366void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {}
1367
Reid Spencer5f016e22007-07-11 17:01:13 +00001368//===----------------------------------------------------------------------===//
1369// Stmt method implementations
1370//===----------------------------------------------------------------------===//
1371
Eli Friedman48d14a22009-05-30 05:03:24 +00001372void Stmt::dumpPretty(ASTContext& Context) const {
Argyrios Kyrtzidis96680332010-08-09 10:54:31 +00001373 printPretty(llvm::errs(), Context, 0,
Chris Lattnere4f21422009-06-30 01:26:17 +00001374 PrintingPolicy(Context.getLangOptions()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001375}
1376
Eli Friedman48d14a22009-05-30 05:03:24 +00001377void Stmt::printPretty(llvm::raw_ostream &OS, ASTContext& Context,
1378 PrinterHelper* Helper,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001379 const PrintingPolicy &Policy,
1380 unsigned Indentation) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001381 if (this == 0) {
1382 OS << "<NULL>";
1383 return;
1384 }
1385
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001386 if (Policy.Dump && &Context) {
Argyrios Kyrtzidis96680332010-08-09 10:54:31 +00001387 dump(OS, Context.getSourceManager());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001388 return;
1389 }
Mike Stump1eb44332009-09-09 15:08:12 +00001390
Eli Friedman48d14a22009-05-30 05:03:24 +00001391 StmtPrinter P(OS, Context, Helper, Policy, Indentation);
Chris Lattnerc5598cb2007-08-21 04:04:25 +00001392 P.Visit(const_cast<Stmt*>(this));
Reid Spencer5f016e22007-07-11 17:01:13 +00001393}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001394
1395//===----------------------------------------------------------------------===//
1396// PrinterHelper
1397//===----------------------------------------------------------------------===//
1398
1399// Implement virtual destructor.
Gabor Greif84675832007-09-11 15:32:40 +00001400PrinterHelper::~PrinterHelper() {}