blob: 892442ea3bff9475e2a2f73e4e2804e74b88ad86 [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
Benjamin Kramer471c8b42012-07-04 20:19:54 +000015#include "clang/AST/ASTContext.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/AST/StmtVisitor.h"
Douglas Gregor1a49af92009-01-06 05:10:23 +000017#include "clang/AST/DeclCXX.h"
Ted Kremenek91d1d7a2007-10-17 18:36:42 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregorc7793c72011-01-15 01:15:58 +000019#include "clang/AST/DeclTemplate.h"
Ted Kremenek42a509f2007-08-31 21:30:12 +000020#include "clang/AST/PrettyPrinter.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"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000023#include "llvm/ADT/SmallString.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// StmtPrinter Visitor
28//===----------------------------------------------------------------------===//
29
30namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +000031 class StmtPrinter : public StmtVisitor<StmtPrinter> {
Chris Lattner5f9e2722011-07-23 10:55:15 +000032 raw_ostream &OS;
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:
Richard Smithd1420c62012-08-16 03:56:14 +000038 StmtPrinter(raw_ostream &os, PrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +000039 const PrintingPolicy &Policy,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000040 unsigned Indentation = 0)
Richard Smithd1420c62012-08-16 03:56:14 +000041 : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy) {}
Mike Stump1eb44332009-09-09 15:08:12 +000042
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000043 void PrintStmt(Stmt *S) {
44 PrintStmt(S, Policy.Indentation);
45 }
46
47 void PrintStmt(Stmt *S, int SubIndent) {
Reid Spencer5f016e22007-07-11 17:01:13 +000048 IndentLevel += SubIndent;
49 if (S && isa<Expr>(S)) {
50 // If this is an expr used in a stmt context, indent and newline it.
51 Indent();
Chris Lattnerc5598cb2007-08-21 04:04:25 +000052 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000053 OS << ";\n";
54 } else if (S) {
Chris Lattnerc5598cb2007-08-21 04:04:25 +000055 Visit(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000056 } else {
57 Indent() << "<<<NULL STATEMENT>>>\n";
58 }
59 IndentLevel -= SubIndent;
60 }
Eli Friedmandb23b152009-05-30 00:19:54 +000061
Reid Spencer5f016e22007-07-11 17:01:13 +000062 void PrintRawCompoundStmt(CompoundStmt *S);
63 void PrintRawDecl(Decl *D);
Eli Friedman915c07d2012-10-16 23:45:15 +000064 void PrintRawDeclStmt(const DeclStmt *S);
Reid Spencer5f016e22007-07-11 17:01:13 +000065 void PrintRawIfStmt(IfStmt *If);
Sebastian Redl8351da02008-12-22 21:35:02 +000066 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
Peter Collingbourned64e2372011-02-08 21:17:54 +000067 void PrintCallArgs(CallExpr *E);
John Wiegley28bbe4b2011-04-28 01:08:34 +000068 void PrintRawSEHExceptHandler(SEHExceptStmt *S);
69 void PrintRawSEHFinallyStmt(SEHFinallyStmt *S);
Mike Stump1eb44332009-09-09 15:08:12 +000070
Reid Spencer5f016e22007-07-11 17:01:13 +000071 void PrintExpr(Expr *E) {
72 if (E)
Chris Lattnerc5598cb2007-08-21 04:04:25 +000073 Visit(E);
Reid Spencer5f016e22007-07-11 17:01:13 +000074 else
75 OS << "<null expr>";
76 }
Mike Stump1eb44332009-09-09 15:08:12 +000077
Chris Lattner5f9e2722011-07-23 10:55:15 +000078 raw_ostream &Indent(int Delta = 0) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000079 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
80 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +000081 return OS;
82 }
Mike Stump1eb44332009-09-09 15:08:12 +000083
Mike Stump1eb44332009-09-09 15:08:12 +000084 void Visit(Stmt* S) {
Ted Kremenek42a509f2007-08-31 21:30:12 +000085 if (Helper && Helper->handledStmt(S,OS))
86 return;
87 else StmtVisitor<StmtPrinter>::Visit(S);
88 }
Argyrios Kyrtzidis78e4cc72010-08-15 01:15:33 +000089
Chandler Carruth61e38282010-10-23 08:21:37 +000090 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis78e4cc72010-08-15 01:15:33 +000091 Indent() << "<<unknown stmt type>>\n";
92 }
Chandler Carruth61e38282010-10-23 08:21:37 +000093 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis78e4cc72010-08-15 01:15:33 +000094 OS << "<<unknown expr type>>";
95 }
96 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Mike Stump1eb44332009-09-09 15:08:12 +000097
Argyrios Kyrtzidis78e4cc72010-08-15 01:15:33 +000098#define ABSTRACT_STMT(CLASS)
Douglas Gregorf2cad862008-11-14 12:46:07 +000099#define STMT(CLASS, PARENT) \
Chris Lattnerc5598cb2007-08-21 04:04:25 +0000100 void Visit##CLASS(CLASS *Node);
Sean Hunt4bfe1962010-05-05 15:24:00 +0000101#include "clang/AST/StmtNodes.inc"
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 };
103}
104
105//===----------------------------------------------------------------------===//
106// Stmt printing methods.
107//===----------------------------------------------------------------------===//
108
Reid Spencer5f016e22007-07-11 17:01:13 +0000109/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
110/// with no newline after the }.
111void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
112 OS << "{\n";
113 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
114 I != E; ++I)
115 PrintStmt(*I);
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 Indent() << "}";
118}
119
120void StmtPrinter::PrintRawDecl(Decl *D) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000121 D->print(OS, Policy, IndentLevel);
Mike Stump071e4da2009-02-10 20:16:46 +0000122}
123
Eli Friedman915c07d2012-10-16 23:45:15 +0000124void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
125 DeclStmt::const_decl_iterator Begin = S->decl_begin(), End = S->decl_end();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000126 SmallVector<Decl*, 2> Decls;
Mike Stump1eb44332009-09-09 15:08:12 +0000127 for ( ; Begin != End; ++Begin)
Eli Friedman42f42c02009-05-30 04:20:30 +0000128 Decls.push_back(*Begin);
Eli Friedmandb23b152009-05-30 00:19:54 +0000129
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000130 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
Ted Kremenekecd64c52008-10-06 18:39:36 +0000131}
Reid Spencer5f016e22007-07-11 17:01:13 +0000132
133void StmtPrinter::VisitNullStmt(NullStmt *Node) {
134 Indent() << ";\n";
135}
136
137void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Eli Friedmandb23b152009-05-30 00:19:54 +0000138 Indent();
139 PrintRawDeclStmt(Node);
140 OS << ";\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000141}
142
143void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
144 Indent();
145 PrintRawCompoundStmt(Node);
146 OS << "\n";
147}
148
149void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
150 Indent(-1) << "case ";
151 PrintExpr(Node->getLHS());
152 if (Node->getRHS()) {
153 OS << " ... ";
154 PrintExpr(Node->getRHS());
155 }
156 OS << ":\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 PrintStmt(Node->getSubStmt(), 0);
159}
160
161void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
162 Indent(-1) << "default:\n";
163 PrintStmt(Node->getSubStmt(), 0);
164}
165
166void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
167 Indent(-1) << Node->getName() << ":\n";
168 PrintStmt(Node->getSubStmt(), 0);
169}
170
Richard Smith534986f2012-04-14 00:33:13 +0000171void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
172 OS << "[[";
173 bool first = true;
Alexander Kornienko49908902012-07-09 10:04:07 +0000174 for (ArrayRef<const Attr*>::iterator it = Node->getAttrs().begin(),
175 end = Node->getAttrs().end();
176 it != end; ++it) {
Richard Smith534986f2012-04-14 00:33:13 +0000177 if (!first) {
178 OS << ", ";
179 first = false;
180 }
181 // TODO: check this
Richard Smith0dae7292012-08-16 02:43:29 +0000182 (*it)->printPretty(OS, Policy);
Richard Smith534986f2012-04-14 00:33:13 +0000183 }
184 OS << "]] ";
185 PrintStmt(Node->getSubStmt(), 0);
186}
187
Reid Spencer5f016e22007-07-11 17:01:13 +0000188void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000189 OS << "if (";
Eli Friedman915c07d2012-10-16 23:45:15 +0000190 if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
191 PrintRawDeclStmt(DS);
192 else
193 PrintExpr(If->getCond());
Sebastian Redlbfee9b22009-02-07 20:05:48 +0000194 OS << ')';
Mike Stump1eb44332009-09-09 15:08:12 +0000195
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
197 OS << ' ';
198 PrintRawCompoundStmt(CS);
199 OS << (If->getElse() ? ' ' : '\n');
200 } else {
201 OS << '\n';
202 PrintStmt(If->getThen());
203 if (If->getElse()) Indent();
204 }
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Reid Spencer5f016e22007-07-11 17:01:13 +0000206 if (Stmt *Else = If->getElse()) {
207 OS << "else";
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
210 OS << ' ';
211 PrintRawCompoundStmt(CS);
212 OS << '\n';
213 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
214 OS << ' ';
215 PrintRawIfStmt(ElseIf);
216 } else {
217 OS << '\n';
218 PrintStmt(If->getElse());
219 }
220 }
221}
222
223void StmtPrinter::VisitIfStmt(IfStmt *If) {
224 Indent();
225 PrintRawIfStmt(If);
226}
227
228void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
229 Indent() << "switch (";
Eli Friedman915c07d2012-10-16 23:45:15 +0000230 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
231 PrintRawDeclStmt(DS);
232 else
233 PrintExpr(Node->getCond());
Reid Spencer5f016e22007-07-11 17:01:13 +0000234 OS << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Reid Spencer5f016e22007-07-11 17:01:13 +0000236 // Pretty print compoundstmt bodies (very common).
237 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
238 OS << " ";
239 PrintRawCompoundStmt(CS);
240 OS << "\n";
241 } else {
242 OS << "\n";
243 PrintStmt(Node->getBody());
244 }
245}
246
247void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
248 Indent() << "while (";
Eli Friedman915c07d2012-10-16 23:45:15 +0000249 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
250 PrintRawDeclStmt(DS);
251 else
252 PrintExpr(Node->getCond());
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 OS << ")\n";
254 PrintStmt(Node->getBody());
255}
256
257void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner8bdcc472007-09-15 21:49:37 +0000258 Indent() << "do ";
259 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
260 PrintRawCompoundStmt(CS);
261 OS << " ";
262 } else {
263 OS << "\n";
264 PrintStmt(Node->getBody());
265 Indent();
266 }
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Eli Friedmanb3e22962009-05-17 01:05:34 +0000268 OS << "while (";
Reid Spencer5f016e22007-07-11 17:01:13 +0000269 PrintExpr(Node->getCond());
Eli Friedmanb3e22962009-05-17 01:05:34 +0000270 OS << ");\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000271}
272
273void StmtPrinter::VisitForStmt(ForStmt *Node) {
274 Indent() << "for (";
275 if (Node->getInit()) {
276 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000277 PrintRawDeclStmt(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 else
279 PrintExpr(cast<Expr>(Node->getInit()));
280 }
Chris Lattner8bdcc472007-09-15 21:49:37 +0000281 OS << ";";
282 if (Node->getCond()) {
283 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000284 PrintExpr(Node->getCond());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000285 }
286 OS << ";";
287 if (Node->getInc()) {
288 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000289 PrintExpr(Node->getInc());
Chris Lattner8bdcc472007-09-15 21:49:37 +0000290 }
291 OS << ") ";
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Chris Lattner8bdcc472007-09-15 21:49:37 +0000293 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
294 PrintRawCompoundStmt(CS);
295 OS << "\n";
296 } else {
297 OS << "\n";
298 PrintStmt(Node->getBody());
299 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000300}
301
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000302void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000303 Indent() << "for (";
304 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenekecd64c52008-10-06 18:39:36 +0000305 PrintRawDeclStmt(DS);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000306 else
307 PrintExpr(cast<Expr>(Node->getElement()));
308 OS << " in ";
309 PrintExpr(Node->getCollection());
310 OS << ") ";
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000312 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
313 PrintRawCompoundStmt(CS);
314 OS << "\n";
315 } else {
316 OS << "\n";
317 PrintStmt(Node->getBody());
318 }
319}
320
Richard Smithad762fc2011-04-14 22:09:26 +0000321void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
322 Indent() << "for (";
323 PrintingPolicy SubPolicy(Policy);
324 SubPolicy.SuppressInitializers = true;
325 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
326 OS << " : ";
327 PrintExpr(Node->getRangeInit());
328 OS << ") {\n";
329 PrintStmt(Node->getBody());
330 Indent() << "}\n";
331}
332
Douglas Gregorba0513d2011-10-25 01:33:02 +0000333void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
334 Indent();
335 if (Node->isIfExists())
336 OS << "__if_exists (";
337 else
338 OS << "__if_not_exists (";
339
340 if (NestedNameSpecifier *Qualifier
341 = Node->getQualifierLoc().getNestedNameSpecifier())
342 Qualifier->print(OS, Policy);
343
344 OS << Node->getNameInfo() << ") ";
345
346 PrintRawCompoundStmt(Node->getSubStmt());
347}
348
Reid Spencer5f016e22007-07-11 17:01:13 +0000349void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
350 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
351}
352
353void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
354 Indent() << "goto *";
355 PrintExpr(Node->getTarget());
356 OS << ";\n";
357}
358
359void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
360 Indent() << "continue;\n";
361}
362
363void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
364 Indent() << "break;\n";
365}
366
367
368void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
369 Indent() << "return";
370 if (Node->getRetValue()) {
371 OS << " ";
372 PrintExpr(Node->getRetValue());
373 }
374 OS << ";\n";
375}
376
Chris Lattnerfe795952007-10-29 04:04:16 +0000377
Chad Rosierdf5faf52012-08-25 00:11:56 +0000378void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
Anders Carlsson39c47b52007-11-23 23:12:25 +0000379 Indent() << "asm ";
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Anders Carlsson39c47b52007-11-23 23:12:25 +0000381 if (Node->isVolatile())
382 OS << "volatile ";
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Anders Carlsson39c47b52007-11-23 23:12:25 +0000384 OS << "(";
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000385 VisitStringLiteral(Node->getAsmString());
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Anders Carlssonb235fc22007-11-22 01:36:19 +0000387 // Outputs
388 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
389 Node->getNumClobbers() != 0)
390 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Anders Carlssonb235fc22007-11-22 01:36:19 +0000392 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
393 if (i != 0)
394 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Anders Carlssonb235fc22007-11-22 01:36:19 +0000396 if (!Node->getOutputName(i).empty()) {
397 OS << '[';
398 OS << Node->getOutputName(i);
399 OS << "] ";
400 }
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Chris Lattnerb3277932009-03-10 04:59:06 +0000402 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000403 OS << " ";
404 Visit(Node->getOutputExpr(i));
405 }
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Anders Carlssonb235fc22007-11-22 01:36:19 +0000407 // Inputs
408 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
409 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Anders Carlssonb235fc22007-11-22 01:36:19 +0000411 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
412 if (i != 0)
413 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Anders Carlssonb235fc22007-11-22 01:36:19 +0000415 if (!Node->getInputName(i).empty()) {
416 OS << '[';
417 OS << Node->getInputName(i);
418 OS << "] ";
419 }
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Chris Lattnerb3277932009-03-10 04:59:06 +0000421 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000422 OS << " ";
423 Visit(Node->getInputExpr(i));
424 }
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Anders Carlssonb235fc22007-11-22 01:36:19 +0000426 // Clobbers
427 if (Node->getNumClobbers() != 0)
428 OS << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Anders Carlssonb235fc22007-11-22 01:36:19 +0000430 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
431 if (i != 0)
432 OS << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Chad Rosier5c7f5942012-08-27 23:28:41 +0000434 VisitStringLiteral(Node->getClobberStringLiteral(i));
Anders Carlssonb235fc22007-11-22 01:36:19 +0000435 }
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Anders Carlsson6a0ef4b2007-11-20 19:21:03 +0000437 OS << ");\n";
Chris Lattnerfe795952007-10-29 04:04:16 +0000438}
439
Chad Rosier8cd64b42012-06-11 20:47:18 +0000440void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
441 // FIXME: Implement MS style inline asm statement printer.
Chad Rosier7bd092b2012-08-15 16:53:30 +0000442 Indent() << "__asm ";
443 if (Node->hasBraces())
444 OS << "{\n";
445 OS << *(Node->getAsmString()) << "\n";
446 if (Node->hasBraces())
447 Indent() << "}\n";
Chad Rosier8cd64b42012-06-11 20:47:18 +0000448}
449
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000450void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000451 Indent() << "@try";
452 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
453 PrintRawCompoundStmt(TS);
454 OS << "\n";
455 }
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000457 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
458 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000459 Indent() << "@catch(";
Steve Naroff7ba138a2009-03-03 19:52:17 +0000460 if (catchStmt->getCatchParamDecl()) {
461 if (Decl *DS = catchStmt->getCatchParamDecl())
462 PrintRawDecl(DS);
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000463 }
464 OS << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000465 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
466 PrintRawCompoundStmt(CS);
467 OS << "\n";
468 }
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000469 }
Mike Stump1eb44332009-09-09 15:08:12 +0000470
471 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
472 Node->getFinallyStmt())) {
Fariborz Jahanian1e7eab42007-11-07 00:46:42 +0000473 Indent() << "@finally";
474 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian7794cb82007-11-02 18:16:07 +0000475 OS << "\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000476 }
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000477}
478
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000479void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000480}
481
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000482void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanianb210bd02007-11-01 21:12:44 +0000483 Indent() << "@catch (...) { /* todo */ } \n";
484}
485
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000486void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +0000487 Indent() << "@throw";
488 if (Node->getThrowExpr()) {
489 OS << " ";
490 PrintExpr(Node->getThrowExpr());
491 }
492 OS << ";\n";
493}
494
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000495void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000496 Indent() << "@synchronized (";
497 PrintExpr(Node->getSynchExpr());
498 OS << ")";
Fariborz Jahanian78a677b2008-01-30 17:38:29 +0000499 PrintRawCompoundStmt(Node->getSynchBody());
500 OS << "\n";
Fariborz Jahanianc385c902008-01-29 18:21:32 +0000501}
502
John McCallf85e1932011-06-15 23:02:42 +0000503void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
504 Indent() << "@autoreleasepool";
505 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
506 OS << "\n";
507}
508
Sebastian Redl8351da02008-12-22 21:35:02 +0000509void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
510 OS << "catch (";
Sebastian Redl4b07b292008-12-22 19:15:10 +0000511 if (Decl *ExDecl = Node->getExceptionDecl())
512 PrintRawDecl(ExDecl);
513 else
514 OS << "...";
515 OS << ") ";
516 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl8351da02008-12-22 21:35:02 +0000517}
518
519void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
520 Indent();
521 PrintRawCXXCatchStmt(Node);
522 OS << "\n";
523}
524
525void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
526 Indent() << "try ";
527 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump1eb44332009-09-09 15:08:12 +0000528 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl8351da02008-12-22 21:35:02 +0000529 OS << " ";
530 PrintRawCXXCatchStmt(Node->getHandler(i));
531 }
Sebastian Redl4b07b292008-12-22 19:15:10 +0000532 OS << "\n";
533}
534
John Wiegley28bbe4b2011-04-28 01:08:34 +0000535void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
536 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
537 PrintRawCompoundStmt(Node->getTryBlock());
538 SEHExceptStmt *E = Node->getExceptHandler();
539 SEHFinallyStmt *F = Node->getFinallyHandler();
540 if(E)
541 PrintRawSEHExceptHandler(E);
542 else {
543 assert(F && "Must have a finally block...");
544 PrintRawSEHFinallyStmt(F);
545 }
546 OS << "\n";
547}
548
549void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
550 OS << "__finally ";
551 PrintRawCompoundStmt(Node->getBlock());
552 OS << "\n";
553}
554
555void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
556 OS << "__except (";
557 VisitExpr(Node->getFilterExpr());
Joao Matos568ba872012-09-04 17:49:35 +0000558 OS << ")\n";
John Wiegley28bbe4b2011-04-28 01:08:34 +0000559 PrintRawCompoundStmt(Node->getBlock());
560 OS << "\n";
561}
562
563void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
564 Indent();
565 PrintRawSEHExceptHandler(Node);
566 OS << "\n";
567}
568
569void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
570 Indent();
571 PrintRawSEHFinallyStmt(Node);
572 OS << "\n";
573}
574
Reid Spencer5f016e22007-07-11 17:01:13 +0000575//===----------------------------------------------------------------------===//
576// Expr printing methods.
577//===----------------------------------------------------------------------===//
578
Reid Spencer5f016e22007-07-11 17:01:13 +0000579void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000580 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
581 Qualifier->print(OS, Policy);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000582 if (Node->hasTemplateKeyword())
583 OS << "template ";
Abramo Bagnara25777432010-08-11 22:01:17 +0000584 OS << Node->getNameInfo();
John McCall096832c2010-08-19 23:49:38 +0000585 if (Node->hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000586 OS << TemplateSpecializationType::PrintTemplateArgumentList(
587 Node->getTemplateArgs(),
588 Node->getNumTemplateArgs(),
Sean Huntc3021132010-05-05 15:23:54 +0000589 Policy);
Douglas Gregor1a49af92009-01-06 05:10:23 +0000590}
591
John McCall865d4472009-11-19 22:55:06 +0000592void StmtPrinter::VisitDependentScopeDeclRefExpr(
593 DependentScopeDeclRefExpr *Node) {
Axel Naumann274f83c2011-01-24 15:44:00 +0000594 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
595 Qualifier->print(OS, Policy);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000596 if (Node->hasTemplateKeyword())
597 OS << "template ";
Abramo Bagnara25777432010-08-11 22:01:17 +0000598 OS << Node->getNameInfo();
John McCallf7a1a742009-11-24 19:00:30 +0000599 if (Node->hasExplicitTemplateArgs())
600 OS << TemplateSpecializationType::PrintTemplateArgumentList(
601 Node->getTemplateArgs(),
602 Node->getNumTemplateArgs(),
603 Policy);
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000604}
605
John McCallba135432009-11-21 08:51:07 +0000606void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000607 if (Node->getQualifier())
608 Node->getQualifier()->print(OS, Policy);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000609 if (Node->hasTemplateKeyword())
610 OS << "template ";
Abramo Bagnara25777432010-08-11 22:01:17 +0000611 OS << Node->getNameInfo();
John McCallf7a1a742009-11-24 19:00:30 +0000612 if (Node->hasExplicitTemplateArgs())
613 OS << TemplateSpecializationType::PrintTemplateArgumentList(
614 Node->getTemplateArgs(),
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000615 Node->getNumTemplateArgs(),
John McCallf7a1a742009-11-24 19:00:30 +0000616 Policy);
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000617}
618
Steve Naroff7779db42007-11-12 14:29:37 +0000619void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000620 if (Node->getBase()) {
621 PrintExpr(Node->getBase());
622 OS << (Node->isArrow() ? "->" : ".");
623 }
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000624 OS << *Node->getDecl();
Steve Naroff7779db42007-11-12 14:29:37 +0000625}
626
Steve Naroffae784072008-05-30 00:40:33 +0000627void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000628 if (Node->isSuperReceiver())
629 OS << "super.";
630 else if (Node->getBase()) {
Steve Naroffae784072008-05-30 00:40:33 +0000631 PrintExpr(Node->getBase());
632 OS << ".";
633 }
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +0000634
John McCall12f78a62010-12-02 01:19:52 +0000635 if (Node->isImplicitProperty())
636 OS << Node->getImplicitPropertyGetter()->getSelector().getAsString();
637 else
638 OS << Node->getExplicitProperty()->getName();
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000639}
640
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000641void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
642
643 PrintExpr(Node->getBaseExpr());
644 OS << "[";
645 PrintExpr(Node->getKeyExpr());
646 OS << "]";
647}
648
Chris Lattnerd9f69102008-08-10 01:53:14 +0000649void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson22742662007-07-21 05:21:51 +0000650 switch (Node->getIdentType()) {
651 default:
David Blaikieb219cfc2011-09-23 05:06:16 +0000652 llvm_unreachable("unknown case");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000653 case PredefinedExpr::Func:
Anders Carlsson22742662007-07-21 05:21:51 +0000654 OS << "__func__";
655 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000656 case PredefinedExpr::Function:
Anders Carlsson22742662007-07-21 05:21:51 +0000657 OS << "__FUNCTION__";
658 break;
Nico Weber28ad0632012-06-23 02:07:59 +0000659 case PredefinedExpr::LFunction:
660 OS << "L__FUNCTION__";
661 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000662 case PredefinedExpr::PrettyFunction:
Anders Carlsson22742662007-07-21 05:21:51 +0000663 OS << "__PRETTY_FUNCTION__";
664 break;
665 }
666}
667
Reid Spencer5f016e22007-07-11 17:01:13 +0000668void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000669 unsigned value = Node->getValue();
Douglas Gregor5cee1192011-07-27 05:40:30 +0000670
671 switch (Node->getKind()) {
672 case CharacterLiteral::Ascii: break; // no prefix.
673 case CharacterLiteral::Wide: OS << 'L'; break;
674 case CharacterLiteral::UTF16: OS << 'u'; break;
675 case CharacterLiteral::UTF32: OS << 'U'; break;
676 }
677
Chris Lattner8bf9f072007-07-13 23:58:20 +0000678 switch (value) {
679 case '\\':
680 OS << "'\\\\'";
681 break;
682 case '\'':
683 OS << "'\\''";
684 break;
685 case '\a':
686 // TODO: K&R: the meaning of '\\a' is different in traditional C
687 OS << "'\\a'";
688 break;
689 case '\b':
690 OS << "'\\b'";
691 break;
692 // Nonstandard escape sequence.
693 /*case '\e':
694 OS << "'\\e'";
695 break;*/
696 case '\f':
697 OS << "'\\f'";
698 break;
699 case '\n':
700 OS << "'\\n'";
701 break;
702 case '\r':
703 OS << "'\\r'";
704 break;
705 case '\t':
706 OS << "'\\t'";
707 break;
708 case '\v':
709 OS << "'\\v'";
710 break;
711 default:
Ted Kremenek471733d2008-02-23 00:52:04 +0000712 if (value < 256 && isprint(value)) {
Chris Lattner8bf9f072007-07-13 23:58:20 +0000713 OS << "'" << (char)value << "'";
714 } else if (value < 256) {
Benjamin Kramer8fe83e12012-02-04 13:45:25 +0000715 OS << "'\\x";
716 OS.write_hex(value) << "'";
Chris Lattner8bf9f072007-07-13 23:58:20 +0000717 } else {
718 // FIXME what to really do here?
719 OS << value;
720 }
Chris Lattnerb0a721a2007-07-13 05:18:11 +0000721 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000722}
723
724void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
725 bool isSigned = Node->getType()->isSignedIntegerType();
726 OS << Node->getValue().toString(10, isSigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000727
Reid Spencer5f016e22007-07-11 17:01:13 +0000728 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall183700f2009-09-21 23:43:11 +0000729 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000730 default: llvm_unreachable("Unexpected type for integer literal!");
Richard Trieu11cbe2a2011-11-07 18:40:31 +0000731 // FIXME: The Short and UShort cases are to handle cases where a short
732 // integeral literal is formed during template instantiation. They should
733 // be removed when template instantiation no longer needs integer literals.
734 case BuiltinType::Short:
735 case BuiltinType::UShort:
Reid Spencer5f016e22007-07-11 17:01:13 +0000736 case BuiltinType::Int: break; // no suffix.
737 case BuiltinType::UInt: OS << 'U'; break;
738 case BuiltinType::Long: OS << 'L'; break;
739 case BuiltinType::ULong: OS << "UL"; break;
740 case BuiltinType::LongLong: OS << "LL"; break;
741 case BuiltinType::ULongLong: OS << "ULL"; break;
Richard Trieu11cbe2a2011-11-07 18:40:31 +0000742 case BuiltinType::Int128: OS << "i128"; break;
743 case BuiltinType::UInt128: OS << "Ui128"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000744 }
745}
Benjamin Kramera0a3c902012-09-20 14:07:17 +0000746
747static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
748 bool PrintSuffix) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000749 SmallString<16> Str;
Eli Friedmanb3909212011-10-05 20:32:03 +0000750 Node->getValue().toString(Str);
751 OS << Str;
Benjamin Kramera0a3c902012-09-20 14:07:17 +0000752 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
753 OS << '.'; // Trailing dot in order to separate from ints.
754
755 if (!PrintSuffix)
756 return;
757
758 // Emit suffixes. Float literals are always a builtin float type.
759 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
760 default: llvm_unreachable("Unexpected type for float literal!");
761 case BuiltinType::Half: break; // FIXME: suffix?
762 case BuiltinType::Double: break; // no suffix.
763 case BuiltinType::Float: OS << 'F'; break;
764 case BuiltinType::LongDouble: OS << 'L'; break;
765 }
766}
767
768void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
769 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000770}
Chris Lattner5d661452007-08-26 03:42:43 +0000771
772void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
773 PrintExpr(Node->getSubExpr());
774 OS << "i";
775}
776
Reid Spencer5f016e22007-07-11 17:01:13 +0000777void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Richard Trieu8ab09da2012-06-13 20:25:24 +0000778 Str->outputString(OS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000779}
780void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
781 OS << "(";
782 PrintExpr(Node->getSubExpr());
783 OS << ")";
784}
785void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattner296bf192007-08-23 21:46:40 +0000786 if (!Node->isPostfix()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000787 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump1eb44332009-09-09 15:08:12 +0000788
Eli Friedman7df71ac2009-06-14 22:39:26 +0000789 // Print a space if this is an "identifier operator" like __real, or if
790 // it might be concatenated incorrectly like '+'.
Chris Lattner296bf192007-08-23 21:46:40 +0000791 switch (Node->getOpcode()) {
792 default: break;
John McCall2de56d12010-08-25 11:45:40 +0000793 case UO_Real:
794 case UO_Imag:
795 case UO_Extension:
Chris Lattner296bf192007-08-23 21:46:40 +0000796 OS << ' ';
797 break;
John McCall2de56d12010-08-25 11:45:40 +0000798 case UO_Plus:
799 case UO_Minus:
Eli Friedman7df71ac2009-06-14 22:39:26 +0000800 if (isa<UnaryOperator>(Node->getSubExpr()))
801 OS << ' ';
802 break;
Chris Lattner296bf192007-08-23 21:46:40 +0000803 }
804 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000805 PrintExpr(Node->getSubExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Reid Spencer5f016e22007-07-11 17:01:13 +0000807 if (Node->isPostfix())
808 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Reid Spencer5f016e22007-07-11 17:01:13 +0000809}
Chris Lattner704fe352007-08-30 17:59:59 +0000810
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000811void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
812 OS << "__builtin_offsetof(";
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000813 OS << Node->getTypeSourceInfo()->getType().getAsString(Policy) << ", ";
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000814 bool PrintedSomething = false;
815 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
816 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
817 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
818 // Array node
819 OS << "[";
820 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
821 OS << "]";
822 PrintedSomething = true;
823 continue;
824 }
Douglas Gregorcc8a5d52010-04-29 00:18:15 +0000825
826 // Skip implicit base indirections.
827 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
828 continue;
829
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000830 // Field or identifier node.
831 IdentifierInfo *Id = ON.getFieldName();
832 if (!Id)
833 continue;
Sean Huntc3021132010-05-05 15:23:54 +0000834
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000835 if (PrintedSomething)
836 OS << ".";
837 else
838 PrintedSomething = true;
Sean Huntc3021132010-05-05 15:23:54 +0000839 OS << Id->getName();
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000840 }
841 OS << ")";
842}
843
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000844void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
845 switch(Node->getKind()) {
846 case UETT_SizeOf:
847 OS << "sizeof";
848 break;
849 case UETT_AlignOf:
Jordan Rosef70a8862012-06-30 21:33:57 +0000850 if (Policy.LangOpts.CPlusPlus)
851 OS << "alignof";
852 else if (Policy.LangOpts.C11)
853 OS << "_Alignof";
854 else
855 OS << "__alignof";
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000856 break;
857 case UETT_VecStep:
858 OS << "vec_step";
859 break;
860 }
Sebastian Redl05189992008-11-11 17:56:53 +0000861 if (Node->isArgumentType())
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000862 OS << "(" << Node->getArgumentType().getAsString(Policy) << ")";
Sebastian Redl05189992008-11-11 17:56:53 +0000863 else {
864 OS << " ";
865 PrintExpr(Node->getArgumentExpr());
866 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000867}
Peter Collingbournef111d932011-04-15 00:35:48 +0000868
869void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
870 OS << "_Generic(";
871 PrintExpr(Node->getControllingExpr());
872 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
873 OS << ", ";
874 QualType T = Node->getAssocType(i);
875 if (T.isNull())
876 OS << "default";
877 else
878 OS << T.getAsString(Policy);
879 OS << ": ";
880 PrintExpr(Node->getAssocExpr(i));
881 }
882 OS << ")";
883}
884
Reid Spencer5f016e22007-07-11 17:01:13 +0000885void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenek23245122007-08-20 16:18:38 +0000886 PrintExpr(Node->getLHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000887 OS << "[";
Ted Kremenek23245122007-08-20 16:18:38 +0000888 PrintExpr(Node->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000889 OS << "]";
890}
891
Peter Collingbourned64e2372011-02-08 21:17:54 +0000892void StmtPrinter::PrintCallArgs(CallExpr *Call) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000893 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattner04421082008-04-08 04:40:51 +0000894 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
895 // Don't print any defaulted arguments
896 break;
897 }
898
Reid Spencer5f016e22007-07-11 17:01:13 +0000899 if (i) OS << ", ";
900 PrintExpr(Call->getArg(i));
901 }
Peter Collingbourned64e2372011-02-08 21:17:54 +0000902}
903
904void StmtPrinter::VisitCallExpr(CallExpr *Call) {
905 PrintExpr(Call->getCallee());
906 OS << "(";
907 PrintCallArgs(Call);
Reid Spencer5f016e22007-07-11 17:01:13 +0000908 OS << ")";
909}
910void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregorb3eef682009-01-08 22:45:41 +0000911 // FIXME: Suppress printing implicit bases (like "this")
912 PrintExpr(Node->getBase());
Fariborz Jahanian97fd83a2010-01-11 21:17:32 +0000913 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
914 if (FD->isAnonymousStructOrUnion())
915 return;
Douglas Gregorb3eef682009-01-08 22:45:41 +0000916 OS << (Node->isArrow() ? "->" : ".");
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000917 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
918 Qualifier->print(OS, Policy);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000919 if (Node->hasTemplateKeyword())
920 OS << "template ";
Abramo Bagnara25777432010-08-11 22:01:17 +0000921 OS << Node->getMemberNameInfo();
John McCall096832c2010-08-19 23:49:38 +0000922 if (Node->hasExplicitTemplateArgs())
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000923 OS << TemplateSpecializationType::PrintTemplateArgumentList(
924 Node->getTemplateArgs(),
925 Node->getNumTemplateArgs(),
926 Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000927}
Steve Narofff242b1b2009-07-24 17:54:45 +0000928void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
929 PrintExpr(Node->getBase());
930 OS << (Node->isArrow() ? "->isa" : ".isa");
931}
932
Nate Begeman213541a2008-04-18 23:10:10 +0000933void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Naroff31a45842007-07-28 23:10:27 +0000934 PrintExpr(Node->getBase());
935 OS << ".";
936 OS << Node->getAccessor().getName();
937}
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000938void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Fariborz Jahanian03e80e42010-06-30 16:31:08 +0000939 OS << "(" << Node->getType().getAsString(Policy) << ")";
Reid Spencer5f016e22007-07-11 17:01:13 +0000940 PrintExpr(Node->getSubExpr());
941}
Steve Naroffaff1edd2007-07-19 21:32:11 +0000942void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000943 OS << "(" << Node->getType().getAsString(Policy) << ")";
Steve Naroffaff1edd2007-07-19 21:32:11 +0000944 PrintExpr(Node->getInitializer());
945}
Steve Naroff49b45262007-07-13 16:58:59 +0000946void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroff90045e82007-07-13 23:32:42 +0000947 // No need to print anything, simply forward to the sub expression.
948 PrintExpr(Node->getSubExpr());
Steve Naroff49b45262007-07-13 16:58:59 +0000949}
Reid Spencer5f016e22007-07-11 17:01:13 +0000950void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
951 PrintExpr(Node->getLHS());
952 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
953 PrintExpr(Node->getRHS());
954}
Chris Lattnereb14fe82007-08-25 02:00:02 +0000955void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
956 PrintExpr(Node->getLHS());
957 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
958 PrintExpr(Node->getRHS());
959}
Reid Spencer5f016e22007-07-11 17:01:13 +0000960void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
961 PrintExpr(Node->getCond());
John McCall56ca35d2011-02-17 10:25:35 +0000962 OS << " ? ";
963 PrintExpr(Node->getLHS());
964 OS << " : ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000965 PrintExpr(Node->getRHS());
966}
967
968// GNU extensions.
969
John McCall56ca35d2011-02-17 10:25:35 +0000970void
971StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
972 PrintExpr(Node->getCommon());
973 OS << " ?: ";
974 PrintExpr(Node->getFalseExpr());
975}
Chris Lattner6481a572007-08-03 17:31:20 +0000976void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000977 OS << "&&" << Node->getLabel()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000978}
979
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000980void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
981 OS << "(";
982 PrintRawCompoundStmt(E->getSubStmt());
983 OS << ")";
984}
985
Steve Naroffd04fdd52007-08-03 21:21:27 +0000986void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
987 OS << "__builtin_choose_expr(";
988 PrintExpr(Node->getCond());
Chris Lattner94f05e32007-08-04 00:20:15 +0000989 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000990 PrintExpr(Node->getLHS());
Chris Lattner94f05e32007-08-04 00:20:15 +0000991 OS << ", ";
Steve Naroffd04fdd52007-08-03 21:21:27 +0000992 PrintExpr(Node->getRHS());
993 OS << ")";
994}
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000995
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000996void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
997 OS << "__null";
998}
999
Eli Friedmand38617c2008-05-14 19:38:39 +00001000void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1001 OS << "__builtin_shufflevector(";
1002 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1003 if (i) OS << ", ";
1004 PrintExpr(Node->getExpr(i));
1005 }
1006 OS << ")";
1007}
1008
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001009void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor64f65002009-05-30 00:56:08 +00001010 if (Node->getSyntacticForm()) {
1011 Visit(Node->getSyntacticForm());
1012 return;
1013 }
1014
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001015 OS << "{ ";
1016 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1017 if (i) OS << ", ";
Douglas Gregor4c678342009-01-28 21:54:33 +00001018 if (Node->getInit(i))
1019 PrintExpr(Node->getInit(i));
1020 else
1021 OS << "0";
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001022 }
1023 OS << " }";
1024}
1025
Nate Begeman2ef13e52009-08-10 23:49:36 +00001026void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1027 OS << "( ";
1028 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1029 if (i) OS << ", ";
1030 PrintExpr(Node->getExpr(i));
1031 }
1032 OS << " )";
1033}
1034
Douglas Gregor05c13a32009-01-22 00:58:24 +00001035void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor4c678342009-01-28 21:54:33 +00001036 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
1037 DEnd = Node->designators_end();
1038 D != DEnd; ++D) {
1039 if (D->isFieldDesignator()) {
1040 if (D->getDotLoc().isInvalid())
1041 OS << D->getFieldName()->getName() << ":";
1042 else
1043 OS << "." << D->getFieldName()->getName();
1044 } else {
1045 OS << "[";
1046 if (D->isArrayDesignator()) {
1047 PrintExpr(Node->getArrayIndex(*D));
1048 } else {
1049 PrintExpr(Node->getArrayRangeStart(*D));
1050 OS << " ... ";
Mike Stump1eb44332009-09-09 15:08:12 +00001051 PrintExpr(Node->getArrayRangeEnd(*D));
Douglas Gregor4c678342009-01-28 21:54:33 +00001052 }
1053 OS << "]";
1054 }
1055 }
1056
1057 OS << " = ";
1058 PrintExpr(Node->getInit());
Douglas Gregor05c13a32009-01-22 00:58:24 +00001059}
1060
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001061void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Chris Lattnere4f21422009-06-30 01:26:17 +00001062 if (Policy.LangOpts.CPlusPlus)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001063 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()";
1064 else {
1065 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")";
1066 if (Node->getType()->isRecordType())
1067 OS << "{}";
1068 else
1069 OS << 0;
1070 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001071}
1072
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001073void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman42f42c02009-05-30 04:20:30 +00001074 OS << "__builtin_va_arg(";
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001075 PrintExpr(Node->getSubExpr());
1076 OS << ", ";
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001077 OS << Node->getType().getAsString(Policy);
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001078 OS << ")";
1079}
1080
John McCall4b9c2d22011-11-06 09:01:30 +00001081void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1082 PrintExpr(Node->getSyntacticForm());
1083}
1084
Eli Friedman276b0612011-10-11 02:20:01 +00001085void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
Eli Friedman9e3c20b2011-10-11 20:00:47 +00001086 const char *Name = 0;
Eli Friedman276b0612011-10-11 02:20:01 +00001087 switch (Node->getOp()) {
Richard Smithff34d402012-04-12 05:08:17 +00001088#define BUILTIN(ID, TYPE, ATTRS)
1089#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1090 case AtomicExpr::AO ## ID: \
1091 Name = #ID "("; \
1092 break;
1093#include "clang/Basic/Builtins.def"
Eli Friedman276b0612011-10-11 02:20:01 +00001094 }
1095 OS << Name;
Richard Smithff34d402012-04-12 05:08:17 +00001096
1097 // AtomicExpr stores its subexpressions in a permuted order.
Eli Friedman276b0612011-10-11 02:20:01 +00001098 PrintExpr(Node->getPtr());
1099 OS << ", ";
Richard Smithff34d402012-04-12 05:08:17 +00001100 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1101 Node->getOp() != AtomicExpr::AO__atomic_load_n) {
Eli Friedman276b0612011-10-11 02:20:01 +00001102 PrintExpr(Node->getVal1());
1103 OS << ", ";
1104 }
Richard Smithff34d402012-04-12 05:08:17 +00001105 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1106 Node->isCmpXChg()) {
Eli Friedman276b0612011-10-11 02:20:01 +00001107 PrintExpr(Node->getVal2());
1108 OS << ", ";
1109 }
Richard Smithff34d402012-04-12 05:08:17 +00001110 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1111 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
1112 PrintExpr(Node->getWeak());
1113 OS << ", ";
1114 }
1115 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init)
David Chisnall7a7ee302012-01-16 17:27:18 +00001116 PrintExpr(Node->getOrder());
Eli Friedman276b0612011-10-11 02:20:01 +00001117 if (Node->isCmpXChg()) {
1118 OS << ", ";
1119 PrintExpr(Node->getOrderFail());
1120 }
1121 OS << ")";
1122}
1123
Reid Spencer5f016e22007-07-11 17:01:13 +00001124// C++
Douglas Gregorb4609802008-11-14 16:09:21 +00001125void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1126 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1127 "",
1128#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1129 Spelling,
1130#include "clang/Basic/OperatorKinds.def"
1131 };
1132
1133 OverloadedOperatorKind Kind = Node->getOperator();
1134 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1135 if (Node->getNumArgs() == 1) {
1136 OS << OpStrings[Kind] << ' ';
1137 PrintExpr(Node->getArg(0));
1138 } else {
1139 PrintExpr(Node->getArg(0));
1140 OS << ' ' << OpStrings[Kind];
1141 }
Eli Friedmana143a9d2012-10-12 22:45:14 +00001142 } else if (Kind == OO_Arrow) {
1143 PrintExpr(Node->getArg(0));
Douglas Gregorb4609802008-11-14 16:09:21 +00001144 } else if (Kind == OO_Call) {
1145 PrintExpr(Node->getArg(0));
1146 OS << '(';
1147 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1148 if (ArgIdx > 1)
1149 OS << ", ";
1150 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1151 PrintExpr(Node->getArg(ArgIdx));
1152 }
1153 OS << ')';
1154 } else if (Kind == OO_Subscript) {
1155 PrintExpr(Node->getArg(0));
1156 OS << '[';
1157 PrintExpr(Node->getArg(1));
1158 OS << ']';
1159 } else if (Node->getNumArgs() == 1) {
1160 OS << OpStrings[Kind] << ' ';
1161 PrintExpr(Node->getArg(0));
1162 } else if (Node->getNumArgs() == 2) {
1163 PrintExpr(Node->getArg(0));
1164 OS << ' ' << OpStrings[Kind] << ' ';
1165 PrintExpr(Node->getArg(1));
1166 } else {
David Blaikieb219cfc2011-09-23 05:06:16 +00001167 llvm_unreachable("unknown overloaded operator");
Douglas Gregorb4609802008-11-14 16:09:21 +00001168 }
1169}
Reid Spencer5f016e22007-07-11 17:01:13 +00001170
Douglas Gregor88a35142008-12-22 05:46:06 +00001171void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1172 VisitCallExpr(cast<CallExpr>(Node));
1173}
1174
Peter Collingbournee08ce652011-02-09 21:07:24 +00001175void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1176 PrintExpr(Node->getCallee());
1177 OS << "<<<";
1178 PrintCallArgs(Node->getConfig());
1179 OS << ">>>(";
1180 PrintCallArgs(Node);
1181 OS << ")";
1182}
1183
Douglas Gregor49badde2008-10-27 19:41:14 +00001184void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1185 OS << Node->getCastName() << '<';
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001186 OS << Node->getTypeAsWritten().getAsString(Policy) << ">(";
Reid Spencer5f016e22007-07-11 17:01:13 +00001187 PrintExpr(Node->getSubExpr());
1188 OS << ")";
1189}
1190
Douglas Gregor49badde2008-10-27 19:41:14 +00001191void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1192 VisitCXXNamedCastExpr(Node);
1193}
1194
1195void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1196 VisitCXXNamedCastExpr(Node);
1197}
1198
1199void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1200 VisitCXXNamedCastExpr(Node);
1201}
1202
1203void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1204 VisitCXXNamedCastExpr(Node);
1205}
1206
Sebastian Redlc42e1182008-11-11 11:37:55 +00001207void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1208 OS << "typeid(";
1209 if (Node->isTypeOperand()) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001210 OS << Node->getTypeOperand().getAsString(Policy);
Sebastian Redlc42e1182008-11-11 11:37:55 +00001211 } else {
1212 PrintExpr(Node->getExprOperand());
1213 }
1214 OS << ")";
1215}
1216
Francois Pichet01b7c302010-09-08 12:20:18 +00001217void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1218 OS << "__uuidof(";
1219 if (Node->isTypeOperand()) {
1220 OS << Node->getTypeOperand().getAsString(Policy);
1221 } else {
1222 PrintExpr(Node->getExprOperand());
1223 }
1224 OS << ")";
1225}
1226
Richard Smith9fcce652012-03-07 08:35:16 +00001227void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1228 switch (Node->getLiteralOperatorKind()) {
1229 case UserDefinedLiteral::LOK_Raw:
Richard Smithef9f2982012-03-09 10:10:02 +00001230 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
Richard Smith9fcce652012-03-07 08:35:16 +00001231 break;
1232 case UserDefinedLiteral::LOK_Template: {
Richard Smithef9f2982012-03-09 10:10:02 +00001233 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1234 const TemplateArgumentList *Args =
1235 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1236 assert(Args);
1237 const TemplateArgument &Pack = Args->get(0);
1238 for (TemplateArgument::pack_iterator I = Pack.pack_begin(),
1239 E = Pack.pack_end(); I != E; ++I) {
Benjamin Kramer85524372012-06-07 15:09:51 +00001240 char C = (char)I->getAsIntegral().getZExtValue();
Richard Smith9fcce652012-03-07 08:35:16 +00001241 OS << C;
1242 }
1243 break;
1244 }
Richard Smith91688302012-03-08 09:02:38 +00001245 case UserDefinedLiteral::LOK_Integer: {
1246 // Print integer literal without suffix.
1247 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1248 OS << Int->getValue().toString(10, /*isSigned*/false);
1249 break;
1250 }
Benjamin Kramera0a3c902012-09-20 14:07:17 +00001251 case UserDefinedLiteral::LOK_Floating: {
1252 // Print floating literal without suffix.
1253 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1254 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1255 break;
1256 }
Richard Smith9fcce652012-03-07 08:35:16 +00001257 case UserDefinedLiteral::LOK_String:
1258 case UserDefinedLiteral::LOK_Character:
1259 PrintExpr(Node->getCookedLiteral());
1260 break;
1261 }
1262 OS << Node->getUDSuffix()->getName();
1263}
1264
Reid Spencer5f016e22007-07-11 17:01:13 +00001265void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1266 OS << (Node->getValue() ? "true" : "false");
1267}
1268
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001269void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1270 OS << "nullptr";
1271}
1272
Douglas Gregor796da182008-11-04 14:32:21 +00001273void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1274 OS << "this";
1275}
1276
Chris Lattner50dd2892008-02-26 00:51:44 +00001277void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1278 if (Node->getSubExpr() == 0)
1279 OS << "throw";
1280 else {
1281 OS << "throw ";
1282 PrintExpr(Node->getSubExpr());
1283 }
1284}
1285
Chris Lattner04421082008-04-08 04:40:51 +00001286void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1287 // Nothing to print: we picked up the default argument
1288}
1289
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001290void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001291 OS << Node->getType().getAsString(Policy);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001292 OS << "(";
1293 PrintExpr(Node->getSubExpr());
1294 OS << ")";
1295}
1296
Anders Carlssonfceb0a82009-05-30 20:03:25 +00001297void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1298 PrintExpr(Node->getSubExpr());
1299}
1300
Douglas Gregor506ae412009-01-16 18:33:17 +00001301void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001302 OS << Node->getType().getAsString(Policy);
Douglas Gregor506ae412009-01-16 18:33:17 +00001303 OS << "(";
1304 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001305 ArgEnd = Node->arg_end();
Douglas Gregor506ae412009-01-16 18:33:17 +00001306 Arg != ArgEnd; ++Arg) {
1307 if (Arg != Node->arg_begin())
1308 OS << ", ";
1309 PrintExpr(*Arg);
1310 }
1311 OS << ")";
1312}
1313
Douglas Gregor01d08012012-02-07 10:09:13 +00001314void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1315 OS << '[';
1316 bool NeedComma = false;
1317 switch (Node->getCaptureDefault()) {
1318 case LCD_None:
1319 break;
1320
1321 case LCD_ByCopy:
1322 OS << '=';
1323 NeedComma = true;
1324 break;
1325
1326 case LCD_ByRef:
1327 OS << '&';
1328 NeedComma = true;
1329 break;
1330 }
1331 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1332 CEnd = Node->explicit_capture_end();
1333 C != CEnd;
1334 ++C) {
1335 if (NeedComma)
1336 OS << ", ";
1337 NeedComma = true;
1338
1339 switch (C->getCaptureKind()) {
1340 case LCK_This:
1341 OS << "this";
1342 break;
1343
1344 case LCK_ByRef:
1345 if (Node->getCaptureDefault() != LCD_ByRef)
1346 OS << '&';
1347 OS << C->getCapturedVar()->getName();
1348 break;
1349
1350 case LCK_ByCopy:
1351 if (Node->getCaptureDefault() != LCD_ByCopy)
1352 OS << '=';
1353 OS << C->getCapturedVar()->getName();
1354 break;
1355 }
1356 }
1357 OS << ']';
1358
1359 if (Node->hasExplicitParameters()) {
1360 OS << " (";
1361 CXXMethodDecl *Method = Node->getCallOperator();
1362 NeedComma = false;
1363 for (CXXMethodDecl::param_iterator P = Method->param_begin(),
1364 PEnd = Method->param_end();
1365 P != PEnd; ++P) {
1366 if (NeedComma) {
1367 OS << ", ";
1368 } else {
1369 NeedComma = true;
1370 }
1371 std::string ParamStr = (*P)->getNameAsString();
1372 (*P)->getOriginalType().getAsStringInternal(ParamStr, Policy);
1373 OS << ParamStr;
1374 }
1375 if (Method->isVariadic()) {
1376 if (NeedComma)
1377 OS << ", ";
1378 OS << "...";
1379 }
1380 OS << ')';
1381
1382 if (Node->isMutable())
1383 OS << " mutable";
1384
1385 const FunctionProtoType *Proto
1386 = Method->getType()->getAs<FunctionProtoType>();
1387 {
1388 std::string ExceptionSpec;
1389 Proto->printExceptionSpecification(ExceptionSpec, Policy);
1390 OS << ExceptionSpec;
1391 }
1392
1393 // FIXME: Attributes
1394
Douglas Gregordfca6f52012-02-13 22:00:16 +00001395 // Print the trailing return type if it was specified in the source.
1396 if (Node->hasExplicitResultType())
1397 OS << " -> " << Proto->getResultType().getAsString(Policy);
Douglas Gregor01d08012012-02-07 10:09:13 +00001398 }
1399
1400 // Print the body.
1401 CompoundStmt *Body = Node->getBody();
1402 OS << ' ';
1403 PrintStmt(Body);
1404}
1405
Douglas Gregored8abf12010-07-08 06:14:04 +00001406void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregorab6677e2010-09-08 00:15:04 +00001407 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
1408 OS << TSInfo->getType().getAsString(Policy) << "()";
1409 else
1410 OS << Node->getType().getAsString(Policy) << "()";
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001411}
1412
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001413void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1414 if (E->isGlobalNew())
1415 OS << "::";
1416 OS << "new ";
1417 unsigned NumPlace = E->getNumPlacementArgs();
1418 if (NumPlace > 0) {
1419 OS << "(";
1420 PrintExpr(E->getPlacementArg(0));
1421 for (unsigned i = 1; i < NumPlace; ++i) {
1422 OS << ", ";
1423 PrintExpr(E->getPlacementArg(i));
1424 }
1425 OS << ") ";
1426 }
1427 if (E->isParenTypeId())
1428 OS << "(";
Sebastian Redl6fec6482008-12-02 22:08:59 +00001429 std::string TypeS;
1430 if (Expr *Size = E->getArraySize()) {
1431 llvm::raw_string_ostream s(TypeS);
Richard Smithd1420c62012-08-16 03:56:14 +00001432 Size->printPretty(s, Helper, Policy);
Sebastian Redl6fec6482008-12-02 22:08:59 +00001433 s.flush();
1434 TypeS = "[" + TypeS + "]";
1435 }
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001436 E->getAllocatedType().getAsStringInternal(TypeS, Policy);
Sebastian Redl6fec6482008-12-02 22:08:59 +00001437 OS << TypeS;
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001438 if (E->isParenTypeId())
1439 OS << ")";
1440
Sebastian Redl2aed8b82012-02-16 12:22:20 +00001441 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
1442 if (InitStyle) {
1443 if (InitStyle == CXXNewExpr::CallInit)
1444 OS << "(";
1445 PrintExpr(E->getInitializer());
1446 if (InitStyle == CXXNewExpr::CallInit)
1447 OS << ")";
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001448 }
1449}
1450
1451void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1452 if (E->isGlobalDelete())
1453 OS << "::";
1454 OS << "delete ";
1455 if (E->isArrayForm())
1456 OS << "[] ";
1457 PrintExpr(E->getArgument());
1458}
1459
Douglas Gregora71d8192009-09-04 17:36:40 +00001460void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1461 PrintExpr(E->getBase());
1462 if (E->isArrow())
1463 OS << "->";
1464 else
1465 OS << '.';
1466 if (E->getQualifier())
1467 E->getQualifier()->print(OS, Policy);
Mike Stump1eb44332009-09-09 15:08:12 +00001468
Douglas Gregora71d8192009-09-04 17:36:40 +00001469 std::string TypeS;
Douglas Gregora2e7dd22010-02-25 01:56:36 +00001470 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1471 OS << II->getName();
1472 else
1473 E->getDestroyedType().getAsStringInternal(TypeS, Policy);
Douglas Gregora71d8192009-09-04 17:36:40 +00001474 OS << TypeS;
1475}
1476
Anders Carlssone349bea2009-04-23 02:32:43 +00001477void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Douglas Gregord0fb3ad2011-01-24 17:25:03 +00001478 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1479 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1480 // Don't print any defaulted arguments
1481 break;
1482 }
1483
1484 if (i) OS << ", ";
1485 PrintExpr(E->getArg(i));
Fariborz Jahanianc75da512010-01-13 21:41:11 +00001486 }
Anders Carlssone349bea2009-04-23 02:32:43 +00001487}
1488
John McCall4765fa02010-12-06 08:20:24 +00001489void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Anders Carlsson02bbfa32009-04-24 22:47:04 +00001490 // Just forward to the sub expression.
1491 PrintExpr(E->getSubExpr());
1492}
1493
Mike Stump1eb44332009-09-09 15:08:12 +00001494void
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001495StmtPrinter::VisitCXXUnresolvedConstructExpr(
1496 CXXUnresolvedConstructExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001497 OS << Node->getTypeAsWritten().getAsString(Policy);
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001498 OS << "(";
1499 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001500 ArgEnd = Node->arg_end();
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001501 Arg != ArgEnd; ++Arg) {
1502 if (Arg != Node->arg_begin())
1503 OS << ", ";
1504 PrintExpr(*Arg);
1505 }
1506 OS << ")";
1507}
1508
John McCall865d4472009-11-19 22:55:06 +00001509void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1510 CXXDependentScopeMemberExpr *Node) {
John McCallaa81e162009-12-01 22:10:20 +00001511 if (!Node->isImplicitAccess()) {
1512 PrintExpr(Node->getBase());
1513 OS << (Node->isArrow() ? "->" : ".");
1514 }
Douglas Gregora38c6872009-09-03 16:14:30 +00001515 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1516 Qualifier->print(OS, Policy);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001517 if (Node->hasTemplateKeyword())
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001518 OS << "template ";
Abramo Bagnara25777432010-08-11 22:01:17 +00001519 OS << Node->getMemberNameInfo();
John McCallaa81e162009-12-01 22:10:20 +00001520 if (Node->hasExplicitTemplateArgs()) {
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001521 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1522 Node->getTemplateArgs(),
1523 Node->getNumTemplateArgs(),
1524 Policy);
1525 }
Douglas Gregor1c0ca592009-05-22 21:13:27 +00001526}
1527
John McCall129e2df2009-11-30 22:42:35 +00001528void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCallaa81e162009-12-01 22:10:20 +00001529 if (!Node->isImplicitAccess()) {
1530 PrintExpr(Node->getBase());
1531 OS << (Node->isArrow() ? "->" : ".");
1532 }
John McCall129e2df2009-11-30 22:42:35 +00001533 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1534 Qualifier->print(OS, Policy);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001535 if (Node->hasTemplateKeyword())
1536 OS << "template ";
Abramo Bagnara25777432010-08-11 22:01:17 +00001537 OS << Node->getMemberNameInfo();
John McCall129e2df2009-11-30 22:42:35 +00001538 if (Node->hasExplicitTemplateArgs()) {
1539 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1540 Node->getTemplateArgs(),
1541 Node->getNumTemplateArgs(),
1542 Policy);
1543 }
1544}
1545
Sebastian Redl64b45f72009-01-05 20:52:13 +00001546static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1547 switch (UTT) {
Sebastian Redl64b45f72009-01-05 20:52:13 +00001548 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001549 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
John Wiegley20c0da72011-04-27 23:09:49 +00001550 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001551 case UTT_HasTrivialAssign: return "__has_trivial_assign";
Sean Hunt023df372011-05-09 18:22:59 +00001552 case UTT_HasTrivialDefaultConstructor: return "__has_trivial_constructor";
John Wiegley20c0da72011-04-27 23:09:49 +00001553 case UTT_HasTrivialCopy: return "__has_trivial_copy";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001554 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1555 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1556 case UTT_IsAbstract: return "__is_abstract";
John Wiegley20c0da72011-04-27 23:09:49 +00001557 case UTT_IsArithmetic: return "__is_arithmetic";
1558 case UTT_IsArray: return "__is_array";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001559 case UTT_IsClass: return "__is_class";
John Wiegley20c0da72011-04-27 23:09:49 +00001560 case UTT_IsCompleteType: return "__is_complete_type";
1561 case UTT_IsCompound: return "__is_compound";
1562 case UTT_IsConst: return "__is_const";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001563 case UTT_IsEmpty: return "__is_empty";
1564 case UTT_IsEnum: return "__is_enum";
Douglas Gregor5e9392b2011-12-03 18:14:24 +00001565 case UTT_IsFinal: return "__is_final";
John Wiegley20c0da72011-04-27 23:09:49 +00001566 case UTT_IsFloatingPoint: return "__is_floating_point";
1567 case UTT_IsFunction: return "__is_function";
1568 case UTT_IsFundamental: return "__is_fundamental";
1569 case UTT_IsIntegral: return "__is_integral";
John McCallea30e2f2012-09-25 07:32:49 +00001570 case UTT_IsInterfaceClass: return "__is_interface_class";
Chandler Carruthe1947102011-05-01 07:23:20 +00001571 case UTT_IsLiteral: return "__is_literal";
John Wiegley20c0da72011-04-27 23:09:49 +00001572 case UTT_IsLvalueReference: return "__is_lvalue_reference";
1573 case UTT_IsMemberFunctionPointer: return "__is_member_function_pointer";
1574 case UTT_IsMemberObjectPointer: return "__is_member_object_pointer";
1575 case UTT_IsMemberPointer: return "__is_member_pointer";
1576 case UTT_IsObject: return "__is_object";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001577 case UTT_IsPOD: return "__is_pod";
John Wiegley20c0da72011-04-27 23:09:49 +00001578 case UTT_IsPointer: return "__is_pointer";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001579 case UTT_IsPolymorphic: return "__is_polymorphic";
John Wiegley20c0da72011-04-27 23:09:49 +00001580 case UTT_IsReference: return "__is_reference";
John Wiegley20c0da72011-04-27 23:09:49 +00001581 case UTT_IsRvalueReference: return "__is_rvalue_reference";
1582 case UTT_IsScalar: return "__is_scalar";
1583 case UTT_IsSigned: return "__is_signed";
1584 case UTT_IsStandardLayout: return "__is_standard_layout";
1585 case UTT_IsTrivial: return "__is_trivial";
Sean Huntfeb375d2011-05-13 00:31:07 +00001586 case UTT_IsTriviallyCopyable: return "__is_trivially_copyable";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001587 case UTT_IsUnion: return "__is_union";
John Wiegley20c0da72011-04-27 23:09:49 +00001588 case UTT_IsUnsigned: return "__is_unsigned";
1589 case UTT_IsVoid: return "__is_void";
1590 case UTT_IsVolatile: return "__is_volatile";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001591 }
Chandler Carruthe1947102011-05-01 07:23:20 +00001592 llvm_unreachable("Type trait not covered by switch statement");
Francois Pichet6ad6f282010-12-07 00:08:36 +00001593}
1594
1595static const char *getTypeTraitName(BinaryTypeTrait BTT) {
1596 switch (BTT) {
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00001597 case BTT_IsBaseOf: return "__is_base_of";
1598 case BTT_IsConvertible: return "__is_convertible";
1599 case BTT_IsSame: return "__is_same";
1600 case BTT_TypeCompatible: return "__builtin_types_compatible_p";
1601 case BTT_IsConvertibleTo: return "__is_convertible_to";
1602 case BTT_IsTriviallyAssignable: return "__is_trivially_assignable";
Francois Pichet6ad6f282010-12-07 00:08:36 +00001603 }
Chandler Carrutha280bc82011-05-01 07:23:23 +00001604 llvm_unreachable("Binary type trait not covered by switch");
Sebastian Redl64b45f72009-01-05 20:52:13 +00001605}
1606
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00001607static const char *getTypeTraitName(TypeTrait TT) {
1608 switch (TT) {
1609 case clang::TT_IsTriviallyConstructible:return "__is_trivially_constructible";
1610 }
1611 llvm_unreachable("Type trait not covered by switch");
1612}
1613
John Wiegley21ff2e52011-04-28 00:16:57 +00001614static const char *getTypeTraitName(ArrayTypeTrait ATT) {
1615 switch (ATT) {
1616 case ATT_ArrayRank: return "__array_rank";
1617 case ATT_ArrayExtent: return "__array_extent";
1618 }
Chandler Carrutha280bc82011-05-01 07:23:23 +00001619 llvm_unreachable("Array type trait not covered by switch");
John Wiegley21ff2e52011-04-28 00:16:57 +00001620}
1621
John Wiegley55262202011-04-25 06:54:41 +00001622static const char *getExpressionTraitName(ExpressionTrait ET) {
1623 switch (ET) {
John Wiegley55262202011-04-25 06:54:41 +00001624 case ET_IsLValueExpr: return "__is_lvalue_expr";
1625 case ET_IsRValueExpr: return "__is_rvalue_expr";
1626 }
Chandler Carrutha280bc82011-05-01 07:23:23 +00001627 llvm_unreachable("Expression type trait not covered by switch");
John Wiegley55262202011-04-25 06:54:41 +00001628}
1629
Sebastian Redl64b45f72009-01-05 20:52:13 +00001630void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1631 OS << getTypeTraitName(E->getTrait()) << "("
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001632 << E->getQueriedType().getAsString(Policy) << ")";
Sebastian Redl64b45f72009-01-05 20:52:13 +00001633}
1634
Francois Pichet6ad6f282010-12-07 00:08:36 +00001635void StmtPrinter::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
1636 OS << getTypeTraitName(E->getTrait()) << "("
1637 << E->getLhsType().getAsString(Policy) << ","
1638 << E->getRhsType().getAsString(Policy) << ")";
1639}
1640
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00001641void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
1642 OS << getTypeTraitName(E->getTrait()) << "(";
1643 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
1644 if (I > 0)
1645 OS << ", ";
1646 OS << E->getArg(I)->getType().getAsString(Policy);
1647 }
1648 OS << ")";
1649}
1650
John Wiegley21ff2e52011-04-28 00:16:57 +00001651void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
1652 OS << getTypeTraitName(E->getTrait()) << "("
1653 << E->getQueriedType().getAsString(Policy) << ")";
1654}
1655
John Wiegley55262202011-04-25 06:54:41 +00001656void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
1657 OS << getExpressionTraitName(E->getTrait()) << "(";
1658 PrintExpr(E->getQueriedExpression());
1659 OS << ")";
1660}
1661
Sebastian Redl2e156222010-09-10 20:55:43 +00001662void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1663 OS << "noexcept(";
1664 PrintExpr(E->getOperand());
1665 OS << ")";
1666}
1667
Douglas Gregoree8aff02011-01-04 17:33:58 +00001668void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregorbe230c32011-01-03 17:17:50 +00001669 PrintExpr(E->getPattern());
1670 OS << "...";
1671}
1672
Douglas Gregoree8aff02011-01-04 17:33:58 +00001673void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
Benjamin Kramera59d20b2012-02-07 11:57:57 +00001674 OS << "sizeof...(" << *E->getPack() << ")";
Douglas Gregoree8aff02011-01-04 17:33:58 +00001675}
1676
Douglas Gregorc7793c72011-01-15 01:15:58 +00001677void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
1678 SubstNonTypeTemplateParmPackExpr *Node) {
Benjamin Kramera59d20b2012-02-07 11:57:57 +00001679 OS << *Node->getParameterPack();
Douglas Gregorc7793c72011-01-15 01:15:58 +00001680}
1681
John McCall91a57552011-07-15 05:09:51 +00001682void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
1683 SubstNonTypeTemplateParmExpr *Node) {
1684 Visit(Node->getReplacement());
1685}
1686
Richard Smith9a4db032012-09-12 00:56:43 +00001687void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
1688 OS << *E->getParameterPack();
1689}
1690
Douglas Gregor03e80032011-06-21 17:03:29 +00001691void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
1692 PrintExpr(Node->GetTemporaryExpr());
1693}
1694
Mike Stump1eb44332009-09-09 15:08:12 +00001695// Obj-C
Anders Carlsson55085182007-08-21 17:43:55 +00001696
1697void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1698 OS << "@";
1699 VisitStringLiteral(Node->getString());
1700}
Reid Spencer5f016e22007-07-11 17:01:13 +00001701
Patrick Beardeb382ec2012-04-19 00:25:12 +00001702void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001703 OS << "@";
Patrick Beardeb382ec2012-04-19 00:25:12 +00001704 Visit(E->getSubExpr());
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001705}
1706
1707void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
1708 OS << "@[ ";
1709 StmtRange ch = E->children();
1710 if (ch.first != ch.second) {
1711 while (1) {
1712 Visit(*ch.first);
1713 ++ch.first;
1714 if (ch.first == ch.second) break;
1715 OS << ", ";
1716 }
1717 }
1718 OS << " ]";
1719}
1720
1721void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
1722 OS << "@{ ";
1723 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
1724 if (I > 0)
1725 OS << ", ";
1726
1727 ObjCDictionaryElement Element = E->getKeyValueElement(I);
1728 Visit(Element.Key);
1729 OS << " : ";
1730 Visit(Element.Value);
1731 if (Element.isPackExpansion())
1732 OS << "...";
1733 }
1734 OS << " }";
1735}
1736
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001737void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +00001738 OS << "@encode(" << Node->getEncodedType().getAsString(Policy) << ')';
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001739}
1740
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001741void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner39f34e92008-11-24 04:00:27 +00001742 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001743}
1744
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001745void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramerb8989f22011-10-14 18:45:37 +00001746 OS << "@protocol(" << *Node->getProtocol() << ')';
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001747}
1748
Steve Naroff563477d2007-09-18 23:55:05 +00001749void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1750 OS << "[";
Douglas Gregor04badcf2010-04-21 00:45:42 +00001751 switch (Mess->getReceiverKind()) {
1752 case ObjCMessageExpr::Instance:
1753 PrintExpr(Mess->getInstanceReceiver());
1754 break;
1755
1756 case ObjCMessageExpr::Class:
1757 OS << Mess->getClassReceiver().getAsString(Policy);
1758 break;
1759
1760 case ObjCMessageExpr::SuperInstance:
1761 case ObjCMessageExpr::SuperClass:
1762 OS << "Super";
1763 break;
1764 }
1765
Ted Kremenekc29efd82008-05-02 17:32:38 +00001766 OS << ' ';
Ted Kremenek97b7f262008-04-16 04:30:16 +00001767 Selector selector = Mess->getSelector();
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001768 if (selector.isUnarySelector()) {
Douglas Gregor813d8342011-02-18 22:29:55 +00001769 OS << selector.getNameForSlot(0);
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001770 } else {
1771 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremenekc29efd82008-05-02 17:32:38 +00001772 if (i < selector.getNumArgs()) {
1773 if (i > 0) OS << ' ';
1774 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner39f34e92008-11-24 04:00:27 +00001775 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremenekc29efd82008-05-02 17:32:38 +00001776 else
1777 OS << ":";
1778 }
1779 else OS << ", "; // Handle variadic methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001780
Steve Naroff6a8a9a42007-10-02 20:01:56 +00001781 PrintExpr(Mess->getArg(i));
1782 }
Steve Naroff563477d2007-09-18 23:55:05 +00001783 }
1784 OS << "]";
1785}
1786
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001787void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
1788 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
1789}
1790
John McCallf85e1932011-06-15 23:02:42 +00001791void
1792StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
1793 PrintExpr(E->getSubExpr());
1794}
1795
1796void
1797StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
1798 OS << "(" << E->getBridgeKindName() << E->getType().getAsString(Policy)
1799 << ")";
1800 PrintExpr(E->getSubExpr());
1801}
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001802
Steve Naroff4eb206b2008-09-03 18:15:37 +00001803void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001804 BlockDecl *BD = Node->getBlockDecl();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001805 OS << "^";
Mike Stump1eb44332009-09-09 15:08:12 +00001806
Steve Naroff4eb206b2008-09-03 18:15:37 +00001807 const FunctionType *AFT = Node->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +00001808
Douglas Gregor72564e72009-02-26 23:50:07 +00001809 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001810 OS << "()";
Douglas Gregor72564e72009-02-26 23:50:07 +00001811 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroff4eb206b2008-09-03 18:15:37 +00001812 OS << '(';
1813 std::string ParamStr;
Steve Naroff56ee6892008-10-08 17:01:13 +00001814 for (BlockDecl::param_iterator AI = BD->param_begin(),
1815 E = BD->param_end(); AI != E; ++AI) {
1816 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner39f34e92008-11-24 04:00:27 +00001817 ParamStr = (*AI)->getNameAsString();
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001818 (*AI)->getType().getAsStringInternal(ParamStr, Policy);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001819 OS << ParamStr;
1820 }
Mike Stump1eb44332009-09-09 15:08:12 +00001821
Douglas Gregor72564e72009-02-26 23:50:07 +00001822 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroff4eb206b2008-09-03 18:15:37 +00001823 if (FT->isVariadic()) {
Steve Naroff56ee6892008-10-08 17:01:13 +00001824 if (!BD->param_empty()) OS << ", ";
Steve Naroff4eb206b2008-09-03 18:15:37 +00001825 OS << "...";
1826 }
1827 OS << ')';
1828 }
1829}
1830
Ted Kremenek381c0662011-11-30 22:08:08 +00001831void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
1832 PrintExpr(Node->getSourceExpr());
1833}
John McCall7cd7d1a2010-11-15 23:31:06 +00001834
Tanya Lattner61eee0c2011-06-04 00:47:47 +00001835void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
1836 OS << "__builtin_astype(";
1837 PrintExpr(Node->getSrcExpr());
1838 OS << ", " << Node->getType().getAsString();
1839 OS << ")";
1840}
1841
Reid Spencer5f016e22007-07-11 17:01:13 +00001842//===----------------------------------------------------------------------===//
1843// Stmt method implementations
1844//===----------------------------------------------------------------------===//
1845
Richard Smithd1420c62012-08-16 03:56:14 +00001846void Stmt::dumpPretty(ASTContext &Context) const {
1847 printPretty(llvm::errs(), 0, PrintingPolicy(Context.getLangOpts()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001848}
1849
Richard Smithd1420c62012-08-16 03:56:14 +00001850void Stmt::printPretty(raw_ostream &OS,
1851 PrinterHelper *Helper,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001852 const PrintingPolicy &Policy,
1853 unsigned Indentation) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001854 if (this == 0) {
1855 OS << "<NULL>";
1856 return;
1857 }
1858
Richard Smithd1420c62012-08-16 03:56:14 +00001859 if (Policy.DumpSourceManager) {
1860 dump(OS, *Policy.DumpSourceManager);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001861 return;
1862 }
Mike Stump1eb44332009-09-09 15:08:12 +00001863
Richard Smithd1420c62012-08-16 03:56:14 +00001864 StmtPrinter P(OS, Helper, Policy, Indentation);
Chris Lattnerc5598cb2007-08-21 04:04:25 +00001865 P.Visit(const_cast<Stmt*>(this));
Reid Spencer5f016e22007-07-11 17:01:13 +00001866}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001867
1868//===----------------------------------------------------------------------===//
1869// PrinterHelper
1870//===----------------------------------------------------------------------===//
1871
1872// Implement virtual destructor.
Gabor Greif84675832007-09-11 15:32:40 +00001873PrinterHelper::~PrinterHelper() {}