blob: 2a704c3a8493180e51527a068414a7b1fc6de56b [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"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000016#include "clang/AST/Attr.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"
Douglas Gregor882211c2010-04-28 22:16:22 +000020#include "clang/AST/Expr.h"
Douglas Gregor2b88c112010-09-08 00:15:04 +000021#include "clang/AST/ExprCXX.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000022#include "clang/AST/PrettyPrinter.h"
23#include "clang/AST/StmtVisitor.h"
Jordan Rose00d1b592013-02-08 22:30:27 +000024#include "clang/Basic/CharInfo.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000025#include "llvm/ADT/SmallString.h"
Jordan Rose00d1b592013-02-08 22:30:27 +000026#include "llvm/Support/Format.h"
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000027using namespace clang;
28
29//===----------------------------------------------------------------------===//
30// StmtPrinter Visitor
31//===----------------------------------------------------------------------===//
32
33namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +000034 class StmtPrinter : public StmtVisitor<StmtPrinter> {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000035 raw_ostream &OS;
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000036 unsigned IndentLevel;
Ted Kremenek04f3cee2007-08-31 21:30:12 +000037 clang::PrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +000038 PrintingPolicy Policy;
39
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000040 public:
Richard Smith235341b2012-08-16 03:56:14 +000041 StmtPrinter(raw_ostream &os, PrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +000042 const PrintingPolicy &Policy,
Douglas Gregor7de59662009-05-29 20:38:28 +000043 unsigned Indentation = 0)
Richard Smith235341b2012-08-16 03:56:14 +000044 : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy) {}
Mike Stump11289f42009-09-09 15:08:12 +000045
Douglas Gregor7de59662009-05-29 20:38:28 +000046 void PrintStmt(Stmt *S) {
47 PrintStmt(S, Policy.Indentation);
48 }
49
50 void PrintStmt(Stmt *S, int SubIndent) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000051 IndentLevel += SubIndent;
Chris Lattnera076fde2007-05-31 18:21:33 +000052 if (S && isa<Expr>(S)) {
Chris Lattner882f7882006-11-04 18:52:07 +000053 // If this is an expr used in a stmt context, indent and newline it.
54 Indent();
Chris Lattner62249a62007-08-21 04:04:25 +000055 Visit(S);
Chris Lattnerfc068c12007-05-30 17:57:36 +000056 OS << ";\n";
Chris Lattner882f7882006-11-04 18:52:07 +000057 } else if (S) {
Chris Lattner62249a62007-08-21 04:04:25 +000058 Visit(S);
Chris Lattner882f7882006-11-04 18:52:07 +000059 } else {
Chris Lattner2f6ac262007-05-28 01:47:47 +000060 Indent() << "<<<NULL STATEMENT>>>\n";
Chris Lattner882f7882006-11-04 18:52:07 +000061 }
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000062 IndentLevel -= SubIndent;
Chris Lattner882f7882006-11-04 18:52:07 +000063 }
Eli Friedman15ea8802009-05-30 00:19:54 +000064
Chris Lattner073926e2007-05-20 23:04:55 +000065 void PrintRawCompoundStmt(CompoundStmt *S);
Chris Lattnerfdc195a2007-06-05 20:52:47 +000066 void PrintRawDecl(Decl *D);
Eli Friedmanf86e5072012-10-16 23:45:15 +000067 void PrintRawDeclStmt(const DeclStmt *S);
Chris Lattnerc0a38dd2007-06-11 22:26:23 +000068 void PrintRawIfStmt(IfStmt *If);
Sebastian Redl9b244a82008-12-22 21:35:02 +000069 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
Peter Collingbourne4b279a02011-02-08 21:17:54 +000070 void PrintCallArgs(CallExpr *E);
John Wiegley1c0675e2011-04-28 01:08:34 +000071 void PrintRawSEHExceptHandler(SEHExceptStmt *S);
72 void PrintRawSEHFinallyStmt(SEHFinallyStmt *S);
Alexey Bataev1b59ab52014-02-27 08:29:12 +000073 void PrintOMPExecutableDirective(OMPExecutableDirective *S);
Mike Stump11289f42009-09-09 15:08:12 +000074
Chris Lattner882f7882006-11-04 18:52:07 +000075 void PrintExpr(Expr *E) {
76 if (E)
Chris Lattner62249a62007-08-21 04:04:25 +000077 Visit(E);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000078 else
Chris Lattner882f7882006-11-04 18:52:07 +000079 OS << "<null expr>";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000080 }
Mike Stump11289f42009-09-09 15:08:12 +000081
Chris Lattner0e62c1c2011-07-23 10:55:15 +000082 raw_ostream &Indent(int Delta = 0) {
Douglas Gregor7de59662009-05-29 20:38:28 +000083 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
84 OS << " ";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000085 return OS;
86 }
Mike Stump11289f42009-09-09 15:08:12 +000087
Mike Stump11289f42009-09-09 15:08:12 +000088 void Visit(Stmt* S) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +000089 if (Helper && Helper->handledStmt(S,OS))
90 return;
91 else StmtVisitor<StmtPrinter>::Visit(S);
92 }
Alexey Bataev1b59ab52014-02-27 08:29:12 +000093
Chandler Carruthb7967b92010-10-23 08:21:37 +000094 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +000095 Indent() << "<<unknown stmt type>>\n";
96 }
Chandler Carruthb7967b92010-10-23 08:21:37 +000097 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +000098 OS << "<<unknown expr type>>";
99 }
100 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Mike Stump11289f42009-09-09 15:08:12 +0000101
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +0000102#define ABSTRACT_STMT(CLASS)
Douglas Gregorbe35ce92008-11-14 12:46:07 +0000103#define STMT(CLASS, PARENT) \
Chris Lattner62249a62007-08-21 04:04:25 +0000104 void Visit##CLASS(CLASS *Node);
Alexis Hunt656bb312010-05-05 15:24:00 +0000105#include "clang/AST/StmtNodes.inc"
Chris Lattner71e23ce2006-11-04 20:18:38 +0000106 };
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000107}
108
Chris Lattner71e23ce2006-11-04 20:18:38 +0000109//===----------------------------------------------------------------------===//
110// Stmt printing methods.
111//===----------------------------------------------------------------------===//
112
Chris Lattner073926e2007-05-20 23:04:55 +0000113/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
114/// with no newline after the }.
115void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
116 OS << "{\n";
Aaron Ballmanc7e4e212014-03-17 14:19:37 +0000117 for (auto *I : Node->body())
118 PrintStmt(I);
Mike Stump11289f42009-09-09 15:08:12 +0000119
Chris Lattner073926e2007-05-20 23:04:55 +0000120 Indent() << "}";
121}
122
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000123void StmtPrinter::PrintRawDecl(Decl *D) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000124 D->print(OS, Policy, IndentLevel);
Mike Stump74a76472009-02-10 20:16:46 +0000125}
126
Eli Friedmanf86e5072012-10-16 23:45:15 +0000127void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
Aaron Ballman535bbcc2014-03-14 17:01:24 +0000128 SmallVector<Decl*, 2> Decls(S->decls());
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000129 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
Ted Kremenek15e6b402008-10-06 18:39:36 +0000130}
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000131
132void StmtPrinter::VisitNullStmt(NullStmt *Node) {
133 Indent() << ";\n";
134}
135
136void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Eli Friedman15ea8802009-05-30 00:19:54 +0000137 Indent();
138 PrintRawDeclStmt(Node);
139 OS << ";\n";
Steve Naroff2a8ad182007-05-29 22:59:26 +0000140}
141
Chris Lattner073926e2007-05-20 23:04:55 +0000142void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
143 Indent();
144 PrintRawCompoundStmt(Node);
Chris Lattnerdf3cafb2007-05-31 05:08:56 +0000145 OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000146}
147
Chris Lattner6c0ff132006-11-05 00:19:50 +0000148void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000149 Indent(-1) << "case ";
Chris Lattner6c0ff132006-11-05 00:19:50 +0000150 PrintExpr(Node->getLHS());
151 if (Node->getRHS()) {
152 OS << " ... ";
153 PrintExpr(Node->getRHS());
154 }
155 OS << ":\n";
Mike Stump11289f42009-09-09 15:08:12 +0000156
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000157 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000158}
159
160void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000161 Indent(-1) << "default:\n";
162 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000163}
164
165void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000166 Indent(-1) << Node->getName() << ":\n";
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000167 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000168}
169
Richard Smithc202b282012-04-14 00:33:13 +0000170void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
Aaron Ballmanb06b15a2014-06-06 12:40:24 +0000171 for (const auto *Attr : Node->getAttrs()) {
Tyler Nowickie8b07ed2014-06-13 17:57:25 +0000172 Attr->printPretty(OS, Policy);
Aaron Ballmanb06b15a2014-06-06 12:40:24 +0000173 }
174
Richard Smithc202b282012-04-14 00:33:13 +0000175 PrintStmt(Node->getSubStmt(), 0);
176}
177
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000178void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000179 OS << "if (";
Eli Friedmanf86e5072012-10-16 23:45:15 +0000180 if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
181 PrintRawDeclStmt(DS);
182 else
183 PrintExpr(If->getCond());
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000184 OS << ')';
Mike Stump11289f42009-09-09 15:08:12 +0000185
Chris Lattner073926e2007-05-20 23:04:55 +0000186 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
187 OS << ' ';
188 PrintRawCompoundStmt(CS);
189 OS << (If->getElse() ? ' ' : '\n');
190 } else {
191 OS << '\n';
192 PrintStmt(If->getThen());
193 if (If->getElse()) Indent();
194 }
Mike Stump11289f42009-09-09 15:08:12 +0000195
Chris Lattner073926e2007-05-20 23:04:55 +0000196 if (Stmt *Else = If->getElse()) {
197 OS << "else";
Mike Stump11289f42009-09-09 15:08:12 +0000198
Chris Lattner073926e2007-05-20 23:04:55 +0000199 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
200 OS << ' ';
201 PrintRawCompoundStmt(CS);
202 OS << '\n';
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000203 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
204 OS << ' ';
205 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000206 } else {
207 OS << '\n';
208 PrintStmt(If->getElse());
209 }
Chris Lattner882f7882006-11-04 18:52:07 +0000210 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000211}
212
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000213void StmtPrinter::VisitIfStmt(IfStmt *If) {
214 Indent();
215 PrintRawIfStmt(If);
216}
217
Chris Lattnerf2174b62006-11-04 20:59:27 +0000218void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
219 Indent() << "switch (";
Eli Friedmanf86e5072012-10-16 23:45:15 +0000220 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
221 PrintRawDeclStmt(DS);
222 else
223 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000224 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000225
Chris Lattner073926e2007-05-20 23:04:55 +0000226 // Pretty print compoundstmt bodies (very common).
227 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
228 OS << " ";
229 PrintRawCompoundStmt(CS);
230 OS << "\n";
231 } else {
232 OS << "\n";
233 PrintStmt(Node->getBody());
234 }
Chris Lattnerf2174b62006-11-04 20:59:27 +0000235}
236
Chris Lattner85ed8732006-11-04 20:40:44 +0000237void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
238 Indent() << "while (";
Eli Friedmanf86e5072012-10-16 23:45:15 +0000239 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
240 PrintRawDeclStmt(DS);
241 else
242 PrintExpr(Node->getCond());
Chris Lattner85ed8732006-11-04 20:40:44 +0000243 OS << ")\n";
244 PrintStmt(Node->getBody());
245}
246
247void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000248 Indent() << "do ";
249 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
250 PrintRawCompoundStmt(CS);
251 OS << " ";
252 } else {
253 OS << "\n";
254 PrintStmt(Node->getBody());
255 Indent();
256 }
Mike Stump11289f42009-09-09 15:08:12 +0000257
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000258 OS << "while (";
Chris Lattner85ed8732006-11-04 20:40:44 +0000259 PrintExpr(Node->getCond());
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000260 OS << ");\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000261}
262
Chris Lattner71e23ce2006-11-04 20:18:38 +0000263void StmtPrinter::VisitForStmt(ForStmt *Node) {
264 Indent() << "for (";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000265 if (Node->getInit()) {
266 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000267 PrintRawDeclStmt(DS);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000268 else
269 PrintExpr(cast<Expr>(Node->getInit()));
270 }
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000271 OS << ";";
272 if (Node->getCond()) {
273 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000274 PrintExpr(Node->getCond());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000275 }
276 OS << ";";
277 if (Node->getInc()) {
278 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000279 PrintExpr(Node->getInc());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000280 }
281 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000282
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000283 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
284 PrintRawCompoundStmt(CS);
285 OS << "\n";
286 } else {
287 OS << "\n";
288 PrintStmt(Node->getBody());
289 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000290}
291
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000292void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian83615522008-01-02 22:54:34 +0000293 Indent() << "for (";
294 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000295 PrintRawDeclStmt(DS);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000296 else
297 PrintExpr(cast<Expr>(Node->getElement()));
298 OS << " in ";
299 PrintExpr(Node->getCollection());
300 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000301
Fariborz Jahanian83615522008-01-02 22:54:34 +0000302 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
303 PrintRawCompoundStmt(CS);
304 OS << "\n";
305 } else {
306 OS << "\n";
307 PrintStmt(Node->getBody());
308 }
309}
310
Richard Smith02e85f32011-04-14 22:09:26 +0000311void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
312 Indent() << "for (";
313 PrintingPolicy SubPolicy(Policy);
314 SubPolicy.SuppressInitializers = true;
315 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
316 OS << " : ";
317 PrintExpr(Node->getRangeInit());
318 OS << ") {\n";
319 PrintStmt(Node->getBody());
Ted Kremenek88446602013-12-11 23:44:02 +0000320 Indent() << "}";
321 if (Policy.IncludeNewlines) OS << "\n";
Richard Smith02e85f32011-04-14 22:09:26 +0000322}
323
Douglas Gregordeb4a2be2011-10-25 01:33:02 +0000324void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
325 Indent();
326 if (Node->isIfExists())
327 OS << "__if_exists (";
328 else
329 OS << "__if_not_exists (";
330
331 if (NestedNameSpecifier *Qualifier
332 = Node->getQualifierLoc().getNestedNameSpecifier())
333 Qualifier->print(OS, Policy);
334
335 OS << Node->getNameInfo() << ") ";
336
337 PrintRawCompoundStmt(Node->getSubStmt());
338}
339
Chris Lattner16976d32006-11-05 01:46:01 +0000340void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000341 Indent() << "goto " << Node->getLabel()->getName() << ";";
342 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000343}
344
345void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000346 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000347 PrintExpr(Node->getTarget());
Ted Kremenek88446602013-12-11 23:44:02 +0000348 OS << ";";
349 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000350}
351
352void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000353 Indent() << "continue;";
354 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000355}
356
357void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000358 Indent() << "break;";
359 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000360}
361
362
Chris Lattner882f7882006-11-04 18:52:07 +0000363void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
364 Indent() << "return";
365 if (Node->getRetValue()) {
366 OS << " ";
367 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000368 }
Ted Kremenek88446602013-12-11 23:44:02 +0000369 OS << ";";
370 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000371}
372
Chris Lattner73c56c02007-10-29 04:04:16 +0000373
Chad Rosierde70e0e2012-08-25 00:11:56 +0000374void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
Anders Carlsson660bdd12007-11-23 23:12:25 +0000375 Indent() << "asm ";
Mike Stump11289f42009-09-09 15:08:12 +0000376
Anders Carlsson660bdd12007-11-23 23:12:25 +0000377 if (Node->isVolatile())
378 OS << "volatile ";
Mike Stump11289f42009-09-09 15:08:12 +0000379
Anders Carlsson660bdd12007-11-23 23:12:25 +0000380 OS << "(";
Anders Carlsson81a5a692007-11-20 19:21:03 +0000381 VisitStringLiteral(Node->getAsmString());
Mike Stump11289f42009-09-09 15:08:12 +0000382
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000383 // Outputs
384 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
385 Node->getNumClobbers() != 0)
386 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000387
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000388 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
389 if (i != 0)
390 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000391
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000392 if (!Node->getOutputName(i).empty()) {
393 OS << '[';
394 OS << Node->getOutputName(i);
395 OS << "] ";
396 }
Mike Stump11289f42009-09-09 15:08:12 +0000397
Chris Lattner72bbf172009-03-10 04:59:06 +0000398 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000399 OS << " ";
400 Visit(Node->getOutputExpr(i));
401 }
Mike Stump11289f42009-09-09 15:08:12 +0000402
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000403 // Inputs
404 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
405 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000406
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000407 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
408 if (i != 0)
409 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000410
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000411 if (!Node->getInputName(i).empty()) {
412 OS << '[';
413 OS << Node->getInputName(i);
414 OS << "] ";
415 }
Mike Stump11289f42009-09-09 15:08:12 +0000416
Chris Lattner72bbf172009-03-10 04:59:06 +0000417 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000418 OS << " ";
419 Visit(Node->getInputExpr(i));
420 }
Mike Stump11289f42009-09-09 15:08:12 +0000421
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000422 // Clobbers
423 if (Node->getNumClobbers() != 0)
424 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000425
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000426 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
427 if (i != 0)
428 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000429
Chad Rosierd9fb09a2012-08-27 23:28:41 +0000430 VisitStringLiteral(Node->getClobberStringLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000431 }
Mike Stump11289f42009-09-09 15:08:12 +0000432
Ted Kremenek88446602013-12-11 23:44:02 +0000433 OS << ");";
434 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner73c56c02007-10-29 04:04:16 +0000435}
436
Chad Rosier32503022012-06-11 20:47:18 +0000437void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
438 // FIXME: Implement MS style inline asm statement printer.
Chad Rosierb6f46c12012-08-15 16:53:30 +0000439 Indent() << "__asm ";
440 if (Node->hasBraces())
441 OS << "{\n";
John McCallf413f5e2013-05-03 00:10:13 +0000442 OS << Node->getAsmString() << "\n";
Chad Rosierb6f46c12012-08-15 16:53:30 +0000443 if (Node->hasBraces())
444 Indent() << "}\n";
Chad Rosier32503022012-06-11 20:47:18 +0000445}
446
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000447void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000448 PrintStmt(Node->getCapturedDecl()->getBody());
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000449}
450
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000451void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000452 Indent() << "@try";
453 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
454 PrintRawCompoundStmt(TS);
455 OS << "\n";
456 }
Mike Stump11289f42009-09-09 15:08:12 +0000457
Douglas Gregor96c79492010-04-23 22:50:49 +0000458 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
459 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000460 Indent() << "@catch(";
Steve Naroff371b8fb2009-03-03 19:52:17 +0000461 if (catchStmt->getCatchParamDecl()) {
462 if (Decl *DS = catchStmt->getCatchParamDecl())
463 PrintRawDecl(DS);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000464 }
465 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000466 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
467 PrintRawCompoundStmt(CS);
468 OS << "\n";
469 }
Fariborz Jahanian88157952007-11-02 18:16:07 +0000470 }
Mike Stump11289f42009-09-09 15:08:12 +0000471
472 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
473 Node->getFinallyStmt())) {
Fariborz Jahaniandefbf9a2007-11-07 00:46:42 +0000474 Indent() << "@finally";
475 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian88157952007-11-02 18:16:07 +0000476 OS << "\n";
Mike Stump11289f42009-09-09 15:08:12 +0000477 }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000478}
479
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000480void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000481}
482
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000483void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000484 Indent() << "@catch (...) { /* todo */ } \n";
485}
486
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000487void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000488 Indent() << "@throw";
489 if (Node->getThrowExpr()) {
490 OS << " ";
491 PrintExpr(Node->getThrowExpr());
492 }
493 OS << ";\n";
494}
495
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000496void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000497 Indent() << "@synchronized (";
498 PrintExpr(Node->getSynchExpr());
499 OS << ")";
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000500 PrintRawCompoundStmt(Node->getSynchBody());
501 OS << "\n";
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000502}
503
John McCall31168b02011-06-15 23:02:42 +0000504void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
505 Indent() << "@autoreleasepool";
506 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
507 OS << "\n";
508}
509
Sebastian Redl9b244a82008-12-22 21:35:02 +0000510void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
511 OS << "catch (";
Sebastian Redl54c04d42008-12-22 19:15:10 +0000512 if (Decl *ExDecl = Node->getExceptionDecl())
513 PrintRawDecl(ExDecl);
514 else
515 OS << "...";
516 OS << ") ";
517 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl9b244a82008-12-22 21:35:02 +0000518}
519
520void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
521 Indent();
522 PrintRawCXXCatchStmt(Node);
523 OS << "\n";
524}
525
526void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
527 Indent() << "try ";
528 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump11289f42009-09-09 15:08:12 +0000529 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl9b244a82008-12-22 21:35:02 +0000530 OS << " ";
531 PrintRawCXXCatchStmt(Node->getHandler(i));
532 }
Sebastian Redl54c04d42008-12-22 19:15:10 +0000533 OS << "\n";
534}
535
John Wiegley1c0675e2011-04-28 01:08:34 +0000536void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
537 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
538 PrintRawCompoundStmt(Node->getTryBlock());
539 SEHExceptStmt *E = Node->getExceptHandler();
540 SEHFinallyStmt *F = Node->getFinallyHandler();
541 if(E)
542 PrintRawSEHExceptHandler(E);
543 else {
544 assert(F && "Must have a finally block...");
545 PrintRawSEHFinallyStmt(F);
546 }
547 OS << "\n";
548}
549
550void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
551 OS << "__finally ";
552 PrintRawCompoundStmt(Node->getBlock());
553 OS << "\n";
554}
555
556void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
557 OS << "__except (";
558 VisitExpr(Node->getFilterExpr());
Joao Matos566359c2012-09-04 17:49:35 +0000559 OS << ")\n";
John Wiegley1c0675e2011-04-28 01:08:34 +0000560 PrintRawCompoundStmt(Node->getBlock());
561 OS << "\n";
562}
563
564void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
565 Indent();
566 PrintRawSEHExceptHandler(Node);
567 OS << "\n";
568}
569
570void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
571 Indent();
572 PrintRawSEHFinallyStmt(Node);
573 OS << "\n";
574}
575
Nico Weber9b982072014-07-07 00:12:30 +0000576void StmtPrinter::VisitSEHLeaveStmt(SEHLeaveStmt *Node) {
577 Indent() << "__leave;";
578 if (Policy.IncludeNewlines) OS << "\n";
579}
580
Chris Lattner71e23ce2006-11-04 20:18:38 +0000581//===----------------------------------------------------------------------===//
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000582// OpenMP clauses printing methods
583//===----------------------------------------------------------------------===//
584
585namespace {
586class OMPClausePrinter : public OMPClauseVisitor<OMPClausePrinter> {
587 raw_ostream &OS;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000588 const PrintingPolicy &Policy;
Alexey Bataev756c1962013-09-24 03:17:45 +0000589 /// \brief Process clauses with list of variables.
590 template <typename T>
591 void VisitOMPClauseList(T *Node, char StartSym);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000592public:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000593 OMPClausePrinter(raw_ostream &OS, const PrintingPolicy &Policy)
594 : OS(OS), Policy(Policy) { }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000595#define OPENMP_CLAUSE(Name, Class) \
596 void Visit##Class(Class *S);
597#include "clang/Basic/OpenMPKinds.def"
598};
599
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000600void OMPClausePrinter::VisitOMPIfClause(OMPIfClause *Node) {
601 OS << "if(";
Craig Topper36250ad2014-05-12 05:36:57 +0000602 Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000603 OS << ")";
604}
605
Alexey Bataev3778b602014-07-17 07:32:53 +0000606void OMPClausePrinter::VisitOMPFinalClause(OMPFinalClause *Node) {
607 OS << "final(";
608 Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
609 OS << ")";
610}
611
Alexey Bataev568a8332014-03-06 06:15:19 +0000612void OMPClausePrinter::VisitOMPNumThreadsClause(OMPNumThreadsClause *Node) {
613 OS << "num_threads(";
Craig Topper36250ad2014-05-12 05:36:57 +0000614 Node->getNumThreads()->printPretty(OS, nullptr, Policy, 0);
Alexey Bataev568a8332014-03-06 06:15:19 +0000615 OS << ")";
616}
617
Alexey Bataev62c87d22014-03-21 04:51:18 +0000618void OMPClausePrinter::VisitOMPSafelenClause(OMPSafelenClause *Node) {
619 OS << "safelen(";
Craig Topper36250ad2014-05-12 05:36:57 +0000620 Node->getSafelen()->printPretty(OS, nullptr, Policy, 0);
Alexey Bataev62c87d22014-03-21 04:51:18 +0000621 OS << ")";
622}
623
Alexander Musman8bd31e62014-05-27 15:12:19 +0000624void OMPClausePrinter::VisitOMPCollapseClause(OMPCollapseClause *Node) {
625 OS << "collapse(";
626 Node->getNumForLoops()->printPretty(OS, nullptr, Policy, 0);
627 OS << ")";
628}
629
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000630void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
631 OS << "default("
632 << getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind())
633 << ")";
634}
635
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000636void OMPClausePrinter::VisitOMPProcBindClause(OMPProcBindClause *Node) {
637 OS << "proc_bind("
638 << getOpenMPSimpleClauseTypeName(OMPC_proc_bind, Node->getProcBindKind())
639 << ")";
640}
641
Alexey Bataev56dafe82014-06-20 07:16:17 +0000642void OMPClausePrinter::VisitOMPScheduleClause(OMPScheduleClause *Node) {
643 OS << "schedule("
644 << getOpenMPSimpleClauseTypeName(OMPC_schedule, Node->getScheduleKind());
645 if (Node->getChunkSize()) {
646 OS << ", ";
647 Node->getChunkSize()->printPretty(OS, nullptr, Policy);
648 }
649 OS << ")";
650}
651
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000652void OMPClausePrinter::VisitOMPOrderedClause(OMPOrderedClause *) {
653 OS << "ordered";
654}
655
Alexey Bataev236070f2014-06-20 11:19:47 +0000656void OMPClausePrinter::VisitOMPNowaitClause(OMPNowaitClause *) {
657 OS << "nowait";
658}
659
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000660void OMPClausePrinter::VisitOMPUntiedClause(OMPUntiedClause *) {
661 OS << "untied";
662}
663
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000664void OMPClausePrinter::VisitOMPMergeableClause(OMPMergeableClause *) {
665 OS << "mergeable";
666}
667
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000668void OMPClausePrinter::VisitOMPReadClause(OMPReadClause *) { OS << "read"; }
669
Alexey Bataev756c1962013-09-24 03:17:45 +0000670template<typename T>
671void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
672 for (typename T::varlist_iterator I = Node->varlist_begin(),
673 E = Node->varlist_end();
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000674 I != E; ++I) {
Richard Trieuddd01ce2014-06-09 22:53:25 +0000675 assert(*I && "Expected non-null Stmt");
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000676 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(*I)) {
677 OS << (I == Node->varlist_begin() ? StartSym : ',');
678 cast<NamedDecl>(DRE->getDecl())->printQualifiedName(OS);
679 } else {
680 OS << (I == Node->varlist_begin() ? StartSym : ',');
Craig Topper36250ad2014-05-12 05:36:57 +0000681 (*I)->printPretty(OS, nullptr, Policy, 0);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000682 }
683 }
Alexey Bataev756c1962013-09-24 03:17:45 +0000684}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000685
686void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) {
687 if (!Node->varlist_empty()) {
688 OS << "private";
Alexey Bataev756c1962013-09-24 03:17:45 +0000689 VisitOMPClauseList(Node, '(');
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000690 OS << ")";
691 }
692}
693
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000694void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) {
695 if (!Node->varlist_empty()) {
696 OS << "firstprivate";
697 VisitOMPClauseList(Node, '(');
698 OS << ")";
699 }
700}
701
Alexander Musman1bb328c2014-06-04 13:06:39 +0000702void OMPClausePrinter::VisitOMPLastprivateClause(OMPLastprivateClause *Node) {
703 if (!Node->varlist_empty()) {
704 OS << "lastprivate";
705 VisitOMPClauseList(Node, '(');
706 OS << ")";
707 }
708}
709
Alexey Bataev758e55e2013-09-06 18:03:48 +0000710void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
711 if (!Node->varlist_empty()) {
712 OS << "shared";
Alexey Bataev756c1962013-09-24 03:17:45 +0000713 VisitOMPClauseList(Node, '(');
Alexey Bataev758e55e2013-09-06 18:03:48 +0000714 OS << ")";
715 }
716}
717
Alexey Bataevc5e02582014-06-16 07:08:35 +0000718void OMPClausePrinter::VisitOMPReductionClause(OMPReductionClause *Node) {
719 if (!Node->varlist_empty()) {
720 OS << "reduction(";
721 NestedNameSpecifier *QualifierLoc =
722 Node->getQualifierLoc().getNestedNameSpecifier();
723 OverloadedOperatorKind OOK =
724 Node->getNameInfo().getName().getCXXOverloadedOperator();
725 if (QualifierLoc == nullptr && OOK != OO_None) {
726 // Print reduction identifier in C format
727 OS << getOperatorSpelling(OOK);
728 } else {
729 // Use C++ format
730 if (QualifierLoc != nullptr)
731 QualifierLoc->print(OS, Policy);
732 OS << Node->getNameInfo();
733 }
734 OS << ":";
735 VisitOMPClauseList(Node, ' ');
736 OS << ")";
737 }
738}
739
Alexander Musman8dba6642014-04-22 13:09:42 +0000740void OMPClausePrinter::VisitOMPLinearClause(OMPLinearClause *Node) {
741 if (!Node->varlist_empty()) {
742 OS << "linear";
743 VisitOMPClauseList(Node, '(');
Craig Topper36250ad2014-05-12 05:36:57 +0000744 if (Node->getStep() != nullptr) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000745 OS << ": ";
Craig Topper36250ad2014-05-12 05:36:57 +0000746 Node->getStep()->printPretty(OS, nullptr, Policy, 0);
Alexander Musman8dba6642014-04-22 13:09:42 +0000747 }
748 OS << ")";
749 }
750}
751
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000752void OMPClausePrinter::VisitOMPAlignedClause(OMPAlignedClause *Node) {
753 if (!Node->varlist_empty()) {
754 OS << "aligned";
755 VisitOMPClauseList(Node, '(');
756 if (Node->getAlignment() != nullptr) {
757 OS << ": ";
758 Node->getAlignment()->printPretty(OS, nullptr, Policy, 0);
759 }
760 OS << ")";
761 }
762}
763
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000764void OMPClausePrinter::VisitOMPCopyinClause(OMPCopyinClause *Node) {
765 if (!Node->varlist_empty()) {
766 OS << "copyin";
767 VisitOMPClauseList(Node, '(');
768 OS << ")";
769 }
770}
771
Alexey Bataevbae9a792014-06-27 10:37:06 +0000772void OMPClausePrinter::VisitOMPCopyprivateClause(OMPCopyprivateClause *Node) {
773 if (!Node->varlist_empty()) {
774 OS << "copyprivate";
775 VisitOMPClauseList(Node, '(');
776 OS << ")";
777 }
778}
779
Alexey Bataev6125da92014-07-21 11:26:11 +0000780void OMPClausePrinter::VisitOMPFlushClause(OMPFlushClause *Node) {
781 if (!Node->varlist_empty()) {
782 VisitOMPClauseList(Node, '(');
783 OS << ")";
784 }
785}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000786}
787
788//===----------------------------------------------------------------------===//
789// OpenMP directives printing methods
790//===----------------------------------------------------------------------===//
791
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000792void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000793 OMPClausePrinter Printer(OS, Policy);
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000794 ArrayRef<OMPClause *> Clauses = S->clauses();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000795 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
796 I != E; ++I)
797 if (*I && !(*I)->isImplicit()) {
798 Printer.Visit(*I);
799 OS << ' ';
800 }
801 OS << "\n";
Alexey Bataev68446b72014-07-18 07:47:19 +0000802 if (S->hasAssociatedStmt() && S->getAssociatedStmt()) {
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000803 assert(isa<CapturedStmt>(S->getAssociatedStmt()) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000804 "Expected captured statement!");
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000805 Stmt *CS = cast<CapturedStmt>(S->getAssociatedStmt())->getCapturedStmt();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000806 PrintStmt(CS);
807 }
808}
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000809
810void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
811 Indent() << "#pragma omp parallel ";
812 PrintOMPExecutableDirective(Node);
813}
814
815void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
816 Indent() << "#pragma omp simd ";
817 PrintOMPExecutableDirective(Node);
818}
819
Alexey Bataevf29276e2014-06-18 04:14:57 +0000820void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) {
821 Indent() << "#pragma omp for ";
822 PrintOMPExecutableDirective(Node);
823}
824
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000825void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) {
826 Indent() << "#pragma omp sections ";
827 PrintOMPExecutableDirective(Node);
828}
829
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000830void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) {
831 Indent() << "#pragma omp section";
832 PrintOMPExecutableDirective(Node);
833}
834
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000835void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) {
836 Indent() << "#pragma omp single ";
837 PrintOMPExecutableDirective(Node);
838}
839
Alexander Musman80c22892014-07-17 08:54:58 +0000840void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) {
841 Indent() << "#pragma omp master";
842 PrintOMPExecutableDirective(Node);
843}
844
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000845void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) {
846 Indent() << "#pragma omp critical";
847 if (Node->getDirectiveName().getName()) {
848 OS << " (";
849 Node->getDirectiveName().printName(OS);
850 OS << ")";
851 }
852 PrintOMPExecutableDirective(Node);
853}
854
Alexey Bataev4acb8592014-07-07 13:01:15 +0000855void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) {
856 Indent() << "#pragma omp parallel for ";
857 PrintOMPExecutableDirective(Node);
858}
859
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000860void StmtPrinter::VisitOMPParallelSectionsDirective(
861 OMPParallelSectionsDirective *Node) {
862 Indent() << "#pragma omp parallel sections ";
863 PrintOMPExecutableDirective(Node);
864}
865
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000866void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) {
867 Indent() << "#pragma omp task ";
868 PrintOMPExecutableDirective(Node);
869}
870
Alexey Bataev68446b72014-07-18 07:47:19 +0000871void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) {
872 Indent() << "#pragma omp taskyield";
873 PrintOMPExecutableDirective(Node);
874}
875
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000876void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) {
877 Indent() << "#pragma omp barrier";
878 PrintOMPExecutableDirective(Node);
879}
880
Alexey Bataev2df347a2014-07-18 10:17:07 +0000881void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) {
882 Indent() << "#pragma omp taskwait";
883 PrintOMPExecutableDirective(Node);
884}
885
Alexey Bataev6125da92014-07-21 11:26:11 +0000886void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) {
887 Indent() << "#pragma omp flush ";
888 PrintOMPExecutableDirective(Node);
889}
890
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000891void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) {
892 Indent() << "#pragma omp ordered";
893 PrintOMPExecutableDirective(Node);
894}
895
Alexey Bataev0162e452014-07-22 10:10:35 +0000896void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) {
897 Indent() << "#pragma omp atomic ";
898 PrintOMPExecutableDirective(Node);
899}
900
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000901//===----------------------------------------------------------------------===//
Chris Lattner71e23ce2006-11-04 20:18:38 +0000902// Expr printing methods.
903//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000904
Chris Lattner882f7882006-11-04 18:52:07 +0000905void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000906 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
907 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000908 if (Node->hasTemplateKeyword())
909 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000910 OS << Node->getNameInfo();
John McCallb3774b52010-08-19 23:49:38 +0000911 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +0000912 TemplateSpecializationType::PrintTemplateArgumentList(
913 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000914}
915
John McCall8cd78132009-11-19 22:55:06 +0000916void StmtPrinter::VisitDependentScopeDeclRefExpr(
917 DependentScopeDeclRefExpr *Node) {
Axel Naumann20b27862011-01-24 15:44:00 +0000918 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
919 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000920 if (Node->hasTemplateKeyword())
921 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000922 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000923 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +0000924 TemplateSpecializationType::PrintTemplateArgumentList(
925 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregor90a1a652009-03-19 17:26:29 +0000926}
927
John McCalld14a8642009-11-21 08:51:07 +0000928void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregora727cb92009-06-30 22:34:41 +0000929 if (Node->getQualifier())
930 Node->getQualifier()->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000931 if (Node->hasTemplateKeyword())
932 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000933 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000934 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +0000935 TemplateSpecializationType::PrintTemplateArgumentList(
936 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregora727cb92009-06-30 22:34:41 +0000937}
938
Steve Naroffe46504b2007-11-12 14:29:37 +0000939void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000940 if (Node->getBase()) {
941 PrintExpr(Node->getBase());
942 OS << (Node->isArrow() ? "->" : ".");
943 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000944 OS << *Node->getDecl();
Steve Naroffe46504b2007-11-12 14:29:37 +0000945}
946
Steve Naroffec944032008-05-30 00:40:33 +0000947void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000948 if (Node->isSuperReceiver())
949 OS << "super.";
Richard Trieu6d92e502014-02-06 23:26:23 +0000950 else if (Node->isObjectReceiver() && Node->getBase()) {
Steve Naroffec944032008-05-30 00:40:33 +0000951 PrintExpr(Node->getBase());
952 OS << ".";
Richard Trieu6d92e502014-02-06 23:26:23 +0000953 } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
954 OS << Node->getClassReceiver()->getName() << ".";
Steve Naroffec944032008-05-30 00:40:33 +0000955 }
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000956
John McCallb7bd14f2010-12-02 01:19:52 +0000957 if (Node->isImplicitProperty())
Aaron Ballmanb190f972014-01-03 17:59:55 +0000958 Node->getImplicitPropertyGetter()->getSelector().print(OS);
John McCallb7bd14f2010-12-02 01:19:52 +0000959 else
960 OS << Node->getExplicitProperty()->getName();
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000961}
962
Ted Kremeneke65b0862012-03-06 20:05:56 +0000963void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
964
965 PrintExpr(Node->getBaseExpr());
966 OS << "[";
967 PrintExpr(Node->getKeyExpr());
968 OS << "]";
969}
970
Chris Lattner6307f192008-08-10 01:53:14 +0000971void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson625bfc82007-07-21 05:21:51 +0000972 switch (Node->getIdentType()) {
973 default:
David Blaikie83d382b2011-09-23 05:06:16 +0000974 llvm_unreachable("unknown case");
Chris Lattner6307f192008-08-10 01:53:14 +0000975 case PredefinedExpr::Func:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000976 OS << "__func__";
977 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000978 case PredefinedExpr::Function:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000979 OS << "__FUNCTION__";
980 break;
David Majnemerbed356a2013-11-06 23:31:56 +0000981 case PredefinedExpr::FuncDName:
982 OS << "__FUNCDNAME__";
983 break;
Reid Kleckner52eddda2014-04-08 18:13:24 +0000984 case PredefinedExpr::FuncSig:
985 OS << "__FUNCSIG__";
986 break;
Nico Weber3a691a32012-06-23 02:07:59 +0000987 case PredefinedExpr::LFunction:
988 OS << "L__FUNCTION__";
989 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000990 case PredefinedExpr::PrettyFunction:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000991 OS << "__PRETTY_FUNCTION__";
992 break;
993 }
994}
995
Steve Naroffae4143e2007-04-26 20:39:23 +0000996void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000997 unsigned value = Node->getValue();
Douglas Gregorfb65e592011-07-27 05:40:30 +0000998
999 switch (Node->getKind()) {
1000 case CharacterLiteral::Ascii: break; // no prefix.
1001 case CharacterLiteral::Wide: OS << 'L'; break;
1002 case CharacterLiteral::UTF16: OS << 'u'; break;
1003 case CharacterLiteral::UTF32: OS << 'U'; break;
1004 }
1005
Chris Lattner666115c2007-07-13 23:58:20 +00001006 switch (value) {
1007 case '\\':
1008 OS << "'\\\\'";
1009 break;
1010 case '\'':
1011 OS << "'\\''";
1012 break;
1013 case '\a':
1014 // TODO: K&R: the meaning of '\\a' is different in traditional C
1015 OS << "'\\a'";
1016 break;
1017 case '\b':
1018 OS << "'\\b'";
1019 break;
1020 // Nonstandard escape sequence.
1021 /*case '\e':
1022 OS << "'\\e'";
1023 break;*/
1024 case '\f':
1025 OS << "'\\f'";
1026 break;
1027 case '\n':
1028 OS << "'\\n'";
1029 break;
1030 case '\r':
1031 OS << "'\\r'";
1032 break;
1033 case '\t':
1034 OS << "'\\t'";
1035 break;
1036 case '\v':
1037 OS << "'\\v'";
1038 break;
1039 default:
Jordan Rose00d1b592013-02-08 22:30:27 +00001040 if (value < 256 && isPrintable((unsigned char)value))
Chris Lattner666115c2007-07-13 23:58:20 +00001041 OS << "'" << (char)value << "'";
Jordan Rose00d1b592013-02-08 22:30:27 +00001042 else if (value < 256)
1043 OS << "'\\x" << llvm::format("%02x", value) << "'";
1044 else if (value <= 0xFFFF)
1045 OS << "'\\u" << llvm::format("%04x", value) << "'";
1046 else
1047 OS << "'\\U" << llvm::format("%08x", value) << "'";
Chris Lattner6e9d9b32007-07-13 05:18:11 +00001048 }
Steve Naroffae4143e2007-04-26 20:39:23 +00001049}
1050
Steve Naroffdf7855b2007-02-21 23:46:25 +00001051void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +00001052 bool isSigned = Node->getType()->isSignedIntegerType();
1053 OS << Node->getValue().toString(10, isSigned);
Mike Stump11289f42009-09-09 15:08:12 +00001054
Chris Lattner06430412007-05-21 05:45:03 +00001055 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall9dd450b2009-09-21 23:43:11 +00001056 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
David Blaikie83d382b2011-09-23 05:06:16 +00001057 default: llvm_unreachable("Unexpected type for integer literal!");
David Majnemer65a407c2014-06-21 18:46:07 +00001058 case BuiltinType::SChar: OS << "i8"; break;
1059 case BuiltinType::UChar: OS << "Ui8"; break;
1060 case BuiltinType::Short: OS << "i16"; break;
1061 case BuiltinType::UShort: OS << "Ui16"; break;
Chris Lattner06430412007-05-21 05:45:03 +00001062 case BuiltinType::Int: break; // no suffix.
1063 case BuiltinType::UInt: OS << 'U'; break;
1064 case BuiltinType::Long: OS << 'L'; break;
1065 case BuiltinType::ULong: OS << "UL"; break;
1066 case BuiltinType::LongLong: OS << "LL"; break;
1067 case BuiltinType::ULongLong: OS << "ULL"; break;
Richard Trieu8b626ba2011-11-07 18:40:31 +00001068 case BuiltinType::Int128: OS << "i128"; break;
1069 case BuiltinType::UInt128: OS << "Ui128"; break;
Chris Lattner06430412007-05-21 05:45:03 +00001070 }
Chris Lattner882f7882006-11-04 18:52:07 +00001071}
Benjamin Kramer8a526762012-09-20 14:07:17 +00001072
1073static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
1074 bool PrintSuffix) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001075 SmallString<16> Str;
Eli Friedman53392062011-10-05 20:32:03 +00001076 Node->getValue().toString(Str);
1077 OS << Str;
Benjamin Kramer8a526762012-09-20 14:07:17 +00001078 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
1079 OS << '.'; // Trailing dot in order to separate from ints.
1080
1081 if (!PrintSuffix)
1082 return;
1083
1084 // Emit suffixes. Float literals are always a builtin float type.
1085 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
1086 default: llvm_unreachable("Unexpected type for float literal!");
1087 case BuiltinType::Half: break; // FIXME: suffix?
1088 case BuiltinType::Double: break; // no suffix.
1089 case BuiltinType::Float: OS << 'F'; break;
1090 case BuiltinType::LongDouble: OS << 'L'; break;
1091 }
1092}
1093
1094void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
1095 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
Chris Lattner882f7882006-11-04 18:52:07 +00001096}
Chris Lattner1c20a172007-08-26 03:42:43 +00001097
1098void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
1099 PrintExpr(Node->getSubExpr());
1100 OS << "i";
1101}
1102
Steve Naroffdf7855b2007-02-21 23:46:25 +00001103void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Richard Trieudc355912012-06-13 20:25:24 +00001104 Str->outputString(OS);
Chris Lattner882f7882006-11-04 18:52:07 +00001105}
1106void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
1107 OS << "(";
1108 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +00001109 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +00001110}
1111void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +00001112 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +00001113 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump11289f42009-09-09 15:08:12 +00001114
Eli Friedman1cf25362009-06-14 22:39:26 +00001115 // Print a space if this is an "identifier operator" like __real, or if
1116 // it might be concatenated incorrectly like '+'.
Chris Lattnere5b60442007-08-23 21:46:40 +00001117 switch (Node->getOpcode()) {
1118 default: break;
John McCalle3027922010-08-25 11:45:40 +00001119 case UO_Real:
1120 case UO_Imag:
1121 case UO_Extension:
Chris Lattnere5b60442007-08-23 21:46:40 +00001122 OS << ' ';
1123 break;
John McCalle3027922010-08-25 11:45:40 +00001124 case UO_Plus:
1125 case UO_Minus:
Eli Friedman1cf25362009-06-14 22:39:26 +00001126 if (isa<UnaryOperator>(Node->getSubExpr()))
1127 OS << ' ';
1128 break;
Chris Lattnere5b60442007-08-23 21:46:40 +00001129 }
1130 }
Chris Lattner882f7882006-11-04 18:52:07 +00001131 PrintExpr(Node->getSubExpr());
Mike Stump11289f42009-09-09 15:08:12 +00001132
Chris Lattner15768702006-11-05 23:54:51 +00001133 if (Node->isPostfix())
1134 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +00001135}
Chris Lattner98dbf0a2007-08-30 17:59:59 +00001136
Douglas Gregor882211c2010-04-28 22:16:22 +00001137void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
1138 OS << "__builtin_offsetof(";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001139 Node->getTypeSourceInfo()->getType().print(OS, Policy);
1140 OS << ", ";
Douglas Gregor882211c2010-04-28 22:16:22 +00001141 bool PrintedSomething = false;
1142 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
1143 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
1144 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
1145 // Array node
1146 OS << "[";
1147 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
1148 OS << "]";
1149 PrintedSomething = true;
1150 continue;
1151 }
Douglas Gregord1702062010-04-29 00:18:15 +00001152
1153 // Skip implicit base indirections.
1154 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
1155 continue;
1156
Douglas Gregor882211c2010-04-28 22:16:22 +00001157 // Field or identifier node.
1158 IdentifierInfo *Id = ON.getFieldName();
1159 if (!Id)
1160 continue;
Alexis Hunta8136cc2010-05-05 15:23:54 +00001161
Douglas Gregor882211c2010-04-28 22:16:22 +00001162 if (PrintedSomething)
1163 OS << ".";
1164 else
1165 PrintedSomething = true;
Alexis Hunta8136cc2010-05-05 15:23:54 +00001166 OS << Id->getName();
Douglas Gregor882211c2010-04-28 22:16:22 +00001167 }
1168 OS << ")";
1169}
1170
Peter Collingbournee190dee2011-03-11 19:24:49 +00001171void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
1172 switch(Node->getKind()) {
1173 case UETT_SizeOf:
1174 OS << "sizeof";
1175 break;
1176 case UETT_AlignOf:
Jordan Rose58d54722012-06-30 21:33:57 +00001177 if (Policy.LangOpts.CPlusPlus)
1178 OS << "alignof";
1179 else if (Policy.LangOpts.C11)
1180 OS << "_Alignof";
1181 else
1182 OS << "__alignof";
Peter Collingbournee190dee2011-03-11 19:24:49 +00001183 break;
1184 case UETT_VecStep:
1185 OS << "vec_step";
1186 break;
1187 }
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001188 if (Node->isArgumentType()) {
1189 OS << '(';
1190 Node->getArgumentType().print(OS, Policy);
1191 OS << ')';
1192 } else {
Sebastian Redl6f282892008-11-11 17:56:53 +00001193 OS << " ";
1194 PrintExpr(Node->getArgumentExpr());
1195 }
Chris Lattner882f7882006-11-04 18:52:07 +00001196}
Peter Collingbourne91147592011-04-15 00:35:48 +00001197
1198void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
1199 OS << "_Generic(";
1200 PrintExpr(Node->getControllingExpr());
1201 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
1202 OS << ", ";
1203 QualType T = Node->getAssocType(i);
1204 if (T.isNull())
1205 OS << "default";
1206 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001207 T.print(OS, Policy);
Peter Collingbourne91147592011-04-15 00:35:48 +00001208 OS << ": ";
1209 PrintExpr(Node->getAssocExpr(i));
1210 }
1211 OS << ")";
1212}
1213
Chris Lattner882f7882006-11-04 18:52:07 +00001214void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +00001215 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +00001216 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +00001217 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +00001218 OS << "]";
1219}
1220
Peter Collingbourne4b279a02011-02-08 21:17:54 +00001221void StmtPrinter::PrintCallArgs(CallExpr *Call) {
Chris Lattner882f7882006-11-04 18:52:07 +00001222 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001223 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1224 // Don't print any defaulted arguments
1225 break;
1226 }
1227
Chris Lattner882f7882006-11-04 18:52:07 +00001228 if (i) OS << ", ";
1229 PrintExpr(Call->getArg(i));
1230 }
Peter Collingbourne4b279a02011-02-08 21:17:54 +00001231}
1232
1233void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1234 PrintExpr(Call->getCallee());
1235 OS << "(";
1236 PrintCallArgs(Call);
Chris Lattner882f7882006-11-04 18:52:07 +00001237 OS << ")";
1238}
1239void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregore4b414c2009-01-08 22:45:41 +00001240 // FIXME: Suppress printing implicit bases (like "this")
1241 PrintExpr(Node->getBase());
David Blaikie8fab8e52012-11-12 19:12:12 +00001242
1243 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
David Blaikie6bffd6f2012-11-12 19:32:32 +00001244 FieldDecl *ParentDecl = ParentMember
Craig Topper36250ad2014-05-12 05:36:57 +00001245 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : nullptr;
David Blaikie8fab8e52012-11-12 19:12:12 +00001246
David Blaikie6bffd6f2012-11-12 19:32:32 +00001247 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
David Blaikie8fab8e52012-11-12 19:12:12 +00001248 OS << (Node->isArrow() ? "->" : ".");
David Blaikie8fab8e52012-11-12 19:12:12 +00001249
Fariborz Jahanian2990c022010-01-11 21:17:32 +00001250 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1251 if (FD->isAnonymousStructOrUnion())
1252 return;
David Blaikie8fab8e52012-11-12 19:12:12 +00001253
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001254 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1255 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001256 if (Node->hasTemplateKeyword())
1257 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001258 OS << Node->getMemberNameInfo();
John McCallb3774b52010-08-19 23:49:38 +00001259 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +00001260 TemplateSpecializationType::PrintTemplateArgumentList(
1261 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001262}
Steve Naroffe87026a2009-07-24 17:54:45 +00001263void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1264 PrintExpr(Node->getBase());
1265 OS << (Node->isArrow() ? "->isa" : ".isa");
1266}
1267
Nate Begemance4d7fc2008-04-18 23:10:10 +00001268void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +00001269 PrintExpr(Node->getBase());
1270 OS << ".";
1271 OS << Node->getAccessor().getName();
1272}
Douglas Gregorf19b2312008-10-28 15:36:24 +00001273void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001274 OS << '(';
1275 Node->getTypeAsWritten().print(OS, Policy);
1276 OS << ')';
Chris Lattner882f7882006-11-04 18:52:07 +00001277 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001278}
Steve Naroff57eb2c52007-07-19 21:32:11 +00001279void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001280 OS << '(';
1281 Node->getType().print(OS, Policy);
1282 OS << ')';
Steve Naroff57eb2c52007-07-19 21:32:11 +00001283 PrintExpr(Node->getInitializer());
1284}
Steve Naroff7a5af782007-07-13 16:58:59 +00001285void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Alp Toker028ed912013-12-06 17:56:43 +00001286 // No need to print anything, simply forward to the subexpression.
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001287 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +00001288}
Chris Lattner882f7882006-11-04 18:52:07 +00001289void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1290 PrintExpr(Node->getLHS());
1291 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1292 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001293}
Chris Lattner86928112007-08-25 02:00:02 +00001294void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1295 PrintExpr(Node->getLHS());
1296 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1297 PrintExpr(Node->getRHS());
1298}
Chris Lattner882f7882006-11-04 18:52:07 +00001299void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1300 PrintExpr(Node->getCond());
John McCallc07a0c72011-02-17 10:25:35 +00001301 OS << " ? ";
1302 PrintExpr(Node->getLHS());
1303 OS << " : ";
Chris Lattner882f7882006-11-04 18:52:07 +00001304 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001305}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001306
Chris Lattnereefa10e2007-05-28 06:56:27 +00001307// GNU extensions.
1308
John McCallc07a0c72011-02-17 10:25:35 +00001309void
1310StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1311 PrintExpr(Node->getCommon());
1312 OS << " ?: ";
1313 PrintExpr(Node->getFalseExpr());
1314}
Chris Lattnerd268a7a2007-08-03 17:31:20 +00001315void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +00001316 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +00001317}
1318
Chris Lattner366727f2007-07-24 16:58:17 +00001319void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1320 OS << "(";
1321 PrintRawCompoundStmt(E->getSubStmt());
1322 OS << ")";
1323}
1324
Steve Naroff9efdabc2007-08-03 21:21:27 +00001325void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1326 OS << "__builtin_choose_expr(";
1327 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +00001328 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +00001329 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +00001330 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +00001331 PrintExpr(Node->getRHS());
1332 OS << ")";
1333}
Chris Lattner366727f2007-07-24 16:58:17 +00001334
Douglas Gregor3be4b122008-11-29 04:51:27 +00001335void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1336 OS << "__null";
1337}
1338
Eli Friedmana1b4ed82008-05-14 19:38:39 +00001339void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1340 OS << "__builtin_shufflevector(";
1341 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1342 if (i) OS << ", ";
1343 PrintExpr(Node->getExpr(i));
1344 }
1345 OS << ")";
1346}
1347
Hal Finkelc4d7c822013-09-18 03:29:45 +00001348void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1349 OS << "__builtin_convertvector(";
1350 PrintExpr(Node->getSrcExpr());
1351 OS << ", ";
1352 Node->getType().print(OS, Policy);
1353 OS << ")";
1354}
1355
Anders Carlsson4692db02007-08-31 04:56:16 +00001356void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001357 if (Node->getSyntacticForm()) {
1358 Visit(Node->getSyntacticForm());
1359 return;
1360 }
1361
Anders Carlsson4692db02007-08-31 04:56:16 +00001362 OS << "{ ";
1363 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1364 if (i) OS << ", ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001365 if (Node->getInit(i))
1366 PrintExpr(Node->getInit(i));
1367 else
1368 OS << "0";
Anders Carlsson4692db02007-08-31 04:56:16 +00001369 }
1370 OS << " }";
1371}
1372
Nate Begeman5ec4b312009-08-10 23:49:36 +00001373void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1374 OS << "( ";
1375 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1376 if (i) OS << ", ";
1377 PrintExpr(Node->getExpr(i));
1378 }
1379 OS << " )";
1380}
1381
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001382void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001383 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
1384 DEnd = Node->designators_end();
1385 D != DEnd; ++D) {
1386 if (D->isFieldDesignator()) {
Serge Pavloveb57aa62014-06-20 17:08:28 +00001387 if (D->getDotLoc().isInvalid()) {
1388 if (IdentifierInfo *II = D->getFieldName())
1389 OS << II->getName() << ":";
1390 } else {
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001391 OS << "." << D->getFieldName()->getName();
Serge Pavloveb57aa62014-06-20 17:08:28 +00001392 }
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001393 } else {
1394 OS << "[";
1395 if (D->isArrayDesignator()) {
1396 PrintExpr(Node->getArrayIndex(*D));
1397 } else {
1398 PrintExpr(Node->getArrayRangeStart(*D));
1399 OS << " ... ";
Mike Stump11289f42009-09-09 15:08:12 +00001400 PrintExpr(Node->getArrayRangeEnd(*D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001401 }
1402 OS << "]";
1403 }
1404 }
1405
1406 OS << " = ";
1407 PrintExpr(Node->getInit());
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001408}
1409
Douglas Gregor0202cb42009-01-29 17:44:32 +00001410void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001411 if (Policy.LangOpts.CPlusPlus) {
1412 OS << "/*implicit*/";
1413 Node->getType().print(OS, Policy);
1414 OS << "()";
1415 } else {
1416 OS << "/*implicit*/(";
1417 Node->getType().print(OS, Policy);
1418 OS << ')';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001419 if (Node->getType()->isRecordType())
1420 OS << "{}";
1421 else
1422 OS << 0;
1423 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001424}
1425
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001426void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman79635842009-05-30 04:20:30 +00001427 OS << "__builtin_va_arg(";
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001428 PrintExpr(Node->getSubExpr());
1429 OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001430 Node->getType().print(OS, Policy);
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001431 OS << ")";
1432}
1433
John McCallfe96e0b2011-11-06 09:01:30 +00001434void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1435 PrintExpr(Node->getSyntacticForm());
1436}
1437
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001438void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
Craig Topper36250ad2014-05-12 05:36:57 +00001439 const char *Name = nullptr;
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001440 switch (Node->getOp()) {
Richard Smithfeea8832012-04-12 05:08:17 +00001441#define BUILTIN(ID, TYPE, ATTRS)
1442#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1443 case AtomicExpr::AO ## ID: \
1444 Name = #ID "("; \
1445 break;
1446#include "clang/Basic/Builtins.def"
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001447 }
1448 OS << Name;
Richard Smithfeea8832012-04-12 05:08:17 +00001449
1450 // AtomicExpr stores its subexpressions in a permuted order.
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001451 PrintExpr(Node->getPtr());
Richard Smithfeea8832012-04-12 05:08:17 +00001452 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1453 Node->getOp() != AtomicExpr::AO__atomic_load_n) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001454 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001455 PrintExpr(Node->getVal1());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001456 }
Richard Smithfeea8832012-04-12 05:08:17 +00001457 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1458 Node->isCmpXChg()) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001459 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001460 PrintExpr(Node->getVal2());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001461 }
Richard Smithfeea8832012-04-12 05:08:17 +00001462 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1463 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
Richard Smithfeea8832012-04-12 05:08:17 +00001464 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001465 PrintExpr(Node->getWeak());
Richard Smithfeea8832012-04-12 05:08:17 +00001466 }
Richard Smithdf6bee82013-05-01 19:02:43 +00001467 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) {
1468 OS << ", ";
David Chisnallfa35df62012-01-16 17:27:18 +00001469 PrintExpr(Node->getOrder());
Richard Smithdf6bee82013-05-01 19:02:43 +00001470 }
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001471 if (Node->isCmpXChg()) {
1472 OS << ", ";
1473 PrintExpr(Node->getOrderFail());
1474 }
1475 OS << ")";
1476}
1477
Chris Lattnereefa10e2007-05-28 06:56:27 +00001478// C++
Douglas Gregor993603d2008-11-14 16:09:21 +00001479void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1480 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1481 "",
1482#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1483 Spelling,
1484#include "clang/Basic/OperatorKinds.def"
1485 };
1486
1487 OverloadedOperatorKind Kind = Node->getOperator();
1488 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1489 if (Node->getNumArgs() == 1) {
1490 OS << OpStrings[Kind] << ' ';
1491 PrintExpr(Node->getArg(0));
1492 } else {
1493 PrintExpr(Node->getArg(0));
1494 OS << ' ' << OpStrings[Kind];
1495 }
Eli Friedman8e236422012-10-12 22:45:14 +00001496 } else if (Kind == OO_Arrow) {
1497 PrintExpr(Node->getArg(0));
Douglas Gregor993603d2008-11-14 16:09:21 +00001498 } else if (Kind == OO_Call) {
1499 PrintExpr(Node->getArg(0));
1500 OS << '(';
1501 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1502 if (ArgIdx > 1)
1503 OS << ", ";
1504 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1505 PrintExpr(Node->getArg(ArgIdx));
1506 }
1507 OS << ')';
1508 } else if (Kind == OO_Subscript) {
1509 PrintExpr(Node->getArg(0));
1510 OS << '[';
1511 PrintExpr(Node->getArg(1));
1512 OS << ']';
1513 } else if (Node->getNumArgs() == 1) {
1514 OS << OpStrings[Kind] << ' ';
1515 PrintExpr(Node->getArg(0));
1516 } else if (Node->getNumArgs() == 2) {
1517 PrintExpr(Node->getArg(0));
1518 OS << ' ' << OpStrings[Kind] << ' ';
1519 PrintExpr(Node->getArg(1));
1520 } else {
David Blaikie83d382b2011-09-23 05:06:16 +00001521 llvm_unreachable("unknown overloaded operator");
Douglas Gregor993603d2008-11-14 16:09:21 +00001522 }
1523}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001524
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001525void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
Benjamin Kramer00e8a192014-02-25 18:03:55 +00001526 // If we have a conversion operator call only print the argument.
1527 CXXMethodDecl *MD = Node->getMethodDecl();
1528 if (MD && isa<CXXConversionDecl>(MD)) {
1529 PrintExpr(Node->getImplicitObjectArgument());
1530 return;
1531 }
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001532 VisitCallExpr(cast<CallExpr>(Node));
1533}
1534
Peter Collingbourne41f85462011-02-09 21:07:24 +00001535void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1536 PrintExpr(Node->getCallee());
1537 OS << "<<<";
1538 PrintCallArgs(Node->getConfig());
1539 OS << ">>>(";
1540 PrintCallArgs(Node);
1541 OS << ")";
1542}
1543
Douglas Gregore200adc2008-10-27 19:41:14 +00001544void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1545 OS << Node->getCastName() << '<';
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001546 Node->getTypeAsWritten().print(OS, Policy);
1547 OS << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +00001548 PrintExpr(Node->getSubExpr());
1549 OS << ")";
1550}
1551
Douglas Gregore200adc2008-10-27 19:41:14 +00001552void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1553 VisitCXXNamedCastExpr(Node);
1554}
1555
1556void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1557 VisitCXXNamedCastExpr(Node);
1558}
1559
1560void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1561 VisitCXXNamedCastExpr(Node);
1562}
1563
1564void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1565 VisitCXXNamedCastExpr(Node);
1566}
1567
Sebastian Redlc4704762008-11-11 11:37:55 +00001568void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1569 OS << "typeid(";
1570 if (Node->isTypeOperand()) {
David Majnemer143c55e2013-09-27 07:04:31 +00001571 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
Sebastian Redlc4704762008-11-11 11:37:55 +00001572 } else {
1573 PrintExpr(Node->getExprOperand());
1574 }
1575 OS << ")";
1576}
1577
Francois Pichet9f4f2072010-09-08 12:20:18 +00001578void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1579 OS << "__uuidof(";
1580 if (Node->isTypeOperand()) {
David Majnemer143c55e2013-09-27 07:04:31 +00001581 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
Francois Pichet9f4f2072010-09-08 12:20:18 +00001582 } else {
1583 PrintExpr(Node->getExprOperand());
1584 }
1585 OS << ")";
1586}
1587
John McCall5e77d762013-04-16 07:28:30 +00001588void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1589 PrintExpr(Node->getBaseExpr());
1590 if (Node->isArrow())
1591 OS << "->";
1592 else
1593 OS << ".";
1594 if (NestedNameSpecifier *Qualifier =
1595 Node->getQualifierLoc().getNestedNameSpecifier())
1596 Qualifier->print(OS, Policy);
1597 OS << Node->getPropertyDecl()->getDeclName();
1598}
1599
Richard Smithc67fdd42012-03-07 08:35:16 +00001600void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1601 switch (Node->getLiteralOperatorKind()) {
1602 case UserDefinedLiteral::LOK_Raw:
Richard Smith29e95952012-03-09 10:10:02 +00001603 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
Richard Smithc67fdd42012-03-07 08:35:16 +00001604 break;
1605 case UserDefinedLiteral::LOK_Template: {
Richard Smith29e95952012-03-09 10:10:02 +00001606 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1607 const TemplateArgumentList *Args =
1608 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1609 assert(Args);
1610 const TemplateArgument &Pack = Args->get(0);
Aaron Ballman2a89e852014-07-15 21:32:31 +00001611 for (const auto &P : Pack.pack_elements()) {
1612 char C = (char)P.getAsIntegral().getZExtValue();
Richard Smithc67fdd42012-03-07 08:35:16 +00001613 OS << C;
1614 }
1615 break;
1616 }
Richard Smith75025ba2012-03-08 09:02:38 +00001617 case UserDefinedLiteral::LOK_Integer: {
1618 // Print integer literal without suffix.
1619 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1620 OS << Int->getValue().toString(10, /*isSigned*/false);
1621 break;
1622 }
Benjamin Kramer8a526762012-09-20 14:07:17 +00001623 case UserDefinedLiteral::LOK_Floating: {
1624 // Print floating literal without suffix.
1625 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1626 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1627 break;
1628 }
Richard Smithc67fdd42012-03-07 08:35:16 +00001629 case UserDefinedLiteral::LOK_String:
1630 case UserDefinedLiteral::LOK_Character:
1631 PrintExpr(Node->getCookedLiteral());
1632 break;
1633 }
1634 OS << Node->getUDSuffix()->getName();
1635}
1636
Chris Lattnereefa10e2007-05-28 06:56:27 +00001637void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1638 OS << (Node->getValue() ? "true" : "false");
1639}
1640
Sebastian Redl576fd422009-05-10 18:38:11 +00001641void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1642 OS << "nullptr";
1643}
1644
Douglas Gregor97a9c812008-11-04 14:32:21 +00001645void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1646 OS << "this";
1647}
1648
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001649void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
Craig Topper36250ad2014-05-12 05:36:57 +00001650 if (!Node->getSubExpr())
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001651 OS << "throw";
1652 else {
1653 OS << "throw ";
1654 PrintExpr(Node->getSubExpr());
1655 }
1656}
1657
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001658void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
Richard Smith852c9db2013-04-20 22:23:05 +00001659 // Nothing to print: we picked up the default argument.
1660}
1661
1662void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1663 // Nothing to print: we picked up the default initializer.
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001664}
1665
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001666void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001667 Node->getType().print(OS, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001668 OS << "(";
1669 PrintExpr(Node->getSubExpr());
1670 OS << ")";
1671}
1672
Anders Carlsson993a4b32009-05-30 20:03:25 +00001673void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1674 PrintExpr(Node->getSubExpr());
1675}
1676
Douglas Gregordd04d332009-01-16 18:33:17 +00001677void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001678 Node->getType().print(OS, Policy);
Douglas Gregordd04d332009-01-16 18:33:17 +00001679 OS << "(";
1680 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001681 ArgEnd = Node->arg_end();
Douglas Gregordd04d332009-01-16 18:33:17 +00001682 Arg != ArgEnd; ++Arg) {
Rafael Espindola6f6f3c42013-05-24 16:11:44 +00001683 if (Arg->isDefaultArgument())
1684 break;
Douglas Gregordd04d332009-01-16 18:33:17 +00001685 if (Arg != Node->arg_begin())
1686 OS << ", ";
1687 PrintExpr(*Arg);
1688 }
1689 OS << ")";
1690}
1691
Douglas Gregore31e6062012-02-07 10:09:13 +00001692void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1693 OS << '[';
1694 bool NeedComma = false;
1695 switch (Node->getCaptureDefault()) {
1696 case LCD_None:
1697 break;
1698
1699 case LCD_ByCopy:
1700 OS << '=';
1701 NeedComma = true;
1702 break;
1703
1704 case LCD_ByRef:
1705 OS << '&';
1706 NeedComma = true;
1707 break;
1708 }
1709 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1710 CEnd = Node->explicit_capture_end();
1711 C != CEnd;
1712 ++C) {
1713 if (NeedComma)
1714 OS << ", ";
1715 NeedComma = true;
1716
1717 switch (C->getCaptureKind()) {
1718 case LCK_This:
1719 OS << "this";
1720 break;
1721
1722 case LCK_ByRef:
Richard Smithbb13c9a2013-09-28 04:02:39 +00001723 if (Node->getCaptureDefault() != LCD_ByRef || C->isInitCapture())
Douglas Gregore31e6062012-02-07 10:09:13 +00001724 OS << '&';
1725 OS << C->getCapturedVar()->getName();
1726 break;
1727
1728 case LCK_ByCopy:
Douglas Gregore31e6062012-02-07 10:09:13 +00001729 OS << C->getCapturedVar()->getName();
1730 break;
1731 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00001732
1733 if (C->isInitCapture())
1734 PrintExpr(C->getCapturedVar()->getInit());
Douglas Gregore31e6062012-02-07 10:09:13 +00001735 }
1736 OS << ']';
1737
1738 if (Node->hasExplicitParameters()) {
1739 OS << " (";
1740 CXXMethodDecl *Method = Node->getCallOperator();
1741 NeedComma = false;
Aaron Ballman43b68be2014-03-07 17:50:17 +00001742 for (auto P : Method->params()) {
Douglas Gregore31e6062012-02-07 10:09:13 +00001743 if (NeedComma) {
1744 OS << ", ";
1745 } else {
1746 NeedComma = true;
1747 }
Aaron Ballman43b68be2014-03-07 17:50:17 +00001748 std::string ParamStr = P->getNameAsString();
1749 P->getOriginalType().print(OS, Policy, ParamStr);
Douglas Gregore31e6062012-02-07 10:09:13 +00001750 }
1751 if (Method->isVariadic()) {
1752 if (NeedComma)
1753 OS << ", ";
1754 OS << "...";
1755 }
1756 OS << ')';
1757
1758 if (Node->isMutable())
1759 OS << " mutable";
1760
1761 const FunctionProtoType *Proto
1762 = Method->getType()->getAs<FunctionProtoType>();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001763 Proto->printExceptionSpecification(OS, Policy);
Douglas Gregore31e6062012-02-07 10:09:13 +00001764
Bill Wendling44426052012-12-20 19:22:21 +00001765 // FIXME: Attributes
Douglas Gregore31e6062012-02-07 10:09:13 +00001766
Douglas Gregor0c46b2b2012-02-13 22:00:16 +00001767 // Print the trailing return type if it was specified in the source.
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001768 if (Node->hasExplicitResultType()) {
1769 OS << " -> ";
Alp Toker314cc812014-01-25 16:55:45 +00001770 Proto->getReturnType().print(OS, Policy);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001771 }
Douglas Gregore31e6062012-02-07 10:09:13 +00001772 }
1773
1774 // Print the body.
1775 CompoundStmt *Body = Node->getBody();
1776 OS << ' ';
1777 PrintStmt(Body);
1778}
1779
Douglas Gregor747eb782010-07-08 06:14:04 +00001780void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregor2b88c112010-09-08 00:15:04 +00001781 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001782 TSInfo->getType().print(OS, Policy);
Douglas Gregor2b88c112010-09-08 00:15:04 +00001783 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001784 Node->getType().print(OS, Policy);
1785 OS << "()";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001786}
1787
Sebastian Redlbd150f42008-11-21 19:14:01 +00001788void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1789 if (E->isGlobalNew())
1790 OS << "::";
1791 OS << "new ";
1792 unsigned NumPlace = E->getNumPlacementArgs();
Eli Friedmana6fdfaa2012-10-18 20:54:37 +00001793 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00001794 OS << "(";
1795 PrintExpr(E->getPlacementArg(0));
1796 for (unsigned i = 1; i < NumPlace; ++i) {
Eli Friedmana6fdfaa2012-10-18 20:54:37 +00001797 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
1798 break;
Sebastian Redlbd150f42008-11-21 19:14:01 +00001799 OS << ", ";
1800 PrintExpr(E->getPlacementArg(i));
1801 }
1802 OS << ") ";
1803 }
1804 if (E->isParenTypeId())
1805 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001806 std::string TypeS;
1807 if (Expr *Size = E->getArraySize()) {
1808 llvm::raw_string_ostream s(TypeS);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001809 s << '[';
Richard Smith235341b2012-08-16 03:56:14 +00001810 Size->printPretty(s, Helper, Policy);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001811 s << ']';
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001812 }
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001813 E->getAllocatedType().print(OS, Policy, TypeS);
Sebastian Redlbd150f42008-11-21 19:14:01 +00001814 if (E->isParenTypeId())
1815 OS << ")";
1816
Sebastian Redl6047f072012-02-16 12:22:20 +00001817 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
1818 if (InitStyle) {
1819 if (InitStyle == CXXNewExpr::CallInit)
1820 OS << "(";
1821 PrintExpr(E->getInitializer());
1822 if (InitStyle == CXXNewExpr::CallInit)
1823 OS << ")";
Sebastian Redlbd150f42008-11-21 19:14:01 +00001824 }
1825}
1826
1827void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1828 if (E->isGlobalDelete())
1829 OS << "::";
1830 OS << "delete ";
1831 if (E->isArrayForm())
1832 OS << "[] ";
1833 PrintExpr(E->getArgument());
1834}
1835
Douglas Gregorad8a3362009-09-04 17:36:40 +00001836void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1837 PrintExpr(E->getBase());
1838 if (E->isArrow())
1839 OS << "->";
1840 else
1841 OS << '.';
1842 if (E->getQualifier())
1843 E->getQualifier()->print(OS, Policy);
Eli Friedman9cc8ac52012-10-23 20:26:57 +00001844 OS << "~";
Mike Stump11289f42009-09-09 15:08:12 +00001845
Douglas Gregor678f90d2010-02-25 01:56:36 +00001846 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1847 OS << II->getName();
1848 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001849 E->getDestroyedType().print(OS, Policy);
Douglas Gregorad8a3362009-09-04 17:36:40 +00001850}
1851
Anders Carlsson0781ce72009-04-23 02:32:43 +00001852void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Richard Smithd59b8322012-12-19 01:39:02 +00001853 if (E->isListInitialization())
1854 OS << "{ ";
1855
Douglas Gregorbaba85d2011-01-24 17:25:03 +00001856 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1857 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1858 // Don't print any defaulted arguments
1859 break;
1860 }
1861
1862 if (i) OS << ", ";
1863 PrintExpr(E->getArg(i));
Fariborz Jahaniand0bbf662010-01-13 21:41:11 +00001864 }
Richard Smithd59b8322012-12-19 01:39:02 +00001865
1866 if (E->isListInitialization())
1867 OS << " }";
Anders Carlsson0781ce72009-04-23 02:32:43 +00001868}
1869
Richard Smithcc1b96d2013-06-12 22:31:48 +00001870void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
1871 PrintExpr(E->getSubExpr());
1872}
1873
John McCall5d413782010-12-06 08:20:24 +00001874void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Alp Toker028ed912013-12-06 17:56:43 +00001875 // Just forward to the subexpression.
Anders Carlssondefc6442009-04-24 22:47:04 +00001876 PrintExpr(E->getSubExpr());
1877}
1878
Mike Stump11289f42009-09-09 15:08:12 +00001879void
Douglas Gregorce934142009-05-20 18:46:25 +00001880StmtPrinter::VisitCXXUnresolvedConstructExpr(
1881 CXXUnresolvedConstructExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001882 Node->getTypeAsWritten().print(OS, Policy);
Douglas Gregorce934142009-05-20 18:46:25 +00001883 OS << "(";
1884 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001885 ArgEnd = Node->arg_end();
Douglas Gregorce934142009-05-20 18:46:25 +00001886 Arg != ArgEnd; ++Arg) {
1887 if (Arg != Node->arg_begin())
1888 OS << ", ";
1889 PrintExpr(*Arg);
1890 }
1891 OS << ")";
1892}
1893
John McCall8cd78132009-11-19 22:55:06 +00001894void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1895 CXXDependentScopeMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001896 if (!Node->isImplicitAccess()) {
1897 PrintExpr(Node->getBase());
1898 OS << (Node->isArrow() ? "->" : ".");
1899 }
Douglas Gregorc26e0f62009-09-03 16:14:30 +00001900 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1901 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001902 if (Node->hasTemplateKeyword())
Douglas Gregor308047d2009-09-09 00:23:06 +00001903 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001904 OS << Node->getMemberNameInfo();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001905 if (Node->hasExplicitTemplateArgs())
1906 TemplateSpecializationType::PrintTemplateArgumentList(
1907 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregora8db9542009-05-22 21:13:27 +00001908}
1909
John McCall10eae182009-11-30 22:42:35 +00001910void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001911 if (!Node->isImplicitAccess()) {
1912 PrintExpr(Node->getBase());
1913 OS << (Node->isArrow() ? "->" : ".");
1914 }
John McCall10eae182009-11-30 22:42:35 +00001915 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1916 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001917 if (Node->hasTemplateKeyword())
1918 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001919 OS << Node->getMemberNameInfo();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001920 if (Node->hasExplicitTemplateArgs())
1921 TemplateSpecializationType::PrintTemplateArgumentList(
1922 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
John McCall10eae182009-11-30 22:42:35 +00001923}
1924
Douglas Gregor29c42f22012-02-24 07:38:34 +00001925static const char *getTypeTraitName(TypeTrait TT) {
1926 switch (TT) {
Alp Toker95e7ff22014-01-01 05:57:51 +00001927#define TYPE_TRAIT_1(Spelling, Name, Key) \
1928case clang::UTT_##Name: return #Spelling;
Alp Tokercbb90342013-12-13 20:49:58 +00001929#define TYPE_TRAIT_2(Spelling, Name, Key) \
1930case clang::BTT_##Name: return #Spelling;
Alp Toker40f9b1c2013-12-12 21:23:03 +00001931#define TYPE_TRAIT_N(Spelling, Name, Key) \
1932 case clang::TT_##Name: return #Spelling;
1933#include "clang/Basic/TokenKinds.def"
Douglas Gregor29c42f22012-02-24 07:38:34 +00001934 }
1935 llvm_unreachable("Type trait not covered by switch");
1936}
1937
John Wiegley6242b6a2011-04-28 00:16:57 +00001938static const char *getTypeTraitName(ArrayTypeTrait ATT) {
1939 switch (ATT) {
1940 case ATT_ArrayRank: return "__array_rank";
1941 case ATT_ArrayExtent: return "__array_extent";
1942 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001943 llvm_unreachable("Array type trait not covered by switch");
John Wiegley6242b6a2011-04-28 00:16:57 +00001944}
1945
John Wiegleyf9f65842011-04-25 06:54:41 +00001946static const char *getExpressionTraitName(ExpressionTrait ET) {
1947 switch (ET) {
John Wiegleyf9f65842011-04-25 06:54:41 +00001948 case ET_IsLValueExpr: return "__is_lvalue_expr";
1949 case ET_IsRValueExpr: return "__is_rvalue_expr";
1950 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001951 llvm_unreachable("Expression type trait not covered by switch");
John Wiegleyf9f65842011-04-25 06:54:41 +00001952}
1953
Douglas Gregor29c42f22012-02-24 07:38:34 +00001954void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
1955 OS << getTypeTraitName(E->getTrait()) << "(";
1956 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
1957 if (I > 0)
1958 OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001959 E->getArg(I)->getType().print(OS, Policy);
Douglas Gregor29c42f22012-02-24 07:38:34 +00001960 }
1961 OS << ")";
1962}
1963
John Wiegley6242b6a2011-04-28 00:16:57 +00001964void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001965 OS << getTypeTraitName(E->getTrait()) << '(';
1966 E->getQueriedType().print(OS, Policy);
1967 OS << ')';
John Wiegley6242b6a2011-04-28 00:16:57 +00001968}
1969
John Wiegleyf9f65842011-04-25 06:54:41 +00001970void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001971 OS << getExpressionTraitName(E->getTrait()) << '(';
1972 PrintExpr(E->getQueriedExpression());
1973 OS << ')';
John Wiegleyf9f65842011-04-25 06:54:41 +00001974}
1975
Sebastian Redl4202c0f2010-09-10 20:55:43 +00001976void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1977 OS << "noexcept(";
1978 PrintExpr(E->getOperand());
1979 OS << ")";
1980}
1981
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001982void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregore8e9dd62011-01-03 17:17:50 +00001983 PrintExpr(E->getPattern());
1984 OS << "...";
1985}
1986
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001987void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001988 OS << "sizeof...(" << *E->getPack() << ")";
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001989}
1990
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001991void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
1992 SubstNonTypeTemplateParmPackExpr *Node) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001993 OS << *Node->getParameterPack();
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001994}
1995
John McCall7c454bb2011-07-15 05:09:51 +00001996void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
1997 SubstNonTypeTemplateParmExpr *Node) {
1998 Visit(Node->getReplacement());
1999}
2000
Richard Smithb15fe3a2012-09-12 00:56:43 +00002001void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
2002 OS << *E->getParameterPack();
2003}
2004
Douglas Gregorfe314812011-06-21 17:03:29 +00002005void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
2006 PrintExpr(Node->GetTemporaryExpr());
2007}
2008
Mike Stump11289f42009-09-09 15:08:12 +00002009// Obj-C
Anders Carlsson76f4a902007-08-21 17:43:55 +00002010
2011void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
2012 OS << "@";
2013 VisitStringLiteral(Node->getString());
2014}
Chris Lattnereefa10e2007-05-28 06:56:27 +00002015
Patrick Beard0caa3942012-04-19 00:25:12 +00002016void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00002017 OS << "@";
Patrick Beard0caa3942012-04-19 00:25:12 +00002018 Visit(E->getSubExpr());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002019}
2020
2021void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
2022 OS << "@[ ";
2023 StmtRange ch = E->children();
2024 if (ch.first != ch.second) {
2025 while (1) {
2026 Visit(*ch.first);
2027 ++ch.first;
2028 if (ch.first == ch.second) break;
2029 OS << ", ";
2030 }
2031 }
2032 OS << " ]";
2033}
2034
2035void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
2036 OS << "@{ ";
2037 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
2038 if (I > 0)
2039 OS << ", ";
2040
2041 ObjCDictionaryElement Element = E->getKeyValueElement(I);
2042 Visit(Element.Key);
2043 OS << " : ";
2044 Visit(Element.Value);
2045 if (Element.isPackExpansion())
2046 OS << "...";
2047 }
2048 OS << " }";
2049}
2050
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002051void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002052 OS << "@encode(";
2053 Node->getEncodedType().print(OS, Policy);
2054 OS << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002055}
2056
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002057void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00002058 OS << "@selector(";
2059 Node->getSelector().print(OS);
2060 OS << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002061}
2062
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002063void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00002064 OS << "@protocol(" << *Node->getProtocol() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002065}
2066
Steve Naroffd54978b2007-09-18 23:55:05 +00002067void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
2068 OS << "[";
Douglas Gregor9a129192010-04-21 00:45:42 +00002069 switch (Mess->getReceiverKind()) {
2070 case ObjCMessageExpr::Instance:
2071 PrintExpr(Mess->getInstanceReceiver());
2072 break;
2073
2074 case ObjCMessageExpr::Class:
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002075 Mess->getClassReceiver().print(OS, Policy);
Douglas Gregor9a129192010-04-21 00:45:42 +00002076 break;
2077
2078 case ObjCMessageExpr::SuperInstance:
2079 case ObjCMessageExpr::SuperClass:
2080 OS << "Super";
2081 break;
2082 }
2083
Ted Kremeneka06e7122008-05-02 17:32:38 +00002084 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00002085 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00002086 if (selector.isUnarySelector()) {
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00002087 OS << selector.getNameForSlot(0);
Steve Naroffc6814ea2007-10-02 20:01:56 +00002088 } else {
2089 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00002090 if (i < selector.getNumArgs()) {
2091 if (i > 0) OS << ' ';
2092 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00002093 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00002094 else
2095 OS << ":";
2096 }
2097 else OS << ", "; // Handle variadic methods.
Mike Stump11289f42009-09-09 15:08:12 +00002098
Steve Naroffc6814ea2007-10-02 20:01:56 +00002099 PrintExpr(Mess->getArg(i));
2100 }
Steve Naroffd54978b2007-09-18 23:55:05 +00002101 }
2102 OS << "]";
2103}
2104
Ted Kremeneke65b0862012-03-06 20:05:56 +00002105void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
2106 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
2107}
2108
John McCall31168b02011-06-15 23:02:42 +00002109void
2110StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
2111 PrintExpr(E->getSubExpr());
2112}
2113
2114void
2115StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002116 OS << '(' << E->getBridgeKindName();
2117 E->getType().print(OS, Policy);
2118 OS << ')';
John McCall31168b02011-06-15 23:02:42 +00002119 PrintExpr(E->getSubExpr());
2120}
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002121
Steve Naroffc540d662008-09-03 18:15:37 +00002122void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00002123 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00002124 OS << "^";
Mike Stump11289f42009-09-09 15:08:12 +00002125
Steve Naroffc540d662008-09-03 18:15:37 +00002126 const FunctionType *AFT = Node->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00002127
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002128 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroffc540d662008-09-03 18:15:37 +00002129 OS << "()";
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002130 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00002131 OS << '(';
Steve Naroff415d3d52008-10-08 17:01:13 +00002132 for (BlockDecl::param_iterator AI = BD->param_begin(),
2133 E = BD->param_end(); AI != E; ++AI) {
2134 if (AI != BD->param_begin()) OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002135 std::string ParamStr = (*AI)->getNameAsString();
2136 (*AI)->getType().print(OS, Policy, ParamStr);
Steve Naroffc540d662008-09-03 18:15:37 +00002137 }
Mike Stump11289f42009-09-09 15:08:12 +00002138
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002139 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00002140 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00002141 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00002142 OS << "...";
2143 }
2144 OS << ')';
2145 }
Fariborz Jahanian1ace8cb2012-12-04 21:15:23 +00002146 OS << "{ }";
Steve Naroffc540d662008-09-03 18:15:37 +00002147}
2148
Ted Kremenekf551f322011-11-30 22:08:08 +00002149void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
2150 PrintExpr(Node->getSourceExpr());
2151}
John McCall8d69a212010-11-15 23:31:06 +00002152
Tanya Lattner55808c12011-06-04 00:47:47 +00002153void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
2154 OS << "__builtin_astype(";
2155 PrintExpr(Node->getSrcExpr());
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002156 OS << ", ";
2157 Node->getType().print(OS, Policy);
Tanya Lattner55808c12011-06-04 00:47:47 +00002158 OS << ")";
2159}
2160
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00002161//===----------------------------------------------------------------------===//
2162// Stmt method implementations
2163//===----------------------------------------------------------------------===//
2164
Craig Topperc571c812013-08-22 06:02:26 +00002165void Stmt::dumpPretty(const ASTContext &Context) const {
Craig Topper36250ad2014-05-12 05:36:57 +00002166 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00002167}
2168
Richard Smith235341b2012-08-16 03:56:14 +00002169void Stmt::printPretty(raw_ostream &OS,
2170 PrinterHelper *Helper,
Douglas Gregor7de59662009-05-29 20:38:28 +00002171 const PrintingPolicy &Policy,
2172 unsigned Indentation) const {
Richard Smith235341b2012-08-16 03:56:14 +00002173 StmtPrinter P(OS, Helper, Policy, Indentation);
Chris Lattner62249a62007-08-21 04:04:25 +00002174 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00002175}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002176
2177//===----------------------------------------------------------------------===//
2178// PrinterHelper
2179//===----------------------------------------------------------------------===//
2180
2181// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +00002182PrinterHelper::~PrinterHelper() {}