blob: 74429ce31df4a072b11538dc7b5e6ce4d9f2523f [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- StmtPrinter.cpp - Printing implementation for Stmt ASTs ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner6000dac2007-08-08 22:51:59 +000010// This file implements the Stmt::dumpPretty/Stmt::printPretty methods, which
11// pretty print the AST back out to C code.
Reid Spencer5f016e22007-07-11 17:01:13 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "clang/AST/StmtVisitor.h"
Douglas Gregor1a49af92009-01-06 05:10:23 +000016#include "clang/AST/DeclCXX.h"
Ted Kremenek91d1d7a2007-10-17 18:36:42 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregorc7793c72011-01-15 01:15:58 +000018#include "clang/AST/DeclTemplate.h"
Ted Kremenek42a509f2007-08-31 21:30:12 +000019#include "clang/AST/PrettyPrinter.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000020#include "llvm/Support/Format.h"
Douglas Gregor8ecdb652010-04-28 22:16:22 +000021#include "clang/AST/Expr.h"
Douglas Gregorab6677e2010-09-08 00:15:04 +000022#include "clang/AST/ExprCXX.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// StmtPrinter Visitor
27//===----------------------------------------------------------------------===//
28
29namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +000030 class StmtPrinter : public StmtVisitor<StmtPrinter> {
Ted Kremeneka95d3752008-09-13 05:16:45 +000031 llvm::raw_ostream &OS;
Eli Friedman48d14a22009-05-30 05:03:24 +000032 ASTContext &Context;
Reid Spencer5f016e22007-07-11 17:01:13 +000033 unsigned IndentLevel;
Ted Kremenek42a509f2007-08-31 21:30:12 +000034 clang::PrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000035 PrintingPolicy Policy;
36
Reid Spencer5f016e22007-07-11 17:01:13 +000037 public:
Mike Stump1eb44332009-09-09 15:08:12 +000038 StmtPrinter(llvm::raw_ostream &os, ASTContext &C, PrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +000039 const PrintingPolicy &Policy,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000040 unsigned Indentation = 0)
Eli Friedman48d14a22009-05-30 05:03:24 +000041 : OS(os), Context(C), IndentLevel(Indentation), Helper(helper),
42 Policy(Policy) {}
Mike Stump1eb44332009-09-09 15:08:12 +000043
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000044 void PrintStmt(Stmt *S) {
45 PrintStmt(S, Policy.Indentation);
46 }
47
48 void PrintStmt(Stmt *S, int SubIndent) {
Reid Spencer5f016e22007-07-11 17:01:13 +000049 IndentLevel += SubIndent;
50 if (S && isa<Expr>(S)) {
51 // If this is an expr used in a stmt context, indent and newline it.
52 Indent();
Chris Lattnerc5598cb2007-08-21 04:04:25 +000053 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000054 OS << ";\n";
55 } else if (S) {
Chris Lattnerc5598cb2007-08-21 04:04:25 +000056 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000057 } else {
58 Indent() << "<<<NULL STATEMENT>>>\n";
59 }
60 IndentLevel -= SubIndent;
61 }
Eli Friedmandb23b152009-05-30 00:19:54 +000062
Reid Spencer5f016e22007-07-11 17:01:13 +000063 void PrintRawCompoundStmt(CompoundStmt *S);
64 void PrintRawDecl(Decl *D);
Ted Kremenekecd64c52008-10-06 18:39:36 +000065 void PrintRawDeclStmt(DeclStmt *S);
Reid Spencer5f016e22007-07-11 17:01:13 +000066 void PrintRawIfStmt(IfStmt *If);
Sebastian Redl8351da02008-12-22 21:35:02 +000067 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
Mike Stump1eb44332009-09-09 15:08:12 +000068
Reid Spencer5f016e22007-07-11 17:01:13 +000069 void PrintExpr(Expr *E) {
70 if (E)
Chris Lattnerc5598cb2007-08-21 04:04:25 +000071 Visit(E);
Reid Spencer5f016e22007-07-11 17:01:13 +000072 else
73 OS << "<null expr>";
74 }
Mike Stump1eb44332009-09-09 15:08:12 +000075
Mike Stump071e4da2009-02-10 20:16:46 +000076 llvm::raw_ostream &Indent(int Delta = 0) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000077 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
78 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +000079 return OS;
80 }
Mike Stump1eb44332009-09-09 15:08:12 +000081
Mike Stump1eb44332009-09-09 15:08:12 +000082 void Visit(Stmt* S) {
Ted Kremenek42a509f2007-08-31 21:30:12 +000083 if (Helper && Helper->handledStmt(S,OS))
84 return;
85 else StmtVisitor<StmtPrinter>::Visit(S);
86 }
Argyrios Kyrtzidis78e4cc72010-08-15 01:15:33 +000087
Chandler Carruth61e38282010-10-23 08:21:37 +000088 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis78e4cc72010-08-15 01:15:33 +000089 Indent() << "<<unknown stmt type>>\n";
90 }
Chandler Carruth61e38282010-10-23 08:21:37 +000091 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis78e4cc72010-08-15 01:15:33 +000092 OS << "<<unknown expr type>>";
93 }
94 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Mike Stump1eb44332009-09-09 15:08:12 +000095
Argyrios Kyrtzidis78e4cc72010-08-15 01:15:33 +000096#define ABSTRACT_STMT(CLASS)
Douglas Gregorf2cad862008-11-14 12:46:07 +000097#define STMT(CLASS, PARENT) \
Chris Lattnerc5598cb2007-08-21 04:04:25 +000098 void Visit##CLASS(CLASS *Node);
Sean Hunt4bfe1962010-05-05 15:24:00 +000099#include "clang/AST/StmtNodes.inc"
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 };
101}
102
103//===----------------------------------------------------------------------===//
104// Stmt printing methods.
105//===----------------------------------------------------------------------===//
106
Reid Spencer5f016e22007-07-11 17:01:13 +0000107/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
108/// with no newline after the }.
109void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
110 OS << "{\n";
111 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
112 I != E; ++I)
113 PrintStmt(*I);
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 Indent() << "}";
116}
117
118void StmtPrinter::PrintRawDecl(Decl *D) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000119 D->print(OS, Policy, IndentLevel);
Mike Stump071e4da2009-02-10 20:16:46 +0000120}
121
Ted Kremenekecd64c52008-10-06 18:39:36 +0000122void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
Eli Friedmandb23b152009-05-30 00:19:54 +0000123 DeclStmt::decl_iterator Begin = S->decl_begin(), End = S->decl_end();
Eli Friedman42f42c02009-05-30 04:20:30 +0000124 llvm::SmallVector<Decl*, 2> Decls;
Mike Stump1eb44332009-09-09 15:08:12 +0000125 for ( ; Begin != End; ++Begin)
Eli Friedman42f42c02009-05-30 04:20:30 +0000126 Decls.push_back(*Begin);
Eli Friedmandb23b152009-05-30 00:19:54 +0000127
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000128 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
Ted Kremenekecd64c52008-10-06 18:39:36 +0000129}
Reid Spencer5f016e22007-07-11 17:01:13 +0000130
131void StmtPrinter::VisitNullStmt(NullStmt *Node) {
132 Indent() << ";\n";
133}
134
135void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Eli Friedmandb23b152009-05-30 00:19:54 +0000136 Indent();
137 PrintRawDeclStmt(Node);
138 OS << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000139}
140
141void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
142 Indent();
143 PrintRawCompoundStmt(Node);
144 OS << "\n";
145}
146
147void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
148 Indent(-1) << "case ";
149 PrintExpr(Node->getLHS());
150 if (Node->getRHS()) {
151 OS << " ... ";
152 PrintExpr(Node->getRHS());
153 }
154 OS << ":\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 PrintStmt(Node->getSubStmt(), 0);
157}
158
159void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
160 Indent(-1) << "default:\n";
161 PrintStmt(Node->getSubStmt(), 0);
162}
163
164void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
165 Indent(-1) << Node->getName() << ":\n";
166 PrintStmt(Node->getSubStmt(), 0);
167}
168
169void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000170 OS << "if (";
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 PrintExpr(If->getCond());
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000172 OS << ')';
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Reid Spencer5f016e22007-07-11 17:01:13 +0000174 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
175 OS << ' ';
176 PrintRawCompoundStmt(CS);
177 OS << (If->getElse() ? ' ' : '\n');
178 } else {
179 OS << '\n';
180 PrintStmt(If->getThen());
181 if (If->getElse()) Indent();
182 }
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Reid Spencer5f016e22007-07-11 17:01:13 +0000184 if (Stmt *Else = If->getElse()) {
185 OS << "else";
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Reid Spencer5f016e22007-07-11 17:01:13 +0000187 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
188 OS << ' ';
189 PrintRawCompoundStmt(CS);
190 OS << '\n';
191 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
192 OS << ' ';
193 PrintRawIfStmt(ElseIf);
194 } else {
195 OS << '\n';
196 PrintStmt(If->getElse());
197 }
198 }
199}
200
201void StmtPrinter::VisitIfStmt(IfStmt *If) {
202 Indent();
203 PrintRawIfStmt(If);
204}
205
206void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
207 Indent() << "switch (";
208 PrintExpr(Node->getCond());
209 OS << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Reid Spencer5f016e22007-07-11 17:01:13 +0000211 // Pretty print compoundstmt bodies (very common).
212 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
213 OS << " ";
214 PrintRawCompoundStmt(CS);
215 OS << "\n";
216 } else {
217 OS << "\n";
218 PrintStmt(Node->getBody());
219 }
220}
221
Anders Carlssonc1fcb772007-07-22 07:07:56 +0000222void StmtPrinter::VisitSwitchCase(SwitchCase*) {
223 assert(0 && "SwitchCase is an abstract class");
224}
225
Reid Spencer5f016e22007-07-11 17:01:13 +0000226void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
227 Indent() << "while (";
228 PrintExpr(Node->getCond());
229 OS << ")\n";
230 PrintStmt(Node->getBody());
231}
232
233void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner8bdcc472007-09-15 21:49:37 +0000234 Indent() << "do ";
235 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
236 PrintRawCompoundStmt(CS);
237 OS << " ";
238 } else {
239 OS << "\n";
240 PrintStmt(Node->getBody());
241 Indent();
242 }
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Eli Friedmanb3e22962009-05-17 01:05:34 +0000244 OS << "while (";
Reid Spencer5f016e22007-07-11 17:01:13 +0000245 PrintExpr(Node->getCond());
Eli Friedmanb3e22962009-05-17 01:05:34 +0000246 OS << ");\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000247}
248
249void StmtPrinter::VisitForStmt(ForStmt *Node) {
250 Indent() << "for (";
251 if (Node->getInit()) {
252 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000253 PrintRawDeclStmt(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000254 else
255 PrintExpr(cast<Expr>(Node->getInit()));
256 }
Chris Lattner8bdcc472007-09-15 21:49:37 +0000257 OS << ";";
258 if (Node->getCond()) {
259 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000260 PrintExpr(Node->getCond());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000261 }
262 OS << ";";
263 if (Node->getInc()) {
264 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000265 PrintExpr(Node->getInc());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000266 }
267 OS << ") ";
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Chris Lattner8bdcc472007-09-15 21:49:37 +0000269 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
270 PrintRawCompoundStmt(CS);
271 OS << "\n";
272 } else {
273 OS << "\n";
274 PrintStmt(Node->getBody());
275 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000276}
277
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000278void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000279 Indent() << "for (";
280 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000281 PrintRawDeclStmt(DS);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000282 else
283 PrintExpr(cast<Expr>(Node->getElement()));
284 OS << " in ";
285 PrintExpr(Node->getCollection());
286 OS << ") ";
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000288 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
289 PrintRawCompoundStmt(CS);
290 OS << "\n";
291 } else {
292 OS << "\n";
293 PrintStmt(Node->getBody());
294 }
295}
296
Reid Spencer5f016e22007-07-11 17:01:13 +0000297void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
298 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
299}
300
301void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
302 Indent() << "goto *";
303 PrintExpr(Node->getTarget());
304 OS << ";\n";
305}
306
307void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
308 Indent() << "continue;\n";
309}
310
311void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
312 Indent() << "break;\n";
313}
314
315
316void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
317 Indent() << "return";
318 if (Node->getRetValue()) {
319 OS << " ";
320 PrintExpr(Node->getRetValue());
321 }
322 OS << ";\n";
323}
324
Chris Lattnerfe795952007-10-29 04:04:16 +0000325
326void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson39c47b52007-11-23 23:12:25 +0000327 Indent() << "asm ";
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Anders Carlsson39c47b52007-11-23 23:12:25 +0000329 if (Node->isVolatile())
330 OS << "volatile ";
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Anders Carlsson39c47b52007-11-23 23:12:25 +0000332 OS << "(";
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000333 VisitStringLiteral(Node->getAsmString());
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Anders Carlssonb235fc22007-11-22 01:36:19 +0000335 // Outputs
336 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
337 Node->getNumClobbers() != 0)
338 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Anders Carlssonb235fc22007-11-22 01:36:19 +0000340 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
341 if (i != 0)
342 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Anders Carlssonb235fc22007-11-22 01:36:19 +0000344 if (!Node->getOutputName(i).empty()) {
345 OS << '[';
346 OS << Node->getOutputName(i);
347 OS << "] ";
348 }
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Chris Lattnerb3277932009-03-10 04:59:06 +0000350 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000351 OS << " ";
352 Visit(Node->getOutputExpr(i));
353 }
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Anders Carlssonb235fc22007-11-22 01:36:19 +0000355 // Inputs
356 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
357 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Anders Carlssonb235fc22007-11-22 01:36:19 +0000359 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
360 if (i != 0)
361 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Anders Carlssonb235fc22007-11-22 01:36:19 +0000363 if (!Node->getInputName(i).empty()) {
364 OS << '[';
365 OS << Node->getInputName(i);
366 OS << "] ";
367 }
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Chris Lattnerb3277932009-03-10 04:59:06 +0000369 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000370 OS << " ";
371 Visit(Node->getInputExpr(i));
372 }
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Anders Carlssonb235fc22007-11-22 01:36:19 +0000374 // Clobbers
375 if (Node->getNumClobbers() != 0)
376 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Anders Carlssonb235fc22007-11-22 01:36:19 +0000378 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
379 if (i != 0)
380 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Anders Carlssonb235fc22007-11-22 01:36:19 +0000382 VisitStringLiteral(Node->getClobber(i));
383 }
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000385 OS << ");\n";
Chris Lattnerfe795952007-10-29 04:04:16 +0000386}
387
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000388void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000389 Indent() << "@try";
390 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
391 PrintRawCompoundStmt(TS);
392 OS << "\n";
393 }
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000395 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
396 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000397 Indent() << "@catch(";
Steve Naroff7ba138a2009-03-03 19:52:17 +0000398 if (catchStmt->getCatchParamDecl()) {
399 if (Decl *DS = catchStmt->getCatchParamDecl())
400 PrintRawDecl(DS);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000401 }
402 OS << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000403 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
404 PrintRawCompoundStmt(CS);
405 OS << "\n";
406 }
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000407 }
Mike Stump1eb44332009-09-09 15:08:12 +0000408
409 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
410 Node->getFinallyStmt())) {
Fariborz Jahanian1e7eab42007-11-07 00:46:42 +0000411 Indent() << "@finally";
412 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000413 OS << "\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000414 }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000415}
416
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000417void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000418}
419
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000420void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000421 Indent() << "@catch (...) { /* todo */ } \n";
422}
423
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000424void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000425 Indent() << "@throw";
426 if (Node->getThrowExpr()) {
427 OS << " ";
428 PrintExpr(Node->getThrowExpr());
429 }
430 OS << ";\n";
431}
432
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000433void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000434 Indent() << "@synchronized (";
435 PrintExpr(Node->getSynchExpr());
436 OS << ")";
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000437 PrintRawCompoundStmt(Node->getSynchBody());
438 OS << "\n";
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000439}
440
Sebastian Redl8351da02008-12-22 21:35:02 +0000441void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
442 OS << "catch (";
Sebastian Redl4b07b292008-12-22 19:15:10 +0000443 if (Decl *ExDecl = Node->getExceptionDecl())
444 PrintRawDecl(ExDecl);
445 else
446 OS << "...";
447 OS << ") ";
448 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl8351da02008-12-22 21:35:02 +0000449}
450
451void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
452 Indent();
453 PrintRawCXXCatchStmt(Node);
454 OS << "\n";
455}
456
457void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
458 Indent() << "try ";
459 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump1eb44332009-09-09 15:08:12 +0000460 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl8351da02008-12-22 21:35:02 +0000461 OS << " ";
462 PrintRawCXXCatchStmt(Node->getHandler(i));
463 }
Sebastian Redl4b07b292008-12-22 19:15:10 +0000464 OS << "\n";
465}
466
Reid Spencer5f016e22007-07-11 17:01:13 +0000467//===----------------------------------------------------------------------===//
468// Expr printing methods.
469//===----------------------------------------------------------------------===//
470
Reid Spencer5f016e22007-07-11 17:01:13 +0000471void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000472 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
473 Qualifier->print(OS, Policy);
Abramo Bagnara25777432010-08-11 22:01:17 +0000474 OS << Node->getNameInfo();
John McCall096832c2010-08-19 23:49:38 +0000475 if (Node->hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000476 OS << TemplateSpecializationType::PrintTemplateArgumentList(
477 Node->getTemplateArgs(),
478 Node->getNumTemplateArgs(),
Sean Huntc3021132010-05-05 15:23:54 +0000479 Policy);
Douglas Gregor1a49af92009-01-06 05:10:23 +0000480}
481
John McCall865d4472009-11-19 22:55:06 +0000482void StmtPrinter::VisitDependentScopeDeclRefExpr(
483 DependentScopeDeclRefExpr *Node) {
Axel Naumann274f83c2011-01-24 15:44:00 +0000484 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
485 Qualifier->print(OS, Policy);
Abramo Bagnara25777432010-08-11 22:01:17 +0000486 OS << Node->getNameInfo();
John McCallf7a1a742009-11-24 19:00:30 +0000487 if (Node->hasExplicitTemplateArgs())
488 OS << TemplateSpecializationType::PrintTemplateArgumentList(
489 Node->getTemplateArgs(),
490 Node->getNumTemplateArgs(),
491 Policy);
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000492}
493
John McCallba135432009-11-21 08:51:07 +0000494void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000495 if (Node->getQualifier())
496 Node->getQualifier()->print(OS, Policy);
Abramo Bagnara25777432010-08-11 22:01:17 +0000497 OS << Node->getNameInfo();
John McCallf7a1a742009-11-24 19:00:30 +0000498 if (Node->hasExplicitTemplateArgs())
499 OS << TemplateSpecializationType::PrintTemplateArgumentList(
500 Node->getTemplateArgs(),
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000501 Node->getNumTemplateArgs(),
John McCallf7a1a742009-11-24 19:00:30 +0000502 Policy);
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000503}
504
Steve Naroff7779db42007-11-12 14:29:37 +0000505void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000506 if (Node->getBase()) {
507 PrintExpr(Node->getBase());
508 OS << (Node->isArrow() ? "->" : ".");
509 }
Benjamin Kramer900fc632010-04-17 09:33:03 +0000510 OS << Node->getDecl();
Steve Naroff7779db42007-11-12 14:29:37 +0000511}
512
Steve Naroffae784072008-05-30 00:40:33 +0000513void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000514 if (Node->isSuperReceiver())
515 OS << "super.";
516 else if (Node->getBase()) {
Steve Naroffae784072008-05-30 00:40:33 +0000517 PrintExpr(Node->getBase());
518 OS << ".";
519 }
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000520
John McCall12f78a62010-12-02 01:19:52 +0000521 if (Node->isImplicitProperty())
522 OS << Node->getImplicitPropertyGetter()->getSelector().getAsString();
523 else
524 OS << Node->getExplicitProperty()->getName();
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000525}
526
Chris Lattnerd9f69102008-08-10 01:53:14 +0000527void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson22742662007-07-21 05:21:51 +0000528 switch (Node->getIdentType()) {
529 default:
530 assert(0 && "unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000531 case PredefinedExpr::Func:
Anders Carlsson22742662007-07-21 05:21:51 +0000532 OS << "__func__";
533 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000534 case PredefinedExpr::Function:
Anders Carlsson22742662007-07-21 05:21:51 +0000535 OS << "__FUNCTION__";
536 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000537 case PredefinedExpr::PrettyFunction:
Anders Carlsson22742662007-07-21 05:21:51 +0000538 OS << "__PRETTY_FUNCTION__";
539 break;
540 }
541}
542
Reid Spencer5f016e22007-07-11 17:01:13 +0000543void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000544 unsigned value = Node->getValue();
Chris Lattnerc250aae2008-06-07 22:35:38 +0000545 if (Node->isWide())
546 OS << "L";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000547 switch (value) {
548 case '\\':
549 OS << "'\\\\'";
550 break;
551 case '\'':
552 OS << "'\\''";
553 break;
554 case '\a':
555 // TODO: K&R: the meaning of '\\a' is different in traditional C
556 OS << "'\\a'";
557 break;
558 case '\b':
559 OS << "'\\b'";
560 break;
561 // Nonstandard escape sequence.
562 /*case '\e':
563 OS << "'\\e'";
564 break;*/
565 case '\f':
566 OS << "'\\f'";
567 break;
568 case '\n':
569 OS << "'\\n'";
570 break;
571 case '\r':
572 OS << "'\\r'";
573 break;
574 case '\t':
575 OS << "'\\t'";
576 break;
577 case '\v':
578 OS << "'\\v'";
579 break;
580 default:
Ted Kremenek471733d2008-02-23 00:52:04 +0000581 if (value < 256 && isprint(value)) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000582 OS << "'" << (char)value << "'";
583 } else if (value < 256) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000584 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000585 } else {
586 // FIXME what to really do here?
587 OS << value;
588 }
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000589 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000590}
591
592void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
593 bool isSigned = Node->getType()->isSignedIntegerType();
594 OS << Node->getValue().toString(10, isSigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000595
Reid Spencer5f016e22007-07-11 17:01:13 +0000596 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall183700f2009-09-21 23:43:11 +0000597 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000598 default: assert(0 && "Unexpected type for integer literal!");
599 case BuiltinType::Int: break; // no suffix.
600 case BuiltinType::UInt: OS << 'U'; break;
601 case BuiltinType::Long: OS << 'L'; break;
602 case BuiltinType::ULong: OS << "UL"; break;
603 case BuiltinType::LongLong: OS << "LL"; break;
604 case BuiltinType::ULongLong: OS << "ULL"; break;
605 }
606}
607void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Chris Lattner86e499d2007-08-01 00:23:58 +0000608 // FIXME: print value more precisely.
Chris Lattnerda8249e2008-06-07 22:13:43 +0000609 OS << Node->getValueAsApproximateDouble();
Reid Spencer5f016e22007-07-11 17:01:13 +0000610}
Chris Lattner5d661452007-08-26 03:42:43 +0000611
612void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
613 PrintExpr(Node->getSubExpr());
614 OS << "i";
615}
616
Reid Spencer5f016e22007-07-11 17:01:13 +0000617void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
618 if (Str->isWide()) OS << 'L';
619 OS << '"';
Anders Carlssonee98ac52007-10-15 02:50:23 +0000620
Reid Spencer5f016e22007-07-11 17:01:13 +0000621 // FIXME: this doesn't print wstrings right.
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +0000622 llvm::StringRef StrData = Str->getString();
623 for (llvm::StringRef::iterator I = StrData.begin(), E = StrData.end();
624 I != E; ++I) {
625 unsigned char Char = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000626
Chris Lattner9a81c872009-01-16 19:25:18 +0000627 switch (Char) {
628 default:
629 if (isprint(Char))
630 OS << (char)Char;
631 else // Output anything hard as an octal escape.
632 OS << '\\'
633 << (char)('0'+ ((Char >> 6) & 7))
634 << (char)('0'+ ((Char >> 3) & 7))
635 << (char)('0'+ ((Char >> 0) & 7));
636 break;
637 // Handle some common non-printable cases to make dumps prettier.
Reid Spencer5f016e22007-07-11 17:01:13 +0000638 case '\\': OS << "\\\\"; break;
639 case '"': OS << "\\\""; break;
640 case '\n': OS << "\\n"; break;
641 case '\t': OS << "\\t"; break;
642 case '\a': OS << "\\a"; break;
643 case '\b': OS << "\\b"; break;
644 }
645 }
646 OS << '"';
647}
648void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
649 OS << "(";
650 PrintExpr(Node->getSubExpr());
651 OS << ")";
652}
653void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +0000654 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000655 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Eli Friedman7df71ac2009-06-14 22:39:26 +0000657 // Print a space if this is an "identifier operator" like __real, or if
658 // it might be concatenated incorrectly like '+'.
Chris Lattner296bf192007-08-23 21:46:40 +0000659 switch (Node->getOpcode()) {
660 default: break;
John McCall2de56d12010-08-25 11:45:40 +0000661 case UO_Real:
662 case UO_Imag:
663 case UO_Extension:
Chris Lattner296bf192007-08-23 21:46:40 +0000664 OS << ' ';
665 break;
John McCall2de56d12010-08-25 11:45:40 +0000666 case UO_Plus:
667 case UO_Minus:
Eli Friedman7df71ac2009-06-14 22:39:26 +0000668 if (isa<UnaryOperator>(Node->getSubExpr()))
669 OS << ' ';
670 break;
Chris Lattner296bf192007-08-23 21:46:40 +0000671 }
672 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000673 PrintExpr(Node->getSubExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 if (Node->isPostfix())
676 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +0000677}
Chris Lattner704fe352007-08-30 17:59:59 +0000678
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000679void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
680 OS << "__builtin_offsetof(";
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000681 OS << Node->getTypeSourceInfo()->getType().getAsString(Policy) << ", ";
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000682 bool PrintedSomething = false;
683 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
684 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
685 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
686 // Array node
687 OS << "[";
688 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
689 OS << "]";
690 PrintedSomething = true;
691 continue;
692 }
Douglas Gregorcc8a5d52010-04-29 00:18:15 +0000693
694 // Skip implicit base indirections.
695 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
696 continue;
697
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000698 // Field or identifier node.
699 IdentifierInfo *Id = ON.getFieldName();
700 if (!Id)
701 continue;
Sean Huntc3021132010-05-05 15:23:54 +0000702
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000703 if (PrintedSomething)
704 OS << ".";
705 else
706 PrintedSomething = true;
Sean Huntc3021132010-05-05 15:23:54 +0000707 OS << Id->getName();
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000708 }
709 OS << ")";
710}
711
Sebastian Redl05189992008-11-11 17:56:53 +0000712void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
713 OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
714 if (Node->isArgumentType())
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000715 OS << "(" << Node->getArgumentType().getAsString(Policy) << ")";
Sebastian Redl05189992008-11-11 17:56:53 +0000716 else {
717 OS << " ";
718 PrintExpr(Node->getArgumentExpr());
719 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000720}
721void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +0000722 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000723 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +0000724 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000725 OS << "]";
726}
727
728void StmtPrinter::VisitCallExpr(CallExpr *Call) {
729 PrintExpr(Call->getCallee());
730 OS << "(";
731 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattner04421082008-04-08 04:40:51 +0000732 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
733 // Don't print any defaulted arguments
734 break;
735 }
736
Reid Spencer5f016e22007-07-11 17:01:13 +0000737 if (i) OS << ", ";
738 PrintExpr(Call->getArg(i));
739 }
740 OS << ")";
741}
742void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregorb3eef682009-01-08 22:45:41 +0000743 // FIXME: Suppress printing implicit bases (like "this")
744 PrintExpr(Node->getBase());
Fariborz Jahanian97fd83a2010-01-11 21:17:32 +0000745 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
746 if (FD->isAnonymousStructOrUnion())
747 return;
Douglas Gregorb3eef682009-01-08 22:45:41 +0000748 OS << (Node->isArrow() ? "->" : ".");
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000749 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
750 Qualifier->print(OS, Policy);
751
Abramo Bagnara25777432010-08-11 22:01:17 +0000752 OS << Node->getMemberNameInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000753
John McCall096832c2010-08-19 23:49:38 +0000754 if (Node->hasExplicitTemplateArgs())
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000755 OS << TemplateSpecializationType::PrintTemplateArgumentList(
756 Node->getTemplateArgs(),
757 Node->getNumTemplateArgs(),
758 Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000759}
Steve Narofff242b1b2009-07-24 17:54:45 +0000760void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
761 PrintExpr(Node->getBase());
762 OS << (Node->isArrow() ? "->isa" : ".isa");
763}
764
Nate Begeman213541a2008-04-18 23:10:10 +0000765void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +0000766 PrintExpr(Node->getBase());
767 OS << ".";
768 OS << Node->getAccessor().getName();
769}
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000770void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Fariborz Jahanian03e80e42010-06-30 16:31:08 +0000771 OS << "(" << Node->getType().getAsString(Policy) << ")";
Reid Spencer5f016e22007-07-11 17:01:13 +0000772 PrintExpr(Node->getSubExpr());
773}
Steve Naroffaff1edd2007-07-19 21:32:11 +0000774void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000775 OS << "(" << Node->getType().getAsString(Policy) << ")";
Steve Naroffaff1edd2007-07-19 21:32:11 +0000776 PrintExpr(Node->getInitializer());
777}
Steve Naroff49b45262007-07-13 16:58:59 +0000778void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroff90045e82007-07-13 23:32:42 +0000779 // No need to print anything, simply forward to the sub expression.
780 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +0000781}
Reid Spencer5f016e22007-07-11 17:01:13 +0000782void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
783 PrintExpr(Node->getLHS());
784 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
785 PrintExpr(Node->getRHS());
786}
Chris Lattnereb14fe82007-08-25 02:00:02 +0000787void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
788 PrintExpr(Node->getLHS());
789 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
790 PrintExpr(Node->getRHS());
791}
Reid Spencer5f016e22007-07-11 17:01:13 +0000792void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
793 PrintExpr(Node->getCond());
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Ted Kremenek8e911c42007-11-26 18:27:54 +0000795 if (Node->getLHS()) {
796 OS << " ? ";
797 PrintExpr(Node->getLHS());
798 OS << " : ";
799 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000800 else { // Handle GCC extension where LHS can be NULL.
Ted Kremenek8e911c42007-11-26 18:27:54 +0000801 OS << " ?: ";
802 }
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Reid Spencer5f016e22007-07-11 17:01:13 +0000804 PrintExpr(Node->getRHS());
805}
806
807// GNU extensions.
808
Chris Lattner6481a572007-08-03 17:31:20 +0000809void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000810 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000811}
812
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000813void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
814 OS << "(";
815 PrintRawCompoundStmt(E->getSubStmt());
816 OS << ")";
817}
818
Steve Naroffd04fdd52007-08-03 21:21:27 +0000819void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
820 OS << "__builtin_choose_expr(";
821 PrintExpr(Node->getCond());
Chris Lattner94f05e32007-08-04 00:20:15 +0000822 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000823 PrintExpr(Node->getLHS());
Chris Lattner94f05e32007-08-04 00:20:15 +0000824 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000825 PrintExpr(Node->getRHS());
826 OS << ")";
827}
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000828
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000829void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
830 OS << "__null";
831}
832
Eli Friedmand38617c2008-05-14 19:38:39 +0000833void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
834 OS << "__builtin_shufflevector(";
835 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
836 if (i) OS << ", ";
837 PrintExpr(Node->getExpr(i));
838 }
839 OS << ")";
840}
841
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000842void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000843 if (Node->getSyntacticForm()) {
844 Visit(Node->getSyntacticForm());
845 return;
846 }
847
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000848 OS << "{ ";
849 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
850 if (i) OS << ", ";
Douglas Gregor4c678342009-01-28 21:54:33 +0000851 if (Node->getInit(i))
852 PrintExpr(Node->getInit(i));
853 else
854 OS << "0";
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000855 }
856 OS << " }";
857}
858
Nate Begeman2ef13e52009-08-10 23:49:36 +0000859void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
860 OS << "( ";
861 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
862 if (i) OS << ", ";
863 PrintExpr(Node->getExpr(i));
864 }
865 OS << " )";
866}
867
Douglas Gregor05c13a32009-01-22 00:58:24 +0000868void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000869 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
870 DEnd = Node->designators_end();
871 D != DEnd; ++D) {
872 if (D->isFieldDesignator()) {
873 if (D->getDotLoc().isInvalid())
874 OS << D->getFieldName()->getName() << ":";
875 else
876 OS << "." << D->getFieldName()->getName();
877 } else {
878 OS << "[";
879 if (D->isArrayDesignator()) {
880 PrintExpr(Node->getArrayIndex(*D));
881 } else {
882 PrintExpr(Node->getArrayRangeStart(*D));
883 OS << " ... ";
Mike Stump1eb44332009-09-09 15:08:12 +0000884 PrintExpr(Node->getArrayRangeEnd(*D));
Douglas Gregor4c678342009-01-28 21:54:33 +0000885 }
886 OS << "]";
887 }
888 }
889
890 OS << " = ";
891 PrintExpr(Node->getInit());
Douglas Gregor05c13a32009-01-22 00:58:24 +0000892}
893
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000894void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Chris Lattnere4f21422009-06-30 01:26:17 +0000895 if (Policy.LangOpts.CPlusPlus)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000896 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()";
897 else {
898 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")";
899 if (Node->getType()->isRecordType())
900 OS << "{}";
901 else
902 OS << 0;
903 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000904}
905
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000906void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000907 OS << "__builtin_va_arg(";
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000908 PrintExpr(Node->getSubExpr());
909 OS << ", ";
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000910 OS << Node->getType().getAsString(Policy);
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000911 OS << ")";
912}
913
Reid Spencer5f016e22007-07-11 17:01:13 +0000914// C++
Douglas Gregorb4609802008-11-14 16:09:21 +0000915void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
916 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
917 "",
918#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
919 Spelling,
920#include "clang/Basic/OperatorKinds.def"
921 };
922
923 OverloadedOperatorKind Kind = Node->getOperator();
924 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
925 if (Node->getNumArgs() == 1) {
926 OS << OpStrings[Kind] << ' ';
927 PrintExpr(Node->getArg(0));
928 } else {
929 PrintExpr(Node->getArg(0));
930 OS << ' ' << OpStrings[Kind];
931 }
932 } else if (Kind == OO_Call) {
933 PrintExpr(Node->getArg(0));
934 OS << '(';
935 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
936 if (ArgIdx > 1)
937 OS << ", ";
938 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
939 PrintExpr(Node->getArg(ArgIdx));
940 }
941 OS << ')';
942 } else if (Kind == OO_Subscript) {
943 PrintExpr(Node->getArg(0));
944 OS << '[';
945 PrintExpr(Node->getArg(1));
946 OS << ']';
947 } else if (Node->getNumArgs() == 1) {
948 OS << OpStrings[Kind] << ' ';
949 PrintExpr(Node->getArg(0));
950 } else if (Node->getNumArgs() == 2) {
951 PrintExpr(Node->getArg(0));
952 OS << ' ' << OpStrings[Kind] << ' ';
953 PrintExpr(Node->getArg(1));
954 } else {
955 assert(false && "unknown overloaded operator");
956 }
957}
Reid Spencer5f016e22007-07-11 17:01:13 +0000958
Douglas Gregor88a35142008-12-22 05:46:06 +0000959void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
960 VisitCallExpr(cast<CallExpr>(Node));
961}
962
Douglas Gregor49badde2008-10-27 19:41:14 +0000963void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
964 OS << Node->getCastName() << '<';
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000965 OS << Node->getTypeAsWritten().getAsString(Policy) << ">(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000966 PrintExpr(Node->getSubExpr());
967 OS << ")";
968}
969
Douglas Gregor49badde2008-10-27 19:41:14 +0000970void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
971 VisitCXXNamedCastExpr(Node);
972}
973
974void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
975 VisitCXXNamedCastExpr(Node);
976}
977
978void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
979 VisitCXXNamedCastExpr(Node);
980}
981
982void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
983 VisitCXXNamedCastExpr(Node);
984}
985
Sebastian Redlc42e1182008-11-11 11:37:55 +0000986void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
987 OS << "typeid(";
988 if (Node->isTypeOperand()) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000989 OS << Node->getTypeOperand().getAsString(Policy);
Sebastian Redlc42e1182008-11-11 11:37:55 +0000990 } else {
991 PrintExpr(Node->getExprOperand());
992 }
993 OS << ")";
994}
995
Francois Pichet01b7c302010-09-08 12:20:18 +0000996void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
997 OS << "__uuidof(";
998 if (Node->isTypeOperand()) {
999 OS << Node->getTypeOperand().getAsString(Policy);
1000 } else {
1001 PrintExpr(Node->getExprOperand());
1002 }
1003 OS << ")";
1004}
1005
Reid Spencer5f016e22007-07-11 17:01:13 +00001006void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1007 OS << (Node->getValue() ? "true" : "false");
1008}
1009
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001010void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1011 OS << "nullptr";
1012}
1013
Douglas Gregor796da182008-11-04 14:32:21 +00001014void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1015 OS << "this";
1016}
1017
Chris Lattner50dd2892008-02-26 00:51:44 +00001018void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1019 if (Node->getSubExpr() == 0)
1020 OS << "throw";
1021 else {
1022 OS << "throw ";
1023 PrintExpr(Node->getSubExpr());
1024 }
1025}
1026
Chris Lattner04421082008-04-08 04:40:51 +00001027void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1028 // Nothing to print: we picked up the default argument
1029}
1030
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001031void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001032 OS << Node->getType().getAsString(Policy);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001033 OS << "(";
1034 PrintExpr(Node->getSubExpr());
1035 OS << ")";
1036}
1037
Anders Carlssonfceb0a82009-05-30 20:03:25 +00001038void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1039 PrintExpr(Node->getSubExpr());
1040}
1041
Douglas Gregor506ae412009-01-16 18:33:17 +00001042void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001043 OS << Node->getType().getAsString(Policy);
Douglas Gregor506ae412009-01-16 18:33:17 +00001044 OS << "(";
1045 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001046 ArgEnd = Node->arg_end();
Douglas Gregor506ae412009-01-16 18:33:17 +00001047 Arg != ArgEnd; ++Arg) {
1048 if (Arg != Node->arg_begin())
1049 OS << ", ";
1050 PrintExpr(*Arg);
1051 }
1052 OS << ")";
1053}
1054
Douglas Gregored8abf12010-07-08 06:14:04 +00001055void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregorab6677e2010-09-08 00:15:04 +00001056 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
1057 OS << TSInfo->getType().getAsString(Policy) << "()";
1058 else
1059 OS << Node->getType().getAsString(Policy) << "()";
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001060}
1061
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001062void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1063 if (E->isGlobalNew())
1064 OS << "::";
1065 OS << "new ";
1066 unsigned NumPlace = E->getNumPlacementArgs();
1067 if (NumPlace > 0) {
1068 OS << "(";
1069 PrintExpr(E->getPlacementArg(0));
1070 for (unsigned i = 1; i < NumPlace; ++i) {
1071 OS << ", ";
1072 PrintExpr(E->getPlacementArg(i));
1073 }
1074 OS << ") ";
1075 }
1076 if (E->isParenTypeId())
1077 OS << "(";
Sebastian Redl6fec6482008-12-02 22:08:59 +00001078 std::string TypeS;
1079 if (Expr *Size = E->getArraySize()) {
1080 llvm::raw_string_ostream s(TypeS);
Eli Friedman6e1a3452009-05-30 05:32:46 +00001081 Size->printPretty(s, Context, Helper, Policy);
Sebastian Redl6fec6482008-12-02 22:08:59 +00001082 s.flush();
1083 TypeS = "[" + TypeS + "]";
1084 }
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001085 E->getAllocatedType().getAsStringInternal(TypeS, Policy);
Sebastian Redl6fec6482008-12-02 22:08:59 +00001086 OS << TypeS;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001087 if (E->isParenTypeId())
1088 OS << ")";
1089
1090 if (E->hasInitializer()) {
1091 OS << "(";
1092 unsigned NumCons = E->getNumConstructorArgs();
1093 if (NumCons > 0) {
1094 PrintExpr(E->getConstructorArg(0));
1095 for (unsigned i = 1; i < NumCons; ++i) {
1096 OS << ", ";
1097 PrintExpr(E->getConstructorArg(i));
1098 }
1099 }
1100 OS << ")";
1101 }
1102}
1103
1104void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1105 if (E->isGlobalDelete())
1106 OS << "::";
1107 OS << "delete ";
1108 if (E->isArrayForm())
1109 OS << "[] ";
1110 PrintExpr(E->getArgument());
1111}
1112
Douglas Gregora71d8192009-09-04 17:36:40 +00001113void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1114 PrintExpr(E->getBase());
1115 if (E->isArrow())
1116 OS << "->";
1117 else
1118 OS << '.';
1119 if (E->getQualifier())
1120 E->getQualifier()->print(OS, Policy);
Mike Stump1eb44332009-09-09 15:08:12 +00001121
Douglas Gregora71d8192009-09-04 17:36:40 +00001122 std::string TypeS;
Douglas Gregora2e7dd22010-02-25 01:56:36 +00001123 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1124 OS << II->getName();
1125 else
1126 E->getDestroyedType().getAsStringInternal(TypeS, Policy);
Douglas Gregora71d8192009-09-04 17:36:40 +00001127 OS << TypeS;
1128}
1129
Anders Carlssone349bea2009-04-23 02:32:43 +00001130void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Douglas Gregord0fb3ad2011-01-24 17:25:03 +00001131 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1132 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1133 // Don't print any defaulted arguments
1134 break;
1135 }
1136
1137 if (i) OS << ", ";
1138 PrintExpr(E->getArg(i));
Fariborz Jahanianc75da512010-01-13 21:41:11 +00001139 }
Anders Carlssone349bea2009-04-23 02:32:43 +00001140}
1141
John McCall4765fa02010-12-06 08:20:24 +00001142void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Anders Carlsson02bbfa32009-04-24 22:47:04 +00001143 // Just forward to the sub expression.
1144 PrintExpr(E->getSubExpr());
1145}
1146
Mike Stump1eb44332009-09-09 15:08:12 +00001147void
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001148StmtPrinter::VisitCXXUnresolvedConstructExpr(
1149 CXXUnresolvedConstructExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001150 OS << Node->getTypeAsWritten().getAsString(Policy);
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001151 OS << "(";
1152 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001153 ArgEnd = Node->arg_end();
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001154 Arg != ArgEnd; ++Arg) {
1155 if (Arg != Node->arg_begin())
1156 OS << ", ";
1157 PrintExpr(*Arg);
1158 }
1159 OS << ")";
1160}
1161
John McCall865d4472009-11-19 22:55:06 +00001162void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1163 CXXDependentScopeMemberExpr *Node) {
John McCallaa81e162009-12-01 22:10:20 +00001164 if (!Node->isImplicitAccess()) {
1165 PrintExpr(Node->getBase());
1166 OS << (Node->isArrow() ? "->" : ".");
1167 }
Douglas Gregora38c6872009-09-03 16:14:30 +00001168 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1169 Qualifier->print(OS, Policy);
John McCallaa81e162009-12-01 22:10:20 +00001170 else if (Node->hasExplicitTemplateArgs())
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001171 // FIXME: Track use of "template" keyword explicitly?
1172 OS << "template ";
Mike Stump1eb44332009-09-09 15:08:12 +00001173
Abramo Bagnara25777432010-08-11 22:01:17 +00001174 OS << Node->getMemberNameInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00001175
John McCallaa81e162009-12-01 22:10:20 +00001176 if (Node->hasExplicitTemplateArgs()) {
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001177 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1178 Node->getTemplateArgs(),
1179 Node->getNumTemplateArgs(),
1180 Policy);
1181 }
Douglas Gregor1c0ca592009-05-22 21:13:27 +00001182}
1183
John McCall129e2df2009-11-30 22:42:35 +00001184void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCallaa81e162009-12-01 22:10:20 +00001185 if (!Node->isImplicitAccess()) {
1186 PrintExpr(Node->getBase());
1187 OS << (Node->isArrow() ? "->" : ".");
1188 }
John McCall129e2df2009-11-30 22:42:35 +00001189 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1190 Qualifier->print(OS, Policy);
1191
1192 // FIXME: this might originally have been written with 'template'
1193
Abramo Bagnara25777432010-08-11 22:01:17 +00001194 OS << Node->getMemberNameInfo();
John McCall129e2df2009-11-30 22:42:35 +00001195
1196 if (Node->hasExplicitTemplateArgs()) {
1197 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1198 Node->getTemplateArgs(),
1199 Node->getNumTemplateArgs(),
1200 Policy);
1201 }
1202}
1203
Sebastian Redl64b45f72009-01-05 20:52:13 +00001204static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1205 switch (UTT) {
Francois Pichet38c2b732010-12-07 00:55:57 +00001206 default: llvm_unreachable("Unknown unary type trait");
Sebastian Redl64b45f72009-01-05 20:52:13 +00001207 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
1208 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
1209 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
1210 case UTT_HasTrivialAssign: return "__has_trivial_assign";
1211 case UTT_HasTrivialCopy: return "__has_trivial_copy";
1212 case UTT_HasTrivialConstructor: return "__has_trivial_constructor";
1213 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1214 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1215 case UTT_IsAbstract: return "__is_abstract";
1216 case UTT_IsClass: return "__is_class";
1217 case UTT_IsEmpty: return "__is_empty";
1218 case UTT_IsEnum: return "__is_enum";
1219 case UTT_IsPOD: return "__is_pod";
1220 case UTT_IsPolymorphic: return "__is_polymorphic";
1221 case UTT_IsUnion: return "__is_union";
1222 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00001223 return "";
1224}
1225
1226static const char *getTypeTraitName(BinaryTypeTrait BTT) {
1227 switch (BTT) {
Francois Pichet38c2b732010-12-07 00:55:57 +00001228 default: llvm_unreachable("Unknown binary type trait");
Francois Pichetf1872372010-12-08 22:35:30 +00001229 case BTT_IsBaseOf: return "__is_base_of";
1230 case BTT_TypeCompatible: return "__builtin_types_compatible_p";
Francois Pichet6ad6f282010-12-07 00:08:36 +00001231 }
Francois Pichet6ad6f282010-12-07 00:08:36 +00001232 return "";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001233}
1234
1235void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1236 OS << getTypeTraitName(E->getTrait()) << "("
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001237 << E->getQueriedType().getAsString(Policy) << ")";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001238}
1239
Francois Pichet6ad6f282010-12-07 00:08:36 +00001240void StmtPrinter::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
1241 OS << getTypeTraitName(E->getTrait()) << "("
1242 << E->getLhsType().getAsString(Policy) << ","
1243 << E->getRhsType().getAsString(Policy) << ")";
1244}
1245
Sebastian Redl2e156222010-09-10 20:55:43 +00001246void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1247 OS << "noexcept(";
1248 PrintExpr(E->getOperand());
1249 OS << ")";
1250}
1251
Douglas Gregoree8aff02011-01-04 17:33:58 +00001252void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregorbe230c32011-01-03 17:17:50 +00001253 PrintExpr(E->getPattern());
1254 OS << "...";
1255}
1256
Douglas Gregoree8aff02011-01-04 17:33:58 +00001257void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
1258 OS << "sizeof...(" << E->getPack()->getNameAsString() << ")";
1259}
1260
Douglas Gregorc7793c72011-01-15 01:15:58 +00001261void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
1262 SubstNonTypeTemplateParmPackExpr *Node) {
1263 OS << Node->getParameterPack()->getNameAsString();
1264}
1265
Mike Stump1eb44332009-09-09 15:08:12 +00001266// Obj-C
Anders Carlsson55085182007-08-21 17:43:55 +00001267
1268void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1269 OS << "@";
1270 VisitStringLiteral(Node->getString());
1271}
Reid Spencer5f016e22007-07-11 17:01:13 +00001272
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001273void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001274 OS << "@encode(" << Node->getEncodedType().getAsString(Policy) << ')';
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001275}
1276
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001277void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001278 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001279}
1280
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001281void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramer900fc632010-04-17 09:33:03 +00001282 OS << "@protocol(" << Node->getProtocol() << ')';
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001283}
1284
Steve Naroff563477d2007-09-18 23:55:05 +00001285void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1286 OS << "[";
Douglas Gregor04badcf2010-04-21 00:45:42 +00001287 switch (Mess->getReceiverKind()) {
1288 case ObjCMessageExpr::Instance:
1289 PrintExpr(Mess->getInstanceReceiver());
1290 break;
1291
1292 case ObjCMessageExpr::Class:
1293 OS << Mess->getClassReceiver().getAsString(Policy);
1294 break;
1295
1296 case ObjCMessageExpr::SuperInstance:
1297 case ObjCMessageExpr::SuperClass:
1298 OS << "Super";
1299 break;
1300 }
1301
Ted Kremenekc29efd82008-05-02 17:32:38 +00001302 OS << ' ';
Ted Kremenek97b7f262008-04-16 04:30:16 +00001303 Selector selector = Mess->getSelector();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001304 if (selector.isUnarySelector()) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001305 OS << selector.getIdentifierInfoForSlot(0)->getName();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001306 } else {
1307 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001308 if (i < selector.getNumArgs()) {
1309 if (i > 0) OS << ' ';
1310 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner39f34e92008-11-24 04:00:27 +00001311 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremenekc29efd82008-05-02 17:32:38 +00001312 else
1313 OS << ":";
1314 }
1315 else OS << ", "; // Handle variadic methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001316
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001317 PrintExpr(Mess->getArg(i));
1318 }
Steve Naroff563477d2007-09-18 23:55:05 +00001319 }
1320 OS << "]";
1321}
1322
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001323
Steve Naroff4eb206b2008-09-03 18:15:37 +00001324void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001325 BlockDecl *BD = Node->getBlockDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001326 OS << "^";
Mike Stump1eb44332009-09-09 15:08:12 +00001327
Steve Naroff4eb206b2008-09-03 18:15:37 +00001328 const FunctionType *AFT = Node->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +00001329
Douglas Gregor72564e72009-02-26 23:50:07 +00001330 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001331 OS << "()";
Douglas Gregor72564e72009-02-26 23:50:07 +00001332 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001333 OS << '(';
1334 std::string ParamStr;
Steve Naroff56ee6892008-10-08 17:01:13 +00001335 for (BlockDecl::param_iterator AI = BD->param_begin(),
1336 E = BD->param_end(); AI != E; ++AI) {
1337 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner39f34e92008-11-24 04:00:27 +00001338 ParamStr = (*AI)->getNameAsString();
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001339 (*AI)->getType().getAsStringInternal(ParamStr, Policy);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001340 OS << ParamStr;
1341 }
Mike Stump1eb44332009-09-09 15:08:12 +00001342
Douglas Gregor72564e72009-02-26 23:50:07 +00001343 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001344 if (FT->isVariadic()) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001345 if (!BD->param_empty()) OS << ", ";
Steve Naroff4eb206b2008-09-03 18:15:37 +00001346 OS << "...";
1347 }
1348 OS << ')';
1349 }
1350}
1351
Steve Naroff4eb206b2008-09-03 18:15:37 +00001352void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Benjamin Kramer900fc632010-04-17 09:33:03 +00001353 OS << Node->getDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001354}
John McCall7cd7d1a2010-11-15 23:31:06 +00001355
1356void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {}
1357
Reid Spencer5f016e22007-07-11 17:01:13 +00001358//===----------------------------------------------------------------------===//
1359// Stmt method implementations
1360//===----------------------------------------------------------------------===//
1361
Eli Friedman48d14a22009-05-30 05:03:24 +00001362void Stmt::dumpPretty(ASTContext& Context) const {
Argyrios Kyrtzidis96680332010-08-09 10:54:31 +00001363 printPretty(llvm::errs(), Context, 0,
Chris Lattnere4f21422009-06-30 01:26:17 +00001364 PrintingPolicy(Context.getLangOptions()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001365}
1366
Eli Friedman48d14a22009-05-30 05:03:24 +00001367void Stmt::printPretty(llvm::raw_ostream &OS, ASTContext& Context,
1368 PrinterHelper* Helper,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001369 const PrintingPolicy &Policy,
1370 unsigned Indentation) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001371 if (this == 0) {
1372 OS << "<NULL>";
1373 return;
1374 }
1375
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001376 if (Policy.Dump && &Context) {
Argyrios Kyrtzidis96680332010-08-09 10:54:31 +00001377 dump(OS, Context.getSourceManager());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001378 return;
1379 }
Mike Stump1eb44332009-09-09 15:08:12 +00001380
Eli Friedman48d14a22009-05-30 05:03:24 +00001381 StmtPrinter P(OS, Context, Helper, Policy, Indentation);
Chris Lattnerc5598cb2007-08-21 04:04:25 +00001382 P.Visit(const_cast<Stmt*>(this));
Reid Spencer5f016e22007-07-11 17:01:13 +00001383}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001384
1385//===----------------------------------------------------------------------===//
1386// PrinterHelper
1387//===----------------------------------------------------------------------===//
1388
1389// Implement virtual destructor.
Gabor Greif84675832007-09-11 15:32:40 +00001390PrinterHelper::~PrinterHelper() {}