blob: 2371b93fe4d1835b8644b6e3031dcd41760583e3 [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 };
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000107}
108
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) {
171 OS << "[[";
172 bool first = true;
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000173 for (ArrayRef<const Attr*>::iterator it = Node->getAttrs().begin(),
174 end = Node->getAttrs().end();
175 it != end; ++it) {
Richard Smithc202b282012-04-14 00:33:13 +0000176 if (!first) {
177 OS << ", ";
178 first = false;
179 }
180 // TODO: check this
Richard Smith52f04a22012-08-16 02:43:29 +0000181 (*it)->printPretty(OS, Policy);
Richard Smithc202b282012-04-14 00:33:13 +0000182 }
183 OS << "]] ";
184 PrintStmt(Node->getSubStmt(), 0);
185}
186
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000187void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000188 OS << "if (";
Eli Friedmanf86e5072012-10-16 23:45:15 +0000189 if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
190 PrintRawDeclStmt(DS);
191 else
192 PrintExpr(If->getCond());
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000193 OS << ')';
Mike Stump11289f42009-09-09 15:08:12 +0000194
Chris Lattner073926e2007-05-20 23:04:55 +0000195 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
196 OS << ' ';
197 PrintRawCompoundStmt(CS);
198 OS << (If->getElse() ? ' ' : '\n');
199 } else {
200 OS << '\n';
201 PrintStmt(If->getThen());
202 if (If->getElse()) Indent();
203 }
Mike Stump11289f42009-09-09 15:08:12 +0000204
Chris Lattner073926e2007-05-20 23:04:55 +0000205 if (Stmt *Else = If->getElse()) {
206 OS << "else";
Mike Stump11289f42009-09-09 15:08:12 +0000207
Chris Lattner073926e2007-05-20 23:04:55 +0000208 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
209 OS << ' ';
210 PrintRawCompoundStmt(CS);
211 OS << '\n';
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000212 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
213 OS << ' ';
214 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000215 } else {
216 OS << '\n';
217 PrintStmt(If->getElse());
218 }
Chris Lattner882f7882006-11-04 18:52:07 +0000219 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000220}
221
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000222void StmtPrinter::VisitIfStmt(IfStmt *If) {
223 Indent();
224 PrintRawIfStmt(If);
225}
226
Chris Lattnerf2174b62006-11-04 20:59:27 +0000227void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
228 Indent() << "switch (";
Eli Friedmanf86e5072012-10-16 23:45:15 +0000229 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
230 PrintRawDeclStmt(DS);
231 else
232 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000233 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000234
Chris Lattner073926e2007-05-20 23:04:55 +0000235 // Pretty print compoundstmt bodies (very common).
236 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
237 OS << " ";
238 PrintRawCompoundStmt(CS);
239 OS << "\n";
240 } else {
241 OS << "\n";
242 PrintStmt(Node->getBody());
243 }
Chris Lattnerf2174b62006-11-04 20:59:27 +0000244}
245
Chris Lattner85ed8732006-11-04 20:40:44 +0000246void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
247 Indent() << "while (";
Eli Friedmanf86e5072012-10-16 23:45:15 +0000248 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
249 PrintRawDeclStmt(DS);
250 else
251 PrintExpr(Node->getCond());
Chris Lattner85ed8732006-11-04 20:40:44 +0000252 OS << ")\n";
253 PrintStmt(Node->getBody());
254}
255
256void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000257 Indent() << "do ";
258 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
259 PrintRawCompoundStmt(CS);
260 OS << " ";
261 } else {
262 OS << "\n";
263 PrintStmt(Node->getBody());
264 Indent();
265 }
Mike Stump11289f42009-09-09 15:08:12 +0000266
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000267 OS << "while (";
Chris Lattner85ed8732006-11-04 20:40:44 +0000268 PrintExpr(Node->getCond());
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000269 OS << ");\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000270}
271
Chris Lattner71e23ce2006-11-04 20:18:38 +0000272void StmtPrinter::VisitForStmt(ForStmt *Node) {
273 Indent() << "for (";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000274 if (Node->getInit()) {
275 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000276 PrintRawDeclStmt(DS);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000277 else
278 PrintExpr(cast<Expr>(Node->getInit()));
279 }
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000280 OS << ";";
281 if (Node->getCond()) {
282 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000283 PrintExpr(Node->getCond());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000284 }
285 OS << ";";
286 if (Node->getInc()) {
287 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000288 PrintExpr(Node->getInc());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000289 }
290 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000291
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000292 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
293 PrintRawCompoundStmt(CS);
294 OS << "\n";
295 } else {
296 OS << "\n";
297 PrintStmt(Node->getBody());
298 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000299}
300
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000301void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian83615522008-01-02 22:54:34 +0000302 Indent() << "for (";
303 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000304 PrintRawDeclStmt(DS);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000305 else
306 PrintExpr(cast<Expr>(Node->getElement()));
307 OS << " in ";
308 PrintExpr(Node->getCollection());
309 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000310
Fariborz Jahanian83615522008-01-02 22:54:34 +0000311 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
312 PrintRawCompoundStmt(CS);
313 OS << "\n";
314 } else {
315 OS << "\n";
316 PrintStmt(Node->getBody());
317 }
318}
319
Richard Smith02e85f32011-04-14 22:09:26 +0000320void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
321 Indent() << "for (";
322 PrintingPolicy SubPolicy(Policy);
323 SubPolicy.SuppressInitializers = true;
324 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
325 OS << " : ";
326 PrintExpr(Node->getRangeInit());
327 OS << ") {\n";
328 PrintStmt(Node->getBody());
Ted Kremenek88446602013-12-11 23:44:02 +0000329 Indent() << "}";
330 if (Policy.IncludeNewlines) OS << "\n";
Richard Smith02e85f32011-04-14 22:09:26 +0000331}
332
Douglas Gregordeb4a2be2011-10-25 01:33:02 +0000333void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
334 Indent();
335 if (Node->isIfExists())
336 OS << "__if_exists (";
337 else
338 OS << "__if_not_exists (";
339
340 if (NestedNameSpecifier *Qualifier
341 = Node->getQualifierLoc().getNestedNameSpecifier())
342 Qualifier->print(OS, Policy);
343
344 OS << Node->getNameInfo() << ") ";
345
346 PrintRawCompoundStmt(Node->getSubStmt());
347}
348
Chris Lattner16976d32006-11-05 01:46:01 +0000349void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000350 Indent() << "goto " << Node->getLabel()->getName() << ";";
351 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000352}
353
354void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000355 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000356 PrintExpr(Node->getTarget());
Ted Kremenek88446602013-12-11 23:44:02 +0000357 OS << ";";
358 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000359}
360
361void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000362 Indent() << "continue;";
363 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000364}
365
366void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000367 Indent() << "break;";
368 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000369}
370
371
Chris Lattner882f7882006-11-04 18:52:07 +0000372void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
373 Indent() << "return";
374 if (Node->getRetValue()) {
375 OS << " ";
376 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000377 }
Ted Kremenek88446602013-12-11 23:44:02 +0000378 OS << ";";
379 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000380}
381
Chris Lattner73c56c02007-10-29 04:04:16 +0000382
Chad Rosierde70e0e2012-08-25 00:11:56 +0000383void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
Anders Carlsson660bdd12007-11-23 23:12:25 +0000384 Indent() << "asm ";
Mike Stump11289f42009-09-09 15:08:12 +0000385
Anders Carlsson660bdd12007-11-23 23:12:25 +0000386 if (Node->isVolatile())
387 OS << "volatile ";
Mike Stump11289f42009-09-09 15:08:12 +0000388
Anders Carlsson660bdd12007-11-23 23:12:25 +0000389 OS << "(";
Anders Carlsson81a5a692007-11-20 19:21:03 +0000390 VisitStringLiteral(Node->getAsmString());
Mike Stump11289f42009-09-09 15:08:12 +0000391
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000392 // Outputs
393 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
394 Node->getNumClobbers() != 0)
395 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000396
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000397 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
398 if (i != 0)
399 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000400
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000401 if (!Node->getOutputName(i).empty()) {
402 OS << '[';
403 OS << Node->getOutputName(i);
404 OS << "] ";
405 }
Mike Stump11289f42009-09-09 15:08:12 +0000406
Chris Lattner72bbf172009-03-10 04:59:06 +0000407 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000408 OS << " ";
409 Visit(Node->getOutputExpr(i));
410 }
Mike Stump11289f42009-09-09 15:08:12 +0000411
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000412 // Inputs
413 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
414 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000415
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000416 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
417 if (i != 0)
418 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000419
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000420 if (!Node->getInputName(i).empty()) {
421 OS << '[';
422 OS << Node->getInputName(i);
423 OS << "] ";
424 }
Mike Stump11289f42009-09-09 15:08:12 +0000425
Chris Lattner72bbf172009-03-10 04:59:06 +0000426 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000427 OS << " ";
428 Visit(Node->getInputExpr(i));
429 }
Mike Stump11289f42009-09-09 15:08:12 +0000430
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000431 // Clobbers
432 if (Node->getNumClobbers() != 0)
433 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000434
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000435 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
436 if (i != 0)
437 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000438
Chad Rosierd9fb09a2012-08-27 23:28:41 +0000439 VisitStringLiteral(Node->getClobberStringLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000440 }
Mike Stump11289f42009-09-09 15:08:12 +0000441
Ted Kremenek88446602013-12-11 23:44:02 +0000442 OS << ");";
443 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner73c56c02007-10-29 04:04:16 +0000444}
445
Chad Rosier32503022012-06-11 20:47:18 +0000446void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
447 // FIXME: Implement MS style inline asm statement printer.
Chad Rosierb6f46c12012-08-15 16:53:30 +0000448 Indent() << "__asm ";
449 if (Node->hasBraces())
450 OS << "{\n";
John McCallf413f5e2013-05-03 00:10:13 +0000451 OS << Node->getAsmString() << "\n";
Chad Rosierb6f46c12012-08-15 16:53:30 +0000452 if (Node->hasBraces())
453 Indent() << "}\n";
Chad Rosier32503022012-06-11 20:47:18 +0000454}
455
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000456void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000457 PrintStmt(Node->getCapturedDecl()->getBody());
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000458}
459
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000460void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000461 Indent() << "@try";
462 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
463 PrintRawCompoundStmt(TS);
464 OS << "\n";
465 }
Mike Stump11289f42009-09-09 15:08:12 +0000466
Douglas Gregor96c79492010-04-23 22:50:49 +0000467 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
468 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000469 Indent() << "@catch(";
Steve Naroff371b8fb2009-03-03 19:52:17 +0000470 if (catchStmt->getCatchParamDecl()) {
471 if (Decl *DS = catchStmt->getCatchParamDecl())
472 PrintRawDecl(DS);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000473 }
474 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000475 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
476 PrintRawCompoundStmt(CS);
477 OS << "\n";
478 }
Fariborz Jahanian88157952007-11-02 18:16:07 +0000479 }
Mike Stump11289f42009-09-09 15:08:12 +0000480
481 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
482 Node->getFinallyStmt())) {
Fariborz Jahaniandefbf9a2007-11-07 00:46:42 +0000483 Indent() << "@finally";
484 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian88157952007-11-02 18:16:07 +0000485 OS << "\n";
Mike Stump11289f42009-09-09 15:08:12 +0000486 }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000487}
488
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000489void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000490}
491
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000492void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000493 Indent() << "@catch (...) { /* todo */ } \n";
494}
495
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000496void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000497 Indent() << "@throw";
498 if (Node->getThrowExpr()) {
499 OS << " ";
500 PrintExpr(Node->getThrowExpr());
501 }
502 OS << ";\n";
503}
504
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000505void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000506 Indent() << "@synchronized (";
507 PrintExpr(Node->getSynchExpr());
508 OS << ")";
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000509 PrintRawCompoundStmt(Node->getSynchBody());
510 OS << "\n";
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000511}
512
John McCall31168b02011-06-15 23:02:42 +0000513void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
514 Indent() << "@autoreleasepool";
515 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
516 OS << "\n";
517}
518
Sebastian Redl9b244a82008-12-22 21:35:02 +0000519void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
520 OS << "catch (";
Sebastian Redl54c04d42008-12-22 19:15:10 +0000521 if (Decl *ExDecl = Node->getExceptionDecl())
522 PrintRawDecl(ExDecl);
523 else
524 OS << "...";
525 OS << ") ";
526 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl9b244a82008-12-22 21:35:02 +0000527}
528
529void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
530 Indent();
531 PrintRawCXXCatchStmt(Node);
532 OS << "\n";
533}
534
535void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
536 Indent() << "try ";
537 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump11289f42009-09-09 15:08:12 +0000538 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl9b244a82008-12-22 21:35:02 +0000539 OS << " ";
540 PrintRawCXXCatchStmt(Node->getHandler(i));
541 }
Sebastian Redl54c04d42008-12-22 19:15:10 +0000542 OS << "\n";
543}
544
John Wiegley1c0675e2011-04-28 01:08:34 +0000545void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
546 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
547 PrintRawCompoundStmt(Node->getTryBlock());
548 SEHExceptStmt *E = Node->getExceptHandler();
549 SEHFinallyStmt *F = Node->getFinallyHandler();
550 if(E)
551 PrintRawSEHExceptHandler(E);
552 else {
553 assert(F && "Must have a finally block...");
554 PrintRawSEHFinallyStmt(F);
555 }
556 OS << "\n";
557}
558
559void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
560 OS << "__finally ";
561 PrintRawCompoundStmt(Node->getBlock());
562 OS << "\n";
563}
564
565void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
566 OS << "__except (";
567 VisitExpr(Node->getFilterExpr());
Joao Matos566359c2012-09-04 17:49:35 +0000568 OS << ")\n";
John Wiegley1c0675e2011-04-28 01:08:34 +0000569 PrintRawCompoundStmt(Node->getBlock());
570 OS << "\n";
571}
572
573void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
574 Indent();
575 PrintRawSEHExceptHandler(Node);
576 OS << "\n";
577}
578
579void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
580 Indent();
581 PrintRawSEHFinallyStmt(Node);
582 OS << "\n";
583}
584
Chris Lattner71e23ce2006-11-04 20:18:38 +0000585//===----------------------------------------------------------------------===//
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000586// OpenMP clauses printing methods
587//===----------------------------------------------------------------------===//
588
589namespace {
590class OMPClausePrinter : public OMPClauseVisitor<OMPClausePrinter> {
591 raw_ostream &OS;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000592 const PrintingPolicy &Policy;
Alexey Bataev756c1962013-09-24 03:17:45 +0000593 /// \brief Process clauses with list of variables.
594 template <typename T>
595 void VisitOMPClauseList(T *Node, char StartSym);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000596public:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000597 OMPClausePrinter(raw_ostream &OS, const PrintingPolicy &Policy)
598 : OS(OS), Policy(Policy) { }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000599#define OPENMP_CLAUSE(Name, Class) \
600 void Visit##Class(Class *S);
601#include "clang/Basic/OpenMPKinds.def"
602};
603
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000604void OMPClausePrinter::VisitOMPIfClause(OMPIfClause *Node) {
605 OS << "if(";
606 Node->getCondition()->printPretty(OS, 0, Policy, 0);
607 OS << ")";
608}
609
Alexey Bataev568a8332014-03-06 06:15:19 +0000610void OMPClausePrinter::VisitOMPNumThreadsClause(OMPNumThreadsClause *Node) {
611 OS << "num_threads(";
612 Node->getNumThreads()->printPretty(OS, 0, Policy, 0);
613 OS << ")";
614}
615
Alexey Bataev62c87d22014-03-21 04:51:18 +0000616void OMPClausePrinter::VisitOMPSafelenClause(OMPSafelenClause *Node) {
617 OS << "safelen(";
618 Node->getSafelen()->printPretty(OS, 0, Policy, 0);
619 OS << ")";
620}
621
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000622void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
623 OS << "default("
624 << getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind())
625 << ")";
626}
627
Alexey Bataev756c1962013-09-24 03:17:45 +0000628template<typename T>
629void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
630 for (typename T::varlist_iterator I = Node->varlist_begin(),
631 E = Node->varlist_end();
632 I != E; ++I)
633 OS << (I == Node->varlist_begin() ? StartSym : ',')
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000634 << *cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl());
Alexey Bataev756c1962013-09-24 03:17:45 +0000635}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000636
637void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) {
638 if (!Node->varlist_empty()) {
639 OS << "private";
Alexey Bataev756c1962013-09-24 03:17:45 +0000640 VisitOMPClauseList(Node, '(');
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000641 OS << ")";
642 }
643}
644
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000645void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) {
646 if (!Node->varlist_empty()) {
647 OS << "firstprivate";
648 VisitOMPClauseList(Node, '(');
649 OS << ")";
650 }
651}
652
Alexey Bataev758e55e2013-09-06 18:03:48 +0000653void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
654 if (!Node->varlist_empty()) {
655 OS << "shared";
Alexey Bataev756c1962013-09-24 03:17:45 +0000656 VisitOMPClauseList(Node, '(');
Alexey Bataev758e55e2013-09-06 18:03:48 +0000657 OS << ")";
658 }
659}
660
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000661}
662
663//===----------------------------------------------------------------------===//
664// OpenMP directives printing methods
665//===----------------------------------------------------------------------===//
666
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000667void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000668 OMPClausePrinter Printer(OS, Policy);
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000669 ArrayRef<OMPClause *> Clauses = S->clauses();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000670 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
671 I != E; ++I)
672 if (*I && !(*I)->isImplicit()) {
673 Printer.Visit(*I);
674 OS << ' ';
675 }
676 OS << "\n";
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000677 if (S->getAssociatedStmt()) {
678 assert(isa<CapturedStmt>(S->getAssociatedStmt()) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000679 "Expected captured statement!");
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000680 Stmt *CS = cast<CapturedStmt>(S->getAssociatedStmt())->getCapturedStmt();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000681 PrintStmt(CS);
682 }
683}
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000684
685void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
686 Indent() << "#pragma omp parallel ";
687 PrintOMPExecutableDirective(Node);
688}
689
690void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
691 Indent() << "#pragma omp simd ";
692 PrintOMPExecutableDirective(Node);
693}
694
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000695//===----------------------------------------------------------------------===//
Chris Lattner71e23ce2006-11-04 20:18:38 +0000696// Expr printing methods.
697//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000698
Chris Lattner882f7882006-11-04 18:52:07 +0000699void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000700 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
701 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000702 if (Node->hasTemplateKeyword())
703 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000704 OS << Node->getNameInfo();
John McCallb3774b52010-08-19 23:49:38 +0000705 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +0000706 TemplateSpecializationType::PrintTemplateArgumentList(
707 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000708}
709
John McCall8cd78132009-11-19 22:55:06 +0000710void StmtPrinter::VisitDependentScopeDeclRefExpr(
711 DependentScopeDeclRefExpr *Node) {
Axel Naumann20b27862011-01-24 15:44:00 +0000712 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
713 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000714 if (Node->hasTemplateKeyword())
715 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000716 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000717 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +0000718 TemplateSpecializationType::PrintTemplateArgumentList(
719 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregor90a1a652009-03-19 17:26:29 +0000720}
721
John McCalld14a8642009-11-21 08:51:07 +0000722void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregora727cb92009-06-30 22:34:41 +0000723 if (Node->getQualifier())
724 Node->getQualifier()->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000725 if (Node->hasTemplateKeyword())
726 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000727 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000728 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +0000729 TemplateSpecializationType::PrintTemplateArgumentList(
730 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregora727cb92009-06-30 22:34:41 +0000731}
732
Steve Naroffe46504b2007-11-12 14:29:37 +0000733void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000734 if (Node->getBase()) {
735 PrintExpr(Node->getBase());
736 OS << (Node->isArrow() ? "->" : ".");
737 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000738 OS << *Node->getDecl();
Steve Naroffe46504b2007-11-12 14:29:37 +0000739}
740
Steve Naroffec944032008-05-30 00:40:33 +0000741void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000742 if (Node->isSuperReceiver())
743 OS << "super.";
Richard Trieu6d92e502014-02-06 23:26:23 +0000744 else if (Node->isObjectReceiver() && Node->getBase()) {
Steve Naroffec944032008-05-30 00:40:33 +0000745 PrintExpr(Node->getBase());
746 OS << ".";
Richard Trieu6d92e502014-02-06 23:26:23 +0000747 } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
748 OS << Node->getClassReceiver()->getName() << ".";
Steve Naroffec944032008-05-30 00:40:33 +0000749 }
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000750
John McCallb7bd14f2010-12-02 01:19:52 +0000751 if (Node->isImplicitProperty())
Aaron Ballmanb190f972014-01-03 17:59:55 +0000752 Node->getImplicitPropertyGetter()->getSelector().print(OS);
John McCallb7bd14f2010-12-02 01:19:52 +0000753 else
754 OS << Node->getExplicitProperty()->getName();
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000755}
756
Ted Kremeneke65b0862012-03-06 20:05:56 +0000757void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
758
759 PrintExpr(Node->getBaseExpr());
760 OS << "[";
761 PrintExpr(Node->getKeyExpr());
762 OS << "]";
763}
764
Chris Lattner6307f192008-08-10 01:53:14 +0000765void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson625bfc82007-07-21 05:21:51 +0000766 switch (Node->getIdentType()) {
767 default:
David Blaikie83d382b2011-09-23 05:06:16 +0000768 llvm_unreachable("unknown case");
Chris Lattner6307f192008-08-10 01:53:14 +0000769 case PredefinedExpr::Func:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000770 OS << "__func__";
771 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000772 case PredefinedExpr::Function:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000773 OS << "__FUNCTION__";
774 break;
David Majnemerbed356a2013-11-06 23:31:56 +0000775 case PredefinedExpr::FuncDName:
776 OS << "__FUNCDNAME__";
777 break;
Nico Weber3a691a32012-06-23 02:07:59 +0000778 case PredefinedExpr::LFunction:
779 OS << "L__FUNCTION__";
780 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000781 case PredefinedExpr::PrettyFunction:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000782 OS << "__PRETTY_FUNCTION__";
783 break;
784 }
785}
786
Steve Naroffae4143e2007-04-26 20:39:23 +0000787void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000788 unsigned value = Node->getValue();
Douglas Gregorfb65e592011-07-27 05:40:30 +0000789
790 switch (Node->getKind()) {
791 case CharacterLiteral::Ascii: break; // no prefix.
792 case CharacterLiteral::Wide: OS << 'L'; break;
793 case CharacterLiteral::UTF16: OS << 'u'; break;
794 case CharacterLiteral::UTF32: OS << 'U'; break;
795 }
796
Chris Lattner666115c2007-07-13 23:58:20 +0000797 switch (value) {
798 case '\\':
799 OS << "'\\\\'";
800 break;
801 case '\'':
802 OS << "'\\''";
803 break;
804 case '\a':
805 // TODO: K&R: the meaning of '\\a' is different in traditional C
806 OS << "'\\a'";
807 break;
808 case '\b':
809 OS << "'\\b'";
810 break;
811 // Nonstandard escape sequence.
812 /*case '\e':
813 OS << "'\\e'";
814 break;*/
815 case '\f':
816 OS << "'\\f'";
817 break;
818 case '\n':
819 OS << "'\\n'";
820 break;
821 case '\r':
822 OS << "'\\r'";
823 break;
824 case '\t':
825 OS << "'\\t'";
826 break;
827 case '\v':
828 OS << "'\\v'";
829 break;
830 default:
Jordan Rose00d1b592013-02-08 22:30:27 +0000831 if (value < 256 && isPrintable((unsigned char)value))
Chris Lattner666115c2007-07-13 23:58:20 +0000832 OS << "'" << (char)value << "'";
Jordan Rose00d1b592013-02-08 22:30:27 +0000833 else if (value < 256)
834 OS << "'\\x" << llvm::format("%02x", value) << "'";
835 else if (value <= 0xFFFF)
836 OS << "'\\u" << llvm::format("%04x", value) << "'";
837 else
838 OS << "'\\U" << llvm::format("%08x", value) << "'";
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000839 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000840}
841
Steve Naroffdf7855b2007-02-21 23:46:25 +0000842void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000843 bool isSigned = Node->getType()->isSignedIntegerType();
844 OS << Node->getValue().toString(10, isSigned);
Mike Stump11289f42009-09-09 15:08:12 +0000845
Chris Lattner06430412007-05-21 05:45:03 +0000846 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall9dd450b2009-09-21 23:43:11 +0000847 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
David Blaikie83d382b2011-09-23 05:06:16 +0000848 default: llvm_unreachable("Unexpected type for integer literal!");
Richard Trieu8b626ba2011-11-07 18:40:31 +0000849 // FIXME: The Short and UShort cases are to handle cases where a short
850 // integeral literal is formed during template instantiation. They should
851 // be removed when template instantiation no longer needs integer literals.
852 case BuiltinType::Short:
853 case BuiltinType::UShort:
Chris Lattner06430412007-05-21 05:45:03 +0000854 case BuiltinType::Int: break; // no suffix.
855 case BuiltinType::UInt: OS << 'U'; break;
856 case BuiltinType::Long: OS << 'L'; break;
857 case BuiltinType::ULong: OS << "UL"; break;
858 case BuiltinType::LongLong: OS << "LL"; break;
859 case BuiltinType::ULongLong: OS << "ULL"; break;
Richard Trieu8b626ba2011-11-07 18:40:31 +0000860 case BuiltinType::Int128: OS << "i128"; break;
861 case BuiltinType::UInt128: OS << "Ui128"; break;
Chris Lattner06430412007-05-21 05:45:03 +0000862 }
Chris Lattner882f7882006-11-04 18:52:07 +0000863}
Benjamin Kramer8a526762012-09-20 14:07:17 +0000864
865static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
866 bool PrintSuffix) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000867 SmallString<16> Str;
Eli Friedman53392062011-10-05 20:32:03 +0000868 Node->getValue().toString(Str);
869 OS << Str;
Benjamin Kramer8a526762012-09-20 14:07:17 +0000870 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
871 OS << '.'; // Trailing dot in order to separate from ints.
872
873 if (!PrintSuffix)
874 return;
875
876 // Emit suffixes. Float literals are always a builtin float type.
877 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
878 default: llvm_unreachable("Unexpected type for float literal!");
879 case BuiltinType::Half: break; // FIXME: suffix?
880 case BuiltinType::Double: break; // no suffix.
881 case BuiltinType::Float: OS << 'F'; break;
882 case BuiltinType::LongDouble: OS << 'L'; break;
883 }
884}
885
886void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
887 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
Chris Lattner882f7882006-11-04 18:52:07 +0000888}
Chris Lattner1c20a172007-08-26 03:42:43 +0000889
890void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
891 PrintExpr(Node->getSubExpr());
892 OS << "i";
893}
894
Steve Naroffdf7855b2007-02-21 23:46:25 +0000895void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Richard Trieudc355912012-06-13 20:25:24 +0000896 Str->outputString(OS);
Chris Lattner882f7882006-11-04 18:52:07 +0000897}
898void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
899 OS << "(";
900 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000901 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000902}
903void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +0000904 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +0000905 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump11289f42009-09-09 15:08:12 +0000906
Eli Friedman1cf25362009-06-14 22:39:26 +0000907 // Print a space if this is an "identifier operator" like __real, or if
908 // it might be concatenated incorrectly like '+'.
Chris Lattnere5b60442007-08-23 21:46:40 +0000909 switch (Node->getOpcode()) {
910 default: break;
John McCalle3027922010-08-25 11:45:40 +0000911 case UO_Real:
912 case UO_Imag:
913 case UO_Extension:
Chris Lattnere5b60442007-08-23 21:46:40 +0000914 OS << ' ';
915 break;
John McCalle3027922010-08-25 11:45:40 +0000916 case UO_Plus:
917 case UO_Minus:
Eli Friedman1cf25362009-06-14 22:39:26 +0000918 if (isa<UnaryOperator>(Node->getSubExpr()))
919 OS << ' ';
920 break;
Chris Lattnere5b60442007-08-23 21:46:40 +0000921 }
922 }
Chris Lattner882f7882006-11-04 18:52:07 +0000923 PrintExpr(Node->getSubExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000924
Chris Lattner15768702006-11-05 23:54:51 +0000925 if (Node->isPostfix())
926 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000927}
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000928
Douglas Gregor882211c2010-04-28 22:16:22 +0000929void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
930 OS << "__builtin_offsetof(";
Benjamin Kramerc5720e92013-02-22 14:19:01 +0000931 Node->getTypeSourceInfo()->getType().print(OS, Policy);
932 OS << ", ";
Douglas Gregor882211c2010-04-28 22:16:22 +0000933 bool PrintedSomething = false;
934 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
935 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
936 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
937 // Array node
938 OS << "[";
939 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
940 OS << "]";
941 PrintedSomething = true;
942 continue;
943 }
Douglas Gregord1702062010-04-29 00:18:15 +0000944
945 // Skip implicit base indirections.
946 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
947 continue;
948
Douglas Gregor882211c2010-04-28 22:16:22 +0000949 // Field or identifier node.
950 IdentifierInfo *Id = ON.getFieldName();
951 if (!Id)
952 continue;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000953
Douglas Gregor882211c2010-04-28 22:16:22 +0000954 if (PrintedSomething)
955 OS << ".";
956 else
957 PrintedSomething = true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000958 OS << Id->getName();
Douglas Gregor882211c2010-04-28 22:16:22 +0000959 }
960 OS << ")";
961}
962
Peter Collingbournee190dee2011-03-11 19:24:49 +0000963void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
964 switch(Node->getKind()) {
965 case UETT_SizeOf:
966 OS << "sizeof";
967 break;
968 case UETT_AlignOf:
Jordan Rose58d54722012-06-30 21:33:57 +0000969 if (Policy.LangOpts.CPlusPlus)
970 OS << "alignof";
971 else if (Policy.LangOpts.C11)
972 OS << "_Alignof";
973 else
974 OS << "__alignof";
Peter Collingbournee190dee2011-03-11 19:24:49 +0000975 break;
976 case UETT_VecStep:
977 OS << "vec_step";
978 break;
979 }
Benjamin Kramerc5720e92013-02-22 14:19:01 +0000980 if (Node->isArgumentType()) {
981 OS << '(';
982 Node->getArgumentType().print(OS, Policy);
983 OS << ')';
984 } else {
Sebastian Redl6f282892008-11-11 17:56:53 +0000985 OS << " ";
986 PrintExpr(Node->getArgumentExpr());
987 }
Chris Lattner882f7882006-11-04 18:52:07 +0000988}
Peter Collingbourne91147592011-04-15 00:35:48 +0000989
990void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
991 OS << "_Generic(";
992 PrintExpr(Node->getControllingExpr());
993 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
994 OS << ", ";
995 QualType T = Node->getAssocType(i);
996 if (T.isNull())
997 OS << "default";
998 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +0000999 T.print(OS, Policy);
Peter Collingbourne91147592011-04-15 00:35:48 +00001000 OS << ": ";
1001 PrintExpr(Node->getAssocExpr(i));
1002 }
1003 OS << ")";
1004}
1005
Chris Lattner882f7882006-11-04 18:52:07 +00001006void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +00001007 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +00001008 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +00001009 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +00001010 OS << "]";
1011}
1012
Peter Collingbourne4b279a02011-02-08 21:17:54 +00001013void StmtPrinter::PrintCallArgs(CallExpr *Call) {
Chris Lattner882f7882006-11-04 18:52:07 +00001014 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001015 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1016 // Don't print any defaulted arguments
1017 break;
1018 }
1019
Chris Lattner882f7882006-11-04 18:52:07 +00001020 if (i) OS << ", ";
1021 PrintExpr(Call->getArg(i));
1022 }
Peter Collingbourne4b279a02011-02-08 21:17:54 +00001023}
1024
1025void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1026 PrintExpr(Call->getCallee());
1027 OS << "(";
1028 PrintCallArgs(Call);
Chris Lattner882f7882006-11-04 18:52:07 +00001029 OS << ")";
1030}
1031void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregore4b414c2009-01-08 22:45:41 +00001032 // FIXME: Suppress printing implicit bases (like "this")
1033 PrintExpr(Node->getBase());
David Blaikie8fab8e52012-11-12 19:12:12 +00001034
1035 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
David Blaikie6bffd6f2012-11-12 19:32:32 +00001036 FieldDecl *ParentDecl = ParentMember
1037 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : NULL;
David Blaikie8fab8e52012-11-12 19:12:12 +00001038
David Blaikie6bffd6f2012-11-12 19:32:32 +00001039 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
David Blaikie8fab8e52012-11-12 19:12:12 +00001040 OS << (Node->isArrow() ? "->" : ".");
David Blaikie8fab8e52012-11-12 19:12:12 +00001041
Fariborz Jahanian2990c022010-01-11 21:17:32 +00001042 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1043 if (FD->isAnonymousStructOrUnion())
1044 return;
David Blaikie8fab8e52012-11-12 19:12:12 +00001045
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001046 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1047 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001048 if (Node->hasTemplateKeyword())
1049 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001050 OS << Node->getMemberNameInfo();
John McCallb3774b52010-08-19 23:49:38 +00001051 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +00001052 TemplateSpecializationType::PrintTemplateArgumentList(
1053 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001054}
Steve Naroffe87026a2009-07-24 17:54:45 +00001055void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1056 PrintExpr(Node->getBase());
1057 OS << (Node->isArrow() ? "->isa" : ".isa");
1058}
1059
Nate Begemance4d7fc2008-04-18 23:10:10 +00001060void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +00001061 PrintExpr(Node->getBase());
1062 OS << ".";
1063 OS << Node->getAccessor().getName();
1064}
Douglas Gregorf19b2312008-10-28 15:36:24 +00001065void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001066 OS << '(';
1067 Node->getTypeAsWritten().print(OS, Policy);
1068 OS << ')';
Chris Lattner882f7882006-11-04 18:52:07 +00001069 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001070}
Steve Naroff57eb2c52007-07-19 21:32:11 +00001071void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001072 OS << '(';
1073 Node->getType().print(OS, Policy);
1074 OS << ')';
Steve Naroff57eb2c52007-07-19 21:32:11 +00001075 PrintExpr(Node->getInitializer());
1076}
Steve Naroff7a5af782007-07-13 16:58:59 +00001077void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Alp Toker028ed912013-12-06 17:56:43 +00001078 // No need to print anything, simply forward to the subexpression.
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001079 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +00001080}
Chris Lattner882f7882006-11-04 18:52:07 +00001081void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1082 PrintExpr(Node->getLHS());
1083 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1084 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001085}
Chris Lattner86928112007-08-25 02:00:02 +00001086void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1087 PrintExpr(Node->getLHS());
1088 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1089 PrintExpr(Node->getRHS());
1090}
Chris Lattner882f7882006-11-04 18:52:07 +00001091void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1092 PrintExpr(Node->getCond());
John McCallc07a0c72011-02-17 10:25:35 +00001093 OS << " ? ";
1094 PrintExpr(Node->getLHS());
1095 OS << " : ";
Chris Lattner882f7882006-11-04 18:52:07 +00001096 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001097}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001098
Chris Lattnereefa10e2007-05-28 06:56:27 +00001099// GNU extensions.
1100
John McCallc07a0c72011-02-17 10:25:35 +00001101void
1102StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1103 PrintExpr(Node->getCommon());
1104 OS << " ?: ";
1105 PrintExpr(Node->getFalseExpr());
1106}
Chris Lattnerd268a7a2007-08-03 17:31:20 +00001107void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +00001108 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +00001109}
1110
Chris Lattner366727f2007-07-24 16:58:17 +00001111void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1112 OS << "(";
1113 PrintRawCompoundStmt(E->getSubStmt());
1114 OS << ")";
1115}
1116
Steve Naroff9efdabc2007-08-03 21:21:27 +00001117void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1118 OS << "__builtin_choose_expr(";
1119 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +00001120 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +00001121 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +00001122 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +00001123 PrintExpr(Node->getRHS());
1124 OS << ")";
1125}
Chris Lattner366727f2007-07-24 16:58:17 +00001126
Douglas Gregor3be4b122008-11-29 04:51:27 +00001127void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1128 OS << "__null";
1129}
1130
Eli Friedmana1b4ed82008-05-14 19:38:39 +00001131void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1132 OS << "__builtin_shufflevector(";
1133 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1134 if (i) OS << ", ";
1135 PrintExpr(Node->getExpr(i));
1136 }
1137 OS << ")";
1138}
1139
Hal Finkelc4d7c822013-09-18 03:29:45 +00001140void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1141 OS << "__builtin_convertvector(";
1142 PrintExpr(Node->getSrcExpr());
1143 OS << ", ";
1144 Node->getType().print(OS, Policy);
1145 OS << ")";
1146}
1147
Anders Carlsson4692db02007-08-31 04:56:16 +00001148void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001149 if (Node->getSyntacticForm()) {
1150 Visit(Node->getSyntacticForm());
1151 return;
1152 }
1153
Anders Carlsson4692db02007-08-31 04:56:16 +00001154 OS << "{ ";
1155 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1156 if (i) OS << ", ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001157 if (Node->getInit(i))
1158 PrintExpr(Node->getInit(i));
1159 else
1160 OS << "0";
Anders Carlsson4692db02007-08-31 04:56:16 +00001161 }
1162 OS << " }";
1163}
1164
Nate Begeman5ec4b312009-08-10 23:49:36 +00001165void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1166 OS << "( ";
1167 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1168 if (i) OS << ", ";
1169 PrintExpr(Node->getExpr(i));
1170 }
1171 OS << " )";
1172}
1173
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001174void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001175 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
1176 DEnd = Node->designators_end();
1177 D != DEnd; ++D) {
1178 if (D->isFieldDesignator()) {
1179 if (D->getDotLoc().isInvalid())
1180 OS << D->getFieldName()->getName() << ":";
1181 else
1182 OS << "." << D->getFieldName()->getName();
1183 } else {
1184 OS << "[";
1185 if (D->isArrayDesignator()) {
1186 PrintExpr(Node->getArrayIndex(*D));
1187 } else {
1188 PrintExpr(Node->getArrayRangeStart(*D));
1189 OS << " ... ";
Mike Stump11289f42009-09-09 15:08:12 +00001190 PrintExpr(Node->getArrayRangeEnd(*D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001191 }
1192 OS << "]";
1193 }
1194 }
1195
1196 OS << " = ";
1197 PrintExpr(Node->getInit());
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001198}
1199
Douglas Gregor0202cb42009-01-29 17:44:32 +00001200void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001201 if (Policy.LangOpts.CPlusPlus) {
1202 OS << "/*implicit*/";
1203 Node->getType().print(OS, Policy);
1204 OS << "()";
1205 } else {
1206 OS << "/*implicit*/(";
1207 Node->getType().print(OS, Policy);
1208 OS << ')';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001209 if (Node->getType()->isRecordType())
1210 OS << "{}";
1211 else
1212 OS << 0;
1213 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001214}
1215
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001216void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman79635842009-05-30 04:20:30 +00001217 OS << "__builtin_va_arg(";
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001218 PrintExpr(Node->getSubExpr());
1219 OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001220 Node->getType().print(OS, Policy);
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001221 OS << ")";
1222}
1223
John McCallfe96e0b2011-11-06 09:01:30 +00001224void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1225 PrintExpr(Node->getSyntacticForm());
1226}
1227
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001228void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
Eli Friedmanc2025562011-10-11 20:00:47 +00001229 const char *Name = 0;
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001230 switch (Node->getOp()) {
Richard Smithfeea8832012-04-12 05:08:17 +00001231#define BUILTIN(ID, TYPE, ATTRS)
1232#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1233 case AtomicExpr::AO ## ID: \
1234 Name = #ID "("; \
1235 break;
1236#include "clang/Basic/Builtins.def"
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001237 }
1238 OS << Name;
Richard Smithfeea8832012-04-12 05:08:17 +00001239
1240 // AtomicExpr stores its subexpressions in a permuted order.
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001241 PrintExpr(Node->getPtr());
Richard Smithfeea8832012-04-12 05:08:17 +00001242 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1243 Node->getOp() != AtomicExpr::AO__atomic_load_n) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001244 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001245 PrintExpr(Node->getVal1());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001246 }
Richard Smithfeea8832012-04-12 05:08:17 +00001247 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1248 Node->isCmpXChg()) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001249 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001250 PrintExpr(Node->getVal2());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001251 }
Richard Smithfeea8832012-04-12 05:08:17 +00001252 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1253 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
Richard Smithfeea8832012-04-12 05:08:17 +00001254 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001255 PrintExpr(Node->getWeak());
Richard Smithfeea8832012-04-12 05:08:17 +00001256 }
Richard Smithdf6bee82013-05-01 19:02:43 +00001257 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) {
1258 OS << ", ";
David Chisnallfa35df62012-01-16 17:27:18 +00001259 PrintExpr(Node->getOrder());
Richard Smithdf6bee82013-05-01 19:02:43 +00001260 }
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001261 if (Node->isCmpXChg()) {
1262 OS << ", ";
1263 PrintExpr(Node->getOrderFail());
1264 }
1265 OS << ")";
1266}
1267
Chris Lattnereefa10e2007-05-28 06:56:27 +00001268// C++
Douglas Gregor993603d2008-11-14 16:09:21 +00001269void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1270 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1271 "",
1272#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1273 Spelling,
1274#include "clang/Basic/OperatorKinds.def"
1275 };
1276
1277 OverloadedOperatorKind Kind = Node->getOperator();
1278 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1279 if (Node->getNumArgs() == 1) {
1280 OS << OpStrings[Kind] << ' ';
1281 PrintExpr(Node->getArg(0));
1282 } else {
1283 PrintExpr(Node->getArg(0));
1284 OS << ' ' << OpStrings[Kind];
1285 }
Eli Friedman8e236422012-10-12 22:45:14 +00001286 } else if (Kind == OO_Arrow) {
1287 PrintExpr(Node->getArg(0));
Douglas Gregor993603d2008-11-14 16:09:21 +00001288 } else if (Kind == OO_Call) {
1289 PrintExpr(Node->getArg(0));
1290 OS << '(';
1291 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1292 if (ArgIdx > 1)
1293 OS << ", ";
1294 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1295 PrintExpr(Node->getArg(ArgIdx));
1296 }
1297 OS << ')';
1298 } else if (Kind == OO_Subscript) {
1299 PrintExpr(Node->getArg(0));
1300 OS << '[';
1301 PrintExpr(Node->getArg(1));
1302 OS << ']';
1303 } else if (Node->getNumArgs() == 1) {
1304 OS << OpStrings[Kind] << ' ';
1305 PrintExpr(Node->getArg(0));
1306 } else if (Node->getNumArgs() == 2) {
1307 PrintExpr(Node->getArg(0));
1308 OS << ' ' << OpStrings[Kind] << ' ';
1309 PrintExpr(Node->getArg(1));
1310 } else {
David Blaikie83d382b2011-09-23 05:06:16 +00001311 llvm_unreachable("unknown overloaded operator");
Douglas Gregor993603d2008-11-14 16:09:21 +00001312 }
1313}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001314
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001315void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
Benjamin Kramer00e8a192014-02-25 18:03:55 +00001316 // If we have a conversion operator call only print the argument.
1317 CXXMethodDecl *MD = Node->getMethodDecl();
1318 if (MD && isa<CXXConversionDecl>(MD)) {
1319 PrintExpr(Node->getImplicitObjectArgument());
1320 return;
1321 }
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001322 VisitCallExpr(cast<CallExpr>(Node));
1323}
1324
Peter Collingbourne41f85462011-02-09 21:07:24 +00001325void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1326 PrintExpr(Node->getCallee());
1327 OS << "<<<";
1328 PrintCallArgs(Node->getConfig());
1329 OS << ">>>(";
1330 PrintCallArgs(Node);
1331 OS << ")";
1332}
1333
Douglas Gregore200adc2008-10-27 19:41:14 +00001334void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1335 OS << Node->getCastName() << '<';
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001336 Node->getTypeAsWritten().print(OS, Policy);
1337 OS << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +00001338 PrintExpr(Node->getSubExpr());
1339 OS << ")";
1340}
1341
Douglas Gregore200adc2008-10-27 19:41:14 +00001342void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1343 VisitCXXNamedCastExpr(Node);
1344}
1345
1346void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1347 VisitCXXNamedCastExpr(Node);
1348}
1349
1350void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1351 VisitCXXNamedCastExpr(Node);
1352}
1353
1354void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1355 VisitCXXNamedCastExpr(Node);
1356}
1357
Sebastian Redlc4704762008-11-11 11:37:55 +00001358void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1359 OS << "typeid(";
1360 if (Node->isTypeOperand()) {
David Majnemer143c55e2013-09-27 07:04:31 +00001361 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
Sebastian Redlc4704762008-11-11 11:37:55 +00001362 } else {
1363 PrintExpr(Node->getExprOperand());
1364 }
1365 OS << ")";
1366}
1367
Francois Pichet9f4f2072010-09-08 12:20:18 +00001368void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1369 OS << "__uuidof(";
1370 if (Node->isTypeOperand()) {
David Majnemer143c55e2013-09-27 07:04:31 +00001371 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
Francois Pichet9f4f2072010-09-08 12:20:18 +00001372 } else {
1373 PrintExpr(Node->getExprOperand());
1374 }
1375 OS << ")";
1376}
1377
John McCall5e77d762013-04-16 07:28:30 +00001378void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1379 PrintExpr(Node->getBaseExpr());
1380 if (Node->isArrow())
1381 OS << "->";
1382 else
1383 OS << ".";
1384 if (NestedNameSpecifier *Qualifier =
1385 Node->getQualifierLoc().getNestedNameSpecifier())
1386 Qualifier->print(OS, Policy);
1387 OS << Node->getPropertyDecl()->getDeclName();
1388}
1389
Richard Smithc67fdd42012-03-07 08:35:16 +00001390void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1391 switch (Node->getLiteralOperatorKind()) {
1392 case UserDefinedLiteral::LOK_Raw:
Richard Smith29e95952012-03-09 10:10:02 +00001393 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
Richard Smithc67fdd42012-03-07 08:35:16 +00001394 break;
1395 case UserDefinedLiteral::LOK_Template: {
Richard Smith29e95952012-03-09 10:10:02 +00001396 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1397 const TemplateArgumentList *Args =
1398 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1399 assert(Args);
1400 const TemplateArgument &Pack = Args->get(0);
1401 for (TemplateArgument::pack_iterator I = Pack.pack_begin(),
1402 E = Pack.pack_end(); I != E; ++I) {
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001403 char C = (char)I->getAsIntegral().getZExtValue();
Richard Smithc67fdd42012-03-07 08:35:16 +00001404 OS << C;
1405 }
1406 break;
1407 }
Richard Smith75025ba2012-03-08 09:02:38 +00001408 case UserDefinedLiteral::LOK_Integer: {
1409 // Print integer literal without suffix.
1410 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1411 OS << Int->getValue().toString(10, /*isSigned*/false);
1412 break;
1413 }
Benjamin Kramer8a526762012-09-20 14:07:17 +00001414 case UserDefinedLiteral::LOK_Floating: {
1415 // Print floating literal without suffix.
1416 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1417 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1418 break;
1419 }
Richard Smithc67fdd42012-03-07 08:35:16 +00001420 case UserDefinedLiteral::LOK_String:
1421 case UserDefinedLiteral::LOK_Character:
1422 PrintExpr(Node->getCookedLiteral());
1423 break;
1424 }
1425 OS << Node->getUDSuffix()->getName();
1426}
1427
Chris Lattnereefa10e2007-05-28 06:56:27 +00001428void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1429 OS << (Node->getValue() ? "true" : "false");
1430}
1431
Sebastian Redl576fd422009-05-10 18:38:11 +00001432void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1433 OS << "nullptr";
1434}
1435
Douglas Gregor97a9c812008-11-04 14:32:21 +00001436void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1437 OS << "this";
1438}
1439
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001440void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1441 if (Node->getSubExpr() == 0)
1442 OS << "throw";
1443 else {
1444 OS << "throw ";
1445 PrintExpr(Node->getSubExpr());
1446 }
1447}
1448
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001449void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
Richard Smith852c9db2013-04-20 22:23:05 +00001450 // Nothing to print: we picked up the default argument.
1451}
1452
1453void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1454 // Nothing to print: we picked up the default initializer.
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001455}
1456
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001457void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001458 Node->getType().print(OS, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001459 OS << "(";
1460 PrintExpr(Node->getSubExpr());
1461 OS << ")";
1462}
1463
Anders Carlsson993a4b32009-05-30 20:03:25 +00001464void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1465 PrintExpr(Node->getSubExpr());
1466}
1467
Douglas Gregordd04d332009-01-16 18:33:17 +00001468void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001469 Node->getType().print(OS, Policy);
Douglas Gregordd04d332009-01-16 18:33:17 +00001470 OS << "(";
1471 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001472 ArgEnd = Node->arg_end();
Douglas Gregordd04d332009-01-16 18:33:17 +00001473 Arg != ArgEnd; ++Arg) {
Rafael Espindola6f6f3c42013-05-24 16:11:44 +00001474 if (Arg->isDefaultArgument())
1475 break;
Douglas Gregordd04d332009-01-16 18:33:17 +00001476 if (Arg != Node->arg_begin())
1477 OS << ", ";
1478 PrintExpr(*Arg);
1479 }
1480 OS << ")";
1481}
1482
Douglas Gregore31e6062012-02-07 10:09:13 +00001483void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1484 OS << '[';
1485 bool NeedComma = false;
1486 switch (Node->getCaptureDefault()) {
1487 case LCD_None:
1488 break;
1489
1490 case LCD_ByCopy:
1491 OS << '=';
1492 NeedComma = true;
1493 break;
1494
1495 case LCD_ByRef:
1496 OS << '&';
1497 NeedComma = true;
1498 break;
1499 }
1500 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1501 CEnd = Node->explicit_capture_end();
1502 C != CEnd;
1503 ++C) {
1504 if (NeedComma)
1505 OS << ", ";
1506 NeedComma = true;
1507
1508 switch (C->getCaptureKind()) {
1509 case LCK_This:
1510 OS << "this";
1511 break;
1512
1513 case LCK_ByRef:
Richard Smithbb13c9a2013-09-28 04:02:39 +00001514 if (Node->getCaptureDefault() != LCD_ByRef || C->isInitCapture())
Douglas Gregore31e6062012-02-07 10:09:13 +00001515 OS << '&';
1516 OS << C->getCapturedVar()->getName();
1517 break;
1518
1519 case LCK_ByCopy:
Douglas Gregore31e6062012-02-07 10:09:13 +00001520 OS << C->getCapturedVar()->getName();
1521 break;
1522 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00001523
1524 if (C->isInitCapture())
1525 PrintExpr(C->getCapturedVar()->getInit());
Douglas Gregore31e6062012-02-07 10:09:13 +00001526 }
1527 OS << ']';
1528
1529 if (Node->hasExplicitParameters()) {
1530 OS << " (";
1531 CXXMethodDecl *Method = Node->getCallOperator();
1532 NeedComma = false;
Aaron Ballman43b68be2014-03-07 17:50:17 +00001533 for (auto P : Method->params()) {
Douglas Gregore31e6062012-02-07 10:09:13 +00001534 if (NeedComma) {
1535 OS << ", ";
1536 } else {
1537 NeedComma = true;
1538 }
Aaron Ballman43b68be2014-03-07 17:50:17 +00001539 std::string ParamStr = P->getNameAsString();
1540 P->getOriginalType().print(OS, Policy, ParamStr);
Douglas Gregore31e6062012-02-07 10:09:13 +00001541 }
1542 if (Method->isVariadic()) {
1543 if (NeedComma)
1544 OS << ", ";
1545 OS << "...";
1546 }
1547 OS << ')';
1548
1549 if (Node->isMutable())
1550 OS << " mutable";
1551
1552 const FunctionProtoType *Proto
1553 = Method->getType()->getAs<FunctionProtoType>();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001554 Proto->printExceptionSpecification(OS, Policy);
Douglas Gregore31e6062012-02-07 10:09:13 +00001555
Bill Wendling44426052012-12-20 19:22:21 +00001556 // FIXME: Attributes
Douglas Gregore31e6062012-02-07 10:09:13 +00001557
Douglas Gregor0c46b2b2012-02-13 22:00:16 +00001558 // Print the trailing return type if it was specified in the source.
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001559 if (Node->hasExplicitResultType()) {
1560 OS << " -> ";
Alp Toker314cc812014-01-25 16:55:45 +00001561 Proto->getReturnType().print(OS, Policy);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001562 }
Douglas Gregore31e6062012-02-07 10:09:13 +00001563 }
1564
1565 // Print the body.
1566 CompoundStmt *Body = Node->getBody();
1567 OS << ' ';
1568 PrintStmt(Body);
1569}
1570
Douglas Gregor747eb782010-07-08 06:14:04 +00001571void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregor2b88c112010-09-08 00:15:04 +00001572 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001573 TSInfo->getType().print(OS, Policy);
Douglas Gregor2b88c112010-09-08 00:15:04 +00001574 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001575 Node->getType().print(OS, Policy);
1576 OS << "()";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001577}
1578
Sebastian Redlbd150f42008-11-21 19:14:01 +00001579void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1580 if (E->isGlobalNew())
1581 OS << "::";
1582 OS << "new ";
1583 unsigned NumPlace = E->getNumPlacementArgs();
Eli Friedmana6fdfaa2012-10-18 20:54:37 +00001584 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00001585 OS << "(";
1586 PrintExpr(E->getPlacementArg(0));
1587 for (unsigned i = 1; i < NumPlace; ++i) {
Eli Friedmana6fdfaa2012-10-18 20:54:37 +00001588 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
1589 break;
Sebastian Redlbd150f42008-11-21 19:14:01 +00001590 OS << ", ";
1591 PrintExpr(E->getPlacementArg(i));
1592 }
1593 OS << ") ";
1594 }
1595 if (E->isParenTypeId())
1596 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001597 std::string TypeS;
1598 if (Expr *Size = E->getArraySize()) {
1599 llvm::raw_string_ostream s(TypeS);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001600 s << '[';
Richard Smith235341b2012-08-16 03:56:14 +00001601 Size->printPretty(s, Helper, Policy);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001602 s << ']';
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001603 }
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001604 E->getAllocatedType().print(OS, Policy, TypeS);
Sebastian Redlbd150f42008-11-21 19:14:01 +00001605 if (E->isParenTypeId())
1606 OS << ")";
1607
Sebastian Redl6047f072012-02-16 12:22:20 +00001608 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
1609 if (InitStyle) {
1610 if (InitStyle == CXXNewExpr::CallInit)
1611 OS << "(";
1612 PrintExpr(E->getInitializer());
1613 if (InitStyle == CXXNewExpr::CallInit)
1614 OS << ")";
Sebastian Redlbd150f42008-11-21 19:14:01 +00001615 }
1616}
1617
1618void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1619 if (E->isGlobalDelete())
1620 OS << "::";
1621 OS << "delete ";
1622 if (E->isArrayForm())
1623 OS << "[] ";
1624 PrintExpr(E->getArgument());
1625}
1626
Douglas Gregorad8a3362009-09-04 17:36:40 +00001627void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1628 PrintExpr(E->getBase());
1629 if (E->isArrow())
1630 OS << "->";
1631 else
1632 OS << '.';
1633 if (E->getQualifier())
1634 E->getQualifier()->print(OS, Policy);
Eli Friedman9cc8ac52012-10-23 20:26:57 +00001635 OS << "~";
Mike Stump11289f42009-09-09 15:08:12 +00001636
Douglas Gregor678f90d2010-02-25 01:56:36 +00001637 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1638 OS << II->getName();
1639 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001640 E->getDestroyedType().print(OS, Policy);
Douglas Gregorad8a3362009-09-04 17:36:40 +00001641}
1642
Anders Carlsson0781ce72009-04-23 02:32:43 +00001643void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Richard Smithd59b8322012-12-19 01:39:02 +00001644 if (E->isListInitialization())
1645 OS << "{ ";
1646
Douglas Gregorbaba85d2011-01-24 17:25:03 +00001647 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1648 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1649 // Don't print any defaulted arguments
1650 break;
1651 }
1652
1653 if (i) OS << ", ";
1654 PrintExpr(E->getArg(i));
Fariborz Jahaniand0bbf662010-01-13 21:41:11 +00001655 }
Richard Smithd59b8322012-12-19 01:39:02 +00001656
1657 if (E->isListInitialization())
1658 OS << " }";
Anders Carlsson0781ce72009-04-23 02:32:43 +00001659}
1660
Richard Smithcc1b96d2013-06-12 22:31:48 +00001661void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
1662 PrintExpr(E->getSubExpr());
1663}
1664
John McCall5d413782010-12-06 08:20:24 +00001665void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Alp Toker028ed912013-12-06 17:56:43 +00001666 // Just forward to the subexpression.
Anders Carlssondefc6442009-04-24 22:47:04 +00001667 PrintExpr(E->getSubExpr());
1668}
1669
Mike Stump11289f42009-09-09 15:08:12 +00001670void
Douglas Gregorce934142009-05-20 18:46:25 +00001671StmtPrinter::VisitCXXUnresolvedConstructExpr(
1672 CXXUnresolvedConstructExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001673 Node->getTypeAsWritten().print(OS, Policy);
Douglas Gregorce934142009-05-20 18:46:25 +00001674 OS << "(";
1675 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001676 ArgEnd = Node->arg_end();
Douglas Gregorce934142009-05-20 18:46:25 +00001677 Arg != ArgEnd; ++Arg) {
1678 if (Arg != Node->arg_begin())
1679 OS << ", ";
1680 PrintExpr(*Arg);
1681 }
1682 OS << ")";
1683}
1684
John McCall8cd78132009-11-19 22:55:06 +00001685void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1686 CXXDependentScopeMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001687 if (!Node->isImplicitAccess()) {
1688 PrintExpr(Node->getBase());
1689 OS << (Node->isArrow() ? "->" : ".");
1690 }
Douglas Gregorc26e0f62009-09-03 16:14:30 +00001691 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1692 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001693 if (Node->hasTemplateKeyword())
Douglas Gregor308047d2009-09-09 00:23:06 +00001694 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001695 OS << Node->getMemberNameInfo();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001696 if (Node->hasExplicitTemplateArgs())
1697 TemplateSpecializationType::PrintTemplateArgumentList(
1698 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregora8db9542009-05-22 21:13:27 +00001699}
1700
John McCall10eae182009-11-30 22:42:35 +00001701void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001702 if (!Node->isImplicitAccess()) {
1703 PrintExpr(Node->getBase());
1704 OS << (Node->isArrow() ? "->" : ".");
1705 }
John McCall10eae182009-11-30 22:42:35 +00001706 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1707 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001708 if (Node->hasTemplateKeyword())
1709 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001710 OS << Node->getMemberNameInfo();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001711 if (Node->hasExplicitTemplateArgs())
1712 TemplateSpecializationType::PrintTemplateArgumentList(
1713 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
John McCall10eae182009-11-30 22:42:35 +00001714}
1715
Douglas Gregor29c42f22012-02-24 07:38:34 +00001716static const char *getTypeTraitName(TypeTrait TT) {
1717 switch (TT) {
Alp Toker95e7ff22014-01-01 05:57:51 +00001718#define TYPE_TRAIT_1(Spelling, Name, Key) \
1719case clang::UTT_##Name: return #Spelling;
Alp Tokercbb90342013-12-13 20:49:58 +00001720#define TYPE_TRAIT_2(Spelling, Name, Key) \
1721case clang::BTT_##Name: return #Spelling;
Alp Toker40f9b1c2013-12-12 21:23:03 +00001722#define TYPE_TRAIT_N(Spelling, Name, Key) \
1723 case clang::TT_##Name: return #Spelling;
1724#include "clang/Basic/TokenKinds.def"
Douglas Gregor29c42f22012-02-24 07:38:34 +00001725 }
1726 llvm_unreachable("Type trait not covered by switch");
1727}
1728
John Wiegley6242b6a2011-04-28 00:16:57 +00001729static const char *getTypeTraitName(ArrayTypeTrait ATT) {
1730 switch (ATT) {
1731 case ATT_ArrayRank: return "__array_rank";
1732 case ATT_ArrayExtent: return "__array_extent";
1733 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001734 llvm_unreachable("Array type trait not covered by switch");
John Wiegley6242b6a2011-04-28 00:16:57 +00001735}
1736
John Wiegleyf9f65842011-04-25 06:54:41 +00001737static const char *getExpressionTraitName(ExpressionTrait ET) {
1738 switch (ET) {
John Wiegleyf9f65842011-04-25 06:54:41 +00001739 case ET_IsLValueExpr: return "__is_lvalue_expr";
1740 case ET_IsRValueExpr: return "__is_rvalue_expr";
1741 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001742 llvm_unreachable("Expression type trait not covered by switch");
John Wiegleyf9f65842011-04-25 06:54:41 +00001743}
1744
Douglas Gregor29c42f22012-02-24 07:38:34 +00001745void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
1746 OS << getTypeTraitName(E->getTrait()) << "(";
1747 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
1748 if (I > 0)
1749 OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001750 E->getArg(I)->getType().print(OS, Policy);
Douglas Gregor29c42f22012-02-24 07:38:34 +00001751 }
1752 OS << ")";
1753}
1754
John Wiegley6242b6a2011-04-28 00:16:57 +00001755void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001756 OS << getTypeTraitName(E->getTrait()) << '(';
1757 E->getQueriedType().print(OS, Policy);
1758 OS << ')';
John Wiegley6242b6a2011-04-28 00:16:57 +00001759}
1760
John Wiegleyf9f65842011-04-25 06:54:41 +00001761void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001762 OS << getExpressionTraitName(E->getTrait()) << '(';
1763 PrintExpr(E->getQueriedExpression());
1764 OS << ')';
John Wiegleyf9f65842011-04-25 06:54:41 +00001765}
1766
Sebastian Redl4202c0f2010-09-10 20:55:43 +00001767void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1768 OS << "noexcept(";
1769 PrintExpr(E->getOperand());
1770 OS << ")";
1771}
1772
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001773void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregore8e9dd62011-01-03 17:17:50 +00001774 PrintExpr(E->getPattern());
1775 OS << "...";
1776}
1777
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001778void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001779 OS << "sizeof...(" << *E->getPack() << ")";
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001780}
1781
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001782void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
1783 SubstNonTypeTemplateParmPackExpr *Node) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001784 OS << *Node->getParameterPack();
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001785}
1786
John McCall7c454bb2011-07-15 05:09:51 +00001787void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
1788 SubstNonTypeTemplateParmExpr *Node) {
1789 Visit(Node->getReplacement());
1790}
1791
Richard Smithb15fe3a2012-09-12 00:56:43 +00001792void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
1793 OS << *E->getParameterPack();
1794}
1795
Douglas Gregorfe314812011-06-21 17:03:29 +00001796void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
1797 PrintExpr(Node->GetTemporaryExpr());
1798}
1799
Mike Stump11289f42009-09-09 15:08:12 +00001800// Obj-C
Anders Carlsson76f4a902007-08-21 17:43:55 +00001801
1802void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1803 OS << "@";
1804 VisitStringLiteral(Node->getString());
1805}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001806
Patrick Beard0caa3942012-04-19 00:25:12 +00001807void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001808 OS << "@";
Patrick Beard0caa3942012-04-19 00:25:12 +00001809 Visit(E->getSubExpr());
Ted Kremeneke65b0862012-03-06 20:05:56 +00001810}
1811
1812void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
1813 OS << "@[ ";
1814 StmtRange ch = E->children();
1815 if (ch.first != ch.second) {
1816 while (1) {
1817 Visit(*ch.first);
1818 ++ch.first;
1819 if (ch.first == ch.second) break;
1820 OS << ", ";
1821 }
1822 }
1823 OS << " ]";
1824}
1825
1826void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
1827 OS << "@{ ";
1828 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
1829 if (I > 0)
1830 OS << ", ";
1831
1832 ObjCDictionaryElement Element = E->getKeyValueElement(I);
1833 Visit(Element.Key);
1834 OS << " : ";
1835 Visit(Element.Value);
1836 if (Element.isPackExpansion())
1837 OS << "...";
1838 }
1839 OS << " }";
1840}
1841
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001842void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001843 OS << "@encode(";
1844 Node->getEncodedType().print(OS, Policy);
1845 OS << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001846}
1847
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001848void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001849 OS << "@selector(";
1850 Node->getSelector().print(OS);
1851 OS << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001852}
1853
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001854void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001855 OS << "@protocol(" << *Node->getProtocol() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001856}
1857
Steve Naroffd54978b2007-09-18 23:55:05 +00001858void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1859 OS << "[";
Douglas Gregor9a129192010-04-21 00:45:42 +00001860 switch (Mess->getReceiverKind()) {
1861 case ObjCMessageExpr::Instance:
1862 PrintExpr(Mess->getInstanceReceiver());
1863 break;
1864
1865 case ObjCMessageExpr::Class:
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001866 Mess->getClassReceiver().print(OS, Policy);
Douglas Gregor9a129192010-04-21 00:45:42 +00001867 break;
1868
1869 case ObjCMessageExpr::SuperInstance:
1870 case ObjCMessageExpr::SuperClass:
1871 OS << "Super";
1872 break;
1873 }
1874
Ted Kremeneka06e7122008-05-02 17:32:38 +00001875 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00001876 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001877 if (selector.isUnarySelector()) {
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00001878 OS << selector.getNameForSlot(0);
Steve Naroffc6814ea2007-10-02 20:01:56 +00001879 } else {
1880 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001881 if (i < selector.getNumArgs()) {
1882 if (i > 0) OS << ' ';
1883 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001884 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00001885 else
1886 OS << ":";
1887 }
1888 else OS << ", "; // Handle variadic methods.
Mike Stump11289f42009-09-09 15:08:12 +00001889
Steve Naroffc6814ea2007-10-02 20:01:56 +00001890 PrintExpr(Mess->getArg(i));
1891 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001892 }
1893 OS << "]";
1894}
1895
Ted Kremeneke65b0862012-03-06 20:05:56 +00001896void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
1897 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
1898}
1899
John McCall31168b02011-06-15 23:02:42 +00001900void
1901StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
1902 PrintExpr(E->getSubExpr());
1903}
1904
1905void
1906StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001907 OS << '(' << E->getBridgeKindName();
1908 E->getType().print(OS, Policy);
1909 OS << ')';
John McCall31168b02011-06-15 23:02:42 +00001910 PrintExpr(E->getSubExpr());
1911}
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001912
Steve Naroffc540d662008-09-03 18:15:37 +00001913void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001914 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00001915 OS << "^";
Mike Stump11289f42009-09-09 15:08:12 +00001916
Steve Naroffc540d662008-09-03 18:15:37 +00001917 const FunctionType *AFT = Node->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00001918
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001919 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroffc540d662008-09-03 18:15:37 +00001920 OS << "()";
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001921 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00001922 OS << '(';
Steve Naroff415d3d52008-10-08 17:01:13 +00001923 for (BlockDecl::param_iterator AI = BD->param_begin(),
1924 E = BD->param_end(); AI != E; ++AI) {
1925 if (AI != BD->param_begin()) OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001926 std::string ParamStr = (*AI)->getNameAsString();
1927 (*AI)->getType().print(OS, Policy, ParamStr);
Steve Naroffc540d662008-09-03 18:15:37 +00001928 }
Mike Stump11289f42009-09-09 15:08:12 +00001929
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001930 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00001931 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001932 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00001933 OS << "...";
1934 }
1935 OS << ')';
1936 }
Fariborz Jahanian1ace8cb2012-12-04 21:15:23 +00001937 OS << "{ }";
Steve Naroffc540d662008-09-03 18:15:37 +00001938}
1939
Ted Kremenekf551f322011-11-30 22:08:08 +00001940void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
1941 PrintExpr(Node->getSourceExpr());
1942}
John McCall8d69a212010-11-15 23:31:06 +00001943
Tanya Lattner55808c12011-06-04 00:47:47 +00001944void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
1945 OS << "__builtin_astype(";
1946 PrintExpr(Node->getSrcExpr());
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001947 OS << ", ";
1948 Node->getType().print(OS, Policy);
Tanya Lattner55808c12011-06-04 00:47:47 +00001949 OS << ")";
1950}
1951
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001952//===----------------------------------------------------------------------===//
1953// Stmt method implementations
1954//===----------------------------------------------------------------------===//
1955
Craig Topperc571c812013-08-22 06:02:26 +00001956void Stmt::dumpPretty(const ASTContext &Context) const {
Richard Smith235341b2012-08-16 03:56:14 +00001957 printPretty(llvm::errs(), 0, PrintingPolicy(Context.getLangOpts()));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001958}
1959
Richard Smith235341b2012-08-16 03:56:14 +00001960void Stmt::printPretty(raw_ostream &OS,
1961 PrinterHelper *Helper,
Douglas Gregor7de59662009-05-29 20:38:28 +00001962 const PrintingPolicy &Policy,
1963 unsigned Indentation) const {
Chris Lattner882f7882006-11-04 18:52:07 +00001964 if (this == 0) {
1965 OS << "<NULL>";
1966 return;
1967 }
1968
Richard Smith235341b2012-08-16 03:56:14 +00001969 StmtPrinter P(OS, Helper, Policy, Indentation);
Chris Lattner62249a62007-08-21 04:04:25 +00001970 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001971}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001972
1973//===----------------------------------------------------------------------===//
1974// PrinterHelper
1975//===----------------------------------------------------------------------===//
1976
1977// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +00001978PrinterHelper::~PrinterHelper() {}