blob: ecf8ed751f517a15001e16a974725e1535a38116 [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"
Ted Kremenek2d470fc2008-09-13 05:16:45 +000020#include "llvm/Support/Format.h"
Douglas Gregor882211c2010-04-28 22:16:22 +000021#include "clang/AST/Expr.h"
Douglas Gregor2b88c112010-09-08 00:15:04 +000022#include "clang/AST/ExprCXX.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 Bagnarad6d2f182010-08-11 22:01:17 +0000547 OS << Node->getNameInfo();
John McCallb3774b52010-08-19 23:49:38 +0000548 if (Node->hasExplicitTemplateArgs())
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000549 OS << TemplateSpecializationType::PrintTemplateArgumentList(
550 Node->getTemplateArgs(),
551 Node->getNumTemplateArgs(),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000552 Policy);
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000553}
554
John McCall8cd78132009-11-19 22:55:06 +0000555void StmtPrinter::VisitDependentScopeDeclRefExpr(
556 DependentScopeDeclRefExpr *Node) {
Axel Naumann20b27862011-01-24 15:44:00 +0000557 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
558 Qualifier->print(OS, Policy);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000559 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000560 if (Node->hasExplicitTemplateArgs())
561 OS << TemplateSpecializationType::PrintTemplateArgumentList(
562 Node->getTemplateArgs(),
563 Node->getNumTemplateArgs(),
564 Policy);
Douglas Gregor90a1a652009-03-19 17:26:29 +0000565}
566
John McCalld14a8642009-11-21 08:51:07 +0000567void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregora727cb92009-06-30 22:34:41 +0000568 if (Node->getQualifier())
569 Node->getQualifier()->print(OS, Policy);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000570 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000571 if (Node->hasExplicitTemplateArgs())
572 OS << TemplateSpecializationType::PrintTemplateArgumentList(
573 Node->getTemplateArgs(),
Douglas Gregora727cb92009-06-30 22:34:41 +0000574 Node->getNumTemplateArgs(),
John McCalle66edc12009-11-24 19:00:30 +0000575 Policy);
Douglas Gregora727cb92009-06-30 22:34:41 +0000576}
577
Steve Naroffe46504b2007-11-12 14:29:37 +0000578void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000579 if (Node->getBase()) {
580 PrintExpr(Node->getBase());
581 OS << (Node->isArrow() ? "->" : ".");
582 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000583 OS << *Node->getDecl();
Steve Naroffe46504b2007-11-12 14:29:37 +0000584}
585
Steve Naroffec944032008-05-30 00:40:33 +0000586void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000587 if (Node->isSuperReceiver())
588 OS << "super.";
589 else if (Node->getBase()) {
Steve Naroffec944032008-05-30 00:40:33 +0000590 PrintExpr(Node->getBase());
591 OS << ".";
592 }
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000593
John McCallb7bd14f2010-12-02 01:19:52 +0000594 if (Node->isImplicitProperty())
595 OS << Node->getImplicitPropertyGetter()->getSelector().getAsString();
596 else
597 OS << Node->getExplicitProperty()->getName();
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000598}
599
Chris Lattner6307f192008-08-10 01:53:14 +0000600void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson625bfc82007-07-21 05:21:51 +0000601 switch (Node->getIdentType()) {
602 default:
David Blaikie83d382b2011-09-23 05:06:16 +0000603 llvm_unreachable("unknown case");
Chris Lattner6307f192008-08-10 01:53:14 +0000604 case PredefinedExpr::Func:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000605 OS << "__func__";
606 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000607 case PredefinedExpr::Function:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000608 OS << "__FUNCTION__";
609 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000610 case PredefinedExpr::PrettyFunction:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000611 OS << "__PRETTY_FUNCTION__";
612 break;
613 }
614}
615
Steve Naroffae4143e2007-04-26 20:39:23 +0000616void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000617 unsigned value = Node->getValue();
Douglas Gregorfb65e592011-07-27 05:40:30 +0000618
619 switch (Node->getKind()) {
620 case CharacterLiteral::Ascii: break; // no prefix.
621 case CharacterLiteral::Wide: OS << 'L'; break;
622 case CharacterLiteral::UTF16: OS << 'u'; break;
623 case CharacterLiteral::UTF32: OS << 'U'; break;
624 }
625
Chris Lattner666115c2007-07-13 23:58:20 +0000626 switch (value) {
627 case '\\':
628 OS << "'\\\\'";
629 break;
630 case '\'':
631 OS << "'\\''";
632 break;
633 case '\a':
634 // TODO: K&R: the meaning of '\\a' is different in traditional C
635 OS << "'\\a'";
636 break;
637 case '\b':
638 OS << "'\\b'";
639 break;
640 // Nonstandard escape sequence.
641 /*case '\e':
642 OS << "'\\e'";
643 break;*/
644 case '\f':
645 OS << "'\\f'";
646 break;
647 case '\n':
648 OS << "'\\n'";
649 break;
650 case '\r':
651 OS << "'\\r'";
652 break;
653 case '\t':
654 OS << "'\\t'";
655 break;
656 case '\v':
657 OS << "'\\v'";
658 break;
659 default:
Ted Kremenek652d18e2008-02-23 00:52:04 +0000660 if (value < 256 && isprint(value)) {
Chris Lattner666115c2007-07-13 23:58:20 +0000661 OS << "'" << (char)value << "'";
662 } else if (value < 256) {
Ted Kremenek2d470fc2008-09-13 05:16:45 +0000663 OS << "'\\x" << llvm::format("%x", value) << "'";
Chris Lattner666115c2007-07-13 23:58:20 +0000664 } else {
665 // FIXME what to really do here?
666 OS << value;
667 }
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000668 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000669}
670
Steve Naroffdf7855b2007-02-21 23:46:25 +0000671void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000672 bool isSigned = Node->getType()->isSignedIntegerType();
673 OS << Node->getValue().toString(10, isSigned);
Mike Stump11289f42009-09-09 15:08:12 +0000674
Chris Lattner06430412007-05-21 05:45:03 +0000675 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall9dd450b2009-09-21 23:43:11 +0000676 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
David Blaikie83d382b2011-09-23 05:06:16 +0000677 default: llvm_unreachable("Unexpected type for integer literal!");
Richard Trieu8b626ba2011-11-07 18:40:31 +0000678 // FIXME: The Short and UShort cases are to handle cases where a short
679 // integeral literal is formed during template instantiation. They should
680 // be removed when template instantiation no longer needs integer literals.
681 case BuiltinType::Short:
682 case BuiltinType::UShort:
Chris Lattner06430412007-05-21 05:45:03 +0000683 case BuiltinType::Int: break; // no suffix.
684 case BuiltinType::UInt: OS << 'U'; break;
685 case BuiltinType::Long: OS << 'L'; break;
686 case BuiltinType::ULong: OS << "UL"; break;
687 case BuiltinType::LongLong: OS << "LL"; break;
688 case BuiltinType::ULongLong: OS << "ULL"; break;
Richard Trieu8b626ba2011-11-07 18:40:31 +0000689 case BuiltinType::Int128: OS << "i128"; break;
690 case BuiltinType::UInt128: OS << "Ui128"; break;
Chris Lattner06430412007-05-21 05:45:03 +0000691 }
Chris Lattner882f7882006-11-04 18:52:07 +0000692}
Steve Naroffab624882007-02-21 22:05:47 +0000693void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Eli Friedman53392062011-10-05 20:32:03 +0000694 llvm::SmallString<16> Str;
695 Node->getValue().toString(Str);
696 OS << Str;
Chris Lattner882f7882006-11-04 18:52:07 +0000697}
Chris Lattner1c20a172007-08-26 03:42:43 +0000698
699void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
700 PrintExpr(Node->getSubExpr());
701 OS << "i";
702}
703
Steve Naroffdf7855b2007-02-21 23:46:25 +0000704void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Douglas Gregorfb65e592011-07-27 05:40:30 +0000705 switch (Str->getKind()) {
706 case StringLiteral::Ascii: break; // no prefix.
707 case StringLiteral::Wide: OS << 'L'; break;
708 case StringLiteral::UTF8: OS << "u8"; break;
709 case StringLiteral::UTF16: OS << 'u'; break;
710 case StringLiteral::UTF32: OS << 'U'; break;
711 }
Chris Lattner5d8f4942006-11-04 20:29:31 +0000712 OS << '"';
Richard Smith88eb4d32011-12-30 23:37:31 +0000713 static char Hex[] = "0123456789ABCDEF";
Anders Carlssoncbfc4b82007-10-15 02:50:23 +0000714
Richard Smith88eb4d32011-12-30 23:37:31 +0000715 for (unsigned I = 0, N = Str->getLength(); I != N; ++I) {
716 switch (uint32_t Char = Str->getCodeUnit(I)) {
Chris Lattnerbb96cc42009-01-16 19:25:18 +0000717 default:
Richard Smith88eb4d32011-12-30 23:37:31 +0000718 // FIXME: Is this the best way to print wchar_t?
719 if (Char > 0xff) {
Richard Smithdbfd4032012-01-02 18:14:06 +0000720 assert(Char <= 0x10ffff && "invalid unicode codepoint");
Richard Smith88eb4d32011-12-30 23:37:31 +0000721 if (Char > 0xffff)
722 OS << "\\U00"
723 << Hex[(Char >> 20) & 15]
724 << Hex[(Char >> 16) & 15];
725 else
726 OS << "\\u";
727 OS << Hex[(Char >> 12) & 15]
728 << Hex[(Char >> 8) & 15]
729 << Hex[(Char >> 4) & 15]
730 << Hex[(Char >> 0) & 15];
731 break;
732 }
733 if (Char <= 0xff && isprint(Char))
Chris Lattnerbb96cc42009-01-16 19:25:18 +0000734 OS << (char)Char;
735 else // Output anything hard as an octal escape.
736 OS << '\\'
737 << (char)('0'+ ((Char >> 6) & 7))
738 << (char)('0'+ ((Char >> 3) & 7))
739 << (char)('0'+ ((Char >> 0) & 7));
740 break;
741 // Handle some common non-printable cases to make dumps prettier.
Chris Lattner5d8f4942006-11-04 20:29:31 +0000742 case '\\': OS << "\\\\"; break;
743 case '"': OS << "\\\""; break;
744 case '\n': OS << "\\n"; break;
745 case '\t': OS << "\\t"; break;
746 case '\a': OS << "\\a"; break;
747 case '\b': OS << "\\b"; break;
748 }
749 }
750 OS << '"';
Chris Lattner882f7882006-11-04 18:52:07 +0000751}
752void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
753 OS << "(";
754 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000755 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000756}
757void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +0000758 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +0000759 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump11289f42009-09-09 15:08:12 +0000760
Eli Friedman1cf25362009-06-14 22:39:26 +0000761 // Print a space if this is an "identifier operator" like __real, or if
762 // it might be concatenated incorrectly like '+'.
Chris Lattnere5b60442007-08-23 21:46:40 +0000763 switch (Node->getOpcode()) {
764 default: break;
John McCalle3027922010-08-25 11:45:40 +0000765 case UO_Real:
766 case UO_Imag:
767 case UO_Extension:
Chris Lattnere5b60442007-08-23 21:46:40 +0000768 OS << ' ';
769 break;
John McCalle3027922010-08-25 11:45:40 +0000770 case UO_Plus:
771 case UO_Minus:
Eli Friedman1cf25362009-06-14 22:39:26 +0000772 if (isa<UnaryOperator>(Node->getSubExpr()))
773 OS << ' ';
774 break;
Chris Lattnere5b60442007-08-23 21:46:40 +0000775 }
776 }
Chris Lattner882f7882006-11-04 18:52:07 +0000777 PrintExpr(Node->getSubExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000778
Chris Lattner15768702006-11-05 23:54:51 +0000779 if (Node->isPostfix())
780 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000781}
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000782
Douglas Gregor882211c2010-04-28 22:16:22 +0000783void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
784 OS << "__builtin_offsetof(";
Daniel Dunbar219fa692010-06-30 19:16:48 +0000785 OS << Node->getTypeSourceInfo()->getType().getAsString(Policy) << ", ";
Douglas Gregor882211c2010-04-28 22:16:22 +0000786 bool PrintedSomething = false;
787 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
788 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
789 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
790 // Array node
791 OS << "[";
792 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
793 OS << "]";
794 PrintedSomething = true;
795 continue;
796 }
Douglas Gregord1702062010-04-29 00:18:15 +0000797
798 // Skip implicit base indirections.
799 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
800 continue;
801
Douglas Gregor882211c2010-04-28 22:16:22 +0000802 // Field or identifier node.
803 IdentifierInfo *Id = ON.getFieldName();
804 if (!Id)
805 continue;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000806
Douglas Gregor882211c2010-04-28 22:16:22 +0000807 if (PrintedSomething)
808 OS << ".";
809 else
810 PrintedSomething = true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000811 OS << Id->getName();
Douglas Gregor882211c2010-04-28 22:16:22 +0000812 }
813 OS << ")";
814}
815
Peter Collingbournee190dee2011-03-11 19:24:49 +0000816void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
817 switch(Node->getKind()) {
818 case UETT_SizeOf:
819 OS << "sizeof";
820 break;
821 case UETT_AlignOf:
822 OS << "__alignof";
823 break;
824 case UETT_VecStep:
825 OS << "vec_step";
826 break;
827 }
Sebastian Redl6f282892008-11-11 17:56:53 +0000828 if (Node->isArgumentType())
Daniel Dunbar219fa692010-06-30 19:16:48 +0000829 OS << "(" << Node->getArgumentType().getAsString(Policy) << ")";
Sebastian Redl6f282892008-11-11 17:56:53 +0000830 else {
831 OS << " ";
832 PrintExpr(Node->getArgumentExpr());
833 }
Chris Lattner882f7882006-11-04 18:52:07 +0000834}
Peter Collingbourne91147592011-04-15 00:35:48 +0000835
836void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
837 OS << "_Generic(";
838 PrintExpr(Node->getControllingExpr());
839 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
840 OS << ", ";
841 QualType T = Node->getAssocType(i);
842 if (T.isNull())
843 OS << "default";
844 else
845 OS << T.getAsString(Policy);
846 OS << ": ";
847 PrintExpr(Node->getAssocExpr(i));
848 }
849 OS << ")";
850}
851
Chris Lattner882f7882006-11-04 18:52:07 +0000852void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000853 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000854 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +0000855 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000856 OS << "]";
857}
858
Peter Collingbourne4b279a02011-02-08 21:17:54 +0000859void StmtPrinter::PrintCallArgs(CallExpr *Call) {
Chris Lattner882f7882006-11-04 18:52:07 +0000860 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000861 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
862 // Don't print any defaulted arguments
863 break;
864 }
865
Chris Lattner882f7882006-11-04 18:52:07 +0000866 if (i) OS << ", ";
867 PrintExpr(Call->getArg(i));
868 }
Peter Collingbourne4b279a02011-02-08 21:17:54 +0000869}
870
871void StmtPrinter::VisitCallExpr(CallExpr *Call) {
872 PrintExpr(Call->getCallee());
873 OS << "(";
874 PrintCallArgs(Call);
Chris Lattner882f7882006-11-04 18:52:07 +0000875 OS << ")";
876}
877void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregore4b414c2009-01-08 22:45:41 +0000878 // FIXME: Suppress printing implicit bases (like "this")
879 PrintExpr(Node->getBase());
Fariborz Jahanian2990c022010-01-11 21:17:32 +0000880 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
881 if (FD->isAnonymousStructOrUnion())
882 return;
Douglas Gregore4b414c2009-01-08 22:45:41 +0000883 OS << (Node->isArrow() ? "->" : ".");
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000884 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
885 Qualifier->print(OS, Policy);
886
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000887 OS << Node->getMemberNameInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000888
John McCallb3774b52010-08-19 23:49:38 +0000889 if (Node->hasExplicitTemplateArgs())
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000890 OS << TemplateSpecializationType::PrintTemplateArgumentList(
891 Node->getTemplateArgs(),
892 Node->getNumTemplateArgs(),
893 Policy);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000894}
Steve Naroffe87026a2009-07-24 17:54:45 +0000895void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
896 PrintExpr(Node->getBase());
897 OS << (Node->isArrow() ? "->isa" : ".isa");
898}
899
Nate Begemance4d7fc2008-04-18 23:10:10 +0000900void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +0000901 PrintExpr(Node->getBase());
902 OS << ".";
903 OS << Node->getAccessor().getName();
904}
Douglas Gregorf19b2312008-10-28 15:36:24 +0000905void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Fariborz Jahaniane33c1162010-06-30 16:31:08 +0000906 OS << "(" << Node->getType().getAsString(Policy) << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000907 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000908}
Steve Naroff57eb2c52007-07-19 21:32:11 +0000909void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +0000910 OS << "(" << Node->getType().getAsString(Policy) << ")";
Steve Naroff57eb2c52007-07-19 21:32:11 +0000911 PrintExpr(Node->getInitializer());
912}
Steve Naroff7a5af782007-07-13 16:58:59 +0000913void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000914 // No need to print anything, simply forward to the sub expression.
915 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +0000916}
Chris Lattner882f7882006-11-04 18:52:07 +0000917void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
918 PrintExpr(Node->getLHS());
919 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
920 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000921}
Chris Lattner86928112007-08-25 02:00:02 +0000922void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
923 PrintExpr(Node->getLHS());
924 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
925 PrintExpr(Node->getRHS());
926}
Chris Lattner882f7882006-11-04 18:52:07 +0000927void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
928 PrintExpr(Node->getCond());
John McCallc07a0c72011-02-17 10:25:35 +0000929 OS << " ? ";
930 PrintExpr(Node->getLHS());
931 OS << " : ";
Chris Lattner882f7882006-11-04 18:52:07 +0000932 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000933}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000934
Chris Lattnereefa10e2007-05-28 06:56:27 +0000935// GNU extensions.
936
John McCallc07a0c72011-02-17 10:25:35 +0000937void
938StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
939 PrintExpr(Node->getCommon());
940 OS << " ?: ";
941 PrintExpr(Node->getFalseExpr());
942}
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000943void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000944 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000945}
946
Chris Lattner366727f2007-07-24 16:58:17 +0000947void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
948 OS << "(";
949 PrintRawCompoundStmt(E->getSubStmt());
950 OS << ")";
951}
952
Steve Naroff9efdabc2007-08-03 21:21:27 +0000953void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
954 OS << "__builtin_choose_expr(";
955 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +0000956 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000957 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +0000958 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000959 PrintExpr(Node->getRHS());
960 OS << ")";
961}
Chris Lattner366727f2007-07-24 16:58:17 +0000962
Douglas Gregor3be4b122008-11-29 04:51:27 +0000963void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
964 OS << "__null";
965}
966
Eli Friedmana1b4ed82008-05-14 19:38:39 +0000967void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
968 OS << "__builtin_shufflevector(";
969 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
970 if (i) OS << ", ";
971 PrintExpr(Node->getExpr(i));
972 }
973 OS << ")";
974}
975
Anders Carlsson4692db02007-08-31 04:56:16 +0000976void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor36098ff2009-05-30 00:56:08 +0000977 if (Node->getSyntacticForm()) {
978 Visit(Node->getSyntacticForm());
979 return;
980 }
981
Anders Carlsson4692db02007-08-31 04:56:16 +0000982 OS << "{ ";
983 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
984 if (i) OS << ", ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000985 if (Node->getInit(i))
986 PrintExpr(Node->getInit(i));
987 else
988 OS << "0";
Anders Carlsson4692db02007-08-31 04:56:16 +0000989 }
990 OS << " }";
991}
992
Nate Begeman5ec4b312009-08-10 23:49:36 +0000993void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
994 OS << "( ";
995 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
996 if (i) OS << ", ";
997 PrintExpr(Node->getExpr(i));
998 }
999 OS << " )";
1000}
1001
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001002void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001003 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
1004 DEnd = Node->designators_end();
1005 D != DEnd; ++D) {
1006 if (D->isFieldDesignator()) {
1007 if (D->getDotLoc().isInvalid())
1008 OS << D->getFieldName()->getName() << ":";
1009 else
1010 OS << "." << D->getFieldName()->getName();
1011 } else {
1012 OS << "[";
1013 if (D->isArrayDesignator()) {
1014 PrintExpr(Node->getArrayIndex(*D));
1015 } else {
1016 PrintExpr(Node->getArrayRangeStart(*D));
1017 OS << " ... ";
Mike Stump11289f42009-09-09 15:08:12 +00001018 PrintExpr(Node->getArrayRangeEnd(*D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001019 }
1020 OS << "]";
1021 }
1022 }
1023
1024 OS << " = ";
1025 PrintExpr(Node->getInit());
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001026}
1027
Douglas Gregor0202cb42009-01-29 17:44:32 +00001028void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Chris Lattnerc61089a2009-06-30 01:26:17 +00001029 if (Policy.LangOpts.CPlusPlus)
Douglas Gregor278f52e2009-05-30 00:08:05 +00001030 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()";
1031 else {
1032 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")";
1033 if (Node->getType()->isRecordType())
1034 OS << "{}";
1035 else
1036 OS << 0;
1037 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001038}
1039
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001040void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman79635842009-05-30 04:20:30 +00001041 OS << "__builtin_va_arg(";
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001042 PrintExpr(Node->getSubExpr());
1043 OS << ", ";
Daniel Dunbar219fa692010-06-30 19:16:48 +00001044 OS << Node->getType().getAsString(Policy);
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001045 OS << ")";
1046}
1047
John McCallfe96e0b2011-11-06 09:01:30 +00001048void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1049 PrintExpr(Node->getSyntacticForm());
1050}
1051
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001052void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
Eli Friedmanc2025562011-10-11 20:00:47 +00001053 const char *Name = 0;
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001054 switch (Node->getOp()) {
David Chisnallfa35df62012-01-16 17:27:18 +00001055 case AtomicExpr::Init:
1056 Name = "__atomic_init(";
1057 break;
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001058 case AtomicExpr::Load:
1059 Name = "__atomic_load(";
1060 break;
1061 case AtomicExpr::Store:
1062 Name = "__atomic_store(";
1063 break;
1064 case AtomicExpr::CmpXchgStrong:
1065 Name = "__atomic_compare_exchange_strong(";
1066 break;
1067 case AtomicExpr::CmpXchgWeak:
1068 Name = "__atomic_compare_exchange_weak(";
1069 break;
1070 case AtomicExpr::Xchg:
1071 Name = "__atomic_exchange(";
1072 break;
1073 case AtomicExpr::Add:
1074 Name = "__atomic_fetch_add(";
1075 break;
1076 case AtomicExpr::Sub:
1077 Name = "__atomic_fetch_sub(";
1078 break;
1079 case AtomicExpr::And:
1080 Name = "__atomic_fetch_and(";
1081 break;
1082 case AtomicExpr::Or:
1083 Name = "__atomic_fetch_or(";
1084 break;
1085 case AtomicExpr::Xor:
1086 Name = "__atomic_fetch_xor(";
1087 break;
1088 }
1089 OS << Name;
1090 PrintExpr(Node->getPtr());
1091 OS << ", ";
1092 if (Node->getOp() != AtomicExpr::Load) {
1093 PrintExpr(Node->getVal1());
1094 OS << ", ";
1095 }
1096 if (Node->isCmpXChg()) {
1097 PrintExpr(Node->getVal2());
1098 OS << ", ";
1099 }
David Chisnallfa35df62012-01-16 17:27:18 +00001100 if (Node->getOp() != AtomicExpr::Init)
1101 PrintExpr(Node->getOrder());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001102 if (Node->isCmpXChg()) {
1103 OS << ", ";
1104 PrintExpr(Node->getOrderFail());
1105 }
1106 OS << ")";
1107}
1108
Chris Lattnereefa10e2007-05-28 06:56:27 +00001109// C++
Douglas Gregor993603d2008-11-14 16:09:21 +00001110void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1111 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1112 "",
1113#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1114 Spelling,
1115#include "clang/Basic/OperatorKinds.def"
1116 };
1117
1118 OverloadedOperatorKind Kind = Node->getOperator();
1119 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1120 if (Node->getNumArgs() == 1) {
1121 OS << OpStrings[Kind] << ' ';
1122 PrintExpr(Node->getArg(0));
1123 } else {
1124 PrintExpr(Node->getArg(0));
1125 OS << ' ' << OpStrings[Kind];
1126 }
1127 } else if (Kind == OO_Call) {
1128 PrintExpr(Node->getArg(0));
1129 OS << '(';
1130 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1131 if (ArgIdx > 1)
1132 OS << ", ";
1133 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1134 PrintExpr(Node->getArg(ArgIdx));
1135 }
1136 OS << ')';
1137 } else if (Kind == OO_Subscript) {
1138 PrintExpr(Node->getArg(0));
1139 OS << '[';
1140 PrintExpr(Node->getArg(1));
1141 OS << ']';
1142 } else if (Node->getNumArgs() == 1) {
1143 OS << OpStrings[Kind] << ' ';
1144 PrintExpr(Node->getArg(0));
1145 } else if (Node->getNumArgs() == 2) {
1146 PrintExpr(Node->getArg(0));
1147 OS << ' ' << OpStrings[Kind] << ' ';
1148 PrintExpr(Node->getArg(1));
1149 } else {
David Blaikie83d382b2011-09-23 05:06:16 +00001150 llvm_unreachable("unknown overloaded operator");
Douglas Gregor993603d2008-11-14 16:09:21 +00001151 }
1152}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001153
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001154void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1155 VisitCallExpr(cast<CallExpr>(Node));
1156}
1157
Peter Collingbourne41f85462011-02-09 21:07:24 +00001158void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1159 PrintExpr(Node->getCallee());
1160 OS << "<<<";
1161 PrintCallArgs(Node->getConfig());
1162 OS << ">>>(";
1163 PrintCallArgs(Node);
1164 OS << ")";
1165}
1166
Douglas Gregore200adc2008-10-27 19:41:14 +00001167void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1168 OS << Node->getCastName() << '<';
Daniel Dunbar219fa692010-06-30 19:16:48 +00001169 OS << Node->getTypeAsWritten().getAsString(Policy) << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +00001170 PrintExpr(Node->getSubExpr());
1171 OS << ")";
1172}
1173
Douglas Gregore200adc2008-10-27 19:41:14 +00001174void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1175 VisitCXXNamedCastExpr(Node);
1176}
1177
1178void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1179 VisitCXXNamedCastExpr(Node);
1180}
1181
1182void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1183 VisitCXXNamedCastExpr(Node);
1184}
1185
1186void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1187 VisitCXXNamedCastExpr(Node);
1188}
1189
Sebastian Redlc4704762008-11-11 11:37:55 +00001190void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1191 OS << "typeid(";
1192 if (Node->isTypeOperand()) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001193 OS << Node->getTypeOperand().getAsString(Policy);
Sebastian Redlc4704762008-11-11 11:37:55 +00001194 } else {
1195 PrintExpr(Node->getExprOperand());
1196 }
1197 OS << ")";
1198}
1199
Francois Pichet9f4f2072010-09-08 12:20:18 +00001200void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1201 OS << "__uuidof(";
1202 if (Node->isTypeOperand()) {
1203 OS << Node->getTypeOperand().getAsString(Policy);
1204 } else {
1205 PrintExpr(Node->getExprOperand());
1206 }
1207 OS << ")";
1208}
1209
Chris Lattnereefa10e2007-05-28 06:56:27 +00001210void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1211 OS << (Node->getValue() ? "true" : "false");
1212}
1213
Sebastian Redl576fd422009-05-10 18:38:11 +00001214void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1215 OS << "nullptr";
1216}
1217
Douglas Gregor97a9c812008-11-04 14:32:21 +00001218void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1219 OS << "this";
1220}
1221
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001222void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1223 if (Node->getSubExpr() == 0)
1224 OS << "throw";
1225 else {
1226 OS << "throw ";
1227 PrintExpr(Node->getSubExpr());
1228 }
1229}
1230
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001231void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1232 // Nothing to print: we picked up the default argument
1233}
1234
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001235void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001236 OS << Node->getType().getAsString(Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001237 OS << "(";
1238 PrintExpr(Node->getSubExpr());
1239 OS << ")";
1240}
1241
Anders Carlsson993a4b32009-05-30 20:03:25 +00001242void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1243 PrintExpr(Node->getSubExpr());
1244}
1245
Douglas Gregordd04d332009-01-16 18:33:17 +00001246void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001247 OS << Node->getType().getAsString(Policy);
Douglas Gregordd04d332009-01-16 18:33:17 +00001248 OS << "(";
1249 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001250 ArgEnd = Node->arg_end();
Douglas Gregordd04d332009-01-16 18:33:17 +00001251 Arg != ArgEnd; ++Arg) {
1252 if (Arg != Node->arg_begin())
1253 OS << ", ";
1254 PrintExpr(*Arg);
1255 }
1256 OS << ")";
1257}
1258
Douglas Gregor747eb782010-07-08 06:14:04 +00001259void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregor2b88c112010-09-08 00:15:04 +00001260 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
1261 OS << TSInfo->getType().getAsString(Policy) << "()";
1262 else
1263 OS << Node->getType().getAsString(Policy) << "()";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001264}
1265
Sebastian Redlbd150f42008-11-21 19:14:01 +00001266void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1267 if (E->isGlobalNew())
1268 OS << "::";
1269 OS << "new ";
1270 unsigned NumPlace = E->getNumPlacementArgs();
1271 if (NumPlace > 0) {
1272 OS << "(";
1273 PrintExpr(E->getPlacementArg(0));
1274 for (unsigned i = 1; i < NumPlace; ++i) {
1275 OS << ", ";
1276 PrintExpr(E->getPlacementArg(i));
1277 }
1278 OS << ") ";
1279 }
1280 if (E->isParenTypeId())
1281 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001282 std::string TypeS;
1283 if (Expr *Size = E->getArraySize()) {
1284 llvm::raw_string_ostream s(TypeS);
Eli Friedmanc4fc8392009-05-30 05:32:46 +00001285 Size->printPretty(s, Context, Helper, Policy);
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001286 s.flush();
1287 TypeS = "[" + TypeS + "]";
1288 }
Douglas Gregor7de59662009-05-29 20:38:28 +00001289 E->getAllocatedType().getAsStringInternal(TypeS, Policy);
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001290 OS << TypeS;
Sebastian Redlbd150f42008-11-21 19:14:01 +00001291 if (E->isParenTypeId())
1292 OS << ")";
1293
1294 if (E->hasInitializer()) {
1295 OS << "(";
1296 unsigned NumCons = E->getNumConstructorArgs();
1297 if (NumCons > 0) {
1298 PrintExpr(E->getConstructorArg(0));
1299 for (unsigned i = 1; i < NumCons; ++i) {
1300 OS << ", ";
1301 PrintExpr(E->getConstructorArg(i));
1302 }
1303 }
1304 OS << ")";
1305 }
1306}
1307
1308void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1309 if (E->isGlobalDelete())
1310 OS << "::";
1311 OS << "delete ";
1312 if (E->isArrayForm())
1313 OS << "[] ";
1314 PrintExpr(E->getArgument());
1315}
1316
Douglas Gregorad8a3362009-09-04 17:36:40 +00001317void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1318 PrintExpr(E->getBase());
1319 if (E->isArrow())
1320 OS << "->";
1321 else
1322 OS << '.';
1323 if (E->getQualifier())
1324 E->getQualifier()->print(OS, Policy);
Mike Stump11289f42009-09-09 15:08:12 +00001325
Douglas Gregorad8a3362009-09-04 17:36:40 +00001326 std::string TypeS;
Douglas Gregor678f90d2010-02-25 01:56:36 +00001327 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1328 OS << II->getName();
1329 else
1330 E->getDestroyedType().getAsStringInternal(TypeS, Policy);
Douglas Gregorad8a3362009-09-04 17:36:40 +00001331 OS << TypeS;
1332}
1333
Anders Carlsson0781ce72009-04-23 02:32:43 +00001334void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Douglas Gregorbaba85d2011-01-24 17:25:03 +00001335 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1336 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1337 // Don't print any defaulted arguments
1338 break;
1339 }
1340
1341 if (i) OS << ", ";
1342 PrintExpr(E->getArg(i));
Fariborz Jahaniand0bbf662010-01-13 21:41:11 +00001343 }
Anders Carlsson0781ce72009-04-23 02:32:43 +00001344}
1345
John McCall5d413782010-12-06 08:20:24 +00001346void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Anders Carlssondefc6442009-04-24 22:47:04 +00001347 // Just forward to the sub expression.
1348 PrintExpr(E->getSubExpr());
1349}
1350
Mike Stump11289f42009-09-09 15:08:12 +00001351void
Douglas Gregorce934142009-05-20 18:46:25 +00001352StmtPrinter::VisitCXXUnresolvedConstructExpr(
1353 CXXUnresolvedConstructExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001354 OS << Node->getTypeAsWritten().getAsString(Policy);
Douglas Gregorce934142009-05-20 18:46:25 +00001355 OS << "(";
1356 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001357 ArgEnd = Node->arg_end();
Douglas Gregorce934142009-05-20 18:46:25 +00001358 Arg != ArgEnd; ++Arg) {
1359 if (Arg != Node->arg_begin())
1360 OS << ", ";
1361 PrintExpr(*Arg);
1362 }
1363 OS << ")";
1364}
1365
John McCall8cd78132009-11-19 22:55:06 +00001366void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1367 CXXDependentScopeMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001368 if (!Node->isImplicitAccess()) {
1369 PrintExpr(Node->getBase());
1370 OS << (Node->isArrow() ? "->" : ".");
1371 }
Douglas Gregorc26e0f62009-09-03 16:14:30 +00001372 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1373 Qualifier->print(OS, Policy);
John McCall2d74de92009-12-01 22:10:20 +00001374 else if (Node->hasExplicitTemplateArgs())
Douglas Gregor308047d2009-09-09 00:23:06 +00001375 // FIXME: Track use of "template" keyword explicitly?
1376 OS << "template ";
Mike Stump11289f42009-09-09 15:08:12 +00001377
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001378 OS << Node->getMemberNameInfo();
Mike Stump11289f42009-09-09 15:08:12 +00001379
John McCall2d74de92009-12-01 22:10:20 +00001380 if (Node->hasExplicitTemplateArgs()) {
Douglas Gregor308047d2009-09-09 00:23:06 +00001381 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1382 Node->getTemplateArgs(),
1383 Node->getNumTemplateArgs(),
1384 Policy);
1385 }
Douglas Gregora8db9542009-05-22 21:13:27 +00001386}
1387
John McCall10eae182009-11-30 22:42:35 +00001388void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001389 if (!Node->isImplicitAccess()) {
1390 PrintExpr(Node->getBase());
1391 OS << (Node->isArrow() ? "->" : ".");
1392 }
John McCall10eae182009-11-30 22:42:35 +00001393 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1394 Qualifier->print(OS, Policy);
1395
1396 // FIXME: this might originally have been written with 'template'
1397
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001398 OS << Node->getMemberNameInfo();
John McCall10eae182009-11-30 22:42:35 +00001399
1400 if (Node->hasExplicitTemplateArgs()) {
1401 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1402 Node->getTemplateArgs(),
1403 Node->getNumTemplateArgs(),
1404 Policy);
1405 }
1406}
1407
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001408static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1409 switch (UTT) {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001410 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001411 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
John Wiegley65497cc2011-04-27 23:09:49 +00001412 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001413 case UTT_HasTrivialAssign: return "__has_trivial_assign";
Alexis Huntf479f1b2011-05-09 18:22:59 +00001414 case UTT_HasTrivialDefaultConstructor: return "__has_trivial_constructor";
John Wiegley65497cc2011-04-27 23:09:49 +00001415 case UTT_HasTrivialCopy: return "__has_trivial_copy";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001416 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1417 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1418 case UTT_IsAbstract: return "__is_abstract";
John Wiegley65497cc2011-04-27 23:09:49 +00001419 case UTT_IsArithmetic: return "__is_arithmetic";
1420 case UTT_IsArray: return "__is_array";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001421 case UTT_IsClass: return "__is_class";
John Wiegley65497cc2011-04-27 23:09:49 +00001422 case UTT_IsCompleteType: return "__is_complete_type";
1423 case UTT_IsCompound: return "__is_compound";
1424 case UTT_IsConst: return "__is_const";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001425 case UTT_IsEmpty: return "__is_empty";
1426 case UTT_IsEnum: return "__is_enum";
Douglas Gregordca70af2011-12-03 18:14:24 +00001427 case UTT_IsFinal: return "__is_final";
John Wiegley65497cc2011-04-27 23:09:49 +00001428 case UTT_IsFloatingPoint: return "__is_floating_point";
1429 case UTT_IsFunction: return "__is_function";
1430 case UTT_IsFundamental: return "__is_fundamental";
1431 case UTT_IsIntegral: return "__is_integral";
Chandler Carruth8e2d6f42011-05-01 07:23:20 +00001432 case UTT_IsLiteral: return "__is_literal";
John Wiegley65497cc2011-04-27 23:09:49 +00001433 case UTT_IsLvalueReference: return "__is_lvalue_reference";
1434 case UTT_IsMemberFunctionPointer: return "__is_member_function_pointer";
1435 case UTT_IsMemberObjectPointer: return "__is_member_object_pointer";
1436 case UTT_IsMemberPointer: return "__is_member_pointer";
1437 case UTT_IsObject: return "__is_object";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001438 case UTT_IsPOD: return "__is_pod";
John Wiegley65497cc2011-04-27 23:09:49 +00001439 case UTT_IsPointer: return "__is_pointer";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001440 case UTT_IsPolymorphic: return "__is_polymorphic";
John Wiegley65497cc2011-04-27 23:09:49 +00001441 case UTT_IsReference: return "__is_reference";
John Wiegley65497cc2011-04-27 23:09:49 +00001442 case UTT_IsRvalueReference: return "__is_rvalue_reference";
1443 case UTT_IsScalar: return "__is_scalar";
1444 case UTT_IsSigned: return "__is_signed";
1445 case UTT_IsStandardLayout: return "__is_standard_layout";
1446 case UTT_IsTrivial: return "__is_trivial";
Alexis Huntd9a5cc12011-05-13 00:31:07 +00001447 case UTT_IsTriviallyCopyable: return "__is_trivially_copyable";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001448 case UTT_IsUnion: return "__is_union";
John Wiegley65497cc2011-04-27 23:09:49 +00001449 case UTT_IsUnsigned: return "__is_unsigned";
1450 case UTT_IsVoid: return "__is_void";
1451 case UTT_IsVolatile: return "__is_volatile";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001452 }
Chandler Carruth8e2d6f42011-05-01 07:23:20 +00001453 llvm_unreachable("Type trait not covered by switch statement");
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001454}
1455
1456static const char *getTypeTraitName(BinaryTypeTrait BTT) {
1457 switch (BTT) {
Francois Pichet34b21132010-12-08 22:35:30 +00001458 case BTT_IsBaseOf: return "__is_base_of";
John Wiegley65497cc2011-04-27 23:09:49 +00001459 case BTT_IsConvertible: return "__is_convertible";
1460 case BTT_IsSame: return "__is_same";
Francois Pichet34b21132010-12-08 22:35:30 +00001461 case BTT_TypeCompatible: return "__builtin_types_compatible_p";
Douglas Gregor8006e762011-01-27 20:28:01 +00001462 case BTT_IsConvertibleTo: return "__is_convertible_to";
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001463 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001464 llvm_unreachable("Binary type trait not covered by switch");
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001465}
1466
John Wiegley6242b6a2011-04-28 00:16:57 +00001467static const char *getTypeTraitName(ArrayTypeTrait ATT) {
1468 switch (ATT) {
1469 case ATT_ArrayRank: return "__array_rank";
1470 case ATT_ArrayExtent: return "__array_extent";
1471 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001472 llvm_unreachable("Array type trait not covered by switch");
John Wiegley6242b6a2011-04-28 00:16:57 +00001473}
1474
John Wiegleyf9f65842011-04-25 06:54:41 +00001475static const char *getExpressionTraitName(ExpressionTrait ET) {
1476 switch (ET) {
John Wiegleyf9f65842011-04-25 06:54:41 +00001477 case ET_IsLValueExpr: return "__is_lvalue_expr";
1478 case ET_IsRValueExpr: return "__is_rvalue_expr";
1479 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001480 llvm_unreachable("Expression type trait not covered by switch");
John Wiegleyf9f65842011-04-25 06:54:41 +00001481}
1482
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001483void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1484 OS << getTypeTraitName(E->getTrait()) << "("
Daniel Dunbar219fa692010-06-30 19:16:48 +00001485 << E->getQueriedType().getAsString(Policy) << ")";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001486}
1487
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001488void StmtPrinter::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
1489 OS << getTypeTraitName(E->getTrait()) << "("
1490 << E->getLhsType().getAsString(Policy) << ","
1491 << E->getRhsType().getAsString(Policy) << ")";
1492}
1493
John Wiegley6242b6a2011-04-28 00:16:57 +00001494void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
1495 OS << getTypeTraitName(E->getTrait()) << "("
1496 << E->getQueriedType().getAsString(Policy) << ")";
1497}
1498
John Wiegleyf9f65842011-04-25 06:54:41 +00001499void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
1500 OS << getExpressionTraitName(E->getTrait()) << "(";
1501 PrintExpr(E->getQueriedExpression());
1502 OS << ")";
1503}
1504
Sebastian Redl4202c0f2010-09-10 20:55:43 +00001505void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1506 OS << "noexcept(";
1507 PrintExpr(E->getOperand());
1508 OS << ")";
1509}
1510
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001511void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregore8e9dd62011-01-03 17:17:50 +00001512 PrintExpr(E->getPattern());
1513 OS << "...";
1514}
1515
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001516void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
1517 OS << "sizeof...(" << E->getPack()->getNameAsString() << ")";
1518}
1519
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001520void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
1521 SubstNonTypeTemplateParmPackExpr *Node) {
1522 OS << Node->getParameterPack()->getNameAsString();
1523}
1524
John McCall7c454bb2011-07-15 05:09:51 +00001525void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
1526 SubstNonTypeTemplateParmExpr *Node) {
1527 Visit(Node->getReplacement());
1528}
1529
Douglas Gregorfe314812011-06-21 17:03:29 +00001530void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
1531 PrintExpr(Node->GetTemporaryExpr());
1532}
1533
Mike Stump11289f42009-09-09 15:08:12 +00001534// Obj-C
Anders Carlsson76f4a902007-08-21 17:43:55 +00001535
1536void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1537 OS << "@";
1538 VisitStringLiteral(Node->getString());
1539}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001540
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001541void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001542 OS << "@encode(" << Node->getEncodedType().getAsString(Policy) << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001543}
1544
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001545void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001546 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001547}
1548
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001549void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001550 OS << "@protocol(" << *Node->getProtocol() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001551}
1552
Steve Naroffd54978b2007-09-18 23:55:05 +00001553void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1554 OS << "[";
Douglas Gregor9a129192010-04-21 00:45:42 +00001555 switch (Mess->getReceiverKind()) {
1556 case ObjCMessageExpr::Instance:
1557 PrintExpr(Mess->getInstanceReceiver());
1558 break;
1559
1560 case ObjCMessageExpr::Class:
1561 OS << Mess->getClassReceiver().getAsString(Policy);
1562 break;
1563
1564 case ObjCMessageExpr::SuperInstance:
1565 case ObjCMessageExpr::SuperClass:
1566 OS << "Super";
1567 break;
1568 }
1569
Ted Kremeneka06e7122008-05-02 17:32:38 +00001570 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00001571 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001572 if (selector.isUnarySelector()) {
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00001573 OS << selector.getNameForSlot(0);
Steve Naroffc6814ea2007-10-02 20:01:56 +00001574 } else {
1575 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001576 if (i < selector.getNumArgs()) {
1577 if (i > 0) OS << ' ';
1578 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001579 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00001580 else
1581 OS << ":";
1582 }
1583 else OS << ", "; // Handle variadic methods.
Mike Stump11289f42009-09-09 15:08:12 +00001584
Steve Naroffc6814ea2007-10-02 20:01:56 +00001585 PrintExpr(Mess->getArg(i));
1586 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001587 }
1588 OS << "]";
1589}
1590
John McCall31168b02011-06-15 23:02:42 +00001591void
1592StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
1593 PrintExpr(E->getSubExpr());
1594}
1595
1596void
1597StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
1598 OS << "(" << E->getBridgeKindName() << E->getType().getAsString(Policy)
1599 << ")";
1600 PrintExpr(E->getSubExpr());
1601}
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001602
Steve Naroffc540d662008-09-03 18:15:37 +00001603void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001604 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00001605 OS << "^";
Mike Stump11289f42009-09-09 15:08:12 +00001606
Steve Naroffc540d662008-09-03 18:15:37 +00001607 const FunctionType *AFT = Node->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00001608
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001609 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroffc540d662008-09-03 18:15:37 +00001610 OS << "()";
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001611 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00001612 OS << '(';
1613 std::string ParamStr;
Steve Naroff415d3d52008-10-08 17:01:13 +00001614 for (BlockDecl::param_iterator AI = BD->param_begin(),
1615 E = BD->param_end(); AI != E; ++AI) {
1616 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001617 ParamStr = (*AI)->getNameAsString();
Douglas Gregor7de59662009-05-29 20:38:28 +00001618 (*AI)->getType().getAsStringInternal(ParamStr, Policy);
Steve Naroffc540d662008-09-03 18:15:37 +00001619 OS << ParamStr;
1620 }
Mike Stump11289f42009-09-09 15:08:12 +00001621
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001622 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00001623 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001624 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00001625 OS << "...";
1626 }
1627 OS << ')';
1628 }
1629}
1630
Steve Naroffc540d662008-09-03 18:15:37 +00001631void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001632 OS << *Node->getDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00001633}
John McCall8d69a212010-11-15 23:31:06 +00001634
Ted Kremenekf551f322011-11-30 22:08:08 +00001635void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
1636 PrintExpr(Node->getSourceExpr());
1637}
John McCall8d69a212010-11-15 23:31:06 +00001638
Tanya Lattner55808c12011-06-04 00:47:47 +00001639void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
1640 OS << "__builtin_astype(";
1641 PrintExpr(Node->getSrcExpr());
1642 OS << ", " << Node->getType().getAsString();
1643 OS << ")";
1644}
1645
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001646//===----------------------------------------------------------------------===//
1647// Stmt method implementations
1648//===----------------------------------------------------------------------===//
1649
Eli Friedmanef334fd2009-05-30 05:03:24 +00001650void Stmt::dumpPretty(ASTContext& Context) const {
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +00001651 printPretty(llvm::errs(), Context, 0,
Chris Lattnerc61089a2009-06-30 01:26:17 +00001652 PrintingPolicy(Context.getLangOptions()));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001653}
1654
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001655void Stmt::printPretty(raw_ostream &OS, ASTContext& Context,
Eli Friedmanef334fd2009-05-30 05:03:24 +00001656 PrinterHelper* Helper,
Douglas Gregor7de59662009-05-29 20:38:28 +00001657 const PrintingPolicy &Policy,
1658 unsigned Indentation) const {
Chris Lattner882f7882006-11-04 18:52:07 +00001659 if (this == 0) {
1660 OS << "<NULL>";
1661 return;
1662 }
1663
Douglas Gregor8655e882009-10-16 22:46:09 +00001664 if (Policy.Dump && &Context) {
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +00001665 dump(OS, Context.getSourceManager());
Douglas Gregor278f52e2009-05-30 00:08:05 +00001666 return;
1667 }
Mike Stump11289f42009-09-09 15:08:12 +00001668
Eli Friedmanef334fd2009-05-30 05:03:24 +00001669 StmtPrinter P(OS, Context, Helper, Policy, Indentation);
Chris Lattner62249a62007-08-21 04:04:25 +00001670 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001671}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001672
1673//===----------------------------------------------------------------------===//
1674// PrinterHelper
1675//===----------------------------------------------------------------------===//
1676
1677// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +00001678PrinterHelper::~PrinterHelper() {}