blob: f4418c92406351ff48c05f6293a9db82ff4fc616 [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
Erik Pilkington29099de2016-07-16 00:35:23 +0000500void StmtPrinter::VisitObjCAvailabilityCheckExpr(
501 ObjCAvailabilityCheckExpr *Node) {
502 OS << "@available(...)";
503}
504
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000505void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000506 Indent() << "@synchronized (";
507 PrintExpr(Node->getSynchExpr());
508 OS << ")";
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000509 PrintRawCompoundStmt(Node->getSynchBody());
510 OS << "\n";
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000511}
512
John McCall31168b02011-06-15 23:02:42 +0000513void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
514 Indent() << "@autoreleasepool";
515 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
516 OS << "\n";
517}
518
Sebastian Redl9b244a82008-12-22 21:35:02 +0000519void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
520 OS << "catch (";
Sebastian Redl54c04d42008-12-22 19:15:10 +0000521 if (Decl *ExDecl = Node->getExceptionDecl())
522 PrintRawDecl(ExDecl);
523 else
524 OS << "...";
525 OS << ") ";
526 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl9b244a82008-12-22 21:35:02 +0000527}
528
529void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
530 Indent();
531 PrintRawCXXCatchStmt(Node);
532 OS << "\n";
533}
534
535void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
536 Indent() << "try ";
537 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump11289f42009-09-09 15:08:12 +0000538 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl9b244a82008-12-22 21:35:02 +0000539 OS << " ";
540 PrintRawCXXCatchStmt(Node->getHandler(i));
541 }
Sebastian Redl54c04d42008-12-22 19:15:10 +0000542 OS << "\n";
543}
544
John Wiegley1c0675e2011-04-28 01:08:34 +0000545void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
546 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
547 PrintRawCompoundStmt(Node->getTryBlock());
548 SEHExceptStmt *E = Node->getExceptHandler();
549 SEHFinallyStmt *F = Node->getFinallyHandler();
550 if(E)
551 PrintRawSEHExceptHandler(E);
552 else {
553 assert(F && "Must have a finally block...");
554 PrintRawSEHFinallyStmt(F);
555 }
556 OS << "\n";
557}
558
559void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
560 OS << "__finally ";
561 PrintRawCompoundStmt(Node->getBlock());
562 OS << "\n";
563}
564
565void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
566 OS << "__except (";
567 VisitExpr(Node->getFilterExpr());
Joao Matos566359c2012-09-04 17:49:35 +0000568 OS << ")\n";
John Wiegley1c0675e2011-04-28 01:08:34 +0000569 PrintRawCompoundStmt(Node->getBlock());
570 OS << "\n";
571}
572
573void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
574 Indent();
575 PrintRawSEHExceptHandler(Node);
576 OS << "\n";
577}
578
579void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
580 Indent();
581 PrintRawSEHFinallyStmt(Node);
582 OS << "\n";
583}
584
Nico Weber9b982072014-07-07 00:12:30 +0000585void StmtPrinter::VisitSEHLeaveStmt(SEHLeaveStmt *Node) {
586 Indent() << "__leave;";
587 if (Policy.IncludeNewlines) OS << "\n";
588}
589
Chris Lattner71e23ce2006-11-04 20:18:38 +0000590//===----------------------------------------------------------------------===//
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000591// OpenMP clauses printing methods
592//===----------------------------------------------------------------------===//
593
594namespace {
595class OMPClausePrinter : public OMPClauseVisitor<OMPClausePrinter> {
596 raw_ostream &OS;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000597 const PrintingPolicy &Policy;
Alexey Bataev756c1962013-09-24 03:17:45 +0000598 /// \brief Process clauses with list of variables.
599 template <typename T>
600 void VisitOMPClauseList(T *Node, char StartSym);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000601public:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000602 OMPClausePrinter(raw_ostream &OS, const PrintingPolicy &Policy)
603 : OS(OS), Policy(Policy) { }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000604#define OPENMP_CLAUSE(Name, Class) \
605 void Visit##Class(Class *S);
606#include "clang/Basic/OpenMPKinds.def"
607};
608
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000609void OMPClausePrinter::VisitOMPIfClause(OMPIfClause *Node) {
610 OS << "if(";
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000611 if (Node->getNameModifier() != OMPD_unknown)
612 OS << getOpenMPDirectiveName(Node->getNameModifier()) << ": ";
Craig Topper36250ad2014-05-12 05:36:57 +0000613 Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000614 OS << ")";
615}
616
Alexey Bataev3778b602014-07-17 07:32:53 +0000617void OMPClausePrinter::VisitOMPFinalClause(OMPFinalClause *Node) {
618 OS << "final(";
619 Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
620 OS << ")";
621}
622
Alexey Bataev568a8332014-03-06 06:15:19 +0000623void OMPClausePrinter::VisitOMPNumThreadsClause(OMPNumThreadsClause *Node) {
624 OS << "num_threads(";
Craig Topper36250ad2014-05-12 05:36:57 +0000625 Node->getNumThreads()->printPretty(OS, nullptr, Policy, 0);
Alexey Bataev568a8332014-03-06 06:15:19 +0000626 OS << ")";
627}
628
Alexey Bataev62c87d22014-03-21 04:51:18 +0000629void OMPClausePrinter::VisitOMPSafelenClause(OMPSafelenClause *Node) {
630 OS << "safelen(";
Craig Topper36250ad2014-05-12 05:36:57 +0000631 Node->getSafelen()->printPretty(OS, nullptr, Policy, 0);
Alexey Bataev62c87d22014-03-21 04:51:18 +0000632 OS << ")";
633}
634
Alexey Bataev66b15b52015-08-21 11:14:16 +0000635void OMPClausePrinter::VisitOMPSimdlenClause(OMPSimdlenClause *Node) {
636 OS << "simdlen(";
637 Node->getSimdlen()->printPretty(OS, nullptr, Policy, 0);
638 OS << ")";
639}
640
Alexander Musman8bd31e62014-05-27 15:12:19 +0000641void OMPClausePrinter::VisitOMPCollapseClause(OMPCollapseClause *Node) {
642 OS << "collapse(";
643 Node->getNumForLoops()->printPretty(OS, nullptr, Policy, 0);
644 OS << ")";
645}
646
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000647void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
648 OS << "default("
649 << getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind())
650 << ")";
651}
652
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000653void OMPClausePrinter::VisitOMPProcBindClause(OMPProcBindClause *Node) {
654 OS << "proc_bind("
655 << getOpenMPSimpleClauseTypeName(OMPC_proc_bind, Node->getProcBindKind())
656 << ")";
657}
658
Alexey Bataev56dafe82014-06-20 07:16:17 +0000659void OMPClausePrinter::VisitOMPScheduleClause(OMPScheduleClause *Node) {
Alexey Bataev6402bca2015-12-28 07:25:51 +0000660 OS << "schedule(";
661 if (Node->getFirstScheduleModifier() != OMPC_SCHEDULE_MODIFIER_unknown) {
662 OS << getOpenMPSimpleClauseTypeName(OMPC_schedule,
663 Node->getFirstScheduleModifier());
664 if (Node->getSecondScheduleModifier() != OMPC_SCHEDULE_MODIFIER_unknown) {
665 OS << ", ";
666 OS << getOpenMPSimpleClauseTypeName(OMPC_schedule,
667 Node->getSecondScheduleModifier());
668 }
669 OS << ": ";
670 }
671 OS << getOpenMPSimpleClauseTypeName(OMPC_schedule, Node->getScheduleKind());
Alexey Bataev3392d762016-02-16 11:18:12 +0000672 if (auto *E = Node->getChunkSize()) {
Alexey Bataev56dafe82014-06-20 07:16:17 +0000673 OS << ", ";
Alexey Bataev60da77e2016-02-29 05:54:20 +0000674 E->printPretty(OS, nullptr, Policy);
Alexey Bataev56dafe82014-06-20 07:16:17 +0000675 }
676 OS << ")";
677}
678
Alexey Bataev10e775f2015-07-30 11:36:16 +0000679void OMPClausePrinter::VisitOMPOrderedClause(OMPOrderedClause *Node) {
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000680 OS << "ordered";
Alexey Bataev10e775f2015-07-30 11:36:16 +0000681 if (auto *Num = Node->getNumForLoops()) {
682 OS << "(";
683 Num->printPretty(OS, nullptr, Policy, 0);
684 OS << ")";
685 }
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000686}
687
Alexey Bataev236070f2014-06-20 11:19:47 +0000688void OMPClausePrinter::VisitOMPNowaitClause(OMPNowaitClause *) {
689 OS << "nowait";
690}
691
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000692void OMPClausePrinter::VisitOMPUntiedClause(OMPUntiedClause *) {
693 OS << "untied";
694}
695
Alexey Bataevb825de12015-12-07 10:51:44 +0000696void OMPClausePrinter::VisitOMPNogroupClause(OMPNogroupClause *) {
697 OS << "nogroup";
698}
699
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000700void OMPClausePrinter::VisitOMPMergeableClause(OMPMergeableClause *) {
701 OS << "mergeable";
702}
703
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000704void OMPClausePrinter::VisitOMPReadClause(OMPReadClause *) { OS << "read"; }
705
Alexey Bataevdea47612014-07-23 07:46:59 +0000706void OMPClausePrinter::VisitOMPWriteClause(OMPWriteClause *) { OS << "write"; }
707
Alexey Bataev67a4f222014-07-23 10:25:33 +0000708void OMPClausePrinter::VisitOMPUpdateClause(OMPUpdateClause *) {
709 OS << "update";
710}
711
Alexey Bataev459dec02014-07-24 06:46:57 +0000712void OMPClausePrinter::VisitOMPCaptureClause(OMPCaptureClause *) {
713 OS << "capture";
714}
715
Alexey Bataev82bad8b2014-07-24 08:55:34 +0000716void OMPClausePrinter::VisitOMPSeqCstClause(OMPSeqCstClause *) {
717 OS << "seq_cst";
718}
719
Alexey Bataev346265e2015-09-25 10:37:12 +0000720void OMPClausePrinter::VisitOMPThreadsClause(OMPThreadsClause *) {
721 OS << "threads";
722}
723
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000724void OMPClausePrinter::VisitOMPSIMDClause(OMPSIMDClause *) { OS << "simd"; }
725
Michael Wonge710d542015-08-07 16:16:36 +0000726void OMPClausePrinter::VisitOMPDeviceClause(OMPDeviceClause *Node) {
727 OS << "device(";
728 Node->getDevice()->printPretty(OS, nullptr, Policy, 0);
729 OS << ")";
730}
731
Kelvin Li099bb8c2015-11-24 20:50:12 +0000732void OMPClausePrinter::VisitOMPNumTeamsClause(OMPNumTeamsClause *Node) {
733 OS << "num_teams(";
734 Node->getNumTeams()->printPretty(OS, nullptr, Policy, 0);
735 OS << ")";
736}
737
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000738void OMPClausePrinter::VisitOMPThreadLimitClause(OMPThreadLimitClause *Node) {
739 OS << "thread_limit(";
740 Node->getThreadLimit()->printPretty(OS, nullptr, Policy, 0);
741 OS << ")";
742}
743
Alexey Bataeva0569352015-12-01 10:17:31 +0000744void OMPClausePrinter::VisitOMPPriorityClause(OMPPriorityClause *Node) {
745 OS << "priority(";
746 Node->getPriority()->printPretty(OS, nullptr, Policy, 0);
747 OS << ")";
748}
749
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000750void OMPClausePrinter::VisitOMPGrainsizeClause(OMPGrainsizeClause *Node) {
751 OS << "grainsize(";
752 Node->getGrainsize()->printPretty(OS, nullptr, Policy, 0);
753 OS << ")";
754}
755
Alexey Bataev382967a2015-12-08 12:06:20 +0000756void OMPClausePrinter::VisitOMPNumTasksClause(OMPNumTasksClause *Node) {
757 OS << "num_tasks(";
758 Node->getNumTasks()->printPretty(OS, nullptr, Policy, 0);
759 OS << ")";
760}
761
Alexey Bataev28c75412015-12-15 08:19:24 +0000762void OMPClausePrinter::VisitOMPHintClause(OMPHintClause *Node) {
763 OS << "hint(";
764 Node->getHint()->printPretty(OS, nullptr, Policy, 0);
765 OS << ")";
766}
767
Alexey Bataev756c1962013-09-24 03:17:45 +0000768template<typename T>
769void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
770 for (typename T::varlist_iterator I = Node->varlist_begin(),
771 E = Node->varlist_end();
Alexey Bataev90c228f2016-02-08 09:29:13 +0000772 I != E; ++I) {
Richard Trieuddd01ce2014-06-09 22:53:25 +0000773 assert(*I && "Expected non-null Stmt");
Alexey Bataev90c228f2016-02-08 09:29:13 +0000774 OS << (I == Node->varlist_begin() ? StartSym : ',');
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000775 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(*I)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +0000776 if (isa<OMPCapturedExprDecl>(DRE->getDecl()))
777 DRE->printPretty(OS, nullptr, Policy, 0);
Alexey Bataev90c228f2016-02-08 09:29:13 +0000778 else
779 DRE->getDecl()->printQualifiedName(OS);
780 } else
Craig Topper36250ad2014-05-12 05:36:57 +0000781 (*I)->printPretty(OS, nullptr, Policy, 0);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000782 }
Alexey Bataev756c1962013-09-24 03:17:45 +0000783}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000784
785void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) {
786 if (!Node->varlist_empty()) {
787 OS << "private";
Alexey Bataev756c1962013-09-24 03:17:45 +0000788 VisitOMPClauseList(Node, '(');
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000789 OS << ")";
790 }
791}
792
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000793void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) {
794 if (!Node->varlist_empty()) {
795 OS << "firstprivate";
796 VisitOMPClauseList(Node, '(');
797 OS << ")";
798 }
799}
800
Alexander Musman1bb328c2014-06-04 13:06:39 +0000801void OMPClausePrinter::VisitOMPLastprivateClause(OMPLastprivateClause *Node) {
802 if (!Node->varlist_empty()) {
803 OS << "lastprivate";
804 VisitOMPClauseList(Node, '(');
805 OS << ")";
806 }
807}
808
Alexey Bataev758e55e2013-09-06 18:03:48 +0000809void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
810 if (!Node->varlist_empty()) {
811 OS << "shared";
Alexey Bataev756c1962013-09-24 03:17:45 +0000812 VisitOMPClauseList(Node, '(');
Alexey Bataev758e55e2013-09-06 18:03:48 +0000813 OS << ")";
814 }
815}
816
Alexey Bataevc5e02582014-06-16 07:08:35 +0000817void OMPClausePrinter::VisitOMPReductionClause(OMPReductionClause *Node) {
818 if (!Node->varlist_empty()) {
819 OS << "reduction(";
820 NestedNameSpecifier *QualifierLoc =
821 Node->getQualifierLoc().getNestedNameSpecifier();
822 OverloadedOperatorKind OOK =
823 Node->getNameInfo().getName().getCXXOverloadedOperator();
824 if (QualifierLoc == nullptr && OOK != OO_None) {
825 // Print reduction identifier in C format
826 OS << getOperatorSpelling(OOK);
827 } else {
828 // Use C++ format
829 if (QualifierLoc != nullptr)
830 QualifierLoc->print(OS, Policy);
831 OS << Node->getNameInfo();
832 }
833 OS << ":";
834 VisitOMPClauseList(Node, ' ');
835 OS << ")";
836 }
837}
838
Alexey Bataev169d96a2017-07-18 20:17:46 +0000839void OMPClausePrinter::VisitOMPTaskReductionClause(
840 OMPTaskReductionClause *Node) {
841 if (!Node->varlist_empty()) {
842 OS << "task_reduction(";
843 NestedNameSpecifier *QualifierLoc =
844 Node->getQualifierLoc().getNestedNameSpecifier();
845 OverloadedOperatorKind OOK =
846 Node->getNameInfo().getName().getCXXOverloadedOperator();
847 if (QualifierLoc == nullptr && OOK != OO_None) {
848 // Print reduction identifier in C format
849 OS << getOperatorSpelling(OOK);
850 } else {
851 // Use C++ format
852 if (QualifierLoc != nullptr)
853 QualifierLoc->print(OS, Policy);
854 OS << Node->getNameInfo();
855 }
856 OS << ":";
857 VisitOMPClauseList(Node, ' ');
858 OS << ")";
859 }
860}
861
Alexey Bataevfa312f32017-07-21 18:48:21 +0000862void OMPClausePrinter::VisitOMPInReductionClause(OMPInReductionClause *Node) {
863 if (!Node->varlist_empty()) {
864 OS << "in_reduction(";
865 NestedNameSpecifier *QualifierLoc =
866 Node->getQualifierLoc().getNestedNameSpecifier();
867 OverloadedOperatorKind OOK =
868 Node->getNameInfo().getName().getCXXOverloadedOperator();
869 if (QualifierLoc == nullptr && OOK != OO_None) {
870 // Print reduction identifier in C format
871 OS << getOperatorSpelling(OOK);
872 } else {
873 // Use C++ format
874 if (QualifierLoc != nullptr)
875 QualifierLoc->print(OS, Policy);
876 OS << Node->getNameInfo();
877 }
878 OS << ":";
879 VisitOMPClauseList(Node, ' ');
880 OS << ")";
881 }
882}
883
Alexander Musman8dba6642014-04-22 13:09:42 +0000884void OMPClausePrinter::VisitOMPLinearClause(OMPLinearClause *Node) {
885 if (!Node->varlist_empty()) {
886 OS << "linear";
Alexey Bataev182227b2015-08-20 10:54:39 +0000887 if (Node->getModifierLoc().isValid()) {
888 OS << '('
889 << getOpenMPSimpleClauseTypeName(OMPC_linear, Node->getModifier());
890 }
Alexander Musman8dba6642014-04-22 13:09:42 +0000891 VisitOMPClauseList(Node, '(');
Alexey Bataev182227b2015-08-20 10:54:39 +0000892 if (Node->getModifierLoc().isValid())
893 OS << ')';
Craig Topper36250ad2014-05-12 05:36:57 +0000894 if (Node->getStep() != nullptr) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000895 OS << ": ";
Craig Topper36250ad2014-05-12 05:36:57 +0000896 Node->getStep()->printPretty(OS, nullptr, Policy, 0);
Alexander Musman8dba6642014-04-22 13:09:42 +0000897 }
898 OS << ")";
899 }
900}
901
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000902void OMPClausePrinter::VisitOMPAlignedClause(OMPAlignedClause *Node) {
903 if (!Node->varlist_empty()) {
904 OS << "aligned";
905 VisitOMPClauseList(Node, '(');
906 if (Node->getAlignment() != nullptr) {
907 OS << ": ";
908 Node->getAlignment()->printPretty(OS, nullptr, Policy, 0);
909 }
910 OS << ")";
911 }
912}
913
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000914void OMPClausePrinter::VisitOMPCopyinClause(OMPCopyinClause *Node) {
915 if (!Node->varlist_empty()) {
916 OS << "copyin";
917 VisitOMPClauseList(Node, '(');
918 OS << ")";
919 }
920}
921
Alexey Bataevbae9a792014-06-27 10:37:06 +0000922void OMPClausePrinter::VisitOMPCopyprivateClause(OMPCopyprivateClause *Node) {
923 if (!Node->varlist_empty()) {
924 OS << "copyprivate";
925 VisitOMPClauseList(Node, '(');
926 OS << ")";
927 }
928}
929
Alexey Bataev6125da92014-07-21 11:26:11 +0000930void OMPClausePrinter::VisitOMPFlushClause(OMPFlushClause *Node) {
931 if (!Node->varlist_empty()) {
932 VisitOMPClauseList(Node, '(');
933 OS << ")";
934 }
935}
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000936
937void OMPClausePrinter::VisitOMPDependClause(OMPDependClause *Node) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000938 OS << "depend(";
939 OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(),
940 Node->getDependencyKind());
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000941 if (!Node->varlist_empty()) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000942 OS << " :";
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000943 VisitOMPClauseList(Node, ' ');
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000944 }
Alexey Bataeveb482352015-12-18 05:05:56 +0000945 OS << ")";
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000946}
Kelvin Li0bff7af2015-11-23 05:32:03 +0000947
948void OMPClausePrinter::VisitOMPMapClause(OMPMapClause *Node) {
949 if (!Node->varlist_empty()) {
950 OS << "map(";
951 if (Node->getMapType() != OMPC_MAP_unknown) {
952 if (Node->getMapTypeModifier() != OMPC_MAP_unknown) {
953 OS << getOpenMPSimpleClauseTypeName(OMPC_map,
954 Node->getMapTypeModifier());
955 OS << ',';
956 }
957 OS << getOpenMPSimpleClauseTypeName(OMPC_map, Node->getMapType());
958 OS << ':';
959 }
960 VisitOMPClauseList(Node, ' ');
961 OS << ")";
962 }
963}
Carlo Bertollib4adf552016-01-15 18:50:31 +0000964
Samuel Antao661c0902016-05-26 17:39:58 +0000965void OMPClausePrinter::VisitOMPToClause(OMPToClause *Node) {
966 if (!Node->varlist_empty()) {
967 OS << "to";
968 VisitOMPClauseList(Node, '(');
969 OS << ")";
970 }
971}
972
Samuel Antaoec172c62016-05-26 17:49:04 +0000973void OMPClausePrinter::VisitOMPFromClause(OMPFromClause *Node) {
974 if (!Node->varlist_empty()) {
975 OS << "from";
976 VisitOMPClauseList(Node, '(');
977 OS << ")";
978 }
979}
980
Carlo Bertollib4adf552016-01-15 18:50:31 +0000981void OMPClausePrinter::VisitOMPDistScheduleClause(OMPDistScheduleClause *Node) {
982 OS << "dist_schedule(" << getOpenMPSimpleClauseTypeName(
983 OMPC_dist_schedule, Node->getDistScheduleKind());
Alexey Bataev3392d762016-02-16 11:18:12 +0000984 if (auto *E = Node->getChunkSize()) {
Carlo Bertollib4adf552016-01-15 18:50:31 +0000985 OS << ", ";
Alexey Bataev60da77e2016-02-29 05:54:20 +0000986 E->printPretty(OS, nullptr, Policy);
Carlo Bertollib4adf552016-01-15 18:50:31 +0000987 }
988 OS << ")";
989}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +0000990
991void OMPClausePrinter::VisitOMPDefaultmapClause(OMPDefaultmapClause *Node) {
992 OS << "defaultmap(";
993 OS << getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
994 Node->getDefaultmapModifier());
995 OS << ": ";
996 OS << getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
997 Node->getDefaultmapKind());
998 OS << ")";
999}
Carlo Bertolli2404b172016-07-13 15:37:16 +00001000
1001void OMPClausePrinter::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *Node) {
1002 if (!Node->varlist_empty()) {
1003 OS << "use_device_ptr";
1004 VisitOMPClauseList(Node, '(');
1005 OS << ")";
1006 }
1007}
Carlo Bertolli70594e92016-07-13 17:16:49 +00001008
1009void OMPClausePrinter::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *Node) {
1010 if (!Node->varlist_empty()) {
1011 OS << "is_device_ptr";
1012 VisitOMPClauseList(Node, '(');
1013 OS << ")";
1014 }
1015}
Alexander Kornienkoab9db512015-06-22 23:07:51 +00001016}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001017
1018//===----------------------------------------------------------------------===//
1019// OpenMP directives printing methods
1020//===----------------------------------------------------------------------===//
1021
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001022void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001023 OMPClausePrinter Printer(OS, Policy);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001024 ArrayRef<OMPClause *> Clauses = S->clauses();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001025 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
1026 I != E; ++I)
1027 if (*I && !(*I)->isImplicit()) {
1028 Printer.Visit(*I);
1029 OS << ' ';
1030 }
1031 OS << "\n";
Alexey Bataev68446b72014-07-18 07:47:19 +00001032 if (S->hasAssociatedStmt() && S->getAssociatedStmt()) {
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001033 assert(isa<CapturedStmt>(S->getAssociatedStmt()) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001034 "Expected captured statement!");
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001035 Stmt *CS = cast<CapturedStmt>(S->getAssociatedStmt())->getCapturedStmt();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001036 PrintStmt(CS);
1037 }
1038}
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001039
1040void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
1041 Indent() << "#pragma omp parallel ";
1042 PrintOMPExecutableDirective(Node);
1043}
1044
1045void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
1046 Indent() << "#pragma omp simd ";
1047 PrintOMPExecutableDirective(Node);
1048}
1049
Alexey Bataevf29276e2014-06-18 04:14:57 +00001050void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) {
1051 Indent() << "#pragma omp for ";
1052 PrintOMPExecutableDirective(Node);
1053}
1054
Alexander Musmanf82886e2014-09-18 05:12:34 +00001055void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) {
1056 Indent() << "#pragma omp for simd ";
1057 PrintOMPExecutableDirective(Node);
1058}
1059
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001060void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) {
1061 Indent() << "#pragma omp sections ";
1062 PrintOMPExecutableDirective(Node);
1063}
1064
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001065void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) {
1066 Indent() << "#pragma omp section";
1067 PrintOMPExecutableDirective(Node);
1068}
1069
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001070void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) {
1071 Indent() << "#pragma omp single ";
1072 PrintOMPExecutableDirective(Node);
1073}
1074
Alexander Musman80c22892014-07-17 08:54:58 +00001075void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) {
1076 Indent() << "#pragma omp master";
1077 PrintOMPExecutableDirective(Node);
1078}
1079
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001080void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) {
1081 Indent() << "#pragma omp critical";
1082 if (Node->getDirectiveName().getName()) {
1083 OS << " (";
1084 Node->getDirectiveName().printName(OS);
1085 OS << ")";
1086 }
Alexey Bataev28c75412015-12-15 08:19:24 +00001087 OS << " ";
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001088 PrintOMPExecutableDirective(Node);
1089}
1090
Alexey Bataev4acb8592014-07-07 13:01:15 +00001091void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) {
1092 Indent() << "#pragma omp parallel for ";
1093 PrintOMPExecutableDirective(Node);
1094}
1095
Alexander Musmane4e893b2014-09-23 09:33:00 +00001096void StmtPrinter::VisitOMPParallelForSimdDirective(
1097 OMPParallelForSimdDirective *Node) {
1098 Indent() << "#pragma omp parallel for simd ";
1099 PrintOMPExecutableDirective(Node);
1100}
1101
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001102void StmtPrinter::VisitOMPParallelSectionsDirective(
1103 OMPParallelSectionsDirective *Node) {
1104 Indent() << "#pragma omp parallel sections ";
1105 PrintOMPExecutableDirective(Node);
1106}
1107
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001108void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) {
1109 Indent() << "#pragma omp task ";
1110 PrintOMPExecutableDirective(Node);
1111}
1112
Alexey Bataev68446b72014-07-18 07:47:19 +00001113void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) {
1114 Indent() << "#pragma omp taskyield";
1115 PrintOMPExecutableDirective(Node);
1116}
1117
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001118void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) {
1119 Indent() << "#pragma omp barrier";
1120 PrintOMPExecutableDirective(Node);
1121}
1122
Alexey Bataev2df347a2014-07-18 10:17:07 +00001123void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) {
1124 Indent() << "#pragma omp taskwait";
1125 PrintOMPExecutableDirective(Node);
1126}
1127
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001128void StmtPrinter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective *Node) {
Alexey Bataev169d96a2017-07-18 20:17:46 +00001129 Indent() << "#pragma omp taskgroup ";
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001130 PrintOMPExecutableDirective(Node);
1131}
1132
Alexey Bataev6125da92014-07-21 11:26:11 +00001133void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) {
1134 Indent() << "#pragma omp flush ";
1135 PrintOMPExecutableDirective(Node);
1136}
1137
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001138void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) {
Alexey Bataev346265e2015-09-25 10:37:12 +00001139 Indent() << "#pragma omp ordered ";
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001140 PrintOMPExecutableDirective(Node);
1141}
1142
Alexey Bataev0162e452014-07-22 10:10:35 +00001143void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) {
1144 Indent() << "#pragma omp atomic ";
1145 PrintOMPExecutableDirective(Node);
1146}
1147
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001148void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) {
1149 Indent() << "#pragma omp target ";
1150 PrintOMPExecutableDirective(Node);
1151}
1152
Michael Wong65f367f2015-07-21 13:44:28 +00001153void StmtPrinter::VisitOMPTargetDataDirective(OMPTargetDataDirective *Node) {
1154 Indent() << "#pragma omp target data ";
1155 PrintOMPExecutableDirective(Node);
1156}
1157
Samuel Antaodf67fc42016-01-19 19:15:56 +00001158void StmtPrinter::VisitOMPTargetEnterDataDirective(
1159 OMPTargetEnterDataDirective *Node) {
1160 Indent() << "#pragma omp target enter data ";
1161 PrintOMPExecutableDirective(Node);
1162}
1163
Samuel Antao72590762016-01-19 20:04:50 +00001164void StmtPrinter::VisitOMPTargetExitDataDirective(
1165 OMPTargetExitDataDirective *Node) {
1166 Indent() << "#pragma omp target exit data ";
1167 PrintOMPExecutableDirective(Node);
1168}
1169
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001170void StmtPrinter::VisitOMPTargetParallelDirective(
1171 OMPTargetParallelDirective *Node) {
1172 Indent() << "#pragma omp target parallel ";
1173 PrintOMPExecutableDirective(Node);
1174}
1175
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001176void StmtPrinter::VisitOMPTargetParallelForDirective(
1177 OMPTargetParallelForDirective *Node) {
1178 Indent() << "#pragma omp target parallel for ";
1179 PrintOMPExecutableDirective(Node);
1180}
1181
Alexey Bataev13314bf2014-10-09 04:18:56 +00001182void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) {
1183 Indent() << "#pragma omp teams ";
1184 PrintOMPExecutableDirective(Node);
1185}
1186
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001187void StmtPrinter::VisitOMPCancellationPointDirective(
1188 OMPCancellationPointDirective *Node) {
1189 Indent() << "#pragma omp cancellation point "
1190 << getOpenMPDirectiveName(Node->getCancelRegion());
1191 PrintOMPExecutableDirective(Node);
1192}
Alexey Bataev80909872015-07-02 11:25:17 +00001193
1194void StmtPrinter::VisitOMPCancelDirective(OMPCancelDirective *Node) {
1195 Indent() << "#pragma omp cancel "
Alexey Bataev87933c72015-09-18 08:07:34 +00001196 << getOpenMPDirectiveName(Node->getCancelRegion()) << " ";
Alexey Bataev80909872015-07-02 11:25:17 +00001197 PrintOMPExecutableDirective(Node);
1198}
Alexey Bataev49f6e782015-12-01 04:18:41 +00001199
1200void StmtPrinter::VisitOMPTaskLoopDirective(OMPTaskLoopDirective *Node) {
1201 Indent() << "#pragma omp taskloop ";
1202 PrintOMPExecutableDirective(Node);
1203}
1204
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001205void StmtPrinter::VisitOMPTaskLoopSimdDirective(
1206 OMPTaskLoopSimdDirective *Node) {
1207 Indent() << "#pragma omp taskloop simd ";
1208 PrintOMPExecutableDirective(Node);
1209}
1210
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001211void StmtPrinter::VisitOMPDistributeDirective(OMPDistributeDirective *Node) {
1212 Indent() << "#pragma omp distribute ";
1213 PrintOMPExecutableDirective(Node);
1214}
1215
Samuel Antao686c70c2016-05-26 17:30:50 +00001216void StmtPrinter::VisitOMPTargetUpdateDirective(
1217 OMPTargetUpdateDirective *Node) {
1218 Indent() << "#pragma omp target update ";
1219 PrintOMPExecutableDirective(Node);
1220}
1221
Carlo Bertolli9925f152016-06-27 14:55:37 +00001222void StmtPrinter::VisitOMPDistributeParallelForDirective(
1223 OMPDistributeParallelForDirective *Node) {
1224 Indent() << "#pragma omp distribute parallel for ";
1225 PrintOMPExecutableDirective(Node);
1226}
1227
Kelvin Li4a39add2016-07-05 05:00:15 +00001228void StmtPrinter::VisitOMPDistributeParallelForSimdDirective(
1229 OMPDistributeParallelForSimdDirective *Node) {
1230 Indent() << "#pragma omp distribute parallel for simd ";
1231 PrintOMPExecutableDirective(Node);
1232}
1233
Kelvin Li787f3fc2016-07-06 04:45:38 +00001234void StmtPrinter::VisitOMPDistributeSimdDirective(
1235 OMPDistributeSimdDirective *Node) {
1236 Indent() << "#pragma omp distribute simd ";
1237 PrintOMPExecutableDirective(Node);
1238}
1239
Kelvin Lia579b912016-07-14 02:54:56 +00001240void StmtPrinter::VisitOMPTargetParallelForSimdDirective(
1241 OMPTargetParallelForSimdDirective *Node) {
1242 Indent() << "#pragma omp target parallel for simd ";
1243 PrintOMPExecutableDirective(Node);
1244}
1245
Kelvin Li986330c2016-07-20 22:57:10 +00001246void StmtPrinter::VisitOMPTargetSimdDirective(OMPTargetSimdDirective *Node) {
1247 Indent() << "#pragma omp target simd ";
1248 PrintOMPExecutableDirective(Node);
1249}
1250
Kelvin Li02532872016-08-05 14:37:37 +00001251void StmtPrinter::VisitOMPTeamsDistributeDirective(
1252 OMPTeamsDistributeDirective *Node) {
1253 Indent() << "#pragma omp teams distribute ";
1254 PrintOMPExecutableDirective(Node);
1255}
1256
Kelvin Li4e325f72016-10-25 12:50:55 +00001257void StmtPrinter::VisitOMPTeamsDistributeSimdDirective(
1258 OMPTeamsDistributeSimdDirective *Node) {
1259 Indent() << "#pragma omp teams distribute simd ";
1260 PrintOMPExecutableDirective(Node);
1261}
1262
Kelvin Li579e41c2016-11-30 23:51:03 +00001263void StmtPrinter::VisitOMPTeamsDistributeParallelForSimdDirective(
1264 OMPTeamsDistributeParallelForSimdDirective *Node) {
1265 Indent() << "#pragma omp teams distribute parallel for simd ";
1266 PrintOMPExecutableDirective(Node);
1267}
1268
Kelvin Li7ade93f2016-12-09 03:24:30 +00001269void StmtPrinter::VisitOMPTeamsDistributeParallelForDirective(
1270 OMPTeamsDistributeParallelForDirective *Node) {
1271 Indent() << "#pragma omp teams distribute parallel for ";
1272 PrintOMPExecutableDirective(Node);
1273}
1274
Kelvin Libf594a52016-12-17 05:48:59 +00001275void StmtPrinter::VisitOMPTargetTeamsDirective(OMPTargetTeamsDirective *Node) {
1276 Indent() << "#pragma omp target teams ";
1277 PrintOMPExecutableDirective(Node);
1278}
1279
Kelvin Li83c451e2016-12-25 04:52:54 +00001280void StmtPrinter::VisitOMPTargetTeamsDistributeDirective(
1281 OMPTargetTeamsDistributeDirective *Node) {
1282 Indent() << "#pragma omp target teams distribute ";
1283 PrintOMPExecutableDirective(Node);
1284}
1285
Kelvin Li80e8f562016-12-29 22:16:30 +00001286void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForDirective(
1287 OMPTargetTeamsDistributeParallelForDirective *Node) {
1288 Indent() << "#pragma omp target teams distribute parallel for ";
1289 PrintOMPExecutableDirective(Node);
1290}
1291
Kelvin Li1851df52017-01-03 05:23:48 +00001292void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForSimdDirective(
1293 OMPTargetTeamsDistributeParallelForSimdDirective *Node) {
1294 Indent() << "#pragma omp target teams distribute parallel for simd ";
1295 PrintOMPExecutableDirective(Node);
1296}
1297
Kelvin Lida681182017-01-10 18:08:18 +00001298void StmtPrinter::VisitOMPTargetTeamsDistributeSimdDirective(
1299 OMPTargetTeamsDistributeSimdDirective *Node) {
1300 Indent() << "#pragma omp target teams distribute simd ";
1301 PrintOMPExecutableDirective(Node);
1302}
1303
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001304//===----------------------------------------------------------------------===//
Chris Lattner71e23ce2006-11-04 20:18:38 +00001305// Expr printing methods.
1306//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001307
Chris Lattner882f7882006-11-04 18:52:07 +00001308void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00001309 if (auto *OCED = dyn_cast<OMPCapturedExprDecl>(Node->getDecl())) {
1310 OCED->getInit()->IgnoreImpCasts()->printPretty(OS, nullptr, Policy);
1311 return;
1312 }
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001313 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1314 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001315 if (Node->hasTemplateKeyword())
1316 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001317 OS << Node->getNameInfo();
John McCallb3774b52010-08-19 23:49:38 +00001318 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +00001319 TemplateSpecializationType::PrintTemplateArgumentList(
David Majnemer6fbeee32016-07-07 04:43:07 +00001320 OS, Node->template_arguments(), Policy);
Douglas Gregorc7acfdf2009-01-06 05:10:23 +00001321}
1322
John McCall8cd78132009-11-19 22:55:06 +00001323void StmtPrinter::VisitDependentScopeDeclRefExpr(
1324 DependentScopeDeclRefExpr *Node) {
Axel Naumann20b27862011-01-24 15:44:00 +00001325 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1326 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001327 if (Node->hasTemplateKeyword())
1328 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001329 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +00001330 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +00001331 TemplateSpecializationType::PrintTemplateArgumentList(
David Majnemer6fbeee32016-07-07 04:43:07 +00001332 OS, Node->template_arguments(), Policy);
Douglas Gregor90a1a652009-03-19 17:26:29 +00001333}
1334
John McCalld14a8642009-11-21 08:51:07 +00001335void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregora727cb92009-06-30 22:34:41 +00001336 if (Node->getQualifier())
1337 Node->getQualifier()->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001338 if (Node->hasTemplateKeyword())
1339 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001340 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +00001341 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +00001342 TemplateSpecializationType::PrintTemplateArgumentList(
David Majnemer6fbeee32016-07-07 04:43:07 +00001343 OS, Node->template_arguments(), Policy);
Douglas Gregora727cb92009-06-30 22:34:41 +00001344}
1345
Steve Naroffe46504b2007-11-12 14:29:37 +00001346void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +00001347 if (Node->getBase()) {
1348 PrintExpr(Node->getBase());
1349 OS << (Node->isArrow() ? "->" : ".");
1350 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001351 OS << *Node->getDecl();
Steve Naroffe46504b2007-11-12 14:29:37 +00001352}
1353
Steve Naroffec944032008-05-30 00:40:33 +00001354void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Fariborz Jahanian681c0752010-10-14 16:04:05 +00001355 if (Node->isSuperReceiver())
1356 OS << "super.";
Richard Trieu6d92e502014-02-06 23:26:23 +00001357 else if (Node->isObjectReceiver() && Node->getBase()) {
Steve Naroffec944032008-05-30 00:40:33 +00001358 PrintExpr(Node->getBase());
1359 OS << ".";
Richard Trieu6d92e502014-02-06 23:26:23 +00001360 } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
1361 OS << Node->getClassReceiver()->getName() << ".";
Steve Naroffec944032008-05-30 00:40:33 +00001362 }
Fariborz Jahanian681c0752010-10-14 16:04:05 +00001363
John McCallb7bd14f2010-12-02 01:19:52 +00001364 if (Node->isImplicitProperty())
Aaron Ballmanb190f972014-01-03 17:59:55 +00001365 Node->getImplicitPropertyGetter()->getSelector().print(OS);
John McCallb7bd14f2010-12-02 01:19:52 +00001366 else
1367 OS << Node->getExplicitProperty()->getName();
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00001368}
1369
Ted Kremeneke65b0862012-03-06 20:05:56 +00001370void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
1371
1372 PrintExpr(Node->getBaseExpr());
1373 OS << "[";
1374 PrintExpr(Node->getKeyExpr());
1375 OS << "]";
1376}
1377
Chris Lattner6307f192008-08-10 01:53:14 +00001378void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Alexey Bataevec474782014-10-09 08:45:04 +00001379 OS << PredefinedExpr::getIdentTypeName(Node->getIdentType());
Anders Carlsson625bfc82007-07-21 05:21:51 +00001380}
1381
Steve Naroffae4143e2007-04-26 20:39:23 +00001382void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +00001383 unsigned value = Node->getValue();
Douglas Gregorfb65e592011-07-27 05:40:30 +00001384
1385 switch (Node->getKind()) {
1386 case CharacterLiteral::Ascii: break; // no prefix.
1387 case CharacterLiteral::Wide: OS << 'L'; break;
Aaron Ballman9a17c852016-01-07 20:59:26 +00001388 case CharacterLiteral::UTF8: OS << "u8"; break;
Douglas Gregorfb65e592011-07-27 05:40:30 +00001389 case CharacterLiteral::UTF16: OS << 'u'; break;
1390 case CharacterLiteral::UTF32: OS << 'U'; break;
1391 }
1392
Chris Lattner666115c2007-07-13 23:58:20 +00001393 switch (value) {
1394 case '\\':
1395 OS << "'\\\\'";
1396 break;
1397 case '\'':
1398 OS << "'\\''";
1399 break;
1400 case '\a':
1401 // TODO: K&R: the meaning of '\\a' is different in traditional C
1402 OS << "'\\a'";
1403 break;
1404 case '\b':
1405 OS << "'\\b'";
1406 break;
1407 // Nonstandard escape sequence.
1408 /*case '\e':
1409 OS << "'\\e'";
1410 break;*/
1411 case '\f':
1412 OS << "'\\f'";
1413 break;
1414 case '\n':
1415 OS << "'\\n'";
1416 break;
1417 case '\r':
1418 OS << "'\\r'";
1419 break;
1420 case '\t':
1421 OS << "'\\t'";
1422 break;
1423 case '\v':
1424 OS << "'\\v'";
1425 break;
1426 default:
Steven Watanabee43ae192016-02-13 02:31:28 +00001427 // A character literal might be sign-extended, which
1428 // would result in an invalid \U escape sequence.
1429 // FIXME: multicharacter literals such as '\xFF\xFF\xFF\xFF'
1430 // are not correctly handled.
1431 if ((value & ~0xFFu) == ~0xFFu && Node->getKind() == CharacterLiteral::Ascii)
1432 value &= 0xFFu;
Jordan Rose00d1b592013-02-08 22:30:27 +00001433 if (value < 256 && isPrintable((unsigned char)value))
Chris Lattner666115c2007-07-13 23:58:20 +00001434 OS << "'" << (char)value << "'";
Jordan Rose00d1b592013-02-08 22:30:27 +00001435 else if (value < 256)
1436 OS << "'\\x" << llvm::format("%02x", value) << "'";
1437 else if (value <= 0xFFFF)
1438 OS << "'\\u" << llvm::format("%04x", value) << "'";
1439 else
1440 OS << "'\\U" << llvm::format("%08x", value) << "'";
Chris Lattner6e9d9b32007-07-13 05:18:11 +00001441 }
Steve Naroffae4143e2007-04-26 20:39:23 +00001442}
1443
Steve Naroffdf7855b2007-02-21 23:46:25 +00001444void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +00001445 bool isSigned = Node->getType()->isSignedIntegerType();
1446 OS << Node->getValue().toString(10, isSigned);
Mike Stump11289f42009-09-09 15:08:12 +00001447
Chris Lattner06430412007-05-21 05:45:03 +00001448 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall9dd450b2009-09-21 23:43:11 +00001449 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
David Blaikie83d382b2011-09-23 05:06:16 +00001450 default: llvm_unreachable("Unexpected type for integer literal!");
David Majnemerbe09e8e2015-03-06 18:04:22 +00001451 case BuiltinType::Char_S:
1452 case BuiltinType::Char_U: OS << "i8"; break;
David Majnemer65a407c2014-06-21 18:46:07 +00001453 case BuiltinType::UChar: OS << "Ui8"; break;
1454 case BuiltinType::Short: OS << "i16"; break;
1455 case BuiltinType::UShort: OS << "Ui16"; break;
Chris Lattner06430412007-05-21 05:45:03 +00001456 case BuiltinType::Int: break; // no suffix.
1457 case BuiltinType::UInt: OS << 'U'; break;
1458 case BuiltinType::Long: OS << 'L'; break;
1459 case BuiltinType::ULong: OS << "UL"; break;
1460 case BuiltinType::LongLong: OS << "LL"; break;
1461 case BuiltinType::ULongLong: OS << "ULL"; break;
1462 }
Chris Lattner882f7882006-11-04 18:52:07 +00001463}
Benjamin Kramer8a526762012-09-20 14:07:17 +00001464
1465static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
1466 bool PrintSuffix) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001467 SmallString<16> Str;
Eli Friedman53392062011-10-05 20:32:03 +00001468 Node->getValue().toString(Str);
1469 OS << Str;
Benjamin Kramer8a526762012-09-20 14:07:17 +00001470 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
1471 OS << '.'; // Trailing dot in order to separate from ints.
1472
1473 if (!PrintSuffix)
1474 return;
1475
1476 // Emit suffixes. Float literals are always a builtin float type.
1477 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
1478 default: llvm_unreachable("Unexpected type for float literal!");
1479 case BuiltinType::Half: break; // FIXME: suffix?
1480 case BuiltinType::Double: break; // no suffix.
1481 case BuiltinType::Float: OS << 'F'; break;
1482 case BuiltinType::LongDouble: OS << 'L'; break;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00001483 case BuiltinType::Float128: OS << 'Q'; break;
Benjamin Kramer8a526762012-09-20 14:07:17 +00001484 }
1485}
1486
1487void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
1488 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
Chris Lattner882f7882006-11-04 18:52:07 +00001489}
Chris Lattner1c20a172007-08-26 03:42:43 +00001490
1491void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
1492 PrintExpr(Node->getSubExpr());
1493 OS << "i";
1494}
1495
Steve Naroffdf7855b2007-02-21 23:46:25 +00001496void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Richard Trieudc355912012-06-13 20:25:24 +00001497 Str->outputString(OS);
Chris Lattner882f7882006-11-04 18:52:07 +00001498}
1499void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
1500 OS << "(";
1501 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +00001502 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +00001503}
1504void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +00001505 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +00001506 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump11289f42009-09-09 15:08:12 +00001507
Eli Friedman1cf25362009-06-14 22:39:26 +00001508 // Print a space if this is an "identifier operator" like __real, or if
1509 // it might be concatenated incorrectly like '+'.
Chris Lattnere5b60442007-08-23 21:46:40 +00001510 switch (Node->getOpcode()) {
1511 default: break;
John McCalle3027922010-08-25 11:45:40 +00001512 case UO_Real:
1513 case UO_Imag:
1514 case UO_Extension:
Chris Lattnere5b60442007-08-23 21:46:40 +00001515 OS << ' ';
1516 break;
John McCalle3027922010-08-25 11:45:40 +00001517 case UO_Plus:
1518 case UO_Minus:
Eli Friedman1cf25362009-06-14 22:39:26 +00001519 if (isa<UnaryOperator>(Node->getSubExpr()))
1520 OS << ' ';
1521 break;
Chris Lattnere5b60442007-08-23 21:46:40 +00001522 }
1523 }
Chris Lattner882f7882006-11-04 18:52:07 +00001524 PrintExpr(Node->getSubExpr());
Mike Stump11289f42009-09-09 15:08:12 +00001525
Chris Lattner15768702006-11-05 23:54:51 +00001526 if (Node->isPostfix())
1527 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +00001528}
Chris Lattner98dbf0a2007-08-30 17:59:59 +00001529
Douglas Gregor882211c2010-04-28 22:16:22 +00001530void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
1531 OS << "__builtin_offsetof(";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001532 Node->getTypeSourceInfo()->getType().print(OS, Policy);
1533 OS << ", ";
Douglas Gregor882211c2010-04-28 22:16:22 +00001534 bool PrintedSomething = false;
1535 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
James Y Knight7281c352015-12-29 22:31:18 +00001536 OffsetOfNode ON = Node->getComponent(i);
1537 if (ON.getKind() == OffsetOfNode::Array) {
Douglas Gregor882211c2010-04-28 22:16:22 +00001538 // Array node
1539 OS << "[";
1540 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
1541 OS << "]";
1542 PrintedSomething = true;
1543 continue;
1544 }
Douglas Gregord1702062010-04-29 00:18:15 +00001545
1546 // Skip implicit base indirections.
James Y Knight7281c352015-12-29 22:31:18 +00001547 if (ON.getKind() == OffsetOfNode::Base)
Douglas Gregord1702062010-04-29 00:18:15 +00001548 continue;
1549
Douglas Gregor882211c2010-04-28 22:16:22 +00001550 // Field or identifier node.
1551 IdentifierInfo *Id = ON.getFieldName();
1552 if (!Id)
1553 continue;
Alexis Hunta8136cc2010-05-05 15:23:54 +00001554
Douglas Gregor882211c2010-04-28 22:16:22 +00001555 if (PrintedSomething)
1556 OS << ".";
1557 else
1558 PrintedSomething = true;
Alexis Hunta8136cc2010-05-05 15:23:54 +00001559 OS << Id->getName();
Douglas Gregor882211c2010-04-28 22:16:22 +00001560 }
1561 OS << ")";
1562}
1563
Peter Collingbournee190dee2011-03-11 19:24:49 +00001564void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
1565 switch(Node->getKind()) {
1566 case UETT_SizeOf:
1567 OS << "sizeof";
1568 break;
1569 case UETT_AlignOf:
Richard Smith301bc212016-05-19 01:39:10 +00001570 if (Policy.Alignof)
Jordan Rose58d54722012-06-30 21:33:57 +00001571 OS << "alignof";
Richard Smith301bc212016-05-19 01:39:10 +00001572 else if (Policy.UnderscoreAlignof)
Jordan Rose58d54722012-06-30 21:33:57 +00001573 OS << "_Alignof";
1574 else
1575 OS << "__alignof";
Peter Collingbournee190dee2011-03-11 19:24:49 +00001576 break;
1577 case UETT_VecStep:
1578 OS << "vec_step";
1579 break;
Alexey Bataev00396512015-07-02 03:40:19 +00001580 case UETT_OpenMPRequiredSimdAlign:
1581 OS << "__builtin_omp_required_simd_align";
1582 break;
Peter Collingbournee190dee2011-03-11 19:24:49 +00001583 }
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001584 if (Node->isArgumentType()) {
1585 OS << '(';
1586 Node->getArgumentType().print(OS, Policy);
1587 OS << ')';
1588 } else {
Sebastian Redl6f282892008-11-11 17:56:53 +00001589 OS << " ";
1590 PrintExpr(Node->getArgumentExpr());
1591 }
Chris Lattner882f7882006-11-04 18:52:07 +00001592}
Peter Collingbourne91147592011-04-15 00:35:48 +00001593
1594void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
1595 OS << "_Generic(";
1596 PrintExpr(Node->getControllingExpr());
1597 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
1598 OS << ", ";
1599 QualType T = Node->getAssocType(i);
1600 if (T.isNull())
1601 OS << "default";
1602 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001603 T.print(OS, Policy);
Peter Collingbourne91147592011-04-15 00:35:48 +00001604 OS << ": ";
1605 PrintExpr(Node->getAssocExpr(i));
1606 }
1607 OS << ")";
1608}
1609
Chris Lattner882f7882006-11-04 18:52:07 +00001610void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +00001611 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +00001612 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +00001613 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +00001614 OS << "]";
1615}
1616
Alexey Bataev1a3320e2015-08-25 14:24:04 +00001617void StmtPrinter::VisitOMPArraySectionExpr(OMPArraySectionExpr *Node) {
1618 PrintExpr(Node->getBase());
1619 OS << "[";
1620 if (Node->getLowerBound())
1621 PrintExpr(Node->getLowerBound());
Alexey Bataeved5fb672015-09-11 04:54:28 +00001622 if (Node->getColonLoc().isValid()) {
1623 OS << ":";
1624 if (Node->getLength())
1625 PrintExpr(Node->getLength());
1626 }
Alexey Bataev1a3320e2015-08-25 14:24:04 +00001627 OS << "]";
1628}
1629
Peter Collingbourne4b279a02011-02-08 21:17:54 +00001630void StmtPrinter::PrintCallArgs(CallExpr *Call) {
Chris Lattner882f7882006-11-04 18:52:07 +00001631 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001632 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1633 // Don't print any defaulted arguments
1634 break;
1635 }
1636
Chris Lattner882f7882006-11-04 18:52:07 +00001637 if (i) OS << ", ";
1638 PrintExpr(Call->getArg(i));
1639 }
Peter Collingbourne4b279a02011-02-08 21:17:54 +00001640}
1641
1642void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1643 PrintExpr(Call->getCallee());
1644 OS << "(";
1645 PrintCallArgs(Call);
Chris Lattner882f7882006-11-04 18:52:07 +00001646 OS << ")";
1647}
1648void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregore4b414c2009-01-08 22:45:41 +00001649 // FIXME: Suppress printing implicit bases (like "this")
1650 PrintExpr(Node->getBase());
David Blaikie8fab8e52012-11-12 19:12:12 +00001651
1652 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
David Blaikie6bffd6f2012-11-12 19:32:32 +00001653 FieldDecl *ParentDecl = ParentMember
Craig Topper36250ad2014-05-12 05:36:57 +00001654 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : nullptr;
David Blaikie8fab8e52012-11-12 19:12:12 +00001655
David Blaikie6bffd6f2012-11-12 19:32:32 +00001656 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
David Blaikie8fab8e52012-11-12 19:12:12 +00001657 OS << (Node->isArrow() ? "->" : ".");
David Blaikie8fab8e52012-11-12 19:12:12 +00001658
Fariborz Jahanian2990c022010-01-11 21:17:32 +00001659 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1660 if (FD->isAnonymousStructOrUnion())
1661 return;
David Blaikie8fab8e52012-11-12 19:12:12 +00001662
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001663 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1664 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001665 if (Node->hasTemplateKeyword())
1666 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001667 OS << Node->getMemberNameInfo();
John McCallb3774b52010-08-19 23:49:38 +00001668 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +00001669 TemplateSpecializationType::PrintTemplateArgumentList(
David Majnemer6fbeee32016-07-07 04:43:07 +00001670 OS, Node->template_arguments(), Policy);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001671}
Steve Naroffe87026a2009-07-24 17:54:45 +00001672void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1673 PrintExpr(Node->getBase());
1674 OS << (Node->isArrow() ? "->isa" : ".isa");
1675}
1676
Nate Begemance4d7fc2008-04-18 23:10:10 +00001677void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +00001678 PrintExpr(Node->getBase());
1679 OS << ".";
1680 OS << Node->getAccessor().getName();
1681}
Douglas Gregorf19b2312008-10-28 15:36:24 +00001682void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001683 OS << '(';
1684 Node->getTypeAsWritten().print(OS, Policy);
1685 OS << ')';
Chris Lattner882f7882006-11-04 18:52:07 +00001686 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001687}
Steve Naroff57eb2c52007-07-19 21:32:11 +00001688void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001689 OS << '(';
1690 Node->getType().print(OS, Policy);
1691 OS << ')';
Steve Naroff57eb2c52007-07-19 21:32:11 +00001692 PrintExpr(Node->getInitializer());
1693}
Steve Naroff7a5af782007-07-13 16:58:59 +00001694void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Alp Toker028ed912013-12-06 17:56:43 +00001695 // No need to print anything, simply forward to the subexpression.
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001696 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +00001697}
Chris Lattner882f7882006-11-04 18:52:07 +00001698void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1699 PrintExpr(Node->getLHS());
1700 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1701 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001702}
Chris Lattner86928112007-08-25 02:00:02 +00001703void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1704 PrintExpr(Node->getLHS());
1705 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1706 PrintExpr(Node->getRHS());
1707}
Chris Lattner882f7882006-11-04 18:52:07 +00001708void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1709 PrintExpr(Node->getCond());
John McCallc07a0c72011-02-17 10:25:35 +00001710 OS << " ? ";
1711 PrintExpr(Node->getLHS());
1712 OS << " : ";
Chris Lattner882f7882006-11-04 18:52:07 +00001713 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001714}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001715
Chris Lattnereefa10e2007-05-28 06:56:27 +00001716// GNU extensions.
1717
John McCallc07a0c72011-02-17 10:25:35 +00001718void
1719StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1720 PrintExpr(Node->getCommon());
1721 OS << " ?: ";
1722 PrintExpr(Node->getFalseExpr());
1723}
Chris Lattnerd268a7a2007-08-03 17:31:20 +00001724void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +00001725 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +00001726}
1727
Chris Lattner366727f2007-07-24 16:58:17 +00001728void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1729 OS << "(";
1730 PrintRawCompoundStmt(E->getSubStmt());
1731 OS << ")";
1732}
1733
Steve Naroff9efdabc2007-08-03 21:21:27 +00001734void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1735 OS << "__builtin_choose_expr(";
1736 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +00001737 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +00001738 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +00001739 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +00001740 PrintExpr(Node->getRHS());
1741 OS << ")";
1742}
Chris Lattner366727f2007-07-24 16:58:17 +00001743
Douglas Gregor3be4b122008-11-29 04:51:27 +00001744void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1745 OS << "__null";
1746}
1747
Eli Friedmana1b4ed82008-05-14 19:38:39 +00001748void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1749 OS << "__builtin_shufflevector(";
1750 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1751 if (i) OS << ", ";
1752 PrintExpr(Node->getExpr(i));
1753 }
1754 OS << ")";
1755}
1756
Hal Finkelc4d7c822013-09-18 03:29:45 +00001757void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1758 OS << "__builtin_convertvector(";
1759 PrintExpr(Node->getSrcExpr());
1760 OS << ", ";
1761 Node->getType().print(OS, Policy);
1762 OS << ")";
1763}
1764
Anders Carlsson4692db02007-08-31 04:56:16 +00001765void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001766 if (Node->getSyntacticForm()) {
1767 Visit(Node->getSyntacticForm());
1768 return;
1769 }
1770
Richard Smithd0e102f2015-01-30 02:04:26 +00001771 OS << "{";
Anders Carlsson4692db02007-08-31 04:56:16 +00001772 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1773 if (i) OS << ", ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001774 if (Node->getInit(i))
1775 PrintExpr(Node->getInit(i));
1776 else
Richard Smithd0e102f2015-01-30 02:04:26 +00001777 OS << "{}";
Anders Carlsson4692db02007-08-31 04:56:16 +00001778 }
Richard Smithd0e102f2015-01-30 02:04:26 +00001779 OS << "}";
Anders Carlsson4692db02007-08-31 04:56:16 +00001780}
1781
Richard Smith410306b2016-12-12 02:53:20 +00001782void StmtPrinter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *Node) {
1783 // There's no way to express this expression in any of our supported
1784 // languages, so just emit something terse and (hopefully) clear.
1785 OS << "{";
1786 PrintExpr(Node->getSubExpr());
1787 OS << "}";
1788}
1789
1790void StmtPrinter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *Node) {
1791 OS << "*";
1792}
1793
Nate Begeman5ec4b312009-08-10 23:49:36 +00001794void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
Richard Smithd0e102f2015-01-30 02:04:26 +00001795 OS << "(";
Nate Begeman5ec4b312009-08-10 23:49:36 +00001796 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1797 if (i) OS << ", ";
1798 PrintExpr(Node->getExpr(i));
1799 }
Richard Smithd0e102f2015-01-30 02:04:26 +00001800 OS << ")";
Nate Begeman5ec4b312009-08-10 23:49:36 +00001801}
1802
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001803void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Justin Bognercb337032015-05-28 22:19:36 +00001804 bool NeedsEquals = true;
David Majnemerf7e36092016-06-23 00:15:04 +00001805 for (const DesignatedInitExpr::Designator &D : Node->designators()) {
1806 if (D.isFieldDesignator()) {
1807 if (D.getDotLoc().isInvalid()) {
1808 if (IdentifierInfo *II = D.getFieldName()) {
Serge Pavloveb57aa62014-06-20 17:08:28 +00001809 OS << II->getName() << ":";
Justin Bognercb337032015-05-28 22:19:36 +00001810 NeedsEquals = false;
1811 }
Serge Pavloveb57aa62014-06-20 17:08:28 +00001812 } else {
David Majnemerf7e36092016-06-23 00:15:04 +00001813 OS << "." << D.getFieldName()->getName();
Serge Pavloveb57aa62014-06-20 17:08:28 +00001814 }
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001815 } else {
1816 OS << "[";
David Majnemerf7e36092016-06-23 00:15:04 +00001817 if (D.isArrayDesignator()) {
1818 PrintExpr(Node->getArrayIndex(D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001819 } else {
David Majnemerf7e36092016-06-23 00:15:04 +00001820 PrintExpr(Node->getArrayRangeStart(D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001821 OS << " ... ";
David Majnemerf7e36092016-06-23 00:15:04 +00001822 PrintExpr(Node->getArrayRangeEnd(D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001823 }
1824 OS << "]";
1825 }
1826 }
1827
Justin Bognercb337032015-05-28 22:19:36 +00001828 if (NeedsEquals)
1829 OS << " = ";
1830 else
1831 OS << " ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001832 PrintExpr(Node->getInit());
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001833}
1834
Yunzhong Gaocb779302015-06-10 00:27:52 +00001835void StmtPrinter::VisitDesignatedInitUpdateExpr(
1836 DesignatedInitUpdateExpr *Node) {
1837 OS << "{";
1838 OS << "/*base*/";
1839 PrintExpr(Node->getBase());
1840 OS << ", ";
1841
1842 OS << "/*updater*/";
1843 PrintExpr(Node->getUpdater());
1844 OS << "}";
1845}
1846
1847void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) {
1848 OS << "/*no init*/";
1849}
1850
Douglas Gregor0202cb42009-01-29 17:44:32 +00001851void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Richard Smith301bc212016-05-19 01:39:10 +00001852 if (Node->getType()->getAsCXXRecordDecl()) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001853 OS << "/*implicit*/";
1854 Node->getType().print(OS, Policy);
1855 OS << "()";
1856 } else {
1857 OS << "/*implicit*/(";
1858 Node->getType().print(OS, Policy);
1859 OS << ')';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001860 if (Node->getType()->isRecordType())
1861 OS << "{}";
1862 else
1863 OS << 0;
1864 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001865}
1866
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001867void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman79635842009-05-30 04:20:30 +00001868 OS << "__builtin_va_arg(";
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001869 PrintExpr(Node->getSubExpr());
1870 OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001871 Node->getType().print(OS, Policy);
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001872 OS << ")";
1873}
1874
John McCallfe96e0b2011-11-06 09:01:30 +00001875void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1876 PrintExpr(Node->getSyntacticForm());
1877}
1878
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001879void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
Craig Topper36250ad2014-05-12 05:36:57 +00001880 const char *Name = nullptr;
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001881 switch (Node->getOp()) {
Richard Smithfeea8832012-04-12 05:08:17 +00001882#define BUILTIN(ID, TYPE, ATTRS)
1883#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1884 case AtomicExpr::AO ## ID: \
1885 Name = #ID "("; \
1886 break;
1887#include "clang/Basic/Builtins.def"
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001888 }
1889 OS << Name;
Richard Smithfeea8832012-04-12 05:08:17 +00001890
1891 // AtomicExpr stores its subexpressions in a permuted order.
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001892 PrintExpr(Node->getPtr());
Richard Smithfeea8832012-04-12 05:08:17 +00001893 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1894 Node->getOp() != AtomicExpr::AO__atomic_load_n) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001895 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001896 PrintExpr(Node->getVal1());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001897 }
Richard Smithfeea8832012-04-12 05:08:17 +00001898 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1899 Node->isCmpXChg()) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001900 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001901 PrintExpr(Node->getVal2());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001902 }
Richard Smithfeea8832012-04-12 05:08:17 +00001903 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1904 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
Richard Smithfeea8832012-04-12 05:08:17 +00001905 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001906 PrintExpr(Node->getWeak());
Richard Smithfeea8832012-04-12 05:08:17 +00001907 }
Richard Smithdf6bee82013-05-01 19:02:43 +00001908 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) {
1909 OS << ", ";
David Chisnallfa35df62012-01-16 17:27:18 +00001910 PrintExpr(Node->getOrder());
Richard Smithdf6bee82013-05-01 19:02:43 +00001911 }
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001912 if (Node->isCmpXChg()) {
1913 OS << ", ";
1914 PrintExpr(Node->getOrderFail());
1915 }
1916 OS << ")";
1917}
1918
Chris Lattnereefa10e2007-05-28 06:56:27 +00001919// C++
Douglas Gregor993603d2008-11-14 16:09:21 +00001920void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1921 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1922 "",
1923#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1924 Spelling,
1925#include "clang/Basic/OperatorKinds.def"
1926 };
1927
1928 OverloadedOperatorKind Kind = Node->getOperator();
1929 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1930 if (Node->getNumArgs() == 1) {
1931 OS << OpStrings[Kind] << ' ';
1932 PrintExpr(Node->getArg(0));
1933 } else {
1934 PrintExpr(Node->getArg(0));
1935 OS << ' ' << OpStrings[Kind];
1936 }
Eli Friedman8e236422012-10-12 22:45:14 +00001937 } else if (Kind == OO_Arrow) {
1938 PrintExpr(Node->getArg(0));
Douglas Gregor993603d2008-11-14 16:09:21 +00001939 } else if (Kind == OO_Call) {
1940 PrintExpr(Node->getArg(0));
1941 OS << '(';
1942 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1943 if (ArgIdx > 1)
1944 OS << ", ";
1945 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1946 PrintExpr(Node->getArg(ArgIdx));
1947 }
1948 OS << ')';
1949 } else if (Kind == OO_Subscript) {
1950 PrintExpr(Node->getArg(0));
1951 OS << '[';
1952 PrintExpr(Node->getArg(1));
1953 OS << ']';
1954 } else if (Node->getNumArgs() == 1) {
1955 OS << OpStrings[Kind] << ' ';
1956 PrintExpr(Node->getArg(0));
1957 } else if (Node->getNumArgs() == 2) {
1958 PrintExpr(Node->getArg(0));
1959 OS << ' ' << OpStrings[Kind] << ' ';
1960 PrintExpr(Node->getArg(1));
1961 } else {
David Blaikie83d382b2011-09-23 05:06:16 +00001962 llvm_unreachable("unknown overloaded operator");
Douglas Gregor993603d2008-11-14 16:09:21 +00001963 }
1964}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001965
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001966void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
Benjamin Kramer00e8a192014-02-25 18:03:55 +00001967 // If we have a conversion operator call only print the argument.
1968 CXXMethodDecl *MD = Node->getMethodDecl();
1969 if (MD && isa<CXXConversionDecl>(MD)) {
1970 PrintExpr(Node->getImplicitObjectArgument());
1971 return;
1972 }
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001973 VisitCallExpr(cast<CallExpr>(Node));
1974}
1975
Peter Collingbourne41f85462011-02-09 21:07:24 +00001976void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1977 PrintExpr(Node->getCallee());
1978 OS << "<<<";
1979 PrintCallArgs(Node->getConfig());
1980 OS << ">>>(";
1981 PrintCallArgs(Node);
1982 OS << ")";
1983}
1984
Douglas Gregore200adc2008-10-27 19:41:14 +00001985void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1986 OS << Node->getCastName() << '<';
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001987 Node->getTypeAsWritten().print(OS, Policy);
1988 OS << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +00001989 PrintExpr(Node->getSubExpr());
1990 OS << ")";
1991}
1992
Douglas Gregore200adc2008-10-27 19:41:14 +00001993void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1994 VisitCXXNamedCastExpr(Node);
1995}
1996
1997void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1998 VisitCXXNamedCastExpr(Node);
1999}
2000
2001void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
2002 VisitCXXNamedCastExpr(Node);
2003}
2004
2005void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
2006 VisitCXXNamedCastExpr(Node);
2007}
2008
Sebastian Redlc4704762008-11-11 11:37:55 +00002009void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
2010 OS << "typeid(";
2011 if (Node->isTypeOperand()) {
David Majnemer143c55e2013-09-27 07:04:31 +00002012 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
Sebastian Redlc4704762008-11-11 11:37:55 +00002013 } else {
2014 PrintExpr(Node->getExprOperand());
2015 }
2016 OS << ")";
2017}
2018
Francois Pichet9f4f2072010-09-08 12:20:18 +00002019void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
2020 OS << "__uuidof(";
2021 if (Node->isTypeOperand()) {
David Majnemer143c55e2013-09-27 07:04:31 +00002022 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
Francois Pichet9f4f2072010-09-08 12:20:18 +00002023 } else {
2024 PrintExpr(Node->getExprOperand());
2025 }
2026 OS << ")";
2027}
2028
John McCall5e77d762013-04-16 07:28:30 +00002029void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
2030 PrintExpr(Node->getBaseExpr());
2031 if (Node->isArrow())
2032 OS << "->";
2033 else
2034 OS << ".";
2035 if (NestedNameSpecifier *Qualifier =
2036 Node->getQualifierLoc().getNestedNameSpecifier())
2037 Qualifier->print(OS, Policy);
2038 OS << Node->getPropertyDecl()->getDeclName();
2039}
2040
Alexey Bataevf7630272015-11-25 12:01:00 +00002041void StmtPrinter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr *Node) {
2042 PrintExpr(Node->getBase());
2043 OS << "[";
2044 PrintExpr(Node->getIdx());
2045 OS << "]";
2046}
2047
Richard Smithc67fdd42012-03-07 08:35:16 +00002048void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
2049 switch (Node->getLiteralOperatorKind()) {
2050 case UserDefinedLiteral::LOK_Raw:
Richard Smith29e95952012-03-09 10:10:02 +00002051 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
Richard Smithc67fdd42012-03-07 08:35:16 +00002052 break;
2053 case UserDefinedLiteral::LOK_Template: {
Richard Smith29e95952012-03-09 10:10:02 +00002054 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
2055 const TemplateArgumentList *Args =
2056 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
2057 assert(Args);
David Majnemer314b72f2015-04-05 05:32:54 +00002058
2059 if (Args->size() != 1) {
Richard Smithe87aeb32015-10-08 00:17:59 +00002060 OS << "operator\"\"" << Node->getUDSuffix()->getName();
David Majnemer314b72f2015-04-05 05:32:54 +00002061 TemplateSpecializationType::PrintTemplateArgumentList(
David Majnemer6fbeee32016-07-07 04:43:07 +00002062 OS, Args->asArray(), Policy);
David Majnemer314b72f2015-04-05 05:32:54 +00002063 OS << "()";
2064 return;
2065 }
2066
Richard Smith29e95952012-03-09 10:10:02 +00002067 const TemplateArgument &Pack = Args->get(0);
Aaron Ballman2a89e852014-07-15 21:32:31 +00002068 for (const auto &P : Pack.pack_elements()) {
2069 char C = (char)P.getAsIntegral().getZExtValue();
Richard Smithc67fdd42012-03-07 08:35:16 +00002070 OS << C;
2071 }
2072 break;
2073 }
Richard Smith75025ba2012-03-08 09:02:38 +00002074 case UserDefinedLiteral::LOK_Integer: {
2075 // Print integer literal without suffix.
2076 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
2077 OS << Int->getValue().toString(10, /*isSigned*/false);
2078 break;
2079 }
Benjamin Kramer8a526762012-09-20 14:07:17 +00002080 case UserDefinedLiteral::LOK_Floating: {
2081 // Print floating literal without suffix.
2082 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
2083 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
2084 break;
2085 }
Richard Smithc67fdd42012-03-07 08:35:16 +00002086 case UserDefinedLiteral::LOK_String:
2087 case UserDefinedLiteral::LOK_Character:
2088 PrintExpr(Node->getCookedLiteral());
2089 break;
2090 }
2091 OS << Node->getUDSuffix()->getName();
2092}
2093
Chris Lattnereefa10e2007-05-28 06:56:27 +00002094void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
2095 OS << (Node->getValue() ? "true" : "false");
2096}
2097
Sebastian Redl576fd422009-05-10 18:38:11 +00002098void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
2099 OS << "nullptr";
2100}
2101
Douglas Gregor97a9c812008-11-04 14:32:21 +00002102void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
2103 OS << "this";
2104}
2105
Chris Lattnerb7e656b2008-02-26 00:51:44 +00002106void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
Craig Topper36250ad2014-05-12 05:36:57 +00002107 if (!Node->getSubExpr())
Chris Lattnerb7e656b2008-02-26 00:51:44 +00002108 OS << "throw";
2109 else {
2110 OS << "throw ";
2111 PrintExpr(Node->getSubExpr());
2112 }
2113}
2114
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002115void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
Richard Smith852c9db2013-04-20 22:23:05 +00002116 // Nothing to print: we picked up the default argument.
2117}
2118
2119void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
2120 // Nothing to print: we picked up the default initializer.
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002121}
2122
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002123void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002124 Node->getType().print(OS, Policy);
Richard Smith1ae689c2015-01-28 22:06:01 +00002125 // If there are no parens, this is list-initialization, and the braces are
2126 // part of the syntax of the inner construct.
2127 if (Node->getLParenLoc().isValid())
2128 OS << "(";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002129 PrintExpr(Node->getSubExpr());
Richard Smith1ae689c2015-01-28 22:06:01 +00002130 if (Node->getLParenLoc().isValid())
2131 OS << ")";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002132}
2133
Anders Carlsson993a4b32009-05-30 20:03:25 +00002134void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
2135 PrintExpr(Node->getSubExpr());
2136}
2137
Douglas Gregordd04d332009-01-16 18:33:17 +00002138void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002139 Node->getType().print(OS, Policy);
Richard Smithed83ebd2015-02-05 07:02:11 +00002140 if (Node->isStdInitListInitialization())
2141 /* Nothing to do; braces are part of creating the std::initializer_list. */;
2142 else if (Node->isListInitialization())
Richard Smith1ae689c2015-01-28 22:06:01 +00002143 OS << "{";
2144 else
2145 OS << "(";
Douglas Gregordd04d332009-01-16 18:33:17 +00002146 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00002147 ArgEnd = Node->arg_end();
Douglas Gregordd04d332009-01-16 18:33:17 +00002148 Arg != ArgEnd; ++Arg) {
Benjamin Kramerf48ee442015-07-18 14:35:53 +00002149 if ((*Arg)->isDefaultArgument())
Rafael Espindola6f6f3c42013-05-24 16:11:44 +00002150 break;
Douglas Gregordd04d332009-01-16 18:33:17 +00002151 if (Arg != Node->arg_begin())
2152 OS << ", ";
2153 PrintExpr(*Arg);
2154 }
Richard Smithed83ebd2015-02-05 07:02:11 +00002155 if (Node->isStdInitListInitialization())
2156 /* See above. */;
2157 else if (Node->isListInitialization())
Richard Smith1ae689c2015-01-28 22:06:01 +00002158 OS << "}";
2159 else
2160 OS << ")";
Douglas Gregordd04d332009-01-16 18:33:17 +00002161}
2162
Douglas Gregore31e6062012-02-07 10:09:13 +00002163void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
2164 OS << '[';
2165 bool NeedComma = false;
2166 switch (Node->getCaptureDefault()) {
2167 case LCD_None:
2168 break;
2169
2170 case LCD_ByCopy:
2171 OS << '=';
2172 NeedComma = true;
2173 break;
2174
2175 case LCD_ByRef:
2176 OS << '&';
2177 NeedComma = true;
2178 break;
2179 }
2180 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
2181 CEnd = Node->explicit_capture_end();
2182 C != CEnd;
2183 ++C) {
2184 if (NeedComma)
2185 OS << ", ";
2186 NeedComma = true;
2187
2188 switch (C->getCaptureKind()) {
2189 case LCK_This:
2190 OS << "this";
2191 break;
Faisal Validc6b5962016-03-21 09:25:37 +00002192 case LCK_StarThis:
2193 OS << "*this";
2194 break;
Douglas Gregore31e6062012-02-07 10:09:13 +00002195 case LCK_ByRef:
James Dennettdd2ffea22015-05-07 18:48:18 +00002196 if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C))
Douglas Gregore31e6062012-02-07 10:09:13 +00002197 OS << '&';
2198 OS << C->getCapturedVar()->getName();
2199 break;
2200
2201 case LCK_ByCopy:
Douglas Gregore31e6062012-02-07 10:09:13 +00002202 OS << C->getCapturedVar()->getName();
2203 break;
Alexey Bataev39c81e22014-08-28 04:28:19 +00002204 case LCK_VLAType:
2205 llvm_unreachable("VLA type in explicit captures.");
Douglas Gregore31e6062012-02-07 10:09:13 +00002206 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00002207
James Dennettdd2ffea22015-05-07 18:48:18 +00002208 if (Node->isInitCapture(C))
Richard Smithbb13c9a2013-09-28 04:02:39 +00002209 PrintExpr(C->getCapturedVar()->getInit());
Douglas Gregore31e6062012-02-07 10:09:13 +00002210 }
2211 OS << ']';
2212
2213 if (Node->hasExplicitParameters()) {
2214 OS << " (";
2215 CXXMethodDecl *Method = Node->getCallOperator();
2216 NeedComma = false;
David Majnemer59f77922016-06-24 04:05:48 +00002217 for (auto P : Method->parameters()) {
Douglas Gregore31e6062012-02-07 10:09:13 +00002218 if (NeedComma) {
2219 OS << ", ";
2220 } else {
2221 NeedComma = true;
2222 }
Aaron Ballman43b68be2014-03-07 17:50:17 +00002223 std::string ParamStr = P->getNameAsString();
2224 P->getOriginalType().print(OS, Policy, ParamStr);
Douglas Gregore31e6062012-02-07 10:09:13 +00002225 }
2226 if (Method->isVariadic()) {
2227 if (NeedComma)
2228 OS << ", ";
2229 OS << "...";
2230 }
2231 OS << ')';
2232
2233 if (Node->isMutable())
2234 OS << " mutable";
2235
2236 const FunctionProtoType *Proto
2237 = Method->getType()->getAs<FunctionProtoType>();
Benjamin Kramer9170e912013-02-22 15:46:01 +00002238 Proto->printExceptionSpecification(OS, Policy);
Douglas Gregore31e6062012-02-07 10:09:13 +00002239
Bill Wendling44426052012-12-20 19:22:21 +00002240 // FIXME: Attributes
Douglas Gregore31e6062012-02-07 10:09:13 +00002241
Douglas Gregor0c46b2b2012-02-13 22:00:16 +00002242 // Print the trailing return type if it was specified in the source.
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002243 if (Node->hasExplicitResultType()) {
2244 OS << " -> ";
Alp Toker314cc812014-01-25 16:55:45 +00002245 Proto->getReturnType().print(OS, Policy);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002246 }
Douglas Gregore31e6062012-02-07 10:09:13 +00002247 }
2248
2249 // Print the body.
2250 CompoundStmt *Body = Node->getBody();
2251 OS << ' ';
2252 PrintStmt(Body);
2253}
2254
Douglas Gregor747eb782010-07-08 06:14:04 +00002255void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregor2b88c112010-09-08 00:15:04 +00002256 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002257 TSInfo->getType().print(OS, Policy);
Douglas Gregor2b88c112010-09-08 00:15:04 +00002258 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002259 Node->getType().print(OS, Policy);
2260 OS << "()";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002261}
2262
Sebastian Redlbd150f42008-11-21 19:14:01 +00002263void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
2264 if (E->isGlobalNew())
2265 OS << "::";
2266 OS << "new ";
2267 unsigned NumPlace = E->getNumPlacementArgs();
Eli Friedmana6fdfaa2012-10-18 20:54:37 +00002268 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002269 OS << "(";
2270 PrintExpr(E->getPlacementArg(0));
2271 for (unsigned i = 1; i < NumPlace; ++i) {
Eli Friedmana6fdfaa2012-10-18 20:54:37 +00002272 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
2273 break;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002274 OS << ", ";
2275 PrintExpr(E->getPlacementArg(i));
2276 }
2277 OS << ") ";
2278 }
2279 if (E->isParenTypeId())
2280 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00002281 std::string TypeS;
2282 if (Expr *Size = E->getArraySize()) {
2283 llvm::raw_string_ostream s(TypeS);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002284 s << '[';
Richard Smith235341b2012-08-16 03:56:14 +00002285 Size->printPretty(s, Helper, Policy);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002286 s << ']';
Sebastian Redld6d55ee2008-12-02 22:08:59 +00002287 }
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002288 E->getAllocatedType().print(OS, Policy, TypeS);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002289 if (E->isParenTypeId())
2290 OS << ")";
2291
Sebastian Redl6047f072012-02-16 12:22:20 +00002292 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
2293 if (InitStyle) {
2294 if (InitStyle == CXXNewExpr::CallInit)
2295 OS << "(";
2296 PrintExpr(E->getInitializer());
2297 if (InitStyle == CXXNewExpr::CallInit)
2298 OS << ")";
Sebastian Redlbd150f42008-11-21 19:14:01 +00002299 }
2300}
2301
2302void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
2303 if (E->isGlobalDelete())
2304 OS << "::";
2305 OS << "delete ";
2306 if (E->isArrayForm())
2307 OS << "[] ";
2308 PrintExpr(E->getArgument());
2309}
2310
Douglas Gregorad8a3362009-09-04 17:36:40 +00002311void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
2312 PrintExpr(E->getBase());
2313 if (E->isArrow())
2314 OS << "->";
2315 else
2316 OS << '.';
2317 if (E->getQualifier())
2318 E->getQualifier()->print(OS, Policy);
Eli Friedman9cc8ac52012-10-23 20:26:57 +00002319 OS << "~";
Mike Stump11289f42009-09-09 15:08:12 +00002320
Douglas Gregor678f90d2010-02-25 01:56:36 +00002321 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
2322 OS << II->getName();
2323 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002324 E->getDestroyedType().print(OS, Policy);
Douglas Gregorad8a3362009-09-04 17:36:40 +00002325}
2326
Anders Carlsson0781ce72009-04-23 02:32:43 +00002327void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Richard Smithed83ebd2015-02-05 07:02:11 +00002328 if (E->isListInitialization() && !E->isStdInitListInitialization())
Richard Smithd0e102f2015-01-30 02:04:26 +00002329 OS << "{";
Richard Smithd59b8322012-12-19 01:39:02 +00002330
Douglas Gregorbaba85d2011-01-24 17:25:03 +00002331 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
2332 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
2333 // Don't print any defaulted arguments
2334 break;
2335 }
2336
2337 if (i) OS << ", ";
2338 PrintExpr(E->getArg(i));
Fariborz Jahaniand0bbf662010-01-13 21:41:11 +00002339 }
Richard Smithd59b8322012-12-19 01:39:02 +00002340
Richard Smithed83ebd2015-02-05 07:02:11 +00002341 if (E->isListInitialization() && !E->isStdInitListInitialization())
Richard Smithd0e102f2015-01-30 02:04:26 +00002342 OS << "}";
Anders Carlsson0781ce72009-04-23 02:32:43 +00002343}
2344
Richard Smith5179eb72016-06-28 19:03:57 +00002345void StmtPrinter::VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E) {
2346 // Parens are printed by the surrounding context.
2347 OS << "<forwarded>";
2348}
2349
Richard Smithcc1b96d2013-06-12 22:31:48 +00002350void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
2351 PrintExpr(E->getSubExpr());
2352}
2353
John McCall5d413782010-12-06 08:20:24 +00002354void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Alp Toker028ed912013-12-06 17:56:43 +00002355 // Just forward to the subexpression.
Anders Carlssondefc6442009-04-24 22:47:04 +00002356 PrintExpr(E->getSubExpr());
2357}
2358
Mike Stump11289f42009-09-09 15:08:12 +00002359void
Douglas Gregorce934142009-05-20 18:46:25 +00002360StmtPrinter::VisitCXXUnresolvedConstructExpr(
2361 CXXUnresolvedConstructExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002362 Node->getTypeAsWritten().print(OS, Policy);
Douglas Gregorce934142009-05-20 18:46:25 +00002363 OS << "(";
2364 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00002365 ArgEnd = Node->arg_end();
Douglas Gregorce934142009-05-20 18:46:25 +00002366 Arg != ArgEnd; ++Arg) {
2367 if (Arg != Node->arg_begin())
2368 OS << ", ";
2369 PrintExpr(*Arg);
2370 }
2371 OS << ")";
2372}
2373
John McCall8cd78132009-11-19 22:55:06 +00002374void StmtPrinter::VisitCXXDependentScopeMemberExpr(
2375 CXXDependentScopeMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00002376 if (!Node->isImplicitAccess()) {
2377 PrintExpr(Node->getBase());
2378 OS << (Node->isArrow() ? "->" : ".");
2379 }
Douglas Gregorc26e0f62009-09-03 16:14:30 +00002380 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2381 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002382 if (Node->hasTemplateKeyword())
Douglas Gregor308047d2009-09-09 00:23:06 +00002383 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002384 OS << Node->getMemberNameInfo();
Benjamin Kramer9170e912013-02-22 15:46:01 +00002385 if (Node->hasExplicitTemplateArgs())
2386 TemplateSpecializationType::PrintTemplateArgumentList(
David Majnemer6fbeee32016-07-07 04:43:07 +00002387 OS, Node->template_arguments(), Policy);
Douglas Gregora8db9542009-05-22 21:13:27 +00002388}
2389
John McCall10eae182009-11-30 22:42:35 +00002390void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00002391 if (!Node->isImplicitAccess()) {
2392 PrintExpr(Node->getBase());
2393 OS << (Node->isArrow() ? "->" : ".");
2394 }
John McCall10eae182009-11-30 22:42:35 +00002395 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2396 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002397 if (Node->hasTemplateKeyword())
2398 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002399 OS << Node->getMemberNameInfo();
Benjamin Kramer9170e912013-02-22 15:46:01 +00002400 if (Node->hasExplicitTemplateArgs())
2401 TemplateSpecializationType::PrintTemplateArgumentList(
David Majnemer6fbeee32016-07-07 04:43:07 +00002402 OS, Node->template_arguments(), Policy);
John McCall10eae182009-11-30 22:42:35 +00002403}
2404
Douglas Gregor29c42f22012-02-24 07:38:34 +00002405static const char *getTypeTraitName(TypeTrait TT) {
2406 switch (TT) {
Alp Toker95e7ff22014-01-01 05:57:51 +00002407#define TYPE_TRAIT_1(Spelling, Name, Key) \
2408case clang::UTT_##Name: return #Spelling;
Alp Tokercbb90342013-12-13 20:49:58 +00002409#define TYPE_TRAIT_2(Spelling, Name, Key) \
2410case clang::BTT_##Name: return #Spelling;
Alp Toker40f9b1c2013-12-12 21:23:03 +00002411#define TYPE_TRAIT_N(Spelling, Name, Key) \
2412 case clang::TT_##Name: return #Spelling;
2413#include "clang/Basic/TokenKinds.def"
Douglas Gregor29c42f22012-02-24 07:38:34 +00002414 }
2415 llvm_unreachable("Type trait not covered by switch");
2416}
2417
John Wiegley6242b6a2011-04-28 00:16:57 +00002418static const char *getTypeTraitName(ArrayTypeTrait ATT) {
2419 switch (ATT) {
2420 case ATT_ArrayRank: return "__array_rank";
2421 case ATT_ArrayExtent: return "__array_extent";
2422 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00002423 llvm_unreachable("Array type trait not covered by switch");
John Wiegley6242b6a2011-04-28 00:16:57 +00002424}
2425
John Wiegleyf9f65842011-04-25 06:54:41 +00002426static const char *getExpressionTraitName(ExpressionTrait ET) {
2427 switch (ET) {
John Wiegleyf9f65842011-04-25 06:54:41 +00002428 case ET_IsLValueExpr: return "__is_lvalue_expr";
2429 case ET_IsRValueExpr: return "__is_rvalue_expr";
2430 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00002431 llvm_unreachable("Expression type trait not covered by switch");
John Wiegleyf9f65842011-04-25 06:54:41 +00002432}
2433
Douglas Gregor29c42f22012-02-24 07:38:34 +00002434void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
2435 OS << getTypeTraitName(E->getTrait()) << "(";
2436 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
2437 if (I > 0)
2438 OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002439 E->getArg(I)->getType().print(OS, Policy);
Douglas Gregor29c42f22012-02-24 07:38:34 +00002440 }
2441 OS << ")";
2442}
2443
John Wiegley6242b6a2011-04-28 00:16:57 +00002444void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002445 OS << getTypeTraitName(E->getTrait()) << '(';
2446 E->getQueriedType().print(OS, Policy);
2447 OS << ')';
John Wiegley6242b6a2011-04-28 00:16:57 +00002448}
2449
John Wiegleyf9f65842011-04-25 06:54:41 +00002450void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002451 OS << getExpressionTraitName(E->getTrait()) << '(';
2452 PrintExpr(E->getQueriedExpression());
2453 OS << ')';
John Wiegleyf9f65842011-04-25 06:54:41 +00002454}
2455
Sebastian Redl4202c0f2010-09-10 20:55:43 +00002456void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
2457 OS << "noexcept(";
2458 PrintExpr(E->getOperand());
2459 OS << ")";
2460}
2461
Douglas Gregor820ba7b2011-01-04 17:33:58 +00002462void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregore8e9dd62011-01-03 17:17:50 +00002463 PrintExpr(E->getPattern());
2464 OS << "...";
2465}
2466
Douglas Gregor820ba7b2011-01-04 17:33:58 +00002467void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00002468 OS << "sizeof...(" << *E->getPack() << ")";
Douglas Gregor820ba7b2011-01-04 17:33:58 +00002469}
2470
Douglas Gregorcdbc5392011-01-15 01:15:58 +00002471void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
2472 SubstNonTypeTemplateParmPackExpr *Node) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00002473 OS << *Node->getParameterPack();
Douglas Gregorcdbc5392011-01-15 01:15:58 +00002474}
2475
John McCall7c454bb2011-07-15 05:09:51 +00002476void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
2477 SubstNonTypeTemplateParmExpr *Node) {
2478 Visit(Node->getReplacement());
2479}
2480
Richard Smithb15fe3a2012-09-12 00:56:43 +00002481void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
2482 OS << *E->getParameterPack();
2483}
2484
Douglas Gregorfe314812011-06-21 17:03:29 +00002485void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
2486 PrintExpr(Node->GetTemporaryExpr());
2487}
2488
Richard Smith0f0af192014-11-08 05:07:16 +00002489void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
2490 OS << "(";
2491 if (E->getLHS()) {
2492 PrintExpr(E->getLHS());
2493 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2494 }
2495 OS << "...";
2496 if (E->getRHS()) {
2497 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2498 PrintExpr(E->getRHS());
2499 }
2500 OS << ")";
2501}
2502
Richard Smith9f690bd2015-10-27 06:02:45 +00002503// C++ Coroutines TS
2504
2505void StmtPrinter::VisitCoroutineBodyStmt(CoroutineBodyStmt *S) {
2506 Visit(S->getBody());
2507}
2508
2509void StmtPrinter::VisitCoreturnStmt(CoreturnStmt *S) {
2510 OS << "co_return";
2511 if (S->getOperand()) {
2512 OS << " ";
2513 Visit(S->getOperand());
2514 }
2515 OS << ";";
2516}
2517
2518void StmtPrinter::VisitCoawaitExpr(CoawaitExpr *S) {
2519 OS << "co_await ";
2520 PrintExpr(S->getOperand());
2521}
2522
Eric Fiselier20f25cb2017-03-06 23:38:15 +00002523
2524void StmtPrinter::VisitDependentCoawaitExpr(DependentCoawaitExpr *S) {
2525 OS << "co_await ";
2526 PrintExpr(S->getOperand());
2527}
2528
2529
Richard Smith9f690bd2015-10-27 06:02:45 +00002530void StmtPrinter::VisitCoyieldExpr(CoyieldExpr *S) {
2531 OS << "co_yield ";
2532 PrintExpr(S->getOperand());
2533}
2534
Mike Stump11289f42009-09-09 15:08:12 +00002535// Obj-C
Anders Carlsson76f4a902007-08-21 17:43:55 +00002536
2537void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
2538 OS << "@";
2539 VisitStringLiteral(Node->getString());
2540}
Chris Lattnereefa10e2007-05-28 06:56:27 +00002541
Patrick Beard0caa3942012-04-19 00:25:12 +00002542void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00002543 OS << "@";
Patrick Beard0caa3942012-04-19 00:25:12 +00002544 Visit(E->getSubExpr());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002545}
2546
2547void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
2548 OS << "@[ ";
Benjamin Kramer5733e352015-07-18 17:09:36 +00002549 ObjCArrayLiteral::child_range Ch = E->children();
2550 for (auto I = Ch.begin(), E = Ch.end(); I != E; ++I) {
2551 if (I != Ch.begin())
Ted Kremeneke65b0862012-03-06 20:05:56 +00002552 OS << ", ";
Benjamin Kramer5733e352015-07-18 17:09:36 +00002553 Visit(*I);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002554 }
2555 OS << " ]";
2556}
2557
2558void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
2559 OS << "@{ ";
2560 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
2561 if (I > 0)
2562 OS << ", ";
2563
2564 ObjCDictionaryElement Element = E->getKeyValueElement(I);
2565 Visit(Element.Key);
2566 OS << " : ";
2567 Visit(Element.Value);
2568 if (Element.isPackExpansion())
2569 OS << "...";
2570 }
2571 OS << " }";
2572}
2573
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002574void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002575 OS << "@encode(";
2576 Node->getEncodedType().print(OS, Policy);
2577 OS << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002578}
2579
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002580void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00002581 OS << "@selector(";
2582 Node->getSelector().print(OS);
2583 OS << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002584}
2585
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002586void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00002587 OS << "@protocol(" << *Node->getProtocol() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002588}
2589
Steve Naroffd54978b2007-09-18 23:55:05 +00002590void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
2591 OS << "[";
Douglas Gregor9a129192010-04-21 00:45:42 +00002592 switch (Mess->getReceiverKind()) {
2593 case ObjCMessageExpr::Instance:
2594 PrintExpr(Mess->getInstanceReceiver());
2595 break;
2596
2597 case ObjCMessageExpr::Class:
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002598 Mess->getClassReceiver().print(OS, Policy);
Douglas Gregor9a129192010-04-21 00:45:42 +00002599 break;
2600
2601 case ObjCMessageExpr::SuperInstance:
2602 case ObjCMessageExpr::SuperClass:
2603 OS << "Super";
2604 break;
2605 }
2606
Ted Kremeneka06e7122008-05-02 17:32:38 +00002607 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00002608 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00002609 if (selector.isUnarySelector()) {
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00002610 OS << selector.getNameForSlot(0);
Steve Naroffc6814ea2007-10-02 20:01:56 +00002611 } else {
2612 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00002613 if (i < selector.getNumArgs()) {
2614 if (i > 0) OS << ' ';
2615 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00002616 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00002617 else
2618 OS << ":";
2619 }
2620 else OS << ", "; // Handle variadic methods.
Mike Stump11289f42009-09-09 15:08:12 +00002621
Steve Naroffc6814ea2007-10-02 20:01:56 +00002622 PrintExpr(Mess->getArg(i));
2623 }
Steve Naroffd54978b2007-09-18 23:55:05 +00002624 }
2625 OS << "]";
2626}
2627
Ted Kremeneke65b0862012-03-06 20:05:56 +00002628void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
2629 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
2630}
2631
John McCall31168b02011-06-15 23:02:42 +00002632void
2633StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
2634 PrintExpr(E->getSubExpr());
2635}
2636
2637void
2638StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002639 OS << '(' << E->getBridgeKindName();
2640 E->getType().print(OS, Policy);
2641 OS << ')';
John McCall31168b02011-06-15 23:02:42 +00002642 PrintExpr(E->getSubExpr());
2643}
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002644
Steve Naroffc540d662008-09-03 18:15:37 +00002645void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00002646 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00002647 OS << "^";
Mike Stump11289f42009-09-09 15:08:12 +00002648
Steve Naroffc540d662008-09-03 18:15:37 +00002649 const FunctionType *AFT = Node->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00002650
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002651 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroffc540d662008-09-03 18:15:37 +00002652 OS << "()";
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002653 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00002654 OS << '(';
Steve Naroff415d3d52008-10-08 17:01:13 +00002655 for (BlockDecl::param_iterator AI = BD->param_begin(),
2656 E = BD->param_end(); AI != E; ++AI) {
2657 if (AI != BD->param_begin()) OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002658 std::string ParamStr = (*AI)->getNameAsString();
2659 (*AI)->getType().print(OS, Policy, ParamStr);
Steve Naroffc540d662008-09-03 18:15:37 +00002660 }
Mike Stump11289f42009-09-09 15:08:12 +00002661
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002662 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00002663 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00002664 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00002665 OS << "...";
2666 }
2667 OS << ')';
2668 }
Fariborz Jahanian1ace8cb2012-12-04 21:15:23 +00002669 OS << "{ }";
Steve Naroffc540d662008-09-03 18:15:37 +00002670}
2671
Ted Kremenekf551f322011-11-30 22:08:08 +00002672void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
2673 PrintExpr(Node->getSourceExpr());
2674}
John McCall8d69a212010-11-15 23:31:06 +00002675
Kaelyn Takatae1f49d52014-10-27 18:07:20 +00002676void StmtPrinter::VisitTypoExpr(TypoExpr *Node) {
2677 // TODO: Print something reasonable for a TypoExpr, if necessary.
Davide Italiano04839a52016-01-30 08:03:54 +00002678 llvm_unreachable("Cannot print TypoExpr nodes");
Kaelyn Takatae1f49d52014-10-27 18:07:20 +00002679}
2680
Tanya Lattner55808c12011-06-04 00:47:47 +00002681void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
2682 OS << "__builtin_astype(";
2683 PrintExpr(Node->getSrcExpr());
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002684 OS << ", ";
2685 Node->getType().print(OS, Policy);
Tanya Lattner55808c12011-06-04 00:47:47 +00002686 OS << ")";
2687}
2688
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00002689//===----------------------------------------------------------------------===//
2690// Stmt method implementations
2691//===----------------------------------------------------------------------===//
2692
Craig Topperc571c812013-08-22 06:02:26 +00002693void Stmt::dumpPretty(const ASTContext &Context) const {
Craig Topper36250ad2014-05-12 05:36:57 +00002694 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00002695}
2696
Richard Smith235341b2012-08-16 03:56:14 +00002697void Stmt::printPretty(raw_ostream &OS,
2698 PrinterHelper *Helper,
Douglas Gregor7de59662009-05-29 20:38:28 +00002699 const PrintingPolicy &Policy,
2700 unsigned Indentation) const {
Richard Smith235341b2012-08-16 03:56:14 +00002701 StmtPrinter P(OS, Helper, Policy, Indentation);
Chris Lattner62249a62007-08-21 04:04:25 +00002702 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00002703}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002704
2705//===----------------------------------------------------------------------===//
2706// PrinterHelper
2707//===----------------------------------------------------------------------===//
2708
2709// Implement virtual destructor.
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +00002710PrinterHelper::~PrinterHelper() {}