blob: 64f90ba951d89d939e83ccf480e37ef3329ff537 [file] [log] [blame]
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001//===--- StmtPrinter.cpp - Printing implementation for Stmt ASTs ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnercbe4f772007-08-08 22:51:59 +000010// This file implements the Stmt::dumpPretty/Stmt::printPretty methods, which
11// pretty print the AST back out to C code.
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000012//
13//===----------------------------------------------------------------------===//
14
Benjamin Kramer4ab984e2012-07-04 20:19:54 +000015#include "clang/AST/ASTContext.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000016#include "clang/AST/Attr.h"
Douglas Gregorc7acfdf2009-01-06 05:10:23 +000017#include "clang/AST/DeclCXX.h"
Ted Kremenek5c84c012007-10-17 18:36:42 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregorcdbc5392011-01-15 01:15:58 +000019#include "clang/AST/DeclTemplate.h"
Douglas Gregor882211c2010-04-28 22:16:22 +000020#include "clang/AST/Expr.h"
Douglas Gregor2b88c112010-09-08 00:15:04 +000021#include "clang/AST/ExprCXX.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000022#include "clang/AST/PrettyPrinter.h"
23#include "clang/AST/StmtVisitor.h"
Jordan Rose00d1b592013-02-08 22:30:27 +000024#include "clang/Basic/CharInfo.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000025#include "llvm/ADT/SmallString.h"
Jordan Rose00d1b592013-02-08 22:30:27 +000026#include "llvm/Support/Format.h"
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000027using namespace clang;
28
29//===----------------------------------------------------------------------===//
30// StmtPrinter Visitor
31//===----------------------------------------------------------------------===//
32
33namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +000034 class StmtPrinter : public StmtVisitor<StmtPrinter> {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000035 raw_ostream &OS;
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000036 unsigned IndentLevel;
Ted Kremenek04f3cee2007-08-31 21:30:12 +000037 clang::PrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +000038 PrintingPolicy Policy;
39
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000040 public:
Richard Smith235341b2012-08-16 03:56:14 +000041 StmtPrinter(raw_ostream &os, PrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +000042 const PrintingPolicy &Policy,
Douglas Gregor7de59662009-05-29 20:38:28 +000043 unsigned Indentation = 0)
Richard Smith235341b2012-08-16 03:56:14 +000044 : OS(os), IndentLevel(Indentation), Helper(helper), Policy(Policy) {}
Mike Stump11289f42009-09-09 15:08:12 +000045
Douglas Gregor7de59662009-05-29 20:38:28 +000046 void PrintStmt(Stmt *S) {
47 PrintStmt(S, Policy.Indentation);
48 }
49
50 void PrintStmt(Stmt *S, int SubIndent) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000051 IndentLevel += SubIndent;
Chris Lattnera076fde2007-05-31 18:21:33 +000052 if (S && isa<Expr>(S)) {
Chris Lattner882f7882006-11-04 18:52:07 +000053 // If this is an expr used in a stmt context, indent and newline it.
54 Indent();
Chris Lattner62249a62007-08-21 04:04:25 +000055 Visit(S);
Chris Lattnerfc068c12007-05-30 17:57:36 +000056 OS << ";\n";
Chris Lattner882f7882006-11-04 18:52:07 +000057 } else if (S) {
Chris Lattner62249a62007-08-21 04:04:25 +000058 Visit(S);
Chris Lattner882f7882006-11-04 18:52:07 +000059 } else {
Chris Lattner2f6ac262007-05-28 01:47:47 +000060 Indent() << "<<<NULL STATEMENT>>>\n";
Chris Lattner882f7882006-11-04 18:52:07 +000061 }
Chris Lattnerb9eb5a12007-05-20 22:52:15 +000062 IndentLevel -= SubIndent;
Chris Lattner882f7882006-11-04 18:52:07 +000063 }
Eli Friedman15ea8802009-05-30 00:19:54 +000064
Chris Lattner073926e2007-05-20 23:04:55 +000065 void PrintRawCompoundStmt(CompoundStmt *S);
Chris Lattnerfdc195a2007-06-05 20:52:47 +000066 void PrintRawDecl(Decl *D);
Eli Friedmanf86e5072012-10-16 23:45:15 +000067 void PrintRawDeclStmt(const DeclStmt *S);
Chris Lattnerc0a38dd2007-06-11 22:26:23 +000068 void PrintRawIfStmt(IfStmt *If);
Sebastian Redl9b244a82008-12-22 21:35:02 +000069 void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
Peter Collingbourne4b279a02011-02-08 21:17:54 +000070 void PrintCallArgs(CallExpr *E);
John Wiegley1c0675e2011-04-28 01:08:34 +000071 void PrintRawSEHExceptHandler(SEHExceptStmt *S);
72 void PrintRawSEHFinallyStmt(SEHFinallyStmt *S);
Alexey Bataev1b59ab52014-02-27 08:29:12 +000073 void PrintOMPExecutableDirective(OMPExecutableDirective *S);
Mike Stump11289f42009-09-09 15:08:12 +000074
Chris Lattner882f7882006-11-04 18:52:07 +000075 void PrintExpr(Expr *E) {
76 if (E)
Chris Lattner62249a62007-08-21 04:04:25 +000077 Visit(E);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000078 else
Chris Lattner882f7882006-11-04 18:52:07 +000079 OS << "<null expr>";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000080 }
Mike Stump11289f42009-09-09 15:08:12 +000081
Chris Lattner0e62c1c2011-07-23 10:55:15 +000082 raw_ostream &Indent(int Delta = 0) {
Douglas Gregor7de59662009-05-29 20:38:28 +000083 for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
84 OS << " ";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +000085 return OS;
86 }
Mike Stump11289f42009-09-09 15:08:12 +000087
Mike Stump11289f42009-09-09 15:08:12 +000088 void Visit(Stmt* S) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +000089 if (Helper && Helper->handledStmt(S,OS))
90 return;
91 else StmtVisitor<StmtPrinter>::Visit(S);
92 }
Alexey Bataev1b59ab52014-02-27 08:29:12 +000093
Chandler Carruthb7967b92010-10-23 08:21:37 +000094 void VisitStmt(Stmt *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +000095 Indent() << "<<unknown stmt type>>\n";
96 }
Chandler Carruthb7967b92010-10-23 08:21:37 +000097 void VisitExpr(Expr *Node) LLVM_ATTRIBUTE_UNUSED {
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +000098 OS << "<<unknown expr type>>";
99 }
100 void VisitCXXNamedCastExpr(CXXNamedCastExpr *Node);
Mike Stump11289f42009-09-09 15:08:12 +0000101
Argyrios Kyrtzidis30805532010-08-15 01:15:33 +0000102#define ABSTRACT_STMT(CLASS)
Douglas Gregorbe35ce92008-11-14 12:46:07 +0000103#define STMT(CLASS, PARENT) \
Chris Lattner62249a62007-08-21 04:04:25 +0000104 void Visit##CLASS(CLASS *Node);
Alexis Hunt656bb312010-05-05 15:24:00 +0000105#include "clang/AST/StmtNodes.inc"
Chris Lattner71e23ce2006-11-04 20:18:38 +0000106 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000107}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000108
Chris Lattner71e23ce2006-11-04 20:18:38 +0000109//===----------------------------------------------------------------------===//
110// Stmt printing methods.
111//===----------------------------------------------------------------------===//
112
Chris Lattner073926e2007-05-20 23:04:55 +0000113/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
114/// with no newline after the }.
115void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
116 OS << "{\n";
Aaron Ballmanc7e4e212014-03-17 14:19:37 +0000117 for (auto *I : Node->body())
118 PrintStmt(I);
Mike Stump11289f42009-09-09 15:08:12 +0000119
Chris Lattner073926e2007-05-20 23:04:55 +0000120 Indent() << "}";
121}
122
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000123void StmtPrinter::PrintRawDecl(Decl *D) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000124 D->print(OS, Policy, IndentLevel);
Mike Stump74a76472009-02-10 20:16:46 +0000125}
126
Eli Friedmanf86e5072012-10-16 23:45:15 +0000127void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
Aaron Ballman535bbcc2014-03-14 17:01:24 +0000128 SmallVector<Decl*, 2> Decls(S->decls());
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000129 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
Ted Kremenek15e6b402008-10-06 18:39:36 +0000130}
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000131
132void StmtPrinter::VisitNullStmt(NullStmt *Node) {
133 Indent() << ";\n";
134}
135
136void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Eli Friedman15ea8802009-05-30 00:19:54 +0000137 Indent();
138 PrintRawDeclStmt(Node);
139 OS << ";\n";
Steve Naroff2a8ad182007-05-29 22:59:26 +0000140}
141
Chris Lattner073926e2007-05-20 23:04:55 +0000142void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
143 Indent();
144 PrintRawCompoundStmt(Node);
Chris Lattnerdf3cafb2007-05-31 05:08:56 +0000145 OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000146}
147
Chris Lattner6c0ff132006-11-05 00:19:50 +0000148void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000149 Indent(-1) << "case ";
Chris Lattner6c0ff132006-11-05 00:19:50 +0000150 PrintExpr(Node->getLHS());
151 if (Node->getRHS()) {
152 OS << " ... ";
153 PrintExpr(Node->getRHS());
154 }
155 OS << ":\n";
Mike Stump11289f42009-09-09 15:08:12 +0000156
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000157 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000158}
159
160void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000161 Indent(-1) << "default:\n";
162 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000163}
164
165void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000166 Indent(-1) << Node->getName() << ":\n";
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000167 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000168}
169
Richard Smithc202b282012-04-14 00:33:13 +0000170void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
Aaron Ballmanb06b15a2014-06-06 12:40:24 +0000171 for (const auto *Attr : Node->getAttrs()) {
Tyler Nowickie8b07ed2014-06-13 17:57:25 +0000172 Attr->printPretty(OS, Policy);
Aaron Ballmanb06b15a2014-06-06 12:40:24 +0000173 }
174
Richard Smithc202b282012-04-14 00:33:13 +0000175 PrintStmt(Node->getSubStmt(), 0);
176}
177
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000178void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000179 OS << "if (";
Eli Friedmanf86e5072012-10-16 23:45:15 +0000180 if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
181 PrintRawDeclStmt(DS);
182 else
183 PrintExpr(If->getCond());
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000184 OS << ')';
Mike Stump11289f42009-09-09 15:08:12 +0000185
Chris Lattner073926e2007-05-20 23:04:55 +0000186 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
187 OS << ' ';
188 PrintRawCompoundStmt(CS);
189 OS << (If->getElse() ? ' ' : '\n');
190 } else {
191 OS << '\n';
192 PrintStmt(If->getThen());
193 if (If->getElse()) Indent();
194 }
Mike Stump11289f42009-09-09 15:08:12 +0000195
Chris Lattner073926e2007-05-20 23:04:55 +0000196 if (Stmt *Else = If->getElse()) {
197 OS << "else";
Mike Stump11289f42009-09-09 15:08:12 +0000198
Chris Lattner073926e2007-05-20 23:04:55 +0000199 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
200 OS << ' ';
201 PrintRawCompoundStmt(CS);
202 OS << '\n';
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000203 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
204 OS << ' ';
205 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000206 } else {
207 OS << '\n';
208 PrintStmt(If->getElse());
209 }
Chris Lattner882f7882006-11-04 18:52:07 +0000210 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000211}
212
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000213void StmtPrinter::VisitIfStmt(IfStmt *If) {
214 Indent();
215 PrintRawIfStmt(If);
216}
217
Chris Lattnerf2174b62006-11-04 20:59:27 +0000218void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
219 Indent() << "switch (";
Eli Friedmanf86e5072012-10-16 23:45:15 +0000220 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
221 PrintRawDeclStmt(DS);
222 else
223 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000224 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000225
Chris Lattner073926e2007-05-20 23:04:55 +0000226 // Pretty print compoundstmt bodies (very common).
227 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
228 OS << " ";
229 PrintRawCompoundStmt(CS);
230 OS << "\n";
231 } else {
232 OS << "\n";
233 PrintStmt(Node->getBody());
234 }
Chris Lattnerf2174b62006-11-04 20:59:27 +0000235}
236
Chris Lattner85ed8732006-11-04 20:40:44 +0000237void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
238 Indent() << "while (";
Eli Friedmanf86e5072012-10-16 23:45:15 +0000239 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
240 PrintRawDeclStmt(DS);
241 else
242 PrintExpr(Node->getCond());
Chris Lattner85ed8732006-11-04 20:40:44 +0000243 OS << ")\n";
244 PrintStmt(Node->getBody());
245}
246
247void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000248 Indent() << "do ";
249 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
250 PrintRawCompoundStmt(CS);
251 OS << " ";
252 } else {
253 OS << "\n";
254 PrintStmt(Node->getBody());
255 Indent();
256 }
Mike Stump11289f42009-09-09 15:08:12 +0000257
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000258 OS << "while (";
Chris Lattner85ed8732006-11-04 20:40:44 +0000259 PrintExpr(Node->getCond());
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000260 OS << ");\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000261}
262
Chris Lattner71e23ce2006-11-04 20:18:38 +0000263void StmtPrinter::VisitForStmt(ForStmt *Node) {
264 Indent() << "for (";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000265 if (Node->getInit()) {
266 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000267 PrintRawDeclStmt(DS);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000268 else
269 PrintExpr(cast<Expr>(Node->getInit()));
270 }
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000271 OS << ";";
272 if (Node->getCond()) {
273 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000274 PrintExpr(Node->getCond());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000275 }
276 OS << ";";
277 if (Node->getInc()) {
278 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000279 PrintExpr(Node->getInc());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000280 }
281 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000282
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000283 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
284 PrintRawCompoundStmt(CS);
285 OS << "\n";
286 } else {
287 OS << "\n";
288 PrintStmt(Node->getBody());
289 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000290}
291
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000292void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian83615522008-01-02 22:54:34 +0000293 Indent() << "for (";
294 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000295 PrintRawDeclStmt(DS);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000296 else
297 PrintExpr(cast<Expr>(Node->getElement()));
298 OS << " in ";
299 PrintExpr(Node->getCollection());
300 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000301
Fariborz Jahanian83615522008-01-02 22:54:34 +0000302 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
303 PrintRawCompoundStmt(CS);
304 OS << "\n";
305 } else {
306 OS << "\n";
307 PrintStmt(Node->getBody());
308 }
309}
310
Richard Smith02e85f32011-04-14 22:09:26 +0000311void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
312 Indent() << "for (";
313 PrintingPolicy SubPolicy(Policy);
314 SubPolicy.SuppressInitializers = true;
315 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
316 OS << " : ";
317 PrintExpr(Node->getRangeInit());
318 OS << ") {\n";
319 PrintStmt(Node->getBody());
Ted Kremenek88446602013-12-11 23:44:02 +0000320 Indent() << "}";
321 if (Policy.IncludeNewlines) OS << "\n";
Richard Smith02e85f32011-04-14 22:09:26 +0000322}
323
Douglas Gregordeb4a2be2011-10-25 01:33:02 +0000324void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
325 Indent();
326 if (Node->isIfExists())
327 OS << "__if_exists (";
328 else
329 OS << "__if_not_exists (";
330
331 if (NestedNameSpecifier *Qualifier
332 = Node->getQualifierLoc().getNestedNameSpecifier())
333 Qualifier->print(OS, Policy);
334
335 OS << Node->getNameInfo() << ") ";
336
337 PrintRawCompoundStmt(Node->getSubStmt());
338}
339
Chris Lattner16976d32006-11-05 01:46:01 +0000340void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000341 Indent() << "goto " << Node->getLabel()->getName() << ";";
342 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000343}
344
345void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000346 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000347 PrintExpr(Node->getTarget());
Ted Kremenek88446602013-12-11 23:44:02 +0000348 OS << ";";
349 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000350}
351
352void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000353 Indent() << "continue;";
354 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000355}
356
357void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000358 Indent() << "break;";
359 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000360}
361
362
Chris Lattner882f7882006-11-04 18:52:07 +0000363void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
364 Indent() << "return";
365 if (Node->getRetValue()) {
366 OS << " ";
367 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000368 }
Ted Kremenek88446602013-12-11 23:44:02 +0000369 OS << ";";
370 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000371}
372
Chris Lattner73c56c02007-10-29 04:04:16 +0000373
Chad Rosierde70e0e2012-08-25 00:11:56 +0000374void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
Anders Carlsson660bdd12007-11-23 23:12:25 +0000375 Indent() << "asm ";
Mike Stump11289f42009-09-09 15:08:12 +0000376
Anders Carlsson660bdd12007-11-23 23:12:25 +0000377 if (Node->isVolatile())
378 OS << "volatile ";
Mike Stump11289f42009-09-09 15:08:12 +0000379
Anders Carlsson660bdd12007-11-23 23:12:25 +0000380 OS << "(";
Anders Carlsson81a5a692007-11-20 19:21:03 +0000381 VisitStringLiteral(Node->getAsmString());
Mike Stump11289f42009-09-09 15:08:12 +0000382
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000383 // Outputs
384 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
385 Node->getNumClobbers() != 0)
386 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000387
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000388 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
389 if (i != 0)
390 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000391
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000392 if (!Node->getOutputName(i).empty()) {
393 OS << '[';
394 OS << Node->getOutputName(i);
395 OS << "] ";
396 }
Mike Stump11289f42009-09-09 15:08:12 +0000397
Chris Lattner72bbf172009-03-10 04:59:06 +0000398 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Jonathan Roelofs78f9e032015-06-09 14:13:31 +0000399 OS << " (";
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000400 Visit(Node->getOutputExpr(i));
Jonathan Roelofs78f9e032015-06-09 14:13:31 +0000401 OS << ")";
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000402 }
Mike Stump11289f42009-09-09 15:08:12 +0000403
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000404 // Inputs
405 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
406 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000407
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000408 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
409 if (i != 0)
410 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000411
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000412 if (!Node->getInputName(i).empty()) {
413 OS << '[';
414 OS << Node->getInputName(i);
415 OS << "] ";
416 }
Mike Stump11289f42009-09-09 15:08:12 +0000417
Chris Lattner72bbf172009-03-10 04:59:06 +0000418 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Jonathan Roelofs78f9e032015-06-09 14:13:31 +0000419 OS << " (";
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000420 Visit(Node->getInputExpr(i));
Jonathan Roelofs78f9e032015-06-09 14:13:31 +0000421 OS << ")";
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000422 }
Mike Stump11289f42009-09-09 15:08:12 +0000423
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000424 // Clobbers
425 if (Node->getNumClobbers() != 0)
426 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000427
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000428 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
429 if (i != 0)
430 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000431
Chad Rosierd9fb09a2012-08-27 23:28:41 +0000432 VisitStringLiteral(Node->getClobberStringLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000433 }
Mike Stump11289f42009-09-09 15:08:12 +0000434
Ted Kremenek88446602013-12-11 23:44:02 +0000435 OS << ");";
436 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner73c56c02007-10-29 04:04:16 +0000437}
438
Chad Rosier32503022012-06-11 20:47:18 +0000439void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
440 // FIXME: Implement MS style inline asm statement printer.
Chad Rosierb6f46c12012-08-15 16:53:30 +0000441 Indent() << "__asm ";
442 if (Node->hasBraces())
443 OS << "{\n";
John McCallf413f5e2013-05-03 00:10:13 +0000444 OS << Node->getAsmString() << "\n";
Chad Rosierb6f46c12012-08-15 16:53:30 +0000445 if (Node->hasBraces())
446 Indent() << "}\n";
Chad Rosier32503022012-06-11 20:47:18 +0000447}
448
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000449void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000450 PrintStmt(Node->getCapturedDecl()->getBody());
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000451}
452
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000453void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000454 Indent() << "@try";
455 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
456 PrintRawCompoundStmt(TS);
457 OS << "\n";
458 }
Mike Stump11289f42009-09-09 15:08:12 +0000459
Douglas Gregor96c79492010-04-23 22:50:49 +0000460 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
461 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000462 Indent() << "@catch(";
Steve Naroff371b8fb2009-03-03 19:52:17 +0000463 if (catchStmt->getCatchParamDecl()) {
464 if (Decl *DS = catchStmt->getCatchParamDecl())
465 PrintRawDecl(DS);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000466 }
467 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000468 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
469 PrintRawCompoundStmt(CS);
470 OS << "\n";
471 }
Fariborz Jahanian88157952007-11-02 18:16:07 +0000472 }
Mike Stump11289f42009-09-09 15:08:12 +0000473
474 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
475 Node->getFinallyStmt())) {
Fariborz Jahaniandefbf9a2007-11-07 00:46:42 +0000476 Indent() << "@finally";
477 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian88157952007-11-02 18:16:07 +0000478 OS << "\n";
Mike Stump11289f42009-09-09 15:08:12 +0000479 }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000480}
481
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000482void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000483}
484
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000485void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000486 Indent() << "@catch (...) { /* todo */ } \n";
487}
488
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000489void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000490 Indent() << "@throw";
491 if (Node->getThrowExpr()) {
492 OS << " ";
493 PrintExpr(Node->getThrowExpr());
494 }
495 OS << ";\n";
496}
497
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000498void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000499 Indent() << "@synchronized (";
500 PrintExpr(Node->getSynchExpr());
501 OS << ")";
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000502 PrintRawCompoundStmt(Node->getSynchBody());
503 OS << "\n";
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000504}
505
John McCall31168b02011-06-15 23:02:42 +0000506void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
507 Indent() << "@autoreleasepool";
508 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
509 OS << "\n";
510}
511
Sebastian Redl9b244a82008-12-22 21:35:02 +0000512void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
513 OS << "catch (";
Sebastian Redl54c04d42008-12-22 19:15:10 +0000514 if (Decl *ExDecl = Node->getExceptionDecl())
515 PrintRawDecl(ExDecl);
516 else
517 OS << "...";
518 OS << ") ";
519 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl9b244a82008-12-22 21:35:02 +0000520}
521
522void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
523 Indent();
524 PrintRawCXXCatchStmt(Node);
525 OS << "\n";
526}
527
528void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
529 Indent() << "try ";
530 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump11289f42009-09-09 15:08:12 +0000531 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl9b244a82008-12-22 21:35:02 +0000532 OS << " ";
533 PrintRawCXXCatchStmt(Node->getHandler(i));
534 }
Sebastian Redl54c04d42008-12-22 19:15:10 +0000535 OS << "\n";
536}
537
John Wiegley1c0675e2011-04-28 01:08:34 +0000538void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
539 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
540 PrintRawCompoundStmt(Node->getTryBlock());
541 SEHExceptStmt *E = Node->getExceptHandler();
542 SEHFinallyStmt *F = Node->getFinallyHandler();
543 if(E)
544 PrintRawSEHExceptHandler(E);
545 else {
546 assert(F && "Must have a finally block...");
547 PrintRawSEHFinallyStmt(F);
548 }
549 OS << "\n";
550}
551
552void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
553 OS << "__finally ";
554 PrintRawCompoundStmt(Node->getBlock());
555 OS << "\n";
556}
557
558void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
559 OS << "__except (";
560 VisitExpr(Node->getFilterExpr());
Joao Matos566359c2012-09-04 17:49:35 +0000561 OS << ")\n";
John Wiegley1c0675e2011-04-28 01:08:34 +0000562 PrintRawCompoundStmt(Node->getBlock());
563 OS << "\n";
564}
565
566void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
567 Indent();
568 PrintRawSEHExceptHandler(Node);
569 OS << "\n";
570}
571
572void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
573 Indent();
574 PrintRawSEHFinallyStmt(Node);
575 OS << "\n";
576}
577
Nico Weber9b982072014-07-07 00:12:30 +0000578void StmtPrinter::VisitSEHLeaveStmt(SEHLeaveStmt *Node) {
579 Indent() << "__leave;";
580 if (Policy.IncludeNewlines) OS << "\n";
581}
582
Chris Lattner71e23ce2006-11-04 20:18:38 +0000583//===----------------------------------------------------------------------===//
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000584// OpenMP clauses printing methods
585//===----------------------------------------------------------------------===//
586
587namespace {
588class OMPClausePrinter : public OMPClauseVisitor<OMPClausePrinter> {
589 raw_ostream &OS;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000590 const PrintingPolicy &Policy;
Alexey Bataev756c1962013-09-24 03:17:45 +0000591 /// \brief Process clauses with list of variables.
592 template <typename T>
593 void VisitOMPClauseList(T *Node, char StartSym);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000594public:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000595 OMPClausePrinter(raw_ostream &OS, const PrintingPolicy &Policy)
596 : OS(OS), Policy(Policy) { }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000597#define OPENMP_CLAUSE(Name, Class) \
598 void Visit##Class(Class *S);
599#include "clang/Basic/OpenMPKinds.def"
600};
601
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000602void OMPClausePrinter::VisitOMPIfClause(OMPIfClause *Node) {
603 OS << "if(";
Craig Topper36250ad2014-05-12 05:36:57 +0000604 Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000605 OS << ")";
606}
607
Alexey Bataev3778b602014-07-17 07:32:53 +0000608void OMPClausePrinter::VisitOMPFinalClause(OMPFinalClause *Node) {
609 OS << "final(";
610 Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
611 OS << ")";
612}
613
Alexey Bataev568a8332014-03-06 06:15:19 +0000614void OMPClausePrinter::VisitOMPNumThreadsClause(OMPNumThreadsClause *Node) {
615 OS << "num_threads(";
Craig Topper36250ad2014-05-12 05:36:57 +0000616 Node->getNumThreads()->printPretty(OS, nullptr, Policy, 0);
Alexey Bataev568a8332014-03-06 06:15:19 +0000617 OS << ")";
618}
619
Alexey Bataev62c87d22014-03-21 04:51:18 +0000620void OMPClausePrinter::VisitOMPSafelenClause(OMPSafelenClause *Node) {
621 OS << "safelen(";
Craig Topper36250ad2014-05-12 05:36:57 +0000622 Node->getSafelen()->printPretty(OS, nullptr, Policy, 0);
Alexey Bataev62c87d22014-03-21 04:51:18 +0000623 OS << ")";
624}
625
Alexander Musman8bd31e62014-05-27 15:12:19 +0000626void OMPClausePrinter::VisitOMPCollapseClause(OMPCollapseClause *Node) {
627 OS << "collapse(";
628 Node->getNumForLoops()->printPretty(OS, nullptr, Policy, 0);
629 OS << ")";
630}
631
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000632void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
633 OS << "default("
634 << getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind())
635 << ")";
636}
637
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000638void OMPClausePrinter::VisitOMPProcBindClause(OMPProcBindClause *Node) {
639 OS << "proc_bind("
640 << getOpenMPSimpleClauseTypeName(OMPC_proc_bind, Node->getProcBindKind())
641 << ")";
642}
643
Alexey Bataev56dafe82014-06-20 07:16:17 +0000644void OMPClausePrinter::VisitOMPScheduleClause(OMPScheduleClause *Node) {
645 OS << "schedule("
646 << getOpenMPSimpleClauseTypeName(OMPC_schedule, Node->getScheduleKind());
647 if (Node->getChunkSize()) {
648 OS << ", ";
649 Node->getChunkSize()->printPretty(OS, nullptr, Policy);
650 }
651 OS << ")";
652}
653
Alexey Bataev10e775f2015-07-30 11:36:16 +0000654void OMPClausePrinter::VisitOMPOrderedClause(OMPOrderedClause *Node) {
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000655 OS << "ordered";
Alexey Bataev10e775f2015-07-30 11:36:16 +0000656 if (auto *Num = Node->getNumForLoops()) {
657 OS << "(";
658 Num->printPretty(OS, nullptr, Policy, 0);
659 OS << ")";
660 }
Alexey Bataev142e1fc2014-06-20 09:44:06 +0000661}
662
Alexey Bataev236070f2014-06-20 11:19:47 +0000663void OMPClausePrinter::VisitOMPNowaitClause(OMPNowaitClause *) {
664 OS << "nowait";
665}
666
Alexey Bataev7aea99a2014-07-17 12:19:31 +0000667void OMPClausePrinter::VisitOMPUntiedClause(OMPUntiedClause *) {
668 OS << "untied";
669}
670
Alexey Bataev74ba3a52014-07-17 12:47:03 +0000671void OMPClausePrinter::VisitOMPMergeableClause(OMPMergeableClause *) {
672 OS << "mergeable";
673}
674
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000675void OMPClausePrinter::VisitOMPReadClause(OMPReadClause *) { OS << "read"; }
676
Alexey Bataevdea47612014-07-23 07:46:59 +0000677void OMPClausePrinter::VisitOMPWriteClause(OMPWriteClause *) { OS << "write"; }
678
Alexey Bataev67a4f222014-07-23 10:25:33 +0000679void OMPClausePrinter::VisitOMPUpdateClause(OMPUpdateClause *) {
680 OS << "update";
681}
682
Alexey Bataev459dec02014-07-24 06:46:57 +0000683void OMPClausePrinter::VisitOMPCaptureClause(OMPCaptureClause *) {
684 OS << "capture";
685}
686
Alexey Bataev82bad8b2014-07-24 08:55:34 +0000687void OMPClausePrinter::VisitOMPSeqCstClause(OMPSeqCstClause *) {
688 OS << "seq_cst";
689}
690
Michael Wonge710d542015-08-07 16:16:36 +0000691void OMPClausePrinter::VisitOMPDeviceClause(OMPDeviceClause *Node) {
692 OS << "device(";
693 Node->getDevice()->printPretty(OS, nullptr, Policy, 0);
694 OS << ")";
695}
696
Alexey Bataev756c1962013-09-24 03:17:45 +0000697template<typename T>
698void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
699 for (typename T::varlist_iterator I = Node->varlist_begin(),
700 E = Node->varlist_end();
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000701 I != E; ++I) {
Richard Trieuddd01ce2014-06-09 22:53:25 +0000702 assert(*I && "Expected non-null Stmt");
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000703 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(*I)) {
704 OS << (I == Node->varlist_begin() ? StartSym : ',');
705 cast<NamedDecl>(DRE->getDecl())->printQualifiedName(OS);
706 } else {
707 OS << (I == Node->varlist_begin() ? StartSym : ',');
Craig Topper36250ad2014-05-12 05:36:57 +0000708 (*I)->printPretty(OS, nullptr, Policy, 0);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000709 }
710 }
Alexey Bataev756c1962013-09-24 03:17:45 +0000711}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000712
713void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) {
714 if (!Node->varlist_empty()) {
715 OS << "private";
Alexey Bataev756c1962013-09-24 03:17:45 +0000716 VisitOMPClauseList(Node, '(');
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000717 OS << ")";
718 }
719}
720
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000721void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) {
722 if (!Node->varlist_empty()) {
723 OS << "firstprivate";
724 VisitOMPClauseList(Node, '(');
725 OS << ")";
726 }
727}
728
Alexander Musman1bb328c2014-06-04 13:06:39 +0000729void OMPClausePrinter::VisitOMPLastprivateClause(OMPLastprivateClause *Node) {
730 if (!Node->varlist_empty()) {
731 OS << "lastprivate";
732 VisitOMPClauseList(Node, '(');
733 OS << ")";
734 }
735}
736
Alexey Bataev758e55e2013-09-06 18:03:48 +0000737void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
738 if (!Node->varlist_empty()) {
739 OS << "shared";
Alexey Bataev756c1962013-09-24 03:17:45 +0000740 VisitOMPClauseList(Node, '(');
Alexey Bataev758e55e2013-09-06 18:03:48 +0000741 OS << ")";
742 }
743}
744
Alexey Bataevc5e02582014-06-16 07:08:35 +0000745void OMPClausePrinter::VisitOMPReductionClause(OMPReductionClause *Node) {
746 if (!Node->varlist_empty()) {
747 OS << "reduction(";
748 NestedNameSpecifier *QualifierLoc =
749 Node->getQualifierLoc().getNestedNameSpecifier();
750 OverloadedOperatorKind OOK =
751 Node->getNameInfo().getName().getCXXOverloadedOperator();
752 if (QualifierLoc == nullptr && OOK != OO_None) {
753 // Print reduction identifier in C format
754 OS << getOperatorSpelling(OOK);
755 } else {
756 // Use C++ format
757 if (QualifierLoc != nullptr)
758 QualifierLoc->print(OS, Policy);
759 OS << Node->getNameInfo();
760 }
761 OS << ":";
762 VisitOMPClauseList(Node, ' ');
763 OS << ")";
764 }
765}
766
Alexander Musman8dba6642014-04-22 13:09:42 +0000767void OMPClausePrinter::VisitOMPLinearClause(OMPLinearClause *Node) {
768 if (!Node->varlist_empty()) {
769 OS << "linear";
770 VisitOMPClauseList(Node, '(');
Craig Topper36250ad2014-05-12 05:36:57 +0000771 if (Node->getStep() != nullptr) {
Alexander Musman8dba6642014-04-22 13:09:42 +0000772 OS << ": ";
Craig Topper36250ad2014-05-12 05:36:57 +0000773 Node->getStep()->printPretty(OS, nullptr, Policy, 0);
Alexander Musman8dba6642014-04-22 13:09:42 +0000774 }
775 OS << ")";
776 }
777}
778
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000779void OMPClausePrinter::VisitOMPAlignedClause(OMPAlignedClause *Node) {
780 if (!Node->varlist_empty()) {
781 OS << "aligned";
782 VisitOMPClauseList(Node, '(');
783 if (Node->getAlignment() != nullptr) {
784 OS << ": ";
785 Node->getAlignment()->printPretty(OS, nullptr, Policy, 0);
786 }
787 OS << ")";
788 }
789}
790
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000791void OMPClausePrinter::VisitOMPCopyinClause(OMPCopyinClause *Node) {
792 if (!Node->varlist_empty()) {
793 OS << "copyin";
794 VisitOMPClauseList(Node, '(');
795 OS << ")";
796 }
797}
798
Alexey Bataevbae9a792014-06-27 10:37:06 +0000799void OMPClausePrinter::VisitOMPCopyprivateClause(OMPCopyprivateClause *Node) {
800 if (!Node->varlist_empty()) {
801 OS << "copyprivate";
802 VisitOMPClauseList(Node, '(');
803 OS << ")";
804 }
805}
806
Alexey Bataev6125da92014-07-21 11:26:11 +0000807void OMPClausePrinter::VisitOMPFlushClause(OMPFlushClause *Node) {
808 if (!Node->varlist_empty()) {
809 VisitOMPClauseList(Node, '(');
810 OS << ")";
811 }
812}
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +0000813
814void OMPClausePrinter::VisitOMPDependClause(OMPDependClause *Node) {
815 if (!Node->varlist_empty()) {
816 OS << "depend(";
817 OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(),
818 Node->getDependencyKind())
819 << " :";
820 VisitOMPClauseList(Node, ' ');
821 OS << ")";
822 }
823}
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000824}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000825
826//===----------------------------------------------------------------------===//
827// OpenMP directives printing methods
828//===----------------------------------------------------------------------===//
829
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000830void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000831 OMPClausePrinter Printer(OS, Policy);
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000832 ArrayRef<OMPClause *> Clauses = S->clauses();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000833 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
834 I != E; ++I)
835 if (*I && !(*I)->isImplicit()) {
836 Printer.Visit(*I);
837 OS << ' ';
838 }
839 OS << "\n";
Alexey Bataev68446b72014-07-18 07:47:19 +0000840 if (S->hasAssociatedStmt() && S->getAssociatedStmt()) {
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000841 assert(isa<CapturedStmt>(S->getAssociatedStmt()) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000842 "Expected captured statement!");
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000843 Stmt *CS = cast<CapturedStmt>(S->getAssociatedStmt())->getCapturedStmt();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000844 PrintStmt(CS);
845 }
846}
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000847
848void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
849 Indent() << "#pragma omp parallel ";
850 PrintOMPExecutableDirective(Node);
851}
852
853void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
854 Indent() << "#pragma omp simd ";
855 PrintOMPExecutableDirective(Node);
856}
857
Alexey Bataevf29276e2014-06-18 04:14:57 +0000858void StmtPrinter::VisitOMPForDirective(OMPForDirective *Node) {
859 Indent() << "#pragma omp for ";
860 PrintOMPExecutableDirective(Node);
861}
862
Alexander Musmanf82886e2014-09-18 05:12:34 +0000863void StmtPrinter::VisitOMPForSimdDirective(OMPForSimdDirective *Node) {
864 Indent() << "#pragma omp for simd ";
865 PrintOMPExecutableDirective(Node);
866}
867
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000868void StmtPrinter::VisitOMPSectionsDirective(OMPSectionsDirective *Node) {
869 Indent() << "#pragma omp sections ";
870 PrintOMPExecutableDirective(Node);
871}
872
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000873void StmtPrinter::VisitOMPSectionDirective(OMPSectionDirective *Node) {
874 Indent() << "#pragma omp section";
875 PrintOMPExecutableDirective(Node);
876}
877
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000878void StmtPrinter::VisitOMPSingleDirective(OMPSingleDirective *Node) {
879 Indent() << "#pragma omp single ";
880 PrintOMPExecutableDirective(Node);
881}
882
Alexander Musman80c22892014-07-17 08:54:58 +0000883void StmtPrinter::VisitOMPMasterDirective(OMPMasterDirective *Node) {
884 Indent() << "#pragma omp master";
885 PrintOMPExecutableDirective(Node);
886}
887
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000888void StmtPrinter::VisitOMPCriticalDirective(OMPCriticalDirective *Node) {
889 Indent() << "#pragma omp critical";
890 if (Node->getDirectiveName().getName()) {
891 OS << " (";
892 Node->getDirectiveName().printName(OS);
893 OS << ")";
894 }
895 PrintOMPExecutableDirective(Node);
896}
897
Alexey Bataev4acb8592014-07-07 13:01:15 +0000898void StmtPrinter::VisitOMPParallelForDirective(OMPParallelForDirective *Node) {
899 Indent() << "#pragma omp parallel for ";
900 PrintOMPExecutableDirective(Node);
901}
902
Alexander Musmane4e893b2014-09-23 09:33:00 +0000903void StmtPrinter::VisitOMPParallelForSimdDirective(
904 OMPParallelForSimdDirective *Node) {
905 Indent() << "#pragma omp parallel for simd ";
906 PrintOMPExecutableDirective(Node);
907}
908
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000909void StmtPrinter::VisitOMPParallelSectionsDirective(
910 OMPParallelSectionsDirective *Node) {
911 Indent() << "#pragma omp parallel sections ";
912 PrintOMPExecutableDirective(Node);
913}
914
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000915void StmtPrinter::VisitOMPTaskDirective(OMPTaskDirective *Node) {
916 Indent() << "#pragma omp task ";
917 PrintOMPExecutableDirective(Node);
918}
919
Alexey Bataev68446b72014-07-18 07:47:19 +0000920void StmtPrinter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *Node) {
921 Indent() << "#pragma omp taskyield";
922 PrintOMPExecutableDirective(Node);
923}
924
Alexey Bataev4d1dfea2014-07-18 09:11:51 +0000925void StmtPrinter::VisitOMPBarrierDirective(OMPBarrierDirective *Node) {
926 Indent() << "#pragma omp barrier";
927 PrintOMPExecutableDirective(Node);
928}
929
Alexey Bataev2df347a2014-07-18 10:17:07 +0000930void StmtPrinter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *Node) {
931 Indent() << "#pragma omp taskwait";
932 PrintOMPExecutableDirective(Node);
933}
934
Alexey Bataevc30dd2d2015-06-18 12:14:09 +0000935void StmtPrinter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective *Node) {
936 Indent() << "#pragma omp taskgroup";
937 PrintOMPExecutableDirective(Node);
938}
939
Alexey Bataev6125da92014-07-21 11:26:11 +0000940void StmtPrinter::VisitOMPFlushDirective(OMPFlushDirective *Node) {
941 Indent() << "#pragma omp flush ";
942 PrintOMPExecutableDirective(Node);
943}
944
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000945void StmtPrinter::VisitOMPOrderedDirective(OMPOrderedDirective *Node) {
946 Indent() << "#pragma omp ordered";
947 PrintOMPExecutableDirective(Node);
948}
949
Alexey Bataev0162e452014-07-22 10:10:35 +0000950void StmtPrinter::VisitOMPAtomicDirective(OMPAtomicDirective *Node) {
951 Indent() << "#pragma omp atomic ";
952 PrintOMPExecutableDirective(Node);
953}
954
Alexey Bataev0bd520b2014-09-19 08:19:49 +0000955void StmtPrinter::VisitOMPTargetDirective(OMPTargetDirective *Node) {
956 Indent() << "#pragma omp target ";
957 PrintOMPExecutableDirective(Node);
958}
959
Michael Wong65f367f2015-07-21 13:44:28 +0000960void StmtPrinter::VisitOMPTargetDataDirective(OMPTargetDataDirective *Node) {
961 Indent() << "#pragma omp target data ";
962 PrintOMPExecutableDirective(Node);
963}
964
Alexey Bataev13314bf2014-10-09 04:18:56 +0000965void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) {
966 Indent() << "#pragma omp teams ";
967 PrintOMPExecutableDirective(Node);
968}
969
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000970void StmtPrinter::VisitOMPCancellationPointDirective(
971 OMPCancellationPointDirective *Node) {
972 Indent() << "#pragma omp cancellation point "
973 << getOpenMPDirectiveName(Node->getCancelRegion());
974 PrintOMPExecutableDirective(Node);
975}
Alexey Bataev80909872015-07-02 11:25:17 +0000976
977void StmtPrinter::VisitOMPCancelDirective(OMPCancelDirective *Node) {
978 Indent() << "#pragma omp cancel "
979 << getOpenMPDirectiveName(Node->getCancelRegion());
980 PrintOMPExecutableDirective(Node);
981}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000982//===----------------------------------------------------------------------===//
Chris Lattner71e23ce2006-11-04 20:18:38 +0000983// Expr printing methods.
984//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000985
Chris Lattner882f7882006-11-04 18:52:07 +0000986void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000987 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
988 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000989 if (Node->hasTemplateKeyword())
990 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000991 OS << Node->getNameInfo();
John McCallb3774b52010-08-19 23:49:38 +0000992 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +0000993 TemplateSpecializationType::PrintTemplateArgumentList(
994 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000995}
996
John McCall8cd78132009-11-19 22:55:06 +0000997void StmtPrinter::VisitDependentScopeDeclRefExpr(
998 DependentScopeDeclRefExpr *Node) {
Axel Naumann20b27862011-01-24 15:44:00 +0000999 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1000 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001001 if (Node->hasTemplateKeyword())
1002 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001003 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +00001004 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +00001005 TemplateSpecializationType::PrintTemplateArgumentList(
1006 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregor90a1a652009-03-19 17:26:29 +00001007}
1008
John McCalld14a8642009-11-21 08:51:07 +00001009void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregora727cb92009-06-30 22:34:41 +00001010 if (Node->getQualifier())
1011 Node->getQualifier()->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001012 if (Node->hasTemplateKeyword())
1013 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001014 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +00001015 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +00001016 TemplateSpecializationType::PrintTemplateArgumentList(
1017 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregora727cb92009-06-30 22:34:41 +00001018}
1019
Steve Naroffe46504b2007-11-12 14:29:37 +00001020void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +00001021 if (Node->getBase()) {
1022 PrintExpr(Node->getBase());
1023 OS << (Node->isArrow() ? "->" : ".");
1024 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001025 OS << *Node->getDecl();
Steve Naroffe46504b2007-11-12 14:29:37 +00001026}
1027
Steve Naroffec944032008-05-30 00:40:33 +00001028void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Fariborz Jahanian681c0752010-10-14 16:04:05 +00001029 if (Node->isSuperReceiver())
1030 OS << "super.";
Richard Trieu6d92e502014-02-06 23:26:23 +00001031 else if (Node->isObjectReceiver() && Node->getBase()) {
Steve Naroffec944032008-05-30 00:40:33 +00001032 PrintExpr(Node->getBase());
1033 OS << ".";
Richard Trieu6d92e502014-02-06 23:26:23 +00001034 } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
1035 OS << Node->getClassReceiver()->getName() << ".";
Steve Naroffec944032008-05-30 00:40:33 +00001036 }
Fariborz Jahanian681c0752010-10-14 16:04:05 +00001037
John McCallb7bd14f2010-12-02 01:19:52 +00001038 if (Node->isImplicitProperty())
Aaron Ballmanb190f972014-01-03 17:59:55 +00001039 Node->getImplicitPropertyGetter()->getSelector().print(OS);
John McCallb7bd14f2010-12-02 01:19:52 +00001040 else
1041 OS << Node->getExplicitProperty()->getName();
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00001042}
1043
Ted Kremeneke65b0862012-03-06 20:05:56 +00001044void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
1045
1046 PrintExpr(Node->getBaseExpr());
1047 OS << "[";
1048 PrintExpr(Node->getKeyExpr());
1049 OS << "]";
1050}
1051
Chris Lattner6307f192008-08-10 01:53:14 +00001052void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Alexey Bataevec474782014-10-09 08:45:04 +00001053 OS << PredefinedExpr::getIdentTypeName(Node->getIdentType());
Anders Carlsson625bfc82007-07-21 05:21:51 +00001054}
1055
Steve Naroffae4143e2007-04-26 20:39:23 +00001056void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +00001057 unsigned value = Node->getValue();
Douglas Gregorfb65e592011-07-27 05:40:30 +00001058
1059 switch (Node->getKind()) {
1060 case CharacterLiteral::Ascii: break; // no prefix.
1061 case CharacterLiteral::Wide: OS << 'L'; break;
1062 case CharacterLiteral::UTF16: OS << 'u'; break;
1063 case CharacterLiteral::UTF32: OS << 'U'; break;
1064 }
1065
Chris Lattner666115c2007-07-13 23:58:20 +00001066 switch (value) {
1067 case '\\':
1068 OS << "'\\\\'";
1069 break;
1070 case '\'':
1071 OS << "'\\''";
1072 break;
1073 case '\a':
1074 // TODO: K&R: the meaning of '\\a' is different in traditional C
1075 OS << "'\\a'";
1076 break;
1077 case '\b':
1078 OS << "'\\b'";
1079 break;
1080 // Nonstandard escape sequence.
1081 /*case '\e':
1082 OS << "'\\e'";
1083 break;*/
1084 case '\f':
1085 OS << "'\\f'";
1086 break;
1087 case '\n':
1088 OS << "'\\n'";
1089 break;
1090 case '\r':
1091 OS << "'\\r'";
1092 break;
1093 case '\t':
1094 OS << "'\\t'";
1095 break;
1096 case '\v':
1097 OS << "'\\v'";
1098 break;
1099 default:
Jordan Rose00d1b592013-02-08 22:30:27 +00001100 if (value < 256 && isPrintable((unsigned char)value))
Chris Lattner666115c2007-07-13 23:58:20 +00001101 OS << "'" << (char)value << "'";
Jordan Rose00d1b592013-02-08 22:30:27 +00001102 else if (value < 256)
1103 OS << "'\\x" << llvm::format("%02x", value) << "'";
1104 else if (value <= 0xFFFF)
1105 OS << "'\\u" << llvm::format("%04x", value) << "'";
1106 else
1107 OS << "'\\U" << llvm::format("%08x", value) << "'";
Chris Lattner6e9d9b32007-07-13 05:18:11 +00001108 }
Steve Naroffae4143e2007-04-26 20:39:23 +00001109}
1110
Steve Naroffdf7855b2007-02-21 23:46:25 +00001111void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +00001112 bool isSigned = Node->getType()->isSignedIntegerType();
1113 OS << Node->getValue().toString(10, isSigned);
Mike Stump11289f42009-09-09 15:08:12 +00001114
Chris Lattner06430412007-05-21 05:45:03 +00001115 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall9dd450b2009-09-21 23:43:11 +00001116 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
David Blaikie83d382b2011-09-23 05:06:16 +00001117 default: llvm_unreachable("Unexpected type for integer literal!");
David Majnemerbe09e8e2015-03-06 18:04:22 +00001118 case BuiltinType::Char_S:
1119 case BuiltinType::Char_U: OS << "i8"; break;
David Majnemer65a407c2014-06-21 18:46:07 +00001120 case BuiltinType::UChar: OS << "Ui8"; break;
1121 case BuiltinType::Short: OS << "i16"; break;
1122 case BuiltinType::UShort: OS << "Ui16"; break;
Chris Lattner06430412007-05-21 05:45:03 +00001123 case BuiltinType::Int: break; // no suffix.
1124 case BuiltinType::UInt: OS << 'U'; break;
1125 case BuiltinType::Long: OS << 'L'; break;
1126 case BuiltinType::ULong: OS << "UL"; break;
1127 case BuiltinType::LongLong: OS << "LL"; break;
1128 case BuiltinType::ULongLong: OS << "ULL"; break;
1129 }
Chris Lattner882f7882006-11-04 18:52:07 +00001130}
Benjamin Kramer8a526762012-09-20 14:07:17 +00001131
1132static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
1133 bool PrintSuffix) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001134 SmallString<16> Str;
Eli Friedman53392062011-10-05 20:32:03 +00001135 Node->getValue().toString(Str);
1136 OS << Str;
Benjamin Kramer8a526762012-09-20 14:07:17 +00001137 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
1138 OS << '.'; // Trailing dot in order to separate from ints.
1139
1140 if (!PrintSuffix)
1141 return;
1142
1143 // Emit suffixes. Float literals are always a builtin float type.
1144 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
1145 default: llvm_unreachable("Unexpected type for float literal!");
1146 case BuiltinType::Half: break; // FIXME: suffix?
1147 case BuiltinType::Double: break; // no suffix.
1148 case BuiltinType::Float: OS << 'F'; break;
1149 case BuiltinType::LongDouble: OS << 'L'; break;
1150 }
1151}
1152
1153void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
1154 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
Chris Lattner882f7882006-11-04 18:52:07 +00001155}
Chris Lattner1c20a172007-08-26 03:42:43 +00001156
1157void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
1158 PrintExpr(Node->getSubExpr());
1159 OS << "i";
1160}
1161
Steve Naroffdf7855b2007-02-21 23:46:25 +00001162void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Richard Trieudc355912012-06-13 20:25:24 +00001163 Str->outputString(OS);
Chris Lattner882f7882006-11-04 18:52:07 +00001164}
1165void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
1166 OS << "(";
1167 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +00001168 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +00001169}
1170void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +00001171 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +00001172 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump11289f42009-09-09 15:08:12 +00001173
Eli Friedman1cf25362009-06-14 22:39:26 +00001174 // Print a space if this is an "identifier operator" like __real, or if
1175 // it might be concatenated incorrectly like '+'.
Chris Lattnere5b60442007-08-23 21:46:40 +00001176 switch (Node->getOpcode()) {
1177 default: break;
John McCalle3027922010-08-25 11:45:40 +00001178 case UO_Real:
1179 case UO_Imag:
1180 case UO_Extension:
Chris Lattnere5b60442007-08-23 21:46:40 +00001181 OS << ' ';
1182 break;
John McCalle3027922010-08-25 11:45:40 +00001183 case UO_Plus:
1184 case UO_Minus:
Eli Friedman1cf25362009-06-14 22:39:26 +00001185 if (isa<UnaryOperator>(Node->getSubExpr()))
1186 OS << ' ';
1187 break;
Chris Lattnere5b60442007-08-23 21:46:40 +00001188 }
1189 }
Chris Lattner882f7882006-11-04 18:52:07 +00001190 PrintExpr(Node->getSubExpr());
Mike Stump11289f42009-09-09 15:08:12 +00001191
Chris Lattner15768702006-11-05 23:54:51 +00001192 if (Node->isPostfix())
1193 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +00001194}
Chris Lattner98dbf0a2007-08-30 17:59:59 +00001195
Douglas Gregor882211c2010-04-28 22:16:22 +00001196void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
1197 OS << "__builtin_offsetof(";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001198 Node->getTypeSourceInfo()->getType().print(OS, Policy);
1199 OS << ", ";
Douglas Gregor882211c2010-04-28 22:16:22 +00001200 bool PrintedSomething = false;
1201 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
1202 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
1203 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
1204 // Array node
1205 OS << "[";
1206 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
1207 OS << "]";
1208 PrintedSomething = true;
1209 continue;
1210 }
Douglas Gregord1702062010-04-29 00:18:15 +00001211
1212 // Skip implicit base indirections.
1213 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
1214 continue;
1215
Douglas Gregor882211c2010-04-28 22:16:22 +00001216 // Field or identifier node.
1217 IdentifierInfo *Id = ON.getFieldName();
1218 if (!Id)
1219 continue;
Alexis Hunta8136cc2010-05-05 15:23:54 +00001220
Douglas Gregor882211c2010-04-28 22:16:22 +00001221 if (PrintedSomething)
1222 OS << ".";
1223 else
1224 PrintedSomething = true;
Alexis Hunta8136cc2010-05-05 15:23:54 +00001225 OS << Id->getName();
Douglas Gregor882211c2010-04-28 22:16:22 +00001226 }
1227 OS << ")";
1228}
1229
Peter Collingbournee190dee2011-03-11 19:24:49 +00001230void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
1231 switch(Node->getKind()) {
1232 case UETT_SizeOf:
1233 OS << "sizeof";
1234 break;
1235 case UETT_AlignOf:
Jordan Rose58d54722012-06-30 21:33:57 +00001236 if (Policy.LangOpts.CPlusPlus)
1237 OS << "alignof";
1238 else if (Policy.LangOpts.C11)
1239 OS << "_Alignof";
1240 else
1241 OS << "__alignof";
Peter Collingbournee190dee2011-03-11 19:24:49 +00001242 break;
1243 case UETT_VecStep:
1244 OS << "vec_step";
1245 break;
Alexey Bataev00396512015-07-02 03:40:19 +00001246 case UETT_OpenMPRequiredSimdAlign:
1247 OS << "__builtin_omp_required_simd_align";
1248 break;
Peter Collingbournee190dee2011-03-11 19:24:49 +00001249 }
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001250 if (Node->isArgumentType()) {
1251 OS << '(';
1252 Node->getArgumentType().print(OS, Policy);
1253 OS << ')';
1254 } else {
Sebastian Redl6f282892008-11-11 17:56:53 +00001255 OS << " ";
1256 PrintExpr(Node->getArgumentExpr());
1257 }
Chris Lattner882f7882006-11-04 18:52:07 +00001258}
Peter Collingbourne91147592011-04-15 00:35:48 +00001259
1260void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
1261 OS << "_Generic(";
1262 PrintExpr(Node->getControllingExpr());
1263 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
1264 OS << ", ";
1265 QualType T = Node->getAssocType(i);
1266 if (T.isNull())
1267 OS << "default";
1268 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001269 T.print(OS, Policy);
Peter Collingbourne91147592011-04-15 00:35:48 +00001270 OS << ": ";
1271 PrintExpr(Node->getAssocExpr(i));
1272 }
1273 OS << ")";
1274}
1275
Chris Lattner882f7882006-11-04 18:52:07 +00001276void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +00001277 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +00001278 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +00001279 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +00001280 OS << "]";
1281}
1282
Peter Collingbourne4b279a02011-02-08 21:17:54 +00001283void StmtPrinter::PrintCallArgs(CallExpr *Call) {
Chris Lattner882f7882006-11-04 18:52:07 +00001284 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001285 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1286 // Don't print any defaulted arguments
1287 break;
1288 }
1289
Chris Lattner882f7882006-11-04 18:52:07 +00001290 if (i) OS << ", ";
1291 PrintExpr(Call->getArg(i));
1292 }
Peter Collingbourne4b279a02011-02-08 21:17:54 +00001293}
1294
1295void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1296 PrintExpr(Call->getCallee());
1297 OS << "(";
1298 PrintCallArgs(Call);
Chris Lattner882f7882006-11-04 18:52:07 +00001299 OS << ")";
1300}
1301void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregore4b414c2009-01-08 22:45:41 +00001302 // FIXME: Suppress printing implicit bases (like "this")
1303 PrintExpr(Node->getBase());
David Blaikie8fab8e52012-11-12 19:12:12 +00001304
1305 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
David Blaikie6bffd6f2012-11-12 19:32:32 +00001306 FieldDecl *ParentDecl = ParentMember
Craig Topper36250ad2014-05-12 05:36:57 +00001307 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : nullptr;
David Blaikie8fab8e52012-11-12 19:12:12 +00001308
David Blaikie6bffd6f2012-11-12 19:32:32 +00001309 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
David Blaikie8fab8e52012-11-12 19:12:12 +00001310 OS << (Node->isArrow() ? "->" : ".");
David Blaikie8fab8e52012-11-12 19:12:12 +00001311
Fariborz Jahanian2990c022010-01-11 21:17:32 +00001312 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1313 if (FD->isAnonymousStructOrUnion())
1314 return;
David Blaikie8fab8e52012-11-12 19:12:12 +00001315
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001316 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1317 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001318 if (Node->hasTemplateKeyword())
1319 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001320 OS << Node->getMemberNameInfo();
John McCallb3774b52010-08-19 23:49:38 +00001321 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +00001322 TemplateSpecializationType::PrintTemplateArgumentList(
1323 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001324}
Steve Naroffe87026a2009-07-24 17:54:45 +00001325void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1326 PrintExpr(Node->getBase());
1327 OS << (Node->isArrow() ? "->isa" : ".isa");
1328}
1329
Nate Begemance4d7fc2008-04-18 23:10:10 +00001330void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +00001331 PrintExpr(Node->getBase());
1332 OS << ".";
1333 OS << Node->getAccessor().getName();
1334}
Douglas Gregorf19b2312008-10-28 15:36:24 +00001335void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001336 OS << '(';
1337 Node->getTypeAsWritten().print(OS, Policy);
1338 OS << ')';
Chris Lattner882f7882006-11-04 18:52:07 +00001339 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001340}
Steve Naroff57eb2c52007-07-19 21:32:11 +00001341void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001342 OS << '(';
1343 Node->getType().print(OS, Policy);
1344 OS << ')';
Steve Naroff57eb2c52007-07-19 21:32:11 +00001345 PrintExpr(Node->getInitializer());
1346}
Steve Naroff7a5af782007-07-13 16:58:59 +00001347void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Alp Toker028ed912013-12-06 17:56:43 +00001348 // No need to print anything, simply forward to the subexpression.
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001349 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +00001350}
Chris Lattner882f7882006-11-04 18:52:07 +00001351void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1352 PrintExpr(Node->getLHS());
1353 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1354 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001355}
Chris Lattner86928112007-08-25 02:00:02 +00001356void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1357 PrintExpr(Node->getLHS());
1358 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1359 PrintExpr(Node->getRHS());
1360}
Chris Lattner882f7882006-11-04 18:52:07 +00001361void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1362 PrintExpr(Node->getCond());
John McCallc07a0c72011-02-17 10:25:35 +00001363 OS << " ? ";
1364 PrintExpr(Node->getLHS());
1365 OS << " : ";
Chris Lattner882f7882006-11-04 18:52:07 +00001366 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001367}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001368
Chris Lattnereefa10e2007-05-28 06:56:27 +00001369// GNU extensions.
1370
John McCallc07a0c72011-02-17 10:25:35 +00001371void
1372StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1373 PrintExpr(Node->getCommon());
1374 OS << " ?: ";
1375 PrintExpr(Node->getFalseExpr());
1376}
Chris Lattnerd268a7a2007-08-03 17:31:20 +00001377void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +00001378 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +00001379}
1380
Chris Lattner366727f2007-07-24 16:58:17 +00001381void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1382 OS << "(";
1383 PrintRawCompoundStmt(E->getSubStmt());
1384 OS << ")";
1385}
1386
Steve Naroff9efdabc2007-08-03 21:21:27 +00001387void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1388 OS << "__builtin_choose_expr(";
1389 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +00001390 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +00001391 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +00001392 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +00001393 PrintExpr(Node->getRHS());
1394 OS << ")";
1395}
Chris Lattner366727f2007-07-24 16:58:17 +00001396
Douglas Gregor3be4b122008-11-29 04:51:27 +00001397void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1398 OS << "__null";
1399}
1400
Eli Friedmana1b4ed82008-05-14 19:38:39 +00001401void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1402 OS << "__builtin_shufflevector(";
1403 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1404 if (i) OS << ", ";
1405 PrintExpr(Node->getExpr(i));
1406 }
1407 OS << ")";
1408}
1409
Hal Finkelc4d7c822013-09-18 03:29:45 +00001410void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1411 OS << "__builtin_convertvector(";
1412 PrintExpr(Node->getSrcExpr());
1413 OS << ", ";
1414 Node->getType().print(OS, Policy);
1415 OS << ")";
1416}
1417
Anders Carlsson4692db02007-08-31 04:56:16 +00001418void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001419 if (Node->getSyntacticForm()) {
1420 Visit(Node->getSyntacticForm());
1421 return;
1422 }
1423
Richard Smithd0e102f2015-01-30 02:04:26 +00001424 OS << "{";
Anders Carlsson4692db02007-08-31 04:56:16 +00001425 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1426 if (i) OS << ", ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001427 if (Node->getInit(i))
1428 PrintExpr(Node->getInit(i));
1429 else
Richard Smithd0e102f2015-01-30 02:04:26 +00001430 OS << "{}";
Anders Carlsson4692db02007-08-31 04:56:16 +00001431 }
Richard Smithd0e102f2015-01-30 02:04:26 +00001432 OS << "}";
Anders Carlsson4692db02007-08-31 04:56:16 +00001433}
1434
Nate Begeman5ec4b312009-08-10 23:49:36 +00001435void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
Richard Smithd0e102f2015-01-30 02:04:26 +00001436 OS << "(";
Nate Begeman5ec4b312009-08-10 23:49:36 +00001437 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1438 if (i) OS << ", ";
1439 PrintExpr(Node->getExpr(i));
1440 }
Richard Smithd0e102f2015-01-30 02:04:26 +00001441 OS << ")";
Nate Begeman5ec4b312009-08-10 23:49:36 +00001442}
1443
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001444void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Justin Bognercb337032015-05-28 22:19:36 +00001445 bool NeedsEquals = true;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001446 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
1447 DEnd = Node->designators_end();
1448 D != DEnd; ++D) {
1449 if (D->isFieldDesignator()) {
Serge Pavloveb57aa62014-06-20 17:08:28 +00001450 if (D->getDotLoc().isInvalid()) {
Justin Bognercb337032015-05-28 22:19:36 +00001451 if (IdentifierInfo *II = D->getFieldName()) {
Serge Pavloveb57aa62014-06-20 17:08:28 +00001452 OS << II->getName() << ":";
Justin Bognercb337032015-05-28 22:19:36 +00001453 NeedsEquals = false;
1454 }
Serge Pavloveb57aa62014-06-20 17:08:28 +00001455 } else {
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001456 OS << "." << D->getFieldName()->getName();
Serge Pavloveb57aa62014-06-20 17:08:28 +00001457 }
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001458 } else {
1459 OS << "[";
1460 if (D->isArrayDesignator()) {
1461 PrintExpr(Node->getArrayIndex(*D));
1462 } else {
1463 PrintExpr(Node->getArrayRangeStart(*D));
1464 OS << " ... ";
Mike Stump11289f42009-09-09 15:08:12 +00001465 PrintExpr(Node->getArrayRangeEnd(*D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001466 }
1467 OS << "]";
1468 }
1469 }
1470
Justin Bognercb337032015-05-28 22:19:36 +00001471 if (NeedsEquals)
1472 OS << " = ";
1473 else
1474 OS << " ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001475 PrintExpr(Node->getInit());
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001476}
1477
Yunzhong Gaocb779302015-06-10 00:27:52 +00001478void StmtPrinter::VisitDesignatedInitUpdateExpr(
1479 DesignatedInitUpdateExpr *Node) {
1480 OS << "{";
1481 OS << "/*base*/";
1482 PrintExpr(Node->getBase());
1483 OS << ", ";
1484
1485 OS << "/*updater*/";
1486 PrintExpr(Node->getUpdater());
1487 OS << "}";
1488}
1489
1490void StmtPrinter::VisitNoInitExpr(NoInitExpr *Node) {
1491 OS << "/*no init*/";
1492}
1493
Douglas Gregor0202cb42009-01-29 17:44:32 +00001494void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001495 if (Policy.LangOpts.CPlusPlus) {
1496 OS << "/*implicit*/";
1497 Node->getType().print(OS, Policy);
1498 OS << "()";
1499 } else {
1500 OS << "/*implicit*/(";
1501 Node->getType().print(OS, Policy);
1502 OS << ')';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001503 if (Node->getType()->isRecordType())
1504 OS << "{}";
1505 else
1506 OS << 0;
1507 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001508}
1509
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001510void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman79635842009-05-30 04:20:30 +00001511 OS << "__builtin_va_arg(";
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001512 PrintExpr(Node->getSubExpr());
1513 OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001514 Node->getType().print(OS, Policy);
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001515 OS << ")";
1516}
1517
John McCallfe96e0b2011-11-06 09:01:30 +00001518void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1519 PrintExpr(Node->getSyntacticForm());
1520}
1521
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001522void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
Craig Topper36250ad2014-05-12 05:36:57 +00001523 const char *Name = nullptr;
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001524 switch (Node->getOp()) {
Richard Smithfeea8832012-04-12 05:08:17 +00001525#define BUILTIN(ID, TYPE, ATTRS)
1526#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1527 case AtomicExpr::AO ## ID: \
1528 Name = #ID "("; \
1529 break;
1530#include "clang/Basic/Builtins.def"
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001531 }
1532 OS << Name;
Richard Smithfeea8832012-04-12 05:08:17 +00001533
1534 // AtomicExpr stores its subexpressions in a permuted order.
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001535 PrintExpr(Node->getPtr());
Richard Smithfeea8832012-04-12 05:08:17 +00001536 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1537 Node->getOp() != AtomicExpr::AO__atomic_load_n) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001538 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001539 PrintExpr(Node->getVal1());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001540 }
Richard Smithfeea8832012-04-12 05:08:17 +00001541 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1542 Node->isCmpXChg()) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001543 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001544 PrintExpr(Node->getVal2());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001545 }
Richard Smithfeea8832012-04-12 05:08:17 +00001546 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1547 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
Richard Smithfeea8832012-04-12 05:08:17 +00001548 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001549 PrintExpr(Node->getWeak());
Richard Smithfeea8832012-04-12 05:08:17 +00001550 }
Richard Smithdf6bee82013-05-01 19:02:43 +00001551 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) {
1552 OS << ", ";
David Chisnallfa35df62012-01-16 17:27:18 +00001553 PrintExpr(Node->getOrder());
Richard Smithdf6bee82013-05-01 19:02:43 +00001554 }
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001555 if (Node->isCmpXChg()) {
1556 OS << ", ";
1557 PrintExpr(Node->getOrderFail());
1558 }
1559 OS << ")";
1560}
1561
Chris Lattnereefa10e2007-05-28 06:56:27 +00001562// C++
Douglas Gregor993603d2008-11-14 16:09:21 +00001563void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1564 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1565 "",
1566#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1567 Spelling,
1568#include "clang/Basic/OperatorKinds.def"
1569 };
1570
1571 OverloadedOperatorKind Kind = Node->getOperator();
1572 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1573 if (Node->getNumArgs() == 1) {
1574 OS << OpStrings[Kind] << ' ';
1575 PrintExpr(Node->getArg(0));
1576 } else {
1577 PrintExpr(Node->getArg(0));
1578 OS << ' ' << OpStrings[Kind];
1579 }
Eli Friedman8e236422012-10-12 22:45:14 +00001580 } else if (Kind == OO_Arrow) {
1581 PrintExpr(Node->getArg(0));
Douglas Gregor993603d2008-11-14 16:09:21 +00001582 } else if (Kind == OO_Call) {
1583 PrintExpr(Node->getArg(0));
1584 OS << '(';
1585 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1586 if (ArgIdx > 1)
1587 OS << ", ";
1588 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1589 PrintExpr(Node->getArg(ArgIdx));
1590 }
1591 OS << ')';
1592 } else if (Kind == OO_Subscript) {
1593 PrintExpr(Node->getArg(0));
1594 OS << '[';
1595 PrintExpr(Node->getArg(1));
1596 OS << ']';
1597 } else if (Node->getNumArgs() == 1) {
1598 OS << OpStrings[Kind] << ' ';
1599 PrintExpr(Node->getArg(0));
1600 } else if (Node->getNumArgs() == 2) {
1601 PrintExpr(Node->getArg(0));
1602 OS << ' ' << OpStrings[Kind] << ' ';
1603 PrintExpr(Node->getArg(1));
1604 } else {
David Blaikie83d382b2011-09-23 05:06:16 +00001605 llvm_unreachable("unknown overloaded operator");
Douglas Gregor993603d2008-11-14 16:09:21 +00001606 }
1607}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001608
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001609void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
Benjamin Kramer00e8a192014-02-25 18:03:55 +00001610 // If we have a conversion operator call only print the argument.
1611 CXXMethodDecl *MD = Node->getMethodDecl();
1612 if (MD && isa<CXXConversionDecl>(MD)) {
1613 PrintExpr(Node->getImplicitObjectArgument());
1614 return;
1615 }
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001616 VisitCallExpr(cast<CallExpr>(Node));
1617}
1618
Peter Collingbourne41f85462011-02-09 21:07:24 +00001619void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1620 PrintExpr(Node->getCallee());
1621 OS << "<<<";
1622 PrintCallArgs(Node->getConfig());
1623 OS << ">>>(";
1624 PrintCallArgs(Node);
1625 OS << ")";
1626}
1627
Douglas Gregore200adc2008-10-27 19:41:14 +00001628void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1629 OS << Node->getCastName() << '<';
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001630 Node->getTypeAsWritten().print(OS, Policy);
1631 OS << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +00001632 PrintExpr(Node->getSubExpr());
1633 OS << ")";
1634}
1635
Douglas Gregore200adc2008-10-27 19:41:14 +00001636void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1637 VisitCXXNamedCastExpr(Node);
1638}
1639
1640void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1641 VisitCXXNamedCastExpr(Node);
1642}
1643
1644void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1645 VisitCXXNamedCastExpr(Node);
1646}
1647
1648void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1649 VisitCXXNamedCastExpr(Node);
1650}
1651
Sebastian Redlc4704762008-11-11 11:37:55 +00001652void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1653 OS << "typeid(";
1654 if (Node->isTypeOperand()) {
David Majnemer143c55e2013-09-27 07:04:31 +00001655 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
Sebastian Redlc4704762008-11-11 11:37:55 +00001656 } else {
1657 PrintExpr(Node->getExprOperand());
1658 }
1659 OS << ")";
1660}
1661
Francois Pichet9f4f2072010-09-08 12:20:18 +00001662void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1663 OS << "__uuidof(";
1664 if (Node->isTypeOperand()) {
David Majnemer143c55e2013-09-27 07:04:31 +00001665 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
Francois Pichet9f4f2072010-09-08 12:20:18 +00001666 } else {
1667 PrintExpr(Node->getExprOperand());
1668 }
1669 OS << ")";
1670}
1671
John McCall5e77d762013-04-16 07:28:30 +00001672void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1673 PrintExpr(Node->getBaseExpr());
1674 if (Node->isArrow())
1675 OS << "->";
1676 else
1677 OS << ".";
1678 if (NestedNameSpecifier *Qualifier =
1679 Node->getQualifierLoc().getNestedNameSpecifier())
1680 Qualifier->print(OS, Policy);
1681 OS << Node->getPropertyDecl()->getDeclName();
1682}
1683
Richard Smithc67fdd42012-03-07 08:35:16 +00001684void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1685 switch (Node->getLiteralOperatorKind()) {
1686 case UserDefinedLiteral::LOK_Raw:
Richard Smith29e95952012-03-09 10:10:02 +00001687 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
Richard Smithc67fdd42012-03-07 08:35:16 +00001688 break;
1689 case UserDefinedLiteral::LOK_Template: {
Richard Smith29e95952012-03-09 10:10:02 +00001690 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1691 const TemplateArgumentList *Args =
1692 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1693 assert(Args);
David Majnemer314b72f2015-04-05 05:32:54 +00001694
1695 if (Args->size() != 1) {
1696 OS << "operator \"\" " << Node->getUDSuffix()->getName();
1697 TemplateSpecializationType::PrintTemplateArgumentList(
1698 OS, Args->data(), Args->size(), Policy);
1699 OS << "()";
1700 return;
1701 }
1702
Richard Smith29e95952012-03-09 10:10:02 +00001703 const TemplateArgument &Pack = Args->get(0);
Aaron Ballman2a89e852014-07-15 21:32:31 +00001704 for (const auto &P : Pack.pack_elements()) {
1705 char C = (char)P.getAsIntegral().getZExtValue();
Richard Smithc67fdd42012-03-07 08:35:16 +00001706 OS << C;
1707 }
1708 break;
1709 }
Richard Smith75025ba2012-03-08 09:02:38 +00001710 case UserDefinedLiteral::LOK_Integer: {
1711 // Print integer literal without suffix.
1712 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1713 OS << Int->getValue().toString(10, /*isSigned*/false);
1714 break;
1715 }
Benjamin Kramer8a526762012-09-20 14:07:17 +00001716 case UserDefinedLiteral::LOK_Floating: {
1717 // Print floating literal without suffix.
1718 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1719 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1720 break;
1721 }
Richard Smithc67fdd42012-03-07 08:35:16 +00001722 case UserDefinedLiteral::LOK_String:
1723 case UserDefinedLiteral::LOK_Character:
1724 PrintExpr(Node->getCookedLiteral());
1725 break;
1726 }
1727 OS << Node->getUDSuffix()->getName();
1728}
1729
Chris Lattnereefa10e2007-05-28 06:56:27 +00001730void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1731 OS << (Node->getValue() ? "true" : "false");
1732}
1733
Sebastian Redl576fd422009-05-10 18:38:11 +00001734void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1735 OS << "nullptr";
1736}
1737
Douglas Gregor97a9c812008-11-04 14:32:21 +00001738void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1739 OS << "this";
1740}
1741
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001742void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
Craig Topper36250ad2014-05-12 05:36:57 +00001743 if (!Node->getSubExpr())
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001744 OS << "throw";
1745 else {
1746 OS << "throw ";
1747 PrintExpr(Node->getSubExpr());
1748 }
1749}
1750
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001751void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
Richard Smith852c9db2013-04-20 22:23:05 +00001752 // Nothing to print: we picked up the default argument.
1753}
1754
1755void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1756 // Nothing to print: we picked up the default initializer.
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001757}
1758
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001759void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001760 Node->getType().print(OS, Policy);
Richard Smith1ae689c2015-01-28 22:06:01 +00001761 // If there are no parens, this is list-initialization, and the braces are
1762 // part of the syntax of the inner construct.
1763 if (Node->getLParenLoc().isValid())
1764 OS << "(";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001765 PrintExpr(Node->getSubExpr());
Richard Smith1ae689c2015-01-28 22:06:01 +00001766 if (Node->getLParenLoc().isValid())
1767 OS << ")";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001768}
1769
Anders Carlsson993a4b32009-05-30 20:03:25 +00001770void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1771 PrintExpr(Node->getSubExpr());
1772}
1773
Douglas Gregordd04d332009-01-16 18:33:17 +00001774void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001775 Node->getType().print(OS, Policy);
Richard Smithed83ebd2015-02-05 07:02:11 +00001776 if (Node->isStdInitListInitialization())
1777 /* Nothing to do; braces are part of creating the std::initializer_list. */;
1778 else if (Node->isListInitialization())
Richard Smith1ae689c2015-01-28 22:06:01 +00001779 OS << "{";
1780 else
1781 OS << "(";
Douglas Gregordd04d332009-01-16 18:33:17 +00001782 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001783 ArgEnd = Node->arg_end();
Douglas Gregordd04d332009-01-16 18:33:17 +00001784 Arg != ArgEnd; ++Arg) {
Benjamin Kramerf48ee442015-07-18 14:35:53 +00001785 if ((*Arg)->isDefaultArgument())
Rafael Espindola6f6f3c42013-05-24 16:11:44 +00001786 break;
Douglas Gregordd04d332009-01-16 18:33:17 +00001787 if (Arg != Node->arg_begin())
1788 OS << ", ";
1789 PrintExpr(*Arg);
1790 }
Richard Smithed83ebd2015-02-05 07:02:11 +00001791 if (Node->isStdInitListInitialization())
1792 /* See above. */;
1793 else if (Node->isListInitialization())
Richard Smith1ae689c2015-01-28 22:06:01 +00001794 OS << "}";
1795 else
1796 OS << ")";
Douglas Gregordd04d332009-01-16 18:33:17 +00001797}
1798
Douglas Gregore31e6062012-02-07 10:09:13 +00001799void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1800 OS << '[';
1801 bool NeedComma = false;
1802 switch (Node->getCaptureDefault()) {
1803 case LCD_None:
1804 break;
1805
1806 case LCD_ByCopy:
1807 OS << '=';
1808 NeedComma = true;
1809 break;
1810
1811 case LCD_ByRef:
1812 OS << '&';
1813 NeedComma = true;
1814 break;
1815 }
1816 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1817 CEnd = Node->explicit_capture_end();
1818 C != CEnd;
1819 ++C) {
1820 if (NeedComma)
1821 OS << ", ";
1822 NeedComma = true;
1823
1824 switch (C->getCaptureKind()) {
1825 case LCK_This:
1826 OS << "this";
1827 break;
1828
1829 case LCK_ByRef:
James Dennettdd2ffea22015-05-07 18:48:18 +00001830 if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C))
Douglas Gregore31e6062012-02-07 10:09:13 +00001831 OS << '&';
1832 OS << C->getCapturedVar()->getName();
1833 break;
1834
1835 case LCK_ByCopy:
Douglas Gregore31e6062012-02-07 10:09:13 +00001836 OS << C->getCapturedVar()->getName();
1837 break;
Alexey Bataev39c81e22014-08-28 04:28:19 +00001838 case LCK_VLAType:
1839 llvm_unreachable("VLA type in explicit captures.");
Douglas Gregore31e6062012-02-07 10:09:13 +00001840 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00001841
James Dennettdd2ffea22015-05-07 18:48:18 +00001842 if (Node->isInitCapture(C))
Richard Smithbb13c9a2013-09-28 04:02:39 +00001843 PrintExpr(C->getCapturedVar()->getInit());
Douglas Gregore31e6062012-02-07 10:09:13 +00001844 }
1845 OS << ']';
1846
1847 if (Node->hasExplicitParameters()) {
1848 OS << " (";
1849 CXXMethodDecl *Method = Node->getCallOperator();
1850 NeedComma = false;
Aaron Ballman43b68be2014-03-07 17:50:17 +00001851 for (auto P : Method->params()) {
Douglas Gregore31e6062012-02-07 10:09:13 +00001852 if (NeedComma) {
1853 OS << ", ";
1854 } else {
1855 NeedComma = true;
1856 }
Aaron Ballman43b68be2014-03-07 17:50:17 +00001857 std::string ParamStr = P->getNameAsString();
1858 P->getOriginalType().print(OS, Policy, ParamStr);
Douglas Gregore31e6062012-02-07 10:09:13 +00001859 }
1860 if (Method->isVariadic()) {
1861 if (NeedComma)
1862 OS << ", ";
1863 OS << "...";
1864 }
1865 OS << ')';
1866
1867 if (Node->isMutable())
1868 OS << " mutable";
1869
1870 const FunctionProtoType *Proto
1871 = Method->getType()->getAs<FunctionProtoType>();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001872 Proto->printExceptionSpecification(OS, Policy);
Douglas Gregore31e6062012-02-07 10:09:13 +00001873
Bill Wendling44426052012-12-20 19:22:21 +00001874 // FIXME: Attributes
Douglas Gregore31e6062012-02-07 10:09:13 +00001875
Douglas Gregor0c46b2b2012-02-13 22:00:16 +00001876 // Print the trailing return type if it was specified in the source.
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001877 if (Node->hasExplicitResultType()) {
1878 OS << " -> ";
Alp Toker314cc812014-01-25 16:55:45 +00001879 Proto->getReturnType().print(OS, Policy);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001880 }
Douglas Gregore31e6062012-02-07 10:09:13 +00001881 }
1882
1883 // Print the body.
1884 CompoundStmt *Body = Node->getBody();
1885 OS << ' ';
1886 PrintStmt(Body);
1887}
1888
Douglas Gregor747eb782010-07-08 06:14:04 +00001889void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregor2b88c112010-09-08 00:15:04 +00001890 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001891 TSInfo->getType().print(OS, Policy);
Douglas Gregor2b88c112010-09-08 00:15:04 +00001892 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001893 Node->getType().print(OS, Policy);
1894 OS << "()";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001895}
1896
Sebastian Redlbd150f42008-11-21 19:14:01 +00001897void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1898 if (E->isGlobalNew())
1899 OS << "::";
1900 OS << "new ";
1901 unsigned NumPlace = E->getNumPlacementArgs();
Eli Friedmana6fdfaa2012-10-18 20:54:37 +00001902 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00001903 OS << "(";
1904 PrintExpr(E->getPlacementArg(0));
1905 for (unsigned i = 1; i < NumPlace; ++i) {
Eli Friedmana6fdfaa2012-10-18 20:54:37 +00001906 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
1907 break;
Sebastian Redlbd150f42008-11-21 19:14:01 +00001908 OS << ", ";
1909 PrintExpr(E->getPlacementArg(i));
1910 }
1911 OS << ") ";
1912 }
1913 if (E->isParenTypeId())
1914 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001915 std::string TypeS;
1916 if (Expr *Size = E->getArraySize()) {
1917 llvm::raw_string_ostream s(TypeS);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001918 s << '[';
Richard Smith235341b2012-08-16 03:56:14 +00001919 Size->printPretty(s, Helper, Policy);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001920 s << ']';
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001921 }
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001922 E->getAllocatedType().print(OS, Policy, TypeS);
Sebastian Redlbd150f42008-11-21 19:14:01 +00001923 if (E->isParenTypeId())
1924 OS << ")";
1925
Sebastian Redl6047f072012-02-16 12:22:20 +00001926 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
1927 if (InitStyle) {
1928 if (InitStyle == CXXNewExpr::CallInit)
1929 OS << "(";
1930 PrintExpr(E->getInitializer());
1931 if (InitStyle == CXXNewExpr::CallInit)
1932 OS << ")";
Sebastian Redlbd150f42008-11-21 19:14:01 +00001933 }
1934}
1935
1936void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1937 if (E->isGlobalDelete())
1938 OS << "::";
1939 OS << "delete ";
1940 if (E->isArrayForm())
1941 OS << "[] ";
1942 PrintExpr(E->getArgument());
1943}
1944
Douglas Gregorad8a3362009-09-04 17:36:40 +00001945void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1946 PrintExpr(E->getBase());
1947 if (E->isArrow())
1948 OS << "->";
1949 else
1950 OS << '.';
1951 if (E->getQualifier())
1952 E->getQualifier()->print(OS, Policy);
Eli Friedman9cc8ac52012-10-23 20:26:57 +00001953 OS << "~";
Mike Stump11289f42009-09-09 15:08:12 +00001954
Douglas Gregor678f90d2010-02-25 01:56:36 +00001955 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1956 OS << II->getName();
1957 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001958 E->getDestroyedType().print(OS, Policy);
Douglas Gregorad8a3362009-09-04 17:36:40 +00001959}
1960
Anders Carlsson0781ce72009-04-23 02:32:43 +00001961void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Richard Smithed83ebd2015-02-05 07:02:11 +00001962 if (E->isListInitialization() && !E->isStdInitListInitialization())
Richard Smithd0e102f2015-01-30 02:04:26 +00001963 OS << "{";
Richard Smithd59b8322012-12-19 01:39:02 +00001964
Douglas Gregorbaba85d2011-01-24 17:25:03 +00001965 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1966 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1967 // Don't print any defaulted arguments
1968 break;
1969 }
1970
1971 if (i) OS << ", ";
1972 PrintExpr(E->getArg(i));
Fariborz Jahaniand0bbf662010-01-13 21:41:11 +00001973 }
Richard Smithd59b8322012-12-19 01:39:02 +00001974
Richard Smithed83ebd2015-02-05 07:02:11 +00001975 if (E->isListInitialization() && !E->isStdInitListInitialization())
Richard Smithd0e102f2015-01-30 02:04:26 +00001976 OS << "}";
Anders Carlsson0781ce72009-04-23 02:32:43 +00001977}
1978
Richard Smithcc1b96d2013-06-12 22:31:48 +00001979void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
1980 PrintExpr(E->getSubExpr());
1981}
1982
John McCall5d413782010-12-06 08:20:24 +00001983void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Alp Toker028ed912013-12-06 17:56:43 +00001984 // Just forward to the subexpression.
Anders Carlssondefc6442009-04-24 22:47:04 +00001985 PrintExpr(E->getSubExpr());
1986}
1987
Mike Stump11289f42009-09-09 15:08:12 +00001988void
Douglas Gregorce934142009-05-20 18:46:25 +00001989StmtPrinter::VisitCXXUnresolvedConstructExpr(
1990 CXXUnresolvedConstructExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001991 Node->getTypeAsWritten().print(OS, Policy);
Douglas Gregorce934142009-05-20 18:46:25 +00001992 OS << "(";
1993 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001994 ArgEnd = Node->arg_end();
Douglas Gregorce934142009-05-20 18:46:25 +00001995 Arg != ArgEnd; ++Arg) {
1996 if (Arg != Node->arg_begin())
1997 OS << ", ";
1998 PrintExpr(*Arg);
1999 }
2000 OS << ")";
2001}
2002
John McCall8cd78132009-11-19 22:55:06 +00002003void StmtPrinter::VisitCXXDependentScopeMemberExpr(
2004 CXXDependentScopeMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00002005 if (!Node->isImplicitAccess()) {
2006 PrintExpr(Node->getBase());
2007 OS << (Node->isArrow() ? "->" : ".");
2008 }
Douglas Gregorc26e0f62009-09-03 16:14:30 +00002009 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2010 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002011 if (Node->hasTemplateKeyword())
Douglas Gregor308047d2009-09-09 00:23:06 +00002012 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002013 OS << Node->getMemberNameInfo();
Benjamin Kramer9170e912013-02-22 15:46:01 +00002014 if (Node->hasExplicitTemplateArgs())
2015 TemplateSpecializationType::PrintTemplateArgumentList(
2016 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregora8db9542009-05-22 21:13:27 +00002017}
2018
John McCall10eae182009-11-30 22:42:35 +00002019void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00002020 if (!Node->isImplicitAccess()) {
2021 PrintExpr(Node->getBase());
2022 OS << (Node->isArrow() ? "->" : ".");
2023 }
John McCall10eae182009-11-30 22:42:35 +00002024 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
2025 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00002026 if (Node->hasTemplateKeyword())
2027 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002028 OS << Node->getMemberNameInfo();
Benjamin Kramer9170e912013-02-22 15:46:01 +00002029 if (Node->hasExplicitTemplateArgs())
2030 TemplateSpecializationType::PrintTemplateArgumentList(
2031 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
John McCall10eae182009-11-30 22:42:35 +00002032}
2033
Douglas Gregor29c42f22012-02-24 07:38:34 +00002034static const char *getTypeTraitName(TypeTrait TT) {
2035 switch (TT) {
Alp Toker95e7ff22014-01-01 05:57:51 +00002036#define TYPE_TRAIT_1(Spelling, Name, Key) \
2037case clang::UTT_##Name: return #Spelling;
Alp Tokercbb90342013-12-13 20:49:58 +00002038#define TYPE_TRAIT_2(Spelling, Name, Key) \
2039case clang::BTT_##Name: return #Spelling;
Alp Toker40f9b1c2013-12-12 21:23:03 +00002040#define TYPE_TRAIT_N(Spelling, Name, Key) \
2041 case clang::TT_##Name: return #Spelling;
2042#include "clang/Basic/TokenKinds.def"
Douglas Gregor29c42f22012-02-24 07:38:34 +00002043 }
2044 llvm_unreachable("Type trait not covered by switch");
2045}
2046
John Wiegley6242b6a2011-04-28 00:16:57 +00002047static const char *getTypeTraitName(ArrayTypeTrait ATT) {
2048 switch (ATT) {
2049 case ATT_ArrayRank: return "__array_rank";
2050 case ATT_ArrayExtent: return "__array_extent";
2051 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00002052 llvm_unreachable("Array type trait not covered by switch");
John Wiegley6242b6a2011-04-28 00:16:57 +00002053}
2054
John Wiegleyf9f65842011-04-25 06:54:41 +00002055static const char *getExpressionTraitName(ExpressionTrait ET) {
2056 switch (ET) {
John Wiegleyf9f65842011-04-25 06:54:41 +00002057 case ET_IsLValueExpr: return "__is_lvalue_expr";
2058 case ET_IsRValueExpr: return "__is_rvalue_expr";
2059 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00002060 llvm_unreachable("Expression type trait not covered by switch");
John Wiegleyf9f65842011-04-25 06:54:41 +00002061}
2062
Douglas Gregor29c42f22012-02-24 07:38:34 +00002063void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
2064 OS << getTypeTraitName(E->getTrait()) << "(";
2065 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
2066 if (I > 0)
2067 OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002068 E->getArg(I)->getType().print(OS, Policy);
Douglas Gregor29c42f22012-02-24 07:38:34 +00002069 }
2070 OS << ")";
2071}
2072
John Wiegley6242b6a2011-04-28 00:16:57 +00002073void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002074 OS << getTypeTraitName(E->getTrait()) << '(';
2075 E->getQueriedType().print(OS, Policy);
2076 OS << ')';
John Wiegley6242b6a2011-04-28 00:16:57 +00002077}
2078
John Wiegleyf9f65842011-04-25 06:54:41 +00002079void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002080 OS << getExpressionTraitName(E->getTrait()) << '(';
2081 PrintExpr(E->getQueriedExpression());
2082 OS << ')';
John Wiegleyf9f65842011-04-25 06:54:41 +00002083}
2084
Sebastian Redl4202c0f2010-09-10 20:55:43 +00002085void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
2086 OS << "noexcept(";
2087 PrintExpr(E->getOperand());
2088 OS << ")";
2089}
2090
Douglas Gregor820ba7b2011-01-04 17:33:58 +00002091void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregore8e9dd62011-01-03 17:17:50 +00002092 PrintExpr(E->getPattern());
2093 OS << "...";
2094}
2095
Douglas Gregor820ba7b2011-01-04 17:33:58 +00002096void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00002097 OS << "sizeof...(" << *E->getPack() << ")";
Douglas Gregor820ba7b2011-01-04 17:33:58 +00002098}
2099
Douglas Gregorcdbc5392011-01-15 01:15:58 +00002100void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
2101 SubstNonTypeTemplateParmPackExpr *Node) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00002102 OS << *Node->getParameterPack();
Douglas Gregorcdbc5392011-01-15 01:15:58 +00002103}
2104
John McCall7c454bb2011-07-15 05:09:51 +00002105void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
2106 SubstNonTypeTemplateParmExpr *Node) {
2107 Visit(Node->getReplacement());
2108}
2109
Richard Smithb15fe3a2012-09-12 00:56:43 +00002110void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
2111 OS << *E->getParameterPack();
2112}
2113
Douglas Gregorfe314812011-06-21 17:03:29 +00002114void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
2115 PrintExpr(Node->GetTemporaryExpr());
2116}
2117
Richard Smith0f0af192014-11-08 05:07:16 +00002118void StmtPrinter::VisitCXXFoldExpr(CXXFoldExpr *E) {
2119 OS << "(";
2120 if (E->getLHS()) {
2121 PrintExpr(E->getLHS());
2122 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2123 }
2124 OS << "...";
2125 if (E->getRHS()) {
2126 OS << " " << BinaryOperator::getOpcodeStr(E->getOperator()) << " ";
2127 PrintExpr(E->getRHS());
2128 }
2129 OS << ")";
2130}
2131
Mike Stump11289f42009-09-09 15:08:12 +00002132// Obj-C
Anders Carlsson76f4a902007-08-21 17:43:55 +00002133
2134void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
2135 OS << "@";
2136 VisitStringLiteral(Node->getString());
2137}
Chris Lattnereefa10e2007-05-28 06:56:27 +00002138
Patrick Beard0caa3942012-04-19 00:25:12 +00002139void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00002140 OS << "@";
Patrick Beard0caa3942012-04-19 00:25:12 +00002141 Visit(E->getSubExpr());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002142}
2143
2144void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
2145 OS << "@[ ";
Benjamin Kramer5733e352015-07-18 17:09:36 +00002146 ObjCArrayLiteral::child_range Ch = E->children();
2147 for (auto I = Ch.begin(), E = Ch.end(); I != E; ++I) {
2148 if (I != Ch.begin())
Ted Kremeneke65b0862012-03-06 20:05:56 +00002149 OS << ", ";
Benjamin Kramer5733e352015-07-18 17:09:36 +00002150 Visit(*I);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002151 }
2152 OS << " ]";
2153}
2154
2155void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
2156 OS << "@{ ";
2157 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
2158 if (I > 0)
2159 OS << ", ";
2160
2161 ObjCDictionaryElement Element = E->getKeyValueElement(I);
2162 Visit(Element.Key);
2163 OS << " : ";
2164 Visit(Element.Value);
2165 if (Element.isPackExpansion())
2166 OS << "...";
2167 }
2168 OS << " }";
2169}
2170
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002171void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002172 OS << "@encode(";
2173 Node->getEncodedType().print(OS, Policy);
2174 OS << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002175}
2176
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002177void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00002178 OS << "@selector(";
2179 Node->getSelector().print(OS);
2180 OS << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002181}
2182
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002183void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00002184 OS << "@protocol(" << *Node->getProtocol() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002185}
2186
Steve Naroffd54978b2007-09-18 23:55:05 +00002187void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
2188 OS << "[";
Douglas Gregor9a129192010-04-21 00:45:42 +00002189 switch (Mess->getReceiverKind()) {
2190 case ObjCMessageExpr::Instance:
2191 PrintExpr(Mess->getInstanceReceiver());
2192 break;
2193
2194 case ObjCMessageExpr::Class:
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002195 Mess->getClassReceiver().print(OS, Policy);
Douglas Gregor9a129192010-04-21 00:45:42 +00002196 break;
2197
2198 case ObjCMessageExpr::SuperInstance:
2199 case ObjCMessageExpr::SuperClass:
2200 OS << "Super";
2201 break;
2202 }
2203
Ted Kremeneka06e7122008-05-02 17:32:38 +00002204 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00002205 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00002206 if (selector.isUnarySelector()) {
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00002207 OS << selector.getNameForSlot(0);
Steve Naroffc6814ea2007-10-02 20:01:56 +00002208 } else {
2209 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00002210 if (i < selector.getNumArgs()) {
2211 if (i > 0) OS << ' ';
2212 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00002213 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00002214 else
2215 OS << ":";
2216 }
2217 else OS << ", "; // Handle variadic methods.
Mike Stump11289f42009-09-09 15:08:12 +00002218
Steve Naroffc6814ea2007-10-02 20:01:56 +00002219 PrintExpr(Mess->getArg(i));
2220 }
Steve Naroffd54978b2007-09-18 23:55:05 +00002221 }
2222 OS << "]";
2223}
2224
Ted Kremeneke65b0862012-03-06 20:05:56 +00002225void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
2226 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
2227}
2228
John McCall31168b02011-06-15 23:02:42 +00002229void
2230StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
2231 PrintExpr(E->getSubExpr());
2232}
2233
2234void
2235StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002236 OS << '(' << E->getBridgeKindName();
2237 E->getType().print(OS, Policy);
2238 OS << ')';
John McCall31168b02011-06-15 23:02:42 +00002239 PrintExpr(E->getSubExpr());
2240}
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002241
Steve Naroffc540d662008-09-03 18:15:37 +00002242void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00002243 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00002244 OS << "^";
Mike Stump11289f42009-09-09 15:08:12 +00002245
Steve Naroffc540d662008-09-03 18:15:37 +00002246 const FunctionType *AFT = Node->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00002247
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002248 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroffc540d662008-09-03 18:15:37 +00002249 OS << "()";
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002250 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00002251 OS << '(';
Steve Naroff415d3d52008-10-08 17:01:13 +00002252 for (BlockDecl::param_iterator AI = BD->param_begin(),
2253 E = BD->param_end(); AI != E; ++AI) {
2254 if (AI != BD->param_begin()) OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002255 std::string ParamStr = (*AI)->getNameAsString();
2256 (*AI)->getType().print(OS, Policy, ParamStr);
Steve Naroffc540d662008-09-03 18:15:37 +00002257 }
Mike Stump11289f42009-09-09 15:08:12 +00002258
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002259 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00002260 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00002261 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00002262 OS << "...";
2263 }
2264 OS << ')';
2265 }
Fariborz Jahanian1ace8cb2012-12-04 21:15:23 +00002266 OS << "{ }";
Steve Naroffc540d662008-09-03 18:15:37 +00002267}
2268
Ted Kremenekf551f322011-11-30 22:08:08 +00002269void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
2270 PrintExpr(Node->getSourceExpr());
2271}
John McCall8d69a212010-11-15 23:31:06 +00002272
Kaelyn Takatae1f49d52014-10-27 18:07:20 +00002273void StmtPrinter::VisitTypoExpr(TypoExpr *Node) {
2274 // TODO: Print something reasonable for a TypoExpr, if necessary.
2275 assert(false && "Cannot print TypoExpr nodes");
2276}
2277
Tanya Lattner55808c12011-06-04 00:47:47 +00002278void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
2279 OS << "__builtin_astype(";
2280 PrintExpr(Node->getSrcExpr());
Benjamin Kramerc5720e92013-02-22 14:19:01 +00002281 OS << ", ";
2282 Node->getType().print(OS, Policy);
Tanya Lattner55808c12011-06-04 00:47:47 +00002283 OS << ")";
2284}
2285
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00002286//===----------------------------------------------------------------------===//
2287// Stmt method implementations
2288//===----------------------------------------------------------------------===//
2289
Craig Topperc571c812013-08-22 06:02:26 +00002290void Stmt::dumpPretty(const ASTContext &Context) const {
Craig Topper36250ad2014-05-12 05:36:57 +00002291 printPretty(llvm::errs(), nullptr, PrintingPolicy(Context.getLangOpts()));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00002292}
2293
Richard Smith235341b2012-08-16 03:56:14 +00002294void Stmt::printPretty(raw_ostream &OS,
2295 PrinterHelper *Helper,
Douglas Gregor7de59662009-05-29 20:38:28 +00002296 const PrintingPolicy &Policy,
2297 unsigned Indentation) const {
Richard Smith235341b2012-08-16 03:56:14 +00002298 StmtPrinter P(OS, Helper, Policy, Indentation);
Chris Lattner62249a62007-08-21 04:04:25 +00002299 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00002300}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002301
2302//===----------------------------------------------------------------------===//
2303// PrinterHelper
2304//===----------------------------------------------------------------------===//
2305
2306// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +00002307PrinterHelper::~PrinterHelper() {}