blob: d7e668a832809cabc86d10c4bc473ca7c1b2f86d [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"
Alex Lorenz36070ed2017-08-17 13:41:55 +000027#include "clang/Lex/Lexer.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000028#include "llvm/ADT/SmallString.h"
Jordan Rose00d1b592013-02-08 22:30:27 +000029#include "llvm/Support/Format.h"
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000030using namespace clang;
31
32//===----------------------------------------------------------------------===//
33// StmtPrinter Visitor
34//===----------------------------------------------------------------------===//
35
36namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +000037 class StmtPrinter : public StmtVisitor<StmtPrinter> {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000038 raw_ostream &OS;
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000039 unsigned IndentLevel;
Ted Kremenek04f3cee2007-08-31 21:30:12 +000040 clang::PrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +000041 PrintingPolicy Policy;
Alex Lorenz36070ed2017-08-17 13:41:55 +000042 const ASTContext *Context;
Douglas Gregor7de59662009-05-29 20:38:28 +000043
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000044 public:
Alex Lorenz36070ed2017-08-17 13:41:55 +000045 StmtPrinter(raw_ostream &os, PrinterHelper *helper,
46 const PrintingPolicy &Policy, unsigned Indentation = 0,
47 const ASTContext *Context = nullptr)
48 : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy),
49 Context(Context) {}
Mike Stump11289f42009-09-09 15:08:12 +000050
Douglas Gregor7de59662009-05-29 20:38:28 +000051 void PrintStmt(Stmt *S) {
52 PrintStmt(S, Policy.Indentation);
53 }
54
55 void PrintStmt(Stmt *S, int SubIndent) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000056 IndentLevel += SubIndent;
Chris Lattnera076fde2007-05-31 18:21:33 +000057 if (S && isa<Expr>(S)) {
Chris Lattner882f7882006-11-04 18:52:07 +000058 // If this is an expr used in a stmt context, indent and newline it.
59 Indent();
Chris Lattner62249a62007-08-21 04:04:25 +000060 Visit(S);
Chris Lattnerfc068c12007-05-30 17:57:36 +000061 OS << ";\n";
Chris Lattner882f7882006-11-04 18:52:07 +000062 } else if (S) {
Chris Lattner62249a62007-08-21 04:04:25 +000063 Visit(S);
Chris Lattner882f7882006-11-04 18:52:07 +000064 } else {
Chris Lattner2f6ac262007-05-28 01:47:47 +000065 Indent() << "<<<NULL STATEMENT>>>\n";
Chris Lattner882f7882006-11-04 18:52:07 +000066 }
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000067 IndentLevel -= SubIndent;
Chris Lattner882f7882006-11-04 18:52:07 +000068 }
Eli Friedman15ea8802009-05-30 00:19:54 +000069
Chris Lattner073926e2007-05-20 23:04:55 +000070 void PrintRawCompoundStmt(CompoundStmt *S);
Chris Lattnerfdc195a2007-06-05 20:52:47 +000071 void PrintRawDecl(Decl *D);
Eli Friedmanf86e5072012-10-16 23:45:15 +000072 void PrintRawDeclStmt(const DeclStmt *S);
Chris Lattnerc0a38dd2007-06-11 22:26:23 +000073 void PrintRawIfStmt(IfStmt *If);
Sebastian Redl9b244a82008-12-22 21:35:02 +000074 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
Peter Collingbourne4b279a02011-02-08 21:17:54 +000075 void PrintCallArgs(CallExpr *E);
John Wiegley1c0675e2011-04-28 01:08:34 +000076 void PrintRawSEHExceptHandler(SEHExceptStmt *S);
77 void PrintRawSEHFinallyStmt(SEHFinallyStmt *S);
Alexey Bataev7828b252017-11-21 17:08:48 +000078 void PrintOMPExecutableDirective(OMPExecutableDirective *S,
79 bool ForceNoStmt = false);
Mike Stump11289f42009-09-09 15:08:12 +000080
Chris Lattner882f7882006-11-04 18:52:07 +000081 void PrintExpr(Expr *E) {
82 if (E)
Chris Lattner62249a62007-08-21 04:04:25 +000083 Visit(E);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000084 else
Chris Lattner882f7882006-11-04 18:52:07 +000085 OS << "<null expr>";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000086 }
Mike Stump11289f42009-09-09 15:08:12 +000087
Chris Lattner0e62c1c2011-07-23 10:55:15 +000088 raw_ostream &Indent(int Delta = 0) {
Douglas Gregor7de59662009-05-29 20:38:28 +000089 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
90 OS << " ";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000091 return OS;
92 }
Mike Stump11289f42009-09-09 15:08:12 +000093
Mike Stump11289f42009-09-09 15:08:12 +000094 void Visit(Stmt* S) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +000095 if (Helper && Helper->handledStmt(S,OS))
96 return;
97 else StmtVisitor<StmtPrinter>::Visit(S);
98 }
Alexey Bataev1b59ab52014-02-27 08:29:12 +000099
Chandler Carruthb7967b92010-10-23 08:21:37 +0000100 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +0000101 Indent() << "<<unknown stmt type>>\n";
102 }
Chandler Carruthb7967b92010-10-23 08:21:37 +0000103 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +0000104 OS << "<<unknown expr type>>";
105 }
106 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Mike Stump11289f42009-09-09 15:08:12 +0000107
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +0000108#define ABSTRACT_STMT(CLASS)
Douglas Gregorbe35ce92008-11-14 12:46:07 +0000109#define STMT(CLASS, PARENT) \
Chris Lattner62249a62007-08-21 04:04:25 +0000110 void Visit##CLASS(CLASS *Node);
Alexis Hunt656bb312010-05-05 15:24:00 +0000111#include "clang/AST/StmtNodes.inc"
Chris Lattner71e23ce2006-11-04 20:18:38 +0000112 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000113}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000114
Chris Lattner71e23ce2006-11-04 20:18:38 +0000115//===----------------------------------------------------------------------===//
116// Stmt printing methods.
117//===----------------------------------------------------------------------===//
118
Chris Lattner073926e2007-05-20 23:04:55 +0000119/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
120/// with no newline after the }.
121void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
122 OS << "{\n";
Aaron Ballmanc7e4e212014-03-17 14:19:37 +0000123 for (auto *I : Node->body())
124 PrintStmt(I);
Mike Stump11289f42009-09-09 15:08:12 +0000125
Chris Lattner073926e2007-05-20 23:04:55 +0000126 Indent() << "}";
127}
128
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000129void StmtPrinter::PrintRawDecl(Decl *D) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000130 D->print(OS, Policy, IndentLevel);
Mike Stump74a76472009-02-10 20:16:46 +0000131}
132
Eli Friedmanf86e5072012-10-16 23:45:15 +0000133void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
Aaron Ballman535bbcc2014-03-14 17:01:24 +0000134 SmallVector<Decl*, 2> Decls(S->decls());
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000135 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
Ted Kremenek15e6b402008-10-06 18:39:36 +0000136}
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000137
138void StmtPrinter::VisitNullStmt(NullStmt *Node) {
139 Indent() << ";\n";
140}
141
142void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Eli Friedman15ea8802009-05-30 00:19:54 +0000143 Indent();
144 PrintRawDeclStmt(Node);
145 OS << ";\n";
Steve Naroff2a8ad182007-05-29 22:59:26 +0000146}
147
Chris Lattner073926e2007-05-20 23:04:55 +0000148void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
149 Indent();
150 PrintRawCompoundStmt(Node);
Chris Lattnerdf3cafb2007-05-31 05:08:56 +0000151 OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000152}
153
Chris Lattner6c0ff132006-11-05 00:19:50 +0000154void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000155 Indent(-1) << "case ";
Chris Lattner6c0ff132006-11-05 00:19:50 +0000156 PrintExpr(Node->getLHS());
157 if (Node->getRHS()) {
158 OS << " ... ";
159 PrintExpr(Node->getRHS());
160 }
161 OS << ":\n";
Mike Stump11289f42009-09-09 15:08:12 +0000162
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000163 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000164}
165
166void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000167 Indent(-1) << "default:\n";
168 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000169}
170
171void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000172 Indent(-1) << Node->getName() << ":\n";
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000173 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000174}
175
Richard Smithc202b282012-04-14 00:33:13 +0000176void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
Aaron Ballmanb06b15a2014-06-06 12:40:24 +0000177 for (const auto *Attr : Node->getAttrs()) {
Tyler Nowickie8b07ed2014-06-13 17:57:25 +0000178 Attr->printPretty(OS, Policy);
Aaron Ballmanb06b15a2014-06-06 12:40:24 +0000179 }
180
Richard Smithc202b282012-04-14 00:33:13 +0000181 PrintStmt(Node->getSubStmt(), 0);
182}
183
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000184void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000185 OS << "if (";
Eli Friedmanf86e5072012-10-16 23:45:15 +0000186 if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
187 PrintRawDeclStmt(DS);
188 else
189 PrintExpr(If->getCond());
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000190 OS << ')';
Mike Stump11289f42009-09-09 15:08:12 +0000191
Chris Lattner073926e2007-05-20 23:04:55 +0000192 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
193 OS << ' ';
194 PrintRawCompoundStmt(CS);
195 OS << (If->getElse() ? ' ' : '\n');
196 } else {
197 OS << '\n';
198 PrintStmt(If->getThen());
199 if (If->getElse()) Indent();
200 }
Mike Stump11289f42009-09-09 15:08:12 +0000201
Chris Lattner073926e2007-05-20 23:04:55 +0000202 if (Stmt *Else = If->getElse()) {
203 OS << "else";
Mike Stump11289f42009-09-09 15:08:12 +0000204
Chris Lattner073926e2007-05-20 23:04:55 +0000205 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
206 OS << ' ';
207 PrintRawCompoundStmt(CS);
208 OS << '\n';
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000209 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
210 OS << ' ';
211 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000212 } else {
213 OS << '\n';
214 PrintStmt(If->getElse());
215 }
Chris Lattner882f7882006-11-04 18:52:07 +0000216 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000217}
218
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000219void StmtPrinter::VisitIfStmt(IfStmt *If) {
220 Indent();
221 PrintRawIfStmt(If);
222}
223
Chris Lattnerf2174b62006-11-04 20:59:27 +0000224void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
225 Indent() << "switch (";
Eli Friedmanf86e5072012-10-16 23:45:15 +0000226 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
227 PrintRawDeclStmt(DS);
228 else
229 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000230 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000231
Chris Lattner073926e2007-05-20 23:04:55 +0000232 // Pretty print compoundstmt bodies (very common).
233 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
234 OS << " ";
235 PrintRawCompoundStmt(CS);
236 OS << "\n";
237 } else {
238 OS << "\n";
239 PrintStmt(Node->getBody());
240 }
Chris Lattnerf2174b62006-11-04 20:59:27 +0000241}
242
Chris Lattner85ed8732006-11-04 20:40:44 +0000243void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
244 Indent() << "while (";
Eli Friedmanf86e5072012-10-16 23:45:15 +0000245 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
246 PrintRawDeclStmt(DS);
247 else
248 PrintExpr(Node->getCond());
Chris Lattner85ed8732006-11-04 20:40:44 +0000249 OS << ")\n";
250 PrintStmt(Node->getBody());
251}
252
253void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000254 Indent() << "do ";
255 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
256 PrintRawCompoundStmt(CS);
257 OS << " ";
258 } else {
259 OS << "\n";
260 PrintStmt(Node->getBody());
261 Indent();
262 }
Mike Stump11289f42009-09-09 15:08:12 +0000263
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000264 OS << "while (";
Chris Lattner85ed8732006-11-04 20:40:44 +0000265 PrintExpr(Node->getCond());
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000266 OS << ");\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000267}
268
Chris Lattner71e23ce2006-11-04 20:18:38 +0000269void StmtPrinter::VisitForStmt(ForStmt *Node) {
270 Indent() << "for (";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000271 if (Node->getInit()) {
272 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000273 PrintRawDeclStmt(DS);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000274 else
275 PrintExpr(cast<Expr>(Node->getInit()));
276 }
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000277 OS << ";";
278 if (Node->getCond()) {
279 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000280 PrintExpr(Node->getCond());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000281 }
282 OS << ";";
283 if (Node->getInc()) {
284 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000285 PrintExpr(Node->getInc());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000286 }
287 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000288
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000289 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
290 PrintRawCompoundStmt(CS);
291 OS << "\n";
292 } else {
293 OS << "\n";
294 PrintStmt(Node->getBody());
295 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000296}
297
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000298void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian83615522008-01-02 22:54:34 +0000299 Indent() << "for (";
300 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000301 PrintRawDeclStmt(DS);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000302 else
303 PrintExpr(cast<Expr>(Node->getElement()));
304 OS << " in ";
305 PrintExpr(Node->getCollection());
306 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000307
Fariborz Jahanian83615522008-01-02 22:54:34 +0000308 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
309 PrintRawCompoundStmt(CS);
310 OS << "\n";
311 } else {
312 OS << "\n";
313 PrintStmt(Node->getBody());
314 }
315}
316
Richard Smith02e85f32011-04-14 22:09:26 +0000317void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
318 Indent() << "for (";
319 PrintingPolicy SubPolicy(Policy);
320 SubPolicy.SuppressInitializers = true;
321 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
322 OS << " : ";
323 PrintExpr(Node->getRangeInit());
324 OS << ") {\n";
325 PrintStmt(Node->getBody());
Ted Kremenek88446602013-12-11 23:44:02 +0000326 Indent() << "}";
327 if (Policy.IncludeNewlines) OS << "\n";
Richard Smith02e85f32011-04-14 22:09:26 +0000328}
329
Douglas Gregordeb4a2be2011-10-25 01:33:02 +0000330void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
331 Indent();
332 if (Node->isIfExists())
333 OS << "__if_exists (";
334 else
335 OS << "__if_not_exists (";
336
337 if (NestedNameSpecifier *Qualifier
338 = Node->getQualifierLoc().getNestedNameSpecifier())
339 Qualifier->print(OS, Policy);
340
341 OS << Node->getNameInfo() << ") ";
342
343 PrintRawCompoundStmt(Node->getSubStmt());
344}
345
Chris Lattner16976d32006-11-05 01:46:01 +0000346void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000347 Indent() << "goto " << Node->getLabel()->getName() << ";";
348 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000349}
350
351void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000352 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000353 PrintExpr(Node->getTarget());
Ted Kremenek88446602013-12-11 23:44:02 +0000354 OS << ";";
355 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000356}
357
358void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000359 Indent() << "continue;";
360 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000361}
362
363void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000364 Indent() << "break;";
365 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000366}
367
368
Chris Lattner882f7882006-11-04 18:52:07 +0000369void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
370 Indent() << "return";
371 if (Node->getRetValue()) {
372 OS << " ";
373 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000374 }
Ted Kremenek88446602013-12-11 23:44:02 +0000375 OS << ";";
376 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000377}
378
Chris Lattner73c56c02007-10-29 04:04:16 +0000379
Chad Rosierde70e0e2012-08-25 00:11:56 +0000380void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
Anders Carlsson660bdd12007-11-23 23:12:25 +0000381 Indent() << "asm ";
Mike Stump11289f42009-09-09 15:08:12 +0000382
Anders Carlsson660bdd12007-11-23 23:12:25 +0000383 if (Node->isVolatile())
384 OS << "volatile ";
Mike Stump11289f42009-09-09 15:08:12 +0000385
Anders Carlsson660bdd12007-11-23 23:12:25 +0000386 OS << "(";
Anders Carlsson81a5a692007-11-20 19:21:03 +0000387 VisitStringLiteral(Node->getAsmString());
Mike Stump11289f42009-09-09 15:08:12 +0000388
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000389 // Outputs
390 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
391 Node->getNumClobbers() != 0)
392 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000393
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000394 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
395 if (i != 0)
396 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000397
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000398 if (!Node->getOutputName(i).empty()) {
399 OS << '[';
400 OS << Node->getOutputName(i);
401 OS << "] ";
402 }
Mike Stump11289f42009-09-09 15:08:12 +0000403
Chris Lattner72bbf172009-03-10 04:59:06 +0000404 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Jonathan Roelofs78f9e032015-06-09 14:13:31 +0000405 OS << " (";
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000406 Visit(Node->getOutputExpr(i));
Jonathan Roelofs78f9e032015-06-09 14:13:31 +0000407 OS << ")";
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000408 }
Mike Stump11289f42009-09-09 15:08:12 +0000409
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000410 // Inputs
411 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
412 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000413
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000414 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
415 if (i != 0)
416 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000417
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000418 if (!Node->getInputName(i).empty()) {
419 OS << '[';
420 OS << Node->getInputName(i);
421 OS << "] ";
422 }
Mike Stump11289f42009-09-09 15:08:12 +0000423
Chris Lattner72bbf172009-03-10 04:59:06 +0000424 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Jonathan Roelofs78f9e032015-06-09 14:13:31 +0000425 OS << " (";
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000426 Visit(Node->getInputExpr(i));
Jonathan Roelofs78f9e032015-06-09 14:13:31 +0000427 OS << ")";
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000428 }
Mike Stump11289f42009-09-09 15:08:12 +0000429
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000430 // Clobbers
431 if (Node->getNumClobbers() != 0)
432 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000433
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000434 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
435 if (i != 0)
436 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000437
Chad Rosierd9fb09a2012-08-27 23:28:41 +0000438 VisitStringLiteral(Node->getClobberStringLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000439 }
Mike Stump11289f42009-09-09 15:08:12 +0000440
Ted Kremenek88446602013-12-11 23:44:02 +0000441 OS << ");";
442 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner73c56c02007-10-29 04:04:16 +0000443}
444
Chad Rosier32503022012-06-11 20:47:18 +0000445void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
446 // FIXME: Implement MS style inline asm statement printer.
Chad Rosierb6f46c12012-08-15 16:53:30 +0000447 Indent() << "__asm ";
448 if (Node->hasBraces())
449 OS << "{\n";
John McCallf413f5e2013-05-03 00:10:13 +0000450 OS << Node->getAsmString() << "\n";
Chad Rosierb6f46c12012-08-15 16:53:30 +0000451 if (Node->hasBraces())
452 Indent() << "}\n";
Chad Rosier32503022012-06-11 20:47:18 +0000453}
454
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000455void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000456 PrintStmt(Node->getCapturedDecl()->getBody());
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000457}
458
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000459void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000460 Indent() << "@try";
461 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
462 PrintRawCompoundStmt(TS);
463 OS << "\n";
464 }
Mike Stump11289f42009-09-09 15:08:12 +0000465
Douglas Gregor96c79492010-04-23 22:50:49 +0000466 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
467 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000468 Indent() << "@catch(";
Steve Naroff371b8fb2009-03-03 19:52:17 +0000469 if (catchStmt->getCatchParamDecl()) {
470 if (Decl *DS = catchStmt->getCatchParamDecl())
471 PrintRawDecl(DS);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000472 }
473 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000474 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
475 PrintRawCompoundStmt(CS);
476 OS << "\n";
477 }
Fariborz Jahanian88157952007-11-02 18:16:07 +0000478 }
Mike Stump11289f42009-09-09 15:08:12 +0000479
480 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
481 Node->getFinallyStmt())) {
Fariborz Jahaniandefbf9a2007-11-07 00:46:42 +0000482 Indent() << "@finally";
483 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian88157952007-11-02 18:16:07 +0000484 OS << "\n";
Mike Stump11289f42009-09-09 15:08:12 +0000485 }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000486}
487
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000488void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000489}
490
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000491void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000492 Indent() << "@catch (...) { /* todo */ } \n";
493}
494
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000495void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000496 Indent() << "@throw";
497 if (Node->getThrowExpr()) {
498 OS << " ";
499 PrintExpr(Node->getThrowExpr());
500 }
501 OS << ";\n";
502}
503
Erik Pilkington29099de2016-07-16 00:35:23 +0000504void StmtPrinter::VisitObjCAvailabilityCheckExpr(
505 ObjCAvailabilityCheckExpr *Node) {
506 OS << "@available(...)";
507}
508
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000509void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000510 Indent() << "@synchronized (";
511 PrintExpr(Node->getSynchExpr());
512 OS << ")";
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000513 PrintRawCompoundStmt(Node->getSynchBody());
514 OS << "\n";
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000515}
516
John McCall31168b02011-06-15 23:02:42 +0000517void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
518 Indent() << "@autoreleasepool";
519 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
520 OS << "\n";
521}
522
Sebastian Redl9b244a82008-12-22 21:35:02 +0000523void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
524 OS << "catch (";
Sebastian Redl54c04d42008-12-22 19:15:10 +0000525 if (Decl *ExDecl = Node->getExceptionDecl())
526 PrintRawDecl(ExDecl);
527 else
528 OS << "...";
529 OS << ") ";
530 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl9b244a82008-12-22 21:35:02 +0000531}
532
533void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
534 Indent();
535 PrintRawCXXCatchStmt(Node);
536 OS << "\n";
537}
538
539void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
540 Indent() << "try ";
541 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump11289f42009-09-09 15:08:12 +0000542 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl9b244a82008-12-22 21:35:02 +0000543 OS << " ";
544 PrintRawCXXCatchStmt(Node->getHandler(i));
545 }
Sebastian Redl54c04d42008-12-22 19:15:10 +0000546 OS << "\n";
547}
548
John Wiegley1c0675e2011-04-28 01:08:34 +0000549void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
550 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
551 PrintRawCompoundStmt(Node->getTryBlock());
552 SEHExceptStmt *E = Node->getExceptHandler();
553 SEHFinallyStmt *F = Node->getFinallyHandler();
554 if(E)
555 PrintRawSEHExceptHandler(E);
556 else {
557 assert(F && "Must have a finally block...");
558 PrintRawSEHFinallyStmt(F);
559 }
560 OS << "\n";
561}
562
563void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
564 OS << "__finally ";
565 PrintRawCompoundStmt(Node->getBlock());
566 OS << "\n";
567}
568
569void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
570 OS << "__except (";
571 VisitExpr(Node->getFilterExpr());
Joao Matos566359c2012-09-04 17:49:35 +0000572 OS << ")\n";
John Wiegley1c0675e2011-04-28 01:08:34 +0000573 PrintRawCompoundStmt(Node->getBlock());
574 OS << "\n";
575}
576
577void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
578 Indent();
579 PrintRawSEHExceptHandler(Node);
580 OS << "\n";
581}
582
583void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
584 Indent();
585 PrintRawSEHFinallyStmt(Node);
586 OS << "\n";
587}
588
Nico Weber9b982072014-07-07 00:12:30 +0000589void StmtPrinter::VisitSEHLeaveStmt(SEHLeaveStmt *Node) {
590 Indent() << "__leave;";
591 if (Policy.IncludeNewlines) OS << "\n";
592}
593
Chris Lattner71e23ce2006-11-04 20:18:38 +0000594//===----------------------------------------------------------------------===//
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000595// OpenMP clauses printing methods
596//===----------------------------------------------------------------------===//
597
598namespace {
599class OMPClausePrinter : public OMPClauseVisitor<OMPClausePrinter> {
600 raw_ostream &OS;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000601 const PrintingPolicy &Policy;
Alexey Bataev756c1962013-09-24 03:17:45 +0000602 /// \brief Process clauses with list of variables.
603 template <typename T>
604 void VisitOMPClauseList(T *Node, char StartSym);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000605public:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000606 OMPClausePrinter(raw_ostream &OS, const PrintingPolicy &Policy)
607 : OS(OS), Policy(Policy) { }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000608#define OPENMP_CLAUSE(Name, Class) \
609 void Visit##Class(Class *S);
610#include "clang/Basic/OpenMPKinds.def"
611};
612
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000613void OMPClausePrinter::VisitOMPIfClause(OMPIfClause *Node) {
614 OS << "if(";
Alexey Bataev6b8046a2015-09-03 07:23:48 +0000615 if (Node->getNameModifier() != OMPD_unknown)
616 OS << getOpenMPDirectiveName(Node->getNameModifier()) << ": ";
Craig Topper36250ad2014-05-12 05:36:57 +0000617 Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000618 OS << ")";
619}
620
Alexey Bataev3778b602014-07-17 07:32:53 +0000621void OMPClausePrinter::VisitOMPFinalClause(OMPFinalClause *Node) {
622 OS << "final(";
623 Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
624 OS << ")";
625}
626
Alexey Bataev568a8332014-03-06 06:15:19 +0000627void OMPClausePrinter::VisitOMPNumThreadsClause(OMPNumThreadsClause *Node) {
628 OS << "num_threads(";
Craig Topper36250ad2014-05-12 05:36:57 +0000629 Node->getNumThreads()->printPretty(OS, nullptr, Policy, 0);
Alexey Bataev568a8332014-03-06 06:15:19 +0000630 OS << ")";
631}
632
Alexey Bataev62c87d22014-03-21 04:51:18 +0000633void OMPClausePrinter::VisitOMPSafelenClause(OMPSafelenClause *Node) {
634 OS << "safelen(";
Craig Topper36250ad2014-05-12 05:36:57 +0000635 Node->getSafelen()->printPretty(OS, nullptr, Policy, 0);
Alexey Bataev62c87d22014-03-21 04:51:18 +0000636 OS << ")";
637}
638
Alexey Bataev66b15b52015-08-21 11:14:16 +0000639void OMPClausePrinter::VisitOMPSimdlenClause(OMPSimdlenClause *Node) {
640 OS << "simdlen(";
641 Node->getSimdlen()->printPretty(OS, nullptr, Policy, 0);
642 OS << ")";
643}
644
Alexander Musman8bd31e62014-05-27 15:12:19 +0000645void OMPClausePrinter::VisitOMPCollapseClause(OMPCollapseClause *Node) {
646 OS << "collapse(";
647 Node->getNumForLoops()->printPretty(OS, nullptr, Policy, 0);
648 OS << ")";
649}
650
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000651void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
652 OS << "default("
653 << getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind())
654 << ")";
655}
656
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000657void OMPClausePrinter::VisitOMPProcBindClause(OMPProcBindClause *Node) {
658 OS << "proc_bind("
659 << getOpenMPSimpleClauseTypeName(OMPC_proc_bind, Node->getProcBindKind())
660 << ")";
661}
662
Alexey Bataev56dafe82014-06-20 07:16:17 +0000663void OMPClausePrinter::VisitOMPScheduleClause(OMPScheduleClause *Node) {
Alexey Bataev6402bca2015-12-28 07:25:51 +0000664 OS << "schedule(";
665 if (Node->getFirstScheduleModifier() != OMPC_SCHEDULE_MODIFIER_unknown) {
666 OS << getOpenMPSimpleClauseTypeName(OMPC_schedule,
667 Node->getFirstScheduleModifier());
668 if (Node->getSecondScheduleModifier() != OMPC_SCHEDULE_MODIFIER_unknown) {
669 OS << ", ";
670 OS << getOpenMPSimpleClauseTypeName(OMPC_schedule,
671 Node->getSecondScheduleModifier());
672 }
673 OS << ": ";
674 }
675 OS << getOpenMPSimpleClauseTypeName(OMPC_schedule, Node->getScheduleKind());
Alexey Bataev3392d762016-02-16 11:18:12 +0000676 if (auto *E = Node->getChunkSize()) {
Alexey Bataev56dafe82014-06-20 07:16:17 +0000677 OS << ", ";
Alexey Bataev60da77e2016-02-29 05:54:20 +0000678 E->printPretty(OS, nullptr, Policy);
Alexey Bataev56dafe82014-06-20 07:16:17 +0000679 }
680 OS << ")";
681}
682
Alexey Bataev10e775f2015-07-30 11:36:16 +0000683void OMPClausePrinter::VisitOMPOrderedClause(OMPOrderedClause *Node) {
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000684 OS << "ordered";
Alexey Bataev10e775f2015-07-30 11:36:16 +0000685 if (auto *Num = Node->getNumForLoops()) {
686 OS << "(";
687 Num->printPretty(OS, nullptr, Policy, 0);
688 OS << ")";
689 }
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000690}
691
Alexey Bataev236070f2014-06-20 11:19:47 +0000692void OMPClausePrinter::VisitOMPNowaitClause(OMPNowaitClause *) {
693 OS << "nowait";
694}
695
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000696void OMPClausePrinter::VisitOMPUntiedClause(OMPUntiedClause *) {
697 OS << "untied";
698}
699
Alexey Bataevb825de12015-12-07 10:51:44 +0000700void OMPClausePrinter::VisitOMPNogroupClause(OMPNogroupClause *) {
701 OS << "nogroup";
702}
703
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000704void OMPClausePrinter::VisitOMPMergeableClause(OMPMergeableClause *) {
705 OS << "mergeable";
706}
707
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000708void OMPClausePrinter::VisitOMPReadClause(OMPReadClause *) { OS << "read"; }
709
Alexey Bataevdea47612014-07-23 07:46:59 +0000710void OMPClausePrinter::VisitOMPWriteClause(OMPWriteClause *) { OS << "write"; }
711
Alexey Bataev67a4f222014-07-23 10:25:33 +0000712void OMPClausePrinter::VisitOMPUpdateClause(OMPUpdateClause *) {
713 OS << "update";
714}
715
Alexey Bataev459dec02014-07-24 06:46:57 +0000716void OMPClausePrinter::VisitOMPCaptureClause(OMPCaptureClause *) {
717 OS << "capture";
718}
719
Alexey Bataev82bad8b2014-07-24 08:55:34 +0000720void OMPClausePrinter::VisitOMPSeqCstClause(OMPSeqCstClause *) {
721 OS << "seq_cst";
722}
723
Alexey Bataev346265e2015-09-25 10:37:12 +0000724void OMPClausePrinter::VisitOMPThreadsClause(OMPThreadsClause *) {
725 OS << "threads";
726}
727
Alexey Bataevd14d1e62015-09-28 06:39:35 +0000728void OMPClausePrinter::VisitOMPSIMDClause(OMPSIMDClause *) { OS << "simd"; }
729
Michael Wonge710d542015-08-07 16:16:36 +0000730void OMPClausePrinter::VisitOMPDeviceClause(OMPDeviceClause *Node) {
731 OS << "device(";
732 Node->getDevice()->printPretty(OS, nullptr, Policy, 0);
733 OS << ")";
734}
735
Kelvin Li099bb8c2015-11-24 20:50:12 +0000736void OMPClausePrinter::VisitOMPNumTeamsClause(OMPNumTeamsClause *Node) {
737 OS << "num_teams(";
738 Node->getNumTeams()->printPretty(OS, nullptr, Policy, 0);
739 OS << ")";
740}
741
Kelvin Lia15fb1a2015-11-27 18:47:36 +0000742void OMPClausePrinter::VisitOMPThreadLimitClause(OMPThreadLimitClause *Node) {
743 OS << "thread_limit(";
744 Node->getThreadLimit()->printPretty(OS, nullptr, Policy, 0);
745 OS << ")";
746}
747
Alexey Bataeva0569352015-12-01 10:17:31 +0000748void OMPClausePrinter::VisitOMPPriorityClause(OMPPriorityClause *Node) {
749 OS << "priority(";
750 Node->getPriority()->printPretty(OS, nullptr, Policy, 0);
751 OS << ")";
752}
753
Alexey Bataev1fd4aed2015-12-07 12:52:51 +0000754void OMPClausePrinter::VisitOMPGrainsizeClause(OMPGrainsizeClause *Node) {
755 OS << "grainsize(";
756 Node->getGrainsize()->printPretty(OS, nullptr, Policy, 0);
757 OS << ")";
758}
759
Alexey Bataev382967a2015-12-08 12:06:20 +0000760void OMPClausePrinter::VisitOMPNumTasksClause(OMPNumTasksClause *Node) {
761 OS << "num_tasks(";
762 Node->getNumTasks()->printPretty(OS, nullptr, Policy, 0);
763 OS << ")";
764}
765
Alexey Bataev28c75412015-12-15 08:19:24 +0000766void OMPClausePrinter::VisitOMPHintClause(OMPHintClause *Node) {
767 OS << "hint(";
768 Node->getHint()->printPretty(OS, nullptr, Policy, 0);
769 OS << ")";
770}
771
Alexey Bataev756c1962013-09-24 03:17:45 +0000772template<typename T>
773void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
774 for (typename T::varlist_iterator I = Node->varlist_begin(),
775 E = Node->varlist_end();
Alexey Bataev90c228f2016-02-08 09:29:13 +0000776 I != E; ++I) {
Richard Trieuddd01ce2014-06-09 22:53:25 +0000777 assert(*I && "Expected non-null Stmt");
Alexey Bataev90c228f2016-02-08 09:29:13 +0000778 OS << (I == Node->varlist_begin() ? StartSym : ',');
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000779 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(*I)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +0000780 if (isa<OMPCapturedExprDecl>(DRE->getDecl()))
781 DRE->printPretty(OS, nullptr, Policy, 0);
Alexey Bataev90c228f2016-02-08 09:29:13 +0000782 else
783 DRE->getDecl()->printQualifiedName(OS);
784 } else
Craig Topper36250ad2014-05-12 05:36:57 +0000785 (*I)->printPretty(OS, nullptr, Policy, 0);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000786 }
Alexey Bataev756c1962013-09-24 03:17:45 +0000787}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000788
789void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) {
790 if (!Node->varlist_empty()) {
791 OS << "private";
Alexey Bataev756c1962013-09-24 03:17:45 +0000792 VisitOMPClauseList(Node, '(');
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000793 OS << ")";
794 }
795}
796
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000797void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) {
798 if (!Node->varlist_empty()) {
799 OS << "firstprivate";
800 VisitOMPClauseList(Node, '(');
801 OS << ")";
802 }
803}
804
Alexander Musman1bb328c2014-06-04 13:06:39 +0000805void OMPClausePrinter::VisitOMPLastprivateClause(OMPLastprivateClause *Node) {
806 if (!Node->varlist_empty()) {
807 OS << "lastprivate";
808 VisitOMPClauseList(Node, '(');
809 OS << ")";
810 }
811}
812
Alexey Bataev758e55e2013-09-06 18:03:48 +0000813void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
814 if (!Node->varlist_empty()) {
815 OS << "shared";
Alexey Bataev756c1962013-09-24 03:17:45 +0000816 VisitOMPClauseList(Node, '(');
Alexey Bataev758e55e2013-09-06 18:03:48 +0000817 OS << ")";
818 }
819}
820
Alexey Bataevc5e02582014-06-16 07:08:35 +0000821void OMPClausePrinter::VisitOMPReductionClause(OMPReductionClause *Node) {
822 if (!Node->varlist_empty()) {
823 OS << "reduction(";
824 NestedNameSpecifier *QualifierLoc =
825 Node->getQualifierLoc().getNestedNameSpecifier();
826 OverloadedOperatorKind OOK =
827 Node->getNameInfo().getName().getCXXOverloadedOperator();
828 if (QualifierLoc == nullptr && OOK != OO_None) {
829 // Print reduction identifier in C format
830 OS << getOperatorSpelling(OOK);
831 } else {
832 // Use C++ format
833 if (QualifierLoc != nullptr)
834 QualifierLoc->print(OS, Policy);
835 OS << Node->getNameInfo();
836 }
837 OS << ":";
838 VisitOMPClauseList(Node, ' ');
839 OS << ")";
840 }
841}
842
Alexey Bataev169d96a2017-07-18 20:17:46 +0000843void OMPClausePrinter::VisitOMPTaskReductionClause(
844 OMPTaskReductionClause *Node) {
845 if (!Node->varlist_empty()) {
846 OS << "task_reduction(";
847 NestedNameSpecifier *QualifierLoc =
848 Node->getQualifierLoc().getNestedNameSpecifier();
849 OverloadedOperatorKind OOK =
850 Node->getNameInfo().getName().getCXXOverloadedOperator();
851 if (QualifierLoc == nullptr && OOK != OO_None) {
852 // Print reduction identifier in C format
853 OS << getOperatorSpelling(OOK);
854 } else {
855 // Use C++ format
856 if (QualifierLoc != nullptr)
857 QualifierLoc->print(OS, Policy);
858 OS << Node->getNameInfo();
859 }
860 OS << ":";
861 VisitOMPClauseList(Node, ' ');
862 OS << ")";
863 }
864}
865
Alexey Bataevfa312f32017-07-21 18:48:21 +0000866void OMPClausePrinter::VisitOMPInReductionClause(OMPInReductionClause *Node) {
867 if (!Node->varlist_empty()) {
868 OS << "in_reduction(";
869 NestedNameSpecifier *QualifierLoc =
870 Node->getQualifierLoc().getNestedNameSpecifier();
871 OverloadedOperatorKind OOK =
872 Node->getNameInfo().getName().getCXXOverloadedOperator();
873 if (QualifierLoc == nullptr && OOK != OO_None) {
874 // Print reduction identifier in C format
875 OS << getOperatorSpelling(OOK);
876 } else {
877 // Use C++ format
878 if (QualifierLoc != nullptr)
879 QualifierLoc->print(OS, Policy);
880 OS << Node->getNameInfo();
881 }
882 OS << ":";
883 VisitOMPClauseList(Node, ' ');
884 OS << ")";
885 }
886}
887
Alexander Musman8dba6642014-04-22 13:09:42 +0000888void OMPClausePrinter::VisitOMPLinearClause(OMPLinearClause *Node) {
889 if (!Node->varlist_empty()) {
890 OS << "linear";
Alexey Bataev182227b2015-08-20 10:54:39 +0000891 if (Node->getModifierLoc().isValid()) {
892 OS << '('
893 << getOpenMPSimpleClauseTypeName(OMPC_linear, Node->getModifier());
894 }
Alexander Musman8dba6642014-04-22 13:09:42 +0000895 VisitOMPClauseList(Node, '(');
Alexey Bataev182227b2015-08-20 10:54:39 +0000896 if (Node->getModifierLoc().isValid())
897 OS << ')';
Craig Topper36250ad2014-05-12 05:36:57 +0000898 if (Node->getStep() != nullptr) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000899 OS << ": ";
Craig Topper36250ad2014-05-12 05:36:57 +0000900 Node->getStep()->printPretty(OS, nullptr, Policy, 0);
Alexander Musman8dba6642014-04-22 13:09:42 +0000901 }
902 OS << ")";
903 }
904}
905
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000906void OMPClausePrinter::VisitOMPAlignedClause(OMPAlignedClause *Node) {
907 if (!Node->varlist_empty()) {
908 OS << "aligned";
909 VisitOMPClauseList(Node, '(');
910 if (Node->getAlignment() != nullptr) {
911 OS << ": ";
912 Node->getAlignment()->printPretty(OS, nullptr, Policy, 0);
913 }
914 OS << ")";
915 }
916}
917
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000918void OMPClausePrinter::VisitOMPCopyinClause(OMPCopyinClause *Node) {
919 if (!Node->varlist_empty()) {
920 OS << "copyin";
921 VisitOMPClauseList(Node, '(');
922 OS << ")";
923 }
924}
925
Alexey Bataevbae9a792014-06-27 10:37:06 +0000926void OMPClausePrinter::VisitOMPCopyprivateClause(OMPCopyprivateClause *Node) {
927 if (!Node->varlist_empty()) {
928 OS << "copyprivate";
929 VisitOMPClauseList(Node, '(');
930 OS << ")";
931 }
932}
933
Alexey Bataev6125da92014-07-21 11:26:11 +0000934void OMPClausePrinter::VisitOMPFlushClause(OMPFlushClause *Node) {
935 if (!Node->varlist_empty()) {
936 VisitOMPClauseList(Node, '(');
937 OS << ")";
938 }
939}
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000940
941void OMPClausePrinter::VisitOMPDependClause(OMPDependClause *Node) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000942 OS << "depend(";
943 OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(),
944 Node->getDependencyKind());
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000945 if (!Node->varlist_empty()) {
Alexey Bataeveb482352015-12-18 05:05:56 +0000946 OS << " :";
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000947 VisitOMPClauseList(Node, ' ');
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000948 }
Alexey Bataeveb482352015-12-18 05:05:56 +0000949 OS << ")";
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000950}
Kelvin Li0bff7af2015-11-23 05:32:03 +0000951
952void OMPClausePrinter::VisitOMPMapClause(OMPMapClause *Node) {
953 if (!Node->varlist_empty()) {
954 OS << "map(";
955 if (Node->getMapType() != OMPC_MAP_unknown) {
956 if (Node->getMapTypeModifier() != OMPC_MAP_unknown) {
957 OS << getOpenMPSimpleClauseTypeName(OMPC_map,
958 Node->getMapTypeModifier());
959 OS << ',';
960 }
961 OS << getOpenMPSimpleClauseTypeName(OMPC_map, Node->getMapType());
962 OS << ':';
963 }
964 VisitOMPClauseList(Node, ' ');
965 OS << ")";
966 }
967}
Carlo Bertollib4adf552016-01-15 18:50:31 +0000968
Samuel Antao661c0902016-05-26 17:39:58 +0000969void OMPClausePrinter::VisitOMPToClause(OMPToClause *Node) {
970 if (!Node->varlist_empty()) {
971 OS << "to";
972 VisitOMPClauseList(Node, '(');
973 OS << ")";
974 }
975}
976
Samuel Antaoec172c62016-05-26 17:49:04 +0000977void OMPClausePrinter::VisitOMPFromClause(OMPFromClause *Node) {
978 if (!Node->varlist_empty()) {
979 OS << "from";
980 VisitOMPClauseList(Node, '(');
981 OS << ")";
982 }
983}
984
Carlo Bertollib4adf552016-01-15 18:50:31 +0000985void OMPClausePrinter::VisitOMPDistScheduleClause(OMPDistScheduleClause *Node) {
986 OS << "dist_schedule(" << getOpenMPSimpleClauseTypeName(
987 OMPC_dist_schedule, Node->getDistScheduleKind());
Alexey Bataev3392d762016-02-16 11:18:12 +0000988 if (auto *E = Node->getChunkSize()) {
Carlo Bertollib4adf552016-01-15 18:50:31 +0000989 OS << ", ";
Alexey Bataev60da77e2016-02-29 05:54:20 +0000990 E->printPretty(OS, nullptr, Policy);
Carlo Bertollib4adf552016-01-15 18:50:31 +0000991 }
992 OS << ")";
993}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +0000994
995void OMPClausePrinter::VisitOMPDefaultmapClause(OMPDefaultmapClause *Node) {
996 OS << "defaultmap(";
997 OS << getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
998 Node->getDefaultmapModifier());
999 OS << ": ";
1000 OS << getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
1001 Node->getDefaultmapKind());
1002 OS << ")";
1003}
Carlo Bertolli2404b172016-07-13 15:37:16 +00001004
1005void OMPClausePrinter::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *Node) {
1006 if (!Node->varlist_empty()) {
1007 OS << "use_device_ptr";
1008 VisitOMPClauseList(Node, '(');
1009 OS << ")";
1010 }
1011}
Carlo Bertolli70594e92016-07-13 17:16:49 +00001012
1013void OMPClausePrinter::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *Node) {
1014 if (!Node->varlist_empty()) {
1015 OS << "is_device_ptr";
1016 VisitOMPClauseList(Node, '(');
1017 OS << ")";
1018 }
1019}
Alexander Kornienkoab9db512015-06-22 23:07:51 +00001020}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001021
1022//===----------------------------------------------------------------------===//
1023// OpenMP directives printing methods
1024//===----------------------------------------------------------------------===//
1025
Alexey Bataev7828b252017-11-21 17:08:48 +00001026void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S,
1027 bool ForceNoStmt) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001028 OMPClausePrinter Printer(OS, Policy);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001029 ArrayRef<OMPClause *> Clauses = S->clauses();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001030 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
1031 I != E; ++I)
1032 if (*I && !(*I)->isImplicit()) {
1033 Printer.Visit(*I);
1034 OS << ' ';
1035 }
1036 OS << "\n";
Alexey Bataev7828b252017-11-21 17:08:48 +00001037 if (S->hasAssociatedStmt() && S->getAssociatedStmt() && !ForceNoStmt) {
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001038 assert(isa<CapturedStmt>(S->getAssociatedStmt()) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001039 "Expected captured statement!");
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001040 Stmt *CS = cast<CapturedStmt>(S->getAssociatedStmt())->getCapturedStmt();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001041 PrintStmt(CS);
1042 }
1043}
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001044
1045void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
1046 Indent() << "#pragma omp parallel ";
1047 PrintOMPExecutableDirective(Node);
1048}
1049
1050void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
1051 Indent() << "#pragma omp simd ";
1052 PrintOMPExecutableDirective(Node);
1053}
1054
Alexey Bataevf29276e2014-06-18 04:14:57 +00001055void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) {
1056 Indent() << "#pragma omp for ";
1057 PrintOMPExecutableDirective(Node);
1058}
1059
Alexander Musmanf82886e2014-09-18 05:12:34 +00001060void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) {
1061 Indent() << "#pragma omp for simd ";
1062 PrintOMPExecutableDirective(Node);
1063}
1064
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001065void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) {
1066 Indent() << "#pragma omp sections ";
1067 PrintOMPExecutableDirective(Node);
1068}
1069
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001070void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) {
1071 Indent() << "#pragma omp section";
1072 PrintOMPExecutableDirective(Node);
1073}
1074
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001075void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) {
1076 Indent() << "#pragma omp single ";
1077 PrintOMPExecutableDirective(Node);
1078}
1079
Alexander Musman80c22892014-07-17 08:54:58 +00001080void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) {
1081 Indent() << "#pragma omp master";
1082 PrintOMPExecutableDirective(Node);
1083}
1084
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001085void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) {
1086 Indent() << "#pragma omp critical";
1087 if (Node->getDirectiveName().getName()) {
1088 OS << " (";
1089 Node->getDirectiveName().printName(OS);
1090 OS << ")";
1091 }
Alexey Bataev28c75412015-12-15 08:19:24 +00001092 OS << " ";
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001093 PrintOMPExecutableDirective(Node);
1094}
1095
Alexey Bataev4acb8592014-07-07 13:01:15 +00001096void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) {
1097 Indent() << "#pragma omp parallel for ";
1098 PrintOMPExecutableDirective(Node);
1099}
1100
Alexander Musmane4e893b2014-09-23 09:33:00 +00001101void StmtPrinter::VisitOMPParallelForSimdDirective(
1102 OMPParallelForSimdDirective *Node) {
1103 Indent() << "#pragma omp parallel for simd ";
1104 PrintOMPExecutableDirective(Node);
1105}
1106
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001107void StmtPrinter::VisitOMPParallelSectionsDirective(
1108 OMPParallelSectionsDirective *Node) {
1109 Indent() << "#pragma omp parallel sections ";
1110 PrintOMPExecutableDirective(Node);
1111}
1112
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001113void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) {
1114 Indent() << "#pragma omp task ";
1115 PrintOMPExecutableDirective(Node);
1116}
1117
Alexey Bataev68446b72014-07-18 07:47:19 +00001118void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) {
1119 Indent() << "#pragma omp taskyield";
1120 PrintOMPExecutableDirective(Node);
1121}
1122
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001123void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) {
1124 Indent() << "#pragma omp barrier";
1125 PrintOMPExecutableDirective(Node);
1126}
1127
Alexey Bataev2df347a2014-07-18 10:17:07 +00001128void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) {
1129 Indent() << "#pragma omp taskwait";
1130 PrintOMPExecutableDirective(Node);
1131}
1132
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001133void StmtPrinter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective *Node) {
Alexey Bataev169d96a2017-07-18 20:17:46 +00001134 Indent() << "#pragma omp taskgroup ";
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001135 PrintOMPExecutableDirective(Node);
1136}
1137
Alexey Bataev6125da92014-07-21 11:26:11 +00001138void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) {
1139 Indent() << "#pragma omp flush ";
1140 PrintOMPExecutableDirective(Node);
1141}
1142
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001143void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) {
Alexey Bataev346265e2015-09-25 10:37:12 +00001144 Indent() << "#pragma omp ordered ";
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001145 PrintOMPExecutableDirective(Node);
1146}
1147
Alexey Bataev0162e452014-07-22 10:10:35 +00001148void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) {
1149 Indent() << "#pragma omp atomic ";
1150 PrintOMPExecutableDirective(Node);
1151}
1152
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001153void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) {
1154 Indent() << "#pragma omp target ";
1155 PrintOMPExecutableDirective(Node);
1156}
1157
Michael Wong65f367f2015-07-21 13:44:28 +00001158void StmtPrinter::VisitOMPTargetDataDirective(OMPTargetDataDirective *Node) {
1159 Indent() << "#pragma omp target data ";
1160 PrintOMPExecutableDirective(Node);
1161}
1162
Samuel Antaodf67fc42016-01-19 19:15:56 +00001163void StmtPrinter::VisitOMPTargetEnterDataDirective(
1164 OMPTargetEnterDataDirective *Node) {
1165 Indent() << "#pragma omp target enter data ";
Alexey Bataev7828b252017-11-21 17:08:48 +00001166 PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
Samuel Antaodf67fc42016-01-19 19:15:56 +00001167}
1168
Samuel Antao72590762016-01-19 20:04:50 +00001169void StmtPrinter::VisitOMPTargetExitDataDirective(
1170 OMPTargetExitDataDirective *Node) {
1171 Indent() << "#pragma omp target exit data ";
Alexey Bataev7828b252017-11-21 17:08:48 +00001172 PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
Samuel Antao72590762016-01-19 20:04:50 +00001173}
1174
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001175void StmtPrinter::VisitOMPTargetParallelDirective(
1176 OMPTargetParallelDirective *Node) {
1177 Indent() << "#pragma omp target parallel ";
1178 PrintOMPExecutableDirective(Node);
1179}
1180
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001181void StmtPrinter::VisitOMPTargetParallelForDirective(
1182 OMPTargetParallelForDirective *Node) {
1183 Indent() << "#pragma omp target parallel for ";
1184 PrintOMPExecutableDirective(Node);
1185}
1186
Alexey Bataev13314bf2014-10-09 04:18:56 +00001187void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) {
1188 Indent() << "#pragma omp teams ";
1189 PrintOMPExecutableDirective(Node);
1190}
1191
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001192void StmtPrinter::VisitOMPCancellationPointDirective(
1193 OMPCancellationPointDirective *Node) {
1194 Indent() << "#pragma omp cancellation point "
1195 << getOpenMPDirectiveName(Node->getCancelRegion());
1196 PrintOMPExecutableDirective(Node);
1197}
Alexey Bataev80909872015-07-02 11:25:17 +00001198
1199void StmtPrinter::VisitOMPCancelDirective(OMPCancelDirective *Node) {
1200 Indent() << "#pragma omp cancel "
Alexey Bataev87933c72015-09-18 08:07:34 +00001201 << getOpenMPDirectiveName(Node->getCancelRegion()) << " ";
Alexey Bataev80909872015-07-02 11:25:17 +00001202 PrintOMPExecutableDirective(Node);
1203}
Alexey Bataev49f6e782015-12-01 04:18:41 +00001204
1205void StmtPrinter::VisitOMPTaskLoopDirective(OMPTaskLoopDirective *Node) {
1206 Indent() << "#pragma omp taskloop ";
1207 PrintOMPExecutableDirective(Node);
1208}
1209
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001210void StmtPrinter::VisitOMPTaskLoopSimdDirective(
1211 OMPTaskLoopSimdDirective *Node) {
1212 Indent() << "#pragma omp taskloop simd ";
1213 PrintOMPExecutableDirective(Node);
1214}
1215
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001216void StmtPrinter::VisitOMPDistributeDirective(OMPDistributeDirective *Node) {
1217 Indent() << "#pragma omp distribute ";
1218 PrintOMPExecutableDirective(Node);
1219}
1220
Samuel Antao686c70c2016-05-26 17:30:50 +00001221void StmtPrinter::VisitOMPTargetUpdateDirective(
1222 OMPTargetUpdateDirective *Node) {
1223 Indent() << "#pragma omp target update ";
Alexey Bataev7828b252017-11-21 17:08:48 +00001224 PrintOMPExecutableDirective(Node, /*ForceNoStmt=*/true);
Samuel Antao686c70c2016-05-26 17:30:50 +00001225}
1226
Carlo Bertolli9925f152016-06-27 14:55:37 +00001227void StmtPrinter::VisitOMPDistributeParallelForDirective(
1228 OMPDistributeParallelForDirective *Node) {
1229 Indent() << "#pragma omp distribute parallel for ";
1230 PrintOMPExecutableDirective(Node);
1231}
1232
Kelvin Li4a39add2016-07-05 05:00:15 +00001233void StmtPrinter::VisitOMPDistributeParallelForSimdDirective(
1234 OMPDistributeParallelForSimdDirective *Node) {
1235 Indent() << "#pragma omp distribute parallel for simd ";
1236 PrintOMPExecutableDirective(Node);
1237}
1238
Kelvin Li787f3fc2016-07-06 04:45:38 +00001239void StmtPrinter::VisitOMPDistributeSimdDirective(
1240 OMPDistributeSimdDirective *Node) {
1241 Indent() << "#pragma omp distribute simd ";
1242 PrintOMPExecutableDirective(Node);
1243}
1244
Kelvin Lia579b912016-07-14 02:54:56 +00001245void StmtPrinter::VisitOMPTargetParallelForSimdDirective(
1246 OMPTargetParallelForSimdDirective *Node) {
1247 Indent() << "#pragma omp target parallel for simd ";
1248 PrintOMPExecutableDirective(Node);
1249}
1250
Kelvin Li986330c2016-07-20 22:57:10 +00001251void StmtPrinter::VisitOMPTargetSimdDirective(OMPTargetSimdDirective *Node) {
1252 Indent() << "#pragma omp target simd ";
1253 PrintOMPExecutableDirective(Node);
1254}
1255
Kelvin Li02532872016-08-05 14:37:37 +00001256void StmtPrinter::VisitOMPTeamsDistributeDirective(
1257 OMPTeamsDistributeDirective *Node) {
1258 Indent() << "#pragma omp teams distribute ";
1259 PrintOMPExecutableDirective(Node);
1260}
1261
Kelvin Li4e325f72016-10-25 12:50:55 +00001262void StmtPrinter::VisitOMPTeamsDistributeSimdDirective(
1263 OMPTeamsDistributeSimdDirective *Node) {
1264 Indent() << "#pragma omp teams distribute simd ";
1265 PrintOMPExecutableDirective(Node);
1266}
1267
Kelvin Li579e41c2016-11-30 23:51:03 +00001268void StmtPrinter::VisitOMPTeamsDistributeParallelForSimdDirective(
1269 OMPTeamsDistributeParallelForSimdDirective *Node) {
1270 Indent() << "#pragma omp teams distribute parallel for simd ";
1271 PrintOMPExecutableDirective(Node);
1272}
1273
Kelvin Li7ade93f2016-12-09 03:24:30 +00001274void StmtPrinter::VisitOMPTeamsDistributeParallelForDirective(
1275 OMPTeamsDistributeParallelForDirective *Node) {
1276 Indent() << "#pragma omp teams distribute parallel for ";
1277 PrintOMPExecutableDirective(Node);
1278}
1279
Kelvin Libf594a52016-12-17 05:48:59 +00001280void StmtPrinter::VisitOMPTargetTeamsDirective(OMPTargetTeamsDirective *Node) {
1281 Indent() << "#pragma omp target teams ";
1282 PrintOMPExecutableDirective(Node);
1283}
1284
Kelvin Li83c451e2016-12-25 04:52:54 +00001285void StmtPrinter::VisitOMPTargetTeamsDistributeDirective(
1286 OMPTargetTeamsDistributeDirective *Node) {
1287 Indent() << "#pragma omp target teams distribute ";
1288 PrintOMPExecutableDirective(Node);
1289}
1290
Kelvin Li80e8f562016-12-29 22:16:30 +00001291void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForDirective(
1292 OMPTargetTeamsDistributeParallelForDirective *Node) {
1293 Indent() << "#pragma omp target teams distribute parallel for ";
1294 PrintOMPExecutableDirective(Node);
1295}
1296
Kelvin Li1851df52017-01-03 05:23:48 +00001297void StmtPrinter::VisitOMPTargetTeamsDistributeParallelForSimdDirective(
1298 OMPTargetTeamsDistributeParallelForSimdDirective *Node) {
1299 Indent() << "#pragma omp target teams distribute parallel for simd ";
1300 PrintOMPExecutableDirective(Node);
1301}
1302
Kelvin Lida681182017-01-10 18:08:18 +00001303void StmtPrinter::VisitOMPTargetTeamsDistributeSimdDirective(
1304 OMPTargetTeamsDistributeSimdDirective *Node) {
1305 Indent() << "#pragma omp target teams distribute simd ";
1306 PrintOMPExecutableDirective(Node);
1307}
1308
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001309//===----------------------------------------------------------------------===//
Chris Lattner71e23ce2006-11-04 20:18:38 +00001310// Expr printing methods.
1311//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001312
Chris Lattner882f7882006-11-04 18:52:07 +00001313void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00001314 if (auto *OCED = dyn_cast<OMPCapturedExprDecl>(Node->getDecl())) {
1315 OCED->getInit()->IgnoreImpCasts()->printPretty(OS, nullptr, Policy);
1316 return;
1317 }
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001318 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1319 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001320 if (Node->hasTemplateKeyword())
1321 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001322 OS << Node->getNameInfo();
John McCallb3774b52010-08-19 23:49:38 +00001323 if (Node->hasExplicitTemplateArgs())
Serge Pavlov03e672c2017-11-28 16:14:14 +00001324 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
Douglas Gregorc7acfdf2009-01-06 05:10:23 +00001325}
1326
John McCall8cd78132009-11-19 22:55:06 +00001327void StmtPrinter::VisitDependentScopeDeclRefExpr(
1328 DependentScopeDeclRefExpr *Node) {
Axel Naumann20b27862011-01-24 15:44:00 +00001329 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1330 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001331 if (Node->hasTemplateKeyword())
1332 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001333 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +00001334 if (Node->hasExplicitTemplateArgs())
Serge Pavlov03e672c2017-11-28 16:14:14 +00001335 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
Douglas Gregor90a1a652009-03-19 17:26:29 +00001336}
1337
John McCalld14a8642009-11-21 08:51:07 +00001338void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregora727cb92009-06-30 22:34:41 +00001339 if (Node->getQualifier())
1340 Node->getQualifier()->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001341 if (Node->hasTemplateKeyword())
1342 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001343 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +00001344 if (Node->hasExplicitTemplateArgs())
Serge Pavlov03e672c2017-11-28 16:14:14 +00001345 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
Douglas Gregora727cb92009-06-30 22:34:41 +00001346}
1347
Alex Lorenzf7579612017-10-26 00:56:54 +00001348static bool isImplicitSelf(const Expr *E) {
1349 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) {
1350 if (const ImplicitParamDecl *PD =
1351 dyn_cast<ImplicitParamDecl>(DRE->getDecl())) {
1352 if (PD->getParameterKind() == ImplicitParamDecl::ObjCSelf &&
1353 DRE->getLocStart().isInvalid())
1354 return true;
1355 }
1356 }
1357 return false;
1358}
1359
Steve Naroffe46504b2007-11-12 14:29:37 +00001360void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +00001361 if (Node->getBase()) {
Alex Lorenzf7579612017-10-26 00:56:54 +00001362 if (!Policy.SuppressImplicitBase ||
1363 !isImplicitSelf(Node->getBase()->IgnoreImpCasts())) {
1364 PrintExpr(Node->getBase());
1365 OS << (Node->isArrow() ? "->" : ".");
1366 }
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +00001367 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001368 OS << *Node->getDecl();
Steve Naroffe46504b2007-11-12 14:29:37 +00001369}
1370
Steve Naroffec944032008-05-30 00:40:33 +00001371void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Fariborz Jahanian681c0752010-10-14 16:04:05 +00001372 if (Node->isSuperReceiver())
1373 OS << "super.";
Richard Trieu6d92e502014-02-06 23:26:23 +00001374 else if (Node->isObjectReceiver() && Node->getBase()) {
Steve Naroffec944032008-05-30 00:40:33 +00001375 PrintExpr(Node->getBase());
1376 OS << ".";
Richard Trieu6d92e502014-02-06 23:26:23 +00001377 } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
1378 OS << Node->getClassReceiver()->getName() << ".";
Steve Naroffec944032008-05-30 00:40:33 +00001379 }
Fariborz Jahanian681c0752010-10-14 16:04:05 +00001380
John McCallb7bd14f2010-12-02 01:19:52 +00001381 if (Node->isImplicitProperty())
Aaron Ballmanb190f972014-01-03 17:59:55 +00001382 Node->getImplicitPropertyGetter()->getSelector().print(OS);
John McCallb7bd14f2010-12-02 01:19:52 +00001383 else
1384 OS << Node->getExplicitProperty()->getName();
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00001385}
1386
Ted Kremeneke65b0862012-03-06 20:05:56 +00001387void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
1388
1389 PrintExpr(Node->getBaseExpr());
1390 OS << "[";
1391 PrintExpr(Node->getKeyExpr());
1392 OS << "]";
1393}
1394
Chris Lattner6307f192008-08-10 01:53:14 +00001395void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Alexey Bataevec474782014-10-09 08:45:04 +00001396 OS << PredefinedExpr::getIdentTypeName(Node->getIdentType());
Anders Carlsson625bfc82007-07-21 05:21:51 +00001397}
1398
Steve Naroffae4143e2007-04-26 20:39:23 +00001399void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +00001400 unsigned value = Node->getValue();
Douglas Gregorfb65e592011-07-27 05:40:30 +00001401
1402 switch (Node->getKind()) {
1403 case CharacterLiteral::Ascii: break; // no prefix.
1404 case CharacterLiteral::Wide: OS << 'L'; break;
Aaron Ballman9a17c852016-01-07 20:59:26 +00001405 case CharacterLiteral::UTF8: OS << "u8"; break;
Douglas Gregorfb65e592011-07-27 05:40:30 +00001406 case CharacterLiteral::UTF16: OS << 'u'; break;
1407 case CharacterLiteral::UTF32: OS << 'U'; break;
1408 }
1409
Chris Lattner666115c2007-07-13 23:58:20 +00001410 switch (value) {
1411 case '\\':
1412 OS << "'\\\\'";
1413 break;
1414 case '\'':
1415 OS << "'\\''";
1416 break;
1417 case '\a':
1418 // TODO: K&R: the meaning of '\\a' is different in traditional C
1419 OS << "'\\a'";
1420 break;
1421 case '\b':
1422 OS << "'\\b'";
1423 break;
1424 // Nonstandard escape sequence.
1425 /*case '\e':
1426 OS << "'\\e'";
1427 break;*/
1428 case '\f':
1429 OS << "'\\f'";
1430 break;
1431 case '\n':
1432 OS << "'\\n'";
1433 break;
1434 case '\r':
1435 OS << "'\\r'";
1436 break;
1437 case '\t':
1438 OS << "'\\t'";
1439 break;
1440 case '\v':
1441 OS << "'\\v'";
1442 break;
1443 default:
Steven Watanabee43ae192016-02-13 02:31:28 +00001444 // A character literal might be sign-extended, which
1445 // would result in an invalid \U escape sequence.
1446 // FIXME: multicharacter literals such as '\xFF\xFF\xFF\xFF'
1447 // are not correctly handled.
1448 if ((value & ~0xFFu) == ~0xFFu && Node->getKind() == CharacterLiteral::Ascii)
1449 value &= 0xFFu;
Jordan Rose00d1b592013-02-08 22:30:27 +00001450 if (value < 256 && isPrintable((unsigned char)value))
Chris Lattner666115c2007-07-13 23:58:20 +00001451 OS << "'" << (char)value << "'";
Jordan Rose00d1b592013-02-08 22:30:27 +00001452 else if (value < 256)
1453 OS << "'\\x" << llvm::format("%02x", value) << "'";
1454 else if (value <= 0xFFFF)
1455 OS << "'\\u" << llvm::format("%04x", value) << "'";
1456 else
1457 OS << "'\\U" << llvm::format("%08x", value) << "'";
Chris Lattner6e9d9b32007-07-13 05:18:11 +00001458 }
Steve Naroffae4143e2007-04-26 20:39:23 +00001459}
1460
Alex Lorenz36070ed2017-08-17 13:41:55 +00001461/// Prints the given expression using the original source text. Returns true on
1462/// success, false otherwise.
1463static bool printExprAsWritten(raw_ostream &OS, Expr *E,
1464 const ASTContext *Context) {
1465 if (!Context)
1466 return false;
1467 bool Invalid = false;
1468 StringRef Source = Lexer::getSourceText(
1469 CharSourceRange::getTokenRange(E->getSourceRange()),
1470 Context->getSourceManager(), Context->getLangOpts(), &Invalid);
1471 if (!Invalid) {
1472 OS << Source;
1473 return true;
1474 }
1475 return false;
1476}
1477
Steve Naroffdf7855b2007-02-21 23:46:25 +00001478void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Alex Lorenz36070ed2017-08-17 13:41:55 +00001479 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1480 return;
Chris Lattner06430412007-05-21 05:45:03 +00001481 bool isSigned = Node->getType()->isSignedIntegerType();
1482 OS << Node->getValue().toString(10, isSigned);
Mike Stump11289f42009-09-09 15:08:12 +00001483
Chris Lattner06430412007-05-21 05:45:03 +00001484 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall9dd450b2009-09-21 23:43:11 +00001485 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
David Blaikie83d382b2011-09-23 05:06:16 +00001486 default: llvm_unreachable("Unexpected type for integer literal!");
David Majnemerbe09e8e2015-03-06 18:04:22 +00001487 case BuiltinType::Char_S:
1488 case BuiltinType::Char_U: OS << "i8"; break;
David Majnemer65a407c2014-06-21 18:46:07 +00001489 case BuiltinType::UChar: OS << "Ui8"; break;
1490 case BuiltinType::Short: OS << "i16"; break;
1491 case BuiltinType::UShort: OS << "Ui16"; break;
Chris Lattner06430412007-05-21 05:45:03 +00001492 case BuiltinType::Int: break; // no suffix.
1493 case BuiltinType::UInt: OS << 'U'; break;
1494 case BuiltinType::Long: OS << 'L'; break;
1495 case BuiltinType::ULong: OS << "UL"; break;
1496 case BuiltinType::LongLong: OS << "LL"; break;
1497 case BuiltinType::ULongLong: OS << "ULL"; break;
1498 }
Chris Lattner882f7882006-11-04 18:52:07 +00001499}
Benjamin Kramer8a526762012-09-20 14:07:17 +00001500
1501static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
1502 bool PrintSuffix) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001503 SmallString<16> Str;
Eli Friedman53392062011-10-05 20:32:03 +00001504 Node->getValue().toString(Str);
1505 OS << Str;
Benjamin Kramer8a526762012-09-20 14:07:17 +00001506 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
1507 OS << '.'; // Trailing dot in order to separate from ints.
1508
1509 if (!PrintSuffix)
1510 return;
1511
1512 // Emit suffixes. Float literals are always a builtin float type.
1513 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
1514 default: llvm_unreachable("Unexpected type for float literal!");
1515 case BuiltinType::Half: break; // FIXME: suffix?
1516 case BuiltinType::Double: break; // no suffix.
Sjoerd Meijercc623ad2017-09-08 15:15:00 +00001517 case BuiltinType::Float16: OS << "F16"; break;
Benjamin Kramer8a526762012-09-20 14:07:17 +00001518 case BuiltinType::Float: OS << 'F'; break;
1519 case BuiltinType::LongDouble: OS << 'L'; break;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00001520 case BuiltinType::Float128: OS << 'Q'; break;
Benjamin Kramer8a526762012-09-20 14:07:17 +00001521 }
1522}
1523
1524void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
Alex Lorenz36070ed2017-08-17 13:41:55 +00001525 if (Policy.ConstantsAsWritten && printExprAsWritten(OS, Node, Context))
1526 return;
Benjamin Kramer8a526762012-09-20 14:07:17 +00001527 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
Chris Lattner882f7882006-11-04 18:52:07 +00001528}
Chris Lattner1c20a172007-08-26 03:42:43 +00001529
1530void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
1531 PrintExpr(Node->getSubExpr());
1532 OS << "i";
1533}
1534
Steve Naroffdf7855b2007-02-21 23:46:25 +00001535void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Richard Trieudc355912012-06-13 20:25:24 +00001536 Str->outputString(OS);
Chris Lattner882f7882006-11-04 18:52:07 +00001537}
1538void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
1539 OS << "(";
1540 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +00001541 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +00001542}
1543void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +00001544 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +00001545 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump11289f42009-09-09 15:08:12 +00001546
Eli Friedman1cf25362009-06-14 22:39:26 +00001547 // Print a space if this is an "identifier operator" like __real, or if
1548 // it might be concatenated incorrectly like '+'.
Chris Lattnere5b60442007-08-23 21:46:40 +00001549 switch (Node->getOpcode()) {
1550 default: break;
John McCalle3027922010-08-25 11:45:40 +00001551 case UO_Real:
1552 case UO_Imag:
1553 case UO_Extension:
Chris Lattnere5b60442007-08-23 21:46:40 +00001554 OS << ' ';
1555 break;
John McCalle3027922010-08-25 11:45:40 +00001556 case UO_Plus:
1557 case UO_Minus:
Eli Friedman1cf25362009-06-14 22:39:26 +00001558 if (isa<UnaryOperator>(Node->getSubExpr()))
1559 OS << ' ';
1560 break;
Chris Lattnere5b60442007-08-23 21:46:40 +00001561 }
1562 }
Chris Lattner882f7882006-11-04 18:52:07 +00001563 PrintExpr(Node->getSubExpr());
Mike Stump11289f42009-09-09 15:08:12 +00001564
Chris Lattner15768702006-11-05 23:54:51 +00001565 if (Node->isPostfix())
1566 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +00001567}
Chris Lattner98dbf0a2007-08-30 17:59:59 +00001568
Douglas Gregor882211c2010-04-28 22:16:22 +00001569void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
1570 OS << "__builtin_offsetof(";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001571 Node->getTypeSourceInfo()->getType().print(OS, Policy);
1572 OS << ", ";
Douglas Gregor882211c2010-04-28 22:16:22 +00001573 bool PrintedSomething = false;
1574 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
James Y Knight7281c352015-12-29 22:31:18 +00001575 OffsetOfNode ON = Node->getComponent(i);
1576 if (ON.getKind() == OffsetOfNode::Array) {
Douglas Gregor882211c2010-04-28 22:16:22 +00001577 // Array node
1578 OS << "[";
1579 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
1580 OS << "]";
1581 PrintedSomething = true;
1582 continue;
1583 }
Douglas Gregord1702062010-04-29 00:18:15 +00001584
1585 // Skip implicit base indirections.
James Y Knight7281c352015-12-29 22:31:18 +00001586 if (ON.getKind() == OffsetOfNode::Base)
Douglas Gregord1702062010-04-29 00:18:15 +00001587 continue;
1588
Douglas Gregor882211c2010-04-28 22:16:22 +00001589 // Field or identifier node.
1590 IdentifierInfo *Id = ON.getFieldName();
1591 if (!Id)
1592 continue;
Alexis Hunta8136cc2010-05-05 15:23:54 +00001593
Douglas Gregor882211c2010-04-28 22:16:22 +00001594 if (PrintedSomething)
1595 OS << ".";
1596 else
1597 PrintedSomething = true;
Alexis Hunta8136cc2010-05-05 15:23:54 +00001598 OS << Id->getName();
Douglas Gregor882211c2010-04-28 22:16:22 +00001599 }
1600 OS << ")";
1601}
1602
Peter Collingbournee190dee2011-03-11 19:24:49 +00001603void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
1604 switch(Node->getKind()) {
1605 case UETT_SizeOf:
1606 OS << "sizeof";
1607 break;
1608 case UETT_AlignOf:
Richard Smith301bc212016-05-19 01:39:10 +00001609 if (Policy.Alignof)
Jordan Rose58d54722012-06-30 21:33:57 +00001610 OS << "alignof";
Richard Smith301bc212016-05-19 01:39:10 +00001611 else if (Policy.UnderscoreAlignof)
Jordan Rose58d54722012-06-30 21:33:57 +00001612 OS << "_Alignof";
1613 else
1614 OS << "__alignof";
Peter Collingbournee190dee2011-03-11 19:24:49 +00001615 break;
1616 case UETT_VecStep:
1617 OS << "vec_step";
1618 break;
Alexey Bataev00396512015-07-02 03:40:19 +00001619 case UETT_OpenMPRequiredSimdAlign:
1620 OS << "__builtin_omp_required_simd_align";
1621 break;
Peter Collingbournee190dee2011-03-11 19:24:49 +00001622 }
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001623 if (Node->isArgumentType()) {
1624 OS << '(';
1625 Node->getArgumentType().print(OS, Policy);
1626 OS << ')';
1627 } else {
Sebastian Redl6f282892008-11-11 17:56:53 +00001628 OS << " ";
1629 PrintExpr(Node->getArgumentExpr());
1630 }
Chris Lattner882f7882006-11-04 18:52:07 +00001631}
Peter Collingbourne91147592011-04-15 00:35:48 +00001632
1633void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
1634 OS << "_Generic(";
1635 PrintExpr(Node->getControllingExpr());
1636 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
1637 OS << ", ";
1638 QualType T = Node->getAssocType(i);
1639 if (T.isNull())
1640 OS << "default";
1641 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001642 T.print(OS, Policy);
Peter Collingbourne91147592011-04-15 00:35:48 +00001643 OS << ": ";
1644 PrintExpr(Node->getAssocExpr(i));
1645 }
1646 OS << ")";
1647}
1648
Chris Lattner882f7882006-11-04 18:52:07 +00001649void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +00001650 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +00001651 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +00001652 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +00001653 OS << "]";
1654}
1655
Alexey Bataev1a3320e2015-08-25 14:24:04 +00001656void StmtPrinter::VisitOMPArraySectionExpr(OMPArraySectionExpr *Node) {
1657 PrintExpr(Node->getBase());
1658 OS << "[";
1659 if (Node->getLowerBound())
1660 PrintExpr(Node->getLowerBound());
Alexey Bataeved5fb672015-09-11 04:54:28 +00001661 if (Node->getColonLoc().isValid()) {
1662 OS << ":";
1663 if (Node->getLength())
1664 PrintExpr(Node->getLength());
1665 }
Alexey Bataev1a3320e2015-08-25 14:24:04 +00001666 OS << "]";
1667}
1668
Peter Collingbourne4b279a02011-02-08 21:17:54 +00001669void StmtPrinter::PrintCallArgs(CallExpr *Call) {
Chris Lattner882f7882006-11-04 18:52:07 +00001670 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001671 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1672 // Don't print any defaulted arguments
1673 break;
1674 }
1675
Chris Lattner882f7882006-11-04 18:52:07 +00001676 if (i) OS << ", ";
1677 PrintExpr(Call->getArg(i));
1678 }
Peter Collingbourne4b279a02011-02-08 21:17:54 +00001679}
1680
1681void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1682 PrintExpr(Call->getCallee());
1683 OS << "(";
1684 PrintCallArgs(Call);
Chris Lattner882f7882006-11-04 18:52:07 +00001685 OS << ")";
1686}
Alex Lorenzf7579612017-10-26 00:56:54 +00001687
1688static bool isImplicitThis(const Expr *E) {
1689 if (const auto *TE = dyn_cast<CXXThisExpr>(E))
1690 return TE->isImplicit();
1691 return false;
1692}
1693
Chris Lattner882f7882006-11-04 18:52:07 +00001694void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Alex Lorenzf7579612017-10-26 00:56:54 +00001695 if (!Policy.SuppressImplicitBase || !isImplicitThis(Node->getBase())) {
1696 PrintExpr(Node->getBase());
David Blaikie8fab8e52012-11-12 19:12:12 +00001697
Alex Lorenzf7579612017-10-26 00:56:54 +00001698 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
1699 FieldDecl *ParentDecl =
1700 ParentMember ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl())
1701 : nullptr;
David Blaikie8fab8e52012-11-12 19:12:12 +00001702
Alex Lorenzf7579612017-10-26 00:56:54 +00001703 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
1704 OS << (Node->isArrow() ? "->" : ".");
1705 }
David Blaikie8fab8e52012-11-12 19:12:12 +00001706
Fariborz Jahanian2990c022010-01-11 21:17:32 +00001707 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1708 if (FD->isAnonymousStructOrUnion())
1709 return;
David Blaikie8fab8e52012-11-12 19:12:12 +00001710
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001711 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1712 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001713 if (Node->hasTemplateKeyword())
1714 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001715 OS << Node->getMemberNameInfo();
John McCallb3774b52010-08-19 23:49:38 +00001716 if (Node->hasExplicitTemplateArgs())
Serge Pavlov03e672c2017-11-28 16:14:14 +00001717 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001718}
Steve Naroffe87026a2009-07-24 17:54:45 +00001719void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1720 PrintExpr(Node->getBase());
1721 OS << (Node->isArrow() ? "->isa" : ".isa");
1722}
1723
Nate Begemance4d7fc2008-04-18 23:10:10 +00001724void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +00001725 PrintExpr(Node->getBase());
1726 OS << ".";
1727 OS << Node->getAccessor().getName();
1728}
Douglas Gregorf19b2312008-10-28 15:36:24 +00001729void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001730 OS << '(';
1731 Node->getTypeAsWritten().print(OS, Policy);
1732 OS << ')';
Chris Lattner882f7882006-11-04 18:52:07 +00001733 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001734}
Steve Naroff57eb2c52007-07-19 21:32:11 +00001735void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001736 OS << '(';
1737 Node->getType().print(OS, Policy);
1738 OS << ')';
Steve Naroff57eb2c52007-07-19 21:32:11 +00001739 PrintExpr(Node->getInitializer());
1740}
Steve Naroff7a5af782007-07-13 16:58:59 +00001741void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Alp Toker028ed912013-12-06 17:56:43 +00001742 // No need to print anything, simply forward to the subexpression.
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001743 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +00001744}
Chris Lattner882f7882006-11-04 18:52:07 +00001745void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1746 PrintExpr(Node->getLHS());
1747 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1748 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001749}
Chris Lattner86928112007-08-25 02:00:02 +00001750void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1751 PrintExpr(Node->getLHS());
1752 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1753 PrintExpr(Node->getRHS());
1754}
Chris Lattner882f7882006-11-04 18:52:07 +00001755void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1756 PrintExpr(Node->getCond());
John McCallc07a0c72011-02-17 10:25:35 +00001757 OS << " ? ";
1758 PrintExpr(Node->getLHS());
1759 OS << " : ";
Chris Lattner882f7882006-11-04 18:52:07 +00001760 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001761}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001762
Chris Lattnereefa10e2007-05-28 06:56:27 +00001763// GNU extensions.
1764
John McCallc07a0c72011-02-17 10:25:35 +00001765void
1766StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1767 PrintExpr(Node->getCommon());
1768 OS << " ?: ";
1769 PrintExpr(Node->getFalseExpr());
1770}
Chris Lattnerd268a7a2007-08-03 17:31:20 +00001771void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +00001772 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +00001773}
1774
Chris Lattner366727f2007-07-24 16:58:17 +00001775void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1776 OS << "(";
1777 PrintRawCompoundStmt(E->getSubStmt());
1778 OS << ")";
1779}
1780
Steve Naroff9efdabc2007-08-03 21:21:27 +00001781void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1782 OS << "__builtin_choose_expr(";
1783 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +00001784 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +00001785 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +00001786 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +00001787 PrintExpr(Node->getRHS());
1788 OS << ")";
1789}
Chris Lattner366727f2007-07-24 16:58:17 +00001790
Douglas Gregor3be4b122008-11-29 04:51:27 +00001791void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1792 OS << "__null";
1793}
1794
Eli Friedmana1b4ed82008-05-14 19:38:39 +00001795void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1796 OS << "__builtin_shufflevector(";
1797 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1798 if (i) OS << ", ";
1799 PrintExpr(Node->getExpr(i));
1800 }
1801 OS << ")";
1802}
1803
Hal Finkelc4d7c822013-09-18 03:29:45 +00001804void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1805 OS << "__builtin_convertvector(";
1806 PrintExpr(Node->getSrcExpr());
1807 OS << ", ";
1808 Node->getType().print(OS, Policy);
1809 OS << ")";
1810}
1811
Anders Carlsson4692db02007-08-31 04:56:16 +00001812void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001813 if (Node->getSyntacticForm()) {
1814 Visit(Node->getSyntacticForm());
1815 return;
1816 }
1817
Richard Smithd0e102f2015-01-30 02:04:26 +00001818 OS << "{";
Anders Carlsson4692db02007-08-31 04:56:16 +00001819 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1820 if (i) OS << ", ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001821 if (Node->getInit(i))
1822 PrintExpr(Node->getInit(i));
1823 else
Richard Smithd0e102f2015-01-30 02:04:26 +00001824 OS << "{}";
Anders Carlsson4692db02007-08-31 04:56:16 +00001825 }
Richard Smithd0e102f2015-01-30 02:04:26 +00001826 OS << "}";
Anders Carlsson4692db02007-08-31 04:56:16 +00001827}
1828
Richard Smith410306b2016-12-12 02:53:20 +00001829void StmtPrinter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *Node) {
1830 // There's no way to express this expression in any of our supported
1831 // languages, so just emit something terse and (hopefully) clear.
1832 OS << "{";
1833 PrintExpr(Node->getSubExpr());
1834 OS << "}";
1835}
1836
1837void StmtPrinter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *Node) {
1838 OS << "*";
1839}
1840
Nate Begeman5ec4b312009-08-10 23:49:36 +00001841void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
Richard Smithd0e102f2015-01-30 02:04:26 +00001842 OS << "(";
Nate Begeman5ec4b312009-08-10 23:49:36 +00001843 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1844 if (i) OS << ", ";
1845 PrintExpr(Node->getExpr(i));
1846 }
Richard Smithd0e102f2015-01-30 02:04:26 +00001847 OS << ")";
Nate Begeman5ec4b312009-08-10 23:49:36 +00001848}
1849
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001850void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Justin Bognercb337032015-05-28 22:19:36 +00001851 bool NeedsEquals = true;
David Majnemerf7e36092016-06-23 00:15:04 +00001852 for (const DesignatedInitExpr::Designator &D : Node->designators()) {
1853 if (D.isFieldDesignator()) {
1854 if (D.getDotLoc().isInvalid()) {
1855 if (IdentifierInfo *II = D.getFieldName()) {
Serge Pavloveb57aa62014-06-20 17:08:28 +00001856 OS << II->getName() << ":";
Justin Bognercb337032015-05-28 22:19:36 +00001857 NeedsEquals = false;
1858 }
Serge Pavloveb57aa62014-06-20 17:08:28 +00001859 } else {
David Majnemerf7e36092016-06-23 00:15:04 +00001860 OS << "." << D.getFieldName()->getName();
Serge Pavloveb57aa62014-06-20 17:08:28 +00001861 }
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001862 } else {
1863 OS << "[";
David Majnemerf7e36092016-06-23 00:15:04 +00001864 if (D.isArrayDesignator()) {
1865 PrintExpr(Node->getArrayIndex(D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001866 } else {
David Majnemerf7e36092016-06-23 00:15:04 +00001867 PrintExpr(Node->getArrayRangeStart(D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001868 OS << " ... ";
David Majnemerf7e36092016-06-23 00:15:04 +00001869 PrintExpr(Node->getArrayRangeEnd(D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001870 }
1871 OS << "]";
1872 }
1873 }
1874
Justin Bognercb337032015-05-28 22:19:36 +00001875 if (NeedsEquals)
1876 OS << " = ";
1877 else
1878 OS << " ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001879 PrintExpr(Node->getInit());
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001880}
1881
Yunzhong Gaocb779302015-06-10 00:27:52 +00001882void StmtPrinter::VisitDesignatedInitUpdateExpr(
1883 DesignatedInitUpdateExpr *Node) {
1884 OS << "{";
1885 OS << "/*base*/";
1886 PrintExpr(Node->getBase());
1887 OS << ", ";
1888
1889 OS << "/*updater*/";
1890 PrintExpr(Node->getUpdater());
1891 OS << "}";
1892}
1893
1894void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) {
1895 OS << "/*no init*/";
1896}
1897
Douglas Gregor0202cb42009-01-29 17:44:32 +00001898void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Richard Smith301bc212016-05-19 01:39:10 +00001899 if (Node->getType()->getAsCXXRecordDecl()) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001900 OS << "/*implicit*/";
1901 Node->getType().print(OS, Policy);
1902 OS << "()";
1903 } else {
1904 OS << "/*implicit*/(";
1905 Node->getType().print(OS, Policy);
1906 OS << ')';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001907 if (Node->getType()->isRecordType())
1908 OS << "{}";
1909 else
1910 OS << 0;
1911 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001912}
1913
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001914void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman79635842009-05-30 04:20:30 +00001915 OS << "__builtin_va_arg(";
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001916 PrintExpr(Node->getSubExpr());
1917 OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001918 Node->getType().print(OS, Policy);
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001919 OS << ")";
1920}
1921
John McCallfe96e0b2011-11-06 09:01:30 +00001922void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1923 PrintExpr(Node->getSyntacticForm());
1924}
1925
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001926void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
Craig Topper36250ad2014-05-12 05:36:57 +00001927 const char *Name = nullptr;
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001928 switch (Node->getOp()) {
Richard Smithfeea8832012-04-12 05:08:17 +00001929#define BUILTIN(ID, TYPE, ATTRS)
1930#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1931 case AtomicExpr::AO ## ID: \
1932 Name = #ID "("; \
1933 break;
1934#include "clang/Basic/Builtins.def"
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001935 }
1936 OS << Name;
Richard Smithfeea8832012-04-12 05:08:17 +00001937
1938 // AtomicExpr stores its subexpressions in a permuted order.
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001939 PrintExpr(Node->getPtr());
Richard Smithfeea8832012-04-12 05:08:17 +00001940 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
Yaxun Liu39195062017-08-04 18:16:31 +00001941 Node->getOp() != AtomicExpr::AO__atomic_load_n &&
1942 Node->getOp() != AtomicExpr::AO__opencl_atomic_load) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001943 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001944 PrintExpr(Node->getVal1());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001945 }
Richard Smithfeea8832012-04-12 05:08:17 +00001946 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1947 Node->isCmpXChg()) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001948 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001949 PrintExpr(Node->getVal2());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001950 }
Richard Smithfeea8832012-04-12 05:08:17 +00001951 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1952 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
Richard Smithfeea8832012-04-12 05:08:17 +00001953 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001954 PrintExpr(Node->getWeak());
Richard Smithfeea8832012-04-12 05:08:17 +00001955 }
Yaxun Liu39195062017-08-04 18:16:31 +00001956 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init &&
1957 Node->getOp() != AtomicExpr::AO__opencl_atomic_init) {
Richard Smithdf6bee82013-05-01 19:02:43 +00001958 OS << ", ";
David Chisnallfa35df62012-01-16 17:27:18 +00001959 PrintExpr(Node->getOrder());
Richard Smithdf6bee82013-05-01 19:02:43 +00001960 }
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001961 if (Node->isCmpXChg()) {
1962 OS << ", ";
1963 PrintExpr(Node->getOrderFail());
1964 }
1965 OS << ")";
1966}
1967
Chris Lattnereefa10e2007-05-28 06:56:27 +00001968// C++
Douglas Gregor993603d2008-11-14 16:09:21 +00001969void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1970 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1971 "",
1972#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1973 Spelling,
1974#include "clang/Basic/OperatorKinds.def"
1975 };
1976
1977 OverloadedOperatorKind Kind = Node->getOperator();
1978 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1979 if (Node->getNumArgs() == 1) {
1980 OS << OpStrings[Kind] << ' ';
1981 PrintExpr(Node->getArg(0));
1982 } else {
1983 PrintExpr(Node->getArg(0));
1984 OS << ' ' << OpStrings[Kind];
1985 }
Eli Friedman8e236422012-10-12 22:45:14 +00001986 } else if (Kind == OO_Arrow) {
1987 PrintExpr(Node->getArg(0));
Douglas Gregor993603d2008-11-14 16:09:21 +00001988 } else if (Kind == OO_Call) {
1989 PrintExpr(Node->getArg(0));
1990 OS << '(';
1991 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1992 if (ArgIdx > 1)
1993 OS << ", ";
1994 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1995 PrintExpr(Node->getArg(ArgIdx));
1996 }
1997 OS << ')';
1998 } else if (Kind == OO_Subscript) {
1999 PrintExpr(Node->getArg(0));
2000 OS << '[';
2001 PrintExpr(Node->getArg(1));
2002 OS << ']';
2003 } else if (Node->getNumArgs() == 1) {
2004 OS << OpStrings[Kind] << ' ';
2005 PrintExpr(Node->getArg(0));
2006 } else if (Node->getNumArgs() == 2) {
2007 PrintExpr(Node->getArg(0));
2008 OS << ' ' << OpStrings[Kind] << ' ';
2009 PrintExpr(Node->getArg(1));
2010 } else {
David Blaikie83d382b2011-09-23 05:06:16 +00002011 llvm_unreachable("unknown overloaded operator");
Douglas Gregor993603d2008-11-14 16:09:21 +00002012 }
2013}
Chris Lattnereefa10e2007-05-28 06:56:27 +00002014
Douglas Gregor97fd6e22008-12-22 05:46:06 +00002015void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
Benjamin Kramer00e8a192014-02-25 18:03:55 +00002016 // If we have a conversion operator call only print the argument.
2017 CXXMethodDecl *MD = Node->getMethodDecl();
2018 if (MD && isa<CXXConversionDecl>(MD)) {
2019 PrintExpr(Node->getImplicitObjectArgument());
2020 return;
2021 }
Douglas Gregor97fd6e22008-12-22 05:46:06 +00002022 VisitCallExpr(cast<CallExpr>(Node));
2023}
2024
Peter Collingbourne41f85462011-02-09 21:07:24 +00002025void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
2026 PrintExpr(Node->getCallee());
2027 OS << "<<<";
2028 PrintCallArgs(Node->getConfig());
2029 OS << ">>>(";
2030 PrintCallArgs(Node);
2031 OS << ")";
2032}
2033
Douglas Gregore200adc2008-10-27 19:41:14 +00002034void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
2035 OS << Node->getCastName() << '<';
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002036 Node->getTypeAsWritten().print(OS, Policy);
2037 OS << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +00002038 PrintExpr(Node->getSubExpr());
2039 OS << ")";
2040}
2041
Douglas Gregore200adc2008-10-27 19:41:14 +00002042void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
2043 VisitCXXNamedCastExpr(Node);
2044}
2045
2046void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
2047 VisitCXXNamedCastExpr(Node);
2048}
2049
2050void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
2051 VisitCXXNamedCastExpr(Node);
2052}
2053
2054void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
2055 VisitCXXNamedCastExpr(Node);
2056}
2057
Sebastian Redlc4704762008-11-11 11:37:55 +00002058void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
2059 OS << "typeid(";
2060 if (Node->isTypeOperand()) {
David Majnemer143c55e2013-09-27 07:04:31 +00002061 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
Sebastian Redlc4704762008-11-11 11:37:55 +00002062 } else {
2063 PrintExpr(Node->getExprOperand());
2064 }
2065 OS << ")";
2066}
2067
Francois Pichet9f4f2072010-09-08 12:20:18 +00002068void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
2069 OS << "__uuidof(";
2070 if (Node->isTypeOperand()) {
David Majnemer143c55e2013-09-27 07:04:31 +00002071 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
Francois Pichet9f4f2072010-09-08 12:20:18 +00002072 } else {
2073 PrintExpr(Node->getExprOperand());
2074 }
2075 OS << ")";
2076}
2077
John McCall5e77d762013-04-16 07:28:30 +00002078void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
2079 PrintExpr(Node->getBaseExpr());
2080 if (Node->isArrow())
2081 OS << "->";
2082 else
2083 OS << ".";
2084 if (NestedNameSpecifier *Qualifier =
2085 Node->getQualifierLoc().getNestedNameSpecifier())
2086 Qualifier->print(OS, Policy);
2087 OS << Node->getPropertyDecl()->getDeclName();
2088}
2089
Alexey Bataevf7630272015-11-25 12:01:00 +00002090void StmtPrinter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr *Node) {
2091 PrintExpr(Node->getBase());
2092 OS << "[";
2093 PrintExpr(Node->getIdx());
2094 OS << "]";
2095}
2096
Richard Smithc67fdd42012-03-07 08:35:16 +00002097void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
2098 switch (Node->getLiteralOperatorKind()) {
2099 case UserDefinedLiteral::LOK_Raw:
Richard Smith29e95952012-03-09 10:10:02 +00002100 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
Richard Smithc67fdd42012-03-07 08:35:16 +00002101 break;
2102 case UserDefinedLiteral::LOK_Template: {
Richard Smith29e95952012-03-09 10:10:02 +00002103 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
2104 const TemplateArgumentList *Args =
2105 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
2106 assert(Args);
David Majnemer314b72f2015-04-05 05:32:54 +00002107
2108 if (Args->size() != 1) {
Richard Smithe87aeb32015-10-08 00:17:59 +00002109 OS << "operator\"\"" << Node->getUDSuffix()->getName();
Serge Pavlov03e672c2017-11-28 16:14:14 +00002110 printTemplateArgumentList(OS, Args->asArray(), Policy);
David Majnemer314b72f2015-04-05 05:32:54 +00002111 OS << "()";
2112 return;
2113 }
2114
Richard Smith29e95952012-03-09 10:10:02 +00002115 const TemplateArgument &Pack = Args->get(0);
Aaron Ballman2a89e852014-07-15 21:32:31 +00002116 for (const auto &P : Pack.pack_elements()) {
2117 char C = (char)P.getAsIntegral().getZExtValue();
Richard Smithc67fdd42012-03-07 08:35:16 +00002118 OS << C;
2119 }
2120 break;
2121 }
Richard Smith75025ba2012-03-08 09:02:38 +00002122 case UserDefinedLiteral::LOK_Integer: {
2123 // Print integer literal without suffix.
2124 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
2125 OS << Int->getValue().toString(10, /*isSigned*/false);
2126 break;
2127 }
Benjamin Kramer8a526762012-09-20 14:07:17 +00002128 case UserDefinedLiteral::LOK_Floating: {
2129 // Print floating literal without suffix.
2130 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
2131 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
2132 break;
2133 }
Richard Smithc67fdd42012-03-07 08:35:16 +00002134 case UserDefinedLiteral::LOK_String:
2135 case UserDefinedLiteral::LOK_Character:
2136 PrintExpr(Node->getCookedLiteral());
2137 break;
2138 }
2139 OS << Node->getUDSuffix()->getName();
2140}
2141
Chris Lattnereefa10e2007-05-28 06:56:27 +00002142void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
2143 OS << (Node->getValue() ? "true" : "false");
2144}
2145
Sebastian Redl576fd422009-05-10 18:38:11 +00002146void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
2147 OS << "nullptr";
2148}
2149
Douglas Gregor97a9c812008-11-04 14:32:21 +00002150void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
2151 OS << "this";
2152}
2153
Chris Lattnerb7e656b2008-02-26 00:51:44 +00002154void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
Craig Topper36250ad2014-05-12 05:36:57 +00002155 if (!Node->getSubExpr())
Chris Lattnerb7e656b2008-02-26 00:51:44 +00002156 OS << "throw";
2157 else {
2158 OS << "throw ";
2159 PrintExpr(Node->getSubExpr());
2160 }
2161}
2162
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002163void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
Richard Smith852c9db2013-04-20 22:23:05 +00002164 // Nothing to print: we picked up the default argument.
2165}
2166
2167void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
2168 // Nothing to print: we picked up the default initializer.
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002169}
2170
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002171void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002172 Node->getType().print(OS, Policy);
Richard Smith1ae689c2015-01-28 22:06:01 +00002173 // If there are no parens, this is list-initialization, and the braces are
2174 // part of the syntax of the inner construct.
2175 if (Node->getLParenLoc().isValid())
2176 OS << "(";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002177 PrintExpr(Node->getSubExpr());
Richard Smith1ae689c2015-01-28 22:06:01 +00002178 if (Node->getLParenLoc().isValid())
2179 OS << ")";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002180}
2181
Anders Carlsson993a4b32009-05-30 20:03:25 +00002182void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
2183 PrintExpr(Node->getSubExpr());
2184}
2185
Douglas Gregordd04d332009-01-16 18:33:17 +00002186void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002187 Node->getType().print(OS, Policy);
Richard Smithed83ebd2015-02-05 07:02:11 +00002188 if (Node->isStdInitListInitialization())
2189 /* Nothing to do; braces are part of creating the std::initializer_list. */;
2190 else if (Node->isListInitialization())
Richard Smith1ae689c2015-01-28 22:06:01 +00002191 OS << "{";
2192 else
2193 OS << "(";
Douglas Gregordd04d332009-01-16 18:33:17 +00002194 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00002195 ArgEnd = Node->arg_end();
Douglas Gregordd04d332009-01-16 18:33:17 +00002196 Arg != ArgEnd; ++Arg) {
Benjamin Kramerf48ee442015-07-18 14:35:53 +00002197 if ((*Arg)->isDefaultArgument())
Rafael Espindola6f6f3c42013-05-24 16:11:44 +00002198 break;
Douglas Gregordd04d332009-01-16 18:33:17 +00002199 if (Arg != Node->arg_begin())
2200 OS << ", ";
2201 PrintExpr(*Arg);
2202 }
Richard Smithed83ebd2015-02-05 07:02:11 +00002203 if (Node->isStdInitListInitialization())
2204 /* See above. */;
2205 else if (Node->isListInitialization())
Richard Smith1ae689c2015-01-28 22:06:01 +00002206 OS << "}";
2207 else
2208 OS << ")";
Douglas Gregordd04d332009-01-16 18:33:17 +00002209}
2210
Douglas Gregore31e6062012-02-07 10:09:13 +00002211void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
2212 OS << '[';
2213 bool NeedComma = false;
2214 switch (Node->getCaptureDefault()) {
2215 case LCD_None:
2216 break;
2217
2218 case LCD_ByCopy:
2219 OS << '=';
2220 NeedComma = true;
2221 break;
2222
2223 case LCD_ByRef:
2224 OS << '&';
2225 NeedComma = true;
2226 break;
2227 }
2228 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
2229 CEnd = Node->explicit_capture_end();
2230 C != CEnd;
2231 ++C) {
Richard Trieu931638e2017-11-11 00:54:25 +00002232 if (C->capturesVLAType())
2233 continue;
2234
Douglas Gregore31e6062012-02-07 10:09:13 +00002235 if (NeedComma)
2236 OS << ", ";
2237 NeedComma = true;
2238
2239 switch (C->getCaptureKind()) {
2240 case LCK_This:
2241 OS << "this";
2242 break;
Faisal Validc6b5962016-03-21 09:25:37 +00002243 case LCK_StarThis:
2244 OS << "*this";
2245 break;
Douglas Gregore31e6062012-02-07 10:09:13 +00002246 case LCK_ByRef:
James Dennettdd2ffea22015-05-07 18:48:18 +00002247 if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C))
Douglas Gregore31e6062012-02-07 10:09:13 +00002248 OS << '&';
2249 OS << C->getCapturedVar()->getName();
2250 break;
2251
2252 case LCK_ByCopy:
Douglas Gregore31e6062012-02-07 10:09:13 +00002253 OS << C->getCapturedVar()->getName();
2254 break;
Alexey Bataev39c81e22014-08-28 04:28:19 +00002255 case LCK_VLAType:
2256 llvm_unreachable("VLA type in explicit captures.");
Douglas Gregore31e6062012-02-07 10:09:13 +00002257 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00002258
James Dennettdd2ffea22015-05-07 18:48:18 +00002259 if (Node->isInitCapture(C))
Richard Smithbb13c9a2013-09-28 04:02:39 +00002260 PrintExpr(C->getCapturedVar()->getInit());
Douglas Gregore31e6062012-02-07 10:09:13 +00002261 }
2262 OS << ']';
2263
2264 if (Node->hasExplicitParameters()) {
2265 OS << " (";
2266 CXXMethodDecl *Method = Node->getCallOperator();
2267 NeedComma = false;
David Majnemer59f77922016-06-24 04:05:48 +00002268 for (auto P : Method->parameters()) {
Douglas Gregore31e6062012-02-07 10:09:13 +00002269 if (NeedComma) {
2270 OS << ", ";
2271 } else {
2272 NeedComma = true;
2273 }
Aaron Ballman43b68be2014-03-07 17:50:17 +00002274 std::string ParamStr = P->getNameAsString();
2275 P->getOriginalType().print(OS, Policy, ParamStr);
Douglas Gregore31e6062012-02-07 10:09:13 +00002276 }
2277 if (Method->isVariadic()) {
2278 if (NeedComma)
2279 OS << ", ";
2280 OS << "...";
2281 }
2282 OS << ')';
2283
2284 if (Node->isMutable())
2285 OS << " mutable";
2286
2287 const FunctionProtoType *Proto
2288 = Method->getType()->getAs<FunctionProtoType>();
Benjamin Kramer9170e912013-02-22 15:46:01 +00002289 Proto->printExceptionSpecification(OS, Policy);
Douglas Gregore31e6062012-02-07 10:09:13 +00002290
Bill Wendling44426052012-12-20 19:22:21 +00002291 // FIXME: Attributes
Douglas Gregore31e6062012-02-07 10:09:13 +00002292
Douglas Gregor0c46b2b2012-02-13 22:00:16 +00002293 // Print the trailing return type if it was specified in the source.
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002294 if (Node->hasExplicitResultType()) {
2295 OS << " -> ";
Alp Toker314cc812014-01-25 16:55:45 +00002296 Proto->getReturnType().print(OS, Policy);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002297 }
Douglas Gregore31e6062012-02-07 10:09:13 +00002298 }
2299
2300 // Print the body.
2301 CompoundStmt *Body = Node->getBody();
2302 OS << ' ';
2303 PrintStmt(Body);
2304}
2305
Douglas Gregor747eb782010-07-08 06:14:04 +00002306void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregor2b88c112010-09-08 00:15:04 +00002307 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002308 TSInfo->getType().print(OS, Policy);
Douglas Gregor2b88c112010-09-08 00:15:04 +00002309 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002310 Node->getType().print(OS, Policy);
2311 OS << "()";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00002312}
2313
Sebastian Redlbd150f42008-11-21 19:14:01 +00002314void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
2315 if (E->isGlobalNew())
2316 OS << "::";
2317 OS << "new ";
2318 unsigned NumPlace = E->getNumPlacementArgs();
Eli Friedmana6fdfaa2012-10-18 20:54:37 +00002319 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002320 OS << "(";
2321 PrintExpr(E->getPlacementArg(0));
2322 for (unsigned i = 1; i < NumPlace; ++i) {
Eli Friedmana6fdfaa2012-10-18 20:54:37 +00002323 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
2324 break;
Sebastian Redlbd150f42008-11-21 19:14:01 +00002325 OS << ", ";
2326 PrintExpr(E->getPlacementArg(i));
2327 }
2328 OS << ") ";
2329 }
2330 if (E->isParenTypeId())
2331 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00002332 std::string TypeS;
2333 if (Expr *Size = E->getArraySize()) {
2334 llvm::raw_string_ostream s(TypeS);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002335 s << '[';
Richard Smith235341b2012-08-16 03:56:14 +00002336 Size->printPretty(s, Helper, Policy);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002337 s << ']';
Sebastian Redld6d55ee2008-12-02 22:08:59 +00002338 }
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002339 E->getAllocatedType().print(OS, Policy, TypeS);
Sebastian Redlbd150f42008-11-21 19:14:01 +00002340 if (E->isParenTypeId())
2341 OS << ")";
2342
Sebastian Redl6047f072012-02-16 12:22:20 +00002343 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
2344 if (InitStyle) {
2345 if (InitStyle == CXXNewExpr::CallInit)
2346 OS << "(";
2347 PrintExpr(E->getInitializer());
2348 if (InitStyle == CXXNewExpr::CallInit)
2349 OS << ")";
Sebastian Redlbd150f42008-11-21 19:14:01 +00002350 }
2351}
2352
2353void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
2354 if (E->isGlobalDelete())
2355 OS << "::";
2356 OS << "delete ";
2357 if (E->isArrayForm())
2358 OS << "[] ";
2359 PrintExpr(E->getArgument());
2360}
2361
Douglas Gregorad8a3362009-09-04 17:36:40 +00002362void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
2363 PrintExpr(E->getBase());
2364 if (E->isArrow())
2365 OS << "->";
2366 else
2367 OS << '.';
2368 if (E->getQualifier())
2369 E->getQualifier()->print(OS, Policy);
Eli Friedman9cc8ac52012-10-23 20:26:57 +00002370 OS << "~";
Mike Stump11289f42009-09-09 15:08:12 +00002371
Douglas Gregor678f90d2010-02-25 01:56:36 +00002372 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
2373 OS << II->getName();
2374 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002375 E->getDestroyedType().print(OS, Policy);
Douglas Gregorad8a3362009-09-04 17:36:40 +00002376}
2377
Anders Carlsson0781ce72009-04-23 02:32:43 +00002378void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Richard Smithed83ebd2015-02-05 07:02:11 +00002379 if (E->isListInitialization() && !E->isStdInitListInitialization())
Richard Smithd0e102f2015-01-30 02:04:26 +00002380 OS << "{";
Richard Smithd59b8322012-12-19 01:39:02 +00002381
Douglas Gregorbaba85d2011-01-24 17:25:03 +00002382 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
2383 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
2384 // Don't print any defaulted arguments
2385 break;
2386 }
2387
2388 if (i) OS << ", ";
2389 PrintExpr(E->getArg(i));
Fariborz Jahaniand0bbf662010-01-13 21:41:11 +00002390 }
Richard Smithd59b8322012-12-19 01:39:02 +00002391
Richard Smithed83ebd2015-02-05 07:02:11 +00002392 if (E->isListInitialization() && !E->isStdInitListInitialization())
Richard Smithd0e102f2015-01-30 02:04:26 +00002393 OS << "}";
Anders Carlsson0781ce72009-04-23 02:32:43 +00002394}
2395
Richard Smith5179eb72016-06-28 19:03:57 +00002396void StmtPrinter::VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E) {
2397 // Parens are printed by the surrounding context.
2398 OS << "<forwarded>";
2399}
2400
Richard Smithcc1b96d2013-06-12 22:31:48 +00002401void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
2402 PrintExpr(E->getSubExpr());
2403}
2404
John McCall5d413782010-12-06 08:20:24 +00002405void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Alp Toker028ed912013-12-06 17:56:43 +00002406 // Just forward to the subexpression.
Anders Carlssondefc6442009-04-24 22:47:04 +00002407 PrintExpr(E->getSubExpr());
2408}
2409
Mike Stump11289f42009-09-09 15:08:12 +00002410void
Douglas Gregorce934142009-05-20 18:46:25 +00002411StmtPrinter::VisitCXXUnresolvedConstructExpr(
2412 CXXUnresolvedConstructExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002413 Node->getTypeAsWritten().print(OS, Policy);
Douglas Gregorce934142009-05-20 18:46:25 +00002414 OS << "(";
2415 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00002416 ArgEnd = Node->arg_end();
Douglas Gregorce934142009-05-20 18:46:25 +00002417 Arg != ArgEnd; ++Arg) {
2418 if (Arg != Node->arg_begin())
2419 OS << ", ";
2420 PrintExpr(*Arg);
2421 }
2422 OS << ")";
2423}
2424
John McCall8cd78132009-11-19 22:55:06 +00002425void StmtPrinter::VisitCXXDependentScopeMemberExpr(
2426 CXXDependentScopeMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00002427 if (!Node->isImplicitAccess()) {
2428 PrintExpr(Node->getBase());
2429 OS << (Node->isArrow() ? "->" : ".");
2430 }
Douglas Gregorc26e0f62009-09-03 16:14:30 +00002431 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2432 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002433 if (Node->hasTemplateKeyword())
Douglas Gregor308047d2009-09-09 00:23:06 +00002434 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002435 OS << Node->getMemberNameInfo();
Benjamin Kramer9170e912013-02-22 15:46:01 +00002436 if (Node->hasExplicitTemplateArgs())
Serge Pavlov03e672c2017-11-28 16:14:14 +00002437 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
Douglas Gregora8db9542009-05-22 21:13:27 +00002438}
2439
John McCall10eae182009-11-30 22:42:35 +00002440void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00002441 if (!Node->isImplicitAccess()) {
2442 PrintExpr(Node->getBase());
2443 OS << (Node->isArrow() ? "->" : ".");
2444 }
John McCall10eae182009-11-30 22:42:35 +00002445 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2446 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002447 if (Node->hasTemplateKeyword())
2448 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002449 OS << Node->getMemberNameInfo();
Benjamin Kramer9170e912013-02-22 15:46:01 +00002450 if (Node->hasExplicitTemplateArgs())
Serge Pavlov03e672c2017-11-28 16:14:14 +00002451 printTemplateArgumentList(OS, Node->template_arguments(), Policy);
John McCall10eae182009-11-30 22:42:35 +00002452}
2453
Douglas Gregor29c42f22012-02-24 07:38:34 +00002454static const char *getTypeTraitName(TypeTrait TT) {
2455 switch (TT) {
Alp Toker95e7ff22014-01-01 05:57:51 +00002456#define TYPE_TRAIT_1(Spelling, Name, Key) \
2457case clang::UTT_##Name: return #Spelling;
Alp Tokercbb90342013-12-13 20:49:58 +00002458#define TYPE_TRAIT_2(Spelling, Name, Key) \
2459case clang::BTT_##Name: return #Spelling;
Alp Toker40f9b1c2013-12-12 21:23:03 +00002460#define TYPE_TRAIT_N(Spelling, Name, Key) \
2461 case clang::TT_##Name: return #Spelling;
2462#include "clang/Basic/TokenKinds.def"
Douglas Gregor29c42f22012-02-24 07:38:34 +00002463 }
2464 llvm_unreachable("Type trait not covered by switch");
2465}
2466
John Wiegley6242b6a2011-04-28 00:16:57 +00002467static const char *getTypeTraitName(ArrayTypeTrait ATT) {
2468 switch (ATT) {
2469 case ATT_ArrayRank: return "__array_rank";
2470 case ATT_ArrayExtent: return "__array_extent";
2471 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00002472 llvm_unreachable("Array type trait not covered by switch");
John Wiegley6242b6a2011-04-28 00:16:57 +00002473}
2474
John Wiegleyf9f65842011-04-25 06:54:41 +00002475static const char *getExpressionTraitName(ExpressionTrait ET) {
2476 switch (ET) {
John Wiegleyf9f65842011-04-25 06:54:41 +00002477 case ET_IsLValueExpr: return "__is_lvalue_expr";
2478 case ET_IsRValueExpr: return "__is_rvalue_expr";
2479 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00002480 llvm_unreachable("Expression type trait not covered by switch");
John Wiegleyf9f65842011-04-25 06:54:41 +00002481}
2482
Douglas Gregor29c42f22012-02-24 07:38:34 +00002483void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
2484 OS << getTypeTraitName(E->getTrait()) << "(";
2485 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
2486 if (I > 0)
2487 OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002488 E->getArg(I)->getType().print(OS, Policy);
Douglas Gregor29c42f22012-02-24 07:38:34 +00002489 }
2490 OS << ")";
2491}
2492
John Wiegley6242b6a2011-04-28 00:16:57 +00002493void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002494 OS << getTypeTraitName(E->getTrait()) << '(';
2495 E->getQueriedType().print(OS, Policy);
2496 OS << ')';
John Wiegley6242b6a2011-04-28 00:16:57 +00002497}
2498
John Wiegleyf9f65842011-04-25 06:54:41 +00002499void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002500 OS << getExpressionTraitName(E->getTrait()) << '(';
2501 PrintExpr(E->getQueriedExpression());
2502 OS << ')';
John Wiegleyf9f65842011-04-25 06:54:41 +00002503}
2504
Sebastian Redl4202c0f2010-09-10 20:55:43 +00002505void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
2506 OS << "noexcept(";
2507 PrintExpr(E->getOperand());
2508 OS << ")";
2509}
2510
Douglas Gregor820ba7b2011-01-04 17:33:58 +00002511void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregore8e9dd62011-01-03 17:17:50 +00002512 PrintExpr(E->getPattern());
2513 OS << "...";
2514}
2515
Douglas Gregor820ba7b2011-01-04 17:33:58 +00002516void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00002517 OS << "sizeof...(" << *E->getPack() << ")";
Douglas Gregor820ba7b2011-01-04 17:33:58 +00002518}
2519
Douglas Gregorcdbc5392011-01-15 01:15:58 +00002520void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
2521 SubstNonTypeTemplateParmPackExpr *Node) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00002522 OS << *Node->getParameterPack();
Douglas Gregorcdbc5392011-01-15 01:15:58 +00002523}
2524
John McCall7c454bb2011-07-15 05:09:51 +00002525void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
2526 SubstNonTypeTemplateParmExpr *Node) {
2527 Visit(Node->getReplacement());
2528}
2529
Richard Smithb15fe3a2012-09-12 00:56:43 +00002530void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
2531 OS << *E->getParameterPack();
2532}
2533
Douglas Gregorfe314812011-06-21 17:03:29 +00002534void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
2535 PrintExpr(Node->GetTemporaryExpr());
2536}
2537
Richard Smith0f0af192014-11-08 05:07:16 +00002538void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
2539 OS << "(";
2540 if (E->getLHS()) {
2541 PrintExpr(E->getLHS());
2542 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2543 }
2544 OS << "...";
2545 if (E->getRHS()) {
2546 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2547 PrintExpr(E->getRHS());
2548 }
2549 OS << ")";
2550}
2551
Richard Smith9f690bd2015-10-27 06:02:45 +00002552// C++ Coroutines TS
2553
2554void StmtPrinter::VisitCoroutineBodyStmt(CoroutineBodyStmt *S) {
2555 Visit(S->getBody());
2556}
2557
2558void StmtPrinter::VisitCoreturnStmt(CoreturnStmt *S) {
2559 OS << "co_return";
2560 if (S->getOperand()) {
2561 OS << " ";
2562 Visit(S->getOperand());
2563 }
2564 OS << ";";
2565}
2566
2567void StmtPrinter::VisitCoawaitExpr(CoawaitExpr *S) {
2568 OS << "co_await ";
2569 PrintExpr(S->getOperand());
2570}
2571
Eric Fiselier20f25cb2017-03-06 23:38:15 +00002572
2573void StmtPrinter::VisitDependentCoawaitExpr(DependentCoawaitExpr *S) {
2574 OS << "co_await ";
2575 PrintExpr(S->getOperand());
2576}
2577
2578
Richard Smith9f690bd2015-10-27 06:02:45 +00002579void StmtPrinter::VisitCoyieldExpr(CoyieldExpr *S) {
2580 OS << "co_yield ";
2581 PrintExpr(S->getOperand());
2582}
2583
Mike Stump11289f42009-09-09 15:08:12 +00002584// Obj-C
Anders Carlsson76f4a902007-08-21 17:43:55 +00002585
2586void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
2587 OS << "@";
2588 VisitStringLiteral(Node->getString());
2589}
Chris Lattnereefa10e2007-05-28 06:56:27 +00002590
Patrick Beard0caa3942012-04-19 00:25:12 +00002591void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00002592 OS << "@";
Patrick Beard0caa3942012-04-19 00:25:12 +00002593 Visit(E->getSubExpr());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002594}
2595
2596void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
2597 OS << "@[ ";
Benjamin Kramer5733e352015-07-18 17:09:36 +00002598 ObjCArrayLiteral::child_range Ch = E->children();
2599 for (auto I = Ch.begin(), E = Ch.end(); I != E; ++I) {
2600 if (I != Ch.begin())
Ted Kremeneke65b0862012-03-06 20:05:56 +00002601 OS << ", ";
Benjamin Kramer5733e352015-07-18 17:09:36 +00002602 Visit(*I);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002603 }
2604 OS << " ]";
2605}
2606
2607void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
2608 OS << "@{ ";
2609 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
2610 if (I > 0)
2611 OS << ", ";
2612
2613 ObjCDictionaryElement Element = E->getKeyValueElement(I);
2614 Visit(Element.Key);
2615 OS << " : ";
2616 Visit(Element.Value);
2617 if (Element.isPackExpansion())
2618 OS << "...";
2619 }
2620 OS << " }";
2621}
2622
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002623void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002624 OS << "@encode(";
2625 Node->getEncodedType().print(OS, Policy);
2626 OS << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002627}
2628
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002629void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00002630 OS << "@selector(";
2631 Node->getSelector().print(OS);
2632 OS << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002633}
2634
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002635void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00002636 OS << "@protocol(" << *Node->getProtocol() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002637}
2638
Steve Naroffd54978b2007-09-18 23:55:05 +00002639void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
2640 OS << "[";
Douglas Gregor9a129192010-04-21 00:45:42 +00002641 switch (Mess->getReceiverKind()) {
2642 case ObjCMessageExpr::Instance:
2643 PrintExpr(Mess->getInstanceReceiver());
2644 break;
2645
2646 case ObjCMessageExpr::Class:
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002647 Mess->getClassReceiver().print(OS, Policy);
Douglas Gregor9a129192010-04-21 00:45:42 +00002648 break;
2649
2650 case ObjCMessageExpr::SuperInstance:
2651 case ObjCMessageExpr::SuperClass:
2652 OS << "Super";
2653 break;
2654 }
2655
Ted Kremeneka06e7122008-05-02 17:32:38 +00002656 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00002657 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00002658 if (selector.isUnarySelector()) {
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00002659 OS << selector.getNameForSlot(0);
Steve Naroffc6814ea2007-10-02 20:01:56 +00002660 } else {
2661 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00002662 if (i < selector.getNumArgs()) {
2663 if (i > 0) OS << ' ';
2664 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00002665 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00002666 else
2667 OS << ":";
2668 }
2669 else OS << ", "; // Handle variadic methods.
Mike Stump11289f42009-09-09 15:08:12 +00002670
Steve Naroffc6814ea2007-10-02 20:01:56 +00002671 PrintExpr(Mess->getArg(i));
2672 }
Steve Naroffd54978b2007-09-18 23:55:05 +00002673 }
2674 OS << "]";
2675}
2676
Ted Kremeneke65b0862012-03-06 20:05:56 +00002677void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
2678 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
2679}
2680
John McCall31168b02011-06-15 23:02:42 +00002681void
2682StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
2683 PrintExpr(E->getSubExpr());
2684}
2685
2686void
2687StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002688 OS << '(' << E->getBridgeKindName();
2689 E->getType().print(OS, Policy);
2690 OS << ')';
John McCall31168b02011-06-15 23:02:42 +00002691 PrintExpr(E->getSubExpr());
2692}
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002693
Steve Naroffc540d662008-09-03 18:15:37 +00002694void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00002695 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00002696 OS << "^";
Mike Stump11289f42009-09-09 15:08:12 +00002697
Steve Naroffc540d662008-09-03 18:15:37 +00002698 const FunctionType *AFT = Node->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00002699
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002700 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroffc540d662008-09-03 18:15:37 +00002701 OS << "()";
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002702 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00002703 OS << '(';
Steve Naroff415d3d52008-10-08 17:01:13 +00002704 for (BlockDecl::param_iterator AI = BD->param_begin(),
2705 E = BD->param_end(); AI != E; ++AI) {
2706 if (AI != BD->param_begin()) OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002707 std::string ParamStr = (*AI)->getNameAsString();
2708 (*AI)->getType().print(OS, Policy, ParamStr);
Steve Naroffc540d662008-09-03 18:15:37 +00002709 }
Mike Stump11289f42009-09-09 15:08:12 +00002710
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002711 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00002712 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00002713 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00002714 OS << "...";
2715 }
2716 OS << ')';
2717 }
Fariborz Jahanian1ace8cb2012-12-04 21:15:23 +00002718 OS << "{ }";
Steve Naroffc540d662008-09-03 18:15:37 +00002719}
2720
Ted Kremenekf551f322011-11-30 22:08:08 +00002721void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
2722 PrintExpr(Node->getSourceExpr());
2723}
John McCall8d69a212010-11-15 23:31:06 +00002724
Kaelyn Takatae1f49d52014-10-27 18:07:20 +00002725void StmtPrinter::VisitTypoExpr(TypoExpr *Node) {
2726 // TODO: Print something reasonable for a TypoExpr, if necessary.
Davide Italiano04839a52016-01-30 08:03:54 +00002727 llvm_unreachable("Cannot print TypoExpr nodes");
Kaelyn Takatae1f49d52014-10-27 18:07:20 +00002728}
2729
Tanya Lattner55808c12011-06-04 00:47:47 +00002730void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
2731 OS << "__builtin_astype(";
2732 PrintExpr(Node->getSrcExpr());
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002733 OS << ", ";
2734 Node->getType().print(OS, Policy);
Tanya Lattner55808c12011-06-04 00:47:47 +00002735 OS << ")";
2736}
2737
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00002738//===----------------------------------------------------------------------===//
2739// Stmt method implementations
2740//===----------------------------------------------------------------------===//
2741
Craig Topperc571c812013-08-22 06:02:26 +00002742void Stmt::dumpPretty(const ASTContext &Context) const {
Craig Topper36250ad2014-05-12 05:36:57 +00002743 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00002744}
2745
Alex Lorenz36070ed2017-08-17 13:41:55 +00002746void Stmt::printPretty(raw_ostream &OS, PrinterHelper *Helper,
2747 const PrintingPolicy &Policy, unsigned Indentation,
2748 const ASTContext *Context) const {
2749 StmtPrinter P(OS, Helper, Policy, Indentation, Context);
Chris Lattner62249a62007-08-21 04:04:25 +00002750 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00002751}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002752
2753//===----------------------------------------------------------------------===//
2754// PrinterHelper
2755//===----------------------------------------------------------------------===//
2756
2757// Implement virtual destructor.
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +00002758PrinterHelper::~PrinterHelper() {}