blob: cd8b6bb5c2310efc99baa7149746d14b6de23105 [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
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000172void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000173 OS << "if (";
Chris Lattner882f7882006-11-04 18:52:07 +0000174 PrintExpr(If->getCond());
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000175 OS << ')';
Mike Stump11289f42009-09-09 15:08:12 +0000176
Chris Lattner073926e2007-05-20 23:04:55 +0000177 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
178 OS << ' ';
179 PrintRawCompoundStmt(CS);
180 OS << (If->getElse() ? ' ' : '\n');
181 } else {
182 OS << '\n';
183 PrintStmt(If->getThen());
184 if (If->getElse()) Indent();
185 }
Mike Stump11289f42009-09-09 15:08:12 +0000186
Chris Lattner073926e2007-05-20 23:04:55 +0000187 if (Stmt *Else = If->getElse()) {
188 OS << "else";
Mike Stump11289f42009-09-09 15:08:12 +0000189
Chris Lattner073926e2007-05-20 23:04:55 +0000190 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
191 OS << ' ';
192 PrintRawCompoundStmt(CS);
193 OS << '\n';
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000194 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
195 OS << ' ';
196 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000197 } else {
198 OS << '\n';
199 PrintStmt(If->getElse());
200 }
Chris Lattner882f7882006-11-04 18:52:07 +0000201 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000202}
203
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000204void StmtPrinter::VisitIfStmt(IfStmt *If) {
205 Indent();
206 PrintRawIfStmt(If);
207}
208
Chris Lattnerf2174b62006-11-04 20:59:27 +0000209void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
210 Indent() << "switch (";
211 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000212 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000213
Chris Lattner073926e2007-05-20 23:04:55 +0000214 // Pretty print compoundstmt bodies (very common).
215 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
216 OS << " ";
217 PrintRawCompoundStmt(CS);
218 OS << "\n";
219 } else {
220 OS << "\n";
221 PrintStmt(Node->getBody());
222 }
Chris Lattnerf2174b62006-11-04 20:59:27 +0000223}
224
Chris Lattner85ed8732006-11-04 20:40:44 +0000225void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
226 Indent() << "while (";
227 PrintExpr(Node->getCond());
228 OS << ")\n";
229 PrintStmt(Node->getBody());
230}
231
232void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000233 Indent() << "do ";
234 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
235 PrintRawCompoundStmt(CS);
236 OS << " ";
237 } else {
238 OS << "\n";
239 PrintStmt(Node->getBody());
240 Indent();
241 }
Mike Stump11289f42009-09-09 15:08:12 +0000242
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000243 OS << "while (";
Chris Lattner85ed8732006-11-04 20:40:44 +0000244 PrintExpr(Node->getCond());
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000245 OS << ");\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000246}
247
Chris Lattner71e23ce2006-11-04 20:18:38 +0000248void StmtPrinter::VisitForStmt(ForStmt *Node) {
249 Indent() << "for (";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000250 if (Node->getInit()) {
251 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000252 PrintRawDeclStmt(DS);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000253 else
254 PrintExpr(cast<Expr>(Node->getInit()));
255 }
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000256 OS << ";";
257 if (Node->getCond()) {
258 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000259 PrintExpr(Node->getCond());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000260 }
261 OS << ";";
262 if (Node->getInc()) {
263 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000264 PrintExpr(Node->getInc());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000265 }
266 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000267
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000268 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
269 PrintRawCompoundStmt(CS);
270 OS << "\n";
271 } else {
272 OS << "\n";
273 PrintStmt(Node->getBody());
274 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000275}
276
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000277void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian83615522008-01-02 22:54:34 +0000278 Indent() << "for (";
279 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000280 PrintRawDeclStmt(DS);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000281 else
282 PrintExpr(cast<Expr>(Node->getElement()));
283 OS << " in ";
284 PrintExpr(Node->getCollection());
285 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000286
Fariborz Jahanian83615522008-01-02 22:54:34 +0000287 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
288 PrintRawCompoundStmt(CS);
289 OS << "\n";
290 } else {
291 OS << "\n";
292 PrintStmt(Node->getBody());
293 }
294}
295
Richard Smith02e85f32011-04-14 22:09:26 +0000296void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
297 Indent() << "for (";
298 PrintingPolicy SubPolicy(Policy);
299 SubPolicy.SuppressInitializers = true;
300 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
301 OS << " : ";
302 PrintExpr(Node->getRangeInit());
303 OS << ") {\n";
304 PrintStmt(Node->getBody());
305 Indent() << "}\n";
306}
307
Douglas Gregordeb4a2be2011-10-25 01:33:02 +0000308void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
309 Indent();
310 if (Node->isIfExists())
311 OS << "__if_exists (";
312 else
313 OS << "__if_not_exists (";
314
315 if (NestedNameSpecifier *Qualifier
316 = Node->getQualifierLoc().getNestedNameSpecifier())
317 Qualifier->print(OS, Policy);
318
319 OS << Node->getNameInfo() << ") ";
320
321 PrintRawCompoundStmt(Node->getSubStmt());
322}
323
Chris Lattner16976d32006-11-05 01:46:01 +0000324void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000325 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000326}
327
328void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000329 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000330 PrintExpr(Node->getTarget());
Chris Lattnerb4619482007-05-31 06:00:14 +0000331 OS << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000332}
333
334void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000335 Indent() << "continue;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000336}
337
338void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000339 Indent() << "break;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000340}
341
342
Chris Lattner882f7882006-11-04 18:52:07 +0000343void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
344 Indent() << "return";
345 if (Node->getRetValue()) {
346 OS << " ";
347 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000348 }
Chris Lattnerb4619482007-05-31 06:00:14 +0000349 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000350}
351
Chris Lattner73c56c02007-10-29 04:04:16 +0000352
353void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson660bdd12007-11-23 23:12:25 +0000354 Indent() << "asm ";
Mike Stump11289f42009-09-09 15:08:12 +0000355
Anders Carlsson660bdd12007-11-23 23:12:25 +0000356 if (Node->isVolatile())
357 OS << "volatile ";
Mike Stump11289f42009-09-09 15:08:12 +0000358
Anders Carlsson660bdd12007-11-23 23:12:25 +0000359 OS << "(";
Anders Carlsson81a5a692007-11-20 19:21:03 +0000360 VisitStringLiteral(Node->getAsmString());
Mike Stump11289f42009-09-09 15:08:12 +0000361
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000362 // Outputs
363 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
364 Node->getNumClobbers() != 0)
365 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000366
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000367 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
368 if (i != 0)
369 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000370
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000371 if (!Node->getOutputName(i).empty()) {
372 OS << '[';
373 OS << Node->getOutputName(i);
374 OS << "] ";
375 }
Mike Stump11289f42009-09-09 15:08:12 +0000376
Chris Lattner72bbf172009-03-10 04:59:06 +0000377 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000378 OS << " ";
379 Visit(Node->getOutputExpr(i));
380 }
Mike Stump11289f42009-09-09 15:08:12 +0000381
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000382 // Inputs
383 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
384 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000385
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000386 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
387 if (i != 0)
388 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000389
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000390 if (!Node->getInputName(i).empty()) {
391 OS << '[';
392 OS << Node->getInputName(i);
393 OS << "] ";
394 }
Mike Stump11289f42009-09-09 15:08:12 +0000395
Chris Lattner72bbf172009-03-10 04:59:06 +0000396 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000397 OS << " ";
398 Visit(Node->getInputExpr(i));
399 }
Mike Stump11289f42009-09-09 15:08:12 +0000400
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000401 // Clobbers
402 if (Node->getNumClobbers() != 0)
403 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000404
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000405 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
406 if (i != 0)
407 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000408
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000409 VisitStringLiteral(Node->getClobber(i));
410 }
Mike Stump11289f42009-09-09 15:08:12 +0000411
Anders Carlsson81a5a692007-11-20 19:21:03 +0000412 OS << ");\n";
Chris Lattner73c56c02007-10-29 04:04:16 +0000413}
414
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000415void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000416 Indent() << "@try";
417 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
418 PrintRawCompoundStmt(TS);
419 OS << "\n";
420 }
Mike Stump11289f42009-09-09 15:08:12 +0000421
Douglas Gregor96c79492010-04-23 22:50:49 +0000422 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
423 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000424 Indent() << "@catch(";
Steve Naroff371b8fb2009-03-03 19:52:17 +0000425 if (catchStmt->getCatchParamDecl()) {
426 if (Decl *DS = catchStmt->getCatchParamDecl())
427 PrintRawDecl(DS);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000428 }
429 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000430 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
431 PrintRawCompoundStmt(CS);
432 OS << "\n";
433 }
Fariborz Jahanian88157952007-11-02 18:16:07 +0000434 }
Mike Stump11289f42009-09-09 15:08:12 +0000435
436 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
437 Node->getFinallyStmt())) {
Fariborz Jahaniandefbf9a2007-11-07 00:46:42 +0000438 Indent() << "@finally";
439 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian88157952007-11-02 18:16:07 +0000440 OS << "\n";
Mike Stump11289f42009-09-09 15:08:12 +0000441 }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000442}
443
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000444void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000445}
446
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000447void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000448 Indent() << "@catch (...) { /* todo */ } \n";
449}
450
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000451void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000452 Indent() << "@throw";
453 if (Node->getThrowExpr()) {
454 OS << " ";
455 PrintExpr(Node->getThrowExpr());
456 }
457 OS << ";\n";
458}
459
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000460void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000461 Indent() << "@synchronized (";
462 PrintExpr(Node->getSynchExpr());
463 OS << ")";
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000464 PrintRawCompoundStmt(Node->getSynchBody());
465 OS << "\n";
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000466}
467
John McCall31168b02011-06-15 23:02:42 +0000468void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
469 Indent() << "@autoreleasepool";
470 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
471 OS << "\n";
472}
473
Sebastian Redl9b244a82008-12-22 21:35:02 +0000474void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
475 OS << "catch (";
Sebastian Redl54c04d42008-12-22 19:15:10 +0000476 if (Decl *ExDecl = Node->getExceptionDecl())
477 PrintRawDecl(ExDecl);
478 else
479 OS << "...";
480 OS << ") ";
481 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl9b244a82008-12-22 21:35:02 +0000482}
483
484void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
485 Indent();
486 PrintRawCXXCatchStmt(Node);
487 OS << "\n";
488}
489
490void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
491 Indent() << "try ";
492 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump11289f42009-09-09 15:08:12 +0000493 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl9b244a82008-12-22 21:35:02 +0000494 OS << " ";
495 PrintRawCXXCatchStmt(Node->getHandler(i));
496 }
Sebastian Redl54c04d42008-12-22 19:15:10 +0000497 OS << "\n";
498}
499
John Wiegley1c0675e2011-04-28 01:08:34 +0000500void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
501 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
502 PrintRawCompoundStmt(Node->getTryBlock());
503 SEHExceptStmt *E = Node->getExceptHandler();
504 SEHFinallyStmt *F = Node->getFinallyHandler();
505 if(E)
506 PrintRawSEHExceptHandler(E);
507 else {
508 assert(F && "Must have a finally block...");
509 PrintRawSEHFinallyStmt(F);
510 }
511 OS << "\n";
512}
513
514void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
515 OS << "__finally ";
516 PrintRawCompoundStmt(Node->getBlock());
517 OS << "\n";
518}
519
520void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
521 OS << "__except (";
522 VisitExpr(Node->getFilterExpr());
523 OS << ")\n";
524 PrintRawCompoundStmt(Node->getBlock());
525 OS << "\n";
526}
527
528void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
529 Indent();
530 PrintRawSEHExceptHandler(Node);
531 OS << "\n";
532}
533
534void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
535 Indent();
536 PrintRawSEHFinallyStmt(Node);
537 OS << "\n";
538}
539
Chris Lattner71e23ce2006-11-04 20:18:38 +0000540//===----------------------------------------------------------------------===//
541// Expr printing methods.
542//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000543
Chris Lattner882f7882006-11-04 18:52:07 +0000544void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000545 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
546 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000547 if (Node->hasTemplateKeyword())
548 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000549 OS << Node->getNameInfo();
John McCallb3774b52010-08-19 23:49:38 +0000550 if (Node->hasExplicitTemplateArgs())
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000551 OS << TemplateSpecializationType::PrintTemplateArgumentList(
552 Node->getTemplateArgs(),
553 Node->getNumTemplateArgs(),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000554 Policy);
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000555}
556
John McCall8cd78132009-11-19 22:55:06 +0000557void StmtPrinter::VisitDependentScopeDeclRefExpr(
558 DependentScopeDeclRefExpr *Node) {
Axel Naumann20b27862011-01-24 15:44:00 +0000559 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
560 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000561 if (Node->hasTemplateKeyword())
562 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000563 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000564 if (Node->hasExplicitTemplateArgs())
565 OS << TemplateSpecializationType::PrintTemplateArgumentList(
566 Node->getTemplateArgs(),
567 Node->getNumTemplateArgs(),
568 Policy);
Douglas Gregor90a1a652009-03-19 17:26:29 +0000569}
570
John McCalld14a8642009-11-21 08:51:07 +0000571void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregora727cb92009-06-30 22:34:41 +0000572 if (Node->getQualifier())
573 Node->getQualifier()->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000574 if (Node->hasTemplateKeyword())
575 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000576 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000577 if (Node->hasExplicitTemplateArgs())
578 OS << TemplateSpecializationType::PrintTemplateArgumentList(
579 Node->getTemplateArgs(),
Douglas Gregora727cb92009-06-30 22:34:41 +0000580 Node->getNumTemplateArgs(),
John McCalle66edc12009-11-24 19:00:30 +0000581 Policy);
Douglas Gregora727cb92009-06-30 22:34:41 +0000582}
583
Steve Naroffe46504b2007-11-12 14:29:37 +0000584void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000585 if (Node->getBase()) {
586 PrintExpr(Node->getBase());
587 OS << (Node->isArrow() ? "->" : ".");
588 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000589 OS << *Node->getDecl();
Steve Naroffe46504b2007-11-12 14:29:37 +0000590}
591
Steve Naroffec944032008-05-30 00:40:33 +0000592void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000593 if (Node->isSuperReceiver())
594 OS << "super.";
595 else if (Node->getBase()) {
Steve Naroffec944032008-05-30 00:40:33 +0000596 PrintExpr(Node->getBase());
597 OS << ".";
598 }
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000599
John McCallb7bd14f2010-12-02 01:19:52 +0000600 if (Node->isImplicitProperty())
601 OS << Node->getImplicitPropertyGetter()->getSelector().getAsString();
602 else
603 OS << Node->getExplicitProperty()->getName();
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000604}
605
Ted Kremeneke65b0862012-03-06 20:05:56 +0000606void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
607
608 PrintExpr(Node->getBaseExpr());
609 OS << "[";
610 PrintExpr(Node->getKeyExpr());
611 OS << "]";
612}
613
Chris Lattner6307f192008-08-10 01:53:14 +0000614void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson625bfc82007-07-21 05:21:51 +0000615 switch (Node->getIdentType()) {
616 default:
David Blaikie83d382b2011-09-23 05:06:16 +0000617 llvm_unreachable("unknown case");
Chris Lattner6307f192008-08-10 01:53:14 +0000618 case PredefinedExpr::Func:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000619 OS << "__func__";
620 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000621 case PredefinedExpr::Function:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000622 OS << "__FUNCTION__";
623 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000624 case PredefinedExpr::PrettyFunction:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000625 OS << "__PRETTY_FUNCTION__";
626 break;
627 }
628}
629
Steve Naroffae4143e2007-04-26 20:39:23 +0000630void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000631 unsigned value = Node->getValue();
Douglas Gregorfb65e592011-07-27 05:40:30 +0000632
633 switch (Node->getKind()) {
634 case CharacterLiteral::Ascii: break; // no prefix.
635 case CharacterLiteral::Wide: OS << 'L'; break;
636 case CharacterLiteral::UTF16: OS << 'u'; break;
637 case CharacterLiteral::UTF32: OS << 'U'; break;
638 }
639
Chris Lattner666115c2007-07-13 23:58:20 +0000640 switch (value) {
641 case '\\':
642 OS << "'\\\\'";
643 break;
644 case '\'':
645 OS << "'\\''";
646 break;
647 case '\a':
648 // TODO: K&R: the meaning of '\\a' is different in traditional C
649 OS << "'\\a'";
650 break;
651 case '\b':
652 OS << "'\\b'";
653 break;
654 // Nonstandard escape sequence.
655 /*case '\e':
656 OS << "'\\e'";
657 break;*/
658 case '\f':
659 OS << "'\\f'";
660 break;
661 case '\n':
662 OS << "'\\n'";
663 break;
664 case '\r':
665 OS << "'\\r'";
666 break;
667 case '\t':
668 OS << "'\\t'";
669 break;
670 case '\v':
671 OS << "'\\v'";
672 break;
673 default:
Ted Kremenek652d18e2008-02-23 00:52:04 +0000674 if (value < 256 && isprint(value)) {
Chris Lattner666115c2007-07-13 23:58:20 +0000675 OS << "'" << (char)value << "'";
676 } else if (value < 256) {
Benjamin Kramer49038022012-02-04 13:45:25 +0000677 OS << "'\\x";
678 OS.write_hex(value) << "'";
Chris Lattner666115c2007-07-13 23:58:20 +0000679 } else {
680 // FIXME what to really do here?
681 OS << value;
682 }
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000683 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000684}
685
Steve Naroffdf7855b2007-02-21 23:46:25 +0000686void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000687 bool isSigned = Node->getType()->isSignedIntegerType();
688 OS << Node->getValue().toString(10, isSigned);
Mike Stump11289f42009-09-09 15:08:12 +0000689
Chris Lattner06430412007-05-21 05:45:03 +0000690 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall9dd450b2009-09-21 23:43:11 +0000691 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
David Blaikie83d382b2011-09-23 05:06:16 +0000692 default: llvm_unreachable("Unexpected type for integer literal!");
Richard Trieu8b626ba2011-11-07 18:40:31 +0000693 // FIXME: The Short and UShort cases are to handle cases where a short
694 // integeral literal is formed during template instantiation. They should
695 // be removed when template instantiation no longer needs integer literals.
696 case BuiltinType::Short:
697 case BuiltinType::UShort:
Chris Lattner06430412007-05-21 05:45:03 +0000698 case BuiltinType::Int: break; // no suffix.
699 case BuiltinType::UInt: OS << 'U'; break;
700 case BuiltinType::Long: OS << 'L'; break;
701 case BuiltinType::ULong: OS << "UL"; break;
702 case BuiltinType::LongLong: OS << "LL"; break;
703 case BuiltinType::ULongLong: OS << "ULL"; break;
Richard Trieu8b626ba2011-11-07 18:40:31 +0000704 case BuiltinType::Int128: OS << "i128"; break;
705 case BuiltinType::UInt128: OS << "Ui128"; break;
Chris Lattner06430412007-05-21 05:45:03 +0000706 }
Chris Lattner882f7882006-11-04 18:52:07 +0000707}
Steve Naroffab624882007-02-21 22:05:47 +0000708void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000709 SmallString<16> Str;
Eli Friedman53392062011-10-05 20:32:03 +0000710 Node->getValue().toString(Str);
711 OS << Str;
Chris Lattner882f7882006-11-04 18:52:07 +0000712}
Chris Lattner1c20a172007-08-26 03:42:43 +0000713
714void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
715 PrintExpr(Node->getSubExpr());
716 OS << "i";
717}
718
Steve Naroffdf7855b2007-02-21 23:46:25 +0000719void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Douglas Gregorfb65e592011-07-27 05:40:30 +0000720 switch (Str->getKind()) {
721 case StringLiteral::Ascii: break; // no prefix.
722 case StringLiteral::Wide: OS << 'L'; break;
723 case StringLiteral::UTF8: OS << "u8"; break;
724 case StringLiteral::UTF16: OS << 'u'; break;
725 case StringLiteral::UTF32: OS << 'U'; break;
726 }
Chris Lattner5d8f4942006-11-04 20:29:31 +0000727 OS << '"';
Richard Smith88eb4d32011-12-30 23:37:31 +0000728 static char Hex[] = "0123456789ABCDEF";
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000729
Richard Smith88eb4d32011-12-30 23:37:31 +0000730 for (unsigned I = 0, N = Str->getLength(); I != N; ++I) {
731 switch (uint32_t Char = Str->getCodeUnit(I)) {
Chris Lattnerbb96cc42009-01-16 19:25:18 +0000732 default:
Richard Smith88eb4d32011-12-30 23:37:31 +0000733 // FIXME: Is this the best way to print wchar_t?
734 if (Char > 0xff) {
Richard Smithdbfd4032012-01-02 18:14:06 +0000735 assert(Char <= 0x10ffff && "invalid unicode codepoint");
Richard Smith88eb4d32011-12-30 23:37:31 +0000736 if (Char > 0xffff)
737 OS << "\\U00"
738 << Hex[(Char >> 20) & 15]
739 << Hex[(Char >> 16) & 15];
740 else
741 OS << "\\u";
742 OS << Hex[(Char >> 12) & 15]
743 << Hex[(Char >> 8) & 15]
744 << Hex[(Char >> 4) & 15]
745 << Hex[(Char >> 0) & 15];
746 break;
747 }
748 if (Char <= 0xff && isprint(Char))
Chris Lattnerbb96cc42009-01-16 19:25:18 +0000749 OS << (char)Char;
750 else // Output anything hard as an octal escape.
751 OS << '\\'
752 << (char)('0'+ ((Char >> 6) & 7))
753 << (char)('0'+ ((Char >> 3) & 7))
754 << (char)('0'+ ((Char >> 0) & 7));
755 break;
756 // Handle some common non-printable cases to make dumps prettier.
Chris Lattner5d8f4942006-11-04 20:29:31 +0000757 case '\\': OS << "\\\\"; break;
758 case '"': OS << "\\\""; break;
759 case '\n': OS << "\\n"; break;
760 case '\t': OS << "\\t"; break;
761 case '\a': OS << "\\a"; break;
762 case '\b': OS << "\\b"; break;
763 }
764 }
765 OS << '"';
Chris Lattner882f7882006-11-04 18:52:07 +0000766}
767void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
768 OS << "(";
769 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000770 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000771}
772void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +0000773 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +0000774 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump11289f42009-09-09 15:08:12 +0000775
Eli Friedman1cf25362009-06-14 22:39:26 +0000776 // Print a space if this is an "identifier operator" like __real, or if
777 // it might be concatenated incorrectly like '+'.
Chris Lattnere5b60442007-08-23 21:46:40 +0000778 switch (Node->getOpcode()) {
779 default: break;
John McCalle3027922010-08-25 11:45:40 +0000780 case UO_Real:
781 case UO_Imag:
782 case UO_Extension:
Chris Lattnere5b60442007-08-23 21:46:40 +0000783 OS << ' ';
784 break;
John McCalle3027922010-08-25 11:45:40 +0000785 case UO_Plus:
786 case UO_Minus:
Eli Friedman1cf25362009-06-14 22:39:26 +0000787 if (isa<UnaryOperator>(Node->getSubExpr()))
788 OS << ' ';
789 break;
Chris Lattnere5b60442007-08-23 21:46:40 +0000790 }
791 }
Chris Lattner882f7882006-11-04 18:52:07 +0000792 PrintExpr(Node->getSubExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000793
Chris Lattner15768702006-11-05 23:54:51 +0000794 if (Node->isPostfix())
795 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000796}
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000797
Douglas Gregor882211c2010-04-28 22:16:22 +0000798void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
799 OS << "__builtin_offsetof(";
Daniel Dunbar219fa692010-06-30 19:16:48 +0000800 OS << Node->getTypeSourceInfo()->getType().getAsString(Policy) << ", ";
Douglas Gregor882211c2010-04-28 22:16:22 +0000801 bool PrintedSomething = false;
802 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
803 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
804 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
805 // Array node
806 OS << "[";
807 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
808 OS << "]";
809 PrintedSomething = true;
810 continue;
811 }
Douglas Gregord1702062010-04-29 00:18:15 +0000812
813 // Skip implicit base indirections.
814 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
815 continue;
816
Douglas Gregor882211c2010-04-28 22:16:22 +0000817 // Field or identifier node.
818 IdentifierInfo *Id = ON.getFieldName();
819 if (!Id)
820 continue;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000821
Douglas Gregor882211c2010-04-28 22:16:22 +0000822 if (PrintedSomething)
823 OS << ".";
824 else
825 PrintedSomething = true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000826 OS << Id->getName();
Douglas Gregor882211c2010-04-28 22:16:22 +0000827 }
828 OS << ")";
829}
830
Peter Collingbournee190dee2011-03-11 19:24:49 +0000831void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
832 switch(Node->getKind()) {
833 case UETT_SizeOf:
834 OS << "sizeof";
835 break;
836 case UETT_AlignOf:
837 OS << "__alignof";
838 break;
839 case UETT_VecStep:
840 OS << "vec_step";
841 break;
842 }
Sebastian Redl6f282892008-11-11 17:56:53 +0000843 if (Node->isArgumentType())
Daniel Dunbar219fa692010-06-30 19:16:48 +0000844 OS << "(" << Node->getArgumentType().getAsString(Policy) << ")";
Sebastian Redl6f282892008-11-11 17:56:53 +0000845 else {
846 OS << " ";
847 PrintExpr(Node->getArgumentExpr());
848 }
Chris Lattner882f7882006-11-04 18:52:07 +0000849}
Peter Collingbourne91147592011-04-15 00:35:48 +0000850
851void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
852 OS << "_Generic(";
853 PrintExpr(Node->getControllingExpr());
854 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
855 OS << ", ";
856 QualType T = Node->getAssocType(i);
857 if (T.isNull())
858 OS << "default";
859 else
860 OS << T.getAsString(Policy);
861 OS << ": ";
862 PrintExpr(Node->getAssocExpr(i));
863 }
864 OS << ")";
865}
866
Chris Lattner882f7882006-11-04 18:52:07 +0000867void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000868 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000869 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +0000870 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000871 OS << "]";
872}
873
Peter Collingbourne4b279a02011-02-08 21:17:54 +0000874void StmtPrinter::PrintCallArgs(CallExpr *Call) {
Chris Lattner882f7882006-11-04 18:52:07 +0000875 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000876 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
877 // Don't print any defaulted arguments
878 break;
879 }
880
Chris Lattner882f7882006-11-04 18:52:07 +0000881 if (i) OS << ", ";
882 PrintExpr(Call->getArg(i));
883 }
Peter Collingbourne4b279a02011-02-08 21:17:54 +0000884}
885
886void StmtPrinter::VisitCallExpr(CallExpr *Call) {
887 PrintExpr(Call->getCallee());
888 OS << "(";
889 PrintCallArgs(Call);
Chris Lattner882f7882006-11-04 18:52:07 +0000890 OS << ")";
891}
892void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregore4b414c2009-01-08 22:45:41 +0000893 // FIXME: Suppress printing implicit bases (like "this")
894 PrintExpr(Node->getBase());
Fariborz Jahanian2990c022010-01-11 21:17:32 +0000895 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
896 if (FD->isAnonymousStructOrUnion())
897 return;
Douglas Gregore4b414c2009-01-08 22:45:41 +0000898 OS << (Node->isArrow() ? "->" : ".");
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000899 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
900 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000901 if (Node->hasTemplateKeyword())
902 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000903 OS << Node->getMemberNameInfo();
John McCallb3774b52010-08-19 23:49:38 +0000904 if (Node->hasExplicitTemplateArgs())
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000905 OS << TemplateSpecializationType::PrintTemplateArgumentList(
906 Node->getTemplateArgs(),
907 Node->getNumTemplateArgs(),
908 Policy);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000909}
Steve Naroffe87026a2009-07-24 17:54:45 +0000910void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
911 PrintExpr(Node->getBase());
912 OS << (Node->isArrow() ? "->isa" : ".isa");
913}
914
Nate Begemance4d7fc2008-04-18 23:10:10 +0000915void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +0000916 PrintExpr(Node->getBase());
917 OS << ".";
918 OS << Node->getAccessor().getName();
919}
Douglas Gregorf19b2312008-10-28 15:36:24 +0000920void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Fariborz Jahaniane33c1162010-06-30 16:31:08 +0000921 OS << "(" << Node->getType().getAsString(Policy) << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000922 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000923}
Steve Naroff57eb2c52007-07-19 21:32:11 +0000924void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +0000925 OS << "(" << Node->getType().getAsString(Policy) << ")";
Steve Naroff57eb2c52007-07-19 21:32:11 +0000926 PrintExpr(Node->getInitializer());
927}
Steve Naroff7a5af782007-07-13 16:58:59 +0000928void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000929 // No need to print anything, simply forward to the sub expression.
930 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +0000931}
Chris Lattner882f7882006-11-04 18:52:07 +0000932void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
933 PrintExpr(Node->getLHS());
934 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
935 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000936}
Chris Lattner86928112007-08-25 02:00:02 +0000937void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
938 PrintExpr(Node->getLHS());
939 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
940 PrintExpr(Node->getRHS());
941}
Chris Lattner882f7882006-11-04 18:52:07 +0000942void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
943 PrintExpr(Node->getCond());
John McCallc07a0c72011-02-17 10:25:35 +0000944 OS << " ? ";
945 PrintExpr(Node->getLHS());
946 OS << " : ";
Chris Lattner882f7882006-11-04 18:52:07 +0000947 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000948}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000949
Chris Lattnereefa10e2007-05-28 06:56:27 +0000950// GNU extensions.
951
John McCallc07a0c72011-02-17 10:25:35 +0000952void
953StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
954 PrintExpr(Node->getCommon());
955 OS << " ?: ";
956 PrintExpr(Node->getFalseExpr());
957}
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000958void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000959 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000960}
961
Chris Lattner366727f2007-07-24 16:58:17 +0000962void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
963 OS << "(";
964 PrintRawCompoundStmt(E->getSubStmt());
965 OS << ")";
966}
967
Steve Naroff9efdabc2007-08-03 21:21:27 +0000968void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
969 OS << "__builtin_choose_expr(";
970 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +0000971 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000972 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +0000973 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000974 PrintExpr(Node->getRHS());
975 OS << ")";
976}
Chris Lattner366727f2007-07-24 16:58:17 +0000977
Douglas Gregor3be4b122008-11-29 04:51:27 +0000978void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
979 OS << "__null";
980}
981
Eli Friedmana1b4ed82008-05-14 19:38:39 +0000982void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
983 OS << "__builtin_shufflevector(";
984 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
985 if (i) OS << ", ";
986 PrintExpr(Node->getExpr(i));
987 }
988 OS << ")";
989}
990
Anders Carlsson4692db02007-08-31 04:56:16 +0000991void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor36098ff2009-05-30 00:56:08 +0000992 if (Node->getSyntacticForm()) {
993 Visit(Node->getSyntacticForm());
994 return;
995 }
996
Anders Carlsson4692db02007-08-31 04:56:16 +0000997 OS << "{ ";
998 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
999 if (i) OS << ", ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001000 if (Node->getInit(i))
1001 PrintExpr(Node->getInit(i));
1002 else
1003 OS << "0";
Anders Carlsson4692db02007-08-31 04:56:16 +00001004 }
1005 OS << " }";
1006}
1007
Nate Begeman5ec4b312009-08-10 23:49:36 +00001008void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1009 OS << "( ";
1010 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1011 if (i) OS << ", ";
1012 PrintExpr(Node->getExpr(i));
1013 }
1014 OS << " )";
1015}
1016
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001017void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001018 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
1019 DEnd = Node->designators_end();
1020 D != DEnd; ++D) {
1021 if (D->isFieldDesignator()) {
1022 if (D->getDotLoc().isInvalid())
1023 OS << D->getFieldName()->getName() << ":";
1024 else
1025 OS << "." << D->getFieldName()->getName();
1026 } else {
1027 OS << "[";
1028 if (D->isArrayDesignator()) {
1029 PrintExpr(Node->getArrayIndex(*D));
1030 } else {
1031 PrintExpr(Node->getArrayRangeStart(*D));
1032 OS << " ... ";
Mike Stump11289f42009-09-09 15:08:12 +00001033 PrintExpr(Node->getArrayRangeEnd(*D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001034 }
1035 OS << "]";
1036 }
1037 }
1038
1039 OS << " = ";
1040 PrintExpr(Node->getInit());
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001041}
1042
Douglas Gregor0202cb42009-01-29 17:44:32 +00001043void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Chris Lattnerc61089a2009-06-30 01:26:17 +00001044 if (Policy.LangOpts.CPlusPlus)
Douglas Gregor278f52e2009-05-30 00:08:05 +00001045 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()";
1046 else {
1047 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")";
1048 if (Node->getType()->isRecordType())
1049 OS << "{}";
1050 else
1051 OS << 0;
1052 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001053}
1054
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001055void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman79635842009-05-30 04:20:30 +00001056 OS << "__builtin_va_arg(";
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001057 PrintExpr(Node->getSubExpr());
1058 OS << ", ";
Daniel Dunbar219fa692010-06-30 19:16:48 +00001059 OS << Node->getType().getAsString(Policy);
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001060 OS << ")";
1061}
1062
John McCallfe96e0b2011-11-06 09:01:30 +00001063void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1064 PrintExpr(Node->getSyntacticForm());
1065}
1066
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001067void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
Eli Friedmanc2025562011-10-11 20:00:47 +00001068 const char *Name = 0;
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001069 switch (Node->getOp()) {
David Chisnallfa35df62012-01-16 17:27:18 +00001070 case AtomicExpr::Init:
1071 Name = "__atomic_init(";
1072 break;
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001073 case AtomicExpr::Load:
1074 Name = "__atomic_load(";
1075 break;
1076 case AtomicExpr::Store:
1077 Name = "__atomic_store(";
1078 break;
1079 case AtomicExpr::CmpXchgStrong:
1080 Name = "__atomic_compare_exchange_strong(";
1081 break;
1082 case AtomicExpr::CmpXchgWeak:
1083 Name = "__atomic_compare_exchange_weak(";
1084 break;
1085 case AtomicExpr::Xchg:
1086 Name = "__atomic_exchange(";
1087 break;
1088 case AtomicExpr::Add:
1089 Name = "__atomic_fetch_add(";
1090 break;
1091 case AtomicExpr::Sub:
1092 Name = "__atomic_fetch_sub(";
1093 break;
1094 case AtomicExpr::And:
1095 Name = "__atomic_fetch_and(";
1096 break;
1097 case AtomicExpr::Or:
1098 Name = "__atomic_fetch_or(";
1099 break;
1100 case AtomicExpr::Xor:
1101 Name = "__atomic_fetch_xor(";
1102 break;
1103 }
1104 OS << Name;
1105 PrintExpr(Node->getPtr());
1106 OS << ", ";
1107 if (Node->getOp() != AtomicExpr::Load) {
1108 PrintExpr(Node->getVal1());
1109 OS << ", ";
1110 }
1111 if (Node->isCmpXChg()) {
1112 PrintExpr(Node->getVal2());
1113 OS << ", ";
1114 }
David Chisnallfa35df62012-01-16 17:27:18 +00001115 if (Node->getOp() != AtomicExpr::Init)
1116 PrintExpr(Node->getOrder());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001117 if (Node->isCmpXChg()) {
1118 OS << ", ";
1119 PrintExpr(Node->getOrderFail());
1120 }
1121 OS << ")";
1122}
1123
Chris Lattnereefa10e2007-05-28 06:56:27 +00001124// C++
Douglas Gregor993603d2008-11-14 16:09:21 +00001125void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1126 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1127 "",
1128#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1129 Spelling,
1130#include "clang/Basic/OperatorKinds.def"
1131 };
1132
1133 OverloadedOperatorKind Kind = Node->getOperator();
1134 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1135 if (Node->getNumArgs() == 1) {
1136 OS << OpStrings[Kind] << ' ';
1137 PrintExpr(Node->getArg(0));
1138 } else {
1139 PrintExpr(Node->getArg(0));
1140 OS << ' ' << OpStrings[Kind];
1141 }
1142 } else if (Kind == OO_Call) {
1143 PrintExpr(Node->getArg(0));
1144 OS << '(';
1145 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1146 if (ArgIdx > 1)
1147 OS << ", ";
1148 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1149 PrintExpr(Node->getArg(ArgIdx));
1150 }
1151 OS << ')';
1152 } else if (Kind == OO_Subscript) {
1153 PrintExpr(Node->getArg(0));
1154 OS << '[';
1155 PrintExpr(Node->getArg(1));
1156 OS << ']';
1157 } else if (Node->getNumArgs() == 1) {
1158 OS << OpStrings[Kind] << ' ';
1159 PrintExpr(Node->getArg(0));
1160 } else if (Node->getNumArgs() == 2) {
1161 PrintExpr(Node->getArg(0));
1162 OS << ' ' << OpStrings[Kind] << ' ';
1163 PrintExpr(Node->getArg(1));
1164 } else {
David Blaikie83d382b2011-09-23 05:06:16 +00001165 llvm_unreachable("unknown overloaded operator");
Douglas Gregor993603d2008-11-14 16:09:21 +00001166 }
1167}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001168
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001169void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1170 VisitCallExpr(cast<CallExpr>(Node));
1171}
1172
Peter Collingbourne41f85462011-02-09 21:07:24 +00001173void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1174 PrintExpr(Node->getCallee());
1175 OS << "<<<";
1176 PrintCallArgs(Node->getConfig());
1177 OS << ">>>(";
1178 PrintCallArgs(Node);
1179 OS << ")";
1180}
1181
Douglas Gregore200adc2008-10-27 19:41:14 +00001182void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1183 OS << Node->getCastName() << '<';
Daniel Dunbar219fa692010-06-30 19:16:48 +00001184 OS << Node->getTypeAsWritten().getAsString(Policy) << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +00001185 PrintExpr(Node->getSubExpr());
1186 OS << ")";
1187}
1188
Douglas Gregore200adc2008-10-27 19:41:14 +00001189void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1190 VisitCXXNamedCastExpr(Node);
1191}
1192
1193void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1194 VisitCXXNamedCastExpr(Node);
1195}
1196
1197void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1198 VisitCXXNamedCastExpr(Node);
1199}
1200
1201void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1202 VisitCXXNamedCastExpr(Node);
1203}
1204
Sebastian Redlc4704762008-11-11 11:37:55 +00001205void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1206 OS << "typeid(";
1207 if (Node->isTypeOperand()) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001208 OS << Node->getTypeOperand().getAsString(Policy);
Sebastian Redlc4704762008-11-11 11:37:55 +00001209 } else {
1210 PrintExpr(Node->getExprOperand());
1211 }
1212 OS << ")";
1213}
1214
Francois Pichet9f4f2072010-09-08 12:20:18 +00001215void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1216 OS << "__uuidof(";
1217 if (Node->isTypeOperand()) {
1218 OS << Node->getTypeOperand().getAsString(Policy);
1219 } else {
1220 PrintExpr(Node->getExprOperand());
1221 }
1222 OS << ")";
1223}
1224
Chris Lattnereefa10e2007-05-28 06:56:27 +00001225void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1226 OS << (Node->getValue() ? "true" : "false");
1227}
1228
Sebastian Redl576fd422009-05-10 18:38:11 +00001229void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1230 OS << "nullptr";
1231}
1232
Douglas Gregor97a9c812008-11-04 14:32:21 +00001233void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1234 OS << "this";
1235}
1236
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001237void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1238 if (Node->getSubExpr() == 0)
1239 OS << "throw";
1240 else {
1241 OS << "throw ";
1242 PrintExpr(Node->getSubExpr());
1243 }
1244}
1245
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001246void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1247 // Nothing to print: we picked up the default argument
1248}
1249
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001250void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001251 OS << Node->getType().getAsString(Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001252 OS << "(";
1253 PrintExpr(Node->getSubExpr());
1254 OS << ")";
1255}
1256
Anders Carlsson993a4b32009-05-30 20:03:25 +00001257void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1258 PrintExpr(Node->getSubExpr());
1259}
1260
Douglas Gregordd04d332009-01-16 18:33:17 +00001261void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001262 OS << Node->getType().getAsString(Policy);
Douglas Gregordd04d332009-01-16 18:33:17 +00001263 OS << "(";
1264 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001265 ArgEnd = Node->arg_end();
Douglas Gregordd04d332009-01-16 18:33:17 +00001266 Arg != ArgEnd; ++Arg) {
1267 if (Arg != Node->arg_begin())
1268 OS << ", ";
1269 PrintExpr(*Arg);
1270 }
1271 OS << ")";
1272}
1273
Douglas Gregore31e6062012-02-07 10:09:13 +00001274void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1275 OS << '[';
1276 bool NeedComma = false;
1277 switch (Node->getCaptureDefault()) {
1278 case LCD_None:
1279 break;
1280
1281 case LCD_ByCopy:
1282 OS << '=';
1283 NeedComma = true;
1284 break;
1285
1286 case LCD_ByRef:
1287 OS << '&';
1288 NeedComma = true;
1289 break;
1290 }
1291 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1292 CEnd = Node->explicit_capture_end();
1293 C != CEnd;
1294 ++C) {
1295 if (NeedComma)
1296 OS << ", ";
1297 NeedComma = true;
1298
1299 switch (C->getCaptureKind()) {
1300 case LCK_This:
1301 OS << "this";
1302 break;
1303
1304 case LCK_ByRef:
1305 if (Node->getCaptureDefault() != LCD_ByRef)
1306 OS << '&';
1307 OS << C->getCapturedVar()->getName();
1308 break;
1309
1310 case LCK_ByCopy:
1311 if (Node->getCaptureDefault() != LCD_ByCopy)
1312 OS << '=';
1313 OS << C->getCapturedVar()->getName();
1314 break;
1315 }
1316 }
1317 OS << ']';
1318
1319 if (Node->hasExplicitParameters()) {
1320 OS << " (";
1321 CXXMethodDecl *Method = Node->getCallOperator();
1322 NeedComma = false;
1323 for (CXXMethodDecl::param_iterator P = Method->param_begin(),
1324 PEnd = Method->param_end();
1325 P != PEnd; ++P) {
1326 if (NeedComma) {
1327 OS << ", ";
1328 } else {
1329 NeedComma = true;
1330 }
1331 std::string ParamStr = (*P)->getNameAsString();
1332 (*P)->getOriginalType().getAsStringInternal(ParamStr, Policy);
1333 OS << ParamStr;
1334 }
1335 if (Method->isVariadic()) {
1336 if (NeedComma)
1337 OS << ", ";
1338 OS << "...";
1339 }
1340 OS << ')';
1341
1342 if (Node->isMutable())
1343 OS << " mutable";
1344
1345 const FunctionProtoType *Proto
1346 = Method->getType()->getAs<FunctionProtoType>();
1347 {
1348 std::string ExceptionSpec;
1349 Proto->printExceptionSpecification(ExceptionSpec, Policy);
1350 OS << ExceptionSpec;
1351 }
1352
1353 // FIXME: Attributes
1354
Douglas Gregor0c46b2b2012-02-13 22:00:16 +00001355 // Print the trailing return type if it was specified in the source.
1356 if (Node->hasExplicitResultType())
1357 OS << " -> " << Proto->getResultType().getAsString(Policy);
Douglas Gregore31e6062012-02-07 10:09:13 +00001358 }
1359
1360 // Print the body.
1361 CompoundStmt *Body = Node->getBody();
1362 OS << ' ';
1363 PrintStmt(Body);
1364}
1365
Douglas Gregor747eb782010-07-08 06:14:04 +00001366void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregor2b88c112010-09-08 00:15:04 +00001367 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
1368 OS << TSInfo->getType().getAsString(Policy) << "()";
1369 else
1370 OS << Node->getType().getAsString(Policy) << "()";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001371}
1372
Sebastian Redlbd150f42008-11-21 19:14:01 +00001373void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1374 if (E->isGlobalNew())
1375 OS << "::";
1376 OS << "new ";
1377 unsigned NumPlace = E->getNumPlacementArgs();
1378 if (NumPlace > 0) {
1379 OS << "(";
1380 PrintExpr(E->getPlacementArg(0));
1381 for (unsigned i = 1; i < NumPlace; ++i) {
1382 OS << ", ";
1383 PrintExpr(E->getPlacementArg(i));
1384 }
1385 OS << ") ";
1386 }
1387 if (E->isParenTypeId())
1388 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001389 std::string TypeS;
1390 if (Expr *Size = E->getArraySize()) {
1391 llvm::raw_string_ostream s(TypeS);
Eli Friedmanc4fc8392009-05-30 05:32:46 +00001392 Size->printPretty(s, Context, Helper, Policy);
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001393 s.flush();
1394 TypeS = "[" + TypeS + "]";
1395 }
Douglas Gregor7de59662009-05-29 20:38:28 +00001396 E->getAllocatedType().getAsStringInternal(TypeS, Policy);
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001397 OS << TypeS;
Sebastian Redlbd150f42008-11-21 19:14:01 +00001398 if (E->isParenTypeId())
1399 OS << ")";
1400
Sebastian Redl6047f072012-02-16 12:22:20 +00001401 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
1402 if (InitStyle) {
1403 if (InitStyle == CXXNewExpr::CallInit)
1404 OS << "(";
1405 PrintExpr(E->getInitializer());
1406 if (InitStyle == CXXNewExpr::CallInit)
1407 OS << ")";
Sebastian Redlbd150f42008-11-21 19:14:01 +00001408 }
1409}
1410
1411void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1412 if (E->isGlobalDelete())
1413 OS << "::";
1414 OS << "delete ";
1415 if (E->isArrayForm())
1416 OS << "[] ";
1417 PrintExpr(E->getArgument());
1418}
1419
Douglas Gregorad8a3362009-09-04 17:36:40 +00001420void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1421 PrintExpr(E->getBase());
1422 if (E->isArrow())
1423 OS << "->";
1424 else
1425 OS << '.';
1426 if (E->getQualifier())
1427 E->getQualifier()->print(OS, Policy);
Mike Stump11289f42009-09-09 15:08:12 +00001428
Douglas Gregorad8a3362009-09-04 17:36:40 +00001429 std::string TypeS;
Douglas Gregor678f90d2010-02-25 01:56:36 +00001430 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1431 OS << II->getName();
1432 else
1433 E->getDestroyedType().getAsStringInternal(TypeS, Policy);
Douglas Gregorad8a3362009-09-04 17:36:40 +00001434 OS << TypeS;
1435}
1436
Anders Carlsson0781ce72009-04-23 02:32:43 +00001437void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Douglas Gregorbaba85d2011-01-24 17:25:03 +00001438 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1439 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1440 // Don't print any defaulted arguments
1441 break;
1442 }
1443
1444 if (i) OS << ", ";
1445 PrintExpr(E->getArg(i));
Fariborz Jahaniand0bbf662010-01-13 21:41:11 +00001446 }
Anders Carlsson0781ce72009-04-23 02:32:43 +00001447}
1448
John McCall5d413782010-12-06 08:20:24 +00001449void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Anders Carlssondefc6442009-04-24 22:47:04 +00001450 // Just forward to the sub expression.
1451 PrintExpr(E->getSubExpr());
1452}
1453
Mike Stump11289f42009-09-09 15:08:12 +00001454void
Douglas Gregorce934142009-05-20 18:46:25 +00001455StmtPrinter::VisitCXXUnresolvedConstructExpr(
1456 CXXUnresolvedConstructExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001457 OS << Node->getTypeAsWritten().getAsString(Policy);
Douglas Gregorce934142009-05-20 18:46:25 +00001458 OS << "(";
1459 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001460 ArgEnd = Node->arg_end();
Douglas Gregorce934142009-05-20 18:46:25 +00001461 Arg != ArgEnd; ++Arg) {
1462 if (Arg != Node->arg_begin())
1463 OS << ", ";
1464 PrintExpr(*Arg);
1465 }
1466 OS << ")";
1467}
1468
John McCall8cd78132009-11-19 22:55:06 +00001469void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1470 CXXDependentScopeMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001471 if (!Node->isImplicitAccess()) {
1472 PrintExpr(Node->getBase());
1473 OS << (Node->isArrow() ? "->" : ".");
1474 }
Douglas Gregorc26e0f62009-09-03 16:14:30 +00001475 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1476 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001477 if (Node->hasTemplateKeyword())
Douglas Gregor308047d2009-09-09 00:23:06 +00001478 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001479 OS << Node->getMemberNameInfo();
John McCall2d74de92009-12-01 22:10:20 +00001480 if (Node->hasExplicitTemplateArgs()) {
Douglas Gregor308047d2009-09-09 00:23:06 +00001481 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1482 Node->getTemplateArgs(),
1483 Node->getNumTemplateArgs(),
1484 Policy);
1485 }
Douglas Gregora8db9542009-05-22 21:13:27 +00001486}
1487
John McCall10eae182009-11-30 22:42:35 +00001488void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001489 if (!Node->isImplicitAccess()) {
1490 PrintExpr(Node->getBase());
1491 OS << (Node->isArrow() ? "->" : ".");
1492 }
John McCall10eae182009-11-30 22:42:35 +00001493 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1494 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001495 if (Node->hasTemplateKeyword())
1496 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001497 OS << Node->getMemberNameInfo();
John McCall10eae182009-11-30 22:42:35 +00001498 if (Node->hasExplicitTemplateArgs()) {
1499 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1500 Node->getTemplateArgs(),
1501 Node->getNumTemplateArgs(),
1502 Policy);
1503 }
1504}
1505
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001506static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1507 switch (UTT) {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001508 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001509 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
John Wiegley65497cc2011-04-27 23:09:49 +00001510 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001511 case UTT_HasTrivialAssign: return "__has_trivial_assign";
Alexis Huntf479f1b2011-05-09 18:22:59 +00001512 case UTT_HasTrivialDefaultConstructor: return "__has_trivial_constructor";
John Wiegley65497cc2011-04-27 23:09:49 +00001513 case UTT_HasTrivialCopy: return "__has_trivial_copy";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001514 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1515 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1516 case UTT_IsAbstract: return "__is_abstract";
John Wiegley65497cc2011-04-27 23:09:49 +00001517 case UTT_IsArithmetic: return "__is_arithmetic";
1518 case UTT_IsArray: return "__is_array";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001519 case UTT_IsClass: return "__is_class";
John Wiegley65497cc2011-04-27 23:09:49 +00001520 case UTT_IsCompleteType: return "__is_complete_type";
1521 case UTT_IsCompound: return "__is_compound";
1522 case UTT_IsConst: return "__is_const";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001523 case UTT_IsEmpty: return "__is_empty";
1524 case UTT_IsEnum: return "__is_enum";
Douglas Gregordca70af2011-12-03 18:14:24 +00001525 case UTT_IsFinal: return "__is_final";
John Wiegley65497cc2011-04-27 23:09:49 +00001526 case UTT_IsFloatingPoint: return "__is_floating_point";
1527 case UTT_IsFunction: return "__is_function";
1528 case UTT_IsFundamental: return "__is_fundamental";
1529 case UTT_IsIntegral: return "__is_integral";
Chandler Carruth8e2d6f42011-05-01 07:23:20 +00001530 case UTT_IsLiteral: return "__is_literal";
John Wiegley65497cc2011-04-27 23:09:49 +00001531 case UTT_IsLvalueReference: return "__is_lvalue_reference";
1532 case UTT_IsMemberFunctionPointer: return "__is_member_function_pointer";
1533 case UTT_IsMemberObjectPointer: return "__is_member_object_pointer";
1534 case UTT_IsMemberPointer: return "__is_member_pointer";
1535 case UTT_IsObject: return "__is_object";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001536 case UTT_IsPOD: return "__is_pod";
John Wiegley65497cc2011-04-27 23:09:49 +00001537 case UTT_IsPointer: return "__is_pointer";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001538 case UTT_IsPolymorphic: return "__is_polymorphic";
John Wiegley65497cc2011-04-27 23:09:49 +00001539 case UTT_IsReference: return "__is_reference";
John Wiegley65497cc2011-04-27 23:09:49 +00001540 case UTT_IsRvalueReference: return "__is_rvalue_reference";
1541 case UTT_IsScalar: return "__is_scalar";
1542 case UTT_IsSigned: return "__is_signed";
1543 case UTT_IsStandardLayout: return "__is_standard_layout";
1544 case UTT_IsTrivial: return "__is_trivial";
Alexis Huntd9a5cc12011-05-13 00:31:07 +00001545 case UTT_IsTriviallyCopyable: return "__is_trivially_copyable";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001546 case UTT_IsUnion: return "__is_union";
John Wiegley65497cc2011-04-27 23:09:49 +00001547 case UTT_IsUnsigned: return "__is_unsigned";
1548 case UTT_IsVoid: return "__is_void";
1549 case UTT_IsVolatile: return "__is_volatile";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001550 }
Chandler Carruth8e2d6f42011-05-01 07:23:20 +00001551 llvm_unreachable("Type trait not covered by switch statement");
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001552}
1553
1554static const char *getTypeTraitName(BinaryTypeTrait BTT) {
1555 switch (BTT) {
Douglas Gregor1be329d2012-02-23 07:33:15 +00001556 case BTT_IsBaseOf: return "__is_base_of";
1557 case BTT_IsConvertible: return "__is_convertible";
1558 case BTT_IsSame: return "__is_same";
1559 case BTT_TypeCompatible: return "__builtin_types_compatible_p";
1560 case BTT_IsConvertibleTo: return "__is_convertible_to";
1561 case BTT_IsTriviallyAssignable: return "__is_trivially_assignable";
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001562 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001563 llvm_unreachable("Binary type trait not covered by switch");
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001564}
1565
Douglas Gregor29c42f22012-02-24 07:38:34 +00001566static const char *getTypeTraitName(TypeTrait TT) {
1567 switch (TT) {
1568 case clang::TT_IsTriviallyConstructible:return "__is_trivially_constructible";
1569 }
1570 llvm_unreachable("Type trait not covered by switch");
1571}
1572
John Wiegley6242b6a2011-04-28 00:16:57 +00001573static const char *getTypeTraitName(ArrayTypeTrait ATT) {
1574 switch (ATT) {
1575 case ATT_ArrayRank: return "__array_rank";
1576 case ATT_ArrayExtent: return "__array_extent";
1577 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001578 llvm_unreachable("Array type trait not covered by switch");
John Wiegley6242b6a2011-04-28 00:16:57 +00001579}
1580
John Wiegleyf9f65842011-04-25 06:54:41 +00001581static const char *getExpressionTraitName(ExpressionTrait ET) {
1582 switch (ET) {
John Wiegleyf9f65842011-04-25 06:54:41 +00001583 case ET_IsLValueExpr: return "__is_lvalue_expr";
1584 case ET_IsRValueExpr: return "__is_rvalue_expr";
1585 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001586 llvm_unreachable("Expression type trait not covered by switch");
John Wiegleyf9f65842011-04-25 06:54:41 +00001587}
1588
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001589void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1590 OS << getTypeTraitName(E->getTrait()) << "("
Daniel Dunbar219fa692010-06-30 19:16:48 +00001591 << E->getQueriedType().getAsString(Policy) << ")";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001592}
1593
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001594void StmtPrinter::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
1595 OS << getTypeTraitName(E->getTrait()) << "("
1596 << E->getLhsType().getAsString(Policy) << ","
1597 << E->getRhsType().getAsString(Policy) << ")";
1598}
1599
Douglas Gregor29c42f22012-02-24 07:38:34 +00001600void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
1601 OS << getTypeTraitName(E->getTrait()) << "(";
1602 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
1603 if (I > 0)
1604 OS << ", ";
1605 OS << E->getArg(I)->getType().getAsString(Policy);
1606 }
1607 OS << ")";
1608}
1609
John Wiegley6242b6a2011-04-28 00:16:57 +00001610void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
1611 OS << getTypeTraitName(E->getTrait()) << "("
1612 << E->getQueriedType().getAsString(Policy) << ")";
1613}
1614
John Wiegleyf9f65842011-04-25 06:54:41 +00001615void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
1616 OS << getExpressionTraitName(E->getTrait()) << "(";
1617 PrintExpr(E->getQueriedExpression());
1618 OS << ")";
1619}
1620
Sebastian Redl4202c0f2010-09-10 20:55:43 +00001621void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1622 OS << "noexcept(";
1623 PrintExpr(E->getOperand());
1624 OS << ")";
1625}
1626
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001627void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregore8e9dd62011-01-03 17:17:50 +00001628 PrintExpr(E->getPattern());
1629 OS << "...";
1630}
1631
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001632void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001633 OS << "sizeof...(" << *E->getPack() << ")";
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001634}
1635
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001636void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
1637 SubstNonTypeTemplateParmPackExpr *Node) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001638 OS << *Node->getParameterPack();
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001639}
1640
John McCall7c454bb2011-07-15 05:09:51 +00001641void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
1642 SubstNonTypeTemplateParmExpr *Node) {
1643 Visit(Node->getReplacement());
1644}
1645
Douglas Gregorfe314812011-06-21 17:03:29 +00001646void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
1647 PrintExpr(Node->GetTemporaryExpr());
1648}
1649
Mike Stump11289f42009-09-09 15:08:12 +00001650// Obj-C
Anders Carlsson76f4a902007-08-21 17:43:55 +00001651
1652void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1653 OS << "@";
1654 VisitStringLiteral(Node->getString());
1655}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001656
Ted Kremeneke65b0862012-03-06 20:05:56 +00001657void StmtPrinter::VisitObjCNumericLiteral(ObjCNumericLiteral *E) {
1658 OS << "@";
1659 Visit(E->getNumber());
1660}
1661
1662void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
1663 OS << "@[ ";
1664 StmtRange ch = E->children();
1665 if (ch.first != ch.second) {
1666 while (1) {
1667 Visit(*ch.first);
1668 ++ch.first;
1669 if (ch.first == ch.second) break;
1670 OS << ", ";
1671 }
1672 }
1673 OS << " ]";
1674}
1675
1676void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
1677 OS << "@{ ";
1678 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
1679 if (I > 0)
1680 OS << ", ";
1681
1682 ObjCDictionaryElement Element = E->getKeyValueElement(I);
1683 Visit(Element.Key);
1684 OS << " : ";
1685 Visit(Element.Value);
1686 if (Element.isPackExpansion())
1687 OS << "...";
1688 }
1689 OS << " }";
1690}
1691
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001692void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001693 OS << "@encode(" << Node->getEncodedType().getAsString(Policy) << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001694}
1695
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001696void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001697 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001698}
1699
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001700void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001701 OS << "@protocol(" << *Node->getProtocol() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001702}
1703
Steve Naroffd54978b2007-09-18 23:55:05 +00001704void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1705 OS << "[";
Douglas Gregor9a129192010-04-21 00:45:42 +00001706 switch (Mess->getReceiverKind()) {
1707 case ObjCMessageExpr::Instance:
1708 PrintExpr(Mess->getInstanceReceiver());
1709 break;
1710
1711 case ObjCMessageExpr::Class:
1712 OS << Mess->getClassReceiver().getAsString(Policy);
1713 break;
1714
1715 case ObjCMessageExpr::SuperInstance:
1716 case ObjCMessageExpr::SuperClass:
1717 OS << "Super";
1718 break;
1719 }
1720
Ted Kremeneka06e7122008-05-02 17:32:38 +00001721 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00001722 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001723 if (selector.isUnarySelector()) {
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00001724 OS << selector.getNameForSlot(0);
Steve Naroffc6814ea2007-10-02 20:01:56 +00001725 } else {
1726 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001727 if (i < selector.getNumArgs()) {
1728 if (i > 0) OS << ' ';
1729 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001730 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00001731 else
1732 OS << ":";
1733 }
1734 else OS << ", "; // Handle variadic methods.
Mike Stump11289f42009-09-09 15:08:12 +00001735
Steve Naroffc6814ea2007-10-02 20:01:56 +00001736 PrintExpr(Mess->getArg(i));
1737 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001738 }
1739 OS << "]";
1740}
1741
Ted Kremeneke65b0862012-03-06 20:05:56 +00001742void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
1743 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
1744}
1745
John McCall31168b02011-06-15 23:02:42 +00001746void
1747StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
1748 PrintExpr(E->getSubExpr());
1749}
1750
1751void
1752StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
1753 OS << "(" << E->getBridgeKindName() << E->getType().getAsString(Policy)
1754 << ")";
1755 PrintExpr(E->getSubExpr());
1756}
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001757
Steve Naroffc540d662008-09-03 18:15:37 +00001758void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001759 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00001760 OS << "^";
Mike Stump11289f42009-09-09 15:08:12 +00001761
Steve Naroffc540d662008-09-03 18:15:37 +00001762 const FunctionType *AFT = Node->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00001763
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001764 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroffc540d662008-09-03 18:15:37 +00001765 OS << "()";
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001766 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00001767 OS << '(';
1768 std::string ParamStr;
Steve Naroff415d3d52008-10-08 17:01:13 +00001769 for (BlockDecl::param_iterator AI = BD->param_begin(),
1770 E = BD->param_end(); AI != E; ++AI) {
1771 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001772 ParamStr = (*AI)->getNameAsString();
Douglas Gregor7de59662009-05-29 20:38:28 +00001773 (*AI)->getType().getAsStringInternal(ParamStr, Policy);
Steve Naroffc540d662008-09-03 18:15:37 +00001774 OS << ParamStr;
1775 }
Mike Stump11289f42009-09-09 15:08:12 +00001776
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001777 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00001778 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001779 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00001780 OS << "...";
1781 }
1782 OS << ')';
1783 }
1784}
1785
Steve Naroffc540d662008-09-03 18:15:37 +00001786void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001787 OS << *Node->getDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00001788}
John McCall8d69a212010-11-15 23:31:06 +00001789
Ted Kremenekf551f322011-11-30 22:08:08 +00001790void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
1791 PrintExpr(Node->getSourceExpr());
1792}
John McCall8d69a212010-11-15 23:31:06 +00001793
Tanya Lattner55808c12011-06-04 00:47:47 +00001794void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
1795 OS << "__builtin_astype(";
1796 PrintExpr(Node->getSrcExpr());
1797 OS << ", " << Node->getType().getAsString();
1798 OS << ")";
1799}
1800
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001801//===----------------------------------------------------------------------===//
1802// Stmt method implementations
1803//===----------------------------------------------------------------------===//
1804
Eli Friedmanef334fd2009-05-30 05:03:24 +00001805void Stmt::dumpPretty(ASTContext& Context) const {
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +00001806 printPretty(llvm::errs(), Context, 0,
Chris Lattnerc61089a2009-06-30 01:26:17 +00001807 PrintingPolicy(Context.getLangOptions()));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001808}
1809
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001810void Stmt::printPretty(raw_ostream &OS, ASTContext& Context,
Eli Friedmanef334fd2009-05-30 05:03:24 +00001811 PrinterHelper* Helper,
Douglas Gregor7de59662009-05-29 20:38:28 +00001812 const PrintingPolicy &Policy,
1813 unsigned Indentation) const {
Chris Lattner882f7882006-11-04 18:52:07 +00001814 if (this == 0) {
1815 OS << "<NULL>";
1816 return;
1817 }
1818
Douglas Gregor8655e882009-10-16 22:46:09 +00001819 if (Policy.Dump && &Context) {
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +00001820 dump(OS, Context.getSourceManager());
Douglas Gregor278f52e2009-05-30 00:08:05 +00001821 return;
1822 }
Mike Stump11289f42009-09-09 15:08:12 +00001823
Eli Friedmanef334fd2009-05-30 05:03:24 +00001824 StmtPrinter P(OS, Context, Helper, Policy, Indentation);
Chris Lattner62249a62007-08-21 04:04:25 +00001825 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001826}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001827
1828//===----------------------------------------------------------------------===//
1829// PrinterHelper
1830//===----------------------------------------------------------------------===//
1831
1832// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +00001833PrinterHelper::~PrinterHelper() {}