blob: 78d9a596fbcaa31633572b1fa8111d4329cac921 [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
Reid Spencer5f016e22007-07-11 17:01:13 +0000308void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
309 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
310}
311
312void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
313 Indent() << "goto *";
314 PrintExpr(Node->getTarget());
315 OS << ";\n";
316}
317
318void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
319 Indent() << "continue;\n";
320}
321
322void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
323 Indent() << "break;\n";
324}
325
326
327void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
328 Indent() << "return";
329 if (Node->getRetValue()) {
330 OS << " ";
331 PrintExpr(Node->getRetValue());
332 }
333 OS << ";\n";
334}
335
Chris Lattnerfe795952007-10-29 04:04:16 +0000336
337void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson39c47b52007-11-23 23:12:25 +0000338 Indent() << "asm ";
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Anders Carlsson39c47b52007-11-23 23:12:25 +0000340 if (Node->isVolatile())
341 OS << "volatile ";
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Anders Carlsson39c47b52007-11-23 23:12:25 +0000343 OS << "(";
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000344 VisitStringLiteral(Node->getAsmString());
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Anders Carlssonb235fc22007-11-22 01:36:19 +0000346 // Outputs
347 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
348 Node->getNumClobbers() != 0)
349 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Anders Carlssonb235fc22007-11-22 01:36:19 +0000351 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
352 if (i != 0)
353 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Anders Carlssonb235fc22007-11-22 01:36:19 +0000355 if (!Node->getOutputName(i).empty()) {
356 OS << '[';
357 OS << Node->getOutputName(i);
358 OS << "] ";
359 }
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Chris Lattnerb3277932009-03-10 04:59:06 +0000361 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000362 OS << " ";
363 Visit(Node->getOutputExpr(i));
364 }
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Anders Carlssonb235fc22007-11-22 01:36:19 +0000366 // Inputs
367 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
368 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Anders Carlssonb235fc22007-11-22 01:36:19 +0000370 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
371 if (i != 0)
372 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Anders Carlssonb235fc22007-11-22 01:36:19 +0000374 if (!Node->getInputName(i).empty()) {
375 OS << '[';
376 OS << Node->getInputName(i);
377 OS << "] ";
378 }
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Chris Lattnerb3277932009-03-10 04:59:06 +0000380 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000381 OS << " ";
382 Visit(Node->getInputExpr(i));
383 }
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Anders Carlssonb235fc22007-11-22 01:36:19 +0000385 // Clobbers
386 if (Node->getNumClobbers() != 0)
387 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Anders Carlssonb235fc22007-11-22 01:36:19 +0000389 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
390 if (i != 0)
391 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Anders Carlssonb235fc22007-11-22 01:36:19 +0000393 VisitStringLiteral(Node->getClobber(i));
394 }
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000396 OS << ");\n";
Chris Lattnerfe795952007-10-29 04:04:16 +0000397}
398
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000399void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000400 Indent() << "@try";
401 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
402 PrintRawCompoundStmt(TS);
403 OS << "\n";
404 }
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000406 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
407 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000408 Indent() << "@catch(";
Steve Naroff7ba138a2009-03-03 19:52:17 +0000409 if (catchStmt->getCatchParamDecl()) {
410 if (Decl *DS = catchStmt->getCatchParamDecl())
411 PrintRawDecl(DS);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000412 }
413 OS << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000414 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
415 PrintRawCompoundStmt(CS);
416 OS << "\n";
417 }
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000418 }
Mike Stump1eb44332009-09-09 15:08:12 +0000419
420 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
421 Node->getFinallyStmt())) {
Fariborz Jahanian1e7eab42007-11-07 00:46:42 +0000422 Indent() << "@finally";
423 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000424 OS << "\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000425 }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000426}
427
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000428void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000429}
430
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000431void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000432 Indent() << "@catch (...) { /* todo */ } \n";
433}
434
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000435void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000436 Indent() << "@throw";
437 if (Node->getThrowExpr()) {
438 OS << " ";
439 PrintExpr(Node->getThrowExpr());
440 }
441 OS << ";\n";
442}
443
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000444void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000445 Indent() << "@synchronized (";
446 PrintExpr(Node->getSynchExpr());
447 OS << ")";
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000448 PrintRawCompoundStmt(Node->getSynchBody());
449 OS << "\n";
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000450}
451
John McCallf85e1932011-06-15 23:02:42 +0000452void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
453 Indent() << "@autoreleasepool";
454 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
455 OS << "\n";
456}
457
Sebastian Redl8351da02008-12-22 21:35:02 +0000458void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
459 OS << "catch (";
Sebastian Redl4b07b292008-12-22 19:15:10 +0000460 if (Decl *ExDecl = Node->getExceptionDecl())
461 PrintRawDecl(ExDecl);
462 else
463 OS << "...";
464 OS << ") ";
465 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl8351da02008-12-22 21:35:02 +0000466}
467
468void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
469 Indent();
470 PrintRawCXXCatchStmt(Node);
471 OS << "\n";
472}
473
474void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
475 Indent() << "try ";
476 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump1eb44332009-09-09 15:08:12 +0000477 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl8351da02008-12-22 21:35:02 +0000478 OS << " ";
479 PrintRawCXXCatchStmt(Node->getHandler(i));
480 }
Sebastian Redl4b07b292008-12-22 19:15:10 +0000481 OS << "\n";
482}
483
John Wiegley28bbe4b2011-04-28 01:08:34 +0000484void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
485 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
486 PrintRawCompoundStmt(Node->getTryBlock());
487 SEHExceptStmt *E = Node->getExceptHandler();
488 SEHFinallyStmt *F = Node->getFinallyHandler();
489 if(E)
490 PrintRawSEHExceptHandler(E);
491 else {
492 assert(F && "Must have a finally block...");
493 PrintRawSEHFinallyStmt(F);
494 }
495 OS << "\n";
496}
497
498void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
499 OS << "__finally ";
500 PrintRawCompoundStmt(Node->getBlock());
501 OS << "\n";
502}
503
504void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
505 OS << "__except (";
506 VisitExpr(Node->getFilterExpr());
507 OS << ")\n";
508 PrintRawCompoundStmt(Node->getBlock());
509 OS << "\n";
510}
511
512void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
513 Indent();
514 PrintRawSEHExceptHandler(Node);
515 OS << "\n";
516}
517
518void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
519 Indent();
520 PrintRawSEHFinallyStmt(Node);
521 OS << "\n";
522}
523
Reid Spencer5f016e22007-07-11 17:01:13 +0000524//===----------------------------------------------------------------------===//
525// Expr printing methods.
526//===----------------------------------------------------------------------===//
527
Reid Spencer5f016e22007-07-11 17:01:13 +0000528void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000529 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
530 Qualifier->print(OS, Policy);
Abramo Bagnara25777432010-08-11 22:01:17 +0000531 OS << Node->getNameInfo();
John McCall096832c2010-08-19 23:49:38 +0000532 if (Node->hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000533 OS << TemplateSpecializationType::PrintTemplateArgumentList(
534 Node->getTemplateArgs(),
535 Node->getNumTemplateArgs(),
Sean Huntc3021132010-05-05 15:23:54 +0000536 Policy);
Douglas Gregor1a49af92009-01-06 05:10:23 +0000537}
538
John McCall865d4472009-11-19 22:55:06 +0000539void StmtPrinter::VisitDependentScopeDeclRefExpr(
540 DependentScopeDeclRefExpr *Node) {
Axel Naumann274f83c2011-01-24 15:44:00 +0000541 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
542 Qualifier->print(OS, Policy);
Abramo Bagnara25777432010-08-11 22:01:17 +0000543 OS << Node->getNameInfo();
John McCallf7a1a742009-11-24 19:00:30 +0000544 if (Node->hasExplicitTemplateArgs())
545 OS << TemplateSpecializationType::PrintTemplateArgumentList(
546 Node->getTemplateArgs(),
547 Node->getNumTemplateArgs(),
548 Policy);
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000549}
550
John McCallba135432009-11-21 08:51:07 +0000551void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000552 if (Node->getQualifier())
553 Node->getQualifier()->print(OS, Policy);
Abramo Bagnara25777432010-08-11 22:01:17 +0000554 OS << Node->getNameInfo();
John McCallf7a1a742009-11-24 19:00:30 +0000555 if (Node->hasExplicitTemplateArgs())
556 OS << TemplateSpecializationType::PrintTemplateArgumentList(
557 Node->getTemplateArgs(),
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000558 Node->getNumTemplateArgs(),
John McCallf7a1a742009-11-24 19:00:30 +0000559 Policy);
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000560}
561
Steve Naroff7779db42007-11-12 14:29:37 +0000562void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000563 if (Node->getBase()) {
564 PrintExpr(Node->getBase());
565 OS << (Node->isArrow() ? "->" : ".");
566 }
Benjamin Kramer900fc632010-04-17 09:33:03 +0000567 OS << Node->getDecl();
Steve Naroff7779db42007-11-12 14:29:37 +0000568}
569
Steve Naroffae784072008-05-30 00:40:33 +0000570void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000571 if (Node->isSuperReceiver())
572 OS << "super.";
573 else if (Node->getBase()) {
Steve Naroffae784072008-05-30 00:40:33 +0000574 PrintExpr(Node->getBase());
575 OS << ".";
576 }
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000577
John McCall12f78a62010-12-02 01:19:52 +0000578 if (Node->isImplicitProperty())
579 OS << Node->getImplicitPropertyGetter()->getSelector().getAsString();
580 else
581 OS << Node->getExplicitProperty()->getName();
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000582}
583
Chris Lattnerd9f69102008-08-10 01:53:14 +0000584void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson22742662007-07-21 05:21:51 +0000585 switch (Node->getIdentType()) {
586 default:
David Blaikieb219cfc2011-09-23 05:06:16 +0000587 llvm_unreachable("unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000588 case PredefinedExpr::Func:
Anders Carlsson22742662007-07-21 05:21:51 +0000589 OS << "__func__";
590 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000591 case PredefinedExpr::Function:
Anders Carlsson22742662007-07-21 05:21:51 +0000592 OS << "__FUNCTION__";
593 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000594 case PredefinedExpr::PrettyFunction:
Anders Carlsson22742662007-07-21 05:21:51 +0000595 OS << "__PRETTY_FUNCTION__";
596 break;
597 }
598}
599
Reid Spencer5f016e22007-07-11 17:01:13 +0000600void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000601 unsigned value = Node->getValue();
Douglas Gregor5cee1192011-07-27 05:40:30 +0000602
603 switch (Node->getKind()) {
604 case CharacterLiteral::Ascii: break; // no prefix.
605 case CharacterLiteral::Wide: OS << 'L'; break;
606 case CharacterLiteral::UTF16: OS << 'u'; break;
607 case CharacterLiteral::UTF32: OS << 'U'; break;
608 }
609
Chris Lattner8bf9f072007-07-13 23:58:20 +0000610 switch (value) {
611 case '\\':
612 OS << "'\\\\'";
613 break;
614 case '\'':
615 OS << "'\\''";
616 break;
617 case '\a':
618 // TODO: K&R: the meaning of '\\a' is different in traditional C
619 OS << "'\\a'";
620 break;
621 case '\b':
622 OS << "'\\b'";
623 break;
624 // Nonstandard escape sequence.
625 /*case '\e':
626 OS << "'\\e'";
627 break;*/
628 case '\f':
629 OS << "'\\f'";
630 break;
631 case '\n':
632 OS << "'\\n'";
633 break;
634 case '\r':
635 OS << "'\\r'";
636 break;
637 case '\t':
638 OS << "'\\t'";
639 break;
640 case '\v':
641 OS << "'\\v'";
642 break;
643 default:
Ted Kremenek471733d2008-02-23 00:52:04 +0000644 if (value < 256 && isprint(value)) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000645 OS << "'" << (char)value << "'";
646 } else if (value < 256) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000647 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000648 } else {
649 // FIXME what to really do here?
650 OS << value;
651 }
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000652 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000653}
654
655void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
656 bool isSigned = Node->getType()->isSignedIntegerType();
657 OS << Node->getValue().toString(10, isSigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Reid Spencer5f016e22007-07-11 17:01:13 +0000659 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall183700f2009-09-21 23:43:11 +0000660 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000661 default: llvm_unreachable("Unexpected type for integer literal!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000662 case BuiltinType::Int: break; // no suffix.
663 case BuiltinType::UInt: OS << 'U'; break;
664 case BuiltinType::Long: OS << 'L'; break;
665 case BuiltinType::ULong: OS << "UL"; break;
666 case BuiltinType::LongLong: OS << "LL"; break;
667 case BuiltinType::ULongLong: OS << "ULL"; break;
668 }
669}
670void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Eli Friedmanb3909212011-10-05 20:32:03 +0000671 llvm::SmallString<16> Str;
672 Node->getValue().toString(Str);
673 OS << Str;
Reid Spencer5f016e22007-07-11 17:01:13 +0000674}
Chris Lattner5d661452007-08-26 03:42:43 +0000675
676void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
677 PrintExpr(Node->getSubExpr());
678 OS << "i";
679}
680
Reid Spencer5f016e22007-07-11 17:01:13 +0000681void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Douglas Gregor5cee1192011-07-27 05:40:30 +0000682 switch (Str->getKind()) {
683 case StringLiteral::Ascii: break; // no prefix.
684 case StringLiteral::Wide: OS << 'L'; break;
685 case StringLiteral::UTF8: OS << "u8"; break;
686 case StringLiteral::UTF16: OS << 'u'; break;
687 case StringLiteral::UTF32: OS << 'U'; break;
688 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000689 OS << '"';
Anders Carlssonee98ac52007-10-15 02:50:23 +0000690
Reid Spencer5f016e22007-07-11 17:01:13 +0000691 // FIXME: this doesn't print wstrings right.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000692 StringRef StrData = Str->getString();
693 for (StringRef::iterator I = StrData.begin(), E = StrData.end();
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +0000694 I != E; ++I) {
695 unsigned char Char = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Chris Lattner9a81c872009-01-16 19:25:18 +0000697 switch (Char) {
698 default:
699 if (isprint(Char))
700 OS << (char)Char;
701 else // Output anything hard as an octal escape.
702 OS << '\\'
703 << (char)('0'+ ((Char >> 6) & 7))
704 << (char)('0'+ ((Char >> 3) & 7))
705 << (char)('0'+ ((Char >> 0) & 7));
706 break;
707 // Handle some common non-printable cases to make dumps prettier.
Reid Spencer5f016e22007-07-11 17:01:13 +0000708 case '\\': OS << "\\\\"; break;
709 case '"': OS << "\\\""; break;
710 case '\n': OS << "\\n"; break;
711 case '\t': OS << "\\t"; break;
712 case '\a': OS << "\\a"; break;
713 case '\b': OS << "\\b"; break;
714 }
715 }
716 OS << '"';
717}
718void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
719 OS << "(";
720 PrintExpr(Node->getSubExpr());
721 OS << ")";
722}
723void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +0000724 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000725 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump1eb44332009-09-09 15:08:12 +0000726
Eli Friedman7df71ac2009-06-14 22:39:26 +0000727 // Print a space if this is an "identifier operator" like __real, or if
728 // it might be concatenated incorrectly like '+'.
Chris Lattner296bf192007-08-23 21:46:40 +0000729 switch (Node->getOpcode()) {
730 default: break;
John McCall2de56d12010-08-25 11:45:40 +0000731 case UO_Real:
732 case UO_Imag:
733 case UO_Extension:
Chris Lattner296bf192007-08-23 21:46:40 +0000734 OS << ' ';
735 break;
John McCall2de56d12010-08-25 11:45:40 +0000736 case UO_Plus:
737 case UO_Minus:
Eli Friedman7df71ac2009-06-14 22:39:26 +0000738 if (isa<UnaryOperator>(Node->getSubExpr()))
739 OS << ' ';
740 break;
Chris Lattner296bf192007-08-23 21:46:40 +0000741 }
742 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000743 PrintExpr(Node->getSubExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Reid Spencer5f016e22007-07-11 17:01:13 +0000745 if (Node->isPostfix())
746 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +0000747}
Chris Lattner704fe352007-08-30 17:59:59 +0000748
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000749void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
750 OS << "__builtin_offsetof(";
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000751 OS << Node->getTypeSourceInfo()->getType().getAsString(Policy) << ", ";
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000752 bool PrintedSomething = false;
753 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
754 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
755 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
756 // Array node
757 OS << "[";
758 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
759 OS << "]";
760 PrintedSomething = true;
761 continue;
762 }
Douglas Gregorcc8a5d52010-04-29 00:18:15 +0000763
764 // Skip implicit base indirections.
765 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
766 continue;
767
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000768 // Field or identifier node.
769 IdentifierInfo *Id = ON.getFieldName();
770 if (!Id)
771 continue;
Sean Huntc3021132010-05-05 15:23:54 +0000772
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000773 if (PrintedSomething)
774 OS << ".";
775 else
776 PrintedSomething = true;
Sean Huntc3021132010-05-05 15:23:54 +0000777 OS << Id->getName();
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000778 }
779 OS << ")";
780}
781
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000782void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
783 switch(Node->getKind()) {
784 case UETT_SizeOf:
785 OS << "sizeof";
786 break;
787 case UETT_AlignOf:
788 OS << "__alignof";
789 break;
790 case UETT_VecStep:
791 OS << "vec_step";
792 break;
793 }
Sebastian Redl05189992008-11-11 17:56:53 +0000794 if (Node->isArgumentType())
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000795 OS << "(" << Node->getArgumentType().getAsString(Policy) << ")";
Sebastian Redl05189992008-11-11 17:56:53 +0000796 else {
797 OS << " ";
798 PrintExpr(Node->getArgumentExpr());
799 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000800}
Peter Collingbournef111d932011-04-15 00:35:48 +0000801
802void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
803 OS << "_Generic(";
804 PrintExpr(Node->getControllingExpr());
805 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
806 OS << ", ";
807 QualType T = Node->getAssocType(i);
808 if (T.isNull())
809 OS << "default";
810 else
811 OS << T.getAsString(Policy);
812 OS << ": ";
813 PrintExpr(Node->getAssocExpr(i));
814 }
815 OS << ")";
816}
817
Reid Spencer5f016e22007-07-11 17:01:13 +0000818void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +0000819 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000820 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +0000821 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000822 OS << "]";
823}
824
Peter Collingbourned64e2372011-02-08 21:17:54 +0000825void StmtPrinter::PrintCallArgs(CallExpr *Call) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000826 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattner04421082008-04-08 04:40:51 +0000827 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
828 // Don't print any defaulted arguments
829 break;
830 }
831
Reid Spencer5f016e22007-07-11 17:01:13 +0000832 if (i) OS << ", ";
833 PrintExpr(Call->getArg(i));
834 }
Peter Collingbourned64e2372011-02-08 21:17:54 +0000835}
836
837void StmtPrinter::VisitCallExpr(CallExpr *Call) {
838 PrintExpr(Call->getCallee());
839 OS << "(";
840 PrintCallArgs(Call);
Reid Spencer5f016e22007-07-11 17:01:13 +0000841 OS << ")";
842}
843void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregorb3eef682009-01-08 22:45:41 +0000844 // FIXME: Suppress printing implicit bases (like "this")
845 PrintExpr(Node->getBase());
Fariborz Jahanian97fd83a2010-01-11 21:17:32 +0000846 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
847 if (FD->isAnonymousStructOrUnion())
848 return;
Douglas Gregorb3eef682009-01-08 22:45:41 +0000849 OS << (Node->isArrow() ? "->" : ".");
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000850 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
851 Qualifier->print(OS, Policy);
852
Abramo Bagnara25777432010-08-11 22:01:17 +0000853 OS << Node->getMemberNameInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000854
John McCall096832c2010-08-19 23:49:38 +0000855 if (Node->hasExplicitTemplateArgs())
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000856 OS << TemplateSpecializationType::PrintTemplateArgumentList(
857 Node->getTemplateArgs(),
858 Node->getNumTemplateArgs(),
859 Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000860}
Steve Narofff242b1b2009-07-24 17:54:45 +0000861void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
862 PrintExpr(Node->getBase());
863 OS << (Node->isArrow() ? "->isa" : ".isa");
864}
865
Nate Begeman213541a2008-04-18 23:10:10 +0000866void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +0000867 PrintExpr(Node->getBase());
868 OS << ".";
869 OS << Node->getAccessor().getName();
870}
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000871void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Fariborz Jahanian03e80e42010-06-30 16:31:08 +0000872 OS << "(" << Node->getType().getAsString(Policy) << ")";
Reid Spencer5f016e22007-07-11 17:01:13 +0000873 PrintExpr(Node->getSubExpr());
874}
Steve Naroffaff1edd2007-07-19 21:32:11 +0000875void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000876 OS << "(" << Node->getType().getAsString(Policy) << ")";
Steve Naroffaff1edd2007-07-19 21:32:11 +0000877 PrintExpr(Node->getInitializer());
878}
Steve Naroff49b45262007-07-13 16:58:59 +0000879void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroff90045e82007-07-13 23:32:42 +0000880 // No need to print anything, simply forward to the sub expression.
881 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +0000882}
Reid Spencer5f016e22007-07-11 17:01:13 +0000883void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
884 PrintExpr(Node->getLHS());
885 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
886 PrintExpr(Node->getRHS());
887}
Chris Lattnereb14fe82007-08-25 02:00:02 +0000888void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
889 PrintExpr(Node->getLHS());
890 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
891 PrintExpr(Node->getRHS());
892}
Reid Spencer5f016e22007-07-11 17:01:13 +0000893void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
894 PrintExpr(Node->getCond());
John McCall56ca35d2011-02-17 10:25:35 +0000895 OS << " ? ";
896 PrintExpr(Node->getLHS());
897 OS << " : ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000898 PrintExpr(Node->getRHS());
899}
900
901// GNU extensions.
902
John McCall56ca35d2011-02-17 10:25:35 +0000903void
904StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
905 PrintExpr(Node->getCommon());
906 OS << " ?: ";
907 PrintExpr(Node->getFalseExpr());
908}
Chris Lattner6481a572007-08-03 17:31:20 +0000909void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000910 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000911}
912
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000913void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
914 OS << "(";
915 PrintRawCompoundStmt(E->getSubStmt());
916 OS << ")";
917}
918
Steve Naroffd04fdd52007-08-03 21:21:27 +0000919void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
920 OS << "__builtin_choose_expr(";
921 PrintExpr(Node->getCond());
Chris Lattner94f05e32007-08-04 00:20:15 +0000922 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000923 PrintExpr(Node->getLHS());
Chris Lattner94f05e32007-08-04 00:20:15 +0000924 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000925 PrintExpr(Node->getRHS());
926 OS << ")";
927}
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000928
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000929void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
930 OS << "__null";
931}
932
Eli Friedmand38617c2008-05-14 19:38:39 +0000933void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
934 OS << "__builtin_shufflevector(";
935 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
936 if (i) OS << ", ";
937 PrintExpr(Node->getExpr(i));
938 }
939 OS << ")";
940}
941
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000942void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000943 if (Node->getSyntacticForm()) {
944 Visit(Node->getSyntacticForm());
945 return;
946 }
947
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000948 OS << "{ ";
949 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
950 if (i) OS << ", ";
Douglas Gregor4c678342009-01-28 21:54:33 +0000951 if (Node->getInit(i))
952 PrintExpr(Node->getInit(i));
953 else
954 OS << "0";
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000955 }
956 OS << " }";
957}
958
Nate Begeman2ef13e52009-08-10 23:49:36 +0000959void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
960 OS << "( ";
961 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
962 if (i) OS << ", ";
963 PrintExpr(Node->getExpr(i));
964 }
965 OS << " )";
966}
967
Douglas Gregor05c13a32009-01-22 00:58:24 +0000968void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000969 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
970 DEnd = Node->designators_end();
971 D != DEnd; ++D) {
972 if (D->isFieldDesignator()) {
973 if (D->getDotLoc().isInvalid())
974 OS << D->getFieldName()->getName() << ":";
975 else
976 OS << "." << D->getFieldName()->getName();
977 } else {
978 OS << "[";
979 if (D->isArrayDesignator()) {
980 PrintExpr(Node->getArrayIndex(*D));
981 } else {
982 PrintExpr(Node->getArrayRangeStart(*D));
983 OS << " ... ";
Mike Stump1eb44332009-09-09 15:08:12 +0000984 PrintExpr(Node->getArrayRangeEnd(*D));
Douglas Gregor4c678342009-01-28 21:54:33 +0000985 }
986 OS << "]";
987 }
988 }
989
990 OS << " = ";
991 PrintExpr(Node->getInit());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000992}
993
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000994void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Chris Lattnere4f21422009-06-30 01:26:17 +0000995 if (Policy.LangOpts.CPlusPlus)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000996 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()";
997 else {
998 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")";
999 if (Node->getType()->isRecordType())
1000 OS << "{}";
1001 else
1002 OS << 0;
1003 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001004}
1005
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001006void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman42f42c02009-05-30 04:20:30 +00001007 OS << "__builtin_va_arg(";
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001008 PrintExpr(Node->getSubExpr());
1009 OS << ", ";
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001010 OS << Node->getType().getAsString(Policy);
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001011 OS << ")";
1012}
1013
Eli Friedman276b0612011-10-11 02:20:01 +00001014void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
1015 const char *Name;
1016 switch (Node->getOp()) {
1017 case AtomicExpr::Load:
1018 Name = "__atomic_load(";
1019 break;
1020 case AtomicExpr::Store:
1021 Name = "__atomic_store(";
1022 break;
1023 case AtomicExpr::CmpXchgStrong:
1024 Name = "__atomic_compare_exchange_strong(";
1025 break;
1026 case AtomicExpr::CmpXchgWeak:
1027 Name = "__atomic_compare_exchange_weak(";
1028 break;
1029 case AtomicExpr::Xchg:
1030 Name = "__atomic_exchange(";
1031 break;
1032 case AtomicExpr::Add:
1033 Name = "__atomic_fetch_add(";
1034 break;
1035 case AtomicExpr::Sub:
1036 Name = "__atomic_fetch_sub(";
1037 break;
1038 case AtomicExpr::And:
1039 Name = "__atomic_fetch_and(";
1040 break;
1041 case AtomicExpr::Or:
1042 Name = "__atomic_fetch_or(";
1043 break;
1044 case AtomicExpr::Xor:
1045 Name = "__atomic_fetch_xor(";
1046 break;
1047 }
1048 OS << Name;
1049 PrintExpr(Node->getPtr());
1050 OS << ", ";
1051 if (Node->getOp() != AtomicExpr::Load) {
1052 PrintExpr(Node->getVal1());
1053 OS << ", ";
1054 }
1055 if (Node->isCmpXChg()) {
1056 PrintExpr(Node->getVal2());
1057 OS << ", ";
1058 }
1059 PrintExpr(Node->getOrder());
1060 if (Node->isCmpXChg()) {
1061 OS << ", ";
1062 PrintExpr(Node->getOrderFail());
1063 }
1064 OS << ")";
1065}
1066
Reid Spencer5f016e22007-07-11 17:01:13 +00001067// C++
Douglas Gregorb4609802008-11-14 16:09:21 +00001068void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1069 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1070 "",
1071#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1072 Spelling,
1073#include "clang/Basic/OperatorKinds.def"
1074 };
1075
1076 OverloadedOperatorKind Kind = Node->getOperator();
1077 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1078 if (Node->getNumArgs() == 1) {
1079 OS << OpStrings[Kind] << ' ';
1080 PrintExpr(Node->getArg(0));
1081 } else {
1082 PrintExpr(Node->getArg(0));
1083 OS << ' ' << OpStrings[Kind];
1084 }
1085 } else if (Kind == OO_Call) {
1086 PrintExpr(Node->getArg(0));
1087 OS << '(';
1088 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1089 if (ArgIdx > 1)
1090 OS << ", ";
1091 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1092 PrintExpr(Node->getArg(ArgIdx));
1093 }
1094 OS << ')';
1095 } else if (Kind == OO_Subscript) {
1096 PrintExpr(Node->getArg(0));
1097 OS << '[';
1098 PrintExpr(Node->getArg(1));
1099 OS << ']';
1100 } else if (Node->getNumArgs() == 1) {
1101 OS << OpStrings[Kind] << ' ';
1102 PrintExpr(Node->getArg(0));
1103 } else if (Node->getNumArgs() == 2) {
1104 PrintExpr(Node->getArg(0));
1105 OS << ' ' << OpStrings[Kind] << ' ';
1106 PrintExpr(Node->getArg(1));
1107 } else {
David Blaikieb219cfc2011-09-23 05:06:16 +00001108 llvm_unreachable("unknown overloaded operator");
Douglas Gregorb4609802008-11-14 16:09:21 +00001109 }
1110}
Reid Spencer5f016e22007-07-11 17:01:13 +00001111
Douglas Gregor88a35142008-12-22 05:46:06 +00001112void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1113 VisitCallExpr(cast<CallExpr>(Node));
1114}
1115
Peter Collingbournee08ce652011-02-09 21:07:24 +00001116void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1117 PrintExpr(Node->getCallee());
1118 OS << "<<<";
1119 PrintCallArgs(Node->getConfig());
1120 OS << ">>>(";
1121 PrintCallArgs(Node);
1122 OS << ")";
1123}
1124
Douglas Gregor49badde2008-10-27 19:41:14 +00001125void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1126 OS << Node->getCastName() << '<';
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001127 OS << Node->getTypeAsWritten().getAsString(Policy) << ">(";
Reid Spencer5f016e22007-07-11 17:01:13 +00001128 PrintExpr(Node->getSubExpr());
1129 OS << ")";
1130}
1131
Douglas Gregor49badde2008-10-27 19:41:14 +00001132void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1133 VisitCXXNamedCastExpr(Node);
1134}
1135
1136void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1137 VisitCXXNamedCastExpr(Node);
1138}
1139
1140void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1141 VisitCXXNamedCastExpr(Node);
1142}
1143
1144void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1145 VisitCXXNamedCastExpr(Node);
1146}
1147
Sebastian Redlc42e1182008-11-11 11:37:55 +00001148void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1149 OS << "typeid(";
1150 if (Node->isTypeOperand()) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001151 OS << Node->getTypeOperand().getAsString(Policy);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001152 } else {
1153 PrintExpr(Node->getExprOperand());
1154 }
1155 OS << ")";
1156}
1157
Francois Pichet01b7c302010-09-08 12:20:18 +00001158void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1159 OS << "__uuidof(";
1160 if (Node->isTypeOperand()) {
1161 OS << Node->getTypeOperand().getAsString(Policy);
1162 } else {
1163 PrintExpr(Node->getExprOperand());
1164 }
1165 OS << ")";
1166}
1167
Reid Spencer5f016e22007-07-11 17:01:13 +00001168void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1169 OS << (Node->getValue() ? "true" : "false");
1170}
1171
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001172void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1173 OS << "nullptr";
1174}
1175
Douglas Gregor796da182008-11-04 14:32:21 +00001176void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1177 OS << "this";
1178}
1179
Chris Lattner50dd2892008-02-26 00:51:44 +00001180void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1181 if (Node->getSubExpr() == 0)
1182 OS << "throw";
1183 else {
1184 OS << "throw ";
1185 PrintExpr(Node->getSubExpr());
1186 }
1187}
1188
Chris Lattner04421082008-04-08 04:40:51 +00001189void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1190 // Nothing to print: we picked up the default argument
1191}
1192
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001193void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001194 OS << Node->getType().getAsString(Policy);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001195 OS << "(";
1196 PrintExpr(Node->getSubExpr());
1197 OS << ")";
1198}
1199
Anders Carlssonfceb0a82009-05-30 20:03:25 +00001200void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1201 PrintExpr(Node->getSubExpr());
1202}
1203
Douglas Gregor506ae412009-01-16 18:33:17 +00001204void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001205 OS << Node->getType().getAsString(Policy);
Douglas Gregor506ae412009-01-16 18:33:17 +00001206 OS << "(";
1207 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001208 ArgEnd = Node->arg_end();
Douglas Gregor506ae412009-01-16 18:33:17 +00001209 Arg != ArgEnd; ++Arg) {
1210 if (Arg != Node->arg_begin())
1211 OS << ", ";
1212 PrintExpr(*Arg);
1213 }
1214 OS << ")";
1215}
1216
Douglas Gregored8abf12010-07-08 06:14:04 +00001217void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregorab6677e2010-09-08 00:15:04 +00001218 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
1219 OS << TSInfo->getType().getAsString(Policy) << "()";
1220 else
1221 OS << Node->getType().getAsString(Policy) << "()";
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001222}
1223
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001224void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1225 if (E->isGlobalNew())
1226 OS << "::";
1227 OS << "new ";
1228 unsigned NumPlace = E->getNumPlacementArgs();
1229 if (NumPlace > 0) {
1230 OS << "(";
1231 PrintExpr(E->getPlacementArg(0));
1232 for (unsigned i = 1; i < NumPlace; ++i) {
1233 OS << ", ";
1234 PrintExpr(E->getPlacementArg(i));
1235 }
1236 OS << ") ";
1237 }
1238 if (E->isParenTypeId())
1239 OS << "(";
Sebastian Redl6fec6482008-12-02 22:08:59 +00001240 std::string TypeS;
1241 if (Expr *Size = E->getArraySize()) {
1242 llvm::raw_string_ostream s(TypeS);
Eli Friedman6e1a3452009-05-30 05:32:46 +00001243 Size->printPretty(s, Context, Helper, Policy);
Sebastian Redl6fec6482008-12-02 22:08:59 +00001244 s.flush();
1245 TypeS = "[" + TypeS + "]";
1246 }
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001247 E->getAllocatedType().getAsStringInternal(TypeS, Policy);
Sebastian Redl6fec6482008-12-02 22:08:59 +00001248 OS << TypeS;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001249 if (E->isParenTypeId())
1250 OS << ")";
1251
1252 if (E->hasInitializer()) {
1253 OS << "(";
1254 unsigned NumCons = E->getNumConstructorArgs();
1255 if (NumCons > 0) {
1256 PrintExpr(E->getConstructorArg(0));
1257 for (unsigned i = 1; i < NumCons; ++i) {
1258 OS << ", ";
1259 PrintExpr(E->getConstructorArg(i));
1260 }
1261 }
1262 OS << ")";
1263 }
1264}
1265
1266void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1267 if (E->isGlobalDelete())
1268 OS << "::";
1269 OS << "delete ";
1270 if (E->isArrayForm())
1271 OS << "[] ";
1272 PrintExpr(E->getArgument());
1273}
1274
Douglas Gregora71d8192009-09-04 17:36:40 +00001275void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1276 PrintExpr(E->getBase());
1277 if (E->isArrow())
1278 OS << "->";
1279 else
1280 OS << '.';
1281 if (E->getQualifier())
1282 E->getQualifier()->print(OS, Policy);
Mike Stump1eb44332009-09-09 15:08:12 +00001283
Douglas Gregora71d8192009-09-04 17:36:40 +00001284 std::string TypeS;
Douglas Gregora2e7dd22010-02-25 01:56:36 +00001285 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1286 OS << II->getName();
1287 else
1288 E->getDestroyedType().getAsStringInternal(TypeS, Policy);
Douglas Gregora71d8192009-09-04 17:36:40 +00001289 OS << TypeS;
1290}
1291
Anders Carlssone349bea2009-04-23 02:32:43 +00001292void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Douglas Gregord0fb3ad2011-01-24 17:25:03 +00001293 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1294 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1295 // Don't print any defaulted arguments
1296 break;
1297 }
1298
1299 if (i) OS << ", ";
1300 PrintExpr(E->getArg(i));
Fariborz Jahanianc75da512010-01-13 21:41:11 +00001301 }
Anders Carlssone349bea2009-04-23 02:32:43 +00001302}
1303
John McCall4765fa02010-12-06 08:20:24 +00001304void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Anders Carlsson02bbfa32009-04-24 22:47:04 +00001305 // Just forward to the sub expression.
1306 PrintExpr(E->getSubExpr());
1307}
1308
Mike Stump1eb44332009-09-09 15:08:12 +00001309void
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001310StmtPrinter::VisitCXXUnresolvedConstructExpr(
1311 CXXUnresolvedConstructExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001312 OS << Node->getTypeAsWritten().getAsString(Policy);
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001313 OS << "(";
1314 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001315 ArgEnd = Node->arg_end();
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001316 Arg != ArgEnd; ++Arg) {
1317 if (Arg != Node->arg_begin())
1318 OS << ", ";
1319 PrintExpr(*Arg);
1320 }
1321 OS << ")";
1322}
1323
John McCall865d4472009-11-19 22:55:06 +00001324void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1325 CXXDependentScopeMemberExpr *Node) {
John McCallaa81e162009-12-01 22:10:20 +00001326 if (!Node->isImplicitAccess()) {
1327 PrintExpr(Node->getBase());
1328 OS << (Node->isArrow() ? "->" : ".");
1329 }
Douglas Gregora38c6872009-09-03 16:14:30 +00001330 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1331 Qualifier->print(OS, Policy);
John McCallaa81e162009-12-01 22:10:20 +00001332 else if (Node->hasExplicitTemplateArgs())
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001333 // FIXME: Track use of "template" keyword explicitly?
1334 OS << "template ";
Mike Stump1eb44332009-09-09 15:08:12 +00001335
Abramo Bagnara25777432010-08-11 22:01:17 +00001336 OS << Node->getMemberNameInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00001337
John McCallaa81e162009-12-01 22:10:20 +00001338 if (Node->hasExplicitTemplateArgs()) {
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001339 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1340 Node->getTemplateArgs(),
1341 Node->getNumTemplateArgs(),
1342 Policy);
1343 }
Douglas Gregor1c0ca592009-05-22 21:13:27 +00001344}
1345
John McCall129e2df2009-11-30 22:42:35 +00001346void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCallaa81e162009-12-01 22:10:20 +00001347 if (!Node->isImplicitAccess()) {
1348 PrintExpr(Node->getBase());
1349 OS << (Node->isArrow() ? "->" : ".");
1350 }
John McCall129e2df2009-11-30 22:42:35 +00001351 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1352 Qualifier->print(OS, Policy);
1353
1354 // FIXME: this might originally have been written with 'template'
1355
Abramo Bagnara25777432010-08-11 22:01:17 +00001356 OS << Node->getMemberNameInfo();
John McCall129e2df2009-11-30 22:42:35 +00001357
1358 if (Node->hasExplicitTemplateArgs()) {
1359 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1360 Node->getTemplateArgs(),
1361 Node->getNumTemplateArgs(),
1362 Policy);
1363 }
1364}
1365
Sebastian Redl64b45f72009-01-05 20:52:13 +00001366static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1367 switch (UTT) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001368 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001369 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
John Wiegley20c0da72011-04-27 23:09:49 +00001370 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001371 case UTT_HasTrivialAssign: return "__has_trivial_assign";
Sean Hunt023df372011-05-09 18:22:59 +00001372 case UTT_HasTrivialDefaultConstructor: return "__has_trivial_constructor";
John Wiegley20c0da72011-04-27 23:09:49 +00001373 case UTT_HasTrivialCopy: return "__has_trivial_copy";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001374 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1375 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1376 case UTT_IsAbstract: return "__is_abstract";
John Wiegley20c0da72011-04-27 23:09:49 +00001377 case UTT_IsArithmetic: return "__is_arithmetic";
1378 case UTT_IsArray: return "__is_array";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001379 case UTT_IsClass: return "__is_class";
John Wiegley20c0da72011-04-27 23:09:49 +00001380 case UTT_IsCompleteType: return "__is_complete_type";
1381 case UTT_IsCompound: return "__is_compound";
1382 case UTT_IsConst: return "__is_const";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001383 case UTT_IsEmpty: return "__is_empty";
1384 case UTT_IsEnum: return "__is_enum";
John Wiegley20c0da72011-04-27 23:09:49 +00001385 case UTT_IsFloatingPoint: return "__is_floating_point";
1386 case UTT_IsFunction: return "__is_function";
1387 case UTT_IsFundamental: return "__is_fundamental";
1388 case UTT_IsIntegral: return "__is_integral";
Chandler Carruthe1947102011-05-01 07:23:20 +00001389 case UTT_IsLiteral: return "__is_literal";
John Wiegley20c0da72011-04-27 23:09:49 +00001390 case UTT_IsLvalueReference: return "__is_lvalue_reference";
1391 case UTT_IsMemberFunctionPointer: return "__is_member_function_pointer";
1392 case UTT_IsMemberObjectPointer: return "__is_member_object_pointer";
1393 case UTT_IsMemberPointer: return "__is_member_pointer";
1394 case UTT_IsObject: return "__is_object";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001395 case UTT_IsPOD: return "__is_pod";
John Wiegley20c0da72011-04-27 23:09:49 +00001396 case UTT_IsPointer: return "__is_pointer";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001397 case UTT_IsPolymorphic: return "__is_polymorphic";
John Wiegley20c0da72011-04-27 23:09:49 +00001398 case UTT_IsReference: return "__is_reference";
John Wiegley20c0da72011-04-27 23:09:49 +00001399 case UTT_IsRvalueReference: return "__is_rvalue_reference";
1400 case UTT_IsScalar: return "__is_scalar";
1401 case UTT_IsSigned: return "__is_signed";
1402 case UTT_IsStandardLayout: return "__is_standard_layout";
1403 case UTT_IsTrivial: return "__is_trivial";
Sean Huntfeb375d2011-05-13 00:31:07 +00001404 case UTT_IsTriviallyCopyable: return "__is_trivially_copyable";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001405 case UTT_IsUnion: return "__is_union";
John Wiegley20c0da72011-04-27 23:09:49 +00001406 case UTT_IsUnsigned: return "__is_unsigned";
1407 case UTT_IsVoid: return "__is_void";
1408 case UTT_IsVolatile: return "__is_volatile";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001409 }
Chandler Carruthe1947102011-05-01 07:23:20 +00001410 llvm_unreachable("Type trait not covered by switch statement");
Francois Pichet6ad6f282010-12-07 00:08:36 +00001411}
1412
1413static const char *getTypeTraitName(BinaryTypeTrait BTT) {
1414 switch (BTT) {
Francois Pichetf1872372010-12-08 22:35:30 +00001415 case BTT_IsBaseOf: return "__is_base_of";
John Wiegley20c0da72011-04-27 23:09:49 +00001416 case BTT_IsConvertible: return "__is_convertible";
1417 case BTT_IsSame: return "__is_same";
Francois Pichetf1872372010-12-08 22:35:30 +00001418 case BTT_TypeCompatible: return "__builtin_types_compatible_p";
Douglas Gregor9f361132011-01-27 20:28:01 +00001419 case BTT_IsConvertibleTo: return "__is_convertible_to";
Francois Pichet6ad6f282010-12-07 00:08:36 +00001420 }
Chandler Carrutha280bc82011-05-01 07:23:23 +00001421 llvm_unreachable("Binary type trait not covered by switch");
Sebastian Redl64b45f72009-01-05 20:52:13 +00001422}
1423
John Wiegley21ff2e52011-04-28 00:16:57 +00001424static const char *getTypeTraitName(ArrayTypeTrait ATT) {
1425 switch (ATT) {
1426 case ATT_ArrayRank: return "__array_rank";
1427 case ATT_ArrayExtent: return "__array_extent";
1428 }
Chandler Carrutha280bc82011-05-01 07:23:23 +00001429 llvm_unreachable("Array type trait not covered by switch");
John Wiegley21ff2e52011-04-28 00:16:57 +00001430}
1431
John Wiegley55262202011-04-25 06:54:41 +00001432static const char *getExpressionTraitName(ExpressionTrait ET) {
1433 switch (ET) {
John Wiegley55262202011-04-25 06:54:41 +00001434 case ET_IsLValueExpr: return "__is_lvalue_expr";
1435 case ET_IsRValueExpr: return "__is_rvalue_expr";
1436 }
Chandler Carrutha280bc82011-05-01 07:23:23 +00001437 llvm_unreachable("Expression type trait not covered by switch");
John Wiegley55262202011-04-25 06:54:41 +00001438}
1439
Sebastian Redl64b45f72009-01-05 20:52:13 +00001440void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1441 OS << getTypeTraitName(E->getTrait()) << "("
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001442 << E->getQueriedType().getAsString(Policy) << ")";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001443}
1444
Francois Pichet6ad6f282010-12-07 00:08:36 +00001445void StmtPrinter::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
1446 OS << getTypeTraitName(E->getTrait()) << "("
1447 << E->getLhsType().getAsString(Policy) << ","
1448 << E->getRhsType().getAsString(Policy) << ")";
1449}
1450
John Wiegley21ff2e52011-04-28 00:16:57 +00001451void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
1452 OS << getTypeTraitName(E->getTrait()) << "("
1453 << E->getQueriedType().getAsString(Policy) << ")";
1454}
1455
John Wiegley55262202011-04-25 06:54:41 +00001456void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
1457 OS << getExpressionTraitName(E->getTrait()) << "(";
1458 PrintExpr(E->getQueriedExpression());
1459 OS << ")";
1460}
1461
Sebastian Redl2e156222010-09-10 20:55:43 +00001462void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1463 OS << "noexcept(";
1464 PrintExpr(E->getOperand());
1465 OS << ")";
1466}
1467
Douglas Gregoree8aff02011-01-04 17:33:58 +00001468void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregorbe230c32011-01-03 17:17:50 +00001469 PrintExpr(E->getPattern());
1470 OS << "...";
1471}
1472
Douglas Gregoree8aff02011-01-04 17:33:58 +00001473void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
1474 OS << "sizeof...(" << E->getPack()->getNameAsString() << ")";
1475}
1476
Douglas Gregorc7793c72011-01-15 01:15:58 +00001477void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
1478 SubstNonTypeTemplateParmPackExpr *Node) {
1479 OS << Node->getParameterPack()->getNameAsString();
1480}
1481
John McCall91a57552011-07-15 05:09:51 +00001482void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
1483 SubstNonTypeTemplateParmExpr *Node) {
1484 Visit(Node->getReplacement());
1485}
1486
Douglas Gregor03e80032011-06-21 17:03:29 +00001487void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
1488 PrintExpr(Node->GetTemporaryExpr());
1489}
1490
Mike Stump1eb44332009-09-09 15:08:12 +00001491// Obj-C
Anders Carlsson55085182007-08-21 17:43:55 +00001492
1493void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1494 OS << "@";
1495 VisitStringLiteral(Node->getString());
1496}
Reid Spencer5f016e22007-07-11 17:01:13 +00001497
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001498void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001499 OS << "@encode(" << Node->getEncodedType().getAsString(Policy) << ')';
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001500}
1501
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001502void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001503 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001504}
1505
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001506void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramer900fc632010-04-17 09:33:03 +00001507 OS << "@protocol(" << Node->getProtocol() << ')';
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001508}
1509
Steve Naroff563477d2007-09-18 23:55:05 +00001510void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1511 OS << "[";
Douglas Gregor04badcf2010-04-21 00:45:42 +00001512 switch (Mess->getReceiverKind()) {
1513 case ObjCMessageExpr::Instance:
1514 PrintExpr(Mess->getInstanceReceiver());
1515 break;
1516
1517 case ObjCMessageExpr::Class:
1518 OS << Mess->getClassReceiver().getAsString(Policy);
1519 break;
1520
1521 case ObjCMessageExpr::SuperInstance:
1522 case ObjCMessageExpr::SuperClass:
1523 OS << "Super";
1524 break;
1525 }
1526
Ted Kremenekc29efd82008-05-02 17:32:38 +00001527 OS << ' ';
Ted Kremenek97b7f262008-04-16 04:30:16 +00001528 Selector selector = Mess->getSelector();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001529 if (selector.isUnarySelector()) {
Douglas Gregor813d8342011-02-18 22:29:55 +00001530 OS << selector.getNameForSlot(0);
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001531 } else {
1532 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001533 if (i < selector.getNumArgs()) {
1534 if (i > 0) OS << ' ';
1535 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner39f34e92008-11-24 04:00:27 +00001536 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremenekc29efd82008-05-02 17:32:38 +00001537 else
1538 OS << ":";
1539 }
1540 else OS << ", "; // Handle variadic methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001541
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001542 PrintExpr(Mess->getArg(i));
1543 }
Steve Naroff563477d2007-09-18 23:55:05 +00001544 }
1545 OS << "]";
1546}
1547
John McCallf85e1932011-06-15 23:02:42 +00001548void
1549StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
1550 PrintExpr(E->getSubExpr());
1551}
1552
1553void
1554StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
1555 OS << "(" << E->getBridgeKindName() << E->getType().getAsString(Policy)
1556 << ")";
1557 PrintExpr(E->getSubExpr());
1558}
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001559
Steve Naroff4eb206b2008-09-03 18:15:37 +00001560void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001561 BlockDecl *BD = Node->getBlockDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001562 OS << "^";
Mike Stump1eb44332009-09-09 15:08:12 +00001563
Steve Naroff4eb206b2008-09-03 18:15:37 +00001564 const FunctionType *AFT = Node->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +00001565
Douglas Gregor72564e72009-02-26 23:50:07 +00001566 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001567 OS << "()";
Douglas Gregor72564e72009-02-26 23:50:07 +00001568 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001569 OS << '(';
1570 std::string ParamStr;
Steve Naroff56ee6892008-10-08 17:01:13 +00001571 for (BlockDecl::param_iterator AI = BD->param_begin(),
1572 E = BD->param_end(); AI != E; ++AI) {
1573 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner39f34e92008-11-24 04:00:27 +00001574 ParamStr = (*AI)->getNameAsString();
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001575 (*AI)->getType().getAsStringInternal(ParamStr, Policy);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001576 OS << ParamStr;
1577 }
Mike Stump1eb44332009-09-09 15:08:12 +00001578
Douglas Gregor72564e72009-02-26 23:50:07 +00001579 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001580 if (FT->isVariadic()) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001581 if (!BD->param_empty()) OS << ", ";
Steve Naroff4eb206b2008-09-03 18:15:37 +00001582 OS << "...";
1583 }
1584 OS << ')';
1585 }
1586}
1587
Steve Naroff4eb206b2008-09-03 18:15:37 +00001588void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Benjamin Kramer900fc632010-04-17 09:33:03 +00001589 OS << Node->getDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001590}
John McCall7cd7d1a2010-11-15 23:31:06 +00001591
1592void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {}
1593
Tanya Lattner61eee0c2011-06-04 00:47:47 +00001594void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
1595 OS << "__builtin_astype(";
1596 PrintExpr(Node->getSrcExpr());
1597 OS << ", " << Node->getType().getAsString();
1598 OS << ")";
1599}
1600
Reid Spencer5f016e22007-07-11 17:01:13 +00001601//===----------------------------------------------------------------------===//
1602// Stmt method implementations
1603//===----------------------------------------------------------------------===//
1604
Eli Friedman48d14a22009-05-30 05:03:24 +00001605void Stmt::dumpPretty(ASTContext& Context) const {
Argyrios Kyrtzidis96680332010-08-09 10:54:31 +00001606 printPretty(llvm::errs(), Context, 0,
Chris Lattnere4f21422009-06-30 01:26:17 +00001607 PrintingPolicy(Context.getLangOptions()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001608}
1609
Chris Lattner5f9e2722011-07-23 10:55:15 +00001610void Stmt::printPretty(raw_ostream &OS, ASTContext& Context,
Eli Friedman48d14a22009-05-30 05:03:24 +00001611 PrinterHelper* Helper,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001612 const PrintingPolicy &Policy,
1613 unsigned Indentation) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001614 if (this == 0) {
1615 OS << "<NULL>";
1616 return;
1617 }
1618
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001619 if (Policy.Dump && &Context) {
Argyrios Kyrtzidis96680332010-08-09 10:54:31 +00001620 dump(OS, Context.getSourceManager());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001621 return;
1622 }
Mike Stump1eb44332009-09-09 15:08:12 +00001623
Eli Friedman48d14a22009-05-30 05:03:24 +00001624 StmtPrinter P(OS, Context, Helper, Policy, Indentation);
Chris Lattnerc5598cb2007-08-21 04:04:25 +00001625 P.Visit(const_cast<Stmt*>(this));
Reid Spencer5f016e22007-07-11 17:01:13 +00001626}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001627
1628//===----------------------------------------------------------------------===//
1629// PrinterHelper
1630//===----------------------------------------------------------------------===//
1631
1632// Implement virtual destructor.
Gabor Greif84675832007-09-11 15:32:40 +00001633PrinterHelper::~PrinterHelper() {}