blob: 2f7cb55c7cc1008adf6aad850d1431dc898d6faa [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
Benjamin Kramer4ab984e2012-07-04 20:19:54 +000015#include "clang/AST/ASTContext.h"
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000016#include "clang/AST/StmtVisitor.h"
Douglas Gregorc7acfdf2009-01-06 05:10:23 +000017#include "clang/AST/DeclCXX.h"
Ted Kremenek5c84c012007-10-17 18:36:42 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregorcdbc5392011-01-15 01:15:58 +000019#include "clang/AST/DeclTemplate.h"
Ted Kremenek04f3cee2007-08-31 21:30:12 +000020#include "clang/AST/PrettyPrinter.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"
Benjamin Kramer49038022012-02-04 13:45:25 +000023#include "llvm/ADT/SmallString.h"
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// StmtPrinter Visitor
28//===----------------------------------------------------------------------===//
29
30namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +000031 class StmtPrinter : public StmtVisitor<StmtPrinter> {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000032 raw_ostream &OS;
Eli Friedmanef334fd2009-05-30 05:03:24 +000033 ASTContext &Context;
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000034 unsigned IndentLevel;
Ted Kremenek04f3cee2007-08-31 21:30:12 +000035 clang::PrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +000036 PrintingPolicy Policy;
37
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000038 public:
Chris Lattner0e62c1c2011-07-23 10:55:15 +000039 StmtPrinter(raw_ostream &os, ASTContext &C, PrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +000040 const PrintingPolicy &Policy,
Douglas Gregor7de59662009-05-29 20:38:28 +000041 unsigned Indentation = 0)
Eli Friedmanef334fd2009-05-30 05:03:24 +000042 : OS(os), Context(C), IndentLevel(Indentation), Helper(helper),
43 Policy(Policy) {}
Mike Stump11289f42009-09-09 15:08:12 +000044
Douglas Gregor7de59662009-05-29 20:38:28 +000045 void PrintStmt(Stmt *S) {
46 PrintStmt(S, Policy.Indentation);
47 }
48
49 void PrintStmt(Stmt *S, int SubIndent) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000050 IndentLevel += SubIndent;
Chris Lattnera076fde2007-05-31 18:21:33 +000051 if (S && isa<Expr>(S)) {
Chris Lattner882f7882006-11-04 18:52:07 +000052 // If this is an expr used in a stmt context, indent and newline it.
53 Indent();
Chris Lattner62249a62007-08-21 04:04:25 +000054 Visit(S);
Chris Lattnerfc068c12007-05-30 17:57:36 +000055 OS << ";\n";
Chris Lattner882f7882006-11-04 18:52:07 +000056 } else if (S) {
Chris Lattner62249a62007-08-21 04:04:25 +000057 Visit(S);
Chris Lattner882f7882006-11-04 18:52:07 +000058 } else {
Chris Lattner2f6ac262007-05-28 01:47:47 +000059 Indent() << "<<<NULL STATEMENT>>>\n";
Chris Lattner882f7882006-11-04 18:52:07 +000060 }
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000061 IndentLevel -= SubIndent;
Chris Lattner882f7882006-11-04 18:52:07 +000062 }
Eli Friedman15ea8802009-05-30 00:19:54 +000063
Chris Lattner073926e2007-05-20 23:04:55 +000064 void PrintRawCompoundStmt(CompoundStmt *S);
Chris Lattnerfdc195a2007-06-05 20:52:47 +000065 void PrintRawDecl(Decl *D);
Ted Kremenek15e6b402008-10-06 18:39:36 +000066 void PrintRawDeclStmt(DeclStmt *S);
Chris Lattnerc0a38dd2007-06-11 22:26:23 +000067 void PrintRawIfStmt(IfStmt *If);
Sebastian Redl9b244a82008-12-22 21:35:02 +000068 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
Peter Collingbourne4b279a02011-02-08 21:17:54 +000069 void PrintCallArgs(CallExpr *E);
John Wiegley1c0675e2011-04-28 01:08:34 +000070 void PrintRawSEHExceptHandler(SEHExceptStmt *S);
71 void PrintRawSEHFinallyStmt(SEHFinallyStmt *S);
Mike Stump11289f42009-09-09 15:08:12 +000072
Chris Lattner882f7882006-11-04 18:52:07 +000073 void PrintExpr(Expr *E) {
74 if (E)
Chris Lattner62249a62007-08-21 04:04:25 +000075 Visit(E);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000076 else
Chris Lattner882f7882006-11-04 18:52:07 +000077 OS << "<null expr>";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000078 }
Mike Stump11289f42009-09-09 15:08:12 +000079
Chris Lattner0e62c1c2011-07-23 10:55:15 +000080 raw_ostream &Indent(int Delta = 0) {
Douglas Gregor7de59662009-05-29 20:38:28 +000081 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
82 OS << " ";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000083 return OS;
84 }
Mike Stump11289f42009-09-09 15:08:12 +000085
Mike Stump11289f42009-09-09 15:08:12 +000086 void Visit(Stmt* S) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +000087 if (Helper && Helper->handledStmt(S,OS))
88 return;
89 else StmtVisitor<StmtPrinter>::Visit(S);
90 }
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +000091
Chandler Carruthb7967b92010-10-23 08:21:37 +000092 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +000093 Indent() << "<<unknown stmt type>>\n";
94 }
Chandler Carruthb7967b92010-10-23 08:21:37 +000095 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +000096 OS << "<<unknown expr type>>";
97 }
98 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Mike Stump11289f42009-09-09 15:08:12 +000099
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +0000100#define ABSTRACT_STMT(CLASS)
Douglas Gregorbe35ce92008-11-14 12:46:07 +0000101#define STMT(CLASS, PARENT) \
Chris Lattner62249a62007-08-21 04:04:25 +0000102 void Visit##CLASS(CLASS *Node);
Alexis Hunt656bb312010-05-05 15:24:00 +0000103#include "clang/AST/StmtNodes.inc"
Chris Lattner71e23ce2006-11-04 20:18:38 +0000104 };
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000105}
106
Chris Lattner71e23ce2006-11-04 20:18:38 +0000107//===----------------------------------------------------------------------===//
108// Stmt printing methods.
109//===----------------------------------------------------------------------===//
110
Chris Lattner073926e2007-05-20 23:04:55 +0000111/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
112/// with no newline after the }.
113void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
114 OS << "{\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000115 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
Chris Lattner882f7882006-11-04 18:52:07 +0000116 I != E; ++I)
117 PrintStmt(*I);
Mike Stump11289f42009-09-09 15:08:12 +0000118
Chris Lattner073926e2007-05-20 23:04:55 +0000119 Indent() << "}";
120}
121
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000122void StmtPrinter::PrintRawDecl(Decl *D) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000123 D->print(OS, Policy, IndentLevel);
Mike Stump74a76472009-02-10 20:16:46 +0000124}
125
Ted Kremenek15e6b402008-10-06 18:39:36 +0000126void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
Eli Friedman15ea8802009-05-30 00:19:54 +0000127 DeclStmt::decl_iterator Begin = S->decl_begin(), End = S->decl_end();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000128 SmallVector<Decl*, 2> Decls;
Mike Stump11289f42009-09-09 15:08:12 +0000129 for ( ; Begin != End; ++Begin)
Eli Friedman79635842009-05-30 04:20:30 +0000130 Decls.push_back(*Begin);
Eli Friedman15ea8802009-05-30 00:19:54 +0000131
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000132 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
Ted Kremenek15e6b402008-10-06 18:39:36 +0000133}
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000134
135void StmtPrinter::VisitNullStmt(NullStmt *Node) {
136 Indent() << ";\n";
137}
138
139void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Eli Friedman15ea8802009-05-30 00:19:54 +0000140 Indent();
141 PrintRawDeclStmt(Node);
142 OS << ";\n";
Steve Naroff2a8ad182007-05-29 22:59:26 +0000143}
144
Chris Lattner073926e2007-05-20 23:04:55 +0000145void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
146 Indent();
147 PrintRawCompoundStmt(Node);
Chris Lattnerdf3cafb2007-05-31 05:08:56 +0000148 OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000149}
150
Chris Lattner6c0ff132006-11-05 00:19:50 +0000151void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000152 Indent(-1) << "case ";
Chris Lattner6c0ff132006-11-05 00:19:50 +0000153 PrintExpr(Node->getLHS());
154 if (Node->getRHS()) {
155 OS << " ... ";
156 PrintExpr(Node->getRHS());
157 }
158 OS << ":\n";
Mike Stump11289f42009-09-09 15:08:12 +0000159
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000160 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000161}
162
163void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000164 Indent(-1) << "default:\n";
165 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000166}
167
168void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000169 Indent(-1) << Node->getName() << ":\n";
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000170 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000171}
172
Richard Smithc202b282012-04-14 00:33:13 +0000173void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
174 OS << "[[";
175 bool first = true;
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000176 for (ArrayRef<const Attr*>::iterator it = Node->getAttrs().begin(),
177 end = Node->getAttrs().end();
178 it != end; ++it) {
Richard Smithc202b282012-04-14 00:33:13 +0000179 if (!first) {
180 OS << ", ";
181 first = false;
182 }
183 // TODO: check this
184 (*it)->printPretty(OS, Context);
185 }
186 OS << "]] ";
187 PrintStmt(Node->getSubStmt(), 0);
188}
189
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000190void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000191 OS << "if (";
Chris Lattner882f7882006-11-04 18:52:07 +0000192 PrintExpr(If->getCond());
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000193 OS << ')';
Mike Stump11289f42009-09-09 15:08:12 +0000194
Chris Lattner073926e2007-05-20 23:04:55 +0000195 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
196 OS << ' ';
197 PrintRawCompoundStmt(CS);
198 OS << (If->getElse() ? ' ' : '\n');
199 } else {
200 OS << '\n';
201 PrintStmt(If->getThen());
202 if (If->getElse()) Indent();
203 }
Mike Stump11289f42009-09-09 15:08:12 +0000204
Chris Lattner073926e2007-05-20 23:04:55 +0000205 if (Stmt *Else = If->getElse()) {
206 OS << "else";
Mike Stump11289f42009-09-09 15:08:12 +0000207
Chris Lattner073926e2007-05-20 23:04:55 +0000208 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
209 OS << ' ';
210 PrintRawCompoundStmt(CS);
211 OS << '\n';
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000212 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
213 OS << ' ';
214 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000215 } else {
216 OS << '\n';
217 PrintStmt(If->getElse());
218 }
Chris Lattner882f7882006-11-04 18:52:07 +0000219 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000220}
221
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000222void StmtPrinter::VisitIfStmt(IfStmt *If) {
223 Indent();
224 PrintRawIfStmt(If);
225}
226
Chris Lattnerf2174b62006-11-04 20:59:27 +0000227void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
228 Indent() << "switch (";
229 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000230 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000231
Chris Lattner073926e2007-05-20 23:04:55 +0000232 // Pretty print compoundstmt bodies (very common).
233 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
234 OS << " ";
235 PrintRawCompoundStmt(CS);
236 OS << "\n";
237 } else {
238 OS << "\n";
239 PrintStmt(Node->getBody());
240 }
Chris Lattnerf2174b62006-11-04 20:59:27 +0000241}
242
Chris Lattner85ed8732006-11-04 20:40:44 +0000243void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
244 Indent() << "while (";
245 PrintExpr(Node->getCond());
246 OS << ")\n";
247 PrintStmt(Node->getBody());
248}
249
250void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000251 Indent() << "do ";
252 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
253 PrintRawCompoundStmt(CS);
254 OS << " ";
255 } else {
256 OS << "\n";
257 PrintStmt(Node->getBody());
258 Indent();
259 }
Mike Stump11289f42009-09-09 15:08:12 +0000260
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000261 OS << "while (";
Chris Lattner85ed8732006-11-04 20:40:44 +0000262 PrintExpr(Node->getCond());
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000263 OS << ");\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000264}
265
Chris Lattner71e23ce2006-11-04 20:18:38 +0000266void StmtPrinter::VisitForStmt(ForStmt *Node) {
267 Indent() << "for (";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000268 if (Node->getInit()) {
269 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000270 PrintRawDeclStmt(DS);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000271 else
272 PrintExpr(cast<Expr>(Node->getInit()));
273 }
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000274 OS << ";";
275 if (Node->getCond()) {
276 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000277 PrintExpr(Node->getCond());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000278 }
279 OS << ";";
280 if (Node->getInc()) {
281 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000282 PrintExpr(Node->getInc());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000283 }
284 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000285
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000286 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
287 PrintRawCompoundStmt(CS);
288 OS << "\n";
289 } else {
290 OS << "\n";
291 PrintStmt(Node->getBody());
292 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000293}
294
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000295void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian83615522008-01-02 22:54:34 +0000296 Indent() << "for (";
297 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000298 PrintRawDeclStmt(DS);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000299 else
300 PrintExpr(cast<Expr>(Node->getElement()));
301 OS << " in ";
302 PrintExpr(Node->getCollection());
303 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000304
Fariborz Jahanian83615522008-01-02 22:54:34 +0000305 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
306 PrintRawCompoundStmt(CS);
307 OS << "\n";
308 } else {
309 OS << "\n";
310 PrintStmt(Node->getBody());
311 }
312}
313
Richard Smith02e85f32011-04-14 22:09:26 +0000314void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
315 Indent() << "for (";
316 PrintingPolicy SubPolicy(Policy);
317 SubPolicy.SuppressInitializers = true;
318 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
319 OS << " : ";
320 PrintExpr(Node->getRangeInit());
321 OS << ") {\n";
322 PrintStmt(Node->getBody());
323 Indent() << "}\n";
324}
325
Douglas Gregordeb4a2be2011-10-25 01:33:02 +0000326void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
327 Indent();
328 if (Node->isIfExists())
329 OS << "__if_exists (";
330 else
331 OS << "__if_not_exists (";
332
333 if (NestedNameSpecifier *Qualifier
334 = Node->getQualifierLoc().getNestedNameSpecifier())
335 Qualifier->print(OS, Policy);
336
337 OS << Node->getNameInfo() << ") ";
338
339 PrintRawCompoundStmt(Node->getSubStmt());
340}
341
Chris Lattner16976d32006-11-05 01:46:01 +0000342void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000343 Indent() << "goto " << Node->getLabel()->getName() << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000344}
345
346void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000347 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000348 PrintExpr(Node->getTarget());
Chris Lattnerb4619482007-05-31 06:00:14 +0000349 OS << ";\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000350}
351
352void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000353 Indent() << "continue;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000354}
355
356void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Chris Lattnerb4619482007-05-31 06:00:14 +0000357 Indent() << "break;\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000358}
359
360
Chris Lattner882f7882006-11-04 18:52:07 +0000361void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
362 Indent() << "return";
363 if (Node->getRetValue()) {
364 OS << " ";
365 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000366 }
Chris Lattnerb4619482007-05-31 06:00:14 +0000367 OS << ";\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000368}
369
Chris Lattner73c56c02007-10-29 04:04:16 +0000370
371void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
Anders Carlsson660bdd12007-11-23 23:12:25 +0000372 Indent() << "asm ";
Mike Stump11289f42009-09-09 15:08:12 +0000373
Anders Carlsson660bdd12007-11-23 23:12:25 +0000374 if (Node->isVolatile())
375 OS << "volatile ";
Mike Stump11289f42009-09-09 15:08:12 +0000376
Anders Carlsson660bdd12007-11-23 23:12:25 +0000377 OS << "(";
Anders Carlsson81a5a692007-11-20 19:21:03 +0000378 VisitStringLiteral(Node->getAsmString());
Mike Stump11289f42009-09-09 15:08:12 +0000379
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000380 // Outputs
381 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
382 Node->getNumClobbers() != 0)
383 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000384
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000385 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
386 if (i != 0)
387 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000388
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000389 if (!Node->getOutputName(i).empty()) {
390 OS << '[';
391 OS << Node->getOutputName(i);
392 OS << "] ";
393 }
Mike Stump11289f42009-09-09 15:08:12 +0000394
Chris Lattner72bbf172009-03-10 04:59:06 +0000395 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000396 OS << " ";
397 Visit(Node->getOutputExpr(i));
398 }
Mike Stump11289f42009-09-09 15:08:12 +0000399
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000400 // Inputs
401 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
402 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000403
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000404 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
405 if (i != 0)
406 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000407
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000408 if (!Node->getInputName(i).empty()) {
409 OS << '[';
410 OS << Node->getInputName(i);
411 OS << "] ";
412 }
Mike Stump11289f42009-09-09 15:08:12 +0000413
Chris Lattner72bbf172009-03-10 04:59:06 +0000414 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000415 OS << " ";
416 Visit(Node->getInputExpr(i));
417 }
Mike Stump11289f42009-09-09 15:08:12 +0000418
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000419 // Clobbers
420 if (Node->getNumClobbers() != 0)
421 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000422
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000423 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
424 if (i != 0)
425 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000426
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000427 VisitStringLiteral(Node->getClobber(i));
428 }
Mike Stump11289f42009-09-09 15:08:12 +0000429
Anders Carlsson81a5a692007-11-20 19:21:03 +0000430 OS << ");\n";
Chris Lattner73c56c02007-10-29 04:04:16 +0000431}
432
Chad Rosier32503022012-06-11 20:47:18 +0000433void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
434 // FIXME: Implement MS style inline asm statement printer.
435 Indent() << "asm ()";
436}
437
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000438void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000439 Indent() << "@try";
440 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
441 PrintRawCompoundStmt(TS);
442 OS << "\n";
443 }
Mike Stump11289f42009-09-09 15:08:12 +0000444
Douglas Gregor96c79492010-04-23 22:50:49 +0000445 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
446 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000447 Indent() << "@catch(";
Steve Naroff371b8fb2009-03-03 19:52:17 +0000448 if (catchStmt->getCatchParamDecl()) {
449 if (Decl *DS = catchStmt->getCatchParamDecl())
450 PrintRawDecl(DS);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000451 }
452 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000453 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
454 PrintRawCompoundStmt(CS);
455 OS << "\n";
456 }
Fariborz Jahanian88157952007-11-02 18:16:07 +0000457 }
Mike Stump11289f42009-09-09 15:08:12 +0000458
459 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
460 Node->getFinallyStmt())) {
Fariborz Jahaniandefbf9a2007-11-07 00:46:42 +0000461 Indent() << "@finally";
462 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian88157952007-11-02 18:16:07 +0000463 OS << "\n";
Mike Stump11289f42009-09-09 15:08:12 +0000464 }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000465}
466
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000467void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000468}
469
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000470void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000471 Indent() << "@catch (...) { /* todo */ } \n";
472}
473
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000474void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000475 Indent() << "@throw";
476 if (Node->getThrowExpr()) {
477 OS << " ";
478 PrintExpr(Node->getThrowExpr());
479 }
480 OS << ";\n";
481}
482
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000483void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000484 Indent() << "@synchronized (";
485 PrintExpr(Node->getSynchExpr());
486 OS << ")";
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000487 PrintRawCompoundStmt(Node->getSynchBody());
488 OS << "\n";
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000489}
490
John McCall31168b02011-06-15 23:02:42 +0000491void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
492 Indent() << "@autoreleasepool";
493 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
494 OS << "\n";
495}
496
Sebastian Redl9b244a82008-12-22 21:35:02 +0000497void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
498 OS << "catch (";
Sebastian Redl54c04d42008-12-22 19:15:10 +0000499 if (Decl *ExDecl = Node->getExceptionDecl())
500 PrintRawDecl(ExDecl);
501 else
502 OS << "...";
503 OS << ") ";
504 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl9b244a82008-12-22 21:35:02 +0000505}
506
507void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
508 Indent();
509 PrintRawCXXCatchStmt(Node);
510 OS << "\n";
511}
512
513void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
514 Indent() << "try ";
515 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump11289f42009-09-09 15:08:12 +0000516 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl9b244a82008-12-22 21:35:02 +0000517 OS << " ";
518 PrintRawCXXCatchStmt(Node->getHandler(i));
519 }
Sebastian Redl54c04d42008-12-22 19:15:10 +0000520 OS << "\n";
521}
522
John Wiegley1c0675e2011-04-28 01:08:34 +0000523void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
524 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
525 PrintRawCompoundStmt(Node->getTryBlock());
526 SEHExceptStmt *E = Node->getExceptHandler();
527 SEHFinallyStmt *F = Node->getFinallyHandler();
528 if(E)
529 PrintRawSEHExceptHandler(E);
530 else {
531 assert(F && "Must have a finally block...");
532 PrintRawSEHFinallyStmt(F);
533 }
534 OS << "\n";
535}
536
537void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
538 OS << "__finally ";
539 PrintRawCompoundStmt(Node->getBlock());
540 OS << "\n";
541}
542
543void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
544 OS << "__except (";
545 VisitExpr(Node->getFilterExpr());
546 OS << ")\n";
547 PrintRawCompoundStmt(Node->getBlock());
548 OS << "\n";
549}
550
551void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
552 Indent();
553 PrintRawSEHExceptHandler(Node);
554 OS << "\n";
555}
556
557void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
558 Indent();
559 PrintRawSEHFinallyStmt(Node);
560 OS << "\n";
561}
562
Chris Lattner71e23ce2006-11-04 20:18:38 +0000563//===----------------------------------------------------------------------===//
564// Expr printing methods.
565//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000566
Chris Lattner882f7882006-11-04 18:52:07 +0000567void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000568 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
569 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000570 if (Node->hasTemplateKeyword())
571 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000572 OS << Node->getNameInfo();
John McCallb3774b52010-08-19 23:49:38 +0000573 if (Node->hasExplicitTemplateArgs())
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000574 OS << TemplateSpecializationType::PrintTemplateArgumentList(
575 Node->getTemplateArgs(),
576 Node->getNumTemplateArgs(),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000577 Policy);
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000578}
579
John McCall8cd78132009-11-19 22:55:06 +0000580void StmtPrinter::VisitDependentScopeDeclRefExpr(
581 DependentScopeDeclRefExpr *Node) {
Axel Naumann20b27862011-01-24 15:44:00 +0000582 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
583 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000584 if (Node->hasTemplateKeyword())
585 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000586 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000587 if (Node->hasExplicitTemplateArgs())
588 OS << TemplateSpecializationType::PrintTemplateArgumentList(
589 Node->getTemplateArgs(),
590 Node->getNumTemplateArgs(),
591 Policy);
Douglas Gregor90a1a652009-03-19 17:26:29 +0000592}
593
John McCalld14a8642009-11-21 08:51:07 +0000594void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregora727cb92009-06-30 22:34:41 +0000595 if (Node->getQualifier())
596 Node->getQualifier()->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000597 if (Node->hasTemplateKeyword())
598 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000599 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000600 if (Node->hasExplicitTemplateArgs())
601 OS << TemplateSpecializationType::PrintTemplateArgumentList(
602 Node->getTemplateArgs(),
Douglas Gregora727cb92009-06-30 22:34:41 +0000603 Node->getNumTemplateArgs(),
John McCalle66edc12009-11-24 19:00:30 +0000604 Policy);
Douglas Gregora727cb92009-06-30 22:34:41 +0000605}
606
Steve Naroffe46504b2007-11-12 14:29:37 +0000607void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000608 if (Node->getBase()) {
609 PrintExpr(Node->getBase());
610 OS << (Node->isArrow() ? "->" : ".");
611 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000612 OS << *Node->getDecl();
Steve Naroffe46504b2007-11-12 14:29:37 +0000613}
614
Steve Naroffec944032008-05-30 00:40:33 +0000615void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000616 if (Node->isSuperReceiver())
617 OS << "super.";
618 else if (Node->getBase()) {
Steve Naroffec944032008-05-30 00:40:33 +0000619 PrintExpr(Node->getBase());
620 OS << ".";
621 }
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000622
John McCallb7bd14f2010-12-02 01:19:52 +0000623 if (Node->isImplicitProperty())
624 OS << Node->getImplicitPropertyGetter()->getSelector().getAsString();
625 else
626 OS << Node->getExplicitProperty()->getName();
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000627}
628
Ted Kremeneke65b0862012-03-06 20:05:56 +0000629void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
630
631 PrintExpr(Node->getBaseExpr());
632 OS << "[";
633 PrintExpr(Node->getKeyExpr());
634 OS << "]";
635}
636
Chris Lattner6307f192008-08-10 01:53:14 +0000637void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson625bfc82007-07-21 05:21:51 +0000638 switch (Node->getIdentType()) {
639 default:
David Blaikie83d382b2011-09-23 05:06:16 +0000640 llvm_unreachable("unknown case");
Chris Lattner6307f192008-08-10 01:53:14 +0000641 case PredefinedExpr::Func:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000642 OS << "__func__";
643 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000644 case PredefinedExpr::Function:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000645 OS << "__FUNCTION__";
646 break;
Nico Weber3a691a32012-06-23 02:07:59 +0000647 case PredefinedExpr::LFunction:
648 OS << "L__FUNCTION__";
649 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000650 case PredefinedExpr::PrettyFunction:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000651 OS << "__PRETTY_FUNCTION__";
652 break;
653 }
654}
655
Steve Naroffae4143e2007-04-26 20:39:23 +0000656void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000657 unsigned value = Node->getValue();
Douglas Gregorfb65e592011-07-27 05:40:30 +0000658
659 switch (Node->getKind()) {
660 case CharacterLiteral::Ascii: break; // no prefix.
661 case CharacterLiteral::Wide: OS << 'L'; break;
662 case CharacterLiteral::UTF16: OS << 'u'; break;
663 case CharacterLiteral::UTF32: OS << 'U'; break;
664 }
665
Chris Lattner666115c2007-07-13 23:58:20 +0000666 switch (value) {
667 case '\\':
668 OS << "'\\\\'";
669 break;
670 case '\'':
671 OS << "'\\''";
672 break;
673 case '\a':
674 // TODO: K&R: the meaning of '\\a' is different in traditional C
675 OS << "'\\a'";
676 break;
677 case '\b':
678 OS << "'\\b'";
679 break;
680 // Nonstandard escape sequence.
681 /*case '\e':
682 OS << "'\\e'";
683 break;*/
684 case '\f':
685 OS << "'\\f'";
686 break;
687 case '\n':
688 OS << "'\\n'";
689 break;
690 case '\r':
691 OS << "'\\r'";
692 break;
693 case '\t':
694 OS << "'\\t'";
695 break;
696 case '\v':
697 OS << "'\\v'";
698 break;
699 default:
Ted Kremenek652d18e2008-02-23 00:52:04 +0000700 if (value < 256 && isprint(value)) {
Chris Lattner666115c2007-07-13 23:58:20 +0000701 OS << "'" << (char)value << "'";
702 } else if (value < 256) {
Benjamin Kramer49038022012-02-04 13:45:25 +0000703 OS << "'\\x";
704 OS.write_hex(value) << "'";
Chris Lattner666115c2007-07-13 23:58:20 +0000705 } else {
706 // FIXME what to really do here?
707 OS << value;
708 }
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000709 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000710}
711
Steve Naroffdf7855b2007-02-21 23:46:25 +0000712void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000713 bool isSigned = Node->getType()->isSignedIntegerType();
714 OS << Node->getValue().toString(10, isSigned);
Mike Stump11289f42009-09-09 15:08:12 +0000715
Chris Lattner06430412007-05-21 05:45:03 +0000716 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall9dd450b2009-09-21 23:43:11 +0000717 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
David Blaikie83d382b2011-09-23 05:06:16 +0000718 default: llvm_unreachable("Unexpected type for integer literal!");
Richard Trieu8b626ba2011-11-07 18:40:31 +0000719 // FIXME: The Short and UShort cases are to handle cases where a short
720 // integeral literal is formed during template instantiation. They should
721 // be removed when template instantiation no longer needs integer literals.
722 case BuiltinType::Short:
723 case BuiltinType::UShort:
Chris Lattner06430412007-05-21 05:45:03 +0000724 case BuiltinType::Int: break; // no suffix.
725 case BuiltinType::UInt: OS << 'U'; break;
726 case BuiltinType::Long: OS << 'L'; break;
727 case BuiltinType::ULong: OS << "UL"; break;
728 case BuiltinType::LongLong: OS << "LL"; break;
729 case BuiltinType::ULongLong: OS << "ULL"; break;
Richard Trieu8b626ba2011-11-07 18:40:31 +0000730 case BuiltinType::Int128: OS << "i128"; break;
731 case BuiltinType::UInt128: OS << "Ui128"; break;
Chris Lattner06430412007-05-21 05:45:03 +0000732 }
Chris Lattner882f7882006-11-04 18:52:07 +0000733}
Steve Naroffab624882007-02-21 22:05:47 +0000734void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000735 SmallString<16> Str;
Eli Friedman53392062011-10-05 20:32:03 +0000736 Node->getValue().toString(Str);
737 OS << Str;
Chris Lattner882f7882006-11-04 18:52:07 +0000738}
Chris Lattner1c20a172007-08-26 03:42:43 +0000739
740void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
741 PrintExpr(Node->getSubExpr());
742 OS << "i";
743}
744
Steve Naroffdf7855b2007-02-21 23:46:25 +0000745void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Richard Trieudc355912012-06-13 20:25:24 +0000746 Str->outputString(OS);
Chris Lattner882f7882006-11-04 18:52:07 +0000747}
748void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
749 OS << "(";
750 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000751 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000752}
753void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +0000754 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +0000755 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump11289f42009-09-09 15:08:12 +0000756
Eli Friedman1cf25362009-06-14 22:39:26 +0000757 // Print a space if this is an "identifier operator" like __real, or if
758 // it might be concatenated incorrectly like '+'.
Chris Lattnere5b60442007-08-23 21:46:40 +0000759 switch (Node->getOpcode()) {
760 default: break;
John McCalle3027922010-08-25 11:45:40 +0000761 case UO_Real:
762 case UO_Imag:
763 case UO_Extension:
Chris Lattnere5b60442007-08-23 21:46:40 +0000764 OS << ' ';
765 break;
John McCalle3027922010-08-25 11:45:40 +0000766 case UO_Plus:
767 case UO_Minus:
Eli Friedman1cf25362009-06-14 22:39:26 +0000768 if (isa<UnaryOperator>(Node->getSubExpr()))
769 OS << ' ';
770 break;
Chris Lattnere5b60442007-08-23 21:46:40 +0000771 }
772 }
Chris Lattner882f7882006-11-04 18:52:07 +0000773 PrintExpr(Node->getSubExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000774
Chris Lattner15768702006-11-05 23:54:51 +0000775 if (Node->isPostfix())
776 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000777}
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000778
Douglas Gregor882211c2010-04-28 22:16:22 +0000779void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
780 OS << "__builtin_offsetof(";
Daniel Dunbar219fa692010-06-30 19:16:48 +0000781 OS << Node->getTypeSourceInfo()->getType().getAsString(Policy) << ", ";
Douglas Gregor882211c2010-04-28 22:16:22 +0000782 bool PrintedSomething = false;
783 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
784 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
785 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
786 // Array node
787 OS << "[";
788 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
789 OS << "]";
790 PrintedSomething = true;
791 continue;
792 }
Douglas Gregord1702062010-04-29 00:18:15 +0000793
794 // Skip implicit base indirections.
795 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
796 continue;
797
Douglas Gregor882211c2010-04-28 22:16:22 +0000798 // Field or identifier node.
799 IdentifierInfo *Id = ON.getFieldName();
800 if (!Id)
801 continue;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000802
Douglas Gregor882211c2010-04-28 22:16:22 +0000803 if (PrintedSomething)
804 OS << ".";
805 else
806 PrintedSomething = true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000807 OS << Id->getName();
Douglas Gregor882211c2010-04-28 22:16:22 +0000808 }
809 OS << ")";
810}
811
Peter Collingbournee190dee2011-03-11 19:24:49 +0000812void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
813 switch(Node->getKind()) {
814 case UETT_SizeOf:
815 OS << "sizeof";
816 break;
817 case UETT_AlignOf:
Jordan Rose58d54722012-06-30 21:33:57 +0000818 if (Policy.LangOpts.CPlusPlus)
819 OS << "alignof";
820 else if (Policy.LangOpts.C11)
821 OS << "_Alignof";
822 else
823 OS << "__alignof";
Peter Collingbournee190dee2011-03-11 19:24:49 +0000824 break;
825 case UETT_VecStep:
826 OS << "vec_step";
827 break;
828 }
Sebastian Redl6f282892008-11-11 17:56:53 +0000829 if (Node->isArgumentType())
Daniel Dunbar219fa692010-06-30 19:16:48 +0000830 OS << "(" << Node->getArgumentType().getAsString(Policy) << ")";
Sebastian Redl6f282892008-11-11 17:56:53 +0000831 else {
832 OS << " ";
833 PrintExpr(Node->getArgumentExpr());
834 }
Chris Lattner882f7882006-11-04 18:52:07 +0000835}
Peter Collingbourne91147592011-04-15 00:35:48 +0000836
837void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
838 OS << "_Generic(";
839 PrintExpr(Node->getControllingExpr());
840 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
841 OS << ", ";
842 QualType T = Node->getAssocType(i);
843 if (T.isNull())
844 OS << "default";
845 else
846 OS << T.getAsString(Policy);
847 OS << ": ";
848 PrintExpr(Node->getAssocExpr(i));
849 }
850 OS << ")";
851}
852
Chris Lattner882f7882006-11-04 18:52:07 +0000853void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000854 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000855 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +0000856 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +0000857 OS << "]";
858}
859
Peter Collingbourne4b279a02011-02-08 21:17:54 +0000860void StmtPrinter::PrintCallArgs(CallExpr *Call) {
Chris Lattner882f7882006-11-04 18:52:07 +0000861 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000862 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
863 // Don't print any defaulted arguments
864 break;
865 }
866
Chris Lattner882f7882006-11-04 18:52:07 +0000867 if (i) OS << ", ";
868 PrintExpr(Call->getArg(i));
869 }
Peter Collingbourne4b279a02011-02-08 21:17:54 +0000870}
871
872void StmtPrinter::VisitCallExpr(CallExpr *Call) {
873 PrintExpr(Call->getCallee());
874 OS << "(";
875 PrintCallArgs(Call);
Chris Lattner882f7882006-11-04 18:52:07 +0000876 OS << ")";
877}
878void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregore4b414c2009-01-08 22:45:41 +0000879 // FIXME: Suppress printing implicit bases (like "this")
880 PrintExpr(Node->getBase());
Fariborz Jahanian2990c022010-01-11 21:17:32 +0000881 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
882 if (FD->isAnonymousStructOrUnion())
883 return;
Douglas Gregore4b414c2009-01-08 22:45:41 +0000884 OS << (Node->isArrow() ? "->" : ".");
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000885 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
886 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000887 if (Node->hasTemplateKeyword())
888 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000889 OS << Node->getMemberNameInfo();
John McCallb3774b52010-08-19 23:49:38 +0000890 if (Node->hasExplicitTemplateArgs())
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000891 OS << TemplateSpecializationType::PrintTemplateArgumentList(
892 Node->getTemplateArgs(),
893 Node->getNumTemplateArgs(),
894 Policy);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000895}
Steve Naroffe87026a2009-07-24 17:54:45 +0000896void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
897 PrintExpr(Node->getBase());
898 OS << (Node->isArrow() ? "->isa" : ".isa");
899}
900
Nate Begemance4d7fc2008-04-18 23:10:10 +0000901void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +0000902 PrintExpr(Node->getBase());
903 OS << ".";
904 OS << Node->getAccessor().getName();
905}
Douglas Gregorf19b2312008-10-28 15:36:24 +0000906void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Fariborz Jahaniane33c1162010-06-30 16:31:08 +0000907 OS << "(" << Node->getType().getAsString(Policy) << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000908 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000909}
Steve Naroff57eb2c52007-07-19 21:32:11 +0000910void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +0000911 OS << "(" << Node->getType().getAsString(Policy) << ")";
Steve Naroff57eb2c52007-07-19 21:32:11 +0000912 PrintExpr(Node->getInitializer());
913}
Steve Naroff7a5af782007-07-13 16:58:59 +0000914void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000915 // No need to print anything, simply forward to the sub expression.
916 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +0000917}
Chris Lattner882f7882006-11-04 18:52:07 +0000918void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
919 PrintExpr(Node->getLHS());
920 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
921 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000922}
Chris Lattner86928112007-08-25 02:00:02 +0000923void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
924 PrintExpr(Node->getLHS());
925 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
926 PrintExpr(Node->getRHS());
927}
Chris Lattner882f7882006-11-04 18:52:07 +0000928void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
929 PrintExpr(Node->getCond());
John McCallc07a0c72011-02-17 10:25:35 +0000930 OS << " ? ";
931 PrintExpr(Node->getLHS());
932 OS << " : ";
Chris Lattner882f7882006-11-04 18:52:07 +0000933 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000934}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000935
Chris Lattnereefa10e2007-05-28 06:56:27 +0000936// GNU extensions.
937
John McCallc07a0c72011-02-17 10:25:35 +0000938void
939StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
940 PrintExpr(Node->getCommon());
941 OS << " ?: ";
942 PrintExpr(Node->getFalseExpr());
943}
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000944void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000945 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +0000946}
947
Chris Lattner366727f2007-07-24 16:58:17 +0000948void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
949 OS << "(";
950 PrintRawCompoundStmt(E->getSubStmt());
951 OS << ")";
952}
953
Steve Naroff9efdabc2007-08-03 21:21:27 +0000954void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
955 OS << "__builtin_choose_expr(";
956 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +0000957 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000958 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +0000959 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +0000960 PrintExpr(Node->getRHS());
961 OS << ")";
962}
Chris Lattner366727f2007-07-24 16:58:17 +0000963
Douglas Gregor3be4b122008-11-29 04:51:27 +0000964void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
965 OS << "__null";
966}
967
Eli Friedmana1b4ed82008-05-14 19:38:39 +0000968void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
969 OS << "__builtin_shufflevector(";
970 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
971 if (i) OS << ", ";
972 PrintExpr(Node->getExpr(i));
973 }
974 OS << ")";
975}
976
Anders Carlsson4692db02007-08-31 04:56:16 +0000977void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor36098ff2009-05-30 00:56:08 +0000978 if (Node->getSyntacticForm()) {
979 Visit(Node->getSyntacticForm());
980 return;
981 }
982
Anders Carlsson4692db02007-08-31 04:56:16 +0000983 OS << "{ ";
984 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
985 if (i) OS << ", ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000986 if (Node->getInit(i))
987 PrintExpr(Node->getInit(i));
988 else
989 OS << "0";
Anders Carlsson4692db02007-08-31 04:56:16 +0000990 }
991 OS << " }";
992}
993
Nate Begeman5ec4b312009-08-10 23:49:36 +0000994void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
995 OS << "( ";
996 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
997 if (i) OS << ", ";
998 PrintExpr(Node->getExpr(i));
999 }
1000 OS << " )";
1001}
1002
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001003void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001004 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
1005 DEnd = Node->designators_end();
1006 D != DEnd; ++D) {
1007 if (D->isFieldDesignator()) {
1008 if (D->getDotLoc().isInvalid())
1009 OS << D->getFieldName()->getName() << ":";
1010 else
1011 OS << "." << D->getFieldName()->getName();
1012 } else {
1013 OS << "[";
1014 if (D->isArrayDesignator()) {
1015 PrintExpr(Node->getArrayIndex(*D));
1016 } else {
1017 PrintExpr(Node->getArrayRangeStart(*D));
1018 OS << " ... ";
Mike Stump11289f42009-09-09 15:08:12 +00001019 PrintExpr(Node->getArrayRangeEnd(*D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001020 }
1021 OS << "]";
1022 }
1023 }
1024
1025 OS << " = ";
1026 PrintExpr(Node->getInit());
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001027}
1028
Douglas Gregor0202cb42009-01-29 17:44:32 +00001029void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Chris Lattnerc61089a2009-06-30 01:26:17 +00001030 if (Policy.LangOpts.CPlusPlus)
Douglas Gregor278f52e2009-05-30 00:08:05 +00001031 OS << "/*implicit*/" << Node->getType().getAsString(Policy) << "()";
1032 else {
1033 OS << "/*implicit*/(" << Node->getType().getAsString(Policy) << ")";
1034 if (Node->getType()->isRecordType())
1035 OS << "{}";
1036 else
1037 OS << 0;
1038 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001039}
1040
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001041void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman79635842009-05-30 04:20:30 +00001042 OS << "__builtin_va_arg(";
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001043 PrintExpr(Node->getSubExpr());
1044 OS << ", ";
Daniel Dunbar219fa692010-06-30 19:16:48 +00001045 OS << Node->getType().getAsString(Policy);
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001046 OS << ")";
1047}
1048
John McCallfe96e0b2011-11-06 09:01:30 +00001049void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1050 PrintExpr(Node->getSyntacticForm());
1051}
1052
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001053void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
Eli Friedmanc2025562011-10-11 20:00:47 +00001054 const char *Name = 0;
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001055 switch (Node->getOp()) {
Richard Smithfeea8832012-04-12 05:08:17 +00001056#define BUILTIN(ID, TYPE, ATTRS)
1057#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1058 case AtomicExpr::AO ## ID: \
1059 Name = #ID "("; \
1060 break;
1061#include "clang/Basic/Builtins.def"
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001062 }
1063 OS << Name;
Richard Smithfeea8832012-04-12 05:08:17 +00001064
1065 // AtomicExpr stores its subexpressions in a permuted order.
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001066 PrintExpr(Node->getPtr());
1067 OS << ", ";
Richard Smithfeea8832012-04-12 05:08:17 +00001068 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1069 Node->getOp() != AtomicExpr::AO__atomic_load_n) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001070 PrintExpr(Node->getVal1());
1071 OS << ", ";
1072 }
Richard Smithfeea8832012-04-12 05:08:17 +00001073 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1074 Node->isCmpXChg()) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001075 PrintExpr(Node->getVal2());
1076 OS << ", ";
1077 }
Richard Smithfeea8832012-04-12 05:08:17 +00001078 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1079 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
1080 PrintExpr(Node->getWeak());
1081 OS << ", ";
1082 }
1083 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init)
David Chisnallfa35df62012-01-16 17:27:18 +00001084 PrintExpr(Node->getOrder());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001085 if (Node->isCmpXChg()) {
1086 OS << ", ";
1087 PrintExpr(Node->getOrderFail());
1088 }
1089 OS << ")";
1090}
1091
Chris Lattnereefa10e2007-05-28 06:56:27 +00001092// C++
Douglas Gregor993603d2008-11-14 16:09:21 +00001093void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1094 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1095 "",
1096#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1097 Spelling,
1098#include "clang/Basic/OperatorKinds.def"
1099 };
1100
1101 OverloadedOperatorKind Kind = Node->getOperator();
1102 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1103 if (Node->getNumArgs() == 1) {
1104 OS << OpStrings[Kind] << ' ';
1105 PrintExpr(Node->getArg(0));
1106 } else {
1107 PrintExpr(Node->getArg(0));
1108 OS << ' ' << OpStrings[Kind];
1109 }
1110 } else if (Kind == OO_Call) {
1111 PrintExpr(Node->getArg(0));
1112 OS << '(';
1113 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1114 if (ArgIdx > 1)
1115 OS << ", ";
1116 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1117 PrintExpr(Node->getArg(ArgIdx));
1118 }
1119 OS << ')';
1120 } else if (Kind == OO_Subscript) {
1121 PrintExpr(Node->getArg(0));
1122 OS << '[';
1123 PrintExpr(Node->getArg(1));
1124 OS << ']';
1125 } else if (Node->getNumArgs() == 1) {
1126 OS << OpStrings[Kind] << ' ';
1127 PrintExpr(Node->getArg(0));
1128 } else if (Node->getNumArgs() == 2) {
1129 PrintExpr(Node->getArg(0));
1130 OS << ' ' << OpStrings[Kind] << ' ';
1131 PrintExpr(Node->getArg(1));
1132 } else {
David Blaikie83d382b2011-09-23 05:06:16 +00001133 llvm_unreachable("unknown overloaded operator");
Douglas Gregor993603d2008-11-14 16:09:21 +00001134 }
1135}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001136
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001137void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
1138 VisitCallExpr(cast<CallExpr>(Node));
1139}
1140
Peter Collingbourne41f85462011-02-09 21:07:24 +00001141void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1142 PrintExpr(Node->getCallee());
1143 OS << "<<<";
1144 PrintCallArgs(Node->getConfig());
1145 OS << ">>>(";
1146 PrintCallArgs(Node);
1147 OS << ")";
1148}
1149
Douglas Gregore200adc2008-10-27 19:41:14 +00001150void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1151 OS << Node->getCastName() << '<';
Daniel Dunbar219fa692010-06-30 19:16:48 +00001152 OS << Node->getTypeAsWritten().getAsString(Policy) << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +00001153 PrintExpr(Node->getSubExpr());
1154 OS << ")";
1155}
1156
Douglas Gregore200adc2008-10-27 19:41:14 +00001157void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1158 VisitCXXNamedCastExpr(Node);
1159}
1160
1161void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1162 VisitCXXNamedCastExpr(Node);
1163}
1164
1165void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1166 VisitCXXNamedCastExpr(Node);
1167}
1168
1169void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1170 VisitCXXNamedCastExpr(Node);
1171}
1172
Sebastian Redlc4704762008-11-11 11:37:55 +00001173void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1174 OS << "typeid(";
1175 if (Node->isTypeOperand()) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001176 OS << Node->getTypeOperand().getAsString(Policy);
Sebastian Redlc4704762008-11-11 11:37:55 +00001177 } else {
1178 PrintExpr(Node->getExprOperand());
1179 }
1180 OS << ")";
1181}
1182
Francois Pichet9f4f2072010-09-08 12:20:18 +00001183void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1184 OS << "__uuidof(";
1185 if (Node->isTypeOperand()) {
1186 OS << Node->getTypeOperand().getAsString(Policy);
1187 } else {
1188 PrintExpr(Node->getExprOperand());
1189 }
1190 OS << ")";
1191}
1192
Richard Smithc67fdd42012-03-07 08:35:16 +00001193void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1194 switch (Node->getLiteralOperatorKind()) {
1195 case UserDefinedLiteral::LOK_Raw:
Richard Smith29e95952012-03-09 10:10:02 +00001196 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
Richard Smithc67fdd42012-03-07 08:35:16 +00001197 break;
1198 case UserDefinedLiteral::LOK_Template: {
Richard Smith29e95952012-03-09 10:10:02 +00001199 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1200 const TemplateArgumentList *Args =
1201 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1202 assert(Args);
1203 const TemplateArgument &Pack = Args->get(0);
1204 for (TemplateArgument::pack_iterator I = Pack.pack_begin(),
1205 E = Pack.pack_end(); I != E; ++I) {
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001206 char C = (char)I->getAsIntegral().getZExtValue();
Richard Smithc67fdd42012-03-07 08:35:16 +00001207 OS << C;
1208 }
1209 break;
1210 }
Richard Smith75025ba2012-03-08 09:02:38 +00001211 case UserDefinedLiteral::LOK_Integer: {
1212 // Print integer literal without suffix.
1213 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1214 OS << Int->getValue().toString(10, /*isSigned*/false);
1215 break;
1216 }
Richard Smithc67fdd42012-03-07 08:35:16 +00001217 case UserDefinedLiteral::LOK_Floating:
1218 case UserDefinedLiteral::LOK_String:
1219 case UserDefinedLiteral::LOK_Character:
1220 PrintExpr(Node->getCookedLiteral());
1221 break;
1222 }
1223 OS << Node->getUDSuffix()->getName();
1224}
1225
Chris Lattnereefa10e2007-05-28 06:56:27 +00001226void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1227 OS << (Node->getValue() ? "true" : "false");
1228}
1229
Sebastian Redl576fd422009-05-10 18:38:11 +00001230void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1231 OS << "nullptr";
1232}
1233
Douglas Gregor97a9c812008-11-04 14:32:21 +00001234void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1235 OS << "this";
1236}
1237
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001238void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1239 if (Node->getSubExpr() == 0)
1240 OS << "throw";
1241 else {
1242 OS << "throw ";
1243 PrintExpr(Node->getSubExpr());
1244 }
1245}
1246
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001247void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
1248 // Nothing to print: we picked up the default argument
1249}
1250
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001251void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001252 OS << Node->getType().getAsString(Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001253 OS << "(";
1254 PrintExpr(Node->getSubExpr());
1255 OS << ")";
1256}
1257
Anders Carlsson993a4b32009-05-30 20:03:25 +00001258void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1259 PrintExpr(Node->getSubExpr());
1260}
1261
Douglas Gregordd04d332009-01-16 18:33:17 +00001262void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001263 OS << Node->getType().getAsString(Policy);
Douglas Gregordd04d332009-01-16 18:33:17 +00001264 OS << "(";
1265 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001266 ArgEnd = Node->arg_end();
Douglas Gregordd04d332009-01-16 18:33:17 +00001267 Arg != ArgEnd; ++Arg) {
1268 if (Arg != Node->arg_begin())
1269 OS << ", ";
1270 PrintExpr(*Arg);
1271 }
1272 OS << ")";
1273}
1274
Douglas Gregore31e6062012-02-07 10:09:13 +00001275void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1276 OS << '[';
1277 bool NeedComma = false;
1278 switch (Node->getCaptureDefault()) {
1279 case LCD_None:
1280 break;
1281
1282 case LCD_ByCopy:
1283 OS << '=';
1284 NeedComma = true;
1285 break;
1286
1287 case LCD_ByRef:
1288 OS << '&';
1289 NeedComma = true;
1290 break;
1291 }
1292 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1293 CEnd = Node->explicit_capture_end();
1294 C != CEnd;
1295 ++C) {
1296 if (NeedComma)
1297 OS << ", ";
1298 NeedComma = true;
1299
1300 switch (C->getCaptureKind()) {
1301 case LCK_This:
1302 OS << "this";
1303 break;
1304
1305 case LCK_ByRef:
1306 if (Node->getCaptureDefault() != LCD_ByRef)
1307 OS << '&';
1308 OS << C->getCapturedVar()->getName();
1309 break;
1310
1311 case LCK_ByCopy:
1312 if (Node->getCaptureDefault() != LCD_ByCopy)
1313 OS << '=';
1314 OS << C->getCapturedVar()->getName();
1315 break;
1316 }
1317 }
1318 OS << ']';
1319
1320 if (Node->hasExplicitParameters()) {
1321 OS << " (";
1322 CXXMethodDecl *Method = Node->getCallOperator();
1323 NeedComma = false;
1324 for (CXXMethodDecl::param_iterator P = Method->param_begin(),
1325 PEnd = Method->param_end();
1326 P != PEnd; ++P) {
1327 if (NeedComma) {
1328 OS << ", ";
1329 } else {
1330 NeedComma = true;
1331 }
1332 std::string ParamStr = (*P)->getNameAsString();
1333 (*P)->getOriginalType().getAsStringInternal(ParamStr, Policy);
1334 OS << ParamStr;
1335 }
1336 if (Method->isVariadic()) {
1337 if (NeedComma)
1338 OS << ", ";
1339 OS << "...";
1340 }
1341 OS << ')';
1342
1343 if (Node->isMutable())
1344 OS << " mutable";
1345
1346 const FunctionProtoType *Proto
1347 = Method->getType()->getAs<FunctionProtoType>();
1348 {
1349 std::string ExceptionSpec;
1350 Proto->printExceptionSpecification(ExceptionSpec, Policy);
1351 OS << ExceptionSpec;
1352 }
1353
1354 // FIXME: Attributes
1355
Douglas Gregor0c46b2b2012-02-13 22:00:16 +00001356 // Print the trailing return type if it was specified in the source.
1357 if (Node->hasExplicitResultType())
1358 OS << " -> " << Proto->getResultType().getAsString(Policy);
Douglas Gregore31e6062012-02-07 10:09:13 +00001359 }
1360
1361 // Print the body.
1362 CompoundStmt *Body = Node->getBody();
1363 OS << ' ';
1364 PrintStmt(Body);
1365}
1366
Douglas Gregor747eb782010-07-08 06:14:04 +00001367void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregor2b88c112010-09-08 00:15:04 +00001368 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
1369 OS << TSInfo->getType().getAsString(Policy) << "()";
1370 else
1371 OS << Node->getType().getAsString(Policy) << "()";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001372}
1373
Sebastian Redlbd150f42008-11-21 19:14:01 +00001374void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1375 if (E->isGlobalNew())
1376 OS << "::";
1377 OS << "new ";
1378 unsigned NumPlace = E->getNumPlacementArgs();
1379 if (NumPlace > 0) {
1380 OS << "(";
1381 PrintExpr(E->getPlacementArg(0));
1382 for (unsigned i = 1; i < NumPlace; ++i) {
1383 OS << ", ";
1384 PrintExpr(E->getPlacementArg(i));
1385 }
1386 OS << ") ";
1387 }
1388 if (E->isParenTypeId())
1389 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001390 std::string TypeS;
1391 if (Expr *Size = E->getArraySize()) {
1392 llvm::raw_string_ostream s(TypeS);
Eli Friedmanc4fc8392009-05-30 05:32:46 +00001393 Size->printPretty(s, Context, Helper, Policy);
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001394 s.flush();
1395 TypeS = "[" + TypeS + "]";
1396 }
Douglas Gregor7de59662009-05-29 20:38:28 +00001397 E->getAllocatedType().getAsStringInternal(TypeS, Policy);
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001398 OS << TypeS;
Sebastian Redlbd150f42008-11-21 19:14:01 +00001399 if (E->isParenTypeId())
1400 OS << ")";
1401
Sebastian Redl6047f072012-02-16 12:22:20 +00001402 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
1403 if (InitStyle) {
1404 if (InitStyle == CXXNewExpr::CallInit)
1405 OS << "(";
1406 PrintExpr(E->getInitializer());
1407 if (InitStyle == CXXNewExpr::CallInit)
1408 OS << ")";
Sebastian Redlbd150f42008-11-21 19:14:01 +00001409 }
1410}
1411
1412void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1413 if (E->isGlobalDelete())
1414 OS << "::";
1415 OS << "delete ";
1416 if (E->isArrayForm())
1417 OS << "[] ";
1418 PrintExpr(E->getArgument());
1419}
1420
Douglas Gregorad8a3362009-09-04 17:36:40 +00001421void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1422 PrintExpr(E->getBase());
1423 if (E->isArrow())
1424 OS << "->";
1425 else
1426 OS << '.';
1427 if (E->getQualifier())
1428 E->getQualifier()->print(OS, Policy);
Mike Stump11289f42009-09-09 15:08:12 +00001429
Douglas Gregorad8a3362009-09-04 17:36:40 +00001430 std::string TypeS;
Douglas Gregor678f90d2010-02-25 01:56:36 +00001431 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1432 OS << II->getName();
1433 else
1434 E->getDestroyedType().getAsStringInternal(TypeS, Policy);
Douglas Gregorad8a3362009-09-04 17:36:40 +00001435 OS << TypeS;
1436}
1437
Anders Carlsson0781ce72009-04-23 02:32:43 +00001438void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Douglas Gregorbaba85d2011-01-24 17:25:03 +00001439 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1440 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1441 // Don't print any defaulted arguments
1442 break;
1443 }
1444
1445 if (i) OS << ", ";
1446 PrintExpr(E->getArg(i));
Fariborz Jahaniand0bbf662010-01-13 21:41:11 +00001447 }
Anders Carlsson0781ce72009-04-23 02:32:43 +00001448}
1449
John McCall5d413782010-12-06 08:20:24 +00001450void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Anders Carlssondefc6442009-04-24 22:47:04 +00001451 // Just forward to the sub expression.
1452 PrintExpr(E->getSubExpr());
1453}
1454
Mike Stump11289f42009-09-09 15:08:12 +00001455void
Douglas Gregorce934142009-05-20 18:46:25 +00001456StmtPrinter::VisitCXXUnresolvedConstructExpr(
1457 CXXUnresolvedConstructExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001458 OS << Node->getTypeAsWritten().getAsString(Policy);
Douglas Gregorce934142009-05-20 18:46:25 +00001459 OS << "(";
1460 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001461 ArgEnd = Node->arg_end();
Douglas Gregorce934142009-05-20 18:46:25 +00001462 Arg != ArgEnd; ++Arg) {
1463 if (Arg != Node->arg_begin())
1464 OS << ", ";
1465 PrintExpr(*Arg);
1466 }
1467 OS << ")";
1468}
1469
John McCall8cd78132009-11-19 22:55:06 +00001470void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1471 CXXDependentScopeMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001472 if (!Node->isImplicitAccess()) {
1473 PrintExpr(Node->getBase());
1474 OS << (Node->isArrow() ? "->" : ".");
1475 }
Douglas Gregorc26e0f62009-09-03 16:14:30 +00001476 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1477 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001478 if (Node->hasTemplateKeyword())
Douglas Gregor308047d2009-09-09 00:23:06 +00001479 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001480 OS << Node->getMemberNameInfo();
John McCall2d74de92009-12-01 22:10:20 +00001481 if (Node->hasExplicitTemplateArgs()) {
Douglas Gregor308047d2009-09-09 00:23:06 +00001482 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1483 Node->getTemplateArgs(),
1484 Node->getNumTemplateArgs(),
1485 Policy);
1486 }
Douglas Gregora8db9542009-05-22 21:13:27 +00001487}
1488
John McCall10eae182009-11-30 22:42:35 +00001489void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001490 if (!Node->isImplicitAccess()) {
1491 PrintExpr(Node->getBase());
1492 OS << (Node->isArrow() ? "->" : ".");
1493 }
John McCall10eae182009-11-30 22:42:35 +00001494 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1495 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001496 if (Node->hasTemplateKeyword())
1497 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001498 OS << Node->getMemberNameInfo();
John McCall10eae182009-11-30 22:42:35 +00001499 if (Node->hasExplicitTemplateArgs()) {
1500 OS << TemplateSpecializationType::PrintTemplateArgumentList(
1501 Node->getTemplateArgs(),
1502 Node->getNumTemplateArgs(),
1503 Policy);
1504 }
1505}
1506
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001507static const char *getTypeTraitName(UnaryTypeTrait UTT) {
1508 switch (UTT) {
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001509 case UTT_HasNothrowAssign: return "__has_nothrow_assign";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001510 case UTT_HasNothrowConstructor: return "__has_nothrow_constructor";
John Wiegley65497cc2011-04-27 23:09:49 +00001511 case UTT_HasNothrowCopy: return "__has_nothrow_copy";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001512 case UTT_HasTrivialAssign: return "__has_trivial_assign";
Alexis Huntf479f1b2011-05-09 18:22:59 +00001513 case UTT_HasTrivialDefaultConstructor: return "__has_trivial_constructor";
John Wiegley65497cc2011-04-27 23:09:49 +00001514 case UTT_HasTrivialCopy: return "__has_trivial_copy";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001515 case UTT_HasTrivialDestructor: return "__has_trivial_destructor";
1516 case UTT_HasVirtualDestructor: return "__has_virtual_destructor";
1517 case UTT_IsAbstract: return "__is_abstract";
John Wiegley65497cc2011-04-27 23:09:49 +00001518 case UTT_IsArithmetic: return "__is_arithmetic";
1519 case UTT_IsArray: return "__is_array";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001520 case UTT_IsClass: return "__is_class";
John Wiegley65497cc2011-04-27 23:09:49 +00001521 case UTT_IsCompleteType: return "__is_complete_type";
1522 case UTT_IsCompound: return "__is_compound";
1523 case UTT_IsConst: return "__is_const";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001524 case UTT_IsEmpty: return "__is_empty";
1525 case UTT_IsEnum: return "__is_enum";
Douglas Gregordca70af2011-12-03 18:14:24 +00001526 case UTT_IsFinal: return "__is_final";
John Wiegley65497cc2011-04-27 23:09:49 +00001527 case UTT_IsFloatingPoint: return "__is_floating_point";
1528 case UTT_IsFunction: return "__is_function";
1529 case UTT_IsFundamental: return "__is_fundamental";
1530 case UTT_IsIntegral: return "__is_integral";
Chandler Carruth8e2d6f42011-05-01 07:23:20 +00001531 case UTT_IsLiteral: return "__is_literal";
John Wiegley65497cc2011-04-27 23:09:49 +00001532 case UTT_IsLvalueReference: return "__is_lvalue_reference";
1533 case UTT_IsMemberFunctionPointer: return "__is_member_function_pointer";
1534 case UTT_IsMemberObjectPointer: return "__is_member_object_pointer";
1535 case UTT_IsMemberPointer: return "__is_member_pointer";
1536 case UTT_IsObject: return "__is_object";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001537 case UTT_IsPOD: return "__is_pod";
John Wiegley65497cc2011-04-27 23:09:49 +00001538 case UTT_IsPointer: return "__is_pointer";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001539 case UTT_IsPolymorphic: return "__is_polymorphic";
John Wiegley65497cc2011-04-27 23:09:49 +00001540 case UTT_IsReference: return "__is_reference";
John Wiegley65497cc2011-04-27 23:09:49 +00001541 case UTT_IsRvalueReference: return "__is_rvalue_reference";
1542 case UTT_IsScalar: return "__is_scalar";
1543 case UTT_IsSigned: return "__is_signed";
1544 case UTT_IsStandardLayout: return "__is_standard_layout";
1545 case UTT_IsTrivial: return "__is_trivial";
Alexis Huntd9a5cc12011-05-13 00:31:07 +00001546 case UTT_IsTriviallyCopyable: return "__is_trivially_copyable";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001547 case UTT_IsUnion: return "__is_union";
John Wiegley65497cc2011-04-27 23:09:49 +00001548 case UTT_IsUnsigned: return "__is_unsigned";
1549 case UTT_IsVoid: return "__is_void";
1550 case UTT_IsVolatile: return "__is_volatile";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001551 }
Chandler Carruth8e2d6f42011-05-01 07:23:20 +00001552 llvm_unreachable("Type trait not covered by switch statement");
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001553}
1554
1555static const char *getTypeTraitName(BinaryTypeTrait BTT) {
1556 switch (BTT) {
Douglas Gregor1be329d2012-02-23 07:33:15 +00001557 case BTT_IsBaseOf: return "__is_base_of";
1558 case BTT_IsConvertible: return "__is_convertible";
1559 case BTT_IsSame: return "__is_same";
1560 case BTT_TypeCompatible: return "__builtin_types_compatible_p";
1561 case BTT_IsConvertibleTo: return "__is_convertible_to";
1562 case BTT_IsTriviallyAssignable: return "__is_trivially_assignable";
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001563 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001564 llvm_unreachable("Binary type trait not covered by switch");
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001565}
1566
Douglas Gregor29c42f22012-02-24 07:38:34 +00001567static const char *getTypeTraitName(TypeTrait TT) {
1568 switch (TT) {
1569 case clang::TT_IsTriviallyConstructible:return "__is_trivially_constructible";
1570 }
1571 llvm_unreachable("Type trait not covered by switch");
1572}
1573
John Wiegley6242b6a2011-04-28 00:16:57 +00001574static const char *getTypeTraitName(ArrayTypeTrait ATT) {
1575 switch (ATT) {
1576 case ATT_ArrayRank: return "__array_rank";
1577 case ATT_ArrayExtent: return "__array_extent";
1578 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001579 llvm_unreachable("Array type trait not covered by switch");
John Wiegley6242b6a2011-04-28 00:16:57 +00001580}
1581
John Wiegleyf9f65842011-04-25 06:54:41 +00001582static const char *getExpressionTraitName(ExpressionTrait ET) {
1583 switch (ET) {
John Wiegleyf9f65842011-04-25 06:54:41 +00001584 case ET_IsLValueExpr: return "__is_lvalue_expr";
1585 case ET_IsRValueExpr: return "__is_rvalue_expr";
1586 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001587 llvm_unreachable("Expression type trait not covered by switch");
John Wiegleyf9f65842011-04-25 06:54:41 +00001588}
1589
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001590void StmtPrinter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1591 OS << getTypeTraitName(E->getTrait()) << "("
Daniel Dunbar219fa692010-06-30 19:16:48 +00001592 << E->getQueriedType().getAsString(Policy) << ")";
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001593}
1594
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001595void StmtPrinter::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
1596 OS << getTypeTraitName(E->getTrait()) << "("
1597 << E->getLhsType().getAsString(Policy) << ","
1598 << E->getRhsType().getAsString(Policy) << ")";
1599}
1600
Douglas Gregor29c42f22012-02-24 07:38:34 +00001601void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
1602 OS << getTypeTraitName(E->getTrait()) << "(";
1603 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
1604 if (I > 0)
1605 OS << ", ";
1606 OS << E->getArg(I)->getType().getAsString(Policy);
1607 }
1608 OS << ")";
1609}
1610
John Wiegley6242b6a2011-04-28 00:16:57 +00001611void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
1612 OS << getTypeTraitName(E->getTrait()) << "("
1613 << E->getQueriedType().getAsString(Policy) << ")";
1614}
1615
John Wiegleyf9f65842011-04-25 06:54:41 +00001616void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
1617 OS << getExpressionTraitName(E->getTrait()) << "(";
1618 PrintExpr(E->getQueriedExpression());
1619 OS << ")";
1620}
1621
Sebastian Redl4202c0f2010-09-10 20:55:43 +00001622void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1623 OS << "noexcept(";
1624 PrintExpr(E->getOperand());
1625 OS << ")";
1626}
1627
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001628void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregore8e9dd62011-01-03 17:17:50 +00001629 PrintExpr(E->getPattern());
1630 OS << "...";
1631}
1632
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001633void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001634 OS << "sizeof...(" << *E->getPack() << ")";
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001635}
1636
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001637void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
1638 SubstNonTypeTemplateParmPackExpr *Node) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001639 OS << *Node->getParameterPack();
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001640}
1641
John McCall7c454bb2011-07-15 05:09:51 +00001642void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
1643 SubstNonTypeTemplateParmExpr *Node) {
1644 Visit(Node->getReplacement());
1645}
1646
Douglas Gregorfe314812011-06-21 17:03:29 +00001647void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
1648 PrintExpr(Node->GetTemporaryExpr());
1649}
1650
Mike Stump11289f42009-09-09 15:08:12 +00001651// Obj-C
Anders Carlsson76f4a902007-08-21 17:43:55 +00001652
1653void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1654 OS << "@";
1655 VisitStringLiteral(Node->getString());
1656}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001657
Patrick Beard0caa3942012-04-19 00:25:12 +00001658void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001659 OS << "@";
Patrick Beard0caa3942012-04-19 00:25:12 +00001660 Visit(E->getSubExpr());
Ted Kremeneke65b0862012-03-06 20:05:56 +00001661}
1662
1663void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
1664 OS << "@[ ";
1665 StmtRange ch = E->children();
1666 if (ch.first != ch.second) {
1667 while (1) {
1668 Visit(*ch.first);
1669 ++ch.first;
1670 if (ch.first == ch.second) break;
1671 OS << ", ";
1672 }
1673 }
1674 OS << " ]";
1675}
1676
1677void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
1678 OS << "@{ ";
1679 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
1680 if (I > 0)
1681 OS << ", ";
1682
1683 ObjCDictionaryElement Element = E->getKeyValueElement(I);
1684 Visit(Element.Key);
1685 OS << " : ";
1686 Visit(Element.Value);
1687 if (Element.isPackExpansion())
1688 OS << "...";
1689 }
1690 OS << " }";
1691}
1692
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001693void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Daniel Dunbar219fa692010-06-30 19:16:48 +00001694 OS << "@encode(" << Node->getEncodedType().getAsString(Policy) << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001695}
1696
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001697void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001698 OS << "@selector(" << Node->getSelector().getAsString() << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001699}
1700
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001701void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001702 OS << "@protocol(" << *Node->getProtocol() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001703}
1704
Steve Naroffd54978b2007-09-18 23:55:05 +00001705void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1706 OS << "[";
Douglas Gregor9a129192010-04-21 00:45:42 +00001707 switch (Mess->getReceiverKind()) {
1708 case ObjCMessageExpr::Instance:
1709 PrintExpr(Mess->getInstanceReceiver());
1710 break;
1711
1712 case ObjCMessageExpr::Class:
1713 OS << Mess->getClassReceiver().getAsString(Policy);
1714 break;
1715
1716 case ObjCMessageExpr::SuperInstance:
1717 case ObjCMessageExpr::SuperClass:
1718 OS << "Super";
1719 break;
1720 }
1721
Ted Kremeneka06e7122008-05-02 17:32:38 +00001722 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00001723 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001724 if (selector.isUnarySelector()) {
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00001725 OS << selector.getNameForSlot(0);
Steve Naroffc6814ea2007-10-02 20:01:56 +00001726 } else {
1727 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001728 if (i < selector.getNumArgs()) {
1729 if (i > 0) OS << ' ';
1730 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001731 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00001732 else
1733 OS << ":";
1734 }
1735 else OS << ", "; // Handle variadic methods.
Mike Stump11289f42009-09-09 15:08:12 +00001736
Steve Naroffc6814ea2007-10-02 20:01:56 +00001737 PrintExpr(Mess->getArg(i));
1738 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001739 }
1740 OS << "]";
1741}
1742
Ted Kremeneke65b0862012-03-06 20:05:56 +00001743void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
1744 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
1745}
1746
John McCall31168b02011-06-15 23:02:42 +00001747void
1748StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
1749 PrintExpr(E->getSubExpr());
1750}
1751
1752void
1753StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
1754 OS << "(" << E->getBridgeKindName() << E->getType().getAsString(Policy)
1755 << ")";
1756 PrintExpr(E->getSubExpr());
1757}
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001758
Steve Naroffc540d662008-09-03 18:15:37 +00001759void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001760 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00001761 OS << "^";
Mike Stump11289f42009-09-09 15:08:12 +00001762
Steve Naroffc540d662008-09-03 18:15:37 +00001763 const FunctionType *AFT = Node->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00001764
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001765 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroffc540d662008-09-03 18:15:37 +00001766 OS << "()";
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001767 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00001768 OS << '(';
1769 std::string ParamStr;
Steve Naroff415d3d52008-10-08 17:01:13 +00001770 for (BlockDecl::param_iterator AI = BD->param_begin(),
1771 E = BD->param_end(); AI != E; ++AI) {
1772 if (AI != BD->param_begin()) OS << ", ";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001773 ParamStr = (*AI)->getNameAsString();
Douglas Gregor7de59662009-05-29 20:38:28 +00001774 (*AI)->getType().getAsStringInternal(ParamStr, Policy);
Steve Naroffc540d662008-09-03 18:15:37 +00001775 OS << ParamStr;
1776 }
Mike Stump11289f42009-09-09 15:08:12 +00001777
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001778 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00001779 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001780 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00001781 OS << "...";
1782 }
1783 OS << ')';
1784 }
1785}
1786
Ted Kremenekf551f322011-11-30 22:08:08 +00001787void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
1788 PrintExpr(Node->getSourceExpr());
1789}
John McCall8d69a212010-11-15 23:31:06 +00001790
Tanya Lattner55808c12011-06-04 00:47:47 +00001791void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
1792 OS << "__builtin_astype(";
1793 PrintExpr(Node->getSrcExpr());
1794 OS << ", " << Node->getType().getAsString();
1795 OS << ")";
1796}
1797
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001798//===----------------------------------------------------------------------===//
1799// Stmt method implementations
1800//===----------------------------------------------------------------------===//
1801
Eli Friedmanef334fd2009-05-30 05:03:24 +00001802void Stmt::dumpPretty(ASTContext& Context) const {
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +00001803 printPretty(llvm::errs(), Context, 0,
David Blaikiebbafb8a2012-03-11 07:00:24 +00001804 PrintingPolicy(Context.getLangOpts()));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001805}
1806
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001807void Stmt::printPretty(raw_ostream &OS, ASTContext& Context,
Eli Friedmanef334fd2009-05-30 05:03:24 +00001808 PrinterHelper* Helper,
Douglas Gregor7de59662009-05-29 20:38:28 +00001809 const PrintingPolicy &Policy,
1810 unsigned Indentation) const {
Chris Lattner882f7882006-11-04 18:52:07 +00001811 if (this == 0) {
1812 OS << "<NULL>";
1813 return;
1814 }
1815
Douglas Gregor8655e882009-10-16 22:46:09 +00001816 if (Policy.Dump && &Context) {
Argyrios Kyrtzidisc049f752010-08-09 10:54:31 +00001817 dump(OS, Context.getSourceManager());
Douglas Gregor278f52e2009-05-30 00:08:05 +00001818 return;
1819 }
Mike Stump11289f42009-09-09 15:08:12 +00001820
Eli Friedmanef334fd2009-05-30 05:03:24 +00001821 StmtPrinter P(OS, Context, Helper, Policy, Indentation);
Chris Lattner62249a62007-08-21 04:04:25 +00001822 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001823}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001824
1825//===----------------------------------------------------------------------===//
1826// PrinterHelper
1827//===----------------------------------------------------------------------===//
1828
1829// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +00001830PrinterHelper::~PrinterHelper() {}