blob: 2e8da360c9430ad740be719b34c57b884eb7ec70 [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"
Alexey Bataev1a3320e2015-08-25 14:24:04 +000022#include "clang/AST/ExprOpenMP.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000023#include "clang/AST/PrettyPrinter.h"
24#include "clang/AST/StmtVisitor.h"
Jordan Rose00d1b592013-02-08 22:30:27 +000025#include "clang/Basic/CharInfo.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000026#include "llvm/ADT/SmallString.h"
Jordan Rose00d1b592013-02-08 22:30:27 +000027#include "llvm/Support/Format.h"
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000028using namespace clang;
29
30//===----------------------------------------------------------------------===//
31// StmtPrinter Visitor
32//===----------------------------------------------------------------------===//
33
34namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +000035 class StmtPrinter : public StmtVisitor<StmtPrinter> {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000036 raw_ostream &OS;
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000037 unsigned IndentLevel;
Ted Kremenek04f3cee2007-08-31 21:30:12 +000038 clang::PrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +000039 PrintingPolicy Policy;
40
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000041 public:
Richard Smith235341b2012-08-16 03:56:14 +000042 StmtPrinter(raw_ostream &os, PrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +000043 const PrintingPolicy &Policy,
Douglas Gregor7de59662009-05-29 20:38:28 +000044 unsigned Indentation = 0)
Richard Smith235341b2012-08-16 03:56:14 +000045 : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy) {}
Mike Stump11289f42009-09-09 15:08:12 +000046
Douglas Gregor7de59662009-05-29 20:38:28 +000047 void PrintStmt(Stmt *S) {
48 PrintStmt(S, Policy.Indentation);
49 }
50
51 void PrintStmt(Stmt *S, int SubIndent) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000052 IndentLevel += SubIndent;
Chris Lattnera076fde2007-05-31 18:21:33 +000053 if (S && isa<Expr>(S)) {
Chris Lattner882f7882006-11-04 18:52:07 +000054 // If this is an expr used in a stmt context, indent and newline it.
55 Indent();
Chris Lattner62249a62007-08-21 04:04:25 +000056 Visit(S);
Chris Lattnerfc068c12007-05-30 17:57:36 +000057 OS << ";\n";
Chris Lattner882f7882006-11-04 18:52:07 +000058 } else if (S) {
Chris Lattner62249a62007-08-21 04:04:25 +000059 Visit(S);
Chris Lattner882f7882006-11-04 18:52:07 +000060 } else {
Chris Lattner2f6ac262007-05-28 01:47:47 +000061 Indent() << "<<<NULL STATEMENT>>>\n";
Chris Lattner882f7882006-11-04 18:52:07 +000062 }
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000063 IndentLevel -= SubIndent;
Chris Lattner882f7882006-11-04 18:52:07 +000064 }
Eli Friedman15ea8802009-05-30 00:19:54 +000065
Chris Lattner073926e2007-05-20 23:04:55 +000066 void PrintRawCompoundStmt(CompoundStmt *S);
Chris Lattnerfdc195a2007-06-05 20:52:47 +000067 void PrintRawDecl(Decl *D);
Eli Friedmanf86e5072012-10-16 23:45:15 +000068 void PrintRawDeclStmt(const DeclStmt *S);
Chris Lattnerc0a38dd2007-06-11 22:26:23 +000069 void PrintRawIfStmt(IfStmt *If);
Sebastian Redl9b244a82008-12-22 21:35:02 +000070 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
Peter Collingbourne4b279a02011-02-08 21:17:54 +000071 void PrintCallArgs(CallExpr *E);
John Wiegley1c0675e2011-04-28 01:08:34 +000072 void PrintRawSEHExceptHandler(SEHExceptStmt *S);
73 void PrintRawSEHFinallyStmt(SEHFinallyStmt *S);
Alexey Bataev1b59ab52014-02-27 08:29:12 +000074 void PrintOMPExecutableDirective(OMPExecutableDirective *S);
Mike Stump11289f42009-09-09 15:08:12 +000075
Chris Lattner882f7882006-11-04 18:52:07 +000076 void PrintExpr(Expr *E) {
77 if (E)
Chris Lattner62249a62007-08-21 04:04:25 +000078 Visit(E);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000079 else
Chris Lattner882f7882006-11-04 18:52:07 +000080 OS << "<null expr>";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000081 }
Mike Stump11289f42009-09-09 15:08:12 +000082
Chris Lattner0e62c1c2011-07-23 10:55:15 +000083 raw_ostream &Indent(int Delta = 0) {
Douglas Gregor7de59662009-05-29 20:38:28 +000084 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
85 OS << " ";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000086 return OS;
87 }
Mike Stump11289f42009-09-09 15:08:12 +000088
Mike Stump11289f42009-09-09 15:08:12 +000089 void Visit(Stmt* S) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +000090 if (Helper && Helper->handledStmt(S,OS))
91 return;
92 else StmtVisitor<StmtPrinter>::Visit(S);
93 }
Alexey Bataev1b59ab52014-02-27 08:29:12 +000094
Chandler Carruthb7967b92010-10-23 08:21:37 +000095 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +000096 Indent() << "<<unknown stmt type>>\n";
97 }
Chandler Carruthb7967b92010-10-23 08:21:37 +000098 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +000099 OS << "<<unknown expr type>>";
100 }
101 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Mike Stump11289f42009-09-09 15:08:12 +0000102
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +0000103#define ABSTRACT_STMT(CLASS)
Douglas Gregorbe35ce92008-11-14 12:46:07 +0000104#define STMT(CLASS, PARENT) \
Chris Lattner62249a62007-08-21 04:04:25 +0000105 void Visit##CLASS(CLASS *Node);
Alexis Hunt656bb312010-05-05 15:24:00 +0000106#include "clang/AST/StmtNodes.inc"
Chris Lattner71e23ce2006-11-04 20:18:38 +0000107 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000108}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000109
Chris Lattner71e23ce2006-11-04 20:18:38 +0000110//===----------------------------------------------------------------------===//
111// Stmt printing methods.
112//===----------------------------------------------------------------------===//
113
Chris Lattner073926e2007-05-20 23:04:55 +0000114/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
115/// with no newline after the }.
116void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
117 OS << "{\n";
Aaron Ballmanc7e4e212014-03-17 14:19:37 +0000118 for (auto *I : Node->body())
119 PrintStmt(I);
Mike Stump11289f42009-09-09 15:08:12 +0000120
Chris Lattner073926e2007-05-20 23:04:55 +0000121 Indent() << "}";
122}
123
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000124void StmtPrinter::PrintRawDecl(Decl *D) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000125 D->print(OS, Policy, IndentLevel);
Mike Stump74a76472009-02-10 20:16:46 +0000126}
127
Eli Friedmanf86e5072012-10-16 23:45:15 +0000128void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
Aaron Ballman535bbcc2014-03-14 17:01:24 +0000129 SmallVector<Decl*, 2> Decls(S->decls());
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000130 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
Ted Kremenek15e6b402008-10-06 18:39:36 +0000131}
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000132
133void StmtPrinter::VisitNullStmt(NullStmt *Node) {
134 Indent() << ";\n";
135}
136
137void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Eli Friedman15ea8802009-05-30 00:19:54 +0000138 Indent();
139 PrintRawDeclStmt(Node);
140 OS << ";\n";
Steve Naroff2a8ad182007-05-29 22:59:26 +0000141}
142
Chris Lattner073926e2007-05-20 23:04:55 +0000143void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
144 Indent();
145 PrintRawCompoundStmt(Node);
Chris Lattnerdf3cafb2007-05-31 05:08:56 +0000146 OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000147}
148
Chris Lattner6c0ff132006-11-05 00:19:50 +0000149void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000150 Indent(-1) << "case ";
Chris Lattner6c0ff132006-11-05 00:19:50 +0000151 PrintExpr(Node->getLHS());
152 if (Node->getRHS()) {
153 OS << " ... ";
154 PrintExpr(Node->getRHS());
155 }
156 OS << ":\n";
Mike Stump11289f42009-09-09 15:08:12 +0000157
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000158 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000159}
160
161void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000162 Indent(-1) << "default:\n";
163 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000164}
165
166void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000167 Indent(-1) << Node->getName() << ":\n";
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000168 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000169}
170
Richard Smithc202b282012-04-14 00:33:13 +0000171void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
Aaron Ballmanb06b15a2014-06-06 12:40:24 +0000172 for (const auto *Attr : Node->getAttrs()) {
Tyler Nowickie8b07ed2014-06-13 17:57:25 +0000173 Attr->printPretty(OS, Policy);
Aaron Ballmanb06b15a2014-06-06 12:40:24 +0000174 }
175
Richard Smithc202b282012-04-14 00:33:13 +0000176 PrintStmt(Node->getSubStmt(), 0);
177}
178
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000179void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000180 OS << "if (";
Eli Friedmanf86e5072012-10-16 23:45:15 +0000181 if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
182 PrintRawDeclStmt(DS);
183 else
184 PrintExpr(If->getCond());
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000185 OS << ')';
Mike Stump11289f42009-09-09 15:08:12 +0000186
Chris Lattner073926e2007-05-20 23:04:55 +0000187 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
188 OS << ' ';
189 PrintRawCompoundStmt(CS);
190 OS << (If->getElse() ? ' ' : '\n');
191 } else {
192 OS << '\n';
193 PrintStmt(If->getThen());
194 if (If->getElse()) Indent();
195 }
Mike Stump11289f42009-09-09 15:08:12 +0000196
Chris Lattner073926e2007-05-20 23:04:55 +0000197 if (Stmt *Else = If->getElse()) {
198 OS << "else";
Mike Stump11289f42009-09-09 15:08:12 +0000199
Chris Lattner073926e2007-05-20 23:04:55 +0000200 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
201 OS << ' ';
202 PrintRawCompoundStmt(CS);
203 OS << '\n';
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000204 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
205 OS << ' ';
206 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000207 } else {
208 OS << '\n';
209 PrintStmt(If->getElse());
210 }
Chris Lattner882f7882006-11-04 18:52:07 +0000211 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000212}
213
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000214void StmtPrinter::VisitIfStmt(IfStmt *If) {
215 Indent();
216 PrintRawIfStmt(If);
217}
218
Chris Lattnerf2174b62006-11-04 20:59:27 +0000219void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
220 Indent() << "switch (";
Eli Friedmanf86e5072012-10-16 23:45:15 +0000221 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
222 PrintRawDeclStmt(DS);
223 else
224 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000225 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000226
Chris Lattner073926e2007-05-20 23:04:55 +0000227 // Pretty print compoundstmt bodies (very common).
228 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
229 OS << " ";
230 PrintRawCompoundStmt(CS);
231 OS << "\n";
232 } else {
233 OS << "\n";
234 PrintStmt(Node->getBody());
235 }
Chris Lattnerf2174b62006-11-04 20:59:27 +0000236}
237
Chris Lattner85ed8732006-11-04 20:40:44 +0000238void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
239 Indent() << "while (";
Eli Friedmanf86e5072012-10-16 23:45:15 +0000240 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
241 PrintRawDeclStmt(DS);
242 else
243 PrintExpr(Node->getCond());
Chris Lattner85ed8732006-11-04 20:40:44 +0000244 OS << ")\n";
245 PrintStmt(Node->getBody());
246}
247
248void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000249 Indent() << "do ";
250 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
251 PrintRawCompoundStmt(CS);
252 OS << " ";
253 } else {
254 OS << "\n";
255 PrintStmt(Node->getBody());
256 Indent();
257 }
Mike Stump11289f42009-09-09 15:08:12 +0000258
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000259 OS << "while (";
Chris Lattner85ed8732006-11-04 20:40:44 +0000260 PrintExpr(Node->getCond());
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000261 OS << ");\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000262}
263
Chris Lattner71e23ce2006-11-04 20:18:38 +0000264void StmtPrinter::VisitForStmt(ForStmt *Node) {
265 Indent() << "for (";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000266 if (Node->getInit()) {
267 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000268 PrintRawDeclStmt(DS);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000269 else
270 PrintExpr(cast<Expr>(Node->getInit()));
271 }
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000272 OS << ";";
273 if (Node->getCond()) {
274 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000275 PrintExpr(Node->getCond());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000276 }
277 OS << ";";
278 if (Node->getInc()) {
279 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000280 PrintExpr(Node->getInc());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000281 }
282 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000283
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000284 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
285 PrintRawCompoundStmt(CS);
286 OS << "\n";
287 } else {
288 OS << "\n";
289 PrintStmt(Node->getBody());
290 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000291}
292
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000293void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian83615522008-01-02 22:54:34 +0000294 Indent() << "for (";
295 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000296 PrintRawDeclStmt(DS);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000297 else
298 PrintExpr(cast<Expr>(Node->getElement()));
299 OS << " in ";
300 PrintExpr(Node->getCollection());
301 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000302
Fariborz Jahanian83615522008-01-02 22:54:34 +0000303 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
304 PrintRawCompoundStmt(CS);
305 OS << "\n";
306 } else {
307 OS << "\n";
308 PrintStmt(Node->getBody());
309 }
310}
311
Richard Smith02e85f32011-04-14 22:09:26 +0000312void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
313 Indent() << "for (";
314 PrintingPolicy SubPolicy(Policy);
315 SubPolicy.SuppressInitializers = true;
316 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
317 OS << " : ";
318 PrintExpr(Node->getRangeInit());
319 OS << ") {\n";
320 PrintStmt(Node->getBody());
Ted Kremenek88446602013-12-11 23:44:02 +0000321 Indent() << "}";
322 if (Policy.IncludeNewlines) OS << "\n";
Richard Smith02e85f32011-04-14 22:09:26 +0000323}
324
Douglas Gregordeb4a2be2011-10-25 01:33:02 +0000325void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
326 Indent();
327 if (Node->isIfExists())
328 OS << "__if_exists (";
329 else
330 OS << "__if_not_exists (";
331
332 if (NestedNameSpecifier *Qualifier
333 = Node->getQualifierLoc().getNestedNameSpecifier())
334 Qualifier->print(OS, Policy);
335
336 OS << Node->getNameInfo() << ") ";
337
338 PrintRawCompoundStmt(Node->getSubStmt());
339}
340
Chris Lattner16976d32006-11-05 01:46:01 +0000341void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000342 Indent() << "goto " << Node->getLabel()->getName() << ";";
343 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000344}
345
346void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000347 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000348 PrintExpr(Node->getTarget());
Ted Kremenek88446602013-12-11 23:44:02 +0000349 OS << ";";
350 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000351}
352
353void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000354 Indent() << "continue;";
355 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000356}
357
358void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000359 Indent() << "break;";
360 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000361}
362
363
Chris Lattner882f7882006-11-04 18:52:07 +0000364void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
365 Indent() << "return";
366 if (Node->getRetValue()) {
367 OS << " ";
368 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000369 }
Ted Kremenek88446602013-12-11 23:44:02 +0000370 OS << ";";
371 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000372}
373
Chris Lattner73c56c02007-10-29 04:04:16 +0000374
Chad Rosierde70e0e2012-08-25 00:11:56 +0000375void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
Anders Carlsson660bdd12007-11-23 23:12:25 +0000376 Indent() << "asm ";
Mike Stump11289f42009-09-09 15:08:12 +0000377
Anders Carlsson660bdd12007-11-23 23:12:25 +0000378 if (Node->isVolatile())
379 OS << "volatile ";
Mike Stump11289f42009-09-09 15:08:12 +0000380
Anders Carlsson660bdd12007-11-23 23:12:25 +0000381 OS << "(";
Anders Carlsson81a5a692007-11-20 19:21:03 +0000382 VisitStringLiteral(Node->getAsmString());
Mike Stump11289f42009-09-09 15:08:12 +0000383
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000384 // Outputs
385 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
386 Node->getNumClobbers() != 0)
387 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000388
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000389 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
390 if (i != 0)
391 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000392
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000393 if (!Node->getOutputName(i).empty()) {
394 OS << '[';
395 OS << Node->getOutputName(i);
396 OS << "] ";
397 }
Mike Stump11289f42009-09-09 15:08:12 +0000398
Chris Lattner72bbf172009-03-10 04:59:06 +0000399 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Jonathan Roelofs78f9e032015-06-09 14:13:31 +0000400 OS << " (";
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000401 Visit(Node->getOutputExpr(i));
Jonathan Roelofs78f9e032015-06-09 14:13:31 +0000402 OS << ")";
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000403 }
Mike Stump11289f42009-09-09 15:08:12 +0000404
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000405 // Inputs
406 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
407 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000408
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000409 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
410 if (i != 0)
411 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000412
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000413 if (!Node->getInputName(i).empty()) {
414 OS << '[';
415 OS << Node->getInputName(i);
416 OS << "] ";
417 }
Mike Stump11289f42009-09-09 15:08:12 +0000418
Chris Lattner72bbf172009-03-10 04:59:06 +0000419 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Jonathan Roelofs78f9e032015-06-09 14:13:31 +0000420 OS << " (";
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000421 Visit(Node->getInputExpr(i));
Jonathan Roelofs78f9e032015-06-09 14:13:31 +0000422 OS << ")";
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000423 }
Mike Stump11289f42009-09-09 15:08:12 +0000424
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000425 // Clobbers
426 if (Node->getNumClobbers() != 0)
427 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000428
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000429 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
430 if (i != 0)
431 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000432
Chad Rosierd9fb09a2012-08-27 23:28:41 +0000433 VisitStringLiteral(Node->getClobberStringLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000434 }
Mike Stump11289f42009-09-09 15:08:12 +0000435
Ted Kremenek88446602013-12-11 23:44:02 +0000436 OS << ");";
437 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner73c56c02007-10-29 04:04:16 +0000438}
439
Chad Rosier32503022012-06-11 20:47:18 +0000440void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
441 // FIXME: Implement MS style inline asm statement printer.
Chad Rosierb6f46c12012-08-15 16:53:30 +0000442 Indent() << "__asm ";
443 if (Node->hasBraces())
444 OS << "{\n";
John McCallf413f5e2013-05-03 00:10:13 +0000445 OS << Node->getAsmString() << "\n";
Chad Rosierb6f46c12012-08-15 16:53:30 +0000446 if (Node->hasBraces())
447 Indent() << "}\n";
Chad Rosier32503022012-06-11 20:47:18 +0000448}
449
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000450void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000451 PrintStmt(Node->getCapturedDecl()->getBody());
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000452}
453
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000454void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000455 Indent() << "@try";
456 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
457 PrintRawCompoundStmt(TS);
458 OS << "\n";
459 }
Mike Stump11289f42009-09-09 15:08:12 +0000460
Douglas Gregor96c79492010-04-23 22:50:49 +0000461 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
462 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000463 Indent() << "@catch(";
Steve Naroff371b8fb2009-03-03 19:52:17 +0000464 if (catchStmt->getCatchParamDecl()) {
465 if (Decl *DS = catchStmt->getCatchParamDecl())
466 PrintRawDecl(DS);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000467 }
468 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000469 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
470 PrintRawCompoundStmt(CS);
471 OS << "\n";
472 }
Fariborz Jahanian88157952007-11-02 18:16:07 +0000473 }
Mike Stump11289f42009-09-09 15:08:12 +0000474
475 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
476 Node->getFinallyStmt())) {
Fariborz Jahaniandefbf9a2007-11-07 00:46:42 +0000477 Indent() << "@finally";
478 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian88157952007-11-02 18:16:07 +0000479 OS << "\n";
Mike Stump11289f42009-09-09 15:08:12 +0000480 }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000481}
482
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000483void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000484}
485
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000486void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000487 Indent() << "@catch (...) { /* todo */ } \n";
488}
489
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000490void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000491 Indent() << "@throw";
492 if (Node->getThrowExpr()) {
493 OS << " ";
494 PrintExpr(Node->getThrowExpr());
495 }
496 OS << ";\n";
497}
498
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000499void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000500 Indent() << "@synchronized (";
501 PrintExpr(Node->getSynchExpr());
502 OS << ")";
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000503 PrintRawCompoundStmt(Node->getSynchBody());
504 OS << "\n";
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000505}
506
John McCall31168b02011-06-15 23:02:42 +0000507void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
508 Indent() << "@autoreleasepool";
509 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
510 OS << "\n";
511}
512
Sebastian Redl9b244a82008-12-22 21:35:02 +0000513void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
514 OS << "catch (";
Sebastian Redl54c04d42008-12-22 19:15:10 +0000515 if (Decl *ExDecl = Node->getExceptionDecl())
516 PrintRawDecl(ExDecl);
517 else
518 OS << "...";
519 OS << ") ";
520 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl9b244a82008-12-22 21:35:02 +0000521}
522
523void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
524 Indent();
525 PrintRawCXXCatchStmt(Node);
526 OS << "\n";
527}
528
529void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
530 Indent() << "try ";
531 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump11289f42009-09-09 15:08:12 +0000532 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl9b244a82008-12-22 21:35:02 +0000533 OS << " ";
534 PrintRawCXXCatchStmt(Node->getHandler(i));
535 }
Sebastian Redl54c04d42008-12-22 19:15:10 +0000536 OS << "\n";
537}
538
John Wiegley1c0675e2011-04-28 01:08:34 +0000539void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
540 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
541 PrintRawCompoundStmt(Node->getTryBlock());
542 SEHExceptStmt *E = Node->getExceptHandler();
543 SEHFinallyStmt *F = Node->getFinallyHandler();
544 if(E)
545 PrintRawSEHExceptHandler(E);
546 else {
547 assert(F && "Must have a finally block...");
548 PrintRawSEHFinallyStmt(F);
549 }
550 OS << "\n";
551}
552
553void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
554 OS << "__finally ";
555 PrintRawCompoundStmt(Node->getBlock());
556 OS << "\n";
557}
558
559void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
560 OS << "__except (";
561 VisitExpr(Node->getFilterExpr());
Joao Matos566359c2012-09-04 17:49:35 +0000562 OS << ")\n";
John Wiegley1c0675e2011-04-28 01:08:34 +0000563 PrintRawCompoundStmt(Node->getBlock());
564 OS << "\n";
565}
566
567void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
568 Indent();
569 PrintRawSEHExceptHandler(Node);
570 OS << "\n";
571}
572
573void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
574 Indent();
575 PrintRawSEHFinallyStmt(Node);
576 OS << "\n";
577}
578
Nico Weber9b982072014-07-07 00:12:30 +0000579void StmtPrinter::VisitSEHLeaveStmt(SEHLeaveStmt *Node) {
580 Indent() << "__leave;";
581 if (Policy.IncludeNewlines) OS << "\n";
582}
583
Chris Lattner71e23ce2006-11-04 20:18:38 +0000584//===----------------------------------------------------------------------===//
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000585// OpenMP clauses printing methods
586//===----------------------------------------------------------------------===//
587
588namespace {
589class OMPClausePrinter : public OMPClauseVisitor<OMPClausePrinter> {
590 raw_ostream &OS;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000591 const PrintingPolicy &Policy;
Alexey Bataev756c1962013-09-24 03:17:45 +0000592 /// \brief Process clauses with list of variables.
593 template <typename T>
594 void VisitOMPClauseList(T *Node, char StartSym);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000595public:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000596 OMPClausePrinter(raw_ostream &OS, const PrintingPolicy &Policy)
597 : OS(OS), Policy(Policy) { }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000598#define OPENMP_CLAUSE(Name, Class) \
599 void Visit##Class(Class *S);
600#include "clang/Basic/OpenMPKinds.def"
601};
602
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000603void OMPClausePrinter::VisitOMPIfClause(OMPIfClause *Node) {
604 OS << "if(";
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000605 if (Node->getNameModifier() != OMPD_unknown)
606 OS << getOpenMPDirectiveName(Node->getNameModifier()) << ": ";
Craig Topper36250ad2014-05-12 05:36:57 +0000607 Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000608 OS << ")";
609}
610
Alexey Bataev3778b602014-07-17 07:32:53 +0000611void OMPClausePrinter::VisitOMPFinalClause(OMPFinalClause *Node) {
612 OS << "final(";
613 Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
614 OS << ")";
615}
616
Alexey Bataev568a8332014-03-06 06:15:19 +0000617void OMPClausePrinter::VisitOMPNumThreadsClause(OMPNumThreadsClause *Node) {
618 OS << "num_threads(";
Craig Topper36250ad2014-05-12 05:36:57 +0000619 Node->getNumThreads()->printPretty(OS, nullptr, Policy, 0);
Alexey Bataev568a8332014-03-06 06:15:19 +0000620 OS << ")";
621}
622
Alexey Bataev62c87d22014-03-21 04:51:18 +0000623void OMPClausePrinter::VisitOMPSafelenClause(OMPSafelenClause *Node) {
624 OS << "safelen(";
Craig Topper36250ad2014-05-12 05:36:57 +0000625 Node->getSafelen()->printPretty(OS, nullptr, Policy, 0);
Alexey Bataev62c87d22014-03-21 04:51:18 +0000626 OS << ")";
627}
628
Alexey Bataev66b15b52015-08-21 11:14:16 +0000629void OMPClausePrinter::VisitOMPSimdlenClause(OMPSimdlenClause *Node) {
630 OS << "simdlen(";
631 Node->getSimdlen()->printPretty(OS, nullptr, Policy, 0);
632 OS << ")";
633}
634
Alexander Musman8bd31e62014-05-27 15:12:19 +0000635void OMPClausePrinter::VisitOMPCollapseClause(OMPCollapseClause *Node) {
636 OS << "collapse(";
637 Node->getNumForLoops()->printPretty(OS, nullptr, Policy, 0);
638 OS << ")";
639}
640
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000641void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
642 OS << "default("
643 << getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind())
644 << ")";
645}
646
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000647void OMPClausePrinter::VisitOMPProcBindClause(OMPProcBindClause *Node) {
648 OS << "proc_bind("
649 << getOpenMPSimpleClauseTypeName(OMPC_proc_bind, Node->getProcBindKind())
650 << ")";
651}
652
Alexey Bataev56dafe82014-06-20 07:16:17 +0000653void OMPClausePrinter::VisitOMPScheduleClause(OMPScheduleClause *Node) {
Alexey Bataev6402bca2015-12-28 07:25:51 +0000654 OS << "schedule(";
655 if (Node->getFirstScheduleModifier() != OMPC_SCHEDULE_MODIFIER_unknown) {
656 OS << getOpenMPSimpleClauseTypeName(OMPC_schedule,
657 Node->getFirstScheduleModifier());
658 if (Node->getSecondScheduleModifier() != OMPC_SCHEDULE_MODIFIER_unknown) {
659 OS << ", ";
660 OS << getOpenMPSimpleClauseTypeName(OMPC_schedule,
661 Node->getSecondScheduleModifier());
662 }
663 OS << ": ";
664 }
665 OS << getOpenMPSimpleClauseTypeName(OMPC_schedule, Node->getScheduleKind());
Alexey Bataev56dafe82014-06-20 07:16:17 +0000666 if (Node->getChunkSize()) {
667 OS << ", ";
668 Node->getChunkSize()->printPretty(OS, nullptr, Policy);
669 }
670 OS << ")";
671}
672
Alexey Bataev10e775f2015-07-30 11:36:16 +0000673void OMPClausePrinter::VisitOMPOrderedClause(OMPOrderedClause *Node) {
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000674 OS << "ordered";
Alexey Bataev10e775f2015-07-30 11:36:16 +0000675 if (auto *Num = Node->getNumForLoops()) {
676 OS << "(";
677 Num->printPretty(OS, nullptr, Policy, 0);
678 OS << ")";
679 }
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000680}
681
Alexey Bataev236070f2014-06-20 11:19:47 +0000682void OMPClausePrinter::VisitOMPNowaitClause(OMPNowaitClause *) {
683 OS << "nowait";
684}
685
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000686void OMPClausePrinter::VisitOMPUntiedClause(OMPUntiedClause *) {
687 OS << "untied";
688}
689
Alexey Bataevb825de12015-12-07 10:51:44 +0000690void OMPClausePrinter::VisitOMPNogroupClause(OMPNogroupClause *) {
691 OS << "nogroup";
692}
693
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000694void OMPClausePrinter::VisitOMPMergeableClause(OMPMergeableClause *) {
695 OS << "mergeable";
696}
697
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000698void OMPClausePrinter::VisitOMPReadClause(OMPReadClause *) { OS << "read"; }
699
Alexey Bataevdea47612014-07-23 07:46:59 +0000700void OMPClausePrinter::VisitOMPWriteClause(OMPWriteClause *) { OS << "write"; }
701
Alexey Bataev67a4f222014-07-23 10:25:33 +0000702void OMPClausePrinter::VisitOMPUpdateClause(OMPUpdateClause *) {
703 OS << "update";
704}
705
Alexey Bataev459dec02014-07-24 06:46:57 +0000706void OMPClausePrinter::VisitOMPCaptureClause(OMPCaptureClause *) {
707 OS << "capture";
708}
709
Alexey Bataev82bad8b2014-07-24 08:55:34 +0000710void OMPClausePrinter::VisitOMPSeqCstClause(OMPSeqCstClause *) {
711 OS << "seq_cst";
712}
713
Alexey Bataev346265e2015-09-25 10:37:12 +0000714void OMPClausePrinter::VisitOMPThreadsClause(OMPThreadsClause *) {
715 OS << "threads";
716}
717
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000718void OMPClausePrinter::VisitOMPSIMDClause(OMPSIMDClause *) { OS << "simd"; }
719
Michael Wonge710d542015-08-07 16:16:36 +0000720void OMPClausePrinter::VisitOMPDeviceClause(OMPDeviceClause *Node) {
721 OS << "device(";
722 Node->getDevice()->printPretty(OS, nullptr, Policy, 0);
723 OS << ")";
724}
725
Kelvin Li099bb8c2015-11-24 20:50:12 +0000726void OMPClausePrinter::VisitOMPNumTeamsClause(OMPNumTeamsClause *Node) {
727 OS << "num_teams(";
728 Node->getNumTeams()->printPretty(OS, nullptr, Policy, 0);
729 OS << ")";
730}
731
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000732void OMPClausePrinter::VisitOMPThreadLimitClause(OMPThreadLimitClause *Node) {
733 OS << "thread_limit(";
734 Node->getThreadLimit()->printPretty(OS, nullptr, Policy, 0);
735 OS << ")";
736}
737
Alexey Bataeva0569352015-12-01 10:17:31 +0000738void OMPClausePrinter::VisitOMPPriorityClause(OMPPriorityClause *Node) {
739 OS << "priority(";
740 Node->getPriority()->printPretty(OS, nullptr, Policy, 0);
741 OS << ")";
742}
743
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000744void OMPClausePrinter::VisitOMPGrainsizeClause(OMPGrainsizeClause *Node) {
745 OS << "grainsize(";
746 Node->getGrainsize()->printPretty(OS, nullptr, Policy, 0);
747 OS << ")";
748}
749
Alexey Bataev382967a2015-12-08 12:06:20 +0000750void OMPClausePrinter::VisitOMPNumTasksClause(OMPNumTasksClause *Node) {
751 OS << "num_tasks(";
752 Node->getNumTasks()->printPretty(OS, nullptr, Policy, 0);
753 OS << ")";
754}
755
Alexey Bataev28c75412015-12-15 08:19:24 +0000756void OMPClausePrinter::VisitOMPHintClause(OMPHintClause *Node) {
757 OS << "hint(";
758 Node->getHint()->printPretty(OS, nullptr, Policy, 0);
759 OS << ")";
760}
761
Alexey Bataev756c1962013-09-24 03:17:45 +0000762template<typename T>
763void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
764 for (typename T::varlist_iterator I = Node->varlist_begin(),
765 E = Node->varlist_end();
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000766 I != E; ++I) {
Richard Trieuddd01ce2014-06-09 22:53:25 +0000767 assert(*I && "Expected non-null Stmt");
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000768 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(*I)) {
769 OS << (I == Node->varlist_begin() ? StartSym : ',');
770 cast<NamedDecl>(DRE->getDecl())->printQualifiedName(OS);
771 } else {
772 OS << (I == Node->varlist_begin() ? StartSym : ',');
Craig Topper36250ad2014-05-12 05:36:57 +0000773 (*I)->printPretty(OS, nullptr, Policy, 0);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000774 }
775 }
Alexey Bataev756c1962013-09-24 03:17:45 +0000776}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000777
778void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) {
779 if (!Node->varlist_empty()) {
780 OS << "private";
Alexey Bataev756c1962013-09-24 03:17:45 +0000781 VisitOMPClauseList(Node, '(');
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000782 OS << ")";
783 }
784}
785
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000786void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) {
787 if (!Node->varlist_empty()) {
788 OS << "firstprivate";
789 VisitOMPClauseList(Node, '(');
790 OS << ")";
791 }
792}
793
Alexander Musman1bb328c2014-06-04 13:06:39 +0000794void OMPClausePrinter::VisitOMPLastprivateClause(OMPLastprivateClause *Node) {
795 if (!Node->varlist_empty()) {
796 OS << "lastprivate";
797 VisitOMPClauseList(Node, '(');
798 OS << ")";
799 }
800}
801
Alexey Bataev758e55e2013-09-06 18:03:48 +0000802void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
803 if (!Node->varlist_empty()) {
804 OS << "shared";
Alexey Bataev756c1962013-09-24 03:17:45 +0000805 VisitOMPClauseList(Node, '(');
Alexey Bataev758e55e2013-09-06 18:03:48 +0000806 OS << ")";
807 }
808}
809
Alexey Bataevc5e02582014-06-16 07:08:35 +0000810void OMPClausePrinter::VisitOMPReductionClause(OMPReductionClause *Node) {
811 if (!Node->varlist_empty()) {
812 OS << "reduction(";
813 NestedNameSpecifier *QualifierLoc =
814 Node->getQualifierLoc().getNestedNameSpecifier();
815 OverloadedOperatorKind OOK =
816 Node->getNameInfo().getName().getCXXOverloadedOperator();
817 if (QualifierLoc == nullptr && OOK != OO_None) {
818 // Print reduction identifier in C format
819 OS << getOperatorSpelling(OOK);
820 } else {
821 // Use C++ format
822 if (QualifierLoc != nullptr)
823 QualifierLoc->print(OS, Policy);
824 OS << Node->getNameInfo();
825 }
826 OS << ":";
827 VisitOMPClauseList(Node, ' ');
828 OS << ")";
829 }
830}
831
Alexander Musman8dba6642014-04-22 13:09:42 +0000832void OMPClausePrinter::VisitOMPLinearClause(OMPLinearClause *Node) {
833 if (!Node->varlist_empty()) {
834 OS << "linear";
Alexey Bataev182227b2015-08-20 10:54:39 +0000835 if (Node->getModifierLoc().isValid()) {
836 OS << '('
837 << getOpenMPSimpleClauseTypeName(OMPC_linear, Node->getModifier());
838 }
Alexander Musman8dba6642014-04-22 13:09:42 +0000839 VisitOMPClauseList(Node, '(');
Alexey Bataev182227b2015-08-20 10:54:39 +0000840 if (Node->getModifierLoc().isValid())
841 OS << ')';
Craig Topper36250ad2014-05-12 05:36:57 +0000842 if (Node->getStep() != nullptr) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000843 OS << ": ";
Craig Topper36250ad2014-05-12 05:36:57 +0000844 Node->getStep()->printPretty(OS, nullptr, Policy, 0);
Alexander Musman8dba6642014-04-22 13:09:42 +0000845 }
846 OS << ")";
847 }
848}
849
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000850void OMPClausePrinter::VisitOMPAlignedClause(OMPAlignedClause *Node) {
851 if (!Node->varlist_empty()) {
852 OS << "aligned";
853 VisitOMPClauseList(Node, '(');
854 if (Node->getAlignment() != nullptr) {
855 OS << ": ";
856 Node->getAlignment()->printPretty(OS, nullptr, Policy, 0);
857 }
858 OS << ")";
859 }
860}
861
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000862void OMPClausePrinter::VisitOMPCopyinClause(OMPCopyinClause *Node) {
863 if (!Node->varlist_empty()) {
864 OS << "copyin";
865 VisitOMPClauseList(Node, '(');
866 OS << ")";
867 }
868}
869
Alexey Bataevbae9a792014-06-27 10:37:06 +0000870void OMPClausePrinter::VisitOMPCopyprivateClause(OMPCopyprivateClause *Node) {
871 if (!Node->varlist_empty()) {
872 OS << "copyprivate";
873 VisitOMPClauseList(Node, '(');
874 OS << ")";
875 }
876}
877
Alexey Bataev6125da92014-07-21 11:26:11 +0000878void OMPClausePrinter::VisitOMPFlushClause(OMPFlushClause *Node) {
879 if (!Node->varlist_empty()) {
880 VisitOMPClauseList(Node, '(');
881 OS << ")";
882 }
883}
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000884
885void OMPClausePrinter::VisitOMPDependClause(OMPDependClause *Node) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000886 OS << "depend(";
887 OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(),
888 Node->getDependencyKind());
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000889 if (!Node->varlist_empty()) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000890 OS << " :";
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000891 VisitOMPClauseList(Node, ' ');
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000892 }
Alexey Bataeveb482352015-12-18 05:05:56 +0000893 OS << ")";
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000894}
Kelvin Li0bff7af2015-11-23 05:32:03 +0000895
896void OMPClausePrinter::VisitOMPMapClause(OMPMapClause *Node) {
897 if (!Node->varlist_empty()) {
898 OS << "map(";
899 if (Node->getMapType() != OMPC_MAP_unknown) {
900 if (Node->getMapTypeModifier() != OMPC_MAP_unknown) {
901 OS << getOpenMPSimpleClauseTypeName(OMPC_map,
902 Node->getMapTypeModifier());
903 OS << ',';
904 }
905 OS << getOpenMPSimpleClauseTypeName(OMPC_map, Node->getMapType());
906 OS << ':';
907 }
908 VisitOMPClauseList(Node, ' ');
909 OS << ")";
910 }
911}
Carlo Bertollib4adf552016-01-15 18:50:31 +0000912
913void OMPClausePrinter::VisitOMPDistScheduleClause(OMPDistScheduleClause *Node) {
914 OS << "dist_schedule(" << getOpenMPSimpleClauseTypeName(
915 OMPC_dist_schedule, Node->getDistScheduleKind());
916 if (Node->getChunkSize()) {
917 OS << ", ";
918 Node->getChunkSize()->printPretty(OS, nullptr, Policy);
919 }
920 OS << ")";
921}
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000922}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000923
924//===----------------------------------------------------------------------===//
925// OpenMP directives printing methods
926//===----------------------------------------------------------------------===//
927
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000928void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000929 OMPClausePrinter Printer(OS, Policy);
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000930 ArrayRef<OMPClause *> Clauses = S->clauses();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000931 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
932 I != E; ++I)
933 if (*I && !(*I)->isImplicit()) {
934 Printer.Visit(*I);
935 OS << ' ';
936 }
937 OS << "\n";
Alexey Bataev68446b72014-07-18 07:47:19 +0000938 if (S->hasAssociatedStmt() && S->getAssociatedStmt()) {
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000939 assert(isa<CapturedStmt>(S->getAssociatedStmt()) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000940 "Expected captured statement!");
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000941 Stmt *CS = cast<CapturedStmt>(S->getAssociatedStmt())->getCapturedStmt();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000942 PrintStmt(CS);
943 }
944}
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000945
946void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
947 Indent() << "#pragma omp parallel ";
948 PrintOMPExecutableDirective(Node);
949}
950
951void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
952 Indent() << "#pragma omp simd ";
953 PrintOMPExecutableDirective(Node);
954}
955
Alexey Bataevf29276e2014-06-18 04:14:57 +0000956void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) {
957 Indent() << "#pragma omp for ";
958 PrintOMPExecutableDirective(Node);
959}
960
Alexander Musmanf82886e2014-09-18 05:12:34 +0000961void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) {
962 Indent() << "#pragma omp for simd ";
963 PrintOMPExecutableDirective(Node);
964}
965
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000966void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) {
967 Indent() << "#pragma omp sections ";
968 PrintOMPExecutableDirective(Node);
969}
970
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000971void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) {
972 Indent() << "#pragma omp section";
973 PrintOMPExecutableDirective(Node);
974}
975
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000976void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) {
977 Indent() << "#pragma omp single ";
978 PrintOMPExecutableDirective(Node);
979}
980
Alexander Musman80c22892014-07-17 08:54:58 +0000981void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) {
982 Indent() << "#pragma omp master";
983 PrintOMPExecutableDirective(Node);
984}
985
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000986void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) {
987 Indent() << "#pragma omp critical";
988 if (Node->getDirectiveName().getName()) {
989 OS << " (";
990 Node->getDirectiveName().printName(OS);
991 OS << ")";
992 }
Alexey Bataev28c75412015-12-15 08:19:24 +0000993 OS << " ";
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000994 PrintOMPExecutableDirective(Node);
995}
996
Alexey Bataev4acb8592014-07-07 13:01:15 +0000997void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) {
998 Indent() << "#pragma omp parallel for ";
999 PrintOMPExecutableDirective(Node);
1000}
1001
Alexander Musmane4e893b2014-09-23 09:33:00 +00001002void StmtPrinter::VisitOMPParallelForSimdDirective(
1003 OMPParallelForSimdDirective *Node) {
1004 Indent() << "#pragma omp parallel for simd ";
1005 PrintOMPExecutableDirective(Node);
1006}
1007
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001008void StmtPrinter::VisitOMPParallelSectionsDirective(
1009 OMPParallelSectionsDirective *Node) {
1010 Indent() << "#pragma omp parallel sections ";
1011 PrintOMPExecutableDirective(Node);
1012}
1013
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001014void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) {
1015 Indent() << "#pragma omp task ";
1016 PrintOMPExecutableDirective(Node);
1017}
1018
Alexey Bataev68446b72014-07-18 07:47:19 +00001019void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) {
1020 Indent() << "#pragma omp taskyield";
1021 PrintOMPExecutableDirective(Node);
1022}
1023
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001024void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) {
1025 Indent() << "#pragma omp barrier";
1026 PrintOMPExecutableDirective(Node);
1027}
1028
Alexey Bataev2df347a2014-07-18 10:17:07 +00001029void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) {
1030 Indent() << "#pragma omp taskwait";
1031 PrintOMPExecutableDirective(Node);
1032}
1033
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001034void StmtPrinter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective *Node) {
1035 Indent() << "#pragma omp taskgroup";
1036 PrintOMPExecutableDirective(Node);
1037}
1038
Alexey Bataev6125da92014-07-21 11:26:11 +00001039void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) {
1040 Indent() << "#pragma omp flush ";
1041 PrintOMPExecutableDirective(Node);
1042}
1043
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001044void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) {
Alexey Bataev346265e2015-09-25 10:37:12 +00001045 Indent() << "#pragma omp ordered ";
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001046 PrintOMPExecutableDirective(Node);
1047}
1048
Alexey Bataev0162e452014-07-22 10:10:35 +00001049void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) {
1050 Indent() << "#pragma omp atomic ";
1051 PrintOMPExecutableDirective(Node);
1052}
1053
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001054void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) {
1055 Indent() << "#pragma omp target ";
1056 PrintOMPExecutableDirective(Node);
1057}
1058
Michael Wong65f367f2015-07-21 13:44:28 +00001059void StmtPrinter::VisitOMPTargetDataDirective(OMPTargetDataDirective *Node) {
1060 Indent() << "#pragma omp target data ";
1061 PrintOMPExecutableDirective(Node);
1062}
1063
Samuel Antaodf67fc42016-01-19 19:15:56 +00001064void StmtPrinter::VisitOMPTargetEnterDataDirective(
1065 OMPTargetEnterDataDirective *Node) {
1066 Indent() << "#pragma omp target enter data ";
1067 PrintOMPExecutableDirective(Node);
1068}
1069
Alexey Bataev13314bf2014-10-09 04:18:56 +00001070void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) {
1071 Indent() << "#pragma omp teams ";
1072 PrintOMPExecutableDirective(Node);
1073}
1074
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001075void StmtPrinter::VisitOMPCancellationPointDirective(
1076 OMPCancellationPointDirective *Node) {
1077 Indent() << "#pragma omp cancellation point "
1078 << getOpenMPDirectiveName(Node->getCancelRegion());
1079 PrintOMPExecutableDirective(Node);
1080}
Alexey Bataev80909872015-07-02 11:25:17 +00001081
1082void StmtPrinter::VisitOMPCancelDirective(OMPCancelDirective *Node) {
1083 Indent() << "#pragma omp cancel "
Alexey Bataev87933c72015-09-18 08:07:34 +00001084 << getOpenMPDirectiveName(Node->getCancelRegion()) << " ";
Alexey Bataev80909872015-07-02 11:25:17 +00001085 PrintOMPExecutableDirective(Node);
1086}
Alexey Bataev49f6e782015-12-01 04:18:41 +00001087
1088void StmtPrinter::VisitOMPTaskLoopDirective(OMPTaskLoopDirective *Node) {
1089 Indent() << "#pragma omp taskloop ";
1090 PrintOMPExecutableDirective(Node);
1091}
1092
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001093void StmtPrinter::VisitOMPTaskLoopSimdDirective(
1094 OMPTaskLoopSimdDirective *Node) {
1095 Indent() << "#pragma omp taskloop simd ";
1096 PrintOMPExecutableDirective(Node);
1097}
1098
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001099void StmtPrinter::VisitOMPDistributeDirective(OMPDistributeDirective *Node) {
1100 Indent() << "#pragma omp distribute ";
1101 PrintOMPExecutableDirective(Node);
1102}
1103
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001104//===----------------------------------------------------------------------===//
Chris Lattner71e23ce2006-11-04 20:18:38 +00001105// Expr printing methods.
1106//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001107
Chris Lattner882f7882006-11-04 18:52:07 +00001108void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001109 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1110 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001111 if (Node->hasTemplateKeyword())
1112 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001113 OS << Node->getNameInfo();
John McCallb3774b52010-08-19 23:49:38 +00001114 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +00001115 TemplateSpecializationType::PrintTemplateArgumentList(
1116 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregorc7acfdf2009-01-06 05:10:23 +00001117}
1118
John McCall8cd78132009-11-19 22:55:06 +00001119void StmtPrinter::VisitDependentScopeDeclRefExpr(
1120 DependentScopeDeclRefExpr *Node) {
Axel Naumann20b27862011-01-24 15:44:00 +00001121 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1122 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001123 if (Node->hasTemplateKeyword())
1124 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001125 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +00001126 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +00001127 TemplateSpecializationType::PrintTemplateArgumentList(
1128 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregor90a1a652009-03-19 17:26:29 +00001129}
1130
John McCalld14a8642009-11-21 08:51:07 +00001131void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregora727cb92009-06-30 22:34:41 +00001132 if (Node->getQualifier())
1133 Node->getQualifier()->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001134 if (Node->hasTemplateKeyword())
1135 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001136 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +00001137 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +00001138 TemplateSpecializationType::PrintTemplateArgumentList(
1139 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregora727cb92009-06-30 22:34:41 +00001140}
1141
Steve Naroffe46504b2007-11-12 14:29:37 +00001142void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +00001143 if (Node->getBase()) {
1144 PrintExpr(Node->getBase());
1145 OS << (Node->isArrow() ? "->" : ".");
1146 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001147 OS << *Node->getDecl();
Steve Naroffe46504b2007-11-12 14:29:37 +00001148}
1149
Steve Naroffec944032008-05-30 00:40:33 +00001150void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Fariborz Jahanian681c0752010-10-14 16:04:05 +00001151 if (Node->isSuperReceiver())
1152 OS << "super.";
Richard Trieu6d92e502014-02-06 23:26:23 +00001153 else if (Node->isObjectReceiver() && Node->getBase()) {
Steve Naroffec944032008-05-30 00:40:33 +00001154 PrintExpr(Node->getBase());
1155 OS << ".";
Richard Trieu6d92e502014-02-06 23:26:23 +00001156 } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
1157 OS << Node->getClassReceiver()->getName() << ".";
Steve Naroffec944032008-05-30 00:40:33 +00001158 }
Fariborz Jahanian681c0752010-10-14 16:04:05 +00001159
John McCallb7bd14f2010-12-02 01:19:52 +00001160 if (Node->isImplicitProperty())
Aaron Ballmanb190f972014-01-03 17:59:55 +00001161 Node->getImplicitPropertyGetter()->getSelector().print(OS);
John McCallb7bd14f2010-12-02 01:19:52 +00001162 else
1163 OS << Node->getExplicitProperty()->getName();
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00001164}
1165
Ted Kremeneke65b0862012-03-06 20:05:56 +00001166void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
1167
1168 PrintExpr(Node->getBaseExpr());
1169 OS << "[";
1170 PrintExpr(Node->getKeyExpr());
1171 OS << "]";
1172}
1173
Chris Lattner6307f192008-08-10 01:53:14 +00001174void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Alexey Bataevec474782014-10-09 08:45:04 +00001175 OS << PredefinedExpr::getIdentTypeName(Node->getIdentType());
Anders Carlsson625bfc82007-07-21 05:21:51 +00001176}
1177
Steve Naroffae4143e2007-04-26 20:39:23 +00001178void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +00001179 unsigned value = Node->getValue();
Douglas Gregorfb65e592011-07-27 05:40:30 +00001180
1181 switch (Node->getKind()) {
1182 case CharacterLiteral::Ascii: break; // no prefix.
1183 case CharacterLiteral::Wide: OS << 'L'; break;
Aaron Ballman9a17c852016-01-07 20:59:26 +00001184 case CharacterLiteral::UTF8: OS << "u8"; break;
Douglas Gregorfb65e592011-07-27 05:40:30 +00001185 case CharacterLiteral::UTF16: OS << 'u'; break;
1186 case CharacterLiteral::UTF32: OS << 'U'; break;
1187 }
1188
Chris Lattner666115c2007-07-13 23:58:20 +00001189 switch (value) {
1190 case '\\':
1191 OS << "'\\\\'";
1192 break;
1193 case '\'':
1194 OS << "'\\''";
1195 break;
1196 case '\a':
1197 // TODO: K&R: the meaning of '\\a' is different in traditional C
1198 OS << "'\\a'";
1199 break;
1200 case '\b':
1201 OS << "'\\b'";
1202 break;
1203 // Nonstandard escape sequence.
1204 /*case '\e':
1205 OS << "'\\e'";
1206 break;*/
1207 case '\f':
1208 OS << "'\\f'";
1209 break;
1210 case '\n':
1211 OS << "'\\n'";
1212 break;
1213 case '\r':
1214 OS << "'\\r'";
1215 break;
1216 case '\t':
1217 OS << "'\\t'";
1218 break;
1219 case '\v':
1220 OS << "'\\v'";
1221 break;
1222 default:
Jordan Rose00d1b592013-02-08 22:30:27 +00001223 if (value < 256 && isPrintable((unsigned char)value))
Chris Lattner666115c2007-07-13 23:58:20 +00001224 OS << "'" << (char)value << "'";
Jordan Rose00d1b592013-02-08 22:30:27 +00001225 else if (value < 256)
1226 OS << "'\\x" << llvm::format("%02x", value) << "'";
1227 else if (value <= 0xFFFF)
1228 OS << "'\\u" << llvm::format("%04x", value) << "'";
1229 else
1230 OS << "'\\U" << llvm::format("%08x", value) << "'";
Chris Lattner6e9d9b32007-07-13 05:18:11 +00001231 }
Steve Naroffae4143e2007-04-26 20:39:23 +00001232}
1233
Steve Naroffdf7855b2007-02-21 23:46:25 +00001234void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +00001235 bool isSigned = Node->getType()->isSignedIntegerType();
1236 OS << Node->getValue().toString(10, isSigned);
Mike Stump11289f42009-09-09 15:08:12 +00001237
Chris Lattner06430412007-05-21 05:45:03 +00001238 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall9dd450b2009-09-21 23:43:11 +00001239 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
David Blaikie83d382b2011-09-23 05:06:16 +00001240 default: llvm_unreachable("Unexpected type for integer literal!");
David Majnemerbe09e8e2015-03-06 18:04:22 +00001241 case BuiltinType::Char_S:
1242 case BuiltinType::Char_U: OS << "i8"; break;
David Majnemer65a407c2014-06-21 18:46:07 +00001243 case BuiltinType::UChar: OS << "Ui8"; break;
1244 case BuiltinType::Short: OS << "i16"; break;
1245 case BuiltinType::UShort: OS << "Ui16"; break;
Chris Lattner06430412007-05-21 05:45:03 +00001246 case BuiltinType::Int: break; // no suffix.
1247 case BuiltinType::UInt: OS << 'U'; break;
1248 case BuiltinType::Long: OS << 'L'; break;
1249 case BuiltinType::ULong: OS << "UL"; break;
1250 case BuiltinType::LongLong: OS << "LL"; break;
1251 case BuiltinType::ULongLong: OS << "ULL"; break;
1252 }
Chris Lattner882f7882006-11-04 18:52:07 +00001253}
Benjamin Kramer8a526762012-09-20 14:07:17 +00001254
1255static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
1256 bool PrintSuffix) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001257 SmallString<16> Str;
Eli Friedman53392062011-10-05 20:32:03 +00001258 Node->getValue().toString(Str);
1259 OS << Str;
Benjamin Kramer8a526762012-09-20 14:07:17 +00001260 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
1261 OS << '.'; // Trailing dot in order to separate from ints.
1262
1263 if (!PrintSuffix)
1264 return;
1265
1266 // Emit suffixes. Float literals are always a builtin float type.
1267 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
1268 default: llvm_unreachable("Unexpected type for float literal!");
1269 case BuiltinType::Half: break; // FIXME: suffix?
1270 case BuiltinType::Double: break; // no suffix.
1271 case BuiltinType::Float: OS << 'F'; break;
1272 case BuiltinType::LongDouble: OS << 'L'; break;
1273 }
1274}
1275
1276void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
1277 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
Chris Lattner882f7882006-11-04 18:52:07 +00001278}
Chris Lattner1c20a172007-08-26 03:42:43 +00001279
1280void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
1281 PrintExpr(Node->getSubExpr());
1282 OS << "i";
1283}
1284
Steve Naroffdf7855b2007-02-21 23:46:25 +00001285void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Richard Trieudc355912012-06-13 20:25:24 +00001286 Str->outputString(OS);
Chris Lattner882f7882006-11-04 18:52:07 +00001287}
1288void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
1289 OS << "(";
1290 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +00001291 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +00001292}
1293void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +00001294 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +00001295 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump11289f42009-09-09 15:08:12 +00001296
Eli Friedman1cf25362009-06-14 22:39:26 +00001297 // Print a space if this is an "identifier operator" like __real, or if
1298 // it might be concatenated incorrectly like '+'.
Chris Lattnere5b60442007-08-23 21:46:40 +00001299 switch (Node->getOpcode()) {
1300 default: break;
John McCalle3027922010-08-25 11:45:40 +00001301 case UO_Real:
1302 case UO_Imag:
1303 case UO_Extension:
Chris Lattnere5b60442007-08-23 21:46:40 +00001304 OS << ' ';
1305 break;
John McCalle3027922010-08-25 11:45:40 +00001306 case UO_Plus:
1307 case UO_Minus:
Eli Friedman1cf25362009-06-14 22:39:26 +00001308 if (isa<UnaryOperator>(Node->getSubExpr()))
1309 OS << ' ';
1310 break;
Chris Lattnere5b60442007-08-23 21:46:40 +00001311 }
1312 }
Chris Lattner882f7882006-11-04 18:52:07 +00001313 PrintExpr(Node->getSubExpr());
Mike Stump11289f42009-09-09 15:08:12 +00001314
Chris Lattner15768702006-11-05 23:54:51 +00001315 if (Node->isPostfix())
1316 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +00001317}
Chris Lattner98dbf0a2007-08-30 17:59:59 +00001318
Douglas Gregor882211c2010-04-28 22:16:22 +00001319void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
1320 OS << "__builtin_offsetof(";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001321 Node->getTypeSourceInfo()->getType().print(OS, Policy);
1322 OS << ", ";
Douglas Gregor882211c2010-04-28 22:16:22 +00001323 bool PrintedSomething = false;
1324 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
James Y Knight7281c352015-12-29 22:31:18 +00001325 OffsetOfNode ON = Node->getComponent(i);
1326 if (ON.getKind() == OffsetOfNode::Array) {
Douglas Gregor882211c2010-04-28 22:16:22 +00001327 // Array node
1328 OS << "[";
1329 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
1330 OS << "]";
1331 PrintedSomething = true;
1332 continue;
1333 }
Douglas Gregord1702062010-04-29 00:18:15 +00001334
1335 // Skip implicit base indirections.
James Y Knight7281c352015-12-29 22:31:18 +00001336 if (ON.getKind() == OffsetOfNode::Base)
Douglas Gregord1702062010-04-29 00:18:15 +00001337 continue;
1338
Douglas Gregor882211c2010-04-28 22:16:22 +00001339 // Field or identifier node.
1340 IdentifierInfo *Id = ON.getFieldName();
1341 if (!Id)
1342 continue;
Alexis Hunta8136cc2010-05-05 15:23:54 +00001343
Douglas Gregor882211c2010-04-28 22:16:22 +00001344 if (PrintedSomething)
1345 OS << ".";
1346 else
1347 PrintedSomething = true;
Alexis Hunta8136cc2010-05-05 15:23:54 +00001348 OS << Id->getName();
Douglas Gregor882211c2010-04-28 22:16:22 +00001349 }
1350 OS << ")";
1351}
1352
Peter Collingbournee190dee2011-03-11 19:24:49 +00001353void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
1354 switch(Node->getKind()) {
1355 case UETT_SizeOf:
1356 OS << "sizeof";
1357 break;
1358 case UETT_AlignOf:
Jordan Rose58d54722012-06-30 21:33:57 +00001359 if (Policy.LangOpts.CPlusPlus)
1360 OS << "alignof";
1361 else if (Policy.LangOpts.C11)
1362 OS << "_Alignof";
1363 else
1364 OS << "__alignof";
Peter Collingbournee190dee2011-03-11 19:24:49 +00001365 break;
1366 case UETT_VecStep:
1367 OS << "vec_step";
1368 break;
Alexey Bataev00396512015-07-02 03:40:19 +00001369 case UETT_OpenMPRequiredSimdAlign:
1370 OS << "__builtin_omp_required_simd_align";
1371 break;
Peter Collingbournee190dee2011-03-11 19:24:49 +00001372 }
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001373 if (Node->isArgumentType()) {
1374 OS << '(';
1375 Node->getArgumentType().print(OS, Policy);
1376 OS << ')';
1377 } else {
Sebastian Redl6f282892008-11-11 17:56:53 +00001378 OS << " ";
1379 PrintExpr(Node->getArgumentExpr());
1380 }
Chris Lattner882f7882006-11-04 18:52:07 +00001381}
Peter Collingbourne91147592011-04-15 00:35:48 +00001382
1383void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
1384 OS << "_Generic(";
1385 PrintExpr(Node->getControllingExpr());
1386 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
1387 OS << ", ";
1388 QualType T = Node->getAssocType(i);
1389 if (T.isNull())
1390 OS << "default";
1391 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001392 T.print(OS, Policy);
Peter Collingbourne91147592011-04-15 00:35:48 +00001393 OS << ": ";
1394 PrintExpr(Node->getAssocExpr(i));
1395 }
1396 OS << ")";
1397}
1398
Chris Lattner882f7882006-11-04 18:52:07 +00001399void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +00001400 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +00001401 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +00001402 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +00001403 OS << "]";
1404}
1405
Alexey Bataev1a3320e2015-08-25 14:24:04 +00001406void StmtPrinter::VisitOMPArraySectionExpr(OMPArraySectionExpr *Node) {
1407 PrintExpr(Node->getBase());
1408 OS << "[";
1409 if (Node->getLowerBound())
1410 PrintExpr(Node->getLowerBound());
Alexey Bataeved5fb672015-09-11 04:54:28 +00001411 if (Node->getColonLoc().isValid()) {
1412 OS << ":";
1413 if (Node->getLength())
1414 PrintExpr(Node->getLength());
1415 }
Alexey Bataev1a3320e2015-08-25 14:24:04 +00001416 OS << "]";
1417}
1418
Peter Collingbourne4b279a02011-02-08 21:17:54 +00001419void StmtPrinter::PrintCallArgs(CallExpr *Call) {
Chris Lattner882f7882006-11-04 18:52:07 +00001420 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001421 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1422 // Don't print any defaulted arguments
1423 break;
1424 }
1425
Chris Lattner882f7882006-11-04 18:52:07 +00001426 if (i) OS << ", ";
1427 PrintExpr(Call->getArg(i));
1428 }
Peter Collingbourne4b279a02011-02-08 21:17:54 +00001429}
1430
1431void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1432 PrintExpr(Call->getCallee());
1433 OS << "(";
1434 PrintCallArgs(Call);
Chris Lattner882f7882006-11-04 18:52:07 +00001435 OS << ")";
1436}
1437void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregore4b414c2009-01-08 22:45:41 +00001438 // FIXME: Suppress printing implicit bases (like "this")
1439 PrintExpr(Node->getBase());
David Blaikie8fab8e52012-11-12 19:12:12 +00001440
1441 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
David Blaikie6bffd6f2012-11-12 19:32:32 +00001442 FieldDecl *ParentDecl = ParentMember
Craig Topper36250ad2014-05-12 05:36:57 +00001443 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : nullptr;
David Blaikie8fab8e52012-11-12 19:12:12 +00001444
David Blaikie6bffd6f2012-11-12 19:32:32 +00001445 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
David Blaikie8fab8e52012-11-12 19:12:12 +00001446 OS << (Node->isArrow() ? "->" : ".");
David Blaikie8fab8e52012-11-12 19:12:12 +00001447
Fariborz Jahanian2990c022010-01-11 21:17:32 +00001448 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1449 if (FD->isAnonymousStructOrUnion())
1450 return;
David Blaikie8fab8e52012-11-12 19:12:12 +00001451
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001452 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1453 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001454 if (Node->hasTemplateKeyword())
1455 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001456 OS << Node->getMemberNameInfo();
John McCallb3774b52010-08-19 23:49:38 +00001457 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +00001458 TemplateSpecializationType::PrintTemplateArgumentList(
1459 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001460}
Steve Naroffe87026a2009-07-24 17:54:45 +00001461void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1462 PrintExpr(Node->getBase());
1463 OS << (Node->isArrow() ? "->isa" : ".isa");
1464}
1465
Nate Begemance4d7fc2008-04-18 23:10:10 +00001466void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +00001467 PrintExpr(Node->getBase());
1468 OS << ".";
1469 OS << Node->getAccessor().getName();
1470}
Douglas Gregorf19b2312008-10-28 15:36:24 +00001471void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001472 OS << '(';
1473 Node->getTypeAsWritten().print(OS, Policy);
1474 OS << ')';
Chris Lattner882f7882006-11-04 18:52:07 +00001475 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001476}
Steve Naroff57eb2c52007-07-19 21:32:11 +00001477void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001478 OS << '(';
1479 Node->getType().print(OS, Policy);
1480 OS << ')';
Steve Naroff57eb2c52007-07-19 21:32:11 +00001481 PrintExpr(Node->getInitializer());
1482}
Steve Naroff7a5af782007-07-13 16:58:59 +00001483void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Alp Toker028ed912013-12-06 17:56:43 +00001484 // No need to print anything, simply forward to the subexpression.
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001485 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +00001486}
Chris Lattner882f7882006-11-04 18:52:07 +00001487void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1488 PrintExpr(Node->getLHS());
1489 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1490 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001491}
Chris Lattner86928112007-08-25 02:00:02 +00001492void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1493 PrintExpr(Node->getLHS());
1494 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1495 PrintExpr(Node->getRHS());
1496}
Chris Lattner882f7882006-11-04 18:52:07 +00001497void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1498 PrintExpr(Node->getCond());
John McCallc07a0c72011-02-17 10:25:35 +00001499 OS << " ? ";
1500 PrintExpr(Node->getLHS());
1501 OS << " : ";
Chris Lattner882f7882006-11-04 18:52:07 +00001502 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001503}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001504
Chris Lattnereefa10e2007-05-28 06:56:27 +00001505// GNU extensions.
1506
John McCallc07a0c72011-02-17 10:25:35 +00001507void
1508StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1509 PrintExpr(Node->getCommon());
1510 OS << " ?: ";
1511 PrintExpr(Node->getFalseExpr());
1512}
Chris Lattnerd268a7a2007-08-03 17:31:20 +00001513void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +00001514 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +00001515}
1516
Chris Lattner366727f2007-07-24 16:58:17 +00001517void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1518 OS << "(";
1519 PrintRawCompoundStmt(E->getSubStmt());
1520 OS << ")";
1521}
1522
Steve Naroff9efdabc2007-08-03 21:21:27 +00001523void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1524 OS << "__builtin_choose_expr(";
1525 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +00001526 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +00001527 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +00001528 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +00001529 PrintExpr(Node->getRHS());
1530 OS << ")";
1531}
Chris Lattner366727f2007-07-24 16:58:17 +00001532
Douglas Gregor3be4b122008-11-29 04:51:27 +00001533void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1534 OS << "__null";
1535}
1536
Eli Friedmana1b4ed82008-05-14 19:38:39 +00001537void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1538 OS << "__builtin_shufflevector(";
1539 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1540 if (i) OS << ", ";
1541 PrintExpr(Node->getExpr(i));
1542 }
1543 OS << ")";
1544}
1545
Hal Finkelc4d7c822013-09-18 03:29:45 +00001546void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1547 OS << "__builtin_convertvector(";
1548 PrintExpr(Node->getSrcExpr());
1549 OS << ", ";
1550 Node->getType().print(OS, Policy);
1551 OS << ")";
1552}
1553
Anders Carlsson4692db02007-08-31 04:56:16 +00001554void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001555 if (Node->getSyntacticForm()) {
1556 Visit(Node->getSyntacticForm());
1557 return;
1558 }
1559
Richard Smithd0e102f2015-01-30 02:04:26 +00001560 OS << "{";
Anders Carlsson4692db02007-08-31 04:56:16 +00001561 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1562 if (i) OS << ", ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001563 if (Node->getInit(i))
1564 PrintExpr(Node->getInit(i));
1565 else
Richard Smithd0e102f2015-01-30 02:04:26 +00001566 OS << "{}";
Anders Carlsson4692db02007-08-31 04:56:16 +00001567 }
Richard Smithd0e102f2015-01-30 02:04:26 +00001568 OS << "}";
Anders Carlsson4692db02007-08-31 04:56:16 +00001569}
1570
Nate Begeman5ec4b312009-08-10 23:49:36 +00001571void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
Richard Smithd0e102f2015-01-30 02:04:26 +00001572 OS << "(";
Nate Begeman5ec4b312009-08-10 23:49:36 +00001573 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1574 if (i) OS << ", ";
1575 PrintExpr(Node->getExpr(i));
1576 }
Richard Smithd0e102f2015-01-30 02:04:26 +00001577 OS << ")";
Nate Begeman5ec4b312009-08-10 23:49:36 +00001578}
1579
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001580void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Justin Bognercb337032015-05-28 22:19:36 +00001581 bool NeedsEquals = true;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001582 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
1583 DEnd = Node->designators_end();
1584 D != DEnd; ++D) {
1585 if (D->isFieldDesignator()) {
Serge Pavloveb57aa62014-06-20 17:08:28 +00001586 if (D->getDotLoc().isInvalid()) {
Justin Bognercb337032015-05-28 22:19:36 +00001587 if (IdentifierInfo *II = D->getFieldName()) {
Serge Pavloveb57aa62014-06-20 17:08:28 +00001588 OS << II->getName() << ":";
Justin Bognercb337032015-05-28 22:19:36 +00001589 NeedsEquals = false;
1590 }
Serge Pavloveb57aa62014-06-20 17:08:28 +00001591 } else {
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001592 OS << "." << D->getFieldName()->getName();
Serge Pavloveb57aa62014-06-20 17:08:28 +00001593 }
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001594 } else {
1595 OS << "[";
1596 if (D->isArrayDesignator()) {
1597 PrintExpr(Node->getArrayIndex(*D));
1598 } else {
1599 PrintExpr(Node->getArrayRangeStart(*D));
1600 OS << " ... ";
Mike Stump11289f42009-09-09 15:08:12 +00001601 PrintExpr(Node->getArrayRangeEnd(*D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001602 }
1603 OS << "]";
1604 }
1605 }
1606
Justin Bognercb337032015-05-28 22:19:36 +00001607 if (NeedsEquals)
1608 OS << " = ";
1609 else
1610 OS << " ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001611 PrintExpr(Node->getInit());
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001612}
1613
Yunzhong Gaocb779302015-06-10 00:27:52 +00001614void StmtPrinter::VisitDesignatedInitUpdateExpr(
1615 DesignatedInitUpdateExpr *Node) {
1616 OS << "{";
1617 OS << "/*base*/";
1618 PrintExpr(Node->getBase());
1619 OS << ", ";
1620
1621 OS << "/*updater*/";
1622 PrintExpr(Node->getUpdater());
1623 OS << "}";
1624}
1625
1626void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) {
1627 OS << "/*no init*/";
1628}
1629
Douglas Gregor0202cb42009-01-29 17:44:32 +00001630void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001631 if (Policy.LangOpts.CPlusPlus) {
1632 OS << "/*implicit*/";
1633 Node->getType().print(OS, Policy);
1634 OS << "()";
1635 } else {
1636 OS << "/*implicit*/(";
1637 Node->getType().print(OS, Policy);
1638 OS << ')';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001639 if (Node->getType()->isRecordType())
1640 OS << "{}";
1641 else
1642 OS << 0;
1643 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001644}
1645
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001646void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman79635842009-05-30 04:20:30 +00001647 OS << "__builtin_va_arg(";
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001648 PrintExpr(Node->getSubExpr());
1649 OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001650 Node->getType().print(OS, Policy);
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001651 OS << ")";
1652}
1653
John McCallfe96e0b2011-11-06 09:01:30 +00001654void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1655 PrintExpr(Node->getSyntacticForm());
1656}
1657
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001658void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
Craig Topper36250ad2014-05-12 05:36:57 +00001659 const char *Name = nullptr;
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001660 switch (Node->getOp()) {
Richard Smithfeea8832012-04-12 05:08:17 +00001661#define BUILTIN(ID, TYPE, ATTRS)
1662#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1663 case AtomicExpr::AO ## ID: \
1664 Name = #ID "("; \
1665 break;
1666#include "clang/Basic/Builtins.def"
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001667 }
1668 OS << Name;
Richard Smithfeea8832012-04-12 05:08:17 +00001669
1670 // AtomicExpr stores its subexpressions in a permuted order.
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001671 PrintExpr(Node->getPtr());
Richard Smithfeea8832012-04-12 05:08:17 +00001672 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1673 Node->getOp() != AtomicExpr::AO__atomic_load_n) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001674 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001675 PrintExpr(Node->getVal1());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001676 }
Richard Smithfeea8832012-04-12 05:08:17 +00001677 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1678 Node->isCmpXChg()) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001679 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001680 PrintExpr(Node->getVal2());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001681 }
Richard Smithfeea8832012-04-12 05:08:17 +00001682 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1683 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
Richard Smithfeea8832012-04-12 05:08:17 +00001684 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001685 PrintExpr(Node->getWeak());
Richard Smithfeea8832012-04-12 05:08:17 +00001686 }
Richard Smithdf6bee82013-05-01 19:02:43 +00001687 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) {
1688 OS << ", ";
David Chisnallfa35df62012-01-16 17:27:18 +00001689 PrintExpr(Node->getOrder());
Richard Smithdf6bee82013-05-01 19:02:43 +00001690 }
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001691 if (Node->isCmpXChg()) {
1692 OS << ", ";
1693 PrintExpr(Node->getOrderFail());
1694 }
1695 OS << ")";
1696}
1697
Chris Lattnereefa10e2007-05-28 06:56:27 +00001698// C++
Douglas Gregor993603d2008-11-14 16:09:21 +00001699void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1700 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1701 "",
1702#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1703 Spelling,
1704#include "clang/Basic/OperatorKinds.def"
1705 };
1706
1707 OverloadedOperatorKind Kind = Node->getOperator();
1708 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1709 if (Node->getNumArgs() == 1) {
1710 OS << OpStrings[Kind] << ' ';
1711 PrintExpr(Node->getArg(0));
1712 } else {
1713 PrintExpr(Node->getArg(0));
1714 OS << ' ' << OpStrings[Kind];
1715 }
Eli Friedman8e236422012-10-12 22:45:14 +00001716 } else if (Kind == OO_Arrow) {
1717 PrintExpr(Node->getArg(0));
Douglas Gregor993603d2008-11-14 16:09:21 +00001718 } else if (Kind == OO_Call) {
1719 PrintExpr(Node->getArg(0));
1720 OS << '(';
1721 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1722 if (ArgIdx > 1)
1723 OS << ", ";
1724 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1725 PrintExpr(Node->getArg(ArgIdx));
1726 }
1727 OS << ')';
1728 } else if (Kind == OO_Subscript) {
1729 PrintExpr(Node->getArg(0));
1730 OS << '[';
1731 PrintExpr(Node->getArg(1));
1732 OS << ']';
1733 } else if (Node->getNumArgs() == 1) {
1734 OS << OpStrings[Kind] << ' ';
1735 PrintExpr(Node->getArg(0));
1736 } else if (Node->getNumArgs() == 2) {
1737 PrintExpr(Node->getArg(0));
1738 OS << ' ' << OpStrings[Kind] << ' ';
1739 PrintExpr(Node->getArg(1));
1740 } else {
David Blaikie83d382b2011-09-23 05:06:16 +00001741 llvm_unreachable("unknown overloaded operator");
Douglas Gregor993603d2008-11-14 16:09:21 +00001742 }
1743}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001744
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001745void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
Benjamin Kramer00e8a192014-02-25 18:03:55 +00001746 // If we have a conversion operator call only print the argument.
1747 CXXMethodDecl *MD = Node->getMethodDecl();
1748 if (MD && isa<CXXConversionDecl>(MD)) {
1749 PrintExpr(Node->getImplicitObjectArgument());
1750 return;
1751 }
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001752 VisitCallExpr(cast<CallExpr>(Node));
1753}
1754
Peter Collingbourne41f85462011-02-09 21:07:24 +00001755void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1756 PrintExpr(Node->getCallee());
1757 OS << "<<<";
1758 PrintCallArgs(Node->getConfig());
1759 OS << ">>>(";
1760 PrintCallArgs(Node);
1761 OS << ")";
1762}
1763
Douglas Gregore200adc2008-10-27 19:41:14 +00001764void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1765 OS << Node->getCastName() << '<';
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001766 Node->getTypeAsWritten().print(OS, Policy);
1767 OS << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +00001768 PrintExpr(Node->getSubExpr());
1769 OS << ")";
1770}
1771
Douglas Gregore200adc2008-10-27 19:41:14 +00001772void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1773 VisitCXXNamedCastExpr(Node);
1774}
1775
1776void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1777 VisitCXXNamedCastExpr(Node);
1778}
1779
1780void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1781 VisitCXXNamedCastExpr(Node);
1782}
1783
1784void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1785 VisitCXXNamedCastExpr(Node);
1786}
1787
Sebastian Redlc4704762008-11-11 11:37:55 +00001788void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1789 OS << "typeid(";
1790 if (Node->isTypeOperand()) {
David Majnemer143c55e2013-09-27 07:04:31 +00001791 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
Sebastian Redlc4704762008-11-11 11:37:55 +00001792 } else {
1793 PrintExpr(Node->getExprOperand());
1794 }
1795 OS << ")";
1796}
1797
Francois Pichet9f4f2072010-09-08 12:20:18 +00001798void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1799 OS << "__uuidof(";
1800 if (Node->isTypeOperand()) {
David Majnemer143c55e2013-09-27 07:04:31 +00001801 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
Francois Pichet9f4f2072010-09-08 12:20:18 +00001802 } else {
1803 PrintExpr(Node->getExprOperand());
1804 }
1805 OS << ")";
1806}
1807
John McCall5e77d762013-04-16 07:28:30 +00001808void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1809 PrintExpr(Node->getBaseExpr());
1810 if (Node->isArrow())
1811 OS << "->";
1812 else
1813 OS << ".";
1814 if (NestedNameSpecifier *Qualifier =
1815 Node->getQualifierLoc().getNestedNameSpecifier())
1816 Qualifier->print(OS, Policy);
1817 OS << Node->getPropertyDecl()->getDeclName();
1818}
1819
Alexey Bataevf7630272015-11-25 12:01:00 +00001820void StmtPrinter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr *Node) {
1821 PrintExpr(Node->getBase());
1822 OS << "[";
1823 PrintExpr(Node->getIdx());
1824 OS << "]";
1825}
1826
Richard Smithc67fdd42012-03-07 08:35:16 +00001827void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1828 switch (Node->getLiteralOperatorKind()) {
1829 case UserDefinedLiteral::LOK_Raw:
Richard Smith29e95952012-03-09 10:10:02 +00001830 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
Richard Smithc67fdd42012-03-07 08:35:16 +00001831 break;
1832 case UserDefinedLiteral::LOK_Template: {
Richard Smith29e95952012-03-09 10:10:02 +00001833 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1834 const TemplateArgumentList *Args =
1835 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1836 assert(Args);
David Majnemer314b72f2015-04-05 05:32:54 +00001837
1838 if (Args->size() != 1) {
Richard Smithe87aeb32015-10-08 00:17:59 +00001839 OS << "operator\"\"" << Node->getUDSuffix()->getName();
David Majnemer314b72f2015-04-05 05:32:54 +00001840 TemplateSpecializationType::PrintTemplateArgumentList(
1841 OS, Args->data(), Args->size(), Policy);
1842 OS << "()";
1843 return;
1844 }
1845
Richard Smith29e95952012-03-09 10:10:02 +00001846 const TemplateArgument &Pack = Args->get(0);
Aaron Ballman2a89e852014-07-15 21:32:31 +00001847 for (const auto &P : Pack.pack_elements()) {
1848 char C = (char)P.getAsIntegral().getZExtValue();
Richard Smithc67fdd42012-03-07 08:35:16 +00001849 OS << C;
1850 }
1851 break;
1852 }
Richard Smith75025ba2012-03-08 09:02:38 +00001853 case UserDefinedLiteral::LOK_Integer: {
1854 // Print integer literal without suffix.
1855 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1856 OS << Int->getValue().toString(10, /*isSigned*/false);
1857 break;
1858 }
Benjamin Kramer8a526762012-09-20 14:07:17 +00001859 case UserDefinedLiteral::LOK_Floating: {
1860 // Print floating literal without suffix.
1861 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1862 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1863 break;
1864 }
Richard Smithc67fdd42012-03-07 08:35:16 +00001865 case UserDefinedLiteral::LOK_String:
1866 case UserDefinedLiteral::LOK_Character:
1867 PrintExpr(Node->getCookedLiteral());
1868 break;
1869 }
1870 OS << Node->getUDSuffix()->getName();
1871}
1872
Chris Lattnereefa10e2007-05-28 06:56:27 +00001873void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1874 OS << (Node->getValue() ? "true" : "false");
1875}
1876
Sebastian Redl576fd422009-05-10 18:38:11 +00001877void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1878 OS << "nullptr";
1879}
1880
Douglas Gregor97a9c812008-11-04 14:32:21 +00001881void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1882 OS << "this";
1883}
1884
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001885void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
Craig Topper36250ad2014-05-12 05:36:57 +00001886 if (!Node->getSubExpr())
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001887 OS << "throw";
1888 else {
1889 OS << "throw ";
1890 PrintExpr(Node->getSubExpr());
1891 }
1892}
1893
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001894void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
Richard Smith852c9db2013-04-20 22:23:05 +00001895 // Nothing to print: we picked up the default argument.
1896}
1897
1898void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1899 // Nothing to print: we picked up the default initializer.
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001900}
1901
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001902void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001903 Node->getType().print(OS, Policy);
Richard Smith1ae689c2015-01-28 22:06:01 +00001904 // If there are no parens, this is list-initialization, and the braces are
1905 // part of the syntax of the inner construct.
1906 if (Node->getLParenLoc().isValid())
1907 OS << "(";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001908 PrintExpr(Node->getSubExpr());
Richard Smith1ae689c2015-01-28 22:06:01 +00001909 if (Node->getLParenLoc().isValid())
1910 OS << ")";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001911}
1912
Anders Carlsson993a4b32009-05-30 20:03:25 +00001913void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1914 PrintExpr(Node->getSubExpr());
1915}
1916
Douglas Gregordd04d332009-01-16 18:33:17 +00001917void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001918 Node->getType().print(OS, Policy);
Richard Smithed83ebd2015-02-05 07:02:11 +00001919 if (Node->isStdInitListInitialization())
1920 /* Nothing to do; braces are part of creating the std::initializer_list. */;
1921 else if (Node->isListInitialization())
Richard Smith1ae689c2015-01-28 22:06:01 +00001922 OS << "{";
1923 else
1924 OS << "(";
Douglas Gregordd04d332009-01-16 18:33:17 +00001925 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001926 ArgEnd = Node->arg_end();
Douglas Gregordd04d332009-01-16 18:33:17 +00001927 Arg != ArgEnd; ++Arg) {
Benjamin Kramerf48ee442015-07-18 14:35:53 +00001928 if ((*Arg)->isDefaultArgument())
Rafael Espindola6f6f3c42013-05-24 16:11:44 +00001929 break;
Douglas Gregordd04d332009-01-16 18:33:17 +00001930 if (Arg != Node->arg_begin())
1931 OS << ", ";
1932 PrintExpr(*Arg);
1933 }
Richard Smithed83ebd2015-02-05 07:02:11 +00001934 if (Node->isStdInitListInitialization())
1935 /* See above. */;
1936 else if (Node->isListInitialization())
Richard Smith1ae689c2015-01-28 22:06:01 +00001937 OS << "}";
1938 else
1939 OS << ")";
Douglas Gregordd04d332009-01-16 18:33:17 +00001940}
1941
Douglas Gregore31e6062012-02-07 10:09:13 +00001942void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1943 OS << '[';
1944 bool NeedComma = false;
1945 switch (Node->getCaptureDefault()) {
1946 case LCD_None:
1947 break;
1948
1949 case LCD_ByCopy:
1950 OS << '=';
1951 NeedComma = true;
1952 break;
1953
1954 case LCD_ByRef:
1955 OS << '&';
1956 NeedComma = true;
1957 break;
1958 }
1959 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1960 CEnd = Node->explicit_capture_end();
1961 C != CEnd;
1962 ++C) {
1963 if (NeedComma)
1964 OS << ", ";
1965 NeedComma = true;
1966
1967 switch (C->getCaptureKind()) {
1968 case LCK_This:
1969 OS << "this";
1970 break;
1971
1972 case LCK_ByRef:
James Dennettdd2ffea22015-05-07 18:48:18 +00001973 if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C))
Douglas Gregore31e6062012-02-07 10:09:13 +00001974 OS << '&';
1975 OS << C->getCapturedVar()->getName();
1976 break;
1977
1978 case LCK_ByCopy:
Douglas Gregore31e6062012-02-07 10:09:13 +00001979 OS << C->getCapturedVar()->getName();
1980 break;
Alexey Bataev39c81e22014-08-28 04:28:19 +00001981 case LCK_VLAType:
1982 llvm_unreachable("VLA type in explicit captures.");
Douglas Gregore31e6062012-02-07 10:09:13 +00001983 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00001984
James Dennettdd2ffea22015-05-07 18:48:18 +00001985 if (Node->isInitCapture(C))
Richard Smithbb13c9a2013-09-28 04:02:39 +00001986 PrintExpr(C->getCapturedVar()->getInit());
Douglas Gregore31e6062012-02-07 10:09:13 +00001987 }
1988 OS << ']';
1989
1990 if (Node->hasExplicitParameters()) {
1991 OS << " (";
1992 CXXMethodDecl *Method = Node->getCallOperator();
1993 NeedComma = false;
Aaron Ballman43b68be2014-03-07 17:50:17 +00001994 for (auto P : Method->params()) {
Douglas Gregore31e6062012-02-07 10:09:13 +00001995 if (NeedComma) {
1996 OS << ", ";
1997 } else {
1998 NeedComma = true;
1999 }
Aaron Ballman43b68be2014-03-07 17:50:17 +00002000 std::string ParamStr = P->getNameAsString();
2001 P->getOriginalType().print(OS, Policy, ParamStr);
Douglas Gregore31e6062012-02-07 10:09:13 +00002002 }
2003 if (Method->isVariadic()) {
2004 if (NeedComma)
2005 OS << ", ";
2006 OS << "...";
2007 }
2008 OS << ')';
2009
2010 if (Node->isMutable())
2011 OS << " mutable";
2012
2013 const FunctionProtoType *Proto
2014 = Method->getType()->getAs<FunctionProtoType>();
Benjamin Kramer9170e912013-02-22 15:46:01 +00002015 Proto->printExceptionSpecification(OS, Policy);
Douglas Gregore31e6062012-02-07 10:09:13 +00002016
Bill Wendling44426052012-12-20 19:22:21 +00002017 // FIXME: Attributes
Douglas Gregore31e6062012-02-07 10:09:13 +00002018
Douglas Gregor0c46b2b2012-02-13 22:00:16 +00002019 // Print the trailing return type if it was specified in the source.
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002020 if (Node->hasExplicitResultType()) {
2021 OS << " -> ";
Alp Toker314cc812014-01-25 16:55:45 +00002022 Proto->getReturnType().print(OS, Policy);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002023 }
Douglas Gregore31e6062012-02-07 10:09:13 +00002024 }
2025
2026 // Print the body.
2027 CompoundStmt *Body = Node->getBody();
2028 OS << ' ';
2029 PrintStmt(Body);
2030}
2031
Douglas Gregor747eb782010-07-08 06:14:04 +00002032void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregor2b88c112010-09-08 00:15:04 +00002033 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002034 TSInfo->getType().print(OS, Policy);
Douglas Gregor2b88c112010-09-08 00:15:04 +00002035 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002036 Node->getType().print(OS, Policy);
2037 OS << "()";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002038}
2039
Sebastian Redlbd150f42008-11-21 19:14:01 +00002040void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
2041 if (E->isGlobalNew())
2042 OS << "::";
2043 OS << "new ";
2044 unsigned NumPlace = E->getNumPlacementArgs();
Eli Friedmana6fdfaa2012-10-18 20:54:37 +00002045 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002046 OS << "(";
2047 PrintExpr(E->getPlacementArg(0));
2048 for (unsigned i = 1; i < NumPlace; ++i) {
Eli Friedmana6fdfaa2012-10-18 20:54:37 +00002049 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
2050 break;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002051 OS << ", ";
2052 PrintExpr(E->getPlacementArg(i));
2053 }
2054 OS << ") ";
2055 }
2056 if (E->isParenTypeId())
2057 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00002058 std::string TypeS;
2059 if (Expr *Size = E->getArraySize()) {
2060 llvm::raw_string_ostream s(TypeS);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002061 s << '[';
Richard Smith235341b2012-08-16 03:56:14 +00002062 Size->printPretty(s, Helper, Policy);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002063 s << ']';
Sebastian Redld6d55ee2008-12-02 22:08:59 +00002064 }
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002065 E->getAllocatedType().print(OS, Policy, TypeS);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002066 if (E->isParenTypeId())
2067 OS << ")";
2068
Sebastian Redl6047f072012-02-16 12:22:20 +00002069 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
2070 if (InitStyle) {
2071 if (InitStyle == CXXNewExpr::CallInit)
2072 OS << "(";
2073 PrintExpr(E->getInitializer());
2074 if (InitStyle == CXXNewExpr::CallInit)
2075 OS << ")";
Sebastian Redlbd150f42008-11-21 19:14:01 +00002076 }
2077}
2078
2079void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
2080 if (E->isGlobalDelete())
2081 OS << "::";
2082 OS << "delete ";
2083 if (E->isArrayForm())
2084 OS << "[] ";
2085 PrintExpr(E->getArgument());
2086}
2087
Douglas Gregorad8a3362009-09-04 17:36:40 +00002088void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
2089 PrintExpr(E->getBase());
2090 if (E->isArrow())
2091 OS << "->";
2092 else
2093 OS << '.';
2094 if (E->getQualifier())
2095 E->getQualifier()->print(OS, Policy);
Eli Friedman9cc8ac52012-10-23 20:26:57 +00002096 OS << "~";
Mike Stump11289f42009-09-09 15:08:12 +00002097
Douglas Gregor678f90d2010-02-25 01:56:36 +00002098 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
2099 OS << II->getName();
2100 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002101 E->getDestroyedType().print(OS, Policy);
Douglas Gregorad8a3362009-09-04 17:36:40 +00002102}
2103
Anders Carlsson0781ce72009-04-23 02:32:43 +00002104void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Richard Smithed83ebd2015-02-05 07:02:11 +00002105 if (E->isListInitialization() && !E->isStdInitListInitialization())
Richard Smithd0e102f2015-01-30 02:04:26 +00002106 OS << "{";
Richard Smithd59b8322012-12-19 01:39:02 +00002107
Douglas Gregorbaba85d2011-01-24 17:25:03 +00002108 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
2109 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
2110 // Don't print any defaulted arguments
2111 break;
2112 }
2113
2114 if (i) OS << ", ";
2115 PrintExpr(E->getArg(i));
Fariborz Jahaniand0bbf662010-01-13 21:41:11 +00002116 }
Richard Smithd59b8322012-12-19 01:39:02 +00002117
Richard Smithed83ebd2015-02-05 07:02:11 +00002118 if (E->isListInitialization() && !E->isStdInitListInitialization())
Richard Smithd0e102f2015-01-30 02:04:26 +00002119 OS << "}";
Anders Carlsson0781ce72009-04-23 02:32:43 +00002120}
2121
Richard Smithcc1b96d2013-06-12 22:31:48 +00002122void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
2123 PrintExpr(E->getSubExpr());
2124}
2125
John McCall5d413782010-12-06 08:20:24 +00002126void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Alp Toker028ed912013-12-06 17:56:43 +00002127 // Just forward to the subexpression.
Anders Carlssondefc6442009-04-24 22:47:04 +00002128 PrintExpr(E->getSubExpr());
2129}
2130
Mike Stump11289f42009-09-09 15:08:12 +00002131void
Douglas Gregorce934142009-05-20 18:46:25 +00002132StmtPrinter::VisitCXXUnresolvedConstructExpr(
2133 CXXUnresolvedConstructExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002134 Node->getTypeAsWritten().print(OS, Policy);
Douglas Gregorce934142009-05-20 18:46:25 +00002135 OS << "(";
2136 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00002137 ArgEnd = Node->arg_end();
Douglas Gregorce934142009-05-20 18:46:25 +00002138 Arg != ArgEnd; ++Arg) {
2139 if (Arg != Node->arg_begin())
2140 OS << ", ";
2141 PrintExpr(*Arg);
2142 }
2143 OS << ")";
2144}
2145
John McCall8cd78132009-11-19 22:55:06 +00002146void StmtPrinter::VisitCXXDependentScopeMemberExpr(
2147 CXXDependentScopeMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00002148 if (!Node->isImplicitAccess()) {
2149 PrintExpr(Node->getBase());
2150 OS << (Node->isArrow() ? "->" : ".");
2151 }
Douglas Gregorc26e0f62009-09-03 16:14:30 +00002152 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2153 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002154 if (Node->hasTemplateKeyword())
Douglas Gregor308047d2009-09-09 00:23:06 +00002155 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002156 OS << Node->getMemberNameInfo();
Benjamin Kramer9170e912013-02-22 15:46:01 +00002157 if (Node->hasExplicitTemplateArgs())
2158 TemplateSpecializationType::PrintTemplateArgumentList(
2159 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregora8db9542009-05-22 21:13:27 +00002160}
2161
John McCall10eae182009-11-30 22:42:35 +00002162void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00002163 if (!Node->isImplicitAccess()) {
2164 PrintExpr(Node->getBase());
2165 OS << (Node->isArrow() ? "->" : ".");
2166 }
John McCall10eae182009-11-30 22:42:35 +00002167 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2168 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002169 if (Node->hasTemplateKeyword())
2170 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002171 OS << Node->getMemberNameInfo();
Benjamin Kramer9170e912013-02-22 15:46:01 +00002172 if (Node->hasExplicitTemplateArgs())
2173 TemplateSpecializationType::PrintTemplateArgumentList(
2174 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
John McCall10eae182009-11-30 22:42:35 +00002175}
2176
Douglas Gregor29c42f22012-02-24 07:38:34 +00002177static const char *getTypeTraitName(TypeTrait TT) {
2178 switch (TT) {
Alp Toker95e7ff22014-01-01 05:57:51 +00002179#define TYPE_TRAIT_1(Spelling, Name, Key) \
2180case clang::UTT_##Name: return #Spelling;
Alp Tokercbb90342013-12-13 20:49:58 +00002181#define TYPE_TRAIT_2(Spelling, Name, Key) \
2182case clang::BTT_##Name: return #Spelling;
Alp Toker40f9b1c2013-12-12 21:23:03 +00002183#define TYPE_TRAIT_N(Spelling, Name, Key) \
2184 case clang::TT_##Name: return #Spelling;
2185#include "clang/Basic/TokenKinds.def"
Douglas Gregor29c42f22012-02-24 07:38:34 +00002186 }
2187 llvm_unreachable("Type trait not covered by switch");
2188}
2189
John Wiegley6242b6a2011-04-28 00:16:57 +00002190static const char *getTypeTraitName(ArrayTypeTrait ATT) {
2191 switch (ATT) {
2192 case ATT_ArrayRank: return "__array_rank";
2193 case ATT_ArrayExtent: return "__array_extent";
2194 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00002195 llvm_unreachable("Array type trait not covered by switch");
John Wiegley6242b6a2011-04-28 00:16:57 +00002196}
2197
John Wiegleyf9f65842011-04-25 06:54:41 +00002198static const char *getExpressionTraitName(ExpressionTrait ET) {
2199 switch (ET) {
John Wiegleyf9f65842011-04-25 06:54:41 +00002200 case ET_IsLValueExpr: return "__is_lvalue_expr";
2201 case ET_IsRValueExpr: return "__is_rvalue_expr";
2202 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00002203 llvm_unreachable("Expression type trait not covered by switch");
John Wiegleyf9f65842011-04-25 06:54:41 +00002204}
2205
Douglas Gregor29c42f22012-02-24 07:38:34 +00002206void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
2207 OS << getTypeTraitName(E->getTrait()) << "(";
2208 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
2209 if (I > 0)
2210 OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002211 E->getArg(I)->getType().print(OS, Policy);
Douglas Gregor29c42f22012-02-24 07:38:34 +00002212 }
2213 OS << ")";
2214}
2215
John Wiegley6242b6a2011-04-28 00:16:57 +00002216void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002217 OS << getTypeTraitName(E->getTrait()) << '(';
2218 E->getQueriedType().print(OS, Policy);
2219 OS << ')';
John Wiegley6242b6a2011-04-28 00:16:57 +00002220}
2221
John Wiegleyf9f65842011-04-25 06:54:41 +00002222void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002223 OS << getExpressionTraitName(E->getTrait()) << '(';
2224 PrintExpr(E->getQueriedExpression());
2225 OS << ')';
John Wiegleyf9f65842011-04-25 06:54:41 +00002226}
2227
Sebastian Redl4202c0f2010-09-10 20:55:43 +00002228void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
2229 OS << "noexcept(";
2230 PrintExpr(E->getOperand());
2231 OS << ")";
2232}
2233
Douglas Gregor820ba7b2011-01-04 17:33:58 +00002234void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregore8e9dd62011-01-03 17:17:50 +00002235 PrintExpr(E->getPattern());
2236 OS << "...";
2237}
2238
Douglas Gregor820ba7b2011-01-04 17:33:58 +00002239void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00002240 OS << "sizeof...(" << *E->getPack() << ")";
Douglas Gregor820ba7b2011-01-04 17:33:58 +00002241}
2242
Douglas Gregorcdbc5392011-01-15 01:15:58 +00002243void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
2244 SubstNonTypeTemplateParmPackExpr *Node) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00002245 OS << *Node->getParameterPack();
Douglas Gregorcdbc5392011-01-15 01:15:58 +00002246}
2247
John McCall7c454bb2011-07-15 05:09:51 +00002248void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
2249 SubstNonTypeTemplateParmExpr *Node) {
2250 Visit(Node->getReplacement());
2251}
2252
Richard Smithb15fe3a2012-09-12 00:56:43 +00002253void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
2254 OS << *E->getParameterPack();
2255}
2256
Douglas Gregorfe314812011-06-21 17:03:29 +00002257void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
2258 PrintExpr(Node->GetTemporaryExpr());
2259}
2260
Richard Smith0f0af192014-11-08 05:07:16 +00002261void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
2262 OS << "(";
2263 if (E->getLHS()) {
2264 PrintExpr(E->getLHS());
2265 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2266 }
2267 OS << "...";
2268 if (E->getRHS()) {
2269 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2270 PrintExpr(E->getRHS());
2271 }
2272 OS << ")";
2273}
2274
Richard Smith9f690bd2015-10-27 06:02:45 +00002275// C++ Coroutines TS
2276
2277void StmtPrinter::VisitCoroutineBodyStmt(CoroutineBodyStmt *S) {
2278 Visit(S->getBody());
2279}
2280
2281void StmtPrinter::VisitCoreturnStmt(CoreturnStmt *S) {
2282 OS << "co_return";
2283 if (S->getOperand()) {
2284 OS << " ";
2285 Visit(S->getOperand());
2286 }
2287 OS << ";";
2288}
2289
2290void StmtPrinter::VisitCoawaitExpr(CoawaitExpr *S) {
2291 OS << "co_await ";
2292 PrintExpr(S->getOperand());
2293}
2294
2295void StmtPrinter::VisitCoyieldExpr(CoyieldExpr *S) {
2296 OS << "co_yield ";
2297 PrintExpr(S->getOperand());
2298}
2299
Mike Stump11289f42009-09-09 15:08:12 +00002300// Obj-C
Anders Carlsson76f4a902007-08-21 17:43:55 +00002301
2302void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
2303 OS << "@";
2304 VisitStringLiteral(Node->getString());
2305}
Chris Lattnereefa10e2007-05-28 06:56:27 +00002306
Patrick Beard0caa3942012-04-19 00:25:12 +00002307void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00002308 OS << "@";
Patrick Beard0caa3942012-04-19 00:25:12 +00002309 Visit(E->getSubExpr());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002310}
2311
2312void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
2313 OS << "@[ ";
Benjamin Kramer5733e352015-07-18 17:09:36 +00002314 ObjCArrayLiteral::child_range Ch = E->children();
2315 for (auto I = Ch.begin(), E = Ch.end(); I != E; ++I) {
2316 if (I != Ch.begin())
Ted Kremeneke65b0862012-03-06 20:05:56 +00002317 OS << ", ";
Benjamin Kramer5733e352015-07-18 17:09:36 +00002318 Visit(*I);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002319 }
2320 OS << " ]";
2321}
2322
2323void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
2324 OS << "@{ ";
2325 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
2326 if (I > 0)
2327 OS << ", ";
2328
2329 ObjCDictionaryElement Element = E->getKeyValueElement(I);
2330 Visit(Element.Key);
2331 OS << " : ";
2332 Visit(Element.Value);
2333 if (Element.isPackExpansion())
2334 OS << "...";
2335 }
2336 OS << " }";
2337}
2338
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002339void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002340 OS << "@encode(";
2341 Node->getEncodedType().print(OS, Policy);
2342 OS << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002343}
2344
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002345void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00002346 OS << "@selector(";
2347 Node->getSelector().print(OS);
2348 OS << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002349}
2350
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002351void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00002352 OS << "@protocol(" << *Node->getProtocol() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002353}
2354
Steve Naroffd54978b2007-09-18 23:55:05 +00002355void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
2356 OS << "[";
Douglas Gregor9a129192010-04-21 00:45:42 +00002357 switch (Mess->getReceiverKind()) {
2358 case ObjCMessageExpr::Instance:
2359 PrintExpr(Mess->getInstanceReceiver());
2360 break;
2361
2362 case ObjCMessageExpr::Class:
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002363 Mess->getClassReceiver().print(OS, Policy);
Douglas Gregor9a129192010-04-21 00:45:42 +00002364 break;
2365
2366 case ObjCMessageExpr::SuperInstance:
2367 case ObjCMessageExpr::SuperClass:
2368 OS << "Super";
2369 break;
2370 }
2371
Ted Kremeneka06e7122008-05-02 17:32:38 +00002372 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00002373 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00002374 if (selector.isUnarySelector()) {
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00002375 OS << selector.getNameForSlot(0);
Steve Naroffc6814ea2007-10-02 20:01:56 +00002376 } else {
2377 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00002378 if (i < selector.getNumArgs()) {
2379 if (i > 0) OS << ' ';
2380 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00002381 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00002382 else
2383 OS << ":";
2384 }
2385 else OS << ", "; // Handle variadic methods.
Mike Stump11289f42009-09-09 15:08:12 +00002386
Steve Naroffc6814ea2007-10-02 20:01:56 +00002387 PrintExpr(Mess->getArg(i));
2388 }
Steve Naroffd54978b2007-09-18 23:55:05 +00002389 }
2390 OS << "]";
2391}
2392
Ted Kremeneke65b0862012-03-06 20:05:56 +00002393void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
2394 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
2395}
2396
John McCall31168b02011-06-15 23:02:42 +00002397void
2398StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
2399 PrintExpr(E->getSubExpr());
2400}
2401
2402void
2403StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002404 OS << '(' << E->getBridgeKindName();
2405 E->getType().print(OS, Policy);
2406 OS << ')';
John McCall31168b02011-06-15 23:02:42 +00002407 PrintExpr(E->getSubExpr());
2408}
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002409
Steve Naroffc540d662008-09-03 18:15:37 +00002410void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00002411 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00002412 OS << "^";
Mike Stump11289f42009-09-09 15:08:12 +00002413
Steve Naroffc540d662008-09-03 18:15:37 +00002414 const FunctionType *AFT = Node->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00002415
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002416 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroffc540d662008-09-03 18:15:37 +00002417 OS << "()";
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002418 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00002419 OS << '(';
Steve Naroff415d3d52008-10-08 17:01:13 +00002420 for (BlockDecl::param_iterator AI = BD->param_begin(),
2421 E = BD->param_end(); AI != E; ++AI) {
2422 if (AI != BD->param_begin()) OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002423 std::string ParamStr = (*AI)->getNameAsString();
2424 (*AI)->getType().print(OS, Policy, ParamStr);
Steve Naroffc540d662008-09-03 18:15:37 +00002425 }
Mike Stump11289f42009-09-09 15:08:12 +00002426
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002427 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00002428 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00002429 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00002430 OS << "...";
2431 }
2432 OS << ')';
2433 }
Fariborz Jahanian1ace8cb2012-12-04 21:15:23 +00002434 OS << "{ }";
Steve Naroffc540d662008-09-03 18:15:37 +00002435}
2436
Ted Kremenekf551f322011-11-30 22:08:08 +00002437void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
2438 PrintExpr(Node->getSourceExpr());
2439}
John McCall8d69a212010-11-15 23:31:06 +00002440
Kaelyn Takatae1f49d52014-10-27 18:07:20 +00002441void StmtPrinter::VisitTypoExpr(TypoExpr *Node) {
2442 // TODO: Print something reasonable for a TypoExpr, if necessary.
2443 assert(false && "Cannot print TypoExpr nodes");
2444}
2445
Tanya Lattner55808c12011-06-04 00:47:47 +00002446void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
2447 OS << "__builtin_astype(";
2448 PrintExpr(Node->getSrcExpr());
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002449 OS << ", ";
2450 Node->getType().print(OS, Policy);
Tanya Lattner55808c12011-06-04 00:47:47 +00002451 OS << ")";
2452}
2453
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00002454//===----------------------------------------------------------------------===//
2455// Stmt method implementations
2456//===----------------------------------------------------------------------===//
2457
Craig Topperc571c812013-08-22 06:02:26 +00002458void Stmt::dumpPretty(const ASTContext &Context) const {
Craig Topper36250ad2014-05-12 05:36:57 +00002459 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00002460}
2461
Richard Smith235341b2012-08-16 03:56:14 +00002462void Stmt::printPretty(raw_ostream &OS,
2463 PrinterHelper *Helper,
Douglas Gregor7de59662009-05-29 20:38:28 +00002464 const PrintingPolicy &Policy,
2465 unsigned Indentation) const {
Richard Smith235341b2012-08-16 03:56:14 +00002466 StmtPrinter P(OS, Helper, Policy, Indentation);
Chris Lattner62249a62007-08-21 04:04:25 +00002467 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00002468}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002469
2470//===----------------------------------------------------------------------===//
2471// PrinterHelper
2472//===----------------------------------------------------------------------===//
2473
2474// Implement virtual destructor.
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +00002475PrinterHelper::~PrinterHelper() {}