blob: 0aa327da85e9fc6ed9d318fea4d5950e931b0e23 [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"
Alexey Bataev90c228f2016-02-08 09:29:13 +000019#include "clang/AST/DeclOpenMP.h"
Douglas Gregorcdbc5392011-01-15 01:15:58 +000020#include "clang/AST/DeclTemplate.h"
Douglas Gregor882211c2010-04-28 22:16:22 +000021#include "clang/AST/Expr.h"
Douglas Gregor2b88c112010-09-08 00:15:04 +000022#include "clang/AST/ExprCXX.h"
Alexey Bataev1a3320e2015-08-25 14:24:04 +000023#include "clang/AST/ExprOpenMP.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000024#include "clang/AST/PrettyPrinter.h"
25#include "clang/AST/StmtVisitor.h"
Jordan Rose00d1b592013-02-08 22:30:27 +000026#include "clang/Basic/CharInfo.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000027#include "llvm/ADT/SmallString.h"
Jordan Rose00d1b592013-02-08 22:30:27 +000028#include "llvm/Support/Format.h"
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000029using namespace clang;
30
31//===----------------------------------------------------------------------===//
32// StmtPrinter Visitor
33//===----------------------------------------------------------------------===//
34
35namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +000036 class StmtPrinter : public StmtVisitor<StmtPrinter> {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000037 raw_ostream &OS;
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000038 unsigned IndentLevel;
Ted Kremenek04f3cee2007-08-31 21:30:12 +000039 clang::PrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +000040 PrintingPolicy Policy;
41
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000042 public:
Richard Smith235341b2012-08-16 03:56:14 +000043 StmtPrinter(raw_ostream &os, PrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +000044 const PrintingPolicy &Policy,
Douglas Gregor7de59662009-05-29 20:38:28 +000045 unsigned Indentation = 0)
Richard Smith235341b2012-08-16 03:56:14 +000046 : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy) {}
Mike Stump11289f42009-09-09 15:08:12 +000047
Douglas Gregor7de59662009-05-29 20:38:28 +000048 void PrintStmt(Stmt *S) {
49 PrintStmt(S, Policy.Indentation);
50 }
51
52 void PrintStmt(Stmt *S, int SubIndent) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000053 IndentLevel += SubIndent;
Chris Lattnera076fde2007-05-31 18:21:33 +000054 if (S && isa<Expr>(S)) {
Chris Lattner882f7882006-11-04 18:52:07 +000055 // If this is an expr used in a stmt context, indent and newline it.
56 Indent();
Chris Lattner62249a62007-08-21 04:04:25 +000057 Visit(S);
Chris Lattnerfc068c12007-05-30 17:57:36 +000058 OS << ";\n";
Chris Lattner882f7882006-11-04 18:52:07 +000059 } else if (S) {
Chris Lattner62249a62007-08-21 04:04:25 +000060 Visit(S);
Chris Lattner882f7882006-11-04 18:52:07 +000061 } else {
Chris Lattner2f6ac262007-05-28 01:47:47 +000062 Indent() << "<<<NULL STATEMENT>>>\n";
Chris Lattner882f7882006-11-04 18:52:07 +000063 }
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000064 IndentLevel -= SubIndent;
Chris Lattner882f7882006-11-04 18:52:07 +000065 }
Eli Friedman15ea8802009-05-30 00:19:54 +000066
Chris Lattner073926e2007-05-20 23:04:55 +000067 void PrintRawCompoundStmt(CompoundStmt *S);
Chris Lattnerfdc195a2007-06-05 20:52:47 +000068 void PrintRawDecl(Decl *D);
Eli Friedmanf86e5072012-10-16 23:45:15 +000069 void PrintRawDeclStmt(const DeclStmt *S);
Chris Lattnerc0a38dd2007-06-11 22:26:23 +000070 void PrintRawIfStmt(IfStmt *If);
Sebastian Redl9b244a82008-12-22 21:35:02 +000071 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
Peter Collingbourne4b279a02011-02-08 21:17:54 +000072 void PrintCallArgs(CallExpr *E);
John Wiegley1c0675e2011-04-28 01:08:34 +000073 void PrintRawSEHExceptHandler(SEHExceptStmt *S);
74 void PrintRawSEHFinallyStmt(SEHFinallyStmt *S);
Alexey Bataev1b59ab52014-02-27 08:29:12 +000075 void PrintOMPExecutableDirective(OMPExecutableDirective *S);
Mike Stump11289f42009-09-09 15:08:12 +000076
Chris Lattner882f7882006-11-04 18:52:07 +000077 void PrintExpr(Expr *E) {
78 if (E)
Chris Lattner62249a62007-08-21 04:04:25 +000079 Visit(E);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000080 else
Chris Lattner882f7882006-11-04 18:52:07 +000081 OS << "<null expr>";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000082 }
Mike Stump11289f42009-09-09 15:08:12 +000083
Chris Lattner0e62c1c2011-07-23 10:55:15 +000084 raw_ostream &Indent(int Delta = 0) {
Douglas Gregor7de59662009-05-29 20:38:28 +000085 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
86 OS << " ";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000087 return OS;
88 }
Mike Stump11289f42009-09-09 15:08:12 +000089
Mike Stump11289f42009-09-09 15:08:12 +000090 void Visit(Stmt* S) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +000091 if (Helper && Helper->handledStmt(S,OS))
92 return;
93 else StmtVisitor<StmtPrinter>::Visit(S);
94 }
Alexey Bataev1b59ab52014-02-27 08:29:12 +000095
Chandler Carruthb7967b92010-10-23 08:21:37 +000096 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +000097 Indent() << "<<unknown stmt type>>\n";
98 }
Chandler Carruthb7967b92010-10-23 08:21:37 +000099 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +0000100 OS << "<<unknown expr type>>";
101 }
102 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Mike Stump11289f42009-09-09 15:08:12 +0000103
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +0000104#define ABSTRACT_STMT(CLASS)
Douglas Gregorbe35ce92008-11-14 12:46:07 +0000105#define STMT(CLASS, PARENT) \
Chris Lattner62249a62007-08-21 04:04:25 +0000106 void Visit##CLASS(CLASS *Node);
Alexis Hunt656bb312010-05-05 15:24:00 +0000107#include "clang/AST/StmtNodes.inc"
Chris Lattner71e23ce2006-11-04 20:18:38 +0000108 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000109}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000110
Chris Lattner71e23ce2006-11-04 20:18:38 +0000111//===----------------------------------------------------------------------===//
112// Stmt printing methods.
113//===----------------------------------------------------------------------===//
114
Chris Lattner073926e2007-05-20 23:04:55 +0000115/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
116/// with no newline after the }.
117void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
118 OS << "{\n";
Aaron Ballmanc7e4e212014-03-17 14:19:37 +0000119 for (auto *I : Node->body())
120 PrintStmt(I);
Mike Stump11289f42009-09-09 15:08:12 +0000121
Chris Lattner073926e2007-05-20 23:04:55 +0000122 Indent() << "}";
123}
124
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000125void StmtPrinter::PrintRawDecl(Decl *D) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000126 D->print(OS, Policy, IndentLevel);
Mike Stump74a76472009-02-10 20:16:46 +0000127}
128
Eli Friedmanf86e5072012-10-16 23:45:15 +0000129void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
Aaron Ballman535bbcc2014-03-14 17:01:24 +0000130 SmallVector<Decl*, 2> Decls(S->decls());
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000131 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
Ted Kremenek15e6b402008-10-06 18:39:36 +0000132}
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000133
134void StmtPrinter::VisitNullStmt(NullStmt *Node) {
135 Indent() << ";\n";
136}
137
138void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Eli Friedman15ea8802009-05-30 00:19:54 +0000139 Indent();
140 PrintRawDeclStmt(Node);
141 OS << ";\n";
Steve Naroff2a8ad182007-05-29 22:59:26 +0000142}
143
Chris Lattner073926e2007-05-20 23:04:55 +0000144void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
145 Indent();
146 PrintRawCompoundStmt(Node);
Chris Lattnerdf3cafb2007-05-31 05:08:56 +0000147 OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000148}
149
Chris Lattner6c0ff132006-11-05 00:19:50 +0000150void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000151 Indent(-1) << "case ";
Chris Lattner6c0ff132006-11-05 00:19:50 +0000152 PrintExpr(Node->getLHS());
153 if (Node->getRHS()) {
154 OS << " ... ";
155 PrintExpr(Node->getRHS());
156 }
157 OS << ":\n";
Mike Stump11289f42009-09-09 15:08:12 +0000158
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000159 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000160}
161
162void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000163 Indent(-1) << "default:\n";
164 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000165}
166
167void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000168 Indent(-1) << Node->getName() << ":\n";
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000169 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000170}
171
Richard Smithc202b282012-04-14 00:33:13 +0000172void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
Aaron Ballmanb06b15a2014-06-06 12:40:24 +0000173 for (const auto *Attr : Node->getAttrs()) {
Tyler Nowickie8b07ed2014-06-13 17:57:25 +0000174 Attr->printPretty(OS, Policy);
Aaron Ballmanb06b15a2014-06-06 12:40:24 +0000175 }
176
Richard Smithc202b282012-04-14 00:33:13 +0000177 PrintStmt(Node->getSubStmt(), 0);
178}
179
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000180void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000181 OS << "if (";
Eli Friedmanf86e5072012-10-16 23:45:15 +0000182 if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
183 PrintRawDeclStmt(DS);
184 else
185 PrintExpr(If->getCond());
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000186 OS << ')';
Mike Stump11289f42009-09-09 15:08:12 +0000187
Chris Lattner073926e2007-05-20 23:04:55 +0000188 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
189 OS << ' ';
190 PrintRawCompoundStmt(CS);
191 OS << (If->getElse() ? ' ' : '\n');
192 } else {
193 OS << '\n';
194 PrintStmt(If->getThen());
195 if (If->getElse()) Indent();
196 }
Mike Stump11289f42009-09-09 15:08:12 +0000197
Chris Lattner073926e2007-05-20 23:04:55 +0000198 if (Stmt *Else = If->getElse()) {
199 OS << "else";
Mike Stump11289f42009-09-09 15:08:12 +0000200
Chris Lattner073926e2007-05-20 23:04:55 +0000201 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
202 OS << ' ';
203 PrintRawCompoundStmt(CS);
204 OS << '\n';
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000205 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
206 OS << ' ';
207 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000208 } else {
209 OS << '\n';
210 PrintStmt(If->getElse());
211 }
Chris Lattner882f7882006-11-04 18:52:07 +0000212 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000213}
214
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000215void StmtPrinter::VisitIfStmt(IfStmt *If) {
216 Indent();
217 PrintRawIfStmt(If);
218}
219
Chris Lattnerf2174b62006-11-04 20:59:27 +0000220void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
221 Indent() << "switch (";
Eli Friedmanf86e5072012-10-16 23:45:15 +0000222 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
223 PrintRawDeclStmt(DS);
224 else
225 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000226 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000227
Chris Lattner073926e2007-05-20 23:04:55 +0000228 // Pretty print compoundstmt bodies (very common).
229 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
230 OS << " ";
231 PrintRawCompoundStmt(CS);
232 OS << "\n";
233 } else {
234 OS << "\n";
235 PrintStmt(Node->getBody());
236 }
Chris Lattnerf2174b62006-11-04 20:59:27 +0000237}
238
Chris Lattner85ed8732006-11-04 20:40:44 +0000239void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
240 Indent() << "while (";
Eli Friedmanf86e5072012-10-16 23:45:15 +0000241 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
242 PrintRawDeclStmt(DS);
243 else
244 PrintExpr(Node->getCond());
Chris Lattner85ed8732006-11-04 20:40:44 +0000245 OS << ")\n";
246 PrintStmt(Node->getBody());
247}
248
249void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000250 Indent() << "do ";
251 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
252 PrintRawCompoundStmt(CS);
253 OS << " ";
254 } else {
255 OS << "\n";
256 PrintStmt(Node->getBody());
257 Indent();
258 }
Mike Stump11289f42009-09-09 15:08:12 +0000259
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000260 OS << "while (";
Chris Lattner85ed8732006-11-04 20:40:44 +0000261 PrintExpr(Node->getCond());
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000262 OS << ");\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000263}
264
Chris Lattner71e23ce2006-11-04 20:18:38 +0000265void StmtPrinter::VisitForStmt(ForStmt *Node) {
266 Indent() << "for (";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000267 if (Node->getInit()) {
268 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000269 PrintRawDeclStmt(DS);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000270 else
271 PrintExpr(cast<Expr>(Node->getInit()));
272 }
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000273 OS << ";";
274 if (Node->getCond()) {
275 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000276 PrintExpr(Node->getCond());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000277 }
278 OS << ";";
279 if (Node->getInc()) {
280 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000281 PrintExpr(Node->getInc());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000282 }
283 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000284
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000285 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
286 PrintRawCompoundStmt(CS);
287 OS << "\n";
288 } else {
289 OS << "\n";
290 PrintStmt(Node->getBody());
291 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000292}
293
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000294void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian83615522008-01-02 22:54:34 +0000295 Indent() << "for (";
296 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000297 PrintRawDeclStmt(DS);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000298 else
299 PrintExpr(cast<Expr>(Node->getElement()));
300 OS << " in ";
301 PrintExpr(Node->getCollection());
302 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000303
Fariborz Jahanian83615522008-01-02 22:54:34 +0000304 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
305 PrintRawCompoundStmt(CS);
306 OS << "\n";
307 } else {
308 OS << "\n";
309 PrintStmt(Node->getBody());
310 }
311}
312
Richard Smith02e85f32011-04-14 22:09:26 +0000313void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
314 Indent() << "for (";
315 PrintingPolicy SubPolicy(Policy);
316 SubPolicy.SuppressInitializers = true;
317 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
318 OS << " : ";
319 PrintExpr(Node->getRangeInit());
320 OS << ") {\n";
321 PrintStmt(Node->getBody());
Ted Kremenek88446602013-12-11 23:44:02 +0000322 Indent() << "}";
323 if (Policy.IncludeNewlines) OS << "\n";
Richard Smith02e85f32011-04-14 22:09:26 +0000324}
325
Douglas Gregordeb4a2be2011-10-25 01:33:02 +0000326void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
327 Indent();
328 if (Node->isIfExists())
329 OS << "__if_exists (";
330 else
331 OS << "__if_not_exists (";
332
333 if (NestedNameSpecifier *Qualifier
334 = Node->getQualifierLoc().getNestedNameSpecifier())
335 Qualifier->print(OS, Policy);
336
337 OS << Node->getNameInfo() << ") ";
338
339 PrintRawCompoundStmt(Node->getSubStmt());
340}
341
Chris Lattner16976d32006-11-05 01:46:01 +0000342void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000343 Indent() << "goto " << Node->getLabel()->getName() << ";";
344 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000345}
346
347void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000348 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000349 PrintExpr(Node->getTarget());
Ted Kremenek88446602013-12-11 23:44:02 +0000350 OS << ";";
351 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000352}
353
354void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000355 Indent() << "continue;";
356 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000357}
358
359void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000360 Indent() << "break;";
361 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000362}
363
364
Chris Lattner882f7882006-11-04 18:52:07 +0000365void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
366 Indent() << "return";
367 if (Node->getRetValue()) {
368 OS << " ";
369 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000370 }
Ted Kremenek88446602013-12-11 23:44:02 +0000371 OS << ";";
372 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000373}
374
Chris Lattner73c56c02007-10-29 04:04:16 +0000375
Chad Rosierde70e0e2012-08-25 00:11:56 +0000376void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
Anders Carlsson660bdd12007-11-23 23:12:25 +0000377 Indent() << "asm ";
Mike Stump11289f42009-09-09 15:08:12 +0000378
Anders Carlsson660bdd12007-11-23 23:12:25 +0000379 if (Node->isVolatile())
380 OS << "volatile ";
Mike Stump11289f42009-09-09 15:08:12 +0000381
Anders Carlsson660bdd12007-11-23 23:12:25 +0000382 OS << "(";
Anders Carlsson81a5a692007-11-20 19:21:03 +0000383 VisitStringLiteral(Node->getAsmString());
Mike Stump11289f42009-09-09 15:08:12 +0000384
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000385 // Outputs
386 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
387 Node->getNumClobbers() != 0)
388 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000389
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000390 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
391 if (i != 0)
392 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000393
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000394 if (!Node->getOutputName(i).empty()) {
395 OS << '[';
396 OS << Node->getOutputName(i);
397 OS << "] ";
398 }
Mike Stump11289f42009-09-09 15:08:12 +0000399
Chris Lattner72bbf172009-03-10 04:59:06 +0000400 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Jonathan Roelofs78f9e032015-06-09 14:13:31 +0000401 OS << " (";
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000402 Visit(Node->getOutputExpr(i));
Jonathan Roelofs78f9e032015-06-09 14:13:31 +0000403 OS << ")";
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000404 }
Mike Stump11289f42009-09-09 15:08:12 +0000405
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000406 // Inputs
407 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
408 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000409
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000410 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
411 if (i != 0)
412 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000413
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000414 if (!Node->getInputName(i).empty()) {
415 OS << '[';
416 OS << Node->getInputName(i);
417 OS << "] ";
418 }
Mike Stump11289f42009-09-09 15:08:12 +0000419
Chris Lattner72bbf172009-03-10 04:59:06 +0000420 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Jonathan Roelofs78f9e032015-06-09 14:13:31 +0000421 OS << " (";
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000422 Visit(Node->getInputExpr(i));
Jonathan Roelofs78f9e032015-06-09 14:13:31 +0000423 OS << ")";
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000424 }
Mike Stump11289f42009-09-09 15:08:12 +0000425
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000426 // Clobbers
427 if (Node->getNumClobbers() != 0)
428 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000429
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000430 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
431 if (i != 0)
432 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000433
Chad Rosierd9fb09a2012-08-27 23:28:41 +0000434 VisitStringLiteral(Node->getClobberStringLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000435 }
Mike Stump11289f42009-09-09 15:08:12 +0000436
Ted Kremenek88446602013-12-11 23:44:02 +0000437 OS << ");";
438 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner73c56c02007-10-29 04:04:16 +0000439}
440
Chad Rosier32503022012-06-11 20:47:18 +0000441void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
442 // FIXME: Implement MS style inline asm statement printer.
Chad Rosierb6f46c12012-08-15 16:53:30 +0000443 Indent() << "__asm ";
444 if (Node->hasBraces())
445 OS << "{\n";
John McCallf413f5e2013-05-03 00:10:13 +0000446 OS << Node->getAsmString() << "\n";
Chad Rosierb6f46c12012-08-15 16:53:30 +0000447 if (Node->hasBraces())
448 Indent() << "}\n";
Chad Rosier32503022012-06-11 20:47:18 +0000449}
450
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000451void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000452 PrintStmt(Node->getCapturedDecl()->getBody());
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000453}
454
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000455void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000456 Indent() << "@try";
457 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
458 PrintRawCompoundStmt(TS);
459 OS << "\n";
460 }
Mike Stump11289f42009-09-09 15:08:12 +0000461
Douglas Gregor96c79492010-04-23 22:50:49 +0000462 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
463 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000464 Indent() << "@catch(";
Steve Naroff371b8fb2009-03-03 19:52:17 +0000465 if (catchStmt->getCatchParamDecl()) {
466 if (Decl *DS = catchStmt->getCatchParamDecl())
467 PrintRawDecl(DS);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000468 }
469 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000470 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
471 PrintRawCompoundStmt(CS);
472 OS << "\n";
473 }
Fariborz Jahanian88157952007-11-02 18:16:07 +0000474 }
Mike Stump11289f42009-09-09 15:08:12 +0000475
476 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
477 Node->getFinallyStmt())) {
Fariborz Jahaniandefbf9a2007-11-07 00:46:42 +0000478 Indent() << "@finally";
479 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian88157952007-11-02 18:16:07 +0000480 OS << "\n";
Mike Stump11289f42009-09-09 15:08:12 +0000481 }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000482}
483
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000484void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000485}
486
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000487void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000488 Indent() << "@catch (...) { /* todo */ } \n";
489}
490
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000491void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000492 Indent() << "@throw";
493 if (Node->getThrowExpr()) {
494 OS << " ";
495 PrintExpr(Node->getThrowExpr());
496 }
497 OS << ";\n";
498}
499
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000500void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000501 Indent() << "@synchronized (";
502 PrintExpr(Node->getSynchExpr());
503 OS << ")";
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000504 PrintRawCompoundStmt(Node->getSynchBody());
505 OS << "\n";
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000506}
507
John McCall31168b02011-06-15 23:02:42 +0000508void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
509 Indent() << "@autoreleasepool";
510 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
511 OS << "\n";
512}
513
Sebastian Redl9b244a82008-12-22 21:35:02 +0000514void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
515 OS << "catch (";
Sebastian Redl54c04d42008-12-22 19:15:10 +0000516 if (Decl *ExDecl = Node->getExceptionDecl())
517 PrintRawDecl(ExDecl);
518 else
519 OS << "...";
520 OS << ") ";
521 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl9b244a82008-12-22 21:35:02 +0000522}
523
524void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
525 Indent();
526 PrintRawCXXCatchStmt(Node);
527 OS << "\n";
528}
529
530void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
531 Indent() << "try ";
532 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump11289f42009-09-09 15:08:12 +0000533 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl9b244a82008-12-22 21:35:02 +0000534 OS << " ";
535 PrintRawCXXCatchStmt(Node->getHandler(i));
536 }
Sebastian Redl54c04d42008-12-22 19:15:10 +0000537 OS << "\n";
538}
539
John Wiegley1c0675e2011-04-28 01:08:34 +0000540void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
541 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
542 PrintRawCompoundStmt(Node->getTryBlock());
543 SEHExceptStmt *E = Node->getExceptHandler();
544 SEHFinallyStmt *F = Node->getFinallyHandler();
545 if(E)
546 PrintRawSEHExceptHandler(E);
547 else {
548 assert(F && "Must have a finally block...");
549 PrintRawSEHFinallyStmt(F);
550 }
551 OS << "\n";
552}
553
554void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
555 OS << "__finally ";
556 PrintRawCompoundStmt(Node->getBlock());
557 OS << "\n";
558}
559
560void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
561 OS << "__except (";
562 VisitExpr(Node->getFilterExpr());
Joao Matos566359c2012-09-04 17:49:35 +0000563 OS << ")\n";
John Wiegley1c0675e2011-04-28 01:08:34 +0000564 PrintRawCompoundStmt(Node->getBlock());
565 OS << "\n";
566}
567
568void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
569 Indent();
570 PrintRawSEHExceptHandler(Node);
571 OS << "\n";
572}
573
574void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
575 Indent();
576 PrintRawSEHFinallyStmt(Node);
577 OS << "\n";
578}
579
Nico Weber9b982072014-07-07 00:12:30 +0000580void StmtPrinter::VisitSEHLeaveStmt(SEHLeaveStmt *Node) {
581 Indent() << "__leave;";
582 if (Policy.IncludeNewlines) OS << "\n";
583}
584
Chris Lattner71e23ce2006-11-04 20:18:38 +0000585//===----------------------------------------------------------------------===//
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000586// OpenMP clauses printing methods
587//===----------------------------------------------------------------------===//
588
589namespace {
590class OMPClausePrinter : public OMPClauseVisitor<OMPClausePrinter> {
591 raw_ostream &OS;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000592 const PrintingPolicy &Policy;
Alexey Bataev756c1962013-09-24 03:17:45 +0000593 /// \brief Process clauses with list of variables.
594 template <typename T>
595 void VisitOMPClauseList(T *Node, char StartSym);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000596public:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000597 OMPClausePrinter(raw_ostream &OS, const PrintingPolicy &Policy)
598 : OS(OS), Policy(Policy) { }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000599#define OPENMP_CLAUSE(Name, Class) \
600 void Visit##Class(Class *S);
601#include "clang/Basic/OpenMPKinds.def"
602};
603
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000604void OMPClausePrinter::VisitOMPIfClause(OMPIfClause *Node) {
605 OS << "if(";
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000606 if (Node->getNameModifier() != OMPD_unknown)
607 OS << getOpenMPDirectiveName(Node->getNameModifier()) << ": ";
Craig Topper36250ad2014-05-12 05:36:57 +0000608 Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000609 OS << ")";
610}
611
Alexey Bataev3778b602014-07-17 07:32:53 +0000612void OMPClausePrinter::VisitOMPFinalClause(OMPFinalClause *Node) {
613 OS << "final(";
614 Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
615 OS << ")";
616}
617
Alexey Bataev568a8332014-03-06 06:15:19 +0000618void OMPClausePrinter::VisitOMPNumThreadsClause(OMPNumThreadsClause *Node) {
619 OS << "num_threads(";
Craig Topper36250ad2014-05-12 05:36:57 +0000620 Node->getNumThreads()->printPretty(OS, nullptr, Policy, 0);
Alexey Bataev568a8332014-03-06 06:15:19 +0000621 OS << ")";
622}
623
Alexey Bataev62c87d22014-03-21 04:51:18 +0000624void OMPClausePrinter::VisitOMPSafelenClause(OMPSafelenClause *Node) {
625 OS << "safelen(";
Craig Topper36250ad2014-05-12 05:36:57 +0000626 Node->getSafelen()->printPretty(OS, nullptr, Policy, 0);
Alexey Bataev62c87d22014-03-21 04:51:18 +0000627 OS << ")";
628}
629
Alexey Bataev66b15b52015-08-21 11:14:16 +0000630void OMPClausePrinter::VisitOMPSimdlenClause(OMPSimdlenClause *Node) {
631 OS << "simdlen(";
632 Node->getSimdlen()->printPretty(OS, nullptr, Policy, 0);
633 OS << ")";
634}
635
Alexander Musman8bd31e62014-05-27 15:12:19 +0000636void OMPClausePrinter::VisitOMPCollapseClause(OMPCollapseClause *Node) {
637 OS << "collapse(";
638 Node->getNumForLoops()->printPretty(OS, nullptr, Policy, 0);
639 OS << ")";
640}
641
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000642void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
643 OS << "default("
644 << getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind())
645 << ")";
646}
647
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000648void OMPClausePrinter::VisitOMPProcBindClause(OMPProcBindClause *Node) {
649 OS << "proc_bind("
650 << getOpenMPSimpleClauseTypeName(OMPC_proc_bind, Node->getProcBindKind())
651 << ")";
652}
653
Alexey Bataev56dafe82014-06-20 07:16:17 +0000654void OMPClausePrinter::VisitOMPScheduleClause(OMPScheduleClause *Node) {
Alexey Bataev6402bca2015-12-28 07:25:51 +0000655 OS << "schedule(";
656 if (Node->getFirstScheduleModifier() != OMPC_SCHEDULE_MODIFIER_unknown) {
657 OS << getOpenMPSimpleClauseTypeName(OMPC_schedule,
658 Node->getFirstScheduleModifier());
659 if (Node->getSecondScheduleModifier() != OMPC_SCHEDULE_MODIFIER_unknown) {
660 OS << ", ";
661 OS << getOpenMPSimpleClauseTypeName(OMPC_schedule,
662 Node->getSecondScheduleModifier());
663 }
664 OS << ": ";
665 }
666 OS << getOpenMPSimpleClauseTypeName(OMPC_schedule, Node->getScheduleKind());
Alexey Bataev3392d762016-02-16 11:18:12 +0000667 if (auto *E = Node->getChunkSize()) {
Alexey Bataev56dafe82014-06-20 07:16:17 +0000668 OS << ", ";
Alexey Bataev60da77e2016-02-29 05:54:20 +0000669 E->printPretty(OS, nullptr, Policy);
Alexey Bataev56dafe82014-06-20 07:16:17 +0000670 }
671 OS << ")";
672}
673
Alexey Bataev10e775f2015-07-30 11:36:16 +0000674void OMPClausePrinter::VisitOMPOrderedClause(OMPOrderedClause *Node) {
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000675 OS << "ordered";
Alexey Bataev10e775f2015-07-30 11:36:16 +0000676 if (auto *Num = Node->getNumForLoops()) {
677 OS << "(";
678 Num->printPretty(OS, nullptr, Policy, 0);
679 OS << ")";
680 }
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000681}
682
Alexey Bataev236070f2014-06-20 11:19:47 +0000683void OMPClausePrinter::VisitOMPNowaitClause(OMPNowaitClause *) {
684 OS << "nowait";
685}
686
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000687void OMPClausePrinter::VisitOMPUntiedClause(OMPUntiedClause *) {
688 OS << "untied";
689}
690
Alexey Bataevb825de12015-12-07 10:51:44 +0000691void OMPClausePrinter::VisitOMPNogroupClause(OMPNogroupClause *) {
692 OS << "nogroup";
693}
694
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000695void OMPClausePrinter::VisitOMPMergeableClause(OMPMergeableClause *) {
696 OS << "mergeable";
697}
698
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000699void OMPClausePrinter::VisitOMPReadClause(OMPReadClause *) { OS << "read"; }
700
Alexey Bataevdea47612014-07-23 07:46:59 +0000701void OMPClausePrinter::VisitOMPWriteClause(OMPWriteClause *) { OS << "write"; }
702
Alexey Bataev67a4f222014-07-23 10:25:33 +0000703void OMPClausePrinter::VisitOMPUpdateClause(OMPUpdateClause *) {
704 OS << "update";
705}
706
Alexey Bataev459dec02014-07-24 06:46:57 +0000707void OMPClausePrinter::VisitOMPCaptureClause(OMPCaptureClause *) {
708 OS << "capture";
709}
710
Alexey Bataev82bad8b2014-07-24 08:55:34 +0000711void OMPClausePrinter::VisitOMPSeqCstClause(OMPSeqCstClause *) {
712 OS << "seq_cst";
713}
714
Alexey Bataev346265e2015-09-25 10:37:12 +0000715void OMPClausePrinter::VisitOMPThreadsClause(OMPThreadsClause *) {
716 OS << "threads";
717}
718
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000719void OMPClausePrinter::VisitOMPSIMDClause(OMPSIMDClause *) { OS << "simd"; }
720
Michael Wonge710d542015-08-07 16:16:36 +0000721void OMPClausePrinter::VisitOMPDeviceClause(OMPDeviceClause *Node) {
722 OS << "device(";
723 Node->getDevice()->printPretty(OS, nullptr, Policy, 0);
724 OS << ")";
725}
726
Kelvin Li099bb8c2015-11-24 20:50:12 +0000727void OMPClausePrinter::VisitOMPNumTeamsClause(OMPNumTeamsClause *Node) {
728 OS << "num_teams(";
729 Node->getNumTeams()->printPretty(OS, nullptr, Policy, 0);
730 OS << ")";
731}
732
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000733void OMPClausePrinter::VisitOMPThreadLimitClause(OMPThreadLimitClause *Node) {
734 OS << "thread_limit(";
735 Node->getThreadLimit()->printPretty(OS, nullptr, Policy, 0);
736 OS << ")";
737}
738
Alexey Bataeva0569352015-12-01 10:17:31 +0000739void OMPClausePrinter::VisitOMPPriorityClause(OMPPriorityClause *Node) {
740 OS << "priority(";
741 Node->getPriority()->printPretty(OS, nullptr, Policy, 0);
742 OS << ")";
743}
744
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000745void OMPClausePrinter::VisitOMPGrainsizeClause(OMPGrainsizeClause *Node) {
746 OS << "grainsize(";
747 Node->getGrainsize()->printPretty(OS, nullptr, Policy, 0);
748 OS << ")";
749}
750
Alexey Bataev382967a2015-12-08 12:06:20 +0000751void OMPClausePrinter::VisitOMPNumTasksClause(OMPNumTasksClause *Node) {
752 OS << "num_tasks(";
753 Node->getNumTasks()->printPretty(OS, nullptr, Policy, 0);
754 OS << ")";
755}
756
Alexey Bataev28c75412015-12-15 08:19:24 +0000757void OMPClausePrinter::VisitOMPHintClause(OMPHintClause *Node) {
758 OS << "hint(";
759 Node->getHint()->printPretty(OS, nullptr, Policy, 0);
760 OS << ")";
761}
762
Alexey Bataev756c1962013-09-24 03:17:45 +0000763template<typename T>
764void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
765 for (typename T::varlist_iterator I = Node->varlist_begin(),
766 E = Node->varlist_end();
Alexey Bataev90c228f2016-02-08 09:29:13 +0000767 I != E; ++I) {
Richard Trieuddd01ce2014-06-09 22:53:25 +0000768 assert(*I && "Expected non-null Stmt");
Alexey Bataev90c228f2016-02-08 09:29:13 +0000769 OS << (I == Node->varlist_begin() ? StartSym : ',');
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000770 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(*I)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +0000771 if (isa<OMPCapturedExprDecl>(DRE->getDecl()))
772 DRE->printPretty(OS, nullptr, Policy, 0);
Alexey Bataev90c228f2016-02-08 09:29:13 +0000773 else
774 DRE->getDecl()->printQualifiedName(OS);
775 } else
Craig Topper36250ad2014-05-12 05:36:57 +0000776 (*I)->printPretty(OS, nullptr, Policy, 0);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000777 }
Alexey Bataev756c1962013-09-24 03:17:45 +0000778}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000779
780void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) {
781 if (!Node->varlist_empty()) {
782 OS << "private";
Alexey Bataev756c1962013-09-24 03:17:45 +0000783 VisitOMPClauseList(Node, '(');
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000784 OS << ")";
785 }
786}
787
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000788void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) {
789 if (!Node->varlist_empty()) {
790 OS << "firstprivate";
791 VisitOMPClauseList(Node, '(');
792 OS << ")";
793 }
794}
795
Alexander Musman1bb328c2014-06-04 13:06:39 +0000796void OMPClausePrinter::VisitOMPLastprivateClause(OMPLastprivateClause *Node) {
797 if (!Node->varlist_empty()) {
798 OS << "lastprivate";
799 VisitOMPClauseList(Node, '(');
800 OS << ")";
801 }
802}
803
Alexey Bataev758e55e2013-09-06 18:03:48 +0000804void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
805 if (!Node->varlist_empty()) {
806 OS << "shared";
Alexey Bataev756c1962013-09-24 03:17:45 +0000807 VisitOMPClauseList(Node, '(');
Alexey Bataev758e55e2013-09-06 18:03:48 +0000808 OS << ")";
809 }
810}
811
Alexey Bataevc5e02582014-06-16 07:08:35 +0000812void OMPClausePrinter::VisitOMPReductionClause(OMPReductionClause *Node) {
813 if (!Node->varlist_empty()) {
814 OS << "reduction(";
815 NestedNameSpecifier *QualifierLoc =
816 Node->getQualifierLoc().getNestedNameSpecifier();
817 OverloadedOperatorKind OOK =
818 Node->getNameInfo().getName().getCXXOverloadedOperator();
819 if (QualifierLoc == nullptr && OOK != OO_None) {
820 // Print reduction identifier in C format
821 OS << getOperatorSpelling(OOK);
822 } else {
823 // Use C++ format
824 if (QualifierLoc != nullptr)
825 QualifierLoc->print(OS, Policy);
826 OS << Node->getNameInfo();
827 }
828 OS << ":";
829 VisitOMPClauseList(Node, ' ');
830 OS << ")";
831 }
832}
833
Alexander Musman8dba6642014-04-22 13:09:42 +0000834void OMPClausePrinter::VisitOMPLinearClause(OMPLinearClause *Node) {
835 if (!Node->varlist_empty()) {
836 OS << "linear";
Alexey Bataev182227b2015-08-20 10:54:39 +0000837 if (Node->getModifierLoc().isValid()) {
838 OS << '('
839 << getOpenMPSimpleClauseTypeName(OMPC_linear, Node->getModifier());
840 }
Alexander Musman8dba6642014-04-22 13:09:42 +0000841 VisitOMPClauseList(Node, '(');
Alexey Bataev182227b2015-08-20 10:54:39 +0000842 if (Node->getModifierLoc().isValid())
843 OS << ')';
Craig Topper36250ad2014-05-12 05:36:57 +0000844 if (Node->getStep() != nullptr) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000845 OS << ": ";
Craig Topper36250ad2014-05-12 05:36:57 +0000846 Node->getStep()->printPretty(OS, nullptr, Policy, 0);
Alexander Musman8dba6642014-04-22 13:09:42 +0000847 }
848 OS << ")";
849 }
850}
851
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000852void OMPClausePrinter::VisitOMPAlignedClause(OMPAlignedClause *Node) {
853 if (!Node->varlist_empty()) {
854 OS << "aligned";
855 VisitOMPClauseList(Node, '(');
856 if (Node->getAlignment() != nullptr) {
857 OS << ": ";
858 Node->getAlignment()->printPretty(OS, nullptr, Policy, 0);
859 }
860 OS << ")";
861 }
862}
863
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000864void OMPClausePrinter::VisitOMPCopyinClause(OMPCopyinClause *Node) {
865 if (!Node->varlist_empty()) {
866 OS << "copyin";
867 VisitOMPClauseList(Node, '(');
868 OS << ")";
869 }
870}
871
Alexey Bataevbae9a792014-06-27 10:37:06 +0000872void OMPClausePrinter::VisitOMPCopyprivateClause(OMPCopyprivateClause *Node) {
873 if (!Node->varlist_empty()) {
874 OS << "copyprivate";
875 VisitOMPClauseList(Node, '(');
876 OS << ")";
877 }
878}
879
Alexey Bataev6125da92014-07-21 11:26:11 +0000880void OMPClausePrinter::VisitOMPFlushClause(OMPFlushClause *Node) {
881 if (!Node->varlist_empty()) {
882 VisitOMPClauseList(Node, '(');
883 OS << ")";
884 }
885}
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000886
887void OMPClausePrinter::VisitOMPDependClause(OMPDependClause *Node) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000888 OS << "depend(";
889 OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(),
890 Node->getDependencyKind());
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000891 if (!Node->varlist_empty()) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000892 OS << " :";
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000893 VisitOMPClauseList(Node, ' ');
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000894 }
Alexey Bataeveb482352015-12-18 05:05:56 +0000895 OS << ")";
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000896}
Kelvin Li0bff7af2015-11-23 05:32:03 +0000897
898void OMPClausePrinter::VisitOMPMapClause(OMPMapClause *Node) {
899 if (!Node->varlist_empty()) {
900 OS << "map(";
901 if (Node->getMapType() != OMPC_MAP_unknown) {
902 if (Node->getMapTypeModifier() != OMPC_MAP_unknown) {
903 OS << getOpenMPSimpleClauseTypeName(OMPC_map,
904 Node->getMapTypeModifier());
905 OS << ',';
906 }
907 OS << getOpenMPSimpleClauseTypeName(OMPC_map, Node->getMapType());
908 OS << ':';
909 }
910 VisitOMPClauseList(Node, ' ');
911 OS << ")";
912 }
913}
Carlo Bertollib4adf552016-01-15 18:50:31 +0000914
915void OMPClausePrinter::VisitOMPDistScheduleClause(OMPDistScheduleClause *Node) {
916 OS << "dist_schedule(" << getOpenMPSimpleClauseTypeName(
917 OMPC_dist_schedule, Node->getDistScheduleKind());
Alexey Bataev3392d762016-02-16 11:18:12 +0000918 if (auto *E = Node->getChunkSize()) {
Carlo Bertollib4adf552016-01-15 18:50:31 +0000919 OS << ", ";
Alexey Bataev60da77e2016-02-29 05:54:20 +0000920 E->printPretty(OS, nullptr, Policy);
Carlo Bertollib4adf552016-01-15 18:50:31 +0000921 }
922 OS << ")";
923}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +0000924
925void OMPClausePrinter::VisitOMPDefaultmapClause(OMPDefaultmapClause *Node) {
926 OS << "defaultmap(";
927 OS << getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
928 Node->getDefaultmapModifier());
929 OS << ": ";
930 OS << getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
931 Node->getDefaultmapKind());
932 OS << ")";
933}
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000934}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000935
936//===----------------------------------------------------------------------===//
937// OpenMP directives printing methods
938//===----------------------------------------------------------------------===//
939
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000940void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000941 OMPClausePrinter Printer(OS, Policy);
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000942 ArrayRef<OMPClause *> Clauses = S->clauses();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000943 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
944 I != E; ++I)
945 if (*I && !(*I)->isImplicit()) {
946 Printer.Visit(*I);
947 OS << ' ';
948 }
949 OS << "\n";
Alexey Bataev68446b72014-07-18 07:47:19 +0000950 if (S->hasAssociatedStmt() && S->getAssociatedStmt()) {
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000951 assert(isa<CapturedStmt>(S->getAssociatedStmt()) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000952 "Expected captured statement!");
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000953 Stmt *CS = cast<CapturedStmt>(S->getAssociatedStmt())->getCapturedStmt();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000954 PrintStmt(CS);
955 }
956}
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000957
958void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
959 Indent() << "#pragma omp parallel ";
960 PrintOMPExecutableDirective(Node);
961}
962
963void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
964 Indent() << "#pragma omp simd ";
965 PrintOMPExecutableDirective(Node);
966}
967
Alexey Bataevf29276e2014-06-18 04:14:57 +0000968void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) {
969 Indent() << "#pragma omp for ";
970 PrintOMPExecutableDirective(Node);
971}
972
Alexander Musmanf82886e2014-09-18 05:12:34 +0000973void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) {
974 Indent() << "#pragma omp for simd ";
975 PrintOMPExecutableDirective(Node);
976}
977
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000978void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) {
979 Indent() << "#pragma omp sections ";
980 PrintOMPExecutableDirective(Node);
981}
982
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000983void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) {
984 Indent() << "#pragma omp section";
985 PrintOMPExecutableDirective(Node);
986}
987
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000988void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) {
989 Indent() << "#pragma omp single ";
990 PrintOMPExecutableDirective(Node);
991}
992
Alexander Musman80c22892014-07-17 08:54:58 +0000993void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) {
994 Indent() << "#pragma omp master";
995 PrintOMPExecutableDirective(Node);
996}
997
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000998void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) {
999 Indent() << "#pragma omp critical";
1000 if (Node->getDirectiveName().getName()) {
1001 OS << " (";
1002 Node->getDirectiveName().printName(OS);
1003 OS << ")";
1004 }
Alexey Bataev28c75412015-12-15 08:19:24 +00001005 OS << " ";
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001006 PrintOMPExecutableDirective(Node);
1007}
1008
Alexey Bataev4acb8592014-07-07 13:01:15 +00001009void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) {
1010 Indent() << "#pragma omp parallel for ";
1011 PrintOMPExecutableDirective(Node);
1012}
1013
Alexander Musmane4e893b2014-09-23 09:33:00 +00001014void StmtPrinter::VisitOMPParallelForSimdDirective(
1015 OMPParallelForSimdDirective *Node) {
1016 Indent() << "#pragma omp parallel for simd ";
1017 PrintOMPExecutableDirective(Node);
1018}
1019
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001020void StmtPrinter::VisitOMPParallelSectionsDirective(
1021 OMPParallelSectionsDirective *Node) {
1022 Indent() << "#pragma omp parallel sections ";
1023 PrintOMPExecutableDirective(Node);
1024}
1025
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001026void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) {
1027 Indent() << "#pragma omp task ";
1028 PrintOMPExecutableDirective(Node);
1029}
1030
Alexey Bataev68446b72014-07-18 07:47:19 +00001031void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) {
1032 Indent() << "#pragma omp taskyield";
1033 PrintOMPExecutableDirective(Node);
1034}
1035
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001036void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) {
1037 Indent() << "#pragma omp barrier";
1038 PrintOMPExecutableDirective(Node);
1039}
1040
Alexey Bataev2df347a2014-07-18 10:17:07 +00001041void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) {
1042 Indent() << "#pragma omp taskwait";
1043 PrintOMPExecutableDirective(Node);
1044}
1045
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001046void StmtPrinter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective *Node) {
1047 Indent() << "#pragma omp taskgroup";
1048 PrintOMPExecutableDirective(Node);
1049}
1050
Alexey Bataev6125da92014-07-21 11:26:11 +00001051void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) {
1052 Indent() << "#pragma omp flush ";
1053 PrintOMPExecutableDirective(Node);
1054}
1055
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001056void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) {
Alexey Bataev346265e2015-09-25 10:37:12 +00001057 Indent() << "#pragma omp ordered ";
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001058 PrintOMPExecutableDirective(Node);
1059}
1060
Alexey Bataev0162e452014-07-22 10:10:35 +00001061void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) {
1062 Indent() << "#pragma omp atomic ";
1063 PrintOMPExecutableDirective(Node);
1064}
1065
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001066void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) {
1067 Indent() << "#pragma omp target ";
1068 PrintOMPExecutableDirective(Node);
1069}
1070
Michael Wong65f367f2015-07-21 13:44:28 +00001071void StmtPrinter::VisitOMPTargetDataDirective(OMPTargetDataDirective *Node) {
1072 Indent() << "#pragma omp target data ";
1073 PrintOMPExecutableDirective(Node);
1074}
1075
Samuel Antaodf67fc42016-01-19 19:15:56 +00001076void StmtPrinter::VisitOMPTargetEnterDataDirective(
1077 OMPTargetEnterDataDirective *Node) {
1078 Indent() << "#pragma omp target enter data ";
1079 PrintOMPExecutableDirective(Node);
1080}
1081
Samuel Antao72590762016-01-19 20:04:50 +00001082void StmtPrinter::VisitOMPTargetExitDataDirective(
1083 OMPTargetExitDataDirective *Node) {
1084 Indent() << "#pragma omp target exit data ";
1085 PrintOMPExecutableDirective(Node);
1086}
1087
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001088void StmtPrinter::VisitOMPTargetParallelDirective(
1089 OMPTargetParallelDirective *Node) {
1090 Indent() << "#pragma omp target parallel ";
1091 PrintOMPExecutableDirective(Node);
1092}
1093
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001094void StmtPrinter::VisitOMPTargetParallelForDirective(
1095 OMPTargetParallelForDirective *Node) {
1096 Indent() << "#pragma omp target parallel for ";
1097 PrintOMPExecutableDirective(Node);
1098}
1099
Alexey Bataev13314bf2014-10-09 04:18:56 +00001100void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) {
1101 Indent() << "#pragma omp teams ";
1102 PrintOMPExecutableDirective(Node);
1103}
1104
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001105void StmtPrinter::VisitOMPCancellationPointDirective(
1106 OMPCancellationPointDirective *Node) {
1107 Indent() << "#pragma omp cancellation point "
1108 << getOpenMPDirectiveName(Node->getCancelRegion());
1109 PrintOMPExecutableDirective(Node);
1110}
Alexey Bataev80909872015-07-02 11:25:17 +00001111
1112void StmtPrinter::VisitOMPCancelDirective(OMPCancelDirective *Node) {
1113 Indent() << "#pragma omp cancel "
Alexey Bataev87933c72015-09-18 08:07:34 +00001114 << getOpenMPDirectiveName(Node->getCancelRegion()) << " ";
Alexey Bataev80909872015-07-02 11:25:17 +00001115 PrintOMPExecutableDirective(Node);
1116}
Alexey Bataev49f6e782015-12-01 04:18:41 +00001117
1118void StmtPrinter::VisitOMPTaskLoopDirective(OMPTaskLoopDirective *Node) {
1119 Indent() << "#pragma omp taskloop ";
1120 PrintOMPExecutableDirective(Node);
1121}
1122
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001123void StmtPrinter::VisitOMPTaskLoopSimdDirective(
1124 OMPTaskLoopSimdDirective *Node) {
1125 Indent() << "#pragma omp taskloop simd ";
1126 PrintOMPExecutableDirective(Node);
1127}
1128
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001129void StmtPrinter::VisitOMPDistributeDirective(OMPDistributeDirective *Node) {
1130 Indent() << "#pragma omp distribute ";
1131 PrintOMPExecutableDirective(Node);
1132}
1133
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001134//===----------------------------------------------------------------------===//
Chris Lattner71e23ce2006-11-04 20:18:38 +00001135// Expr printing methods.
1136//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001137
Chris Lattner882f7882006-11-04 18:52:07 +00001138void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00001139 if (auto *OCED = dyn_cast<OMPCapturedExprDecl>(Node->getDecl())) {
1140 OCED->getInit()->IgnoreImpCasts()->printPretty(OS, nullptr, Policy);
1141 return;
1142 }
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001143 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1144 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001145 if (Node->hasTemplateKeyword())
1146 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001147 OS << Node->getNameInfo();
John McCallb3774b52010-08-19 23:49:38 +00001148 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +00001149 TemplateSpecializationType::PrintTemplateArgumentList(
1150 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregorc7acfdf2009-01-06 05:10:23 +00001151}
1152
John McCall8cd78132009-11-19 22:55:06 +00001153void StmtPrinter::VisitDependentScopeDeclRefExpr(
1154 DependentScopeDeclRefExpr *Node) {
Axel Naumann20b27862011-01-24 15:44:00 +00001155 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1156 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001157 if (Node->hasTemplateKeyword())
1158 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001159 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +00001160 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +00001161 TemplateSpecializationType::PrintTemplateArgumentList(
1162 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregor90a1a652009-03-19 17:26:29 +00001163}
1164
John McCalld14a8642009-11-21 08:51:07 +00001165void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregora727cb92009-06-30 22:34:41 +00001166 if (Node->getQualifier())
1167 Node->getQualifier()->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001168 if (Node->hasTemplateKeyword())
1169 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001170 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +00001171 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +00001172 TemplateSpecializationType::PrintTemplateArgumentList(
1173 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregora727cb92009-06-30 22:34:41 +00001174}
1175
Steve Naroffe46504b2007-11-12 14:29:37 +00001176void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +00001177 if (Node->getBase()) {
1178 PrintExpr(Node->getBase());
1179 OS << (Node->isArrow() ? "->" : ".");
1180 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001181 OS << *Node->getDecl();
Steve Naroffe46504b2007-11-12 14:29:37 +00001182}
1183
Steve Naroffec944032008-05-30 00:40:33 +00001184void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Fariborz Jahanian681c0752010-10-14 16:04:05 +00001185 if (Node->isSuperReceiver())
1186 OS << "super.";
Richard Trieu6d92e502014-02-06 23:26:23 +00001187 else if (Node->isObjectReceiver() && Node->getBase()) {
Steve Naroffec944032008-05-30 00:40:33 +00001188 PrintExpr(Node->getBase());
1189 OS << ".";
Richard Trieu6d92e502014-02-06 23:26:23 +00001190 } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
1191 OS << Node->getClassReceiver()->getName() << ".";
Steve Naroffec944032008-05-30 00:40:33 +00001192 }
Fariborz Jahanian681c0752010-10-14 16:04:05 +00001193
John McCallb7bd14f2010-12-02 01:19:52 +00001194 if (Node->isImplicitProperty())
Aaron Ballmanb190f972014-01-03 17:59:55 +00001195 Node->getImplicitPropertyGetter()->getSelector().print(OS);
John McCallb7bd14f2010-12-02 01:19:52 +00001196 else
1197 OS << Node->getExplicitProperty()->getName();
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00001198}
1199
Ted Kremeneke65b0862012-03-06 20:05:56 +00001200void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
1201
1202 PrintExpr(Node->getBaseExpr());
1203 OS << "[";
1204 PrintExpr(Node->getKeyExpr());
1205 OS << "]";
1206}
1207
Chris Lattner6307f192008-08-10 01:53:14 +00001208void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Alexey Bataevec474782014-10-09 08:45:04 +00001209 OS << PredefinedExpr::getIdentTypeName(Node->getIdentType());
Anders Carlsson625bfc82007-07-21 05:21:51 +00001210}
1211
Steve Naroffae4143e2007-04-26 20:39:23 +00001212void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +00001213 unsigned value = Node->getValue();
Douglas Gregorfb65e592011-07-27 05:40:30 +00001214
1215 switch (Node->getKind()) {
1216 case CharacterLiteral::Ascii: break; // no prefix.
1217 case CharacterLiteral::Wide: OS << 'L'; break;
Aaron Ballman9a17c852016-01-07 20:59:26 +00001218 case CharacterLiteral::UTF8: OS << "u8"; break;
Douglas Gregorfb65e592011-07-27 05:40:30 +00001219 case CharacterLiteral::UTF16: OS << 'u'; break;
1220 case CharacterLiteral::UTF32: OS << 'U'; break;
1221 }
1222
Chris Lattner666115c2007-07-13 23:58:20 +00001223 switch (value) {
1224 case '\\':
1225 OS << "'\\\\'";
1226 break;
1227 case '\'':
1228 OS << "'\\''";
1229 break;
1230 case '\a':
1231 // TODO: K&R: the meaning of '\\a' is different in traditional C
1232 OS << "'\\a'";
1233 break;
1234 case '\b':
1235 OS << "'\\b'";
1236 break;
1237 // Nonstandard escape sequence.
1238 /*case '\e':
1239 OS << "'\\e'";
1240 break;*/
1241 case '\f':
1242 OS << "'\\f'";
1243 break;
1244 case '\n':
1245 OS << "'\\n'";
1246 break;
1247 case '\r':
1248 OS << "'\\r'";
1249 break;
1250 case '\t':
1251 OS << "'\\t'";
1252 break;
1253 case '\v':
1254 OS << "'\\v'";
1255 break;
1256 default:
Steven Watanabee43ae192016-02-13 02:31:28 +00001257 // A character literal might be sign-extended, which
1258 // would result in an invalid \U escape sequence.
1259 // FIXME: multicharacter literals such as '\xFF\xFF\xFF\xFF'
1260 // are not correctly handled.
1261 if ((value & ~0xFFu) == ~0xFFu && Node->getKind() == CharacterLiteral::Ascii)
1262 value &= 0xFFu;
Jordan Rose00d1b592013-02-08 22:30:27 +00001263 if (value < 256 && isPrintable((unsigned char)value))
Chris Lattner666115c2007-07-13 23:58:20 +00001264 OS << "'" << (char)value << "'";
Jordan Rose00d1b592013-02-08 22:30:27 +00001265 else if (value < 256)
1266 OS << "'\\x" << llvm::format("%02x", value) << "'";
1267 else if (value <= 0xFFFF)
1268 OS << "'\\u" << llvm::format("%04x", value) << "'";
1269 else
1270 OS << "'\\U" << llvm::format("%08x", value) << "'";
Chris Lattner6e9d9b32007-07-13 05:18:11 +00001271 }
Steve Naroffae4143e2007-04-26 20:39:23 +00001272}
1273
Steve Naroffdf7855b2007-02-21 23:46:25 +00001274void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +00001275 bool isSigned = Node->getType()->isSignedIntegerType();
1276 OS << Node->getValue().toString(10, isSigned);
Mike Stump11289f42009-09-09 15:08:12 +00001277
Chris Lattner06430412007-05-21 05:45:03 +00001278 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall9dd450b2009-09-21 23:43:11 +00001279 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
David Blaikie83d382b2011-09-23 05:06:16 +00001280 default: llvm_unreachable("Unexpected type for integer literal!");
David Majnemerbe09e8e2015-03-06 18:04:22 +00001281 case BuiltinType::Char_S:
1282 case BuiltinType::Char_U: OS << "i8"; break;
David Majnemer65a407c2014-06-21 18:46:07 +00001283 case BuiltinType::UChar: OS << "Ui8"; break;
1284 case BuiltinType::Short: OS << "i16"; break;
1285 case BuiltinType::UShort: OS << "Ui16"; break;
Chris Lattner06430412007-05-21 05:45:03 +00001286 case BuiltinType::Int: break; // no suffix.
1287 case BuiltinType::UInt: OS << 'U'; break;
1288 case BuiltinType::Long: OS << 'L'; break;
1289 case BuiltinType::ULong: OS << "UL"; break;
1290 case BuiltinType::LongLong: OS << "LL"; break;
1291 case BuiltinType::ULongLong: OS << "ULL"; break;
1292 }
Chris Lattner882f7882006-11-04 18:52:07 +00001293}
Benjamin Kramer8a526762012-09-20 14:07:17 +00001294
1295static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
1296 bool PrintSuffix) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001297 SmallString<16> Str;
Eli Friedman53392062011-10-05 20:32:03 +00001298 Node->getValue().toString(Str);
1299 OS << Str;
Benjamin Kramer8a526762012-09-20 14:07:17 +00001300 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
1301 OS << '.'; // Trailing dot in order to separate from ints.
1302
1303 if (!PrintSuffix)
1304 return;
1305
1306 // Emit suffixes. Float literals are always a builtin float type.
1307 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
1308 default: llvm_unreachable("Unexpected type for float literal!");
1309 case BuiltinType::Half: break; // FIXME: suffix?
1310 case BuiltinType::Double: break; // no suffix.
1311 case BuiltinType::Float: OS << 'F'; break;
1312 case BuiltinType::LongDouble: OS << 'L'; break;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00001313 case BuiltinType::Float128: OS << 'Q'; break;
Benjamin Kramer8a526762012-09-20 14:07:17 +00001314 }
1315}
1316
1317void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
1318 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
Chris Lattner882f7882006-11-04 18:52:07 +00001319}
Chris Lattner1c20a172007-08-26 03:42:43 +00001320
1321void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
1322 PrintExpr(Node->getSubExpr());
1323 OS << "i";
1324}
1325
Steve Naroffdf7855b2007-02-21 23:46:25 +00001326void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Richard Trieudc355912012-06-13 20:25:24 +00001327 Str->outputString(OS);
Chris Lattner882f7882006-11-04 18:52:07 +00001328}
1329void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
1330 OS << "(";
1331 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +00001332 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +00001333}
1334void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +00001335 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +00001336 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump11289f42009-09-09 15:08:12 +00001337
Eli Friedman1cf25362009-06-14 22:39:26 +00001338 // Print a space if this is an "identifier operator" like __real, or if
1339 // it might be concatenated incorrectly like '+'.
Chris Lattnere5b60442007-08-23 21:46:40 +00001340 switch (Node->getOpcode()) {
1341 default: break;
John McCalle3027922010-08-25 11:45:40 +00001342 case UO_Real:
1343 case UO_Imag:
1344 case UO_Extension:
Chris Lattnere5b60442007-08-23 21:46:40 +00001345 OS << ' ';
1346 break;
John McCalle3027922010-08-25 11:45:40 +00001347 case UO_Plus:
1348 case UO_Minus:
Eli Friedman1cf25362009-06-14 22:39:26 +00001349 if (isa<UnaryOperator>(Node->getSubExpr()))
1350 OS << ' ';
1351 break;
Chris Lattnere5b60442007-08-23 21:46:40 +00001352 }
1353 }
Chris Lattner882f7882006-11-04 18:52:07 +00001354 PrintExpr(Node->getSubExpr());
Mike Stump11289f42009-09-09 15:08:12 +00001355
Chris Lattner15768702006-11-05 23:54:51 +00001356 if (Node->isPostfix())
1357 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +00001358}
Chris Lattner98dbf0a2007-08-30 17:59:59 +00001359
Douglas Gregor882211c2010-04-28 22:16:22 +00001360void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
1361 OS << "__builtin_offsetof(";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001362 Node->getTypeSourceInfo()->getType().print(OS, Policy);
1363 OS << ", ";
Douglas Gregor882211c2010-04-28 22:16:22 +00001364 bool PrintedSomething = false;
1365 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
James Y Knight7281c352015-12-29 22:31:18 +00001366 OffsetOfNode ON = Node->getComponent(i);
1367 if (ON.getKind() == OffsetOfNode::Array) {
Douglas Gregor882211c2010-04-28 22:16:22 +00001368 // Array node
1369 OS << "[";
1370 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
1371 OS << "]";
1372 PrintedSomething = true;
1373 continue;
1374 }
Douglas Gregord1702062010-04-29 00:18:15 +00001375
1376 // Skip implicit base indirections.
James Y Knight7281c352015-12-29 22:31:18 +00001377 if (ON.getKind() == OffsetOfNode::Base)
Douglas Gregord1702062010-04-29 00:18:15 +00001378 continue;
1379
Douglas Gregor882211c2010-04-28 22:16:22 +00001380 // Field or identifier node.
1381 IdentifierInfo *Id = ON.getFieldName();
1382 if (!Id)
1383 continue;
Alexis Hunta8136cc2010-05-05 15:23:54 +00001384
Douglas Gregor882211c2010-04-28 22:16:22 +00001385 if (PrintedSomething)
1386 OS << ".";
1387 else
1388 PrintedSomething = true;
Alexis Hunta8136cc2010-05-05 15:23:54 +00001389 OS << Id->getName();
Douglas Gregor882211c2010-04-28 22:16:22 +00001390 }
1391 OS << ")";
1392}
1393
Peter Collingbournee190dee2011-03-11 19:24:49 +00001394void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
1395 switch(Node->getKind()) {
1396 case UETT_SizeOf:
1397 OS << "sizeof";
1398 break;
1399 case UETT_AlignOf:
Richard Smith301bc212016-05-19 01:39:10 +00001400 if (Policy.Alignof)
Jordan Rose58d54722012-06-30 21:33:57 +00001401 OS << "alignof";
Richard Smith301bc212016-05-19 01:39:10 +00001402 else if (Policy.UnderscoreAlignof)
Jordan Rose58d54722012-06-30 21:33:57 +00001403 OS << "_Alignof";
1404 else
1405 OS << "__alignof";
Peter Collingbournee190dee2011-03-11 19:24:49 +00001406 break;
1407 case UETT_VecStep:
1408 OS << "vec_step";
1409 break;
Alexey Bataev00396512015-07-02 03:40:19 +00001410 case UETT_OpenMPRequiredSimdAlign:
1411 OS << "__builtin_omp_required_simd_align";
1412 break;
Peter Collingbournee190dee2011-03-11 19:24:49 +00001413 }
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001414 if (Node->isArgumentType()) {
1415 OS << '(';
1416 Node->getArgumentType().print(OS, Policy);
1417 OS << ')';
1418 } else {
Sebastian Redl6f282892008-11-11 17:56:53 +00001419 OS << " ";
1420 PrintExpr(Node->getArgumentExpr());
1421 }
Chris Lattner882f7882006-11-04 18:52:07 +00001422}
Peter Collingbourne91147592011-04-15 00:35:48 +00001423
1424void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
1425 OS << "_Generic(";
1426 PrintExpr(Node->getControllingExpr());
1427 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
1428 OS << ", ";
1429 QualType T = Node->getAssocType(i);
1430 if (T.isNull())
1431 OS << "default";
1432 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001433 T.print(OS, Policy);
Peter Collingbourne91147592011-04-15 00:35:48 +00001434 OS << ": ";
1435 PrintExpr(Node->getAssocExpr(i));
1436 }
1437 OS << ")";
1438}
1439
Chris Lattner882f7882006-11-04 18:52:07 +00001440void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +00001441 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +00001442 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +00001443 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +00001444 OS << "]";
1445}
1446
Alexey Bataev1a3320e2015-08-25 14:24:04 +00001447void StmtPrinter::VisitOMPArraySectionExpr(OMPArraySectionExpr *Node) {
1448 PrintExpr(Node->getBase());
1449 OS << "[";
1450 if (Node->getLowerBound())
1451 PrintExpr(Node->getLowerBound());
Alexey Bataeved5fb672015-09-11 04:54:28 +00001452 if (Node->getColonLoc().isValid()) {
1453 OS << ":";
1454 if (Node->getLength())
1455 PrintExpr(Node->getLength());
1456 }
Alexey Bataev1a3320e2015-08-25 14:24:04 +00001457 OS << "]";
1458}
1459
Peter Collingbourne4b279a02011-02-08 21:17:54 +00001460void StmtPrinter::PrintCallArgs(CallExpr *Call) {
Chris Lattner882f7882006-11-04 18:52:07 +00001461 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001462 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1463 // Don't print any defaulted arguments
1464 break;
1465 }
1466
Chris Lattner882f7882006-11-04 18:52:07 +00001467 if (i) OS << ", ";
1468 PrintExpr(Call->getArg(i));
1469 }
Peter Collingbourne4b279a02011-02-08 21:17:54 +00001470}
1471
1472void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1473 PrintExpr(Call->getCallee());
1474 OS << "(";
1475 PrintCallArgs(Call);
Chris Lattner882f7882006-11-04 18:52:07 +00001476 OS << ")";
1477}
1478void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregore4b414c2009-01-08 22:45:41 +00001479 // FIXME: Suppress printing implicit bases (like "this")
1480 PrintExpr(Node->getBase());
David Blaikie8fab8e52012-11-12 19:12:12 +00001481
1482 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
David Blaikie6bffd6f2012-11-12 19:32:32 +00001483 FieldDecl *ParentDecl = ParentMember
Craig Topper36250ad2014-05-12 05:36:57 +00001484 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : nullptr;
David Blaikie8fab8e52012-11-12 19:12:12 +00001485
David Blaikie6bffd6f2012-11-12 19:32:32 +00001486 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
David Blaikie8fab8e52012-11-12 19:12:12 +00001487 OS << (Node->isArrow() ? "->" : ".");
David Blaikie8fab8e52012-11-12 19:12:12 +00001488
Fariborz Jahanian2990c022010-01-11 21:17:32 +00001489 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1490 if (FD->isAnonymousStructOrUnion())
1491 return;
David Blaikie8fab8e52012-11-12 19:12:12 +00001492
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001493 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1494 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001495 if (Node->hasTemplateKeyword())
1496 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001497 OS << Node->getMemberNameInfo();
John McCallb3774b52010-08-19 23:49:38 +00001498 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +00001499 TemplateSpecializationType::PrintTemplateArgumentList(
1500 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001501}
Steve Naroffe87026a2009-07-24 17:54:45 +00001502void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1503 PrintExpr(Node->getBase());
1504 OS << (Node->isArrow() ? "->isa" : ".isa");
1505}
1506
Nate Begemance4d7fc2008-04-18 23:10:10 +00001507void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +00001508 PrintExpr(Node->getBase());
1509 OS << ".";
1510 OS << Node->getAccessor().getName();
1511}
Douglas Gregorf19b2312008-10-28 15:36:24 +00001512void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001513 OS << '(';
1514 Node->getTypeAsWritten().print(OS, Policy);
1515 OS << ')';
Chris Lattner882f7882006-11-04 18:52:07 +00001516 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001517}
Steve Naroff57eb2c52007-07-19 21:32:11 +00001518void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001519 OS << '(';
1520 Node->getType().print(OS, Policy);
1521 OS << ')';
Steve Naroff57eb2c52007-07-19 21:32:11 +00001522 PrintExpr(Node->getInitializer());
1523}
Steve Naroff7a5af782007-07-13 16:58:59 +00001524void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Alp Toker028ed912013-12-06 17:56:43 +00001525 // No need to print anything, simply forward to the subexpression.
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001526 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +00001527}
Chris Lattner882f7882006-11-04 18:52:07 +00001528void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1529 PrintExpr(Node->getLHS());
1530 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1531 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001532}
Chris Lattner86928112007-08-25 02:00:02 +00001533void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1534 PrintExpr(Node->getLHS());
1535 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1536 PrintExpr(Node->getRHS());
1537}
Chris Lattner882f7882006-11-04 18:52:07 +00001538void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1539 PrintExpr(Node->getCond());
John McCallc07a0c72011-02-17 10:25:35 +00001540 OS << " ? ";
1541 PrintExpr(Node->getLHS());
1542 OS << " : ";
Chris Lattner882f7882006-11-04 18:52:07 +00001543 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001544}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001545
Chris Lattnereefa10e2007-05-28 06:56:27 +00001546// GNU extensions.
1547
John McCallc07a0c72011-02-17 10:25:35 +00001548void
1549StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1550 PrintExpr(Node->getCommon());
1551 OS << " ?: ";
1552 PrintExpr(Node->getFalseExpr());
1553}
Chris Lattnerd268a7a2007-08-03 17:31:20 +00001554void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +00001555 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +00001556}
1557
Chris Lattner366727f2007-07-24 16:58:17 +00001558void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1559 OS << "(";
1560 PrintRawCompoundStmt(E->getSubStmt());
1561 OS << ")";
1562}
1563
Steve Naroff9efdabc2007-08-03 21:21:27 +00001564void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1565 OS << "__builtin_choose_expr(";
1566 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +00001567 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +00001568 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +00001569 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +00001570 PrintExpr(Node->getRHS());
1571 OS << ")";
1572}
Chris Lattner366727f2007-07-24 16:58:17 +00001573
Douglas Gregor3be4b122008-11-29 04:51:27 +00001574void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1575 OS << "__null";
1576}
1577
Eli Friedmana1b4ed82008-05-14 19:38:39 +00001578void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1579 OS << "__builtin_shufflevector(";
1580 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1581 if (i) OS << ", ";
1582 PrintExpr(Node->getExpr(i));
1583 }
1584 OS << ")";
1585}
1586
Hal Finkelc4d7c822013-09-18 03:29:45 +00001587void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1588 OS << "__builtin_convertvector(";
1589 PrintExpr(Node->getSrcExpr());
1590 OS << ", ";
1591 Node->getType().print(OS, Policy);
1592 OS << ")";
1593}
1594
Anders Carlsson4692db02007-08-31 04:56:16 +00001595void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001596 if (Node->getSyntacticForm()) {
1597 Visit(Node->getSyntacticForm());
1598 return;
1599 }
1600
Richard Smithd0e102f2015-01-30 02:04:26 +00001601 OS << "{";
Anders Carlsson4692db02007-08-31 04:56:16 +00001602 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1603 if (i) OS << ", ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001604 if (Node->getInit(i))
1605 PrintExpr(Node->getInit(i));
1606 else
Richard Smithd0e102f2015-01-30 02:04:26 +00001607 OS << "{}";
Anders Carlsson4692db02007-08-31 04:56:16 +00001608 }
Richard Smithd0e102f2015-01-30 02:04:26 +00001609 OS << "}";
Anders Carlsson4692db02007-08-31 04:56:16 +00001610}
1611
Nate Begeman5ec4b312009-08-10 23:49:36 +00001612void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
Richard Smithd0e102f2015-01-30 02:04:26 +00001613 OS << "(";
Nate Begeman5ec4b312009-08-10 23:49:36 +00001614 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1615 if (i) OS << ", ";
1616 PrintExpr(Node->getExpr(i));
1617 }
Richard Smithd0e102f2015-01-30 02:04:26 +00001618 OS << ")";
Nate Begeman5ec4b312009-08-10 23:49:36 +00001619}
1620
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001621void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Justin Bognercb337032015-05-28 22:19:36 +00001622 bool NeedsEquals = true;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001623 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
1624 DEnd = Node->designators_end();
1625 D != DEnd; ++D) {
1626 if (D->isFieldDesignator()) {
Serge Pavloveb57aa62014-06-20 17:08:28 +00001627 if (D->getDotLoc().isInvalid()) {
Justin Bognercb337032015-05-28 22:19:36 +00001628 if (IdentifierInfo *II = D->getFieldName()) {
Serge Pavloveb57aa62014-06-20 17:08:28 +00001629 OS << II->getName() << ":";
Justin Bognercb337032015-05-28 22:19:36 +00001630 NeedsEquals = false;
1631 }
Serge Pavloveb57aa62014-06-20 17:08:28 +00001632 } else {
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001633 OS << "." << D->getFieldName()->getName();
Serge Pavloveb57aa62014-06-20 17:08:28 +00001634 }
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001635 } else {
1636 OS << "[";
1637 if (D->isArrayDesignator()) {
1638 PrintExpr(Node->getArrayIndex(*D));
1639 } else {
1640 PrintExpr(Node->getArrayRangeStart(*D));
1641 OS << " ... ";
Mike Stump11289f42009-09-09 15:08:12 +00001642 PrintExpr(Node->getArrayRangeEnd(*D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001643 }
1644 OS << "]";
1645 }
1646 }
1647
Justin Bognercb337032015-05-28 22:19:36 +00001648 if (NeedsEquals)
1649 OS << " = ";
1650 else
1651 OS << " ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001652 PrintExpr(Node->getInit());
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001653}
1654
Yunzhong Gaocb779302015-06-10 00:27:52 +00001655void StmtPrinter::VisitDesignatedInitUpdateExpr(
1656 DesignatedInitUpdateExpr *Node) {
1657 OS << "{";
1658 OS << "/*base*/";
1659 PrintExpr(Node->getBase());
1660 OS << ", ";
1661
1662 OS << "/*updater*/";
1663 PrintExpr(Node->getUpdater());
1664 OS << "}";
1665}
1666
1667void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) {
1668 OS << "/*no init*/";
1669}
1670
Douglas Gregor0202cb42009-01-29 17:44:32 +00001671void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Richard Smith301bc212016-05-19 01:39:10 +00001672 if (Node->getType()->getAsCXXRecordDecl()) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001673 OS << "/*implicit*/";
1674 Node->getType().print(OS, Policy);
1675 OS << "()";
1676 } else {
1677 OS << "/*implicit*/(";
1678 Node->getType().print(OS, Policy);
1679 OS << ')';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001680 if (Node->getType()->isRecordType())
1681 OS << "{}";
1682 else
1683 OS << 0;
1684 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001685}
1686
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001687void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman79635842009-05-30 04:20:30 +00001688 OS << "__builtin_va_arg(";
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001689 PrintExpr(Node->getSubExpr());
1690 OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001691 Node->getType().print(OS, Policy);
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001692 OS << ")";
1693}
1694
John McCallfe96e0b2011-11-06 09:01:30 +00001695void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1696 PrintExpr(Node->getSyntacticForm());
1697}
1698
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001699void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
Craig Topper36250ad2014-05-12 05:36:57 +00001700 const char *Name = nullptr;
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001701 switch (Node->getOp()) {
Richard Smithfeea8832012-04-12 05:08:17 +00001702#define BUILTIN(ID, TYPE, ATTRS)
1703#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1704 case AtomicExpr::AO ## ID: \
1705 Name = #ID "("; \
1706 break;
1707#include "clang/Basic/Builtins.def"
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001708 }
1709 OS << Name;
Richard Smithfeea8832012-04-12 05:08:17 +00001710
1711 // AtomicExpr stores its subexpressions in a permuted order.
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001712 PrintExpr(Node->getPtr());
Richard Smithfeea8832012-04-12 05:08:17 +00001713 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1714 Node->getOp() != AtomicExpr::AO__atomic_load_n) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001715 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001716 PrintExpr(Node->getVal1());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001717 }
Richard Smithfeea8832012-04-12 05:08:17 +00001718 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1719 Node->isCmpXChg()) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001720 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001721 PrintExpr(Node->getVal2());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001722 }
Richard Smithfeea8832012-04-12 05:08:17 +00001723 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1724 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
Richard Smithfeea8832012-04-12 05:08:17 +00001725 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001726 PrintExpr(Node->getWeak());
Richard Smithfeea8832012-04-12 05:08:17 +00001727 }
Richard Smithdf6bee82013-05-01 19:02:43 +00001728 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) {
1729 OS << ", ";
David Chisnallfa35df62012-01-16 17:27:18 +00001730 PrintExpr(Node->getOrder());
Richard Smithdf6bee82013-05-01 19:02:43 +00001731 }
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001732 if (Node->isCmpXChg()) {
1733 OS << ", ";
1734 PrintExpr(Node->getOrderFail());
1735 }
1736 OS << ")";
1737}
1738
Chris Lattnereefa10e2007-05-28 06:56:27 +00001739// C++
Douglas Gregor993603d2008-11-14 16:09:21 +00001740void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1741 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1742 "",
1743#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1744 Spelling,
1745#include "clang/Basic/OperatorKinds.def"
1746 };
1747
1748 OverloadedOperatorKind Kind = Node->getOperator();
1749 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1750 if (Node->getNumArgs() == 1) {
1751 OS << OpStrings[Kind] << ' ';
1752 PrintExpr(Node->getArg(0));
1753 } else {
1754 PrintExpr(Node->getArg(0));
1755 OS << ' ' << OpStrings[Kind];
1756 }
Eli Friedman8e236422012-10-12 22:45:14 +00001757 } else if (Kind == OO_Arrow) {
1758 PrintExpr(Node->getArg(0));
Douglas Gregor993603d2008-11-14 16:09:21 +00001759 } else if (Kind == OO_Call) {
1760 PrintExpr(Node->getArg(0));
1761 OS << '(';
1762 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1763 if (ArgIdx > 1)
1764 OS << ", ";
1765 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1766 PrintExpr(Node->getArg(ArgIdx));
1767 }
1768 OS << ')';
1769 } else if (Kind == OO_Subscript) {
1770 PrintExpr(Node->getArg(0));
1771 OS << '[';
1772 PrintExpr(Node->getArg(1));
1773 OS << ']';
1774 } else if (Node->getNumArgs() == 1) {
1775 OS << OpStrings[Kind] << ' ';
1776 PrintExpr(Node->getArg(0));
1777 } else if (Node->getNumArgs() == 2) {
1778 PrintExpr(Node->getArg(0));
1779 OS << ' ' << OpStrings[Kind] << ' ';
1780 PrintExpr(Node->getArg(1));
1781 } else {
David Blaikie83d382b2011-09-23 05:06:16 +00001782 llvm_unreachable("unknown overloaded operator");
Douglas Gregor993603d2008-11-14 16:09:21 +00001783 }
1784}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001785
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001786void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
Benjamin Kramer00e8a192014-02-25 18:03:55 +00001787 // If we have a conversion operator call only print the argument.
1788 CXXMethodDecl *MD = Node->getMethodDecl();
1789 if (MD && isa<CXXConversionDecl>(MD)) {
1790 PrintExpr(Node->getImplicitObjectArgument());
1791 return;
1792 }
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001793 VisitCallExpr(cast<CallExpr>(Node));
1794}
1795
Peter Collingbourne41f85462011-02-09 21:07:24 +00001796void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1797 PrintExpr(Node->getCallee());
1798 OS << "<<<";
1799 PrintCallArgs(Node->getConfig());
1800 OS << ">>>(";
1801 PrintCallArgs(Node);
1802 OS << ")";
1803}
1804
Douglas Gregore200adc2008-10-27 19:41:14 +00001805void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1806 OS << Node->getCastName() << '<';
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001807 Node->getTypeAsWritten().print(OS, Policy);
1808 OS << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +00001809 PrintExpr(Node->getSubExpr());
1810 OS << ")";
1811}
1812
Douglas Gregore200adc2008-10-27 19:41:14 +00001813void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1814 VisitCXXNamedCastExpr(Node);
1815}
1816
1817void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1818 VisitCXXNamedCastExpr(Node);
1819}
1820
1821void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1822 VisitCXXNamedCastExpr(Node);
1823}
1824
1825void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1826 VisitCXXNamedCastExpr(Node);
1827}
1828
Sebastian Redlc4704762008-11-11 11:37:55 +00001829void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1830 OS << "typeid(";
1831 if (Node->isTypeOperand()) {
David Majnemer143c55e2013-09-27 07:04:31 +00001832 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
Sebastian Redlc4704762008-11-11 11:37:55 +00001833 } else {
1834 PrintExpr(Node->getExprOperand());
1835 }
1836 OS << ")";
1837}
1838
Francois Pichet9f4f2072010-09-08 12:20:18 +00001839void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1840 OS << "__uuidof(";
1841 if (Node->isTypeOperand()) {
David Majnemer143c55e2013-09-27 07:04:31 +00001842 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
Francois Pichet9f4f2072010-09-08 12:20:18 +00001843 } else {
1844 PrintExpr(Node->getExprOperand());
1845 }
1846 OS << ")";
1847}
1848
John McCall5e77d762013-04-16 07:28:30 +00001849void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1850 PrintExpr(Node->getBaseExpr());
1851 if (Node->isArrow())
1852 OS << "->";
1853 else
1854 OS << ".";
1855 if (NestedNameSpecifier *Qualifier =
1856 Node->getQualifierLoc().getNestedNameSpecifier())
1857 Qualifier->print(OS, Policy);
1858 OS << Node->getPropertyDecl()->getDeclName();
1859}
1860
Alexey Bataevf7630272015-11-25 12:01:00 +00001861void StmtPrinter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr *Node) {
1862 PrintExpr(Node->getBase());
1863 OS << "[";
1864 PrintExpr(Node->getIdx());
1865 OS << "]";
1866}
1867
Richard Smithc67fdd42012-03-07 08:35:16 +00001868void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1869 switch (Node->getLiteralOperatorKind()) {
1870 case UserDefinedLiteral::LOK_Raw:
Richard Smith29e95952012-03-09 10:10:02 +00001871 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
Richard Smithc67fdd42012-03-07 08:35:16 +00001872 break;
1873 case UserDefinedLiteral::LOK_Template: {
Richard Smith29e95952012-03-09 10:10:02 +00001874 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1875 const TemplateArgumentList *Args =
1876 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1877 assert(Args);
David Majnemer314b72f2015-04-05 05:32:54 +00001878
1879 if (Args->size() != 1) {
Richard Smithe87aeb32015-10-08 00:17:59 +00001880 OS << "operator\"\"" << Node->getUDSuffix()->getName();
David Majnemer314b72f2015-04-05 05:32:54 +00001881 TemplateSpecializationType::PrintTemplateArgumentList(
1882 OS, Args->data(), Args->size(), Policy);
1883 OS << "()";
1884 return;
1885 }
1886
Richard Smith29e95952012-03-09 10:10:02 +00001887 const TemplateArgument &Pack = Args->get(0);
Aaron Ballman2a89e852014-07-15 21:32:31 +00001888 for (const auto &P : Pack.pack_elements()) {
1889 char C = (char)P.getAsIntegral().getZExtValue();
Richard Smithc67fdd42012-03-07 08:35:16 +00001890 OS << C;
1891 }
1892 break;
1893 }
Richard Smith75025ba2012-03-08 09:02:38 +00001894 case UserDefinedLiteral::LOK_Integer: {
1895 // Print integer literal without suffix.
1896 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1897 OS << Int->getValue().toString(10, /*isSigned*/false);
1898 break;
1899 }
Benjamin Kramer8a526762012-09-20 14:07:17 +00001900 case UserDefinedLiteral::LOK_Floating: {
1901 // Print floating literal without suffix.
1902 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1903 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1904 break;
1905 }
Richard Smithc67fdd42012-03-07 08:35:16 +00001906 case UserDefinedLiteral::LOK_String:
1907 case UserDefinedLiteral::LOK_Character:
1908 PrintExpr(Node->getCookedLiteral());
1909 break;
1910 }
1911 OS << Node->getUDSuffix()->getName();
1912}
1913
Chris Lattnereefa10e2007-05-28 06:56:27 +00001914void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1915 OS << (Node->getValue() ? "true" : "false");
1916}
1917
Sebastian Redl576fd422009-05-10 18:38:11 +00001918void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1919 OS << "nullptr";
1920}
1921
Douglas Gregor97a9c812008-11-04 14:32:21 +00001922void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1923 OS << "this";
1924}
1925
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001926void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
Craig Topper36250ad2014-05-12 05:36:57 +00001927 if (!Node->getSubExpr())
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001928 OS << "throw";
1929 else {
1930 OS << "throw ";
1931 PrintExpr(Node->getSubExpr());
1932 }
1933}
1934
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001935void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
Richard Smith852c9db2013-04-20 22:23:05 +00001936 // Nothing to print: we picked up the default argument.
1937}
1938
1939void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1940 // Nothing to print: we picked up the default initializer.
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001941}
1942
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001943void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001944 Node->getType().print(OS, Policy);
Richard Smith1ae689c2015-01-28 22:06:01 +00001945 // If there are no parens, this is list-initialization, and the braces are
1946 // part of the syntax of the inner construct.
1947 if (Node->getLParenLoc().isValid())
1948 OS << "(";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001949 PrintExpr(Node->getSubExpr());
Richard Smith1ae689c2015-01-28 22:06:01 +00001950 if (Node->getLParenLoc().isValid())
1951 OS << ")";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001952}
1953
Anders Carlsson993a4b32009-05-30 20:03:25 +00001954void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1955 PrintExpr(Node->getSubExpr());
1956}
1957
Douglas Gregordd04d332009-01-16 18:33:17 +00001958void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001959 Node->getType().print(OS, Policy);
Richard Smithed83ebd2015-02-05 07:02:11 +00001960 if (Node->isStdInitListInitialization())
1961 /* Nothing to do; braces are part of creating the std::initializer_list. */;
1962 else if (Node->isListInitialization())
Richard Smith1ae689c2015-01-28 22:06:01 +00001963 OS << "{";
1964 else
1965 OS << "(";
Douglas Gregordd04d332009-01-16 18:33:17 +00001966 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001967 ArgEnd = Node->arg_end();
Douglas Gregordd04d332009-01-16 18:33:17 +00001968 Arg != ArgEnd; ++Arg) {
Benjamin Kramerf48ee442015-07-18 14:35:53 +00001969 if ((*Arg)->isDefaultArgument())
Rafael Espindola6f6f3c42013-05-24 16:11:44 +00001970 break;
Douglas Gregordd04d332009-01-16 18:33:17 +00001971 if (Arg != Node->arg_begin())
1972 OS << ", ";
1973 PrintExpr(*Arg);
1974 }
Richard Smithed83ebd2015-02-05 07:02:11 +00001975 if (Node->isStdInitListInitialization())
1976 /* See above. */;
1977 else if (Node->isListInitialization())
Richard Smith1ae689c2015-01-28 22:06:01 +00001978 OS << "}";
1979 else
1980 OS << ")";
Douglas Gregordd04d332009-01-16 18:33:17 +00001981}
1982
Douglas Gregore31e6062012-02-07 10:09:13 +00001983void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1984 OS << '[';
1985 bool NeedComma = false;
1986 switch (Node->getCaptureDefault()) {
1987 case LCD_None:
1988 break;
1989
1990 case LCD_ByCopy:
1991 OS << '=';
1992 NeedComma = true;
1993 break;
1994
1995 case LCD_ByRef:
1996 OS << '&';
1997 NeedComma = true;
1998 break;
1999 }
2000 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
2001 CEnd = Node->explicit_capture_end();
2002 C != CEnd;
2003 ++C) {
2004 if (NeedComma)
2005 OS << ", ";
2006 NeedComma = true;
2007
2008 switch (C->getCaptureKind()) {
2009 case LCK_This:
2010 OS << "this";
2011 break;
Faisal Validc6b5962016-03-21 09:25:37 +00002012 case LCK_StarThis:
2013 OS << "*this";
2014 break;
Douglas Gregore31e6062012-02-07 10:09:13 +00002015 case LCK_ByRef:
James Dennettdd2ffea22015-05-07 18:48:18 +00002016 if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C))
Douglas Gregore31e6062012-02-07 10:09:13 +00002017 OS << '&';
2018 OS << C->getCapturedVar()->getName();
2019 break;
2020
2021 case LCK_ByCopy:
Douglas Gregore31e6062012-02-07 10:09:13 +00002022 OS << C->getCapturedVar()->getName();
2023 break;
Alexey Bataev39c81e22014-08-28 04:28:19 +00002024 case LCK_VLAType:
2025 llvm_unreachable("VLA type in explicit captures.");
Douglas Gregore31e6062012-02-07 10:09:13 +00002026 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00002027
James Dennettdd2ffea22015-05-07 18:48:18 +00002028 if (Node->isInitCapture(C))
Richard Smithbb13c9a2013-09-28 04:02:39 +00002029 PrintExpr(C->getCapturedVar()->getInit());
Douglas Gregore31e6062012-02-07 10:09:13 +00002030 }
2031 OS << ']';
2032
2033 if (Node->hasExplicitParameters()) {
2034 OS << " (";
2035 CXXMethodDecl *Method = Node->getCallOperator();
2036 NeedComma = false;
Aaron Ballman43b68be2014-03-07 17:50:17 +00002037 for (auto P : Method->params()) {
Douglas Gregore31e6062012-02-07 10:09:13 +00002038 if (NeedComma) {
2039 OS << ", ";
2040 } else {
2041 NeedComma = true;
2042 }
Aaron Ballman43b68be2014-03-07 17:50:17 +00002043 std::string ParamStr = P->getNameAsString();
2044 P->getOriginalType().print(OS, Policy, ParamStr);
Douglas Gregore31e6062012-02-07 10:09:13 +00002045 }
2046 if (Method->isVariadic()) {
2047 if (NeedComma)
2048 OS << ", ";
2049 OS << "...";
2050 }
2051 OS << ')';
2052
2053 if (Node->isMutable())
2054 OS << " mutable";
2055
2056 const FunctionProtoType *Proto
2057 = Method->getType()->getAs<FunctionProtoType>();
Benjamin Kramer9170e912013-02-22 15:46:01 +00002058 Proto->printExceptionSpecification(OS, Policy);
Douglas Gregore31e6062012-02-07 10:09:13 +00002059
Bill Wendling44426052012-12-20 19:22:21 +00002060 // FIXME: Attributes
Douglas Gregore31e6062012-02-07 10:09:13 +00002061
Douglas Gregor0c46b2b2012-02-13 22:00:16 +00002062 // Print the trailing return type if it was specified in the source.
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002063 if (Node->hasExplicitResultType()) {
2064 OS << " -> ";
Alp Toker314cc812014-01-25 16:55:45 +00002065 Proto->getReturnType().print(OS, Policy);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002066 }
Douglas Gregore31e6062012-02-07 10:09:13 +00002067 }
2068
2069 // Print the body.
2070 CompoundStmt *Body = Node->getBody();
2071 OS << ' ';
2072 PrintStmt(Body);
2073}
2074
Douglas Gregor747eb782010-07-08 06:14:04 +00002075void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregor2b88c112010-09-08 00:15:04 +00002076 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002077 TSInfo->getType().print(OS, Policy);
Douglas Gregor2b88c112010-09-08 00:15:04 +00002078 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002079 Node->getType().print(OS, Policy);
2080 OS << "()";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002081}
2082
Sebastian Redlbd150f42008-11-21 19:14:01 +00002083void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
2084 if (E->isGlobalNew())
2085 OS << "::";
2086 OS << "new ";
2087 unsigned NumPlace = E->getNumPlacementArgs();
Eli Friedmana6fdfaa2012-10-18 20:54:37 +00002088 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002089 OS << "(";
2090 PrintExpr(E->getPlacementArg(0));
2091 for (unsigned i = 1; i < NumPlace; ++i) {
Eli Friedmana6fdfaa2012-10-18 20:54:37 +00002092 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
2093 break;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002094 OS << ", ";
2095 PrintExpr(E->getPlacementArg(i));
2096 }
2097 OS << ") ";
2098 }
2099 if (E->isParenTypeId())
2100 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00002101 std::string TypeS;
2102 if (Expr *Size = E->getArraySize()) {
2103 llvm::raw_string_ostream s(TypeS);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002104 s << '[';
Richard Smith235341b2012-08-16 03:56:14 +00002105 Size->printPretty(s, Helper, Policy);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002106 s << ']';
Sebastian Redld6d55ee2008-12-02 22:08:59 +00002107 }
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002108 E->getAllocatedType().print(OS, Policy, TypeS);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002109 if (E->isParenTypeId())
2110 OS << ")";
2111
Sebastian Redl6047f072012-02-16 12:22:20 +00002112 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
2113 if (InitStyle) {
2114 if (InitStyle == CXXNewExpr::CallInit)
2115 OS << "(";
2116 PrintExpr(E->getInitializer());
2117 if (InitStyle == CXXNewExpr::CallInit)
2118 OS << ")";
Sebastian Redlbd150f42008-11-21 19:14:01 +00002119 }
2120}
2121
2122void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
2123 if (E->isGlobalDelete())
2124 OS << "::";
2125 OS << "delete ";
2126 if (E->isArrayForm())
2127 OS << "[] ";
2128 PrintExpr(E->getArgument());
2129}
2130
Douglas Gregorad8a3362009-09-04 17:36:40 +00002131void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
2132 PrintExpr(E->getBase());
2133 if (E->isArrow())
2134 OS << "->";
2135 else
2136 OS << '.';
2137 if (E->getQualifier())
2138 E->getQualifier()->print(OS, Policy);
Eli Friedman9cc8ac52012-10-23 20:26:57 +00002139 OS << "~";
Mike Stump11289f42009-09-09 15:08:12 +00002140
Douglas Gregor678f90d2010-02-25 01:56:36 +00002141 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
2142 OS << II->getName();
2143 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002144 E->getDestroyedType().print(OS, Policy);
Douglas Gregorad8a3362009-09-04 17:36:40 +00002145}
2146
Anders Carlsson0781ce72009-04-23 02:32:43 +00002147void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Richard Smithed83ebd2015-02-05 07:02:11 +00002148 if (E->isListInitialization() && !E->isStdInitListInitialization())
Richard Smithd0e102f2015-01-30 02:04:26 +00002149 OS << "{";
Richard Smithd59b8322012-12-19 01:39:02 +00002150
Douglas Gregorbaba85d2011-01-24 17:25:03 +00002151 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
2152 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
2153 // Don't print any defaulted arguments
2154 break;
2155 }
2156
2157 if (i) OS << ", ";
2158 PrintExpr(E->getArg(i));
Fariborz Jahaniand0bbf662010-01-13 21:41:11 +00002159 }
Richard Smithd59b8322012-12-19 01:39:02 +00002160
Richard Smithed83ebd2015-02-05 07:02:11 +00002161 if (E->isListInitialization() && !E->isStdInitListInitialization())
Richard Smithd0e102f2015-01-30 02:04:26 +00002162 OS << "}";
Anders Carlsson0781ce72009-04-23 02:32:43 +00002163}
2164
Richard Smithcc1b96d2013-06-12 22:31:48 +00002165void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
2166 PrintExpr(E->getSubExpr());
2167}
2168
John McCall5d413782010-12-06 08:20:24 +00002169void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Alp Toker028ed912013-12-06 17:56:43 +00002170 // Just forward to the subexpression.
Anders Carlssondefc6442009-04-24 22:47:04 +00002171 PrintExpr(E->getSubExpr());
2172}
2173
Mike Stump11289f42009-09-09 15:08:12 +00002174void
Douglas Gregorce934142009-05-20 18:46:25 +00002175StmtPrinter::VisitCXXUnresolvedConstructExpr(
2176 CXXUnresolvedConstructExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002177 Node->getTypeAsWritten().print(OS, Policy);
Douglas Gregorce934142009-05-20 18:46:25 +00002178 OS << "(";
2179 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00002180 ArgEnd = Node->arg_end();
Douglas Gregorce934142009-05-20 18:46:25 +00002181 Arg != ArgEnd; ++Arg) {
2182 if (Arg != Node->arg_begin())
2183 OS << ", ";
2184 PrintExpr(*Arg);
2185 }
2186 OS << ")";
2187}
2188
John McCall8cd78132009-11-19 22:55:06 +00002189void StmtPrinter::VisitCXXDependentScopeMemberExpr(
2190 CXXDependentScopeMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00002191 if (!Node->isImplicitAccess()) {
2192 PrintExpr(Node->getBase());
2193 OS << (Node->isArrow() ? "->" : ".");
2194 }
Douglas Gregorc26e0f62009-09-03 16:14:30 +00002195 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2196 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002197 if (Node->hasTemplateKeyword())
Douglas Gregor308047d2009-09-09 00:23:06 +00002198 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002199 OS << Node->getMemberNameInfo();
Benjamin Kramer9170e912013-02-22 15:46:01 +00002200 if (Node->hasExplicitTemplateArgs())
2201 TemplateSpecializationType::PrintTemplateArgumentList(
2202 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregora8db9542009-05-22 21:13:27 +00002203}
2204
John McCall10eae182009-11-30 22:42:35 +00002205void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00002206 if (!Node->isImplicitAccess()) {
2207 PrintExpr(Node->getBase());
2208 OS << (Node->isArrow() ? "->" : ".");
2209 }
John McCall10eae182009-11-30 22:42:35 +00002210 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2211 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002212 if (Node->hasTemplateKeyword())
2213 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002214 OS << Node->getMemberNameInfo();
Benjamin Kramer9170e912013-02-22 15:46:01 +00002215 if (Node->hasExplicitTemplateArgs())
2216 TemplateSpecializationType::PrintTemplateArgumentList(
2217 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
John McCall10eae182009-11-30 22:42:35 +00002218}
2219
Douglas Gregor29c42f22012-02-24 07:38:34 +00002220static const char *getTypeTraitName(TypeTrait TT) {
2221 switch (TT) {
Alp Toker95e7ff22014-01-01 05:57:51 +00002222#define TYPE_TRAIT_1(Spelling, Name, Key) \
2223case clang::UTT_##Name: return #Spelling;
Alp Tokercbb90342013-12-13 20:49:58 +00002224#define TYPE_TRAIT_2(Spelling, Name, Key) \
2225case clang::BTT_##Name: return #Spelling;
Alp Toker40f9b1c2013-12-12 21:23:03 +00002226#define TYPE_TRAIT_N(Spelling, Name, Key) \
2227 case clang::TT_##Name: return #Spelling;
2228#include "clang/Basic/TokenKinds.def"
Douglas Gregor29c42f22012-02-24 07:38:34 +00002229 }
2230 llvm_unreachable("Type trait not covered by switch");
2231}
2232
John Wiegley6242b6a2011-04-28 00:16:57 +00002233static const char *getTypeTraitName(ArrayTypeTrait ATT) {
2234 switch (ATT) {
2235 case ATT_ArrayRank: return "__array_rank";
2236 case ATT_ArrayExtent: return "__array_extent";
2237 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00002238 llvm_unreachable("Array type trait not covered by switch");
John Wiegley6242b6a2011-04-28 00:16:57 +00002239}
2240
John Wiegleyf9f65842011-04-25 06:54:41 +00002241static const char *getExpressionTraitName(ExpressionTrait ET) {
2242 switch (ET) {
John Wiegleyf9f65842011-04-25 06:54:41 +00002243 case ET_IsLValueExpr: return "__is_lvalue_expr";
2244 case ET_IsRValueExpr: return "__is_rvalue_expr";
2245 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00002246 llvm_unreachable("Expression type trait not covered by switch");
John Wiegleyf9f65842011-04-25 06:54:41 +00002247}
2248
Douglas Gregor29c42f22012-02-24 07:38:34 +00002249void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
2250 OS << getTypeTraitName(E->getTrait()) << "(";
2251 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
2252 if (I > 0)
2253 OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002254 E->getArg(I)->getType().print(OS, Policy);
Douglas Gregor29c42f22012-02-24 07:38:34 +00002255 }
2256 OS << ")";
2257}
2258
John Wiegley6242b6a2011-04-28 00:16:57 +00002259void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002260 OS << getTypeTraitName(E->getTrait()) << '(';
2261 E->getQueriedType().print(OS, Policy);
2262 OS << ')';
John Wiegley6242b6a2011-04-28 00:16:57 +00002263}
2264
John Wiegleyf9f65842011-04-25 06:54:41 +00002265void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002266 OS << getExpressionTraitName(E->getTrait()) << '(';
2267 PrintExpr(E->getQueriedExpression());
2268 OS << ')';
John Wiegleyf9f65842011-04-25 06:54:41 +00002269}
2270
Sebastian Redl4202c0f2010-09-10 20:55:43 +00002271void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
2272 OS << "noexcept(";
2273 PrintExpr(E->getOperand());
2274 OS << ")";
2275}
2276
Douglas Gregor820ba7b2011-01-04 17:33:58 +00002277void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregore8e9dd62011-01-03 17:17:50 +00002278 PrintExpr(E->getPattern());
2279 OS << "...";
2280}
2281
Douglas Gregor820ba7b2011-01-04 17:33:58 +00002282void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00002283 OS << "sizeof...(" << *E->getPack() << ")";
Douglas Gregor820ba7b2011-01-04 17:33:58 +00002284}
2285
Douglas Gregorcdbc5392011-01-15 01:15:58 +00002286void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
2287 SubstNonTypeTemplateParmPackExpr *Node) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00002288 OS << *Node->getParameterPack();
Douglas Gregorcdbc5392011-01-15 01:15:58 +00002289}
2290
John McCall7c454bb2011-07-15 05:09:51 +00002291void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
2292 SubstNonTypeTemplateParmExpr *Node) {
2293 Visit(Node->getReplacement());
2294}
2295
Richard Smithb15fe3a2012-09-12 00:56:43 +00002296void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
2297 OS << *E->getParameterPack();
2298}
2299
Douglas Gregorfe314812011-06-21 17:03:29 +00002300void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
2301 PrintExpr(Node->GetTemporaryExpr());
2302}
2303
Richard Smith0f0af192014-11-08 05:07:16 +00002304void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
2305 OS << "(";
2306 if (E->getLHS()) {
2307 PrintExpr(E->getLHS());
2308 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2309 }
2310 OS << "...";
2311 if (E->getRHS()) {
2312 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2313 PrintExpr(E->getRHS());
2314 }
2315 OS << ")";
2316}
2317
Richard Smith9f690bd2015-10-27 06:02:45 +00002318// C++ Coroutines TS
2319
2320void StmtPrinter::VisitCoroutineBodyStmt(CoroutineBodyStmt *S) {
2321 Visit(S->getBody());
2322}
2323
2324void StmtPrinter::VisitCoreturnStmt(CoreturnStmt *S) {
2325 OS << "co_return";
2326 if (S->getOperand()) {
2327 OS << " ";
2328 Visit(S->getOperand());
2329 }
2330 OS << ";";
2331}
2332
2333void StmtPrinter::VisitCoawaitExpr(CoawaitExpr *S) {
2334 OS << "co_await ";
2335 PrintExpr(S->getOperand());
2336}
2337
2338void StmtPrinter::VisitCoyieldExpr(CoyieldExpr *S) {
2339 OS << "co_yield ";
2340 PrintExpr(S->getOperand());
2341}
2342
Mike Stump11289f42009-09-09 15:08:12 +00002343// Obj-C
Anders Carlsson76f4a902007-08-21 17:43:55 +00002344
2345void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
2346 OS << "@";
2347 VisitStringLiteral(Node->getString());
2348}
Chris Lattnereefa10e2007-05-28 06:56:27 +00002349
Patrick Beard0caa3942012-04-19 00:25:12 +00002350void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00002351 OS << "@";
Patrick Beard0caa3942012-04-19 00:25:12 +00002352 Visit(E->getSubExpr());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002353}
2354
2355void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
2356 OS << "@[ ";
Benjamin Kramer5733e352015-07-18 17:09:36 +00002357 ObjCArrayLiteral::child_range Ch = E->children();
2358 for (auto I = Ch.begin(), E = Ch.end(); I != E; ++I) {
2359 if (I != Ch.begin())
Ted Kremeneke65b0862012-03-06 20:05:56 +00002360 OS << ", ";
Benjamin Kramer5733e352015-07-18 17:09:36 +00002361 Visit(*I);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002362 }
2363 OS << " ]";
2364}
2365
2366void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
2367 OS << "@{ ";
2368 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
2369 if (I > 0)
2370 OS << ", ";
2371
2372 ObjCDictionaryElement Element = E->getKeyValueElement(I);
2373 Visit(Element.Key);
2374 OS << " : ";
2375 Visit(Element.Value);
2376 if (Element.isPackExpansion())
2377 OS << "...";
2378 }
2379 OS << " }";
2380}
2381
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002382void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002383 OS << "@encode(";
2384 Node->getEncodedType().print(OS, Policy);
2385 OS << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002386}
2387
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002388void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00002389 OS << "@selector(";
2390 Node->getSelector().print(OS);
2391 OS << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002392}
2393
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002394void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00002395 OS << "@protocol(" << *Node->getProtocol() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002396}
2397
Steve Naroffd54978b2007-09-18 23:55:05 +00002398void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
2399 OS << "[";
Douglas Gregor9a129192010-04-21 00:45:42 +00002400 switch (Mess->getReceiverKind()) {
2401 case ObjCMessageExpr::Instance:
2402 PrintExpr(Mess->getInstanceReceiver());
2403 break;
2404
2405 case ObjCMessageExpr::Class:
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002406 Mess->getClassReceiver().print(OS, Policy);
Douglas Gregor9a129192010-04-21 00:45:42 +00002407 break;
2408
2409 case ObjCMessageExpr::SuperInstance:
2410 case ObjCMessageExpr::SuperClass:
2411 OS << "Super";
2412 break;
2413 }
2414
Ted Kremeneka06e7122008-05-02 17:32:38 +00002415 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00002416 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00002417 if (selector.isUnarySelector()) {
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00002418 OS << selector.getNameForSlot(0);
Steve Naroffc6814ea2007-10-02 20:01:56 +00002419 } else {
2420 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00002421 if (i < selector.getNumArgs()) {
2422 if (i > 0) OS << ' ';
2423 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00002424 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00002425 else
2426 OS << ":";
2427 }
2428 else OS << ", "; // Handle variadic methods.
Mike Stump11289f42009-09-09 15:08:12 +00002429
Steve Naroffc6814ea2007-10-02 20:01:56 +00002430 PrintExpr(Mess->getArg(i));
2431 }
Steve Naroffd54978b2007-09-18 23:55:05 +00002432 }
2433 OS << "]";
2434}
2435
Ted Kremeneke65b0862012-03-06 20:05:56 +00002436void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
2437 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
2438}
2439
John McCall31168b02011-06-15 23:02:42 +00002440void
2441StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
2442 PrintExpr(E->getSubExpr());
2443}
2444
2445void
2446StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002447 OS << '(' << E->getBridgeKindName();
2448 E->getType().print(OS, Policy);
2449 OS << ')';
John McCall31168b02011-06-15 23:02:42 +00002450 PrintExpr(E->getSubExpr());
2451}
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002452
Steve Naroffc540d662008-09-03 18:15:37 +00002453void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00002454 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00002455 OS << "^";
Mike Stump11289f42009-09-09 15:08:12 +00002456
Steve Naroffc540d662008-09-03 18:15:37 +00002457 const FunctionType *AFT = Node->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00002458
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002459 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroffc540d662008-09-03 18:15:37 +00002460 OS << "()";
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002461 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00002462 OS << '(';
Steve Naroff415d3d52008-10-08 17:01:13 +00002463 for (BlockDecl::param_iterator AI = BD->param_begin(),
2464 E = BD->param_end(); AI != E; ++AI) {
2465 if (AI != BD->param_begin()) OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002466 std::string ParamStr = (*AI)->getNameAsString();
2467 (*AI)->getType().print(OS, Policy, ParamStr);
Steve Naroffc540d662008-09-03 18:15:37 +00002468 }
Mike Stump11289f42009-09-09 15:08:12 +00002469
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002470 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00002471 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00002472 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00002473 OS << "...";
2474 }
2475 OS << ')';
2476 }
Fariborz Jahanian1ace8cb2012-12-04 21:15:23 +00002477 OS << "{ }";
Steve Naroffc540d662008-09-03 18:15:37 +00002478}
2479
Ted Kremenekf551f322011-11-30 22:08:08 +00002480void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
2481 PrintExpr(Node->getSourceExpr());
2482}
John McCall8d69a212010-11-15 23:31:06 +00002483
Kaelyn Takatae1f49d52014-10-27 18:07:20 +00002484void StmtPrinter::VisitTypoExpr(TypoExpr *Node) {
2485 // TODO: Print something reasonable for a TypoExpr, if necessary.
Davide Italiano04839a52016-01-30 08:03:54 +00002486 llvm_unreachable("Cannot print TypoExpr nodes");
Kaelyn Takatae1f49d52014-10-27 18:07:20 +00002487}
2488
Tanya Lattner55808c12011-06-04 00:47:47 +00002489void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
2490 OS << "__builtin_astype(";
2491 PrintExpr(Node->getSrcExpr());
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002492 OS << ", ";
2493 Node->getType().print(OS, Policy);
Tanya Lattner55808c12011-06-04 00:47:47 +00002494 OS << ")";
2495}
2496
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00002497//===----------------------------------------------------------------------===//
2498// Stmt method implementations
2499//===----------------------------------------------------------------------===//
2500
Craig Topperc571c812013-08-22 06:02:26 +00002501void Stmt::dumpPretty(const ASTContext &Context) const {
Craig Topper36250ad2014-05-12 05:36:57 +00002502 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00002503}
2504
Richard Smith235341b2012-08-16 03:56:14 +00002505void Stmt::printPretty(raw_ostream &OS,
2506 PrinterHelper *Helper,
Douglas Gregor7de59662009-05-29 20:38:28 +00002507 const PrintingPolicy &Policy,
2508 unsigned Indentation) const {
Richard Smith235341b2012-08-16 03:56:14 +00002509 StmtPrinter P(OS, Helper, Policy, Indentation);
Chris Lattner62249a62007-08-21 04:04:25 +00002510 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00002511}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002512
2513//===----------------------------------------------------------------------===//
2514// PrinterHelper
2515//===----------------------------------------------------------------------===//
2516
2517// Implement virtual destructor.
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +00002518PrinterHelper::~PrinterHelper() {}