blob: 5b1654c92402167bc62d0c3213cef0c7e66785c8 [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> {
Chris Lattner5f9e2722011-07-23 10:55:15 +000031 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:
Chris Lattner5f9e2722011-07-23 10:55:15 +000038 StmtPrinter(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);
John Wiegley28bbe4b2011-04-28 01:08:34 +000069 void PrintRawSEHExceptHandler(SEHExceptStmt *S);
70 void PrintRawSEHFinallyStmt(SEHFinallyStmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +000071
Reid Spencer5f016e22007-07-11 17:01:13 +000072 void PrintExpr(Expr *E) {
73 if (E)
Chris Lattnerc5598cb2007-08-21 04:04:25 +000074 Visit(E);
Reid Spencer5f016e22007-07-11 17:01:13 +000075 else
76 OS << "<null expr>";
77 }
Mike Stump1eb44332009-09-09 15:08:12 +000078
Chris Lattner5f9e2722011-07-23 10:55:15 +000079 raw_ostream &Indent(int Delta = 0) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000080 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
81 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +000082 return OS;
83 }
Mike Stump1eb44332009-09-09 15:08:12 +000084
Mike Stump1eb44332009-09-09 15:08:12 +000085 void Visit(Stmt* S) {
Ted Kremenek42a509f2007-08-31 21:30:12 +000086 if (Helper && Helper->handledStmt(S,OS))
87 return;
88 else StmtVisitor<StmtPrinter>::Visit(S);
89 }
Argyrios Kyrtzidis78e4cc72010-08-15 01:15:33 +000090
Chandler Carruth61e38282010-10-23 08:21:37 +000091 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis78e4cc72010-08-15 01:15:33 +000092 Indent() << "<<unknown stmt type>>\n";
93 }
Chandler Carruth61e38282010-10-23 08:21:37 +000094 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis78e4cc72010-08-15 01:15:33 +000095 OS << "<<unknown expr type>>";
96 }
97 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Mike Stump1eb44332009-09-09 15:08:12 +000098
Argyrios Kyrtzidis78e4cc72010-08-15 01:15:33 +000099#define ABSTRACT_STMT(CLASS)
Douglas Gregorf2cad862008-11-14 12:46:07 +0000100#define STMT(CLASS, PARENT) \
Chris Lattnerc5598cb2007-08-21 04:04:25 +0000101 void Visit##CLASS(CLASS *Node);
Sean Hunt4bfe1962010-05-05 15:24:00 +0000102#include "clang/AST/StmtNodes.inc"
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 };
104}
105
106//===----------------------------------------------------------------------===//
107// Stmt printing methods.
108//===----------------------------------------------------------------------===//
109
Reid Spencer5f016e22007-07-11 17:01:13 +0000110/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
111/// with no newline after the }.
112void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
113 OS << "{\n";
114 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
115 I != E; ++I)
116 PrintStmt(*I);
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 Indent() << "}";
119}
120
121void StmtPrinter::PrintRawDecl(Decl *D) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000122 D->print(OS, Policy, IndentLevel);
Mike Stump071e4da2009-02-10 20:16:46 +0000123}
124
Ted Kremenekecd64c52008-10-06 18:39:36 +0000125void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
Eli Friedmandb23b152009-05-30 00:19:54 +0000126 DeclStmt::decl_iterator Begin = S->decl_begin(), End = S->decl_end();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000127 SmallVector<Decl*, 2> Decls;
Mike Stump1eb44332009-09-09 15:08:12 +0000128 for ( ; Begin != End; ++Begin)
Eli Friedman42f42c02009-05-30 04:20:30 +0000129 Decls.push_back(*Begin);
Eli Friedmandb23b152009-05-30 00:19:54 +0000130
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000131 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
Ted Kremenekecd64c52008-10-06 18:39:36 +0000132}
Reid Spencer5f016e22007-07-11 17:01:13 +0000133
134void StmtPrinter::VisitNullStmt(NullStmt *Node) {
135 Indent() << ";\n";
136}
137
138void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Eli Friedmandb23b152009-05-30 00:19:54 +0000139 Indent();
140 PrintRawDeclStmt(Node);
141 OS << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000142}
143
144void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
145 Indent();
146 PrintRawCompoundStmt(Node);
147 OS << "\n";
148}
149
150void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
151 Indent(-1) << "case ";
152 PrintExpr(Node->getLHS());
153 if (Node->getRHS()) {
154 OS << " ... ";
155 PrintExpr(Node->getRHS());
156 }
157 OS << ":\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 PrintStmt(Node->getSubStmt(), 0);
160}
161
162void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
163 Indent(-1) << "default:\n";
164 PrintStmt(Node->getSubStmt(), 0);
165}
166
167void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
168 Indent(-1) << Node->getName() << ":\n";
169 PrintStmt(Node->getSubStmt(), 0);
170}
171
172void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000173 OS << "if (";
Reid Spencer5f016e22007-07-11 17:01:13 +0000174 PrintExpr(If->getCond());
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000175 OS << ')';
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Reid Spencer5f016e22007-07-11 17:01:13 +0000177 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
178 OS << ' ';
179 PrintRawCompoundStmt(CS);
180 OS << (If->getElse() ? ' ' : '\n');
181 } else {
182 OS << '\n';
183 PrintStmt(If->getThen());
184 if (If->getElse()) Indent();
185 }
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Reid Spencer5f016e22007-07-11 17:01:13 +0000187 if (Stmt *Else = If->getElse()) {
188 OS << "else";
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
191 OS << ' ';
192 PrintRawCompoundStmt(CS);
193 OS << '\n';
194 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
195 OS << ' ';
196 PrintRawIfStmt(ElseIf);
197 } else {
198 OS << '\n';
199 PrintStmt(If->getElse());
200 }
201 }
202}
203
204void StmtPrinter::VisitIfStmt(IfStmt *If) {
205 Indent();
206 PrintRawIfStmt(If);
207}
208
209void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
210 Indent() << "switch (";
211 PrintExpr(Node->getCond());
212 OS << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Reid Spencer5f016e22007-07-11 17:01:13 +0000214 // Pretty print compoundstmt bodies (very common).
215 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
216 OS << " ";
217 PrintRawCompoundStmt(CS);
218 OS << "\n";
219 } else {
220 OS << "\n";
221 PrintStmt(Node->getBody());
222 }
223}
224
225void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
226 Indent() << "while (";
227 PrintExpr(Node->getCond());
228 OS << ")\n";
229 PrintStmt(Node->getBody());
230}
231
232void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner8bdcc472007-09-15 21:49:37 +0000233 Indent() << "do ";
234 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
235 PrintRawCompoundStmt(CS);
236 OS << " ";
237 } else {
238 OS << "\n";
239 PrintStmt(Node->getBody());
240 Indent();
241 }
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Eli Friedmanb3e22962009-05-17 01:05:34 +0000243 OS << "while (";
Reid Spencer5f016e22007-07-11 17:01:13 +0000244 PrintExpr(Node->getCond());
Eli Friedmanb3e22962009-05-17 01:05:34 +0000245 OS << ");\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000246}
247
248void StmtPrinter::VisitForStmt(ForStmt *Node) {
249 Indent() << "for (";
250 if (Node->getInit()) {
251 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000252 PrintRawDeclStmt(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 else
254 PrintExpr(cast<Expr>(Node->getInit()));
255 }
Chris Lattner8bdcc472007-09-15 21:49:37 +0000256 OS << ";";
257 if (Node->getCond()) {
258 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000259 PrintExpr(Node->getCond());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000260 }
261 OS << ";";
262 if (Node->getInc()) {
263 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 PrintExpr(Node->getInc());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000265 }
266 OS << ") ";
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Chris Lattner8bdcc472007-09-15 21:49:37 +0000268 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
269 PrintRawCompoundStmt(CS);
270 OS << "\n";
271 } else {
272 OS << "\n";
273 PrintStmt(Node->getBody());
274 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000275}
276
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000277void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000278 Indent() << "for (";
279 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000280 PrintRawDeclStmt(DS);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000281 else
282 PrintExpr(cast<Expr>(Node->getElement()));
283 OS << " in ";
284 PrintExpr(Node->getCollection());
285 OS << ") ";
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000287 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
288 PrintRawCompoundStmt(CS);
289 OS << "\n";
290 } else {
291 OS << "\n";
292 PrintStmt(Node->getBody());
293 }
294}
295
Richard Smithad762fc2011-04-14 22:09:26 +0000296void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
297 Indent() << "for (";
298 PrintingPolicy SubPolicy(Policy);
299 SubPolicy.SuppressInitializers = true;
300 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
301 OS << " : ";
302 PrintExpr(Node->getRangeInit());
303 OS << ") {\n";
304 PrintStmt(Node->getBody());
305 Indent() << "}\n";
306}
307
Douglas Gregorba0513d2011-10-25 01:33:02 +0000308void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
309 Indent();
310 if (Node->isIfExists())
311 OS << "__if_exists (";
312 else
313 OS << "__if_not_exists (";
314
315 if (NestedNameSpecifier *Qualifier
316 = Node->getQualifierLoc().getNestedNameSpecifier())
317 Qualifier->print(OS, Policy);
318
319 OS << Node->getNameInfo() << ") ";
320
321 PrintRawCompoundStmt(Node->getSubStmt());
322}
323
Reid Spencer5f016e22007-07-11 17:01:13 +0000324void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
325 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
326}
327
328void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
329 Indent() << "goto *";
330 PrintExpr(Node->getTarget());
331 OS << ";\n";
332}
333
334void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
335 Indent() << "continue;\n";
336}
337
338void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
339 Indent() << "break;\n";
340}
341
342
343void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
344 Indent() << "return";
345 if (Node->getRetValue()) {
346 OS << " ";
347 PrintExpr(Node->getRetValue());
348 }
349 OS << ";\n";
350}
351
Chris Lattnerfe795952007-10-29 04:04:16 +0000352
353void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson39c47b52007-11-23 23:12:25 +0000354 Indent() << "asm ";
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Anders Carlsson39c47b52007-11-23 23:12:25 +0000356 if (Node->isVolatile())
357 OS << "volatile ";
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Anders Carlsson39c47b52007-11-23 23:12:25 +0000359 OS << "(";
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000360 VisitStringLiteral(Node->getAsmString());
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Anders Carlssonb235fc22007-11-22 01:36:19 +0000362 // Outputs
363 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
364 Node->getNumClobbers() != 0)
365 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Anders Carlssonb235fc22007-11-22 01:36:19 +0000367 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
368 if (i != 0)
369 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Anders Carlssonb235fc22007-11-22 01:36:19 +0000371 if (!Node->getOutputName(i).empty()) {
372 OS << '[';
373 OS << Node->getOutputName(i);
374 OS << "] ";
375 }
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Chris Lattnerb3277932009-03-10 04:59:06 +0000377 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000378 OS << " ";
379 Visit(Node->getOutputExpr(i));
380 }
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Anders Carlssonb235fc22007-11-22 01:36:19 +0000382 // Inputs
383 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
384 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Anders Carlssonb235fc22007-11-22 01:36:19 +0000386 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
387 if (i != 0)
388 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Anders Carlssonb235fc22007-11-22 01:36:19 +0000390 if (!Node->getInputName(i).empty()) {
391 OS << '[';
392 OS << Node->getInputName(i);
393 OS << "] ";
394 }
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Chris Lattnerb3277932009-03-10 04:59:06 +0000396 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000397 OS << " ";
398 Visit(Node->getInputExpr(i));
399 }
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Anders Carlssonb235fc22007-11-22 01:36:19 +0000401 // Clobbers
402 if (Node->getNumClobbers() != 0)
403 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Anders Carlssonb235fc22007-11-22 01:36:19 +0000405 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
406 if (i != 0)
407 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Anders Carlssonb235fc22007-11-22 01:36:19 +0000409 VisitStringLiteral(Node->getClobber(i));
410 }
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000412 OS << ");\n";
Chris Lattnerfe795952007-10-29 04:04:16 +0000413}
414
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000415void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000416 Indent() << "@try";
417 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
418 PrintRawCompoundStmt(TS);
419 OS << "\n";
420 }
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000422 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
423 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000424 Indent() << "@catch(";
Steve Naroff7ba138a2009-03-03 19:52:17 +0000425 if (catchStmt->getCatchParamDecl()) {
426 if (Decl *DS = catchStmt->getCatchParamDecl())
427 PrintRawDecl(DS);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000428 }
429 OS << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000430 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
431 PrintRawCompoundStmt(CS);
432 OS << "\n";
433 }
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000434 }
Mike Stump1eb44332009-09-09 15:08:12 +0000435
436 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
437 Node->getFinallyStmt())) {
Fariborz Jahanian1e7eab42007-11-07 00:46:42 +0000438 Indent() << "@finally";
439 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000440 OS << "\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000441 }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000442}
443
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000444void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000445}
446
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000447void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000448 Indent() << "@catch (...) { /* todo */ } \n";
449}
450
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000451void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000452 Indent() << "@throw";
453 if (Node->getThrowExpr()) {
454 OS << " ";
455 PrintExpr(Node->getThrowExpr());
456 }
457 OS << ";\n";
458}
459
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000460void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000461 Indent() << "@synchronized (";
462 PrintExpr(Node->getSynchExpr());
463 OS << ")";
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000464 PrintRawCompoundStmt(Node->getSynchBody());
465 OS << "\n";
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000466}
467
John McCallf85e1932011-06-15 23:02:42 +0000468void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
469 Indent() << "@autoreleasepool";
470 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
471 OS << "\n";
472}
473
Sebastian Redl8351da02008-12-22 21:35:02 +0000474void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
475 OS << "catch (";
Sebastian Redl4b07b292008-12-22 19:15:10 +0000476 if (Decl *ExDecl = Node->getExceptionDecl())
477 PrintRawDecl(ExDecl);
478 else
479 OS << "...";
480 OS << ") ";
481 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl8351da02008-12-22 21:35:02 +0000482}
483
484void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
485 Indent();
486 PrintRawCXXCatchStmt(Node);
487 OS << "\n";
488}
489
490void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
491 Indent() << "try ";
492 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump1eb44332009-09-09 15:08:12 +0000493 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl8351da02008-12-22 21:35:02 +0000494 OS << " ";
495 PrintRawCXXCatchStmt(Node->getHandler(i));
496 }
Sebastian Redl4b07b292008-12-22 19:15:10 +0000497 OS << "\n";
498}
499
John Wiegley28bbe4b2011-04-28 01:08:34 +0000500void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
501 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
502 PrintRawCompoundStmt(Node->getTryBlock());
503 SEHExceptStmt *E = Node->getExceptHandler();
504 SEHFinallyStmt *F = Node->getFinallyHandler();
505 if(E)
506 PrintRawSEHExceptHandler(E);
507 else {
508 assert(F && "Must have a finally block...");
509 PrintRawSEHFinallyStmt(F);
510 }
511 OS << "\n";
512}
513
514void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
515 OS << "__finally ";
516 PrintRawCompoundStmt(Node->getBlock());
517 OS << "\n";
518}
519
520void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
521 OS << "__except (";
522 VisitExpr(Node->getFilterExpr());
523 OS << ")\n";
524 PrintRawCompoundStmt(Node->getBlock());
525 OS << "\n";
526}
527
528void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
529 Indent();
530 PrintRawSEHExceptHandler(Node);
531 OS << "\n";
532}
533
534void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
535 Indent();
536 PrintRawSEHFinallyStmt(Node);
537 OS << "\n";
538}
539
Reid Spencer5f016e22007-07-11 17:01:13 +0000540//===----------------------------------------------------------------------===//
541// Expr printing methods.
542//===----------------------------------------------------------------------===//
543
Reid Spencer5f016e22007-07-11 17:01:13 +0000544void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000545 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
546 Qualifier->print(OS, Policy);
Abramo Bagnara25777432010-08-11 22:01:17 +0000547 OS << Node->getNameInfo();
John McCall096832c2010-08-19 23:49:38 +0000548 if (Node->hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000549 OS << TemplateSpecializationType::PrintTemplateArgumentList(
550 Node->getTemplateArgs(),
551 Node->getNumTemplateArgs(),
Sean Huntc3021132010-05-05 15:23:54 +0000552 Policy);
Douglas Gregor1a49af92009-01-06 05:10:23 +0000553}
554
John McCall865d4472009-11-19 22:55:06 +0000555void StmtPrinter::VisitDependentScopeDeclRefExpr(
556 DependentScopeDeclRefExpr *Node) {
Axel Naumann274f83c2011-01-24 15:44:00 +0000557 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
558 Qualifier->print(OS, Policy);
Abramo Bagnara25777432010-08-11 22:01:17 +0000559 OS << Node->getNameInfo();
John McCallf7a1a742009-11-24 19:00:30 +0000560 if (Node->hasExplicitTemplateArgs())
561 OS << TemplateSpecializationType::PrintTemplateArgumentList(
562 Node->getTemplateArgs(),
563 Node->getNumTemplateArgs(),
564 Policy);
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000565}
566
John McCallba135432009-11-21 08:51:07 +0000567void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000568 if (Node->getQualifier())
569 Node->getQualifier()->print(OS, Policy);
Abramo Bagnara25777432010-08-11 22:01:17 +0000570 OS << Node->getNameInfo();
John McCallf7a1a742009-11-24 19:00:30 +0000571 if (Node->hasExplicitTemplateArgs())
572 OS << TemplateSpecializationType::PrintTemplateArgumentList(
573 Node->getTemplateArgs(),
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000574 Node->getNumTemplateArgs(),
John McCallf7a1a742009-11-24 19:00:30 +0000575 Policy);
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000576}
577
Steve Naroff7779db42007-11-12 14:29:37 +0000578void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000579 if (Node->getBase()) {
580 PrintExpr(Node->getBase());
581 OS << (Node->isArrow() ? "->" : ".");
582 }
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000583 OS << *Node->getDecl();
Steve Naroff7779db42007-11-12 14:29:37 +0000584}
585
Steve Naroffae784072008-05-30 00:40:33 +0000586void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000587 if (Node->isSuperReceiver())
588 OS << "super.";
589 else if (Node->getBase()) {
Steve Naroffae784072008-05-30 00:40:33 +0000590 PrintExpr(Node->getBase());
591 OS << ".";
592 }
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000593
John McCall12f78a62010-12-02 01:19:52 +0000594 if (Node->isImplicitProperty())
595 OS << Node->getImplicitPropertyGetter()->getSelector().getAsString();
596 else
597 OS << Node->getExplicitProperty()->getName();
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000598}
599
Chris Lattnerd9f69102008-08-10 01:53:14 +0000600void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson22742662007-07-21 05:21:51 +0000601 switch (Node->getIdentType()) {
602 default:
David Blaikieb219cfc2011-09-23 05:06:16 +0000603 llvm_unreachable("unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000604 case PredefinedExpr::Func:
Anders Carlsson22742662007-07-21 05:21:51 +0000605 OS << "__func__";
606 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000607 case PredefinedExpr::Function:
Anders Carlsson22742662007-07-21 05:21:51 +0000608 OS << "__FUNCTION__";
609 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000610 case PredefinedExpr::PrettyFunction:
Anders Carlsson22742662007-07-21 05:21:51 +0000611 OS << "__PRETTY_FUNCTION__";
612 break;
613 }
614}
615
Reid Spencer5f016e22007-07-11 17:01:13 +0000616void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000617 unsigned value = Node->getValue();
Douglas Gregor5cee1192011-07-27 05:40:30 +0000618
619 switch (Node->getKind()) {
620 case CharacterLiteral::Ascii: break; // no prefix.
621 case CharacterLiteral::Wide: OS << 'L'; break;
622 case CharacterLiteral::UTF16: OS << 'u'; break;
623 case CharacterLiteral::UTF32: OS << 'U'; break;
624 }
625
Chris Lattner8bf9f072007-07-13 23:58:20 +0000626 switch (value) {
627 case '\\':
628 OS << "'\\\\'";
629 break;
630 case '\'':
631 OS << "'\\''";
632 break;
633 case '\a':
634 // TODO: K&R: the meaning of '\\a' is different in traditional C
635 OS << "'\\a'";
636 break;
637 case '\b':
638 OS << "'\\b'";
639 break;
640 // Nonstandard escape sequence.
641 /*case '\e':
642 OS << "'\\e'";
643 break;*/
644 case '\f':
645 OS << "'\\f'";
646 break;
647 case '\n':
648 OS << "'\\n'";
649 break;
650 case '\r':
651 OS << "'\\r'";
652 break;
653 case '\t':
654 OS << "'\\t'";
655 break;
656 case '\v':
657 OS << "'\\v'";
658 break;
659 default:
Ted Kremenek471733d2008-02-23 00:52:04 +0000660 if (value < 256 && isprint(value)) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000661 OS << "'" << (char)value << "'";
662 } else if (value < 256) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000663 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000664 } else {
665 // FIXME what to really do here?
666 OS << value;
667 }
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000668 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000669}
670
671void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
672 bool isSigned = Node->getType()->isSignedIntegerType();
673 OS << Node->getValue().toString(10, isSigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall183700f2009-09-21 23:43:11 +0000676 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000677 default: llvm_unreachable("Unexpected type for integer literal!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000678 case BuiltinType::Int: break; // no suffix.
679 case BuiltinType::UInt: OS << 'U'; break;
680 case BuiltinType::Long: OS << 'L'; break;
681 case BuiltinType::ULong: OS << "UL"; break;
682 case BuiltinType::LongLong: OS << "LL"; break;
683 case BuiltinType::ULongLong: OS << "ULL"; break;
684 }
685}
686void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Eli Friedmanb3909212011-10-05 20:32:03 +0000687 llvm::SmallString<16> Str;
688 Node->getValue().toString(Str);
689 OS << Str;
Reid Spencer5f016e22007-07-11 17:01:13 +0000690}
Chris Lattner5d661452007-08-26 03:42:43 +0000691
692void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
693 PrintExpr(Node->getSubExpr());
694 OS << "i";
695}
696
Reid Spencer5f016e22007-07-11 17:01:13 +0000697void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Douglas Gregor5cee1192011-07-27 05:40:30 +0000698 switch (Str->getKind()) {
699 case StringLiteral::Ascii: break; // no prefix.
700 case StringLiteral::Wide: OS << 'L'; break;
701 case StringLiteral::UTF8: OS << "u8"; break;
702 case StringLiteral::UTF16: OS << 'u'; break;
703 case StringLiteral::UTF32: OS << 'U'; break;
704 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000705 OS << '"';
Anders Carlssonee98ac52007-10-15 02:50:23 +0000706
Reid Spencer5f016e22007-07-11 17:01:13 +0000707 // FIXME: this doesn't print wstrings right.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000708 StringRef StrData = Str->getString();
709 for (StringRef::iterator I = StrData.begin(), E = StrData.end();
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +0000710 I != E; ++I) {
711 unsigned char Char = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Chris Lattner9a81c872009-01-16 19:25:18 +0000713 switch (Char) {
714 default:
715 if (isprint(Char))
716 OS << (char)Char;
717 else // Output anything hard as an octal escape.
718 OS << '\\'
719 << (char)('0'+ ((Char >> 6) & 7))
720 << (char)('0'+ ((Char >> 3) & 7))
721 << (char)('0'+ ((Char >> 0) & 7));
722 break;
723 // Handle some common non-printable cases to make dumps prettier.
Reid Spencer5f016e22007-07-11 17:01:13 +0000724 case '\\': OS << "\\\\"; break;
725 case '"': OS << "\\\""; break;
726 case '\n': OS << "\\n"; break;
727 case '\t': OS << "\\t"; break;
728 case '\a': OS << "\\a"; break;
729 case '\b': OS << "\\b"; break;
730 }
731 }
732 OS << '"';
733}
734void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
735 OS << "(";
736 PrintExpr(Node->getSubExpr());
737 OS << ")";
738}
739void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +0000740 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000741 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Eli Friedman7df71ac2009-06-14 22:39:26 +0000743 // Print a space if this is an "identifier operator" like __real, or if
744 // it might be concatenated incorrectly like '+'.
Chris Lattner296bf192007-08-23 21:46:40 +0000745 switch (Node->getOpcode()) {
746 default: break;
John McCall2de56d12010-08-25 11:45:40 +0000747 case UO_Real:
748 case UO_Imag:
749 case UO_Extension:
Chris Lattner296bf192007-08-23 21:46:40 +0000750 OS << ' ';
751 break;
John McCall2de56d12010-08-25 11:45:40 +0000752 case UO_Plus:
753 case UO_Minus:
Eli Friedman7df71ac2009-06-14 22:39:26 +0000754 if (isa<UnaryOperator>(Node->getSubExpr()))
755 OS << ' ';
756 break;
Chris Lattner296bf192007-08-23 21:46:40 +0000757 }
758 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000759 PrintExpr(Node->getSubExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Reid Spencer5f016e22007-07-11 17:01:13 +0000761 if (Node->isPostfix())
762 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +0000763}
Chris Lattner704fe352007-08-30 17:59:59 +0000764
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000765void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
766 OS << "__builtin_offsetof(";
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000767 OS << Node->getTypeSourceInfo()->getType().getAsString(Policy) << ", ";
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000768 bool PrintedSomething = false;
769 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
770 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
771 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
772 // Array node
773 OS << "[";
774 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
775 OS << "]";
776 PrintedSomething = true;
777 continue;
778 }
Douglas Gregorcc8a5d52010-04-29 00:18:15 +0000779
780 // Skip implicit base indirections.
781 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
782 continue;
783
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000784 // Field or identifier node.
785 IdentifierInfo *Id = ON.getFieldName();
786 if (!Id)
787 continue;
Sean Huntc3021132010-05-05 15:23:54 +0000788
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000789 if (PrintedSomething)
790 OS << ".";
791 else
792 PrintedSomething = true;
Sean Huntc3021132010-05-05 15:23:54 +0000793 OS << Id->getName();
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000794 }
795 OS << ")";
796}
797
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000798void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
799 switch(Node->getKind()) {
800 case UETT_SizeOf:
801 OS << "sizeof";
802 break;
803 case UETT_AlignOf:
804 OS << "__alignof";
805 break;
806 case UETT_VecStep:
807 OS << "vec_step";
808 break;
809 }
Sebastian Redl05189992008-11-11 17:56:53 +0000810 if (Node->isArgumentType())
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000811 OS << "(" << Node->getArgumentType().getAsString(Policy) << ")";
Sebastian Redl05189992008-11-11 17:56:53 +0000812 else {
813 OS << " ";
814 PrintExpr(Node->getArgumentExpr());
815 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000816}
Peter Collingbournef111d932011-04-15 00:35:48 +0000817
818void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
819 OS << "_Generic(";
820 PrintExpr(Node->getControllingExpr());
821 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
822 OS << ", ";
823 QualType T = Node->getAssocType(i);
824 if (T.isNull())
825 OS << "default";
826 else
827 OS << T.getAsString(Policy);
828 OS << ": ";
829 PrintExpr(Node->getAssocExpr(i));
830 }
831 OS << ")";
832}
833
Reid Spencer5f016e22007-07-11 17:01:13 +0000834void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +0000835 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000836 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +0000837 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000838 OS << "]";
839}
840
Peter Collingbourned64e2372011-02-08 21:17:54 +0000841void StmtPrinter::PrintCallArgs(CallExpr *Call) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000842 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattner04421082008-04-08 04:40:51 +0000843 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
844 // Don't print any defaulted arguments
845 break;
846 }
847
Reid Spencer5f016e22007-07-11 17:01:13 +0000848 if (i) OS << ", ";
849 PrintExpr(Call->getArg(i));
850 }
Peter Collingbourned64e2372011-02-08 21:17:54 +0000851}
852
853void StmtPrinter::VisitCallExpr(CallExpr *Call) {
854 PrintExpr(Call->getCallee());
855 OS << "(";
856 PrintCallArgs(Call);
Reid Spencer5f016e22007-07-11 17:01:13 +0000857 OS << ")";
858}
859void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregorb3eef682009-01-08 22:45:41 +0000860 // FIXME: Suppress printing implicit bases (like "this")
861 PrintExpr(Node->getBase());
Fariborz Jahanian97fd83a2010-01-11 21:17:32 +0000862 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
863 if (FD->isAnonymousStructOrUnion())
864 return;
Douglas Gregorb3eef682009-01-08 22:45:41 +0000865 OS << (Node->isArrow() ? "->" : ".");
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000866 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
867 Qualifier->print(OS, Policy);
868
Abramo Bagnara25777432010-08-11 22:01:17 +0000869 OS << Node->getMemberNameInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000870
John McCall096832c2010-08-19 23:49:38 +0000871 if (Node->hasExplicitTemplateArgs())
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000872 OS << TemplateSpecializationType::PrintTemplateArgumentList(
873 Node->getTemplateArgs(),
874 Node->getNumTemplateArgs(),
875 Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000876}
Steve Narofff242b1b2009-07-24 17:54:45 +0000877void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
878 PrintExpr(Node->getBase());
879 OS << (Node->isArrow() ? "->isa" : ".isa");
880}
881
Nate Begeman213541a2008-04-18 23:10:10 +0000882void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +0000883 PrintExpr(Node->getBase());
884 OS << ".";
885 OS << Node->getAccessor().getName();
886}
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000887void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Fariborz Jahanian03e80e42010-06-30 16:31:08 +0000888 OS << "(" << Node->getType().getAsString(Policy) << ")";
Reid Spencer5f016e22007-07-11 17:01:13 +0000889 PrintExpr(Node->getSubExpr());
890}
Steve Naroffaff1edd2007-07-19 21:32:11 +0000891void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000892 OS << "(" << Node->getType().getAsString(Policy) << ")";
Steve Naroffaff1edd2007-07-19 21:32:11 +0000893 PrintExpr(Node->getInitializer());
894}
Steve Naroff49b45262007-07-13 16:58:59 +0000895void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroff90045e82007-07-13 23:32:42 +0000896 // No need to print anything, simply forward to the sub expression.
897 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +0000898}
Reid Spencer5f016e22007-07-11 17:01:13 +0000899void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
900 PrintExpr(Node->getLHS());
901 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
902 PrintExpr(Node->getRHS());
903}
Chris Lattnereb14fe82007-08-25 02:00:02 +0000904void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
905 PrintExpr(Node->getLHS());
906 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
907 PrintExpr(Node->getRHS());
908}
Reid Spencer5f016e22007-07-11 17:01:13 +0000909void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
910 PrintExpr(Node->getCond());
John McCall56ca35d2011-02-17 10:25:35 +0000911 OS << " ? ";
912 PrintExpr(Node->getLHS());
913 OS << " : ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000914 PrintExpr(Node->getRHS());
915}
916
917// GNU extensions.
918
John McCall56ca35d2011-02-17 10:25:35 +0000919void
920StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
921 PrintExpr(Node->getCommon());
922 OS << " ?: ";
923 PrintExpr(Node->getFalseExpr());
924}
Chris Lattner6481a572007-08-03 17:31:20 +0000925void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000926 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000927}
928
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000929void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
930 OS << "(";
931 PrintRawCompoundStmt(E->getSubStmt());
932 OS << ")";
933}
934
Steve Naroffd04fdd52007-08-03 21:21:27 +0000935void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
936 OS << "__builtin_choose_expr(";
937 PrintExpr(Node->getCond());
Chris Lattner94f05e32007-08-04 00:20:15 +0000938 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000939 PrintExpr(Node->getLHS());
Chris Lattner94f05e32007-08-04 00:20:15 +0000940 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000941 PrintExpr(Node->getRHS());
942 OS << ")";
943}
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000944
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000945void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
946 OS << "__null";
947}
948
Eli Friedmand38617c2008-05-14 19:38:39 +0000949void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
950 OS << "__builtin_shufflevector(";
951 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
952 if (i) OS << ", ";
953 PrintExpr(Node->getExpr(i));
954 }
955 OS << ")";
956}
957
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000958void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000959 if (Node->getSyntacticForm()) {
960 Visit(Node->getSyntacticForm());
961 return;
962 }
963
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000964 OS << "{ ";
965 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
966 if (i) OS << ", ";
Douglas Gregor4c678342009-01-28 21:54:33 +0000967 if (Node->getInit(i))
968 PrintExpr(Node->getInit(i));
969 else
970 OS << "0";
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000971 }
972 OS << " }";
973}
974
Nate Begeman2ef13e52009-08-10 23:49:36 +0000975void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
976 OS << "( ";
977 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
978 if (i) OS << ", ";
979 PrintExpr(Node->getExpr(i));
980 }
981 OS << " )";
982}
983
Douglas Gregor05c13a32009-01-22 00:58:24 +0000984void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000985 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
986 DEnd = Node->designators_end();
987 D != DEnd; ++D) {
988 if (D->isFieldDesignator()) {
989 if (D->getDotLoc().isInvalid())
990 OS << D->getFieldName()->getName() << ":";
991 else
992 OS << "." << D->getFieldName()->getName();
993 } else {
994 OS << "[";
995 if (D->isArrayDesignator()) {
996 PrintExpr(Node->getArrayIndex(*D));
997 } else {
998 PrintExpr(Node->getArrayRangeStart(*D));
999 OS << " ... ";
Mike Stump1eb44332009-09-09 15:08:12 +00001000 PrintExpr(Node->getArrayRangeEnd(*D));
Douglas Gregor4c678342009-01-28 21:54:33 +00001001 }
1002 OS << "]";
1003 }
1004 }
1005
1006 OS << " = ";
1007 PrintExpr(Node->getInit());
Douglas Gregor05c13a32009-01-22 00:58:24 +00001008}
1009
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001010void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Chris Lattnere4f21422009-06-30 01:26:17 +00001011 if (Policy.LangOpts.CPlusPlus)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001012 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()";
1013 else {
1014 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")";
1015 if (Node->getType()->isRecordType())
1016 OS << "{}";
1017 else
1018 OS << 0;
1019 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001020}
1021
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001022void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman42f42c02009-05-30 04:20:30 +00001023 OS << "__builtin_va_arg(";
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001024 PrintExpr(Node->getSubExpr());
1025 OS << ", ";
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001026 OS << Node->getType().getAsString(Policy);
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001027 OS << ")";
1028}
1029
Eli Friedman276b0612011-10-11 02:20:01 +00001030void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
Eli Friedman9e3c20b2011-10-11 20:00:47 +00001031 const char *Name = 0;
Eli Friedman276b0612011-10-11 02:20:01 +00001032 switch (Node->getOp()) {
1033 case AtomicExpr::Load:
1034 Name = "__atomic_load(";
1035 break;
1036 case AtomicExpr::Store:
1037 Name = "__atomic_store(";
1038 break;
1039 case AtomicExpr::CmpXchgStrong:
1040 Name = "__atomic_compare_exchange_strong(";
1041 break;
1042 case AtomicExpr::CmpXchgWeak:
1043 Name = "__atomic_compare_exchange_weak(";
1044 break;
1045 case AtomicExpr::Xchg:
1046 Name = "__atomic_exchange(";
1047 break;
1048 case AtomicExpr::Add:
1049 Name = "__atomic_fetch_add(";
1050 break;
1051 case AtomicExpr::Sub:
1052 Name = "__atomic_fetch_sub(";
1053 break;
1054 case AtomicExpr::And:
1055 Name = "__atomic_fetch_and(";
1056 break;
1057 case AtomicExpr::Or:
1058 Name = "__atomic_fetch_or(";
1059 break;
1060 case AtomicExpr::Xor:
1061 Name = "__atomic_fetch_xor(";
1062 break;
1063 }
1064 OS << Name;
1065 PrintExpr(Node->getPtr());
1066 OS << ", ";
1067 if (Node->getOp() != AtomicExpr::Load) {
1068 PrintExpr(Node->getVal1());
1069 OS << ", ";
1070 }
1071 if (Node->isCmpXChg()) {
1072 PrintExpr(Node->getVal2());
1073 OS << ", ";
1074 }
1075 PrintExpr(Node->getOrder());
1076 if (Node->isCmpXChg()) {
1077 OS << ", ";
1078 PrintExpr(Node->getOrderFail());
1079 }
1080 OS << ")";
1081}
1082
Reid Spencer5f016e22007-07-11 17:01:13 +00001083// C++
Douglas Gregorb4609802008-11-14 16:09:21 +00001084void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1085 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1086 "",
1087#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1088 Spelling,
1089#include "clang/Basic/OperatorKinds.def"
1090 };
1091
1092 OverloadedOperatorKind Kind = Node->getOperator();
1093 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1094 if (Node->getNumArgs() == 1) {
1095 OS << OpStrings[Kind] << ' ';
1096 PrintExpr(Node->getArg(0));
1097 } else {
1098 PrintExpr(Node->getArg(0));
1099 OS << ' ' << OpStrings[Kind];
1100 }
1101 } else if (Kind == OO_Call) {
1102 PrintExpr(Node->getArg(0));
1103 OS << '(';
1104 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1105 if (ArgIdx > 1)
1106 OS << ", ";
1107 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1108 PrintExpr(Node->getArg(ArgIdx));
1109 }
1110 OS << ')';
1111 } else if (Kind == OO_Subscript) {
1112 PrintExpr(Node->getArg(0));
1113 OS << '[';
1114 PrintExpr(Node->getArg(1));
1115 OS << ']';
1116 } else if (Node->getNumArgs() == 1) {
1117 OS << OpStrings[Kind] << ' ';
1118 PrintExpr(Node->getArg(0));
1119 } else if (Node->getNumArgs() == 2) {
1120 PrintExpr(Node->getArg(0));
1121 OS << ' ' << OpStrings[Kind] << ' ';
1122 PrintExpr(Node->getArg(1));
1123 } else {
David Blaikieb219cfc2011-09-23 05:06:16 +00001124 llvm_unreachable("unknown overloaded operator");
Douglas Gregorb4609802008-11-14 16:09:21 +00001125 }
1126}
Reid Spencer5f016e22007-07-11 17:01:13 +00001127
Douglas Gregor88a35142008-12-22 05:46:06 +00001128void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1129 VisitCallExpr(cast<CallExpr>(Node));
1130}
1131
Peter Collingbournee08ce652011-02-09 21:07:24 +00001132void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1133 PrintExpr(Node->getCallee());
1134 OS << "<<<";
1135 PrintCallArgs(Node->getConfig());
1136 OS << ">>>(";
1137 PrintCallArgs(Node);
1138 OS << ")";
1139}
1140
Douglas Gregor49badde2008-10-27 19:41:14 +00001141void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1142 OS << Node->getCastName() << '<';
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001143 OS << Node->getTypeAsWritten().getAsString(Policy) << ">(";
Reid Spencer5f016e22007-07-11 17:01:13 +00001144 PrintExpr(Node->getSubExpr());
1145 OS << ")";
1146}
1147
Douglas Gregor49badde2008-10-27 19:41:14 +00001148void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1149 VisitCXXNamedCastExpr(Node);
1150}
1151
1152void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1153 VisitCXXNamedCastExpr(Node);
1154}
1155
1156void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1157 VisitCXXNamedCastExpr(Node);
1158}
1159
1160void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1161 VisitCXXNamedCastExpr(Node);
1162}
1163
Sebastian Redlc42e1182008-11-11 11:37:55 +00001164void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1165 OS << "typeid(";
1166 if (Node->isTypeOperand()) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001167 OS << Node->getTypeOperand().getAsString(Policy);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001168 } else {
1169 PrintExpr(Node->getExprOperand());
1170 }
1171 OS << ")";
1172}
1173
Francois Pichet01b7c302010-09-08 12:20:18 +00001174void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1175 OS << "__uuidof(";
1176 if (Node->isTypeOperand()) {
1177 OS << Node->getTypeOperand().getAsString(Policy);
1178 } else {
1179 PrintExpr(Node->getExprOperand());
1180 }
1181 OS << ")";
1182}
1183
Reid Spencer5f016e22007-07-11 17:01:13 +00001184void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1185 OS << (Node->getValue() ? "true" : "false");
1186}
1187
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001188void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1189 OS << "nullptr";
1190}
1191
Douglas Gregor796da182008-11-04 14:32:21 +00001192void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1193 OS << "this";
1194}
1195
Chris Lattner50dd2892008-02-26 00:51:44 +00001196void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1197 if (Node->getSubExpr() == 0)
1198 OS << "throw";
1199 else {
1200 OS << "throw ";
1201 PrintExpr(Node->getSubExpr());
1202 }
1203}
1204
Chris Lattner04421082008-04-08 04:40:51 +00001205void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1206 // Nothing to print: we picked up the default argument
1207}
1208
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001209void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001210 OS << Node->getType().getAsString(Policy);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001211 OS << "(";
1212 PrintExpr(Node->getSubExpr());
1213 OS << ")";
1214}
1215
Anders Carlssonfceb0a82009-05-30 20:03:25 +00001216void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1217 PrintExpr(Node->getSubExpr());
1218}
1219
Douglas Gregor506ae412009-01-16 18:33:17 +00001220void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001221 OS << Node->getType().getAsString(Policy);
Douglas Gregor506ae412009-01-16 18:33:17 +00001222 OS << "(";
1223 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001224 ArgEnd = Node->arg_end();
Douglas Gregor506ae412009-01-16 18:33:17 +00001225 Arg != ArgEnd; ++Arg) {
1226 if (Arg != Node->arg_begin())
1227 OS << ", ";
1228 PrintExpr(*Arg);
1229 }
1230 OS << ")";
1231}
1232
Douglas Gregored8abf12010-07-08 06:14:04 +00001233void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregorab6677e2010-09-08 00:15:04 +00001234 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
1235 OS << TSInfo->getType().getAsString(Policy) << "()";
1236 else
1237 OS << Node->getType().getAsString(Policy) << "()";
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001238}
1239
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001240void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1241 if (E->isGlobalNew())
1242 OS << "::";
1243 OS << "new ";
1244 unsigned NumPlace = E->getNumPlacementArgs();
1245 if (NumPlace > 0) {
1246 OS << "(";
1247 PrintExpr(E->getPlacementArg(0));
1248 for (unsigned i = 1; i < NumPlace; ++i) {
1249 OS << ", ";
1250 PrintExpr(E->getPlacementArg(i));
1251 }
1252 OS << ") ";
1253 }
1254 if (E->isParenTypeId())
1255 OS << "(";
Sebastian Redl6fec6482008-12-02 22:08:59 +00001256 std::string TypeS;
1257 if (Expr *Size = E->getArraySize()) {
1258 llvm::raw_string_ostream s(TypeS);
Eli Friedman6e1a3452009-05-30 05:32:46 +00001259 Size->printPretty(s, Context, Helper, Policy);
Sebastian Redl6fec6482008-12-02 22:08:59 +00001260 s.flush();
1261 TypeS = "[" + TypeS + "]";
1262 }
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001263 E->getAllocatedType().getAsStringInternal(TypeS, Policy);
Sebastian Redl6fec6482008-12-02 22:08:59 +00001264 OS << TypeS;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001265 if (E->isParenTypeId())
1266 OS << ")";
1267
1268 if (E->hasInitializer()) {
1269 OS << "(";
1270 unsigned NumCons = E->getNumConstructorArgs();
1271 if (NumCons > 0) {
1272 PrintExpr(E->getConstructorArg(0));
1273 for (unsigned i = 1; i < NumCons; ++i) {
1274 OS << ", ";
1275 PrintExpr(E->getConstructorArg(i));
1276 }
1277 }
1278 OS << ")";
1279 }
1280}
1281
1282void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1283 if (E->isGlobalDelete())
1284 OS << "::";
1285 OS << "delete ";
1286 if (E->isArrayForm())
1287 OS << "[] ";
1288 PrintExpr(E->getArgument());
1289}
1290
Douglas Gregora71d8192009-09-04 17:36:40 +00001291void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1292 PrintExpr(E->getBase());
1293 if (E->isArrow())
1294 OS << "->";
1295 else
1296 OS << '.';
1297 if (E->getQualifier())
1298 E->getQualifier()->print(OS, Policy);
Mike Stump1eb44332009-09-09 15:08:12 +00001299
Douglas Gregora71d8192009-09-04 17:36:40 +00001300 std::string TypeS;
Douglas Gregora2e7dd22010-02-25 01:56:36 +00001301 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1302 OS << II->getName();
1303 else
1304 E->getDestroyedType().getAsStringInternal(TypeS, Policy);
Douglas Gregora71d8192009-09-04 17:36:40 +00001305 OS << TypeS;
1306}
1307
Anders Carlssone349bea2009-04-23 02:32:43 +00001308void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Douglas Gregord0fb3ad2011-01-24 17:25:03 +00001309 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1310 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1311 // Don't print any defaulted arguments
1312 break;
1313 }
1314
1315 if (i) OS << ", ";
1316 PrintExpr(E->getArg(i));
Fariborz Jahanianc75da512010-01-13 21:41:11 +00001317 }
Anders Carlssone349bea2009-04-23 02:32:43 +00001318}
1319
John McCall4765fa02010-12-06 08:20:24 +00001320void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Anders Carlsson02bbfa32009-04-24 22:47:04 +00001321 // Just forward to the sub expression.
1322 PrintExpr(E->getSubExpr());
1323}
1324
Mike Stump1eb44332009-09-09 15:08:12 +00001325void
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001326StmtPrinter::VisitCXXUnresolvedConstructExpr(
1327 CXXUnresolvedConstructExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001328 OS << Node->getTypeAsWritten().getAsString(Policy);
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001329 OS << "(";
1330 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001331 ArgEnd = Node->arg_end();
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001332 Arg != ArgEnd; ++Arg) {
1333 if (Arg != Node->arg_begin())
1334 OS << ", ";
1335 PrintExpr(*Arg);
1336 }
1337 OS << ")";
1338}
1339
John McCall865d4472009-11-19 22:55:06 +00001340void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1341 CXXDependentScopeMemberExpr *Node) {
John McCallaa81e162009-12-01 22:10:20 +00001342 if (!Node->isImplicitAccess()) {
1343 PrintExpr(Node->getBase());
1344 OS << (Node->isArrow() ? "->" : ".");
1345 }
Douglas Gregora38c6872009-09-03 16:14:30 +00001346 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1347 Qualifier->print(OS, Policy);
John McCallaa81e162009-12-01 22:10:20 +00001348 else if (Node->hasExplicitTemplateArgs())
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001349 // FIXME: Track use of "template" keyword explicitly?
1350 OS << "template ";
Mike Stump1eb44332009-09-09 15:08:12 +00001351
Abramo Bagnara25777432010-08-11 22:01:17 +00001352 OS << Node->getMemberNameInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00001353
John McCallaa81e162009-12-01 22:10:20 +00001354 if (Node->hasExplicitTemplateArgs()) {
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001355 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1356 Node->getTemplateArgs(),
1357 Node->getNumTemplateArgs(),
1358 Policy);
1359 }
Douglas Gregor1c0ca592009-05-22 21:13:27 +00001360}
1361
John McCall129e2df2009-11-30 22:42:35 +00001362void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCallaa81e162009-12-01 22:10:20 +00001363 if (!Node->isImplicitAccess()) {
1364 PrintExpr(Node->getBase());
1365 OS << (Node->isArrow() ? "->" : ".");
1366 }
John McCall129e2df2009-11-30 22:42:35 +00001367 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1368 Qualifier->print(OS, Policy);
1369
1370 // FIXME: this might originally have been written with 'template'
1371
Abramo Bagnara25777432010-08-11 22:01:17 +00001372 OS << Node->getMemberNameInfo();
John McCall129e2df2009-11-30 22:42:35 +00001373
1374 if (Node->hasExplicitTemplateArgs()) {
1375 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1376 Node->getTemplateArgs(),
1377 Node->getNumTemplateArgs(),
1378 Policy);
1379 }
1380}
1381
Sebastian Redl64b45f72009-01-05 20:52:13 +00001382static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1383 switch (UTT) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001384 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001385 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
John Wiegley20c0da72011-04-27 23:09:49 +00001386 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001387 case UTT_HasTrivialAssign: return "__has_trivial_assign";
Sean Hunt023df372011-05-09 18:22:59 +00001388 case UTT_HasTrivialDefaultConstructor: return "__has_trivial_constructor";
John Wiegley20c0da72011-04-27 23:09:49 +00001389 case UTT_HasTrivialCopy: return "__has_trivial_copy";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001390 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1391 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1392 case UTT_IsAbstract: return "__is_abstract";
John Wiegley20c0da72011-04-27 23:09:49 +00001393 case UTT_IsArithmetic: return "__is_arithmetic";
1394 case UTT_IsArray: return "__is_array";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001395 case UTT_IsClass: return "__is_class";
John Wiegley20c0da72011-04-27 23:09:49 +00001396 case UTT_IsCompleteType: return "__is_complete_type";
1397 case UTT_IsCompound: return "__is_compound";
1398 case UTT_IsConst: return "__is_const";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001399 case UTT_IsEmpty: return "__is_empty";
1400 case UTT_IsEnum: return "__is_enum";
John Wiegley20c0da72011-04-27 23:09:49 +00001401 case UTT_IsFloatingPoint: return "__is_floating_point";
1402 case UTT_IsFunction: return "__is_function";
1403 case UTT_IsFundamental: return "__is_fundamental";
1404 case UTT_IsIntegral: return "__is_integral";
Chandler Carruthe1947102011-05-01 07:23:20 +00001405 case UTT_IsLiteral: return "__is_literal";
John Wiegley20c0da72011-04-27 23:09:49 +00001406 case UTT_IsLvalueReference: return "__is_lvalue_reference";
1407 case UTT_IsMemberFunctionPointer: return "__is_member_function_pointer";
1408 case UTT_IsMemberObjectPointer: return "__is_member_object_pointer";
1409 case UTT_IsMemberPointer: return "__is_member_pointer";
1410 case UTT_IsObject: return "__is_object";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001411 case UTT_IsPOD: return "__is_pod";
John Wiegley20c0da72011-04-27 23:09:49 +00001412 case UTT_IsPointer: return "__is_pointer";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001413 case UTT_IsPolymorphic: return "__is_polymorphic";
John Wiegley20c0da72011-04-27 23:09:49 +00001414 case UTT_IsReference: return "__is_reference";
John Wiegley20c0da72011-04-27 23:09:49 +00001415 case UTT_IsRvalueReference: return "__is_rvalue_reference";
1416 case UTT_IsScalar: return "__is_scalar";
1417 case UTT_IsSigned: return "__is_signed";
1418 case UTT_IsStandardLayout: return "__is_standard_layout";
1419 case UTT_IsTrivial: return "__is_trivial";
Sean Huntfeb375d2011-05-13 00:31:07 +00001420 case UTT_IsTriviallyCopyable: return "__is_trivially_copyable";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001421 case UTT_IsUnion: return "__is_union";
John Wiegley20c0da72011-04-27 23:09:49 +00001422 case UTT_IsUnsigned: return "__is_unsigned";
1423 case UTT_IsVoid: return "__is_void";
1424 case UTT_IsVolatile: return "__is_volatile";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001425 }
Chandler Carruthe1947102011-05-01 07:23:20 +00001426 llvm_unreachable("Type trait not covered by switch statement");
Francois Pichet6ad6f282010-12-07 00:08:36 +00001427}
1428
1429static const char *getTypeTraitName(BinaryTypeTrait BTT) {
1430 switch (BTT) {
Francois Pichetf1872372010-12-08 22:35:30 +00001431 case BTT_IsBaseOf: return "__is_base_of";
John Wiegley20c0da72011-04-27 23:09:49 +00001432 case BTT_IsConvertible: return "__is_convertible";
1433 case BTT_IsSame: return "__is_same";
Francois Pichetf1872372010-12-08 22:35:30 +00001434 case BTT_TypeCompatible: return "__builtin_types_compatible_p";
Douglas Gregor9f361132011-01-27 20:28:01 +00001435 case BTT_IsConvertibleTo: return "__is_convertible_to";
Francois Pichet6ad6f282010-12-07 00:08:36 +00001436 }
Chandler Carrutha280bc82011-05-01 07:23:23 +00001437 llvm_unreachable("Binary type trait not covered by switch");
Sebastian Redl64b45f72009-01-05 20:52:13 +00001438}
1439
John Wiegley21ff2e52011-04-28 00:16:57 +00001440static const char *getTypeTraitName(ArrayTypeTrait ATT) {
1441 switch (ATT) {
1442 case ATT_ArrayRank: return "__array_rank";
1443 case ATT_ArrayExtent: return "__array_extent";
1444 }
Chandler Carrutha280bc82011-05-01 07:23:23 +00001445 llvm_unreachable("Array type trait not covered by switch");
John Wiegley21ff2e52011-04-28 00:16:57 +00001446}
1447
John Wiegley55262202011-04-25 06:54:41 +00001448static const char *getExpressionTraitName(ExpressionTrait ET) {
1449 switch (ET) {
John Wiegley55262202011-04-25 06:54:41 +00001450 case ET_IsLValueExpr: return "__is_lvalue_expr";
1451 case ET_IsRValueExpr: return "__is_rvalue_expr";
1452 }
Chandler Carrutha280bc82011-05-01 07:23:23 +00001453 llvm_unreachable("Expression type trait not covered by switch");
John Wiegley55262202011-04-25 06:54:41 +00001454}
1455
Sebastian Redl64b45f72009-01-05 20:52:13 +00001456void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1457 OS << getTypeTraitName(E->getTrait()) << "("
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001458 << E->getQueriedType().getAsString(Policy) << ")";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001459}
1460
Francois Pichet6ad6f282010-12-07 00:08:36 +00001461void StmtPrinter::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
1462 OS << getTypeTraitName(E->getTrait()) << "("
1463 << E->getLhsType().getAsString(Policy) << ","
1464 << E->getRhsType().getAsString(Policy) << ")";
1465}
1466
John Wiegley21ff2e52011-04-28 00:16:57 +00001467void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
1468 OS << getTypeTraitName(E->getTrait()) << "("
1469 << E->getQueriedType().getAsString(Policy) << ")";
1470}
1471
John Wiegley55262202011-04-25 06:54:41 +00001472void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
1473 OS << getExpressionTraitName(E->getTrait()) << "(";
1474 PrintExpr(E->getQueriedExpression());
1475 OS << ")";
1476}
1477
Sebastian Redl2e156222010-09-10 20:55:43 +00001478void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1479 OS << "noexcept(";
1480 PrintExpr(E->getOperand());
1481 OS << ")";
1482}
1483
Douglas Gregoree8aff02011-01-04 17:33:58 +00001484void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregorbe230c32011-01-03 17:17:50 +00001485 PrintExpr(E->getPattern());
1486 OS << "...";
1487}
1488
Douglas Gregoree8aff02011-01-04 17:33:58 +00001489void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
1490 OS << "sizeof...(" << E->getPack()->getNameAsString() << ")";
1491}
1492
Douglas Gregorc7793c72011-01-15 01:15:58 +00001493void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
1494 SubstNonTypeTemplateParmPackExpr *Node) {
1495 OS << Node->getParameterPack()->getNameAsString();
1496}
1497
John McCall91a57552011-07-15 05:09:51 +00001498void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
1499 SubstNonTypeTemplateParmExpr *Node) {
1500 Visit(Node->getReplacement());
1501}
1502
Douglas Gregor03e80032011-06-21 17:03:29 +00001503void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
1504 PrintExpr(Node->GetTemporaryExpr());
1505}
1506
Mike Stump1eb44332009-09-09 15:08:12 +00001507// Obj-C
Anders Carlsson55085182007-08-21 17:43:55 +00001508
1509void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1510 OS << "@";
1511 VisitStringLiteral(Node->getString());
1512}
Reid Spencer5f016e22007-07-11 17:01:13 +00001513
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001514void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001515 OS << "@encode(" << Node->getEncodedType().getAsString(Policy) << ')';
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001516}
1517
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001518void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001519 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001520}
1521
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001522void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001523 OS << "@protocol(" << *Node->getProtocol() << ')';
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001524}
1525
Steve Naroff563477d2007-09-18 23:55:05 +00001526void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1527 OS << "[";
Douglas Gregor04badcf2010-04-21 00:45:42 +00001528 switch (Mess->getReceiverKind()) {
1529 case ObjCMessageExpr::Instance:
1530 PrintExpr(Mess->getInstanceReceiver());
1531 break;
1532
1533 case ObjCMessageExpr::Class:
1534 OS << Mess->getClassReceiver().getAsString(Policy);
1535 break;
1536
1537 case ObjCMessageExpr::SuperInstance:
1538 case ObjCMessageExpr::SuperClass:
1539 OS << "Super";
1540 break;
1541 }
1542
Ted Kremenekc29efd82008-05-02 17:32:38 +00001543 OS << ' ';
Ted Kremenek97b7f262008-04-16 04:30:16 +00001544 Selector selector = Mess->getSelector();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001545 if (selector.isUnarySelector()) {
Douglas Gregor813d8342011-02-18 22:29:55 +00001546 OS << selector.getNameForSlot(0);
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001547 } else {
1548 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001549 if (i < selector.getNumArgs()) {
1550 if (i > 0) OS << ' ';
1551 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner39f34e92008-11-24 04:00:27 +00001552 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremenekc29efd82008-05-02 17:32:38 +00001553 else
1554 OS << ":";
1555 }
1556 else OS << ", "; // Handle variadic methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001557
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001558 PrintExpr(Mess->getArg(i));
1559 }
Steve Naroff563477d2007-09-18 23:55:05 +00001560 }
1561 OS << "]";
1562}
1563
John McCallf85e1932011-06-15 23:02:42 +00001564void
1565StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
1566 PrintExpr(E->getSubExpr());
1567}
1568
1569void
1570StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
1571 OS << "(" << E->getBridgeKindName() << E->getType().getAsString(Policy)
1572 << ")";
1573 PrintExpr(E->getSubExpr());
1574}
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001575
Steve Naroff4eb206b2008-09-03 18:15:37 +00001576void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001577 BlockDecl *BD = Node->getBlockDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001578 OS << "^";
Mike Stump1eb44332009-09-09 15:08:12 +00001579
Steve Naroff4eb206b2008-09-03 18:15:37 +00001580 const FunctionType *AFT = Node->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +00001581
Douglas Gregor72564e72009-02-26 23:50:07 +00001582 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001583 OS << "()";
Douglas Gregor72564e72009-02-26 23:50:07 +00001584 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001585 OS << '(';
1586 std::string ParamStr;
Steve Naroff56ee6892008-10-08 17:01:13 +00001587 for (BlockDecl::param_iterator AI = BD->param_begin(),
1588 E = BD->param_end(); AI != E; ++AI) {
1589 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner39f34e92008-11-24 04:00:27 +00001590 ParamStr = (*AI)->getNameAsString();
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001591 (*AI)->getType().getAsStringInternal(ParamStr, Policy);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001592 OS << ParamStr;
1593 }
Mike Stump1eb44332009-09-09 15:08:12 +00001594
Douglas Gregor72564e72009-02-26 23:50:07 +00001595 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001596 if (FT->isVariadic()) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001597 if (!BD->param_empty()) OS << ", ";
Steve Naroff4eb206b2008-09-03 18:15:37 +00001598 OS << "...";
1599 }
1600 OS << ')';
1601 }
1602}
1603
Steve Naroff4eb206b2008-09-03 18:15:37 +00001604void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001605 OS << *Node->getDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001606}
John McCall7cd7d1a2010-11-15 23:31:06 +00001607
1608void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {}
1609
Tanya Lattner61eee0c2011-06-04 00:47:47 +00001610void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
1611 OS << "__builtin_astype(";
1612 PrintExpr(Node->getSrcExpr());
1613 OS << ", " << Node->getType().getAsString();
1614 OS << ")";
1615}
1616
Reid Spencer5f016e22007-07-11 17:01:13 +00001617//===----------------------------------------------------------------------===//
1618// Stmt method implementations
1619//===----------------------------------------------------------------------===//
1620
Eli Friedman48d14a22009-05-30 05:03:24 +00001621void Stmt::dumpPretty(ASTContext& Context) const {
Argyrios Kyrtzidis96680332010-08-09 10:54:31 +00001622 printPretty(llvm::errs(), Context, 0,
Chris Lattnere4f21422009-06-30 01:26:17 +00001623 PrintingPolicy(Context.getLangOptions()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001624}
1625
Chris Lattner5f9e2722011-07-23 10:55:15 +00001626void Stmt::printPretty(raw_ostream &OS, ASTContext& Context,
Eli Friedman48d14a22009-05-30 05:03:24 +00001627 PrinterHelper* Helper,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001628 const PrintingPolicy &Policy,
1629 unsigned Indentation) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001630 if (this == 0) {
1631 OS << "<NULL>";
1632 return;
1633 }
1634
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001635 if (Policy.Dump && &Context) {
Argyrios Kyrtzidis96680332010-08-09 10:54:31 +00001636 dump(OS, Context.getSourceManager());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001637 return;
1638 }
Mike Stump1eb44332009-09-09 15:08:12 +00001639
Eli Friedman48d14a22009-05-30 05:03:24 +00001640 StmtPrinter P(OS, Context, Helper, Policy, Indentation);
Chris Lattnerc5598cb2007-08-21 04:04:25 +00001641 P.Visit(const_cast<Stmt*>(this));
Reid Spencer5f016e22007-07-11 17:01:13 +00001642}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001643
1644//===----------------------------------------------------------------------===//
1645// PrinterHelper
1646//===----------------------------------------------------------------------===//
1647
1648// Implement virtual destructor.
Gabor Greif84675832007-09-11 15:32:40 +00001649PrinterHelper::~PrinterHelper() {}