blob: af8e5c792a450b8590e1de33995961e065fb3f7d [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
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000432void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000433 Indent() << "@try";
434 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
435 PrintRawCompoundStmt(TS);
436 OS << "\n";
437 }
Mike Stump11289f42009-09-09 15:08:12 +0000438
Douglas Gregor96c79492010-04-23 22:50:49 +0000439 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
440 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000441 Indent() << "@catch(";
Steve Naroff371b8fb2009-03-03 19:52:17 +0000442 if (catchStmt->getCatchParamDecl()) {
443 if (Decl *DS = catchStmt->getCatchParamDecl())
444 PrintRawDecl(DS);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000445 }
446 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000447 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
448 PrintRawCompoundStmt(CS);
449 OS << "\n";
450 }
Fariborz Jahanian88157952007-11-02 18:16:07 +0000451 }
Mike Stump11289f42009-09-09 15:08:12 +0000452
453 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
454 Node->getFinallyStmt())) {
Fariborz Jahaniandefbf9a2007-11-07 00:46:42 +0000455 Indent() << "@finally";
456 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian88157952007-11-02 18:16:07 +0000457 OS << "\n";
Mike Stump11289f42009-09-09 15:08:12 +0000458 }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000459}
460
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000461void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000462}
463
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000464void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000465 Indent() << "@catch (...) { /* todo */ } \n";
466}
467
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000468void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000469 Indent() << "@throw";
470 if (Node->getThrowExpr()) {
471 OS << " ";
472 PrintExpr(Node->getThrowExpr());
473 }
474 OS << ";\n";
475}
476
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000477void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000478 Indent() << "@synchronized (";
479 PrintExpr(Node->getSynchExpr());
480 OS << ")";
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000481 PrintRawCompoundStmt(Node->getSynchBody());
482 OS << "\n";
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000483}
484
John McCall31168b02011-06-15 23:02:42 +0000485void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
486 Indent() << "@autoreleasepool";
487 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
488 OS << "\n";
489}
490
Sebastian Redl9b244a82008-12-22 21:35:02 +0000491void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
492 OS << "catch (";
Sebastian Redl54c04d42008-12-22 19:15:10 +0000493 if (Decl *ExDecl = Node->getExceptionDecl())
494 PrintRawDecl(ExDecl);
495 else
496 OS << "...";
497 OS << ") ";
498 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl9b244a82008-12-22 21:35:02 +0000499}
500
501void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
502 Indent();
503 PrintRawCXXCatchStmt(Node);
504 OS << "\n";
505}
506
507void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
508 Indent() << "try ";
509 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump11289f42009-09-09 15:08:12 +0000510 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl9b244a82008-12-22 21:35:02 +0000511 OS << " ";
512 PrintRawCXXCatchStmt(Node->getHandler(i));
513 }
Sebastian Redl54c04d42008-12-22 19:15:10 +0000514 OS << "\n";
515}
516
John Wiegley1c0675e2011-04-28 01:08:34 +0000517void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
518 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
519 PrintRawCompoundStmt(Node->getTryBlock());
520 SEHExceptStmt *E = Node->getExceptHandler();
521 SEHFinallyStmt *F = Node->getFinallyHandler();
522 if(E)
523 PrintRawSEHExceptHandler(E);
524 else {
525 assert(F && "Must have a finally block...");
526 PrintRawSEHFinallyStmt(F);
527 }
528 OS << "\n";
529}
530
531void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
532 OS << "__finally ";
533 PrintRawCompoundStmt(Node->getBlock());
534 OS << "\n";
535}
536
537void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
538 OS << "__except (";
539 VisitExpr(Node->getFilterExpr());
540 OS << ")\n";
541 PrintRawCompoundStmt(Node->getBlock());
542 OS << "\n";
543}
544
545void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
546 Indent();
547 PrintRawSEHExceptHandler(Node);
548 OS << "\n";
549}
550
551void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
552 Indent();
553 PrintRawSEHFinallyStmt(Node);
554 OS << "\n";
555}
556
Chris Lattner71e23ce2006-11-04 20:18:38 +0000557//===----------------------------------------------------------------------===//
558// Expr printing methods.
559//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000560
Chris Lattner882f7882006-11-04 18:52:07 +0000561void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000562 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
563 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000564 if (Node->hasTemplateKeyword())
565 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000566 OS << Node->getNameInfo();
John McCallb3774b52010-08-19 23:49:38 +0000567 if (Node->hasExplicitTemplateArgs())
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000568 OS << TemplateSpecializationType::PrintTemplateArgumentList(
569 Node->getTemplateArgs(),
570 Node->getNumTemplateArgs(),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000571 Policy);
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000572}
573
John McCall8cd78132009-11-19 22:55:06 +0000574void StmtPrinter::VisitDependentScopeDeclRefExpr(
575 DependentScopeDeclRefExpr *Node) {
Axel Naumann20b27862011-01-24 15:44:00 +0000576 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
577 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000578 if (Node->hasTemplateKeyword())
579 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000580 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000581 if (Node->hasExplicitTemplateArgs())
582 OS << TemplateSpecializationType::PrintTemplateArgumentList(
583 Node->getTemplateArgs(),
584 Node->getNumTemplateArgs(),
585 Policy);
Douglas Gregor90a1a652009-03-19 17:26:29 +0000586}
587
John McCalld14a8642009-11-21 08:51:07 +0000588void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregora727cb92009-06-30 22:34:41 +0000589 if (Node->getQualifier())
590 Node->getQualifier()->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000591 if (Node->hasTemplateKeyword())
592 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000593 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000594 if (Node->hasExplicitTemplateArgs())
595 OS << TemplateSpecializationType::PrintTemplateArgumentList(
596 Node->getTemplateArgs(),
Douglas Gregora727cb92009-06-30 22:34:41 +0000597 Node->getNumTemplateArgs(),
John McCalle66edc12009-11-24 19:00:30 +0000598 Policy);
Douglas Gregora727cb92009-06-30 22:34:41 +0000599}
600
Steve Naroffe46504b2007-11-12 14:29:37 +0000601void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000602 if (Node->getBase()) {
603 PrintExpr(Node->getBase());
604 OS << (Node->isArrow() ? "->" : ".");
605 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000606 OS << *Node->getDecl();
Steve Naroffe46504b2007-11-12 14:29:37 +0000607}
608
Steve Naroffec944032008-05-30 00:40:33 +0000609void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000610 if (Node->isSuperReceiver())
611 OS << "super.";
612 else if (Node->getBase()) {
Steve Naroffec944032008-05-30 00:40:33 +0000613 PrintExpr(Node->getBase());
614 OS << ".";
615 }
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000616
John McCallb7bd14f2010-12-02 01:19:52 +0000617 if (Node->isImplicitProperty())
618 OS << Node->getImplicitPropertyGetter()->getSelector().getAsString();
619 else
620 OS << Node->getExplicitProperty()->getName();
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000621}
622
Ted Kremeneke65b0862012-03-06 20:05:56 +0000623void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
624
625 PrintExpr(Node->getBaseExpr());
626 OS << "[";
627 PrintExpr(Node->getKeyExpr());
628 OS << "]";
629}
630
Chris Lattner6307f192008-08-10 01:53:14 +0000631void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson625bfc82007-07-21 05:21:51 +0000632 switch (Node->getIdentType()) {
633 default:
David Blaikie83d382b2011-09-23 05:06:16 +0000634 llvm_unreachable("unknown case");
Chris Lattner6307f192008-08-10 01:53:14 +0000635 case PredefinedExpr::Func:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000636 OS << "__func__";
637 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000638 case PredefinedExpr::Function:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000639 OS << "__FUNCTION__";
640 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000641 case PredefinedExpr::PrettyFunction:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000642 OS << "__PRETTY_FUNCTION__";
643 break;
644 }
645}
646
Steve Naroffae4143e2007-04-26 20:39:23 +0000647void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000648 unsigned value = Node->getValue();
Douglas Gregorfb65e592011-07-27 05:40:30 +0000649
650 switch (Node->getKind()) {
651 case CharacterLiteral::Ascii: break; // no prefix.
652 case CharacterLiteral::Wide: OS << 'L'; break;
653 case CharacterLiteral::UTF16: OS << 'u'; break;
654 case CharacterLiteral::UTF32: OS << 'U'; break;
655 }
656
Chris Lattner666115c2007-07-13 23:58:20 +0000657 switch (value) {
658 case '\\':
659 OS << "'\\\\'";
660 break;
661 case '\'':
662 OS << "'\\''";
663 break;
664 case '\a':
665 // TODO: K&R: the meaning of '\\a' is different in traditional C
666 OS << "'\\a'";
667 break;
668 case '\b':
669 OS << "'\\b'";
670 break;
671 // Nonstandard escape sequence.
672 /*case '\e':
673 OS << "'\\e'";
674 break;*/
675 case '\f':
676 OS << "'\\f'";
677 break;
678 case '\n':
679 OS << "'\\n'";
680 break;
681 case '\r':
682 OS << "'\\r'";
683 break;
684 case '\t':
685 OS << "'\\t'";
686 break;
687 case '\v':
688 OS << "'\\v'";
689 break;
690 default:
Ted Kremenek652d18e2008-02-23 00:52:04 +0000691 if (value < 256 && isprint(value)) {
Chris Lattner666115c2007-07-13 23:58:20 +0000692 OS << "'" << (char)value << "'";
693 } else if (value < 256) {
Benjamin Kramer49038022012-02-04 13:45:25 +0000694 OS << "'\\x";
695 OS.write_hex(value) << "'";
Chris Lattner666115c2007-07-13 23:58:20 +0000696 } else {
697 // FIXME what to really do here?
698 OS << value;
699 }
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000700 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000701}
702
Steve Naroffdf7855b2007-02-21 23:46:25 +0000703void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000704 bool isSigned = Node->getType()->isSignedIntegerType();
705 OS << Node->getValue().toString(10, isSigned);
Mike Stump11289f42009-09-09 15:08:12 +0000706
Chris Lattner06430412007-05-21 05:45:03 +0000707 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall9dd450b2009-09-21 23:43:11 +0000708 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
David Blaikie83d382b2011-09-23 05:06:16 +0000709 default: llvm_unreachable("Unexpected type for integer literal!");
Richard Trieu8b626ba2011-11-07 18:40:31 +0000710 // FIXME: The Short and UShort cases are to handle cases where a short
711 // integeral literal is formed during template instantiation. They should
712 // be removed when template instantiation no longer needs integer literals.
713 case BuiltinType::Short:
714 case BuiltinType::UShort:
Chris Lattner06430412007-05-21 05:45:03 +0000715 case BuiltinType::Int: break; // no suffix.
716 case BuiltinType::UInt: OS << 'U'; break;
717 case BuiltinType::Long: OS << 'L'; break;
718 case BuiltinType::ULong: OS << "UL"; break;
719 case BuiltinType::LongLong: OS << "LL"; break;
720 case BuiltinType::ULongLong: OS << "ULL"; break;
Richard Trieu8b626ba2011-11-07 18:40:31 +0000721 case BuiltinType::Int128: OS << "i128"; break;
722 case BuiltinType::UInt128: OS << "Ui128"; break;
Chris Lattner06430412007-05-21 05:45:03 +0000723 }
Chris Lattner882f7882006-11-04 18:52:07 +0000724}
Steve Naroffab624882007-02-21 22:05:47 +0000725void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000726 SmallString<16> Str;
Eli Friedman53392062011-10-05 20:32:03 +0000727 Node->getValue().toString(Str);
728 OS << Str;
Chris Lattner882f7882006-11-04 18:52:07 +0000729}
Chris Lattner1c20a172007-08-26 03:42:43 +0000730
731void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
732 PrintExpr(Node->getSubExpr());
733 OS << "i";
734}
735
Steve Naroffdf7855b2007-02-21 23:46:25 +0000736void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Douglas Gregorfb65e592011-07-27 05:40:30 +0000737 switch (Str->getKind()) {
738 case StringLiteral::Ascii: break; // no prefix.
739 case StringLiteral::Wide: OS << 'L'; break;
740 case StringLiteral::UTF8: OS << "u8"; break;
741 case StringLiteral::UTF16: OS << 'u'; break;
742 case StringLiteral::UTF32: OS << 'U'; break;
743 }
Chris Lattner5d8f4942006-11-04 20:29:31 +0000744 OS << '"';
Richard Smith88eb4d32011-12-30 23:37:31 +0000745 static char Hex[] = "0123456789ABCDEF";
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000746
Richard Smith7ba85c32012-04-05 00:17:44 +0000747 unsigned LastSlashX = Str->getLength();
Richard Smith88eb4d32011-12-30 23:37:31 +0000748 for (unsigned I = 0, N = Str->getLength(); I != N; ++I) {
749 switch (uint32_t Char = Str->getCodeUnit(I)) {
Chris Lattnerbb96cc42009-01-16 19:25:18 +0000750 default:
Richard Smith7ba85c32012-04-05 00:17:44 +0000751 // FIXME: Convert UTF-8 back to codepoints before rendering.
752
753 // Convert UTF-16 surrogate pairs back to codepoints before rendering.
754 // Leave invalid surrogates alone; we'll use \x for those.
755 if (Str->getKind() == StringLiteral::UTF16 && I != N - 1 &&
756 Char >= 0xd800 && Char <= 0xdbff) {
757 uint32_t Trail = Str->getCodeUnit(I + 1);
758 if (Trail >= 0xdc00 && Trail <= 0xdfff) {
759 Char = 0x10000 + ((Char - 0xd800) << 10) + (Trail - 0xdc00);
760 ++I;
761 }
762 }
763
Richard Smith88eb4d32011-12-30 23:37:31 +0000764 if (Char > 0xff) {
Richard Smith7ba85c32012-04-05 00:17:44 +0000765 // If this is a wide string, output characters over 0xff using \x
766 // escapes. Otherwise, this is a UTF-16 or UTF-32 string, and Char is a
767 // codepoint: use \x escapes for invalid codepoints.
768 if (Str->getKind() == StringLiteral::Wide ||
769 (Char >= 0xd800 && Char <= 0xdfff) || Char >= 0x110000) {
770 // FIXME: Is this the best way to print wchar_t?
771 OS << "\\x";
772 int Shift = 28;
773 while ((Char >> Shift) == 0)
774 Shift -= 4;
775 for (/**/; Shift >= 0; Shift -= 4)
776 OS << Hex[(Char >> Shift) & 15];
777 LastSlashX = I;
778 break;
779 }
780
Richard Smith88eb4d32011-12-30 23:37:31 +0000781 if (Char > 0xffff)
782 OS << "\\U00"
783 << Hex[(Char >> 20) & 15]
784 << Hex[(Char >> 16) & 15];
785 else
786 OS << "\\u";
787 OS << Hex[(Char >> 12) & 15]
788 << Hex[(Char >> 8) & 15]
789 << Hex[(Char >> 4) & 15]
790 << Hex[(Char >> 0) & 15];
791 break;
792 }
Richard Smith7ba85c32012-04-05 00:17:44 +0000793
794 // If we used \x... for the previous character, and this character is a
795 // hexadecimal digit, prevent it being slurped as part of the \x.
796 if (LastSlashX + 1 == I) {
797 switch (Char) {
798 case '0': case '1': case '2': case '3': case '4':
799 case '5': case '6': case '7': case '8': case '9':
800 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
801 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
802 OS << "\"\"";
803 }
804 }
805
Richard Smith88eb4d32011-12-30 23:37:31 +0000806 if (Char <= 0xff && isprint(Char))
Chris Lattnerbb96cc42009-01-16 19:25:18 +0000807 OS << (char)Char;
808 else // Output anything hard as an octal escape.
809 OS << '\\'
Richard Smith7ba85c32012-04-05 00:17:44 +0000810 << (char)('0' + ((Char >> 6) & 7))
811 << (char)('0' + ((Char >> 3) & 7))
812 << (char)('0' + ((Char >> 0) & 7));
Chris Lattnerbb96cc42009-01-16 19:25:18 +0000813 break;
814 // Handle some common non-printable cases to make dumps prettier.
Chris Lattner5d8f4942006-11-04 20:29:31 +0000815 case '\\': OS << "\\\\"; break;
816 case '"': OS << "\\\""; break;
817 case '\n': OS << "\\n"; break;
818 case '\t': OS << "\\t"; break;
819 case '\a': OS << "\\a"; break;
820 case '\b': OS << "\\b"; break;
821 }
822 }
823 OS << '"';
Chris Lattner882f7882006-11-04 18:52:07 +0000824}
825void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
826 OS << "(";
827 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000828 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000829}
830void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +0000831 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +0000832 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump11289f42009-09-09 15:08:12 +0000833
Eli Friedman1cf25362009-06-14 22:39:26 +0000834 // Print a space if this is an "identifier operator" like __real, or if
835 // it might be concatenated incorrectly like '+'.
Chris Lattnere5b60442007-08-23 21:46:40 +0000836 switch (Node->getOpcode()) {
837 default: break;
John McCalle3027922010-08-25 11:45:40 +0000838 case UO_Real:
839 case UO_Imag:
840 case UO_Extension:
Chris Lattnere5b60442007-08-23 21:46:40 +0000841 OS << ' ';
842 break;
John McCalle3027922010-08-25 11:45:40 +0000843 case UO_Plus:
844 case UO_Minus:
Eli Friedman1cf25362009-06-14 22:39:26 +0000845 if (isa<UnaryOperator>(Node->getSubExpr()))
846 OS << ' ';
847 break;
Chris Lattnere5b60442007-08-23 21:46:40 +0000848 }
849 }
Chris Lattner882f7882006-11-04 18:52:07 +0000850 PrintExpr(Node->getSubExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000851
Chris Lattner15768702006-11-05 23:54:51 +0000852 if (Node->isPostfix())
853 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000854}
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000855
Douglas Gregor882211c2010-04-28 22:16:22 +0000856void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
857 OS << "__builtin_offsetof(";
Daniel Dunbar219fa692010-06-30 19:16:48 +0000858 OS << Node->getTypeSourceInfo()->getType().getAsString(Policy) << ", ";
Douglas Gregor882211c2010-04-28 22:16:22 +0000859 bool PrintedSomething = false;
860 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
861 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
862 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
863 // Array node
864 OS << "[";
865 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
866 OS << "]";
867 PrintedSomething = true;
868 continue;
869 }
Douglas Gregord1702062010-04-29 00:18:15 +0000870
871 // Skip implicit base indirections.
872 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
873 continue;
874
Douglas Gregor882211c2010-04-28 22:16:22 +0000875 // Field or identifier node.
876 IdentifierInfo *Id = ON.getFieldName();
877 if (!Id)
878 continue;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000879
Douglas Gregor882211c2010-04-28 22:16:22 +0000880 if (PrintedSomething)
881 OS << ".";
882 else
883 PrintedSomething = true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000884 OS << Id->getName();
Douglas Gregor882211c2010-04-28 22:16:22 +0000885 }
886 OS << ")";
887}
888
Peter Collingbournee190dee2011-03-11 19:24:49 +0000889void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
890 switch(Node->getKind()) {
891 case UETT_SizeOf:
892 OS << "sizeof";
893 break;
894 case UETT_AlignOf:
895 OS << "__alignof";
896 break;
897 case UETT_VecStep:
898 OS << "vec_step";
899 break;
900 }
Sebastian Redl6f282892008-11-11 17:56:53 +0000901 if (Node->isArgumentType())
Daniel Dunbar219fa692010-06-30 19:16:48 +0000902 OS << "(" << Node->getArgumentType().getAsString(Policy) << ")";
Sebastian Redl6f282892008-11-11 17:56:53 +0000903 else {
904 OS << " ";
905 PrintExpr(Node->getArgumentExpr());
906 }
Chris Lattner882f7882006-11-04 18:52:07 +0000907}
Peter Collingbourne91147592011-04-15 00:35:48 +0000908
909void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
910 OS << "_Generic(";
911 PrintExpr(Node->getControllingExpr());
912 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
913 OS << ", ";
914 QualType T = Node->getAssocType(i);
915 if (T.isNull())
916 OS << "default";
917 else
918 OS << T.getAsString(Policy);
919 OS << ": ";
920 PrintExpr(Node->getAssocExpr(i));
921 }
922 OS << ")";
923}
924
Chris Lattner882f7882006-11-04 18:52:07 +0000925void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000926 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000927 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +0000928 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000929 OS << "]";
930}
931
Peter Collingbourne4b279a02011-02-08 21:17:54 +0000932void StmtPrinter::PrintCallArgs(CallExpr *Call) {
Chris Lattner882f7882006-11-04 18:52:07 +0000933 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000934 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
935 // Don't print any defaulted arguments
936 break;
937 }
938
Chris Lattner882f7882006-11-04 18:52:07 +0000939 if (i) OS << ", ";
940 PrintExpr(Call->getArg(i));
941 }
Peter Collingbourne4b279a02011-02-08 21:17:54 +0000942}
943
944void StmtPrinter::VisitCallExpr(CallExpr *Call) {
945 PrintExpr(Call->getCallee());
946 OS << "(";
947 PrintCallArgs(Call);
Chris Lattner882f7882006-11-04 18:52:07 +0000948 OS << ")";
949}
950void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregore4b414c2009-01-08 22:45:41 +0000951 // FIXME: Suppress printing implicit bases (like "this")
952 PrintExpr(Node->getBase());
Fariborz Jahanian2990c022010-01-11 21:17:32 +0000953 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
954 if (FD->isAnonymousStructOrUnion())
955 return;
Douglas Gregore4b414c2009-01-08 22:45:41 +0000956 OS << (Node->isArrow() ? "->" : ".");
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000957 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
958 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000959 if (Node->hasTemplateKeyword())
960 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000961 OS << Node->getMemberNameInfo();
John McCallb3774b52010-08-19 23:49:38 +0000962 if (Node->hasExplicitTemplateArgs())
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000963 OS << TemplateSpecializationType::PrintTemplateArgumentList(
964 Node->getTemplateArgs(),
965 Node->getNumTemplateArgs(),
966 Policy);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000967}
Steve Naroffe87026a2009-07-24 17:54:45 +0000968void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
969 PrintExpr(Node->getBase());
970 OS << (Node->isArrow() ? "->isa" : ".isa");
971}
972
Nate Begemance4d7fc2008-04-18 23:10:10 +0000973void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +0000974 PrintExpr(Node->getBase());
975 OS << ".";
976 OS << Node->getAccessor().getName();
977}
Douglas Gregorf19b2312008-10-28 15:36:24 +0000978void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Fariborz Jahaniane33c1162010-06-30 16:31:08 +0000979 OS << "(" << Node->getType().getAsString(Policy) << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000980 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000981}
Steve Naroff57eb2c52007-07-19 21:32:11 +0000982void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +0000983 OS << "(" << Node->getType().getAsString(Policy) << ")";
Steve Naroff57eb2c52007-07-19 21:32:11 +0000984 PrintExpr(Node->getInitializer());
985}
Steve Naroff7a5af782007-07-13 16:58:59 +0000986void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000987 // No need to print anything, simply forward to the sub expression.
988 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +0000989}
Chris Lattner882f7882006-11-04 18:52:07 +0000990void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
991 PrintExpr(Node->getLHS());
992 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
993 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000994}
Chris Lattner86928112007-08-25 02:00:02 +0000995void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
996 PrintExpr(Node->getLHS());
997 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
998 PrintExpr(Node->getRHS());
999}
Chris Lattner882f7882006-11-04 18:52:07 +00001000void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1001 PrintExpr(Node->getCond());
John McCallc07a0c72011-02-17 10:25:35 +00001002 OS << " ? ";
1003 PrintExpr(Node->getLHS());
1004 OS << " : ";
Chris Lattner882f7882006-11-04 18:52:07 +00001005 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001006}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001007
Chris Lattnereefa10e2007-05-28 06:56:27 +00001008// GNU extensions.
1009
John McCallc07a0c72011-02-17 10:25:35 +00001010void
1011StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1012 PrintExpr(Node->getCommon());
1013 OS << " ?: ";
1014 PrintExpr(Node->getFalseExpr());
1015}
Chris Lattnerd268a7a2007-08-03 17:31:20 +00001016void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +00001017 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +00001018}
1019
Chris Lattner366727f2007-07-24 16:58:17 +00001020void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1021 OS << "(";
1022 PrintRawCompoundStmt(E->getSubStmt());
1023 OS << ")";
1024}
1025
Steve Naroff9efdabc2007-08-03 21:21:27 +00001026void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1027 OS << "__builtin_choose_expr(";
1028 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +00001029 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +00001030 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +00001031 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +00001032 PrintExpr(Node->getRHS());
1033 OS << ")";
1034}
Chris Lattner366727f2007-07-24 16:58:17 +00001035
Douglas Gregor3be4b122008-11-29 04:51:27 +00001036void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1037 OS << "__null";
1038}
1039
Eli Friedmana1b4ed82008-05-14 19:38:39 +00001040void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1041 OS << "__builtin_shufflevector(";
1042 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1043 if (i) OS << ", ";
1044 PrintExpr(Node->getExpr(i));
1045 }
1046 OS << ")";
1047}
1048
Anders Carlsson4692db02007-08-31 04:56:16 +00001049void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001050 if (Node->getSyntacticForm()) {
1051 Visit(Node->getSyntacticForm());
1052 return;
1053 }
1054
Anders Carlsson4692db02007-08-31 04:56:16 +00001055 OS << "{ ";
1056 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1057 if (i) OS << ", ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001058 if (Node->getInit(i))
1059 PrintExpr(Node->getInit(i));
1060 else
1061 OS << "0";
Anders Carlsson4692db02007-08-31 04:56:16 +00001062 }
1063 OS << " }";
1064}
1065
Nate Begeman5ec4b312009-08-10 23:49:36 +00001066void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1067 OS << "( ";
1068 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1069 if (i) OS << ", ";
1070 PrintExpr(Node->getExpr(i));
1071 }
1072 OS << " )";
1073}
1074
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001075void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001076 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
1077 DEnd = Node->designators_end();
1078 D != DEnd; ++D) {
1079 if (D->isFieldDesignator()) {
1080 if (D->getDotLoc().isInvalid())
1081 OS << D->getFieldName()->getName() << ":";
1082 else
1083 OS << "." << D->getFieldName()->getName();
1084 } else {
1085 OS << "[";
1086 if (D->isArrayDesignator()) {
1087 PrintExpr(Node->getArrayIndex(*D));
1088 } else {
1089 PrintExpr(Node->getArrayRangeStart(*D));
1090 OS << " ... ";
Mike Stump11289f42009-09-09 15:08:12 +00001091 PrintExpr(Node->getArrayRangeEnd(*D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001092 }
1093 OS << "]";
1094 }
1095 }
1096
1097 OS << " = ";
1098 PrintExpr(Node->getInit());
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001099}
1100
Douglas Gregor0202cb42009-01-29 17:44:32 +00001101void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Chris Lattnerc61089a2009-06-30 01:26:17 +00001102 if (Policy.LangOpts.CPlusPlus)
Douglas Gregor278f52e2009-05-30 00:08:05 +00001103 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()";
1104 else {
1105 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")";
1106 if (Node->getType()->isRecordType())
1107 OS << "{}";
1108 else
1109 OS << 0;
1110 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001111}
1112
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001113void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman79635842009-05-30 04:20:30 +00001114 OS << "__builtin_va_arg(";
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001115 PrintExpr(Node->getSubExpr());
1116 OS << ", ";
Daniel Dunbar219fa692010-06-30 19:16:48 +00001117 OS << Node->getType().getAsString(Policy);
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001118 OS << ")";
1119}
1120
John McCallfe96e0b2011-11-06 09:01:30 +00001121void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1122 PrintExpr(Node->getSyntacticForm());
1123}
1124
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001125void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
Eli Friedmanc2025562011-10-11 20:00:47 +00001126 const char *Name = 0;
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001127 switch (Node->getOp()) {
Richard Smithfeea8832012-04-12 05:08:17 +00001128#define BUILTIN(ID, TYPE, ATTRS)
1129#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1130 case AtomicExpr::AO ## ID: \
1131 Name = #ID "("; \
1132 break;
1133#include "clang/Basic/Builtins.def"
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001134 }
1135 OS << Name;
Richard Smithfeea8832012-04-12 05:08:17 +00001136
1137 // AtomicExpr stores its subexpressions in a permuted order.
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001138 PrintExpr(Node->getPtr());
1139 OS << ", ";
Richard Smithfeea8832012-04-12 05:08:17 +00001140 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1141 Node->getOp() != AtomicExpr::AO__atomic_load_n) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001142 PrintExpr(Node->getVal1());
1143 OS << ", ";
1144 }
Richard Smithfeea8832012-04-12 05:08:17 +00001145 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1146 Node->isCmpXChg()) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001147 PrintExpr(Node->getVal2());
1148 OS << ", ";
1149 }
Richard Smithfeea8832012-04-12 05:08:17 +00001150 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1151 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
1152 PrintExpr(Node->getWeak());
1153 OS << ", ";
1154 }
1155 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init)
David Chisnallfa35df62012-01-16 17:27:18 +00001156 PrintExpr(Node->getOrder());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001157 if (Node->isCmpXChg()) {
1158 OS << ", ";
1159 PrintExpr(Node->getOrderFail());
1160 }
1161 OS << ")";
1162}
1163
Chris Lattnereefa10e2007-05-28 06:56:27 +00001164// C++
Douglas Gregor993603d2008-11-14 16:09:21 +00001165void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1166 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1167 "",
1168#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1169 Spelling,
1170#include "clang/Basic/OperatorKinds.def"
1171 };
1172
1173 OverloadedOperatorKind Kind = Node->getOperator();
1174 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1175 if (Node->getNumArgs() == 1) {
1176 OS << OpStrings[Kind] << ' ';
1177 PrintExpr(Node->getArg(0));
1178 } else {
1179 PrintExpr(Node->getArg(0));
1180 OS << ' ' << OpStrings[Kind];
1181 }
1182 } else if (Kind == OO_Call) {
1183 PrintExpr(Node->getArg(0));
1184 OS << '(';
1185 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1186 if (ArgIdx > 1)
1187 OS << ", ";
1188 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1189 PrintExpr(Node->getArg(ArgIdx));
1190 }
1191 OS << ')';
1192 } else if (Kind == OO_Subscript) {
1193 PrintExpr(Node->getArg(0));
1194 OS << '[';
1195 PrintExpr(Node->getArg(1));
1196 OS << ']';
1197 } else if (Node->getNumArgs() == 1) {
1198 OS << OpStrings[Kind] << ' ';
1199 PrintExpr(Node->getArg(0));
1200 } else if (Node->getNumArgs() == 2) {
1201 PrintExpr(Node->getArg(0));
1202 OS << ' ' << OpStrings[Kind] << ' ';
1203 PrintExpr(Node->getArg(1));
1204 } else {
David Blaikie83d382b2011-09-23 05:06:16 +00001205 llvm_unreachable("unknown overloaded operator");
Douglas Gregor993603d2008-11-14 16:09:21 +00001206 }
1207}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001208
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001209void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1210 VisitCallExpr(cast<CallExpr>(Node));
1211}
1212
Peter Collingbourne41f85462011-02-09 21:07:24 +00001213void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1214 PrintExpr(Node->getCallee());
1215 OS << "<<<";
1216 PrintCallArgs(Node->getConfig());
1217 OS << ">>>(";
1218 PrintCallArgs(Node);
1219 OS << ")";
1220}
1221
Douglas Gregore200adc2008-10-27 19:41:14 +00001222void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1223 OS << Node->getCastName() << '<';
Daniel Dunbar219fa692010-06-30 19:16:48 +00001224 OS << Node->getTypeAsWritten().getAsString(Policy) << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +00001225 PrintExpr(Node->getSubExpr());
1226 OS << ")";
1227}
1228
Douglas Gregore200adc2008-10-27 19:41:14 +00001229void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1230 VisitCXXNamedCastExpr(Node);
1231}
1232
1233void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1234 VisitCXXNamedCastExpr(Node);
1235}
1236
1237void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1238 VisitCXXNamedCastExpr(Node);
1239}
1240
1241void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1242 VisitCXXNamedCastExpr(Node);
1243}
1244
Sebastian Redlc4704762008-11-11 11:37:55 +00001245void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1246 OS << "typeid(";
1247 if (Node->isTypeOperand()) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001248 OS << Node->getTypeOperand().getAsString(Policy);
Sebastian Redlc4704762008-11-11 11:37:55 +00001249 } else {
1250 PrintExpr(Node->getExprOperand());
1251 }
1252 OS << ")";
1253}
1254
Francois Pichet9f4f2072010-09-08 12:20:18 +00001255void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1256 OS << "__uuidof(";
1257 if (Node->isTypeOperand()) {
1258 OS << Node->getTypeOperand().getAsString(Policy);
1259 } else {
1260 PrintExpr(Node->getExprOperand());
1261 }
1262 OS << ")";
1263}
1264
Richard Smithc67fdd42012-03-07 08:35:16 +00001265void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1266 switch (Node->getLiteralOperatorKind()) {
1267 case UserDefinedLiteral::LOK_Raw:
Richard Smith29e95952012-03-09 10:10:02 +00001268 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
Richard Smithc67fdd42012-03-07 08:35:16 +00001269 break;
1270 case UserDefinedLiteral::LOK_Template: {
Richard Smith29e95952012-03-09 10:10:02 +00001271 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1272 const TemplateArgumentList *Args =
1273 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1274 assert(Args);
1275 const TemplateArgument &Pack = Args->get(0);
1276 for (TemplateArgument::pack_iterator I = Pack.pack_begin(),
1277 E = Pack.pack_end(); I != E; ++I) {
1278 char C = (char)I->getAsIntegral()->getZExtValue();
Richard Smithc67fdd42012-03-07 08:35:16 +00001279 OS << C;
1280 }
1281 break;
1282 }
Richard Smith75025ba2012-03-08 09:02:38 +00001283 case UserDefinedLiteral::LOK_Integer: {
1284 // Print integer literal without suffix.
1285 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1286 OS << Int->getValue().toString(10, /*isSigned*/false);
1287 break;
1288 }
Richard Smithc67fdd42012-03-07 08:35:16 +00001289 case UserDefinedLiteral::LOK_Floating:
1290 case UserDefinedLiteral::LOK_String:
1291 case UserDefinedLiteral::LOK_Character:
1292 PrintExpr(Node->getCookedLiteral());
1293 break;
1294 }
1295 OS << Node->getUDSuffix()->getName();
1296}
1297
Chris Lattnereefa10e2007-05-28 06:56:27 +00001298void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1299 OS << (Node->getValue() ? "true" : "false");
1300}
1301
Sebastian Redl576fd422009-05-10 18:38:11 +00001302void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1303 OS << "nullptr";
1304}
1305
Douglas Gregor97a9c812008-11-04 14:32:21 +00001306void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1307 OS << "this";
1308}
1309
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001310void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1311 if (Node->getSubExpr() == 0)
1312 OS << "throw";
1313 else {
1314 OS << "throw ";
1315 PrintExpr(Node->getSubExpr());
1316 }
1317}
1318
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001319void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1320 // Nothing to print: we picked up the default argument
1321}
1322
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001323void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001324 OS << Node->getType().getAsString(Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001325 OS << "(";
1326 PrintExpr(Node->getSubExpr());
1327 OS << ")";
1328}
1329
Anders Carlsson993a4b32009-05-30 20:03:25 +00001330void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1331 PrintExpr(Node->getSubExpr());
1332}
1333
Douglas Gregordd04d332009-01-16 18:33:17 +00001334void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001335 OS << Node->getType().getAsString(Policy);
Douglas Gregordd04d332009-01-16 18:33:17 +00001336 OS << "(";
1337 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001338 ArgEnd = Node->arg_end();
Douglas Gregordd04d332009-01-16 18:33:17 +00001339 Arg != ArgEnd; ++Arg) {
1340 if (Arg != Node->arg_begin())
1341 OS << ", ";
1342 PrintExpr(*Arg);
1343 }
1344 OS << ")";
1345}
1346
Douglas Gregore31e6062012-02-07 10:09:13 +00001347void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1348 OS << '[';
1349 bool NeedComma = false;
1350 switch (Node->getCaptureDefault()) {
1351 case LCD_None:
1352 break;
1353
1354 case LCD_ByCopy:
1355 OS << '=';
1356 NeedComma = true;
1357 break;
1358
1359 case LCD_ByRef:
1360 OS << '&';
1361 NeedComma = true;
1362 break;
1363 }
1364 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1365 CEnd = Node->explicit_capture_end();
1366 C != CEnd;
1367 ++C) {
1368 if (NeedComma)
1369 OS << ", ";
1370 NeedComma = true;
1371
1372 switch (C->getCaptureKind()) {
1373 case LCK_This:
1374 OS << "this";
1375 break;
1376
1377 case LCK_ByRef:
1378 if (Node->getCaptureDefault() != LCD_ByRef)
1379 OS << '&';
1380 OS << C->getCapturedVar()->getName();
1381 break;
1382
1383 case LCK_ByCopy:
1384 if (Node->getCaptureDefault() != LCD_ByCopy)
1385 OS << '=';
1386 OS << C->getCapturedVar()->getName();
1387 break;
1388 }
1389 }
1390 OS << ']';
1391
1392 if (Node->hasExplicitParameters()) {
1393 OS << " (";
1394 CXXMethodDecl *Method = Node->getCallOperator();
1395 NeedComma = false;
1396 for (CXXMethodDecl::param_iterator P = Method->param_begin(),
1397 PEnd = Method->param_end();
1398 P != PEnd; ++P) {
1399 if (NeedComma) {
1400 OS << ", ";
1401 } else {
1402 NeedComma = true;
1403 }
1404 std::string ParamStr = (*P)->getNameAsString();
1405 (*P)->getOriginalType().getAsStringInternal(ParamStr, Policy);
1406 OS << ParamStr;
1407 }
1408 if (Method->isVariadic()) {
1409 if (NeedComma)
1410 OS << ", ";
1411 OS << "...";
1412 }
1413 OS << ')';
1414
1415 if (Node->isMutable())
1416 OS << " mutable";
1417
1418 const FunctionProtoType *Proto
1419 = Method->getType()->getAs<FunctionProtoType>();
1420 {
1421 std::string ExceptionSpec;
1422 Proto->printExceptionSpecification(ExceptionSpec, Policy);
1423 OS << ExceptionSpec;
1424 }
1425
1426 // FIXME: Attributes
1427
Douglas Gregor0c46b2b2012-02-13 22:00:16 +00001428 // Print the trailing return type if it was specified in the source.
1429 if (Node->hasExplicitResultType())
1430 OS << " -> " << Proto->getResultType().getAsString(Policy);
Douglas Gregore31e6062012-02-07 10:09:13 +00001431 }
1432
1433 // Print the body.
1434 CompoundStmt *Body = Node->getBody();
1435 OS << ' ';
1436 PrintStmt(Body);
1437}
1438
Douglas Gregor747eb782010-07-08 06:14:04 +00001439void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregor2b88c112010-09-08 00:15:04 +00001440 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
1441 OS << TSInfo->getType().getAsString(Policy) << "()";
1442 else
1443 OS << Node->getType().getAsString(Policy) << "()";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001444}
1445
Sebastian Redlbd150f42008-11-21 19:14:01 +00001446void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1447 if (E->isGlobalNew())
1448 OS << "::";
1449 OS << "new ";
1450 unsigned NumPlace = E->getNumPlacementArgs();
1451 if (NumPlace > 0) {
1452 OS << "(";
1453 PrintExpr(E->getPlacementArg(0));
1454 for (unsigned i = 1; i < NumPlace; ++i) {
1455 OS << ", ";
1456 PrintExpr(E->getPlacementArg(i));
1457 }
1458 OS << ") ";
1459 }
1460 if (E->isParenTypeId())
1461 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001462 std::string TypeS;
1463 if (Expr *Size = E->getArraySize()) {
1464 llvm::raw_string_ostream s(TypeS);
Eli Friedmanc4fc8392009-05-30 05:32:46 +00001465 Size->printPretty(s, Context, Helper, Policy);
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001466 s.flush();
1467 TypeS = "[" + TypeS + "]";
1468 }
Douglas Gregor7de59662009-05-29 20:38:28 +00001469 E->getAllocatedType().getAsStringInternal(TypeS, Policy);
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001470 OS << TypeS;
Sebastian Redlbd150f42008-11-21 19:14:01 +00001471 if (E->isParenTypeId())
1472 OS << ")";
1473
Sebastian Redl6047f072012-02-16 12:22:20 +00001474 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
1475 if (InitStyle) {
1476 if (InitStyle == CXXNewExpr::CallInit)
1477 OS << "(";
1478 PrintExpr(E->getInitializer());
1479 if (InitStyle == CXXNewExpr::CallInit)
1480 OS << ")";
Sebastian Redlbd150f42008-11-21 19:14:01 +00001481 }
1482}
1483
1484void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1485 if (E->isGlobalDelete())
1486 OS << "::";
1487 OS << "delete ";
1488 if (E->isArrayForm())
1489 OS << "[] ";
1490 PrintExpr(E->getArgument());
1491}
1492
Douglas Gregorad8a3362009-09-04 17:36:40 +00001493void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1494 PrintExpr(E->getBase());
1495 if (E->isArrow())
1496 OS << "->";
1497 else
1498 OS << '.';
1499 if (E->getQualifier())
1500 E->getQualifier()->print(OS, Policy);
Mike Stump11289f42009-09-09 15:08:12 +00001501
Douglas Gregorad8a3362009-09-04 17:36:40 +00001502 std::string TypeS;
Douglas Gregor678f90d2010-02-25 01:56:36 +00001503 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1504 OS << II->getName();
1505 else
1506 E->getDestroyedType().getAsStringInternal(TypeS, Policy);
Douglas Gregorad8a3362009-09-04 17:36:40 +00001507 OS << TypeS;
1508}
1509
Anders Carlsson0781ce72009-04-23 02:32:43 +00001510void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Douglas Gregorbaba85d2011-01-24 17:25:03 +00001511 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1512 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1513 // Don't print any defaulted arguments
1514 break;
1515 }
1516
1517 if (i) OS << ", ";
1518 PrintExpr(E->getArg(i));
Fariborz Jahaniand0bbf662010-01-13 21:41:11 +00001519 }
Anders Carlsson0781ce72009-04-23 02:32:43 +00001520}
1521
John McCall5d413782010-12-06 08:20:24 +00001522void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Anders Carlssondefc6442009-04-24 22:47:04 +00001523 // Just forward to the sub expression.
1524 PrintExpr(E->getSubExpr());
1525}
1526
Mike Stump11289f42009-09-09 15:08:12 +00001527void
Douglas Gregorce934142009-05-20 18:46:25 +00001528StmtPrinter::VisitCXXUnresolvedConstructExpr(
1529 CXXUnresolvedConstructExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001530 OS << Node->getTypeAsWritten().getAsString(Policy);
Douglas Gregorce934142009-05-20 18:46:25 +00001531 OS << "(";
1532 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001533 ArgEnd = Node->arg_end();
Douglas Gregorce934142009-05-20 18:46:25 +00001534 Arg != ArgEnd; ++Arg) {
1535 if (Arg != Node->arg_begin())
1536 OS << ", ";
1537 PrintExpr(*Arg);
1538 }
1539 OS << ")";
1540}
1541
John McCall8cd78132009-11-19 22:55:06 +00001542void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1543 CXXDependentScopeMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001544 if (!Node->isImplicitAccess()) {
1545 PrintExpr(Node->getBase());
1546 OS << (Node->isArrow() ? "->" : ".");
1547 }
Douglas Gregorc26e0f62009-09-03 16:14:30 +00001548 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1549 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001550 if (Node->hasTemplateKeyword())
Douglas Gregor308047d2009-09-09 00:23:06 +00001551 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001552 OS << Node->getMemberNameInfo();
John McCall2d74de92009-12-01 22:10:20 +00001553 if (Node->hasExplicitTemplateArgs()) {
Douglas Gregor308047d2009-09-09 00:23:06 +00001554 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1555 Node->getTemplateArgs(),
1556 Node->getNumTemplateArgs(),
1557 Policy);
1558 }
Douglas Gregora8db9542009-05-22 21:13:27 +00001559}
1560
John McCall10eae182009-11-30 22:42:35 +00001561void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001562 if (!Node->isImplicitAccess()) {
1563 PrintExpr(Node->getBase());
1564 OS << (Node->isArrow() ? "->" : ".");
1565 }
John McCall10eae182009-11-30 22:42:35 +00001566 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1567 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001568 if (Node->hasTemplateKeyword())
1569 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001570 OS << Node->getMemberNameInfo();
John McCall10eae182009-11-30 22:42:35 +00001571 if (Node->hasExplicitTemplateArgs()) {
1572 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1573 Node->getTemplateArgs(),
1574 Node->getNumTemplateArgs(),
1575 Policy);
1576 }
1577}
1578
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001579static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1580 switch (UTT) {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001581 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001582 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
John Wiegley65497cc2011-04-27 23:09:49 +00001583 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001584 case UTT_HasTrivialAssign: return "__has_trivial_assign";
Alexis Huntf479f1b2011-05-09 18:22:59 +00001585 case UTT_HasTrivialDefaultConstructor: return "__has_trivial_constructor";
John Wiegley65497cc2011-04-27 23:09:49 +00001586 case UTT_HasTrivialCopy: return "__has_trivial_copy";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001587 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1588 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1589 case UTT_IsAbstract: return "__is_abstract";
John Wiegley65497cc2011-04-27 23:09:49 +00001590 case UTT_IsArithmetic: return "__is_arithmetic";
1591 case UTT_IsArray: return "__is_array";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001592 case UTT_IsClass: return "__is_class";
John Wiegley65497cc2011-04-27 23:09:49 +00001593 case UTT_IsCompleteType: return "__is_complete_type";
1594 case UTT_IsCompound: return "__is_compound";
1595 case UTT_IsConst: return "__is_const";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001596 case UTT_IsEmpty: return "__is_empty";
1597 case UTT_IsEnum: return "__is_enum";
Douglas Gregordca70af2011-12-03 18:14:24 +00001598 case UTT_IsFinal: return "__is_final";
John Wiegley65497cc2011-04-27 23:09:49 +00001599 case UTT_IsFloatingPoint: return "__is_floating_point";
1600 case UTT_IsFunction: return "__is_function";
1601 case UTT_IsFundamental: return "__is_fundamental";
1602 case UTT_IsIntegral: return "__is_integral";
Chandler Carruth8e2d6f42011-05-01 07:23:20 +00001603 case UTT_IsLiteral: return "__is_literal";
John Wiegley65497cc2011-04-27 23:09:49 +00001604 case UTT_IsLvalueReference: return "__is_lvalue_reference";
1605 case UTT_IsMemberFunctionPointer: return "__is_member_function_pointer";
1606 case UTT_IsMemberObjectPointer: return "__is_member_object_pointer";
1607 case UTT_IsMemberPointer: return "__is_member_pointer";
1608 case UTT_IsObject: return "__is_object";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001609 case UTT_IsPOD: return "__is_pod";
John Wiegley65497cc2011-04-27 23:09:49 +00001610 case UTT_IsPointer: return "__is_pointer";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001611 case UTT_IsPolymorphic: return "__is_polymorphic";
John Wiegley65497cc2011-04-27 23:09:49 +00001612 case UTT_IsReference: return "__is_reference";
John Wiegley65497cc2011-04-27 23:09:49 +00001613 case UTT_IsRvalueReference: return "__is_rvalue_reference";
1614 case UTT_IsScalar: return "__is_scalar";
1615 case UTT_IsSigned: return "__is_signed";
1616 case UTT_IsStandardLayout: return "__is_standard_layout";
1617 case UTT_IsTrivial: return "__is_trivial";
Alexis Huntd9a5cc12011-05-13 00:31:07 +00001618 case UTT_IsTriviallyCopyable: return "__is_trivially_copyable";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001619 case UTT_IsUnion: return "__is_union";
John Wiegley65497cc2011-04-27 23:09:49 +00001620 case UTT_IsUnsigned: return "__is_unsigned";
1621 case UTT_IsVoid: return "__is_void";
1622 case UTT_IsVolatile: return "__is_volatile";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001623 }
Chandler Carruth8e2d6f42011-05-01 07:23:20 +00001624 llvm_unreachable("Type trait not covered by switch statement");
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001625}
1626
1627static const char *getTypeTraitName(BinaryTypeTrait BTT) {
1628 switch (BTT) {
Douglas Gregor1be329d2012-02-23 07:33:15 +00001629 case BTT_IsBaseOf: return "__is_base_of";
1630 case BTT_IsConvertible: return "__is_convertible";
1631 case BTT_IsSame: return "__is_same";
1632 case BTT_TypeCompatible: return "__builtin_types_compatible_p";
1633 case BTT_IsConvertibleTo: return "__is_convertible_to";
1634 case BTT_IsTriviallyAssignable: return "__is_trivially_assignable";
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001635 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001636 llvm_unreachable("Binary type trait not covered by switch");
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001637}
1638
Douglas Gregor29c42f22012-02-24 07:38:34 +00001639static const char *getTypeTraitName(TypeTrait TT) {
1640 switch (TT) {
1641 case clang::TT_IsTriviallyConstructible:return "__is_trivially_constructible";
1642 }
1643 llvm_unreachable("Type trait not covered by switch");
1644}
1645
John Wiegley6242b6a2011-04-28 00:16:57 +00001646static const char *getTypeTraitName(ArrayTypeTrait ATT) {
1647 switch (ATT) {
1648 case ATT_ArrayRank: return "__array_rank";
1649 case ATT_ArrayExtent: return "__array_extent";
1650 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001651 llvm_unreachable("Array type trait not covered by switch");
John Wiegley6242b6a2011-04-28 00:16:57 +00001652}
1653
John Wiegleyf9f65842011-04-25 06:54:41 +00001654static const char *getExpressionTraitName(ExpressionTrait ET) {
1655 switch (ET) {
John Wiegleyf9f65842011-04-25 06:54:41 +00001656 case ET_IsLValueExpr: return "__is_lvalue_expr";
1657 case ET_IsRValueExpr: return "__is_rvalue_expr";
1658 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001659 llvm_unreachable("Expression type trait not covered by switch");
John Wiegleyf9f65842011-04-25 06:54:41 +00001660}
1661
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001662void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1663 OS << getTypeTraitName(E->getTrait()) << "("
Daniel Dunbar219fa692010-06-30 19:16:48 +00001664 << E->getQueriedType().getAsString(Policy) << ")";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001665}
1666
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001667void StmtPrinter::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
1668 OS << getTypeTraitName(E->getTrait()) << "("
1669 << E->getLhsType().getAsString(Policy) << ","
1670 << E->getRhsType().getAsString(Policy) << ")";
1671}
1672
Douglas Gregor29c42f22012-02-24 07:38:34 +00001673void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
1674 OS << getTypeTraitName(E->getTrait()) << "(";
1675 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
1676 if (I > 0)
1677 OS << ", ";
1678 OS << E->getArg(I)->getType().getAsString(Policy);
1679 }
1680 OS << ")";
1681}
1682
John Wiegley6242b6a2011-04-28 00:16:57 +00001683void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
1684 OS << getTypeTraitName(E->getTrait()) << "("
1685 << E->getQueriedType().getAsString(Policy) << ")";
1686}
1687
John Wiegleyf9f65842011-04-25 06:54:41 +00001688void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
1689 OS << getExpressionTraitName(E->getTrait()) << "(";
1690 PrintExpr(E->getQueriedExpression());
1691 OS << ")";
1692}
1693
Sebastian Redl4202c0f2010-09-10 20:55:43 +00001694void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1695 OS << "noexcept(";
1696 PrintExpr(E->getOperand());
1697 OS << ")";
1698}
1699
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001700void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregore8e9dd62011-01-03 17:17:50 +00001701 PrintExpr(E->getPattern());
1702 OS << "...";
1703}
1704
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001705void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001706 OS << "sizeof...(" << *E->getPack() << ")";
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001707}
1708
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001709void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
1710 SubstNonTypeTemplateParmPackExpr *Node) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001711 OS << *Node->getParameterPack();
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001712}
1713
John McCall7c454bb2011-07-15 05:09:51 +00001714void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
1715 SubstNonTypeTemplateParmExpr *Node) {
1716 Visit(Node->getReplacement());
1717}
1718
Douglas Gregorfe314812011-06-21 17:03:29 +00001719void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
1720 PrintExpr(Node->GetTemporaryExpr());
1721}
1722
Mike Stump11289f42009-09-09 15:08:12 +00001723// Obj-C
Anders Carlsson76f4a902007-08-21 17:43:55 +00001724
1725void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1726 OS << "@";
1727 VisitStringLiteral(Node->getString());
1728}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001729
Patrick Beard0caa3942012-04-19 00:25:12 +00001730void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001731 OS << "@";
Patrick Beard0caa3942012-04-19 00:25:12 +00001732 Visit(E->getSubExpr());
Ted Kremeneke65b0862012-03-06 20:05:56 +00001733}
1734
1735void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
1736 OS << "@[ ";
1737 StmtRange ch = E->children();
1738 if (ch.first != ch.second) {
1739 while (1) {
1740 Visit(*ch.first);
1741 ++ch.first;
1742 if (ch.first == ch.second) break;
1743 OS << ", ";
1744 }
1745 }
1746 OS << " ]";
1747}
1748
1749void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
1750 OS << "@{ ";
1751 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
1752 if (I > 0)
1753 OS << ", ";
1754
1755 ObjCDictionaryElement Element = E->getKeyValueElement(I);
1756 Visit(Element.Key);
1757 OS << " : ";
1758 Visit(Element.Value);
1759 if (Element.isPackExpansion())
1760 OS << "...";
1761 }
1762 OS << " }";
1763}
1764
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001765void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001766 OS << "@encode(" << Node->getEncodedType().getAsString(Policy) << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001767}
1768
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001769void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001770 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001771}
1772
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001773void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001774 OS << "@protocol(" << *Node->getProtocol() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001775}
1776
Steve Naroffd54978b2007-09-18 23:55:05 +00001777void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1778 OS << "[";
Douglas Gregor9a129192010-04-21 00:45:42 +00001779 switch (Mess->getReceiverKind()) {
1780 case ObjCMessageExpr::Instance:
1781 PrintExpr(Mess->getInstanceReceiver());
1782 break;
1783
1784 case ObjCMessageExpr::Class:
1785 OS << Mess->getClassReceiver().getAsString(Policy);
1786 break;
1787
1788 case ObjCMessageExpr::SuperInstance:
1789 case ObjCMessageExpr::SuperClass:
1790 OS << "Super";
1791 break;
1792 }
1793
Ted Kremeneka06e7122008-05-02 17:32:38 +00001794 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00001795 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001796 if (selector.isUnarySelector()) {
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00001797 OS << selector.getNameForSlot(0);
Steve Naroffc6814ea2007-10-02 20:01:56 +00001798 } else {
1799 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001800 if (i < selector.getNumArgs()) {
1801 if (i > 0) OS << ' ';
1802 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001803 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00001804 else
1805 OS << ":";
1806 }
1807 else OS << ", "; // Handle variadic methods.
Mike Stump11289f42009-09-09 15:08:12 +00001808
Steve Naroffc6814ea2007-10-02 20:01:56 +00001809 PrintExpr(Mess->getArg(i));
1810 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001811 }
1812 OS << "]";
1813}
1814
Ted Kremeneke65b0862012-03-06 20:05:56 +00001815void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
1816 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
1817}
1818
John McCall31168b02011-06-15 23:02:42 +00001819void
1820StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
1821 PrintExpr(E->getSubExpr());
1822}
1823
1824void
1825StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
1826 OS << "(" << E->getBridgeKindName() << E->getType().getAsString(Policy)
1827 << ")";
1828 PrintExpr(E->getSubExpr());
1829}
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001830
Steve Naroffc540d662008-09-03 18:15:37 +00001831void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001832 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00001833 OS << "^";
Mike Stump11289f42009-09-09 15:08:12 +00001834
Steve Naroffc540d662008-09-03 18:15:37 +00001835 const FunctionType *AFT = Node->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00001836
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001837 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroffc540d662008-09-03 18:15:37 +00001838 OS << "()";
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001839 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00001840 OS << '(';
1841 std::string ParamStr;
Steve Naroff415d3d52008-10-08 17:01:13 +00001842 for (BlockDecl::param_iterator AI = BD->param_begin(),
1843 E = BD->param_end(); AI != E; ++AI) {
1844 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001845 ParamStr = (*AI)->getNameAsString();
Douglas Gregor7de59662009-05-29 20:38:28 +00001846 (*AI)->getType().getAsStringInternal(ParamStr, Policy);
Steve Naroffc540d662008-09-03 18:15:37 +00001847 OS << ParamStr;
1848 }
Mike Stump11289f42009-09-09 15:08:12 +00001849
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001850 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00001851 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001852 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00001853 OS << "...";
1854 }
1855 OS << ')';
1856 }
1857}
1858
Ted Kremenekf551f322011-11-30 22:08:08 +00001859void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
1860 PrintExpr(Node->getSourceExpr());
1861}
John McCall8d69a212010-11-15 23:31:06 +00001862
Tanya Lattner55808c12011-06-04 00:47:47 +00001863void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
1864 OS << "__builtin_astype(";
1865 PrintExpr(Node->getSrcExpr());
1866 OS << ", " << Node->getType().getAsString();
1867 OS << ")";
1868}
1869
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001870//===----------------------------------------------------------------------===//
1871// Stmt method implementations
1872//===----------------------------------------------------------------------===//
1873
Eli Friedmanef334fd2009-05-30 05:03:24 +00001874void Stmt::dumpPretty(ASTContext& Context) const {
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +00001875 printPretty(llvm::errs(), Context, 0,
David Blaikiebbafb8a2012-03-11 07:00:24 +00001876 PrintingPolicy(Context.getLangOpts()));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001877}
1878
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001879void Stmt::printPretty(raw_ostream &OS, ASTContext& Context,
Eli Friedmanef334fd2009-05-30 05:03:24 +00001880 PrinterHelper* Helper,
Douglas Gregor7de59662009-05-29 20:38:28 +00001881 const PrintingPolicy &Policy,
1882 unsigned Indentation) const {
Chris Lattner882f7882006-11-04 18:52:07 +00001883 if (this == 0) {
1884 OS << "<NULL>";
1885 return;
1886 }
1887
Douglas Gregor8655e882009-10-16 22:46:09 +00001888 if (Policy.Dump && &Context) {
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +00001889 dump(OS, Context.getSourceManager());
Douglas Gregor278f52e2009-05-30 00:08:05 +00001890 return;
1891 }
Mike Stump11289f42009-09-09 15:08:12 +00001892
Eli Friedmanef334fd2009-05-30 05:03:24 +00001893 StmtPrinter P(OS, Context, Helper, Policy, Indentation);
Chris Lattner62249a62007-08-21 04:04:25 +00001894 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001895}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001896
1897//===----------------------------------------------------------------------===//
1898// PrinterHelper
1899//===----------------------------------------------------------------------===//
1900
1901// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +00001902PrinterHelper::~PrinterHelper() {}