blob: 9c7259745ca83ee91adae358fb67efcc1641e2a4 [file] [log] [blame]
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001//===--- StmtPrinter.cpp - Printing implementation for Stmt ASTs ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnercbe4f772007-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.
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "clang/AST/StmtVisitor.h"
Douglas Gregorc7acfdf2009-01-06 05:10:23 +000016#include "clang/AST/DeclCXX.h"
Ted Kremenek5c84c012007-10-17 18:36:42 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregorcdbc5392011-01-15 01:15:58 +000018#include "clang/AST/DeclTemplate.h"
Ted Kremenek04f3cee2007-08-31 21:30:12 +000019#include "clang/AST/PrettyPrinter.h"
Douglas Gregor882211c2010-04-28 22:16:22 +000020#include "clang/AST/Expr.h"
Douglas Gregor2b88c112010-09-08 00:15:04 +000021#include "clang/AST/ExprCXX.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000022#include "llvm/ADT/SmallString.h"
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// StmtPrinter Visitor
27//===----------------------------------------------------------------------===//
28
29namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +000030 class StmtPrinter : public StmtVisitor<StmtPrinter> {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000031 raw_ostream &OS;
Eli Friedmanef334fd2009-05-30 05:03:24 +000032 ASTContext &Context;
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000033 unsigned IndentLevel;
Ted Kremenek04f3cee2007-08-31 21:30:12 +000034 clang::PrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +000035 PrintingPolicy Policy;
36
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000037 public:
Chris Lattner0e62c1c2011-07-23 10:55:15 +000038 StmtPrinter(raw_ostream &os, ASTContext &C, PrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +000039 const PrintingPolicy &Policy,
Douglas Gregor7de59662009-05-29 20:38:28 +000040 unsigned Indentation = 0)
Eli Friedmanef334fd2009-05-30 05:03:24 +000041 : OS(os), Context(C), IndentLevel(Indentation), Helper(helper),
42 Policy(Policy) {}
Mike Stump11289f42009-09-09 15:08:12 +000043
Douglas Gregor7de59662009-05-29 20:38:28 +000044 void PrintStmt(Stmt *S) {
45 PrintStmt(S, Policy.Indentation);
46 }
47
48 void PrintStmt(Stmt *S, int SubIndent) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000049 IndentLevel += SubIndent;
Chris Lattnera076fde2007-05-31 18:21:33 +000050 if (S && isa<Expr>(S)) {
Chris Lattner882f7882006-11-04 18:52:07 +000051 // If this is an expr used in a stmt context, indent and newline it.
52 Indent();
Chris Lattner62249a62007-08-21 04:04:25 +000053 Visit(S);
Chris Lattnerfc068c12007-05-30 17:57:36 +000054 OS << ";\n";
Chris Lattner882f7882006-11-04 18:52:07 +000055 } else if (S) {
Chris Lattner62249a62007-08-21 04:04:25 +000056 Visit(S);
Chris Lattner882f7882006-11-04 18:52:07 +000057 } else {
Chris Lattner2f6ac262007-05-28 01:47:47 +000058 Indent() << "<<<NULL STATEMENT>>>\n";
Chris Lattner882f7882006-11-04 18:52:07 +000059 }
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000060 IndentLevel -= SubIndent;
Chris Lattner882f7882006-11-04 18:52:07 +000061 }
Eli Friedman15ea8802009-05-30 00:19:54 +000062
Chris Lattner073926e2007-05-20 23:04:55 +000063 void PrintRawCompoundStmt(CompoundStmt *S);
Chris Lattnerfdc195a2007-06-05 20:52:47 +000064 void PrintRawDecl(Decl *D);
Ted Kremenek15e6b402008-10-06 18:39:36 +000065 void PrintRawDeclStmt(DeclStmt *S);
Chris Lattnerc0a38dd2007-06-11 22:26:23 +000066 void PrintRawIfStmt(IfStmt *If);
Sebastian Redl9b244a82008-12-22 21:35:02 +000067 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
Peter Collingbourne4b279a02011-02-08 21:17:54 +000068 void PrintCallArgs(CallExpr *E);
John Wiegley1c0675e2011-04-28 01:08:34 +000069 void PrintRawSEHExceptHandler(SEHExceptStmt *S);
70 void PrintRawSEHFinallyStmt(SEHFinallyStmt *S);
Mike Stump11289f42009-09-09 15:08:12 +000071
Chris Lattner882f7882006-11-04 18:52:07 +000072 void PrintExpr(Expr *E) {
73 if (E)
Chris Lattner62249a62007-08-21 04:04:25 +000074 Visit(E);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000075 else
Chris Lattner882f7882006-11-04 18:52:07 +000076 OS << "<null expr>";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000077 }
Mike Stump11289f42009-09-09 15:08:12 +000078
Chris Lattner0e62c1c2011-07-23 10:55:15 +000079 raw_ostream &Indent(int Delta = 0) {
Douglas Gregor7de59662009-05-29 20:38:28 +000080 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
81 OS << " ";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000082 return OS;
83 }
Mike Stump11289f42009-09-09 15:08:12 +000084
Mike Stump11289f42009-09-09 15:08:12 +000085 void Visit(Stmt* S) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +000086 if (Helper && Helper->handledStmt(S,OS))
87 return;
88 else StmtVisitor<StmtPrinter>::Visit(S);
89 }
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +000090
Chandler Carruthb7967b92010-10-23 08:21:37 +000091 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +000092 Indent() << "<<unknown stmt type>>\n";
93 }
Chandler Carruthb7967b92010-10-23 08:21:37 +000094 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +000095 OS << "<<unknown expr type>>";
96 }
97 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Mike Stump11289f42009-09-09 15:08:12 +000098
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +000099#define ABSTRACT_STMT(CLASS)
Douglas Gregorbe35ce92008-11-14 12:46:07 +0000100#define STMT(CLASS, PARENT) \
Chris Lattner62249a62007-08-21 04:04:25 +0000101 void Visit##CLASS(CLASS *Node);
Alexis Hunt656bb312010-05-05 15:24:00 +0000102#include "clang/AST/StmtNodes.inc"
Chris Lattner71e23ce2006-11-04 20:18:38 +0000103 };
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000104}
105
Chris Lattner71e23ce2006-11-04 20:18:38 +0000106//===----------------------------------------------------------------------===//
107// Stmt printing methods.
108//===----------------------------------------------------------------------===//
109
Chris Lattner073926e2007-05-20 23:04:55 +0000110/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
111/// with no newline after the }.
112void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
113 OS << "{\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000114 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
Chris Lattner882f7882006-11-04 18:52:07 +0000115 I != E; ++I)
116 PrintStmt(*I);
Mike Stump11289f42009-09-09 15:08:12 +0000117
Chris Lattner073926e2007-05-20 23:04:55 +0000118 Indent() << "}";
119}
120
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000121void StmtPrinter::PrintRawDecl(Decl *D) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000122 D->print(OS, Policy, IndentLevel);
Mike Stump74a76472009-02-10 20:16:46 +0000123}
124
Ted Kremenek15e6b402008-10-06 18:39:36 +0000125void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
Eli Friedman15ea8802009-05-30 00:19:54 +0000126 DeclStmt::decl_iterator Begin = S->decl_begin(), End = S->decl_end();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000127 SmallVector<Decl*, 2> Decls;
Mike Stump11289f42009-09-09 15:08:12 +0000128 for ( ; Begin != End; ++Begin)
Eli Friedman79635842009-05-30 04:20:30 +0000129 Decls.push_back(*Begin);
Eli Friedman15ea8802009-05-30 00:19:54 +0000130
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000131 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
Ted Kremenek15e6b402008-10-06 18:39:36 +0000132}
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000133
134void StmtPrinter::VisitNullStmt(NullStmt *Node) {
135 Indent() << ";\n";
136}
137
138void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Eli Friedman15ea8802009-05-30 00:19:54 +0000139 Indent();
140 PrintRawDeclStmt(Node);
141 OS << ";\n";
Steve Naroff2a8ad182007-05-29 22:59:26 +0000142}
143
Chris Lattner073926e2007-05-20 23:04:55 +0000144void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
145 Indent();
146 PrintRawCompoundStmt(Node);
Chris Lattnerdf3cafb2007-05-31 05:08:56 +0000147 OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000148}
149
Chris Lattner6c0ff132006-11-05 00:19:50 +0000150void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000151 Indent(-1) << "case ";
Chris Lattner6c0ff132006-11-05 00:19:50 +0000152 PrintExpr(Node->getLHS());
153 if (Node->getRHS()) {
154 OS << " ... ";
155 PrintExpr(Node->getRHS());
156 }
157 OS << ":\n";
Mike Stump11289f42009-09-09 15:08:12 +0000158
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000159 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000160}
161
162void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000163 Indent(-1) << "default:\n";
164 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000165}
166
167void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000168 Indent(-1) << Node->getName() << ":\n";
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000169 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000170}
171
Richard Smithc202b282012-04-14 00:33:13 +0000172void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
173 OS << "[[";
174 bool first = true;
175 for (AttrVec::const_iterator it = Node->getAttrs().begin(),
176 end = Node->getAttrs().end();
177 it != end; ++it) {
178 if (!first) {
179 OS << ", ";
180 first = false;
181 }
182 // TODO: check this
183 (*it)->printPretty(OS, Context);
184 }
185 OS << "]] ";
186 PrintStmt(Node->getSubStmt(), 0);
187}
188
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000189void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000190 OS << "if (";
Chris Lattner882f7882006-11-04 18:52:07 +0000191 PrintExpr(If->getCond());
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000192 OS << ')';
Mike Stump11289f42009-09-09 15:08:12 +0000193
Chris Lattner073926e2007-05-20 23:04:55 +0000194 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
195 OS << ' ';
196 PrintRawCompoundStmt(CS);
197 OS << (If->getElse() ? ' ' : '\n');
198 } else {
199 OS << '\n';
200 PrintStmt(If->getThen());
201 if (If->getElse()) Indent();
202 }
Mike Stump11289f42009-09-09 15:08:12 +0000203
Chris Lattner073926e2007-05-20 23:04:55 +0000204 if (Stmt *Else = If->getElse()) {
205 OS << "else";
Mike Stump11289f42009-09-09 15:08:12 +0000206
Chris Lattner073926e2007-05-20 23:04:55 +0000207 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
208 OS << ' ';
209 PrintRawCompoundStmt(CS);
210 OS << '\n';
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000211 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
212 OS << ' ';
213 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000214 } else {
215 OS << '\n';
216 PrintStmt(If->getElse());
217 }
Chris Lattner882f7882006-11-04 18:52:07 +0000218 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000219}
220
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000221void StmtPrinter::VisitIfStmt(IfStmt *If) {
222 Indent();
223 PrintRawIfStmt(If);
224}
225
Chris Lattnerf2174b62006-11-04 20:59:27 +0000226void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
227 Indent() << "switch (";
228 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000229 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000230
Chris Lattner073926e2007-05-20 23:04:55 +0000231 // Pretty print compoundstmt bodies (very common).
232 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
233 OS << " ";
234 PrintRawCompoundStmt(CS);
235 OS << "\n";
236 } else {
237 OS << "\n";
238 PrintStmt(Node->getBody());
239 }
Chris Lattnerf2174b62006-11-04 20:59:27 +0000240}
241
Chris Lattner85ed8732006-11-04 20:40:44 +0000242void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
243 Indent() << "while (";
244 PrintExpr(Node->getCond());
245 OS << ")\n";
246 PrintStmt(Node->getBody());
247}
248
249void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000250 Indent() << "do ";
251 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
252 PrintRawCompoundStmt(CS);
253 OS << " ";
254 } else {
255 OS << "\n";
256 PrintStmt(Node->getBody());
257 Indent();
258 }
Mike Stump11289f42009-09-09 15:08:12 +0000259
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000260 OS << "while (";
Chris Lattner85ed8732006-11-04 20:40:44 +0000261 PrintExpr(Node->getCond());
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000262 OS << ");\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000263}
264
Chris Lattner71e23ce2006-11-04 20:18:38 +0000265void StmtPrinter::VisitForStmt(ForStmt *Node) {
266 Indent() << "for (";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000267 if (Node->getInit()) {
268 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000269 PrintRawDeclStmt(DS);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000270 else
271 PrintExpr(cast<Expr>(Node->getInit()));
272 }
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000273 OS << ";";
274 if (Node->getCond()) {
275 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000276 PrintExpr(Node->getCond());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000277 }
278 OS << ";";
279 if (Node->getInc()) {
280 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000281 PrintExpr(Node->getInc());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000282 }
283 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000284
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000285 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
286 PrintRawCompoundStmt(CS);
287 OS << "\n";
288 } else {
289 OS << "\n";
290 PrintStmt(Node->getBody());
291 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000292}
293
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000294void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian83615522008-01-02 22:54:34 +0000295 Indent() << "for (";
296 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000297 PrintRawDeclStmt(DS);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000298 else
299 PrintExpr(cast<Expr>(Node->getElement()));
300 OS << " in ";
301 PrintExpr(Node->getCollection());
302 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000303
Fariborz Jahanian83615522008-01-02 22:54:34 +0000304 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
305 PrintRawCompoundStmt(CS);
306 OS << "\n";
307 } else {
308 OS << "\n";
309 PrintStmt(Node->getBody());
310 }
311}
312
Richard Smith02e85f32011-04-14 22:09:26 +0000313void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
314 Indent() << "for (";
315 PrintingPolicy SubPolicy(Policy);
316 SubPolicy.SuppressInitializers = true;
317 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
318 OS << " : ";
319 PrintExpr(Node->getRangeInit());
320 OS << ") {\n";
321 PrintStmt(Node->getBody());
322 Indent() << "}\n";
323}
324
Douglas Gregordeb4a2be2011-10-25 01:33:02 +0000325void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
326 Indent();
327 if (Node->isIfExists())
328 OS << "__if_exists (";
329 else
330 OS << "__if_not_exists (";
331
332 if (NestedNameSpecifier *Qualifier
333 = Node->getQualifierLoc().getNestedNameSpecifier())
334 Qualifier->print(OS, Policy);
335
336 OS << Node->getNameInfo() << ") ";
337
338 PrintRawCompoundStmt(Node->getSubStmt());
339}
340
Chris Lattner16976d32006-11-05 01:46:01 +0000341void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000342 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000343}
344
345void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000346 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000347 PrintExpr(Node->getTarget());
Chris Lattnerb4619482007-05-31 06:00:14 +0000348 OS << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000349}
350
351void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000352 Indent() << "continue;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000353}
354
355void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000356 Indent() << "break;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000357}
358
359
Chris Lattner882f7882006-11-04 18:52:07 +0000360void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
361 Indent() << "return";
362 if (Node->getRetValue()) {
363 OS << " ";
364 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000365 }
Chris Lattnerb4619482007-05-31 06:00:14 +0000366 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000367}
368
Chris Lattner73c56c02007-10-29 04:04:16 +0000369
370void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson660bdd12007-11-23 23:12:25 +0000371 Indent() << "asm ";
Mike Stump11289f42009-09-09 15:08:12 +0000372
Anders Carlsson660bdd12007-11-23 23:12:25 +0000373 if (Node->isVolatile())
374 OS << "volatile ";
Mike Stump11289f42009-09-09 15:08:12 +0000375
Anders Carlsson660bdd12007-11-23 23:12:25 +0000376 OS << "(";
Anders Carlsson81a5a692007-11-20 19:21:03 +0000377 VisitStringLiteral(Node->getAsmString());
Mike Stump11289f42009-09-09 15:08:12 +0000378
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000379 // Outputs
380 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
381 Node->getNumClobbers() != 0)
382 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000383
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000384 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
385 if (i != 0)
386 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000387
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000388 if (!Node->getOutputName(i).empty()) {
389 OS << '[';
390 OS << Node->getOutputName(i);
391 OS << "] ";
392 }
Mike Stump11289f42009-09-09 15:08:12 +0000393
Chris Lattner72bbf172009-03-10 04:59:06 +0000394 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000395 OS << " ";
396 Visit(Node->getOutputExpr(i));
397 }
Mike Stump11289f42009-09-09 15:08:12 +0000398
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000399 // Inputs
400 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
401 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000402
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000403 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
404 if (i != 0)
405 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000406
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000407 if (!Node->getInputName(i).empty()) {
408 OS << '[';
409 OS << Node->getInputName(i);
410 OS << "] ";
411 }
Mike Stump11289f42009-09-09 15:08:12 +0000412
Chris Lattner72bbf172009-03-10 04:59:06 +0000413 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000414 OS << " ";
415 Visit(Node->getInputExpr(i));
416 }
Mike Stump11289f42009-09-09 15:08:12 +0000417
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000418 // Clobbers
419 if (Node->getNumClobbers() != 0)
420 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000421
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000422 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
423 if (i != 0)
424 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000425
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000426 VisitStringLiteral(Node->getClobber(i));
427 }
Mike Stump11289f42009-09-09 15:08:12 +0000428
Anders Carlsson81a5a692007-11-20 19:21:03 +0000429 OS << ");\n";
Chris Lattner73c56c02007-10-29 04:04:16 +0000430}
431
Chad Rosier32503022012-06-11 20:47:18 +0000432void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
433 // FIXME: Implement MS style inline asm statement printer.
434 Indent() << "asm ()";
435}
436
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000437void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000438 Indent() << "@try";
439 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
440 PrintRawCompoundStmt(TS);
441 OS << "\n";
442 }
Mike Stump11289f42009-09-09 15:08:12 +0000443
Douglas Gregor96c79492010-04-23 22:50:49 +0000444 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
445 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000446 Indent() << "@catch(";
Steve Naroff371b8fb2009-03-03 19:52:17 +0000447 if (catchStmt->getCatchParamDecl()) {
448 if (Decl *DS = catchStmt->getCatchParamDecl())
449 PrintRawDecl(DS);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000450 }
451 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000452 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
453 PrintRawCompoundStmt(CS);
454 OS << "\n";
455 }
Fariborz Jahanian88157952007-11-02 18:16:07 +0000456 }
Mike Stump11289f42009-09-09 15:08:12 +0000457
458 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
459 Node->getFinallyStmt())) {
Fariborz Jahaniandefbf9a2007-11-07 00:46:42 +0000460 Indent() << "@finally";
461 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian88157952007-11-02 18:16:07 +0000462 OS << "\n";
Mike Stump11289f42009-09-09 15:08:12 +0000463 }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000464}
465
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000466void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000467}
468
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000469void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000470 Indent() << "@catch (...) { /* todo */ } \n";
471}
472
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000473void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000474 Indent() << "@throw";
475 if (Node->getThrowExpr()) {
476 OS << " ";
477 PrintExpr(Node->getThrowExpr());
478 }
479 OS << ";\n";
480}
481
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000482void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000483 Indent() << "@synchronized (";
484 PrintExpr(Node->getSynchExpr());
485 OS << ")";
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000486 PrintRawCompoundStmt(Node->getSynchBody());
487 OS << "\n";
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000488}
489
John McCall31168b02011-06-15 23:02:42 +0000490void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
491 Indent() << "@autoreleasepool";
492 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
493 OS << "\n";
494}
495
Sebastian Redl9b244a82008-12-22 21:35:02 +0000496void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
497 OS << "catch (";
Sebastian Redl54c04d42008-12-22 19:15:10 +0000498 if (Decl *ExDecl = Node->getExceptionDecl())
499 PrintRawDecl(ExDecl);
500 else
501 OS << "...";
502 OS << ") ";
503 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl9b244a82008-12-22 21:35:02 +0000504}
505
506void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
507 Indent();
508 PrintRawCXXCatchStmt(Node);
509 OS << "\n";
510}
511
512void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
513 Indent() << "try ";
514 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump11289f42009-09-09 15:08:12 +0000515 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl9b244a82008-12-22 21:35:02 +0000516 OS << " ";
517 PrintRawCXXCatchStmt(Node->getHandler(i));
518 }
Sebastian Redl54c04d42008-12-22 19:15:10 +0000519 OS << "\n";
520}
521
John Wiegley1c0675e2011-04-28 01:08:34 +0000522void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
523 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
524 PrintRawCompoundStmt(Node->getTryBlock());
525 SEHExceptStmt *E = Node->getExceptHandler();
526 SEHFinallyStmt *F = Node->getFinallyHandler();
527 if(E)
528 PrintRawSEHExceptHandler(E);
529 else {
530 assert(F && "Must have a finally block...");
531 PrintRawSEHFinallyStmt(F);
532 }
533 OS << "\n";
534}
535
536void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
537 OS << "__finally ";
538 PrintRawCompoundStmt(Node->getBlock());
539 OS << "\n";
540}
541
542void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
543 OS << "__except (";
544 VisitExpr(Node->getFilterExpr());
545 OS << ")\n";
546 PrintRawCompoundStmt(Node->getBlock());
547 OS << "\n";
548}
549
550void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
551 Indent();
552 PrintRawSEHExceptHandler(Node);
553 OS << "\n";
554}
555
556void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
557 Indent();
558 PrintRawSEHFinallyStmt(Node);
559 OS << "\n";
560}
561
Chris Lattner71e23ce2006-11-04 20:18:38 +0000562//===----------------------------------------------------------------------===//
563// Expr printing methods.
564//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000565
Chris Lattner882f7882006-11-04 18:52:07 +0000566void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000567 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
568 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000569 if (Node->hasTemplateKeyword())
570 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000571 OS << Node->getNameInfo();
John McCallb3774b52010-08-19 23:49:38 +0000572 if (Node->hasExplicitTemplateArgs())
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000573 OS << TemplateSpecializationType::PrintTemplateArgumentList(
574 Node->getTemplateArgs(),
575 Node->getNumTemplateArgs(),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000576 Policy);
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000577}
578
John McCall8cd78132009-11-19 22:55:06 +0000579void StmtPrinter::VisitDependentScopeDeclRefExpr(
580 DependentScopeDeclRefExpr *Node) {
Axel Naumann20b27862011-01-24 15:44:00 +0000581 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
582 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000583 if (Node->hasTemplateKeyword())
584 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000585 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000586 if (Node->hasExplicitTemplateArgs())
587 OS << TemplateSpecializationType::PrintTemplateArgumentList(
588 Node->getTemplateArgs(),
589 Node->getNumTemplateArgs(),
590 Policy);
Douglas Gregor90a1a652009-03-19 17:26:29 +0000591}
592
John McCalld14a8642009-11-21 08:51:07 +0000593void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregora727cb92009-06-30 22:34:41 +0000594 if (Node->getQualifier())
595 Node->getQualifier()->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000596 if (Node->hasTemplateKeyword())
597 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000598 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000599 if (Node->hasExplicitTemplateArgs())
600 OS << TemplateSpecializationType::PrintTemplateArgumentList(
601 Node->getTemplateArgs(),
Douglas Gregora727cb92009-06-30 22:34:41 +0000602 Node->getNumTemplateArgs(),
John McCalle66edc12009-11-24 19:00:30 +0000603 Policy);
Douglas Gregora727cb92009-06-30 22:34:41 +0000604}
605
Steve Naroffe46504b2007-11-12 14:29:37 +0000606void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000607 if (Node->getBase()) {
608 PrintExpr(Node->getBase());
609 OS << (Node->isArrow() ? "->" : ".");
610 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000611 OS << *Node->getDecl();
Steve Naroffe46504b2007-11-12 14:29:37 +0000612}
613
Steve Naroffec944032008-05-30 00:40:33 +0000614void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000615 if (Node->isSuperReceiver())
616 OS << "super.";
617 else if (Node->getBase()) {
Steve Naroffec944032008-05-30 00:40:33 +0000618 PrintExpr(Node->getBase());
619 OS << ".";
620 }
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000621
John McCallb7bd14f2010-12-02 01:19:52 +0000622 if (Node->isImplicitProperty())
623 OS << Node->getImplicitPropertyGetter()->getSelector().getAsString();
624 else
625 OS << Node->getExplicitProperty()->getName();
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000626}
627
Ted Kremeneke65b0862012-03-06 20:05:56 +0000628void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
629
630 PrintExpr(Node->getBaseExpr());
631 OS << "[";
632 PrintExpr(Node->getKeyExpr());
633 OS << "]";
634}
635
Chris Lattner6307f192008-08-10 01:53:14 +0000636void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson625bfc82007-07-21 05:21:51 +0000637 switch (Node->getIdentType()) {
638 default:
David Blaikie83d382b2011-09-23 05:06:16 +0000639 llvm_unreachable("unknown case");
Chris Lattner6307f192008-08-10 01:53:14 +0000640 case PredefinedExpr::Func:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000641 OS << "__func__";
642 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000643 case PredefinedExpr::Function:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000644 OS << "__FUNCTION__";
645 break;
Nico Weber3a691a32012-06-23 02:07:59 +0000646 case PredefinedExpr::LFunction:
647 OS << "L__FUNCTION__";
648 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000649 case PredefinedExpr::PrettyFunction:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000650 OS << "__PRETTY_FUNCTION__";
651 break;
652 }
653}
654
Steve Naroffae4143e2007-04-26 20:39:23 +0000655void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000656 unsigned value = Node->getValue();
Douglas Gregorfb65e592011-07-27 05:40:30 +0000657
658 switch (Node->getKind()) {
659 case CharacterLiteral::Ascii: break; // no prefix.
660 case CharacterLiteral::Wide: OS << 'L'; break;
661 case CharacterLiteral::UTF16: OS << 'u'; break;
662 case CharacterLiteral::UTF32: OS << 'U'; break;
663 }
664
Chris Lattner666115c2007-07-13 23:58:20 +0000665 switch (value) {
666 case '\\':
667 OS << "'\\\\'";
668 break;
669 case '\'':
670 OS << "'\\''";
671 break;
672 case '\a':
673 // TODO: K&R: the meaning of '\\a' is different in traditional C
674 OS << "'\\a'";
675 break;
676 case '\b':
677 OS << "'\\b'";
678 break;
679 // Nonstandard escape sequence.
680 /*case '\e':
681 OS << "'\\e'";
682 break;*/
683 case '\f':
684 OS << "'\\f'";
685 break;
686 case '\n':
687 OS << "'\\n'";
688 break;
689 case '\r':
690 OS << "'\\r'";
691 break;
692 case '\t':
693 OS << "'\\t'";
694 break;
695 case '\v':
696 OS << "'\\v'";
697 break;
698 default:
Ted Kremenek652d18e2008-02-23 00:52:04 +0000699 if (value < 256 && isprint(value)) {
Chris Lattner666115c2007-07-13 23:58:20 +0000700 OS << "'" << (char)value << "'";
701 } else if (value < 256) {
Benjamin Kramer49038022012-02-04 13:45:25 +0000702 OS << "'\\x";
703 OS.write_hex(value) << "'";
Chris Lattner666115c2007-07-13 23:58:20 +0000704 } else {
705 // FIXME what to really do here?
706 OS << value;
707 }
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000708 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000709}
710
Steve Naroffdf7855b2007-02-21 23:46:25 +0000711void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000712 bool isSigned = Node->getType()->isSignedIntegerType();
713 OS << Node->getValue().toString(10, isSigned);
Mike Stump11289f42009-09-09 15:08:12 +0000714
Chris Lattner06430412007-05-21 05:45:03 +0000715 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall9dd450b2009-09-21 23:43:11 +0000716 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
David Blaikie83d382b2011-09-23 05:06:16 +0000717 default: llvm_unreachable("Unexpected type for integer literal!");
Richard Trieu8b626ba2011-11-07 18:40:31 +0000718 // FIXME: The Short and UShort cases are to handle cases where a short
719 // integeral literal is formed during template instantiation. They should
720 // be removed when template instantiation no longer needs integer literals.
721 case BuiltinType::Short:
722 case BuiltinType::UShort:
Chris Lattner06430412007-05-21 05:45:03 +0000723 case BuiltinType::Int: break; // no suffix.
724 case BuiltinType::UInt: OS << 'U'; break;
725 case BuiltinType::Long: OS << 'L'; break;
726 case BuiltinType::ULong: OS << "UL"; break;
727 case BuiltinType::LongLong: OS << "LL"; break;
728 case BuiltinType::ULongLong: OS << "ULL"; break;
Richard Trieu8b626ba2011-11-07 18:40:31 +0000729 case BuiltinType::Int128: OS << "i128"; break;
730 case BuiltinType::UInt128: OS << "Ui128"; break;
Chris Lattner06430412007-05-21 05:45:03 +0000731 }
Chris Lattner882f7882006-11-04 18:52:07 +0000732}
Steve Naroffab624882007-02-21 22:05:47 +0000733void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000734 SmallString<16> Str;
Eli Friedman53392062011-10-05 20:32:03 +0000735 Node->getValue().toString(Str);
736 OS << Str;
Chris Lattner882f7882006-11-04 18:52:07 +0000737}
Chris Lattner1c20a172007-08-26 03:42:43 +0000738
739void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
740 PrintExpr(Node->getSubExpr());
741 OS << "i";
742}
743
Steve Naroffdf7855b2007-02-21 23:46:25 +0000744void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Richard Trieudc355912012-06-13 20:25:24 +0000745 Str->outputString(OS);
Chris Lattner882f7882006-11-04 18:52:07 +0000746}
747void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
748 OS << "(";
749 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000750 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000751}
752void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +0000753 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +0000754 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump11289f42009-09-09 15:08:12 +0000755
Eli Friedman1cf25362009-06-14 22:39:26 +0000756 // Print a space if this is an "identifier operator" like __real, or if
757 // it might be concatenated incorrectly like '+'.
Chris Lattnere5b60442007-08-23 21:46:40 +0000758 switch (Node->getOpcode()) {
759 default: break;
John McCalle3027922010-08-25 11:45:40 +0000760 case UO_Real:
761 case UO_Imag:
762 case UO_Extension:
Chris Lattnere5b60442007-08-23 21:46:40 +0000763 OS << ' ';
764 break;
John McCalle3027922010-08-25 11:45:40 +0000765 case UO_Plus:
766 case UO_Minus:
Eli Friedman1cf25362009-06-14 22:39:26 +0000767 if (isa<UnaryOperator>(Node->getSubExpr()))
768 OS << ' ';
769 break;
Chris Lattnere5b60442007-08-23 21:46:40 +0000770 }
771 }
Chris Lattner882f7882006-11-04 18:52:07 +0000772 PrintExpr(Node->getSubExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000773
Chris Lattner15768702006-11-05 23:54:51 +0000774 if (Node->isPostfix())
775 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000776}
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000777
Douglas Gregor882211c2010-04-28 22:16:22 +0000778void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
779 OS << "__builtin_offsetof(";
Daniel Dunbar219fa692010-06-30 19:16:48 +0000780 OS << Node->getTypeSourceInfo()->getType().getAsString(Policy) << ", ";
Douglas Gregor882211c2010-04-28 22:16:22 +0000781 bool PrintedSomething = false;
782 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
783 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
784 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
785 // Array node
786 OS << "[";
787 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
788 OS << "]";
789 PrintedSomething = true;
790 continue;
791 }
Douglas Gregord1702062010-04-29 00:18:15 +0000792
793 // Skip implicit base indirections.
794 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
795 continue;
796
Douglas Gregor882211c2010-04-28 22:16:22 +0000797 // Field or identifier node.
798 IdentifierInfo *Id = ON.getFieldName();
799 if (!Id)
800 continue;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000801
Douglas Gregor882211c2010-04-28 22:16:22 +0000802 if (PrintedSomething)
803 OS << ".";
804 else
805 PrintedSomething = true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000806 OS << Id->getName();
Douglas Gregor882211c2010-04-28 22:16:22 +0000807 }
808 OS << ")";
809}
810
Peter Collingbournee190dee2011-03-11 19:24:49 +0000811void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
812 switch(Node->getKind()) {
813 case UETT_SizeOf:
814 OS << "sizeof";
815 break;
816 case UETT_AlignOf:
817 OS << "__alignof";
818 break;
819 case UETT_VecStep:
820 OS << "vec_step";
821 break;
822 }
Sebastian Redl6f282892008-11-11 17:56:53 +0000823 if (Node->isArgumentType())
Daniel Dunbar219fa692010-06-30 19:16:48 +0000824 OS << "(" << Node->getArgumentType().getAsString(Policy) << ")";
Sebastian Redl6f282892008-11-11 17:56:53 +0000825 else {
826 OS << " ";
827 PrintExpr(Node->getArgumentExpr());
828 }
Chris Lattner882f7882006-11-04 18:52:07 +0000829}
Peter Collingbourne91147592011-04-15 00:35:48 +0000830
831void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
832 OS << "_Generic(";
833 PrintExpr(Node->getControllingExpr());
834 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
835 OS << ", ";
836 QualType T = Node->getAssocType(i);
837 if (T.isNull())
838 OS << "default";
839 else
840 OS << T.getAsString(Policy);
841 OS << ": ";
842 PrintExpr(Node->getAssocExpr(i));
843 }
844 OS << ")";
845}
846
Chris Lattner882f7882006-11-04 18:52:07 +0000847void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000848 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000849 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +0000850 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000851 OS << "]";
852}
853
Peter Collingbourne4b279a02011-02-08 21:17:54 +0000854void StmtPrinter::PrintCallArgs(CallExpr *Call) {
Chris Lattner882f7882006-11-04 18:52:07 +0000855 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000856 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
857 // Don't print any defaulted arguments
858 break;
859 }
860
Chris Lattner882f7882006-11-04 18:52:07 +0000861 if (i) OS << ", ";
862 PrintExpr(Call->getArg(i));
863 }
Peter Collingbourne4b279a02011-02-08 21:17:54 +0000864}
865
866void StmtPrinter::VisitCallExpr(CallExpr *Call) {
867 PrintExpr(Call->getCallee());
868 OS << "(";
869 PrintCallArgs(Call);
Chris Lattner882f7882006-11-04 18:52:07 +0000870 OS << ")";
871}
872void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregore4b414c2009-01-08 22:45:41 +0000873 // FIXME: Suppress printing implicit bases (like "this")
874 PrintExpr(Node->getBase());
Fariborz Jahanian2990c022010-01-11 21:17:32 +0000875 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
876 if (FD->isAnonymousStructOrUnion())
877 return;
Douglas Gregore4b414c2009-01-08 22:45:41 +0000878 OS << (Node->isArrow() ? "->" : ".");
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000879 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
880 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000881 if (Node->hasTemplateKeyword())
882 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000883 OS << Node->getMemberNameInfo();
John McCallb3774b52010-08-19 23:49:38 +0000884 if (Node->hasExplicitTemplateArgs())
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000885 OS << TemplateSpecializationType::PrintTemplateArgumentList(
886 Node->getTemplateArgs(),
887 Node->getNumTemplateArgs(),
888 Policy);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000889}
Steve Naroffe87026a2009-07-24 17:54:45 +0000890void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
891 PrintExpr(Node->getBase());
892 OS << (Node->isArrow() ? "->isa" : ".isa");
893}
894
Nate Begemance4d7fc2008-04-18 23:10:10 +0000895void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +0000896 PrintExpr(Node->getBase());
897 OS << ".";
898 OS << Node->getAccessor().getName();
899}
Douglas Gregorf19b2312008-10-28 15:36:24 +0000900void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Fariborz Jahaniane33c1162010-06-30 16:31:08 +0000901 OS << "(" << Node->getType().getAsString(Policy) << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000902 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000903}
Steve Naroff57eb2c52007-07-19 21:32:11 +0000904void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +0000905 OS << "(" << Node->getType().getAsString(Policy) << ")";
Steve Naroff57eb2c52007-07-19 21:32:11 +0000906 PrintExpr(Node->getInitializer());
907}
Steve Naroff7a5af782007-07-13 16:58:59 +0000908void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000909 // No need to print anything, simply forward to the sub expression.
910 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +0000911}
Chris Lattner882f7882006-11-04 18:52:07 +0000912void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
913 PrintExpr(Node->getLHS());
914 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
915 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000916}
Chris Lattner86928112007-08-25 02:00:02 +0000917void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
918 PrintExpr(Node->getLHS());
919 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
920 PrintExpr(Node->getRHS());
921}
Chris Lattner882f7882006-11-04 18:52:07 +0000922void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
923 PrintExpr(Node->getCond());
John McCallc07a0c72011-02-17 10:25:35 +0000924 OS << " ? ";
925 PrintExpr(Node->getLHS());
926 OS << " : ";
Chris Lattner882f7882006-11-04 18:52:07 +0000927 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000928}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000929
Chris Lattnereefa10e2007-05-28 06:56:27 +0000930// GNU extensions.
931
John McCallc07a0c72011-02-17 10:25:35 +0000932void
933StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
934 PrintExpr(Node->getCommon());
935 OS << " ?: ";
936 PrintExpr(Node->getFalseExpr());
937}
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000938void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000939 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000940}
941
Chris Lattner366727f2007-07-24 16:58:17 +0000942void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
943 OS << "(";
944 PrintRawCompoundStmt(E->getSubStmt());
945 OS << ")";
946}
947
Steve Naroff9efdabc2007-08-03 21:21:27 +0000948void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
949 OS << "__builtin_choose_expr(";
950 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +0000951 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000952 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +0000953 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000954 PrintExpr(Node->getRHS());
955 OS << ")";
956}
Chris Lattner366727f2007-07-24 16:58:17 +0000957
Douglas Gregor3be4b122008-11-29 04:51:27 +0000958void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
959 OS << "__null";
960}
961
Eli Friedmana1b4ed82008-05-14 19:38:39 +0000962void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
963 OS << "__builtin_shufflevector(";
964 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
965 if (i) OS << ", ";
966 PrintExpr(Node->getExpr(i));
967 }
968 OS << ")";
969}
970
Anders Carlsson4692db02007-08-31 04:56:16 +0000971void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor36098ff2009-05-30 00:56:08 +0000972 if (Node->getSyntacticForm()) {
973 Visit(Node->getSyntacticForm());
974 return;
975 }
976
Anders Carlsson4692db02007-08-31 04:56:16 +0000977 OS << "{ ";
978 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
979 if (i) OS << ", ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000980 if (Node->getInit(i))
981 PrintExpr(Node->getInit(i));
982 else
983 OS << "0";
Anders Carlsson4692db02007-08-31 04:56:16 +0000984 }
985 OS << " }";
986}
987
Nate Begeman5ec4b312009-08-10 23:49:36 +0000988void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
989 OS << "( ";
990 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
991 if (i) OS << ", ";
992 PrintExpr(Node->getExpr(i));
993 }
994 OS << " )";
995}
996
Douglas Gregore4a0bb72009-01-22 00:58:24 +0000997void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000998 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
999 DEnd = Node->designators_end();
1000 D != DEnd; ++D) {
1001 if (D->isFieldDesignator()) {
1002 if (D->getDotLoc().isInvalid())
1003 OS << D->getFieldName()->getName() << ":";
1004 else
1005 OS << "." << D->getFieldName()->getName();
1006 } else {
1007 OS << "[";
1008 if (D->isArrayDesignator()) {
1009 PrintExpr(Node->getArrayIndex(*D));
1010 } else {
1011 PrintExpr(Node->getArrayRangeStart(*D));
1012 OS << " ... ";
Mike Stump11289f42009-09-09 15:08:12 +00001013 PrintExpr(Node->getArrayRangeEnd(*D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001014 }
1015 OS << "]";
1016 }
1017 }
1018
1019 OS << " = ";
1020 PrintExpr(Node->getInit());
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001021}
1022
Douglas Gregor0202cb42009-01-29 17:44:32 +00001023void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Chris Lattnerc61089a2009-06-30 01:26:17 +00001024 if (Policy.LangOpts.CPlusPlus)
Douglas Gregor278f52e2009-05-30 00:08:05 +00001025 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()";
1026 else {
1027 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")";
1028 if (Node->getType()->isRecordType())
1029 OS << "{}";
1030 else
1031 OS << 0;
1032 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001033}
1034
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001035void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman79635842009-05-30 04:20:30 +00001036 OS << "__builtin_va_arg(";
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001037 PrintExpr(Node->getSubExpr());
1038 OS << ", ";
Daniel Dunbar219fa692010-06-30 19:16:48 +00001039 OS << Node->getType().getAsString(Policy);
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001040 OS << ")";
1041}
1042
John McCallfe96e0b2011-11-06 09:01:30 +00001043void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1044 PrintExpr(Node->getSyntacticForm());
1045}
1046
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001047void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
Eli Friedmanc2025562011-10-11 20:00:47 +00001048 const char *Name = 0;
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001049 switch (Node->getOp()) {
Richard Smithfeea8832012-04-12 05:08:17 +00001050#define BUILTIN(ID, TYPE, ATTRS)
1051#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1052 case AtomicExpr::AO ## ID: \
1053 Name = #ID "("; \
1054 break;
1055#include "clang/Basic/Builtins.def"
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001056 }
1057 OS << Name;
Richard Smithfeea8832012-04-12 05:08:17 +00001058
1059 // AtomicExpr stores its subexpressions in a permuted order.
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001060 PrintExpr(Node->getPtr());
1061 OS << ", ";
Richard Smithfeea8832012-04-12 05:08:17 +00001062 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1063 Node->getOp() != AtomicExpr::AO__atomic_load_n) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001064 PrintExpr(Node->getVal1());
1065 OS << ", ";
1066 }
Richard Smithfeea8832012-04-12 05:08:17 +00001067 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1068 Node->isCmpXChg()) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001069 PrintExpr(Node->getVal2());
1070 OS << ", ";
1071 }
Richard Smithfeea8832012-04-12 05:08:17 +00001072 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1073 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
1074 PrintExpr(Node->getWeak());
1075 OS << ", ";
1076 }
1077 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init)
David Chisnallfa35df62012-01-16 17:27:18 +00001078 PrintExpr(Node->getOrder());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001079 if (Node->isCmpXChg()) {
1080 OS << ", ";
1081 PrintExpr(Node->getOrderFail());
1082 }
1083 OS << ")";
1084}
1085
Chris Lattnereefa10e2007-05-28 06:56:27 +00001086// C++
Douglas Gregor993603d2008-11-14 16:09:21 +00001087void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1088 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1089 "",
1090#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1091 Spelling,
1092#include "clang/Basic/OperatorKinds.def"
1093 };
1094
1095 OverloadedOperatorKind Kind = Node->getOperator();
1096 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1097 if (Node->getNumArgs() == 1) {
1098 OS << OpStrings[Kind] << ' ';
1099 PrintExpr(Node->getArg(0));
1100 } else {
1101 PrintExpr(Node->getArg(0));
1102 OS << ' ' << OpStrings[Kind];
1103 }
1104 } else if (Kind == OO_Call) {
1105 PrintExpr(Node->getArg(0));
1106 OS << '(';
1107 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1108 if (ArgIdx > 1)
1109 OS << ", ";
1110 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1111 PrintExpr(Node->getArg(ArgIdx));
1112 }
1113 OS << ')';
1114 } else if (Kind == OO_Subscript) {
1115 PrintExpr(Node->getArg(0));
1116 OS << '[';
1117 PrintExpr(Node->getArg(1));
1118 OS << ']';
1119 } else if (Node->getNumArgs() == 1) {
1120 OS << OpStrings[Kind] << ' ';
1121 PrintExpr(Node->getArg(0));
1122 } else if (Node->getNumArgs() == 2) {
1123 PrintExpr(Node->getArg(0));
1124 OS << ' ' << OpStrings[Kind] << ' ';
1125 PrintExpr(Node->getArg(1));
1126 } else {
David Blaikie83d382b2011-09-23 05:06:16 +00001127 llvm_unreachable("unknown overloaded operator");
Douglas Gregor993603d2008-11-14 16:09:21 +00001128 }
1129}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001130
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001131void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1132 VisitCallExpr(cast<CallExpr>(Node));
1133}
1134
Peter Collingbourne41f85462011-02-09 21:07:24 +00001135void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1136 PrintExpr(Node->getCallee());
1137 OS << "<<<";
1138 PrintCallArgs(Node->getConfig());
1139 OS << ">>>(";
1140 PrintCallArgs(Node);
1141 OS << ")";
1142}
1143
Douglas Gregore200adc2008-10-27 19:41:14 +00001144void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1145 OS << Node->getCastName() << '<';
Daniel Dunbar219fa692010-06-30 19:16:48 +00001146 OS << Node->getTypeAsWritten().getAsString(Policy) << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +00001147 PrintExpr(Node->getSubExpr());
1148 OS << ")";
1149}
1150
Douglas Gregore200adc2008-10-27 19:41:14 +00001151void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1152 VisitCXXNamedCastExpr(Node);
1153}
1154
1155void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1156 VisitCXXNamedCastExpr(Node);
1157}
1158
1159void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1160 VisitCXXNamedCastExpr(Node);
1161}
1162
1163void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1164 VisitCXXNamedCastExpr(Node);
1165}
1166
Sebastian Redlc4704762008-11-11 11:37:55 +00001167void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1168 OS << "typeid(";
1169 if (Node->isTypeOperand()) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001170 OS << Node->getTypeOperand().getAsString(Policy);
Sebastian Redlc4704762008-11-11 11:37:55 +00001171 } else {
1172 PrintExpr(Node->getExprOperand());
1173 }
1174 OS << ")";
1175}
1176
Francois Pichet9f4f2072010-09-08 12:20:18 +00001177void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1178 OS << "__uuidof(";
1179 if (Node->isTypeOperand()) {
1180 OS << Node->getTypeOperand().getAsString(Policy);
1181 } else {
1182 PrintExpr(Node->getExprOperand());
1183 }
1184 OS << ")";
1185}
1186
Richard Smithc67fdd42012-03-07 08:35:16 +00001187void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1188 switch (Node->getLiteralOperatorKind()) {
1189 case UserDefinedLiteral::LOK_Raw:
Richard Smith29e95952012-03-09 10:10:02 +00001190 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
Richard Smithc67fdd42012-03-07 08:35:16 +00001191 break;
1192 case UserDefinedLiteral::LOK_Template: {
Richard Smith29e95952012-03-09 10:10:02 +00001193 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1194 const TemplateArgumentList *Args =
1195 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1196 assert(Args);
1197 const TemplateArgument &Pack = Args->get(0);
1198 for (TemplateArgument::pack_iterator I = Pack.pack_begin(),
1199 E = Pack.pack_end(); I != E; ++I) {
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001200 char C = (char)I->getAsIntegral().getZExtValue();
Richard Smithc67fdd42012-03-07 08:35:16 +00001201 OS << C;
1202 }
1203 break;
1204 }
Richard Smith75025ba2012-03-08 09:02:38 +00001205 case UserDefinedLiteral::LOK_Integer: {
1206 // Print integer literal without suffix.
1207 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1208 OS << Int->getValue().toString(10, /*isSigned*/false);
1209 break;
1210 }
Richard Smithc67fdd42012-03-07 08:35:16 +00001211 case UserDefinedLiteral::LOK_Floating:
1212 case UserDefinedLiteral::LOK_String:
1213 case UserDefinedLiteral::LOK_Character:
1214 PrintExpr(Node->getCookedLiteral());
1215 break;
1216 }
1217 OS << Node->getUDSuffix()->getName();
1218}
1219
Chris Lattnereefa10e2007-05-28 06:56:27 +00001220void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1221 OS << (Node->getValue() ? "true" : "false");
1222}
1223
Sebastian Redl576fd422009-05-10 18:38:11 +00001224void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1225 OS << "nullptr";
1226}
1227
Douglas Gregor97a9c812008-11-04 14:32:21 +00001228void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1229 OS << "this";
1230}
1231
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001232void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1233 if (Node->getSubExpr() == 0)
1234 OS << "throw";
1235 else {
1236 OS << "throw ";
1237 PrintExpr(Node->getSubExpr());
1238 }
1239}
1240
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001241void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1242 // Nothing to print: we picked up the default argument
1243}
1244
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001245void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001246 OS << Node->getType().getAsString(Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001247 OS << "(";
1248 PrintExpr(Node->getSubExpr());
1249 OS << ")";
1250}
1251
Anders Carlsson993a4b32009-05-30 20:03:25 +00001252void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1253 PrintExpr(Node->getSubExpr());
1254}
1255
Douglas Gregordd04d332009-01-16 18:33:17 +00001256void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001257 OS << Node->getType().getAsString(Policy);
Douglas Gregordd04d332009-01-16 18:33:17 +00001258 OS << "(";
1259 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001260 ArgEnd = Node->arg_end();
Douglas Gregordd04d332009-01-16 18:33:17 +00001261 Arg != ArgEnd; ++Arg) {
1262 if (Arg != Node->arg_begin())
1263 OS << ", ";
1264 PrintExpr(*Arg);
1265 }
1266 OS << ")";
1267}
1268
Douglas Gregore31e6062012-02-07 10:09:13 +00001269void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1270 OS << '[';
1271 bool NeedComma = false;
1272 switch (Node->getCaptureDefault()) {
1273 case LCD_None:
1274 break;
1275
1276 case LCD_ByCopy:
1277 OS << '=';
1278 NeedComma = true;
1279 break;
1280
1281 case LCD_ByRef:
1282 OS << '&';
1283 NeedComma = true;
1284 break;
1285 }
1286 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1287 CEnd = Node->explicit_capture_end();
1288 C != CEnd;
1289 ++C) {
1290 if (NeedComma)
1291 OS << ", ";
1292 NeedComma = true;
1293
1294 switch (C->getCaptureKind()) {
1295 case LCK_This:
1296 OS << "this";
1297 break;
1298
1299 case LCK_ByRef:
1300 if (Node->getCaptureDefault() != LCD_ByRef)
1301 OS << '&';
1302 OS << C->getCapturedVar()->getName();
1303 break;
1304
1305 case LCK_ByCopy:
1306 if (Node->getCaptureDefault() != LCD_ByCopy)
1307 OS << '=';
1308 OS << C->getCapturedVar()->getName();
1309 break;
1310 }
1311 }
1312 OS << ']';
1313
1314 if (Node->hasExplicitParameters()) {
1315 OS << " (";
1316 CXXMethodDecl *Method = Node->getCallOperator();
1317 NeedComma = false;
1318 for (CXXMethodDecl::param_iterator P = Method->param_begin(),
1319 PEnd = Method->param_end();
1320 P != PEnd; ++P) {
1321 if (NeedComma) {
1322 OS << ", ";
1323 } else {
1324 NeedComma = true;
1325 }
1326 std::string ParamStr = (*P)->getNameAsString();
1327 (*P)->getOriginalType().getAsStringInternal(ParamStr, Policy);
1328 OS << ParamStr;
1329 }
1330 if (Method->isVariadic()) {
1331 if (NeedComma)
1332 OS << ", ";
1333 OS << "...";
1334 }
1335 OS << ')';
1336
1337 if (Node->isMutable())
1338 OS << " mutable";
1339
1340 const FunctionProtoType *Proto
1341 = Method->getType()->getAs<FunctionProtoType>();
1342 {
1343 std::string ExceptionSpec;
1344 Proto->printExceptionSpecification(ExceptionSpec, Policy);
1345 OS << ExceptionSpec;
1346 }
1347
1348 // FIXME: Attributes
1349
Douglas Gregor0c46b2b2012-02-13 22:00:16 +00001350 // Print the trailing return type if it was specified in the source.
1351 if (Node->hasExplicitResultType())
1352 OS << " -> " << Proto->getResultType().getAsString(Policy);
Douglas Gregore31e6062012-02-07 10:09:13 +00001353 }
1354
1355 // Print the body.
1356 CompoundStmt *Body = Node->getBody();
1357 OS << ' ';
1358 PrintStmt(Body);
1359}
1360
Douglas Gregor747eb782010-07-08 06:14:04 +00001361void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregor2b88c112010-09-08 00:15:04 +00001362 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
1363 OS << TSInfo->getType().getAsString(Policy) << "()";
1364 else
1365 OS << Node->getType().getAsString(Policy) << "()";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001366}
1367
Sebastian Redlbd150f42008-11-21 19:14:01 +00001368void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1369 if (E->isGlobalNew())
1370 OS << "::";
1371 OS << "new ";
1372 unsigned NumPlace = E->getNumPlacementArgs();
1373 if (NumPlace > 0) {
1374 OS << "(";
1375 PrintExpr(E->getPlacementArg(0));
1376 for (unsigned i = 1; i < NumPlace; ++i) {
1377 OS << ", ";
1378 PrintExpr(E->getPlacementArg(i));
1379 }
1380 OS << ") ";
1381 }
1382 if (E->isParenTypeId())
1383 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001384 std::string TypeS;
1385 if (Expr *Size = E->getArraySize()) {
1386 llvm::raw_string_ostream s(TypeS);
Eli Friedmanc4fc8392009-05-30 05:32:46 +00001387 Size->printPretty(s, Context, Helper, Policy);
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001388 s.flush();
1389 TypeS = "[" + TypeS + "]";
1390 }
Douglas Gregor7de59662009-05-29 20:38:28 +00001391 E->getAllocatedType().getAsStringInternal(TypeS, Policy);
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001392 OS << TypeS;
Sebastian Redlbd150f42008-11-21 19:14:01 +00001393 if (E->isParenTypeId())
1394 OS << ")";
1395
Sebastian Redl6047f072012-02-16 12:22:20 +00001396 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
1397 if (InitStyle) {
1398 if (InitStyle == CXXNewExpr::CallInit)
1399 OS << "(";
1400 PrintExpr(E->getInitializer());
1401 if (InitStyle == CXXNewExpr::CallInit)
1402 OS << ")";
Sebastian Redlbd150f42008-11-21 19:14:01 +00001403 }
1404}
1405
1406void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1407 if (E->isGlobalDelete())
1408 OS << "::";
1409 OS << "delete ";
1410 if (E->isArrayForm())
1411 OS << "[] ";
1412 PrintExpr(E->getArgument());
1413}
1414
Douglas Gregorad8a3362009-09-04 17:36:40 +00001415void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1416 PrintExpr(E->getBase());
1417 if (E->isArrow())
1418 OS << "->";
1419 else
1420 OS << '.';
1421 if (E->getQualifier())
1422 E->getQualifier()->print(OS, Policy);
Mike Stump11289f42009-09-09 15:08:12 +00001423
Douglas Gregorad8a3362009-09-04 17:36:40 +00001424 std::string TypeS;
Douglas Gregor678f90d2010-02-25 01:56:36 +00001425 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1426 OS << II->getName();
1427 else
1428 E->getDestroyedType().getAsStringInternal(TypeS, Policy);
Douglas Gregorad8a3362009-09-04 17:36:40 +00001429 OS << TypeS;
1430}
1431
Anders Carlsson0781ce72009-04-23 02:32:43 +00001432void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Douglas Gregorbaba85d2011-01-24 17:25:03 +00001433 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1434 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1435 // Don't print any defaulted arguments
1436 break;
1437 }
1438
1439 if (i) OS << ", ";
1440 PrintExpr(E->getArg(i));
Fariborz Jahaniand0bbf662010-01-13 21:41:11 +00001441 }
Anders Carlsson0781ce72009-04-23 02:32:43 +00001442}
1443
John McCall5d413782010-12-06 08:20:24 +00001444void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Anders Carlssondefc6442009-04-24 22:47:04 +00001445 // Just forward to the sub expression.
1446 PrintExpr(E->getSubExpr());
1447}
1448
Mike Stump11289f42009-09-09 15:08:12 +00001449void
Douglas Gregorce934142009-05-20 18:46:25 +00001450StmtPrinter::VisitCXXUnresolvedConstructExpr(
1451 CXXUnresolvedConstructExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001452 OS << Node->getTypeAsWritten().getAsString(Policy);
Douglas Gregorce934142009-05-20 18:46:25 +00001453 OS << "(";
1454 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001455 ArgEnd = Node->arg_end();
Douglas Gregorce934142009-05-20 18:46:25 +00001456 Arg != ArgEnd; ++Arg) {
1457 if (Arg != Node->arg_begin())
1458 OS << ", ";
1459 PrintExpr(*Arg);
1460 }
1461 OS << ")";
1462}
1463
John McCall8cd78132009-11-19 22:55:06 +00001464void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1465 CXXDependentScopeMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001466 if (!Node->isImplicitAccess()) {
1467 PrintExpr(Node->getBase());
1468 OS << (Node->isArrow() ? "->" : ".");
1469 }
Douglas Gregorc26e0f62009-09-03 16:14:30 +00001470 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1471 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001472 if (Node->hasTemplateKeyword())
Douglas Gregor308047d2009-09-09 00:23:06 +00001473 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001474 OS << Node->getMemberNameInfo();
John McCall2d74de92009-12-01 22:10:20 +00001475 if (Node->hasExplicitTemplateArgs()) {
Douglas Gregor308047d2009-09-09 00:23:06 +00001476 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1477 Node->getTemplateArgs(),
1478 Node->getNumTemplateArgs(),
1479 Policy);
1480 }
Douglas Gregora8db9542009-05-22 21:13:27 +00001481}
1482
John McCall10eae182009-11-30 22:42:35 +00001483void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001484 if (!Node->isImplicitAccess()) {
1485 PrintExpr(Node->getBase());
1486 OS << (Node->isArrow() ? "->" : ".");
1487 }
John McCall10eae182009-11-30 22:42:35 +00001488 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1489 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001490 if (Node->hasTemplateKeyword())
1491 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001492 OS << Node->getMemberNameInfo();
John McCall10eae182009-11-30 22:42:35 +00001493 if (Node->hasExplicitTemplateArgs()) {
1494 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1495 Node->getTemplateArgs(),
1496 Node->getNumTemplateArgs(),
1497 Policy);
1498 }
1499}
1500
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001501static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1502 switch (UTT) {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001503 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001504 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
John Wiegley65497cc2011-04-27 23:09:49 +00001505 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001506 case UTT_HasTrivialAssign: return "__has_trivial_assign";
Alexis Huntf479f1b2011-05-09 18:22:59 +00001507 case UTT_HasTrivialDefaultConstructor: return "__has_trivial_constructor";
John Wiegley65497cc2011-04-27 23:09:49 +00001508 case UTT_HasTrivialCopy: return "__has_trivial_copy";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001509 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1510 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1511 case UTT_IsAbstract: return "__is_abstract";
John Wiegley65497cc2011-04-27 23:09:49 +00001512 case UTT_IsArithmetic: return "__is_arithmetic";
1513 case UTT_IsArray: return "__is_array";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001514 case UTT_IsClass: return "__is_class";
John Wiegley65497cc2011-04-27 23:09:49 +00001515 case UTT_IsCompleteType: return "__is_complete_type";
1516 case UTT_IsCompound: return "__is_compound";
1517 case UTT_IsConst: return "__is_const";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001518 case UTT_IsEmpty: return "__is_empty";
1519 case UTT_IsEnum: return "__is_enum";
Douglas Gregordca70af2011-12-03 18:14:24 +00001520 case UTT_IsFinal: return "__is_final";
John Wiegley65497cc2011-04-27 23:09:49 +00001521 case UTT_IsFloatingPoint: return "__is_floating_point";
1522 case UTT_IsFunction: return "__is_function";
1523 case UTT_IsFundamental: return "__is_fundamental";
1524 case UTT_IsIntegral: return "__is_integral";
Chandler Carruth8e2d6f42011-05-01 07:23:20 +00001525 case UTT_IsLiteral: return "__is_literal";
John Wiegley65497cc2011-04-27 23:09:49 +00001526 case UTT_IsLvalueReference: return "__is_lvalue_reference";
1527 case UTT_IsMemberFunctionPointer: return "__is_member_function_pointer";
1528 case UTT_IsMemberObjectPointer: return "__is_member_object_pointer";
1529 case UTT_IsMemberPointer: return "__is_member_pointer";
1530 case UTT_IsObject: return "__is_object";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001531 case UTT_IsPOD: return "__is_pod";
John Wiegley65497cc2011-04-27 23:09:49 +00001532 case UTT_IsPointer: return "__is_pointer";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001533 case UTT_IsPolymorphic: return "__is_polymorphic";
John Wiegley65497cc2011-04-27 23:09:49 +00001534 case UTT_IsReference: return "__is_reference";
John Wiegley65497cc2011-04-27 23:09:49 +00001535 case UTT_IsRvalueReference: return "__is_rvalue_reference";
1536 case UTT_IsScalar: return "__is_scalar";
1537 case UTT_IsSigned: return "__is_signed";
1538 case UTT_IsStandardLayout: return "__is_standard_layout";
1539 case UTT_IsTrivial: return "__is_trivial";
Alexis Huntd9a5cc12011-05-13 00:31:07 +00001540 case UTT_IsTriviallyCopyable: return "__is_trivially_copyable";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001541 case UTT_IsUnion: return "__is_union";
John Wiegley65497cc2011-04-27 23:09:49 +00001542 case UTT_IsUnsigned: return "__is_unsigned";
1543 case UTT_IsVoid: return "__is_void";
1544 case UTT_IsVolatile: return "__is_volatile";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001545 }
Chandler Carruth8e2d6f42011-05-01 07:23:20 +00001546 llvm_unreachable("Type trait not covered by switch statement");
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001547}
1548
1549static const char *getTypeTraitName(BinaryTypeTrait BTT) {
1550 switch (BTT) {
Douglas Gregor1be329d2012-02-23 07:33:15 +00001551 case BTT_IsBaseOf: return "__is_base_of";
1552 case BTT_IsConvertible: return "__is_convertible";
1553 case BTT_IsSame: return "__is_same";
1554 case BTT_TypeCompatible: return "__builtin_types_compatible_p";
1555 case BTT_IsConvertibleTo: return "__is_convertible_to";
1556 case BTT_IsTriviallyAssignable: return "__is_trivially_assignable";
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001557 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001558 llvm_unreachable("Binary type trait not covered by switch");
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001559}
1560
Douglas Gregor29c42f22012-02-24 07:38:34 +00001561static const char *getTypeTraitName(TypeTrait TT) {
1562 switch (TT) {
1563 case clang::TT_IsTriviallyConstructible:return "__is_trivially_constructible";
1564 }
1565 llvm_unreachable("Type trait not covered by switch");
1566}
1567
John Wiegley6242b6a2011-04-28 00:16:57 +00001568static const char *getTypeTraitName(ArrayTypeTrait ATT) {
1569 switch (ATT) {
1570 case ATT_ArrayRank: return "__array_rank";
1571 case ATT_ArrayExtent: return "__array_extent";
1572 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001573 llvm_unreachable("Array type trait not covered by switch");
John Wiegley6242b6a2011-04-28 00:16:57 +00001574}
1575
John Wiegleyf9f65842011-04-25 06:54:41 +00001576static const char *getExpressionTraitName(ExpressionTrait ET) {
1577 switch (ET) {
John Wiegleyf9f65842011-04-25 06:54:41 +00001578 case ET_IsLValueExpr: return "__is_lvalue_expr";
1579 case ET_IsRValueExpr: return "__is_rvalue_expr";
1580 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001581 llvm_unreachable("Expression type trait not covered by switch");
John Wiegleyf9f65842011-04-25 06:54:41 +00001582}
1583
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001584void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1585 OS << getTypeTraitName(E->getTrait()) << "("
Daniel Dunbar219fa692010-06-30 19:16:48 +00001586 << E->getQueriedType().getAsString(Policy) << ")";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001587}
1588
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001589void StmtPrinter::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
1590 OS << getTypeTraitName(E->getTrait()) << "("
1591 << E->getLhsType().getAsString(Policy) << ","
1592 << E->getRhsType().getAsString(Policy) << ")";
1593}
1594
Douglas Gregor29c42f22012-02-24 07:38:34 +00001595void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
1596 OS << getTypeTraitName(E->getTrait()) << "(";
1597 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
1598 if (I > 0)
1599 OS << ", ";
1600 OS << E->getArg(I)->getType().getAsString(Policy);
1601 }
1602 OS << ")";
1603}
1604
John Wiegley6242b6a2011-04-28 00:16:57 +00001605void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
1606 OS << getTypeTraitName(E->getTrait()) << "("
1607 << E->getQueriedType().getAsString(Policy) << ")";
1608}
1609
John Wiegleyf9f65842011-04-25 06:54:41 +00001610void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
1611 OS << getExpressionTraitName(E->getTrait()) << "(";
1612 PrintExpr(E->getQueriedExpression());
1613 OS << ")";
1614}
1615
Sebastian Redl4202c0f2010-09-10 20:55:43 +00001616void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1617 OS << "noexcept(";
1618 PrintExpr(E->getOperand());
1619 OS << ")";
1620}
1621
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001622void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregore8e9dd62011-01-03 17:17:50 +00001623 PrintExpr(E->getPattern());
1624 OS << "...";
1625}
1626
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001627void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001628 OS << "sizeof...(" << *E->getPack() << ")";
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001629}
1630
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001631void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
1632 SubstNonTypeTemplateParmPackExpr *Node) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001633 OS << *Node->getParameterPack();
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001634}
1635
John McCall7c454bb2011-07-15 05:09:51 +00001636void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
1637 SubstNonTypeTemplateParmExpr *Node) {
1638 Visit(Node->getReplacement());
1639}
1640
Douglas Gregorfe314812011-06-21 17:03:29 +00001641void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
1642 PrintExpr(Node->GetTemporaryExpr());
1643}
1644
Mike Stump11289f42009-09-09 15:08:12 +00001645// Obj-C
Anders Carlsson76f4a902007-08-21 17:43:55 +00001646
1647void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1648 OS << "@";
1649 VisitStringLiteral(Node->getString());
1650}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001651
Patrick Beard0caa3942012-04-19 00:25:12 +00001652void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001653 OS << "@";
Patrick Beard0caa3942012-04-19 00:25:12 +00001654 Visit(E->getSubExpr());
Ted Kremeneke65b0862012-03-06 20:05:56 +00001655}
1656
1657void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
1658 OS << "@[ ";
1659 StmtRange ch = E->children();
1660 if (ch.first != ch.second) {
1661 while (1) {
1662 Visit(*ch.first);
1663 ++ch.first;
1664 if (ch.first == ch.second) break;
1665 OS << ", ";
1666 }
1667 }
1668 OS << " ]";
1669}
1670
1671void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
1672 OS << "@{ ";
1673 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
1674 if (I > 0)
1675 OS << ", ";
1676
1677 ObjCDictionaryElement Element = E->getKeyValueElement(I);
1678 Visit(Element.Key);
1679 OS << " : ";
1680 Visit(Element.Value);
1681 if (Element.isPackExpansion())
1682 OS << "...";
1683 }
1684 OS << " }";
1685}
1686
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001687void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001688 OS << "@encode(" << Node->getEncodedType().getAsString(Policy) << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001689}
1690
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001691void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001692 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001693}
1694
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001695void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001696 OS << "@protocol(" << *Node->getProtocol() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001697}
1698
Steve Naroffd54978b2007-09-18 23:55:05 +00001699void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1700 OS << "[";
Douglas Gregor9a129192010-04-21 00:45:42 +00001701 switch (Mess->getReceiverKind()) {
1702 case ObjCMessageExpr::Instance:
1703 PrintExpr(Mess->getInstanceReceiver());
1704 break;
1705
1706 case ObjCMessageExpr::Class:
1707 OS << Mess->getClassReceiver().getAsString(Policy);
1708 break;
1709
1710 case ObjCMessageExpr::SuperInstance:
1711 case ObjCMessageExpr::SuperClass:
1712 OS << "Super";
1713 break;
1714 }
1715
Ted Kremeneka06e7122008-05-02 17:32:38 +00001716 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00001717 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001718 if (selector.isUnarySelector()) {
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00001719 OS << selector.getNameForSlot(0);
Steve Naroffc6814ea2007-10-02 20:01:56 +00001720 } else {
1721 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001722 if (i < selector.getNumArgs()) {
1723 if (i > 0) OS << ' ';
1724 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001725 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00001726 else
1727 OS << ":";
1728 }
1729 else OS << ", "; // Handle variadic methods.
Mike Stump11289f42009-09-09 15:08:12 +00001730
Steve Naroffc6814ea2007-10-02 20:01:56 +00001731 PrintExpr(Mess->getArg(i));
1732 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001733 }
1734 OS << "]";
1735}
1736
Ted Kremeneke65b0862012-03-06 20:05:56 +00001737void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
1738 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
1739}
1740
John McCall31168b02011-06-15 23:02:42 +00001741void
1742StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
1743 PrintExpr(E->getSubExpr());
1744}
1745
1746void
1747StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
1748 OS << "(" << E->getBridgeKindName() << E->getType().getAsString(Policy)
1749 << ")";
1750 PrintExpr(E->getSubExpr());
1751}
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001752
Steve Naroffc540d662008-09-03 18:15:37 +00001753void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001754 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00001755 OS << "^";
Mike Stump11289f42009-09-09 15:08:12 +00001756
Steve Naroffc540d662008-09-03 18:15:37 +00001757 const FunctionType *AFT = Node->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00001758
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001759 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroffc540d662008-09-03 18:15:37 +00001760 OS << "()";
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001761 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00001762 OS << '(';
1763 std::string ParamStr;
Steve Naroff415d3d52008-10-08 17:01:13 +00001764 for (BlockDecl::param_iterator AI = BD->param_begin(),
1765 E = BD->param_end(); AI != E; ++AI) {
1766 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001767 ParamStr = (*AI)->getNameAsString();
Douglas Gregor7de59662009-05-29 20:38:28 +00001768 (*AI)->getType().getAsStringInternal(ParamStr, Policy);
Steve Naroffc540d662008-09-03 18:15:37 +00001769 OS << ParamStr;
1770 }
Mike Stump11289f42009-09-09 15:08:12 +00001771
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001772 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00001773 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001774 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00001775 OS << "...";
1776 }
1777 OS << ')';
1778 }
1779}
1780
Ted Kremenekf551f322011-11-30 22:08:08 +00001781void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
1782 PrintExpr(Node->getSourceExpr());
1783}
John McCall8d69a212010-11-15 23:31:06 +00001784
Tanya Lattner55808c12011-06-04 00:47:47 +00001785void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
1786 OS << "__builtin_astype(";
1787 PrintExpr(Node->getSrcExpr());
1788 OS << ", " << Node->getType().getAsString();
1789 OS << ")";
1790}
1791
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001792//===----------------------------------------------------------------------===//
1793// Stmt method implementations
1794//===----------------------------------------------------------------------===//
1795
Eli Friedmanef334fd2009-05-30 05:03:24 +00001796void Stmt::dumpPretty(ASTContext& Context) const {
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +00001797 printPretty(llvm::errs(), Context, 0,
David Blaikiebbafb8a2012-03-11 07:00:24 +00001798 PrintingPolicy(Context.getLangOpts()));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001799}
1800
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001801void Stmt::printPretty(raw_ostream &OS, ASTContext& Context,
Eli Friedmanef334fd2009-05-30 05:03:24 +00001802 PrinterHelper* Helper,
Douglas Gregor7de59662009-05-29 20:38:28 +00001803 const PrintingPolicy &Policy,
1804 unsigned Indentation) const {
Chris Lattner882f7882006-11-04 18:52:07 +00001805 if (this == 0) {
1806 OS << "<NULL>";
1807 return;
1808 }
1809
Douglas Gregor8655e882009-10-16 22:46:09 +00001810 if (Policy.Dump && &Context) {
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +00001811 dump(OS, Context.getSourceManager());
Douglas Gregor278f52e2009-05-30 00:08:05 +00001812 return;
1813 }
Mike Stump11289f42009-09-09 15:08:12 +00001814
Eli Friedmanef334fd2009-05-30 05:03:24 +00001815 StmtPrinter P(OS, Context, Helper, Policy, Indentation);
Chris Lattner62249a62007-08-21 04:04:25 +00001816 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001817}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001818
1819//===----------------------------------------------------------------------===//
1820// PrinterHelper
1821//===----------------------------------------------------------------------===//
1822
1823// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +00001824PrinterHelper::~PrinterHelper() {}