blob: a0a73be0a5a8341aa1039c8b53a89c04420f14bd [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";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000117 for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
Chris Lattner882f7882006-11-04 18:52:07 +0000118 I != E; ++I)
119 PrintStmt(*I);
Mike Stump11289f42009-09-09 15:08:12 +0000120
Chris Lattner073926e2007-05-20 23:04:55 +0000121 Indent() << "}";
122}
123
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000124void StmtPrinter::PrintRawDecl(Decl *D) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000125 D->print(OS, Policy, IndentLevel);
Mike Stump74a76472009-02-10 20:16:46 +0000126}
127
Eli Friedmanf86e5072012-10-16 23:45:15 +0000128void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
129 DeclStmt::const_decl_iterator Begin = S->decl_begin(), End = S->decl_end();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000130 SmallVector<Decl*, 2> Decls;
Mike Stump11289f42009-09-09 15:08:12 +0000131 for ( ; Begin != End; ++Begin)
Eli Friedman79635842009-05-30 04:20:30 +0000132 Decls.push_back(*Begin);
Eli Friedman15ea8802009-05-30 00:19:54 +0000133
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000134 Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
Ted Kremenek15e6b402008-10-06 18:39:36 +0000135}
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000136
137void StmtPrinter::VisitNullStmt(NullStmt *Node) {
138 Indent() << ";\n";
139}
140
141void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
Eli Friedman15ea8802009-05-30 00:19:54 +0000142 Indent();
143 PrintRawDeclStmt(Node);
144 OS << ";\n";
Steve Naroff2a8ad182007-05-29 22:59:26 +0000145}
146
Chris Lattner073926e2007-05-20 23:04:55 +0000147void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
148 Indent();
149 PrintRawCompoundStmt(Node);
Chris Lattnerdf3cafb2007-05-31 05:08:56 +0000150 OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000151}
152
Chris Lattner6c0ff132006-11-05 00:19:50 +0000153void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000154 Indent(-1) << "case ";
Chris Lattner6c0ff132006-11-05 00:19:50 +0000155 PrintExpr(Node->getLHS());
156 if (Node->getRHS()) {
157 OS << " ... ";
158 PrintExpr(Node->getRHS());
159 }
160 OS << ":\n";
Mike Stump11289f42009-09-09 15:08:12 +0000161
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000162 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000163}
164
165void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000166 Indent(-1) << "default:\n";
167 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000168}
169
170void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +0000171 Indent(-1) << Node->getName() << ":\n";
Chris Lattnerb9eb5a12007-05-20 22:52:15 +0000172 PrintStmt(Node->getSubStmt(), 0);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000173}
174
Richard Smithc202b282012-04-14 00:33:13 +0000175void StmtPrinter::VisitAttributedStmt(AttributedStmt *Node) {
176 OS << "[[";
177 bool first = true;
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000178 for (ArrayRef<const Attr*>::iterator it = Node->getAttrs().begin(),
179 end = Node->getAttrs().end();
180 it != end; ++it) {
Richard Smithc202b282012-04-14 00:33:13 +0000181 if (!first) {
182 OS << ", ";
183 first = false;
184 }
185 // TODO: check this
Richard Smith52f04a22012-08-16 02:43:29 +0000186 (*it)->printPretty(OS, Policy);
Richard Smithc202b282012-04-14 00:33:13 +0000187 }
188 OS << "]] ";
189 PrintStmt(Node->getSubStmt(), 0);
190}
191
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000192void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000193 OS << "if (";
Eli Friedmanf86e5072012-10-16 23:45:15 +0000194 if (const DeclStmt *DS = If->getConditionVariableDeclStmt())
195 PrintRawDeclStmt(DS);
196 else
197 PrintExpr(If->getCond());
Sebastian Redl7b7cec62009-02-07 20:05:48 +0000198 OS << ')';
Mike Stump11289f42009-09-09 15:08:12 +0000199
Chris Lattner073926e2007-05-20 23:04:55 +0000200 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
201 OS << ' ';
202 PrintRawCompoundStmt(CS);
203 OS << (If->getElse() ? ' ' : '\n');
204 } else {
205 OS << '\n';
206 PrintStmt(If->getThen());
207 if (If->getElse()) Indent();
208 }
Mike Stump11289f42009-09-09 15:08:12 +0000209
Chris Lattner073926e2007-05-20 23:04:55 +0000210 if (Stmt *Else = If->getElse()) {
211 OS << "else";
Mike Stump11289f42009-09-09 15:08:12 +0000212
Chris Lattner073926e2007-05-20 23:04:55 +0000213 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
214 OS << ' ';
215 PrintRawCompoundStmt(CS);
216 OS << '\n';
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000217 } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
218 OS << ' ';
219 PrintRawIfStmt(ElseIf);
Chris Lattner073926e2007-05-20 23:04:55 +0000220 } else {
221 OS << '\n';
222 PrintStmt(If->getElse());
223 }
Chris Lattner882f7882006-11-04 18:52:07 +0000224 }
Chris Lattner85ed8732006-11-04 20:40:44 +0000225}
226
Chris Lattnerc0a38dd2007-06-11 22:26:23 +0000227void StmtPrinter::VisitIfStmt(IfStmt *If) {
228 Indent();
229 PrintRawIfStmt(If);
230}
231
Chris Lattnerf2174b62006-11-04 20:59:27 +0000232void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
233 Indent() << "switch (";
Eli Friedmanf86e5072012-10-16 23:45:15 +0000234 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
235 PrintRawDeclStmt(DS);
236 else
237 PrintExpr(Node->getCond());
Chris Lattner073926e2007-05-20 23:04:55 +0000238 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000239
Chris Lattner073926e2007-05-20 23:04:55 +0000240 // Pretty print compoundstmt bodies (very common).
241 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
242 OS << " ";
243 PrintRawCompoundStmt(CS);
244 OS << "\n";
245 } else {
246 OS << "\n";
247 PrintStmt(Node->getBody());
248 }
Chris Lattnerf2174b62006-11-04 20:59:27 +0000249}
250
Chris Lattner85ed8732006-11-04 20:40:44 +0000251void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
252 Indent() << "while (";
Eli Friedmanf86e5072012-10-16 23:45:15 +0000253 if (const DeclStmt *DS = Node->getConditionVariableDeclStmt())
254 PrintRawDeclStmt(DS);
255 else
256 PrintExpr(Node->getCond());
Chris Lattner85ed8732006-11-04 20:40:44 +0000257 OS << ")\n";
258 PrintStmt(Node->getBody());
259}
260
261void StmtPrinter::VisitDoStmt(DoStmt *Node) {
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000262 Indent() << "do ";
263 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
264 PrintRawCompoundStmt(CS);
265 OS << " ";
266 } else {
267 OS << "\n";
268 PrintStmt(Node->getBody());
269 Indent();
270 }
Mike Stump11289f42009-09-09 15:08:12 +0000271
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000272 OS << "while (";
Chris Lattner85ed8732006-11-04 20:40:44 +0000273 PrintExpr(Node->getCond());
Eli Friedman8f1d33e2009-05-17 01:05:34 +0000274 OS << ");\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000275}
276
Chris Lattner71e23ce2006-11-04 20:18:38 +0000277void StmtPrinter::VisitForStmt(ForStmt *Node) {
278 Indent() << "for (";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000279 if (Node->getInit()) {
280 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000281 PrintRawDeclStmt(DS);
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000282 else
283 PrintExpr(cast<Expr>(Node->getInit()));
284 }
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000285 OS << ";";
286 if (Node->getCond()) {
287 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000288 PrintExpr(Node->getCond());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000289 }
290 OS << ";";
291 if (Node->getInc()) {
292 OS << " ";
Chris Lattnerfdc195a2007-06-05 20:52:47 +0000293 PrintExpr(Node->getInc());
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000294 }
295 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000296
Chris Lattner5a4e9d22007-09-15 21:49:37 +0000297 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
298 PrintRawCompoundStmt(CS);
299 OS << "\n";
300 } else {
301 OS << "\n";
302 PrintStmt(Node->getBody());
303 }
Chris Lattner71e23ce2006-11-04 20:18:38 +0000304}
305
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000306void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
Fariborz Jahanian83615522008-01-02 22:54:34 +0000307 Indent() << "for (";
308 if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
Ted Kremenek15e6b402008-10-06 18:39:36 +0000309 PrintRawDeclStmt(DS);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000310 else
311 PrintExpr(cast<Expr>(Node->getElement()));
312 OS << " in ";
313 PrintExpr(Node->getCollection());
314 OS << ") ";
Mike Stump11289f42009-09-09 15:08:12 +0000315
Fariborz Jahanian83615522008-01-02 22:54:34 +0000316 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
317 PrintRawCompoundStmt(CS);
318 OS << "\n";
319 } else {
320 OS << "\n";
321 PrintStmt(Node->getBody());
322 }
323}
324
Richard Smith02e85f32011-04-14 22:09:26 +0000325void StmtPrinter::VisitCXXForRangeStmt(CXXForRangeStmt *Node) {
326 Indent() << "for (";
327 PrintingPolicy SubPolicy(Policy);
328 SubPolicy.SuppressInitializers = true;
329 Node->getLoopVariable()->print(OS, SubPolicy, IndentLevel);
330 OS << " : ";
331 PrintExpr(Node->getRangeInit());
332 OS << ") {\n";
333 PrintStmt(Node->getBody());
Ted Kremenek88446602013-12-11 23:44:02 +0000334 Indent() << "}";
335 if (Policy.IncludeNewlines) OS << "\n";
Richard Smith02e85f32011-04-14 22:09:26 +0000336}
337
Douglas Gregordeb4a2be2011-10-25 01:33:02 +0000338void StmtPrinter::VisitMSDependentExistsStmt(MSDependentExistsStmt *Node) {
339 Indent();
340 if (Node->isIfExists())
341 OS << "__if_exists (";
342 else
343 OS << "__if_not_exists (";
344
345 if (NestedNameSpecifier *Qualifier
346 = Node->getQualifierLoc().getNestedNameSpecifier())
347 Qualifier->print(OS, Policy);
348
349 OS << Node->getNameInfo() << ") ";
350
351 PrintRawCompoundStmt(Node->getSubStmt());
352}
353
Chris Lattner16976d32006-11-05 01:46:01 +0000354void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000355 Indent() << "goto " << Node->getLabel()->getName() << ";";
356 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000357}
358
359void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
Chris Lattner36ad1232006-11-05 01:51:06 +0000360 Indent() << "goto *";
Chris Lattner16976d32006-11-05 01:46:01 +0000361 PrintExpr(Node->getTarget());
Ted Kremenek88446602013-12-11 23:44:02 +0000362 OS << ";";
363 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000364}
365
366void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000367 Indent() << "continue;";
368 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000369}
370
371void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
Ted Kremenek88446602013-12-11 23:44:02 +0000372 Indent() << "break;";
373 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner16976d32006-11-05 01:46:01 +0000374}
375
376
Chris Lattner882f7882006-11-04 18:52:07 +0000377void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
378 Indent() << "return";
379 if (Node->getRetValue()) {
380 OS << " ";
381 PrintExpr(Node->getRetValue());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000382 }
Ted Kremenek88446602013-12-11 23:44:02 +0000383 OS << ";";
384 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000385}
386
Chris Lattner73c56c02007-10-29 04:04:16 +0000387
Chad Rosierde70e0e2012-08-25 00:11:56 +0000388void StmtPrinter::VisitGCCAsmStmt(GCCAsmStmt *Node) {
Anders Carlsson660bdd12007-11-23 23:12:25 +0000389 Indent() << "asm ";
Mike Stump11289f42009-09-09 15:08:12 +0000390
Anders Carlsson660bdd12007-11-23 23:12:25 +0000391 if (Node->isVolatile())
392 OS << "volatile ";
Mike Stump11289f42009-09-09 15:08:12 +0000393
Anders Carlsson660bdd12007-11-23 23:12:25 +0000394 OS << "(";
Anders Carlsson81a5a692007-11-20 19:21:03 +0000395 VisitStringLiteral(Node->getAsmString());
Mike Stump11289f42009-09-09 15:08:12 +0000396
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000397 // Outputs
398 if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
399 Node->getNumClobbers() != 0)
400 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000401
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000402 for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
403 if (i != 0)
404 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000405
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000406 if (!Node->getOutputName(i).empty()) {
407 OS << '[';
408 OS << Node->getOutputName(i);
409 OS << "] ";
410 }
Mike Stump11289f42009-09-09 15:08:12 +0000411
Chris Lattner72bbf172009-03-10 04:59:06 +0000412 VisitStringLiteral(Node->getOutputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000413 OS << " ";
414 Visit(Node->getOutputExpr(i));
415 }
Mike Stump11289f42009-09-09 15:08:12 +0000416
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000417 // Inputs
418 if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
419 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000420
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000421 for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
422 if (i != 0)
423 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000424
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000425 if (!Node->getInputName(i).empty()) {
426 OS << '[';
427 OS << Node->getInputName(i);
428 OS << "] ";
429 }
Mike Stump11289f42009-09-09 15:08:12 +0000430
Chris Lattner72bbf172009-03-10 04:59:06 +0000431 VisitStringLiteral(Node->getInputConstraintLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000432 OS << " ";
433 Visit(Node->getInputExpr(i));
434 }
Mike Stump11289f42009-09-09 15:08:12 +0000435
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000436 // Clobbers
437 if (Node->getNumClobbers() != 0)
438 OS << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000439
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000440 for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
441 if (i != 0)
442 OS << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000443
Chad Rosierd9fb09a2012-08-27 23:28:41 +0000444 VisitStringLiteral(Node->getClobberStringLiteral(i));
Anders Carlsson94ea8aa2007-11-22 01:36:19 +0000445 }
Mike Stump11289f42009-09-09 15:08:12 +0000446
Ted Kremenek88446602013-12-11 23:44:02 +0000447 OS << ");";
448 if (Policy.IncludeNewlines) OS << "\n";
Chris Lattner73c56c02007-10-29 04:04:16 +0000449}
450
Chad Rosier32503022012-06-11 20:47:18 +0000451void StmtPrinter::VisitMSAsmStmt(MSAsmStmt *Node) {
452 // FIXME: Implement MS style inline asm statement printer.
Chad Rosierb6f46c12012-08-15 16:53:30 +0000453 Indent() << "__asm ";
454 if (Node->hasBraces())
455 OS << "{\n";
John McCallf413f5e2013-05-03 00:10:13 +0000456 OS << Node->getAsmString() << "\n";
Chad Rosierb6f46c12012-08-15 16:53:30 +0000457 if (Node->hasBraces())
458 Indent() << "}\n";
Chad Rosier32503022012-06-11 20:47:18 +0000459}
460
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000461void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000462 PrintStmt(Node->getCapturedDecl()->getBody());
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000463}
464
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000465void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Fariborz Jahanian88157952007-11-02 18:16:07 +0000466 Indent() << "@try";
467 if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
468 PrintRawCompoundStmt(TS);
469 OS << "\n";
470 }
Mike Stump11289f42009-09-09 15:08:12 +0000471
Douglas Gregor96c79492010-04-23 22:50:49 +0000472 for (unsigned I = 0, N = Node->getNumCatchStmts(); I != N; ++I) {
473 ObjCAtCatchStmt *catchStmt = Node->getCatchStmt(I);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000474 Indent() << "@catch(";
Steve Naroff371b8fb2009-03-03 19:52:17 +0000475 if (catchStmt->getCatchParamDecl()) {
476 if (Decl *DS = catchStmt->getCatchParamDecl())
477 PrintRawDecl(DS);
Fariborz Jahanian88157952007-11-02 18:16:07 +0000478 }
479 OS << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000480 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody())) {
481 PrintRawCompoundStmt(CS);
482 OS << "\n";
483 }
Fariborz Jahanian88157952007-11-02 18:16:07 +0000484 }
Mike Stump11289f42009-09-09 15:08:12 +0000485
486 if (ObjCAtFinallyStmt *FS = static_cast<ObjCAtFinallyStmt *>(
487 Node->getFinallyStmt())) {
Fariborz Jahaniandefbf9a2007-11-07 00:46:42 +0000488 Indent() << "@finally";
489 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
Fariborz Jahanian88157952007-11-02 18:16:07 +0000490 OS << "\n";
Mike Stump11289f42009-09-09 15:08:12 +0000491 }
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000492}
493
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000494void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000495}
496
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000497void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
Fariborz Jahanian65590b22007-11-01 21:12:44 +0000498 Indent() << "@catch (...) { /* todo */ } \n";
499}
500
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000501void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +0000502 Indent() << "@throw";
503 if (Node->getThrowExpr()) {
504 OS << " ";
505 PrintExpr(Node->getThrowExpr());
506 }
507 OS << ";\n";
508}
509
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000510void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000511 Indent() << "@synchronized (";
512 PrintExpr(Node->getSynchExpr());
513 OS << ")";
Fariborz Jahanian049fa582008-01-30 17:38:29 +0000514 PrintRawCompoundStmt(Node->getSynchBody());
515 OS << "\n";
Fariborz Jahanianf89ca382008-01-29 18:21:32 +0000516}
517
John McCall31168b02011-06-15 23:02:42 +0000518void StmtPrinter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *Node) {
519 Indent() << "@autoreleasepool";
520 PrintRawCompoundStmt(dyn_cast<CompoundStmt>(Node->getSubStmt()));
521 OS << "\n";
522}
523
Sebastian Redl9b244a82008-12-22 21:35:02 +0000524void StmtPrinter::PrintRawCXXCatchStmt(CXXCatchStmt *Node) {
525 OS << "catch (";
Sebastian Redl54c04d42008-12-22 19:15:10 +0000526 if (Decl *ExDecl = Node->getExceptionDecl())
527 PrintRawDecl(ExDecl);
528 else
529 OS << "...";
530 OS << ") ";
531 PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
Sebastian Redl9b244a82008-12-22 21:35:02 +0000532}
533
534void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
535 Indent();
536 PrintRawCXXCatchStmt(Node);
537 OS << "\n";
538}
539
540void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
541 Indent() << "try ";
542 PrintRawCompoundStmt(Node->getTryBlock());
Mike Stump11289f42009-09-09 15:08:12 +0000543 for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
Sebastian Redl9b244a82008-12-22 21:35:02 +0000544 OS << " ";
545 PrintRawCXXCatchStmt(Node->getHandler(i));
546 }
Sebastian Redl54c04d42008-12-22 19:15:10 +0000547 OS << "\n";
548}
549
John Wiegley1c0675e2011-04-28 01:08:34 +0000550void StmtPrinter::VisitSEHTryStmt(SEHTryStmt *Node) {
551 Indent() << (Node->getIsCXXTry() ? "try " : "__try ");
552 PrintRawCompoundStmt(Node->getTryBlock());
553 SEHExceptStmt *E = Node->getExceptHandler();
554 SEHFinallyStmt *F = Node->getFinallyHandler();
555 if(E)
556 PrintRawSEHExceptHandler(E);
557 else {
558 assert(F && "Must have a finally block...");
559 PrintRawSEHFinallyStmt(F);
560 }
561 OS << "\n";
562}
563
564void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
565 OS << "__finally ";
566 PrintRawCompoundStmt(Node->getBlock());
567 OS << "\n";
568}
569
570void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
571 OS << "__except (";
572 VisitExpr(Node->getFilterExpr());
Joao Matos566359c2012-09-04 17:49:35 +0000573 OS << ")\n";
John Wiegley1c0675e2011-04-28 01:08:34 +0000574 PrintRawCompoundStmt(Node->getBlock());
575 OS << "\n";
576}
577
578void StmtPrinter::VisitSEHExceptStmt(SEHExceptStmt *Node) {
579 Indent();
580 PrintRawSEHExceptHandler(Node);
581 OS << "\n";
582}
583
584void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
585 Indent();
586 PrintRawSEHFinallyStmt(Node);
587 OS << "\n";
588}
589
Chris Lattner71e23ce2006-11-04 20:18:38 +0000590//===----------------------------------------------------------------------===//
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000591// OpenMP clauses printing methods
592//===----------------------------------------------------------------------===//
593
594namespace {
595class OMPClausePrinter : public OMPClauseVisitor<OMPClausePrinter> {
596 raw_ostream &OS;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000597 const PrintingPolicy &Policy;
Alexey Bataev756c1962013-09-24 03:17:45 +0000598 /// \brief Process clauses with list of variables.
599 template <typename T>
600 void VisitOMPClauseList(T *Node, char StartSym);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000601public:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000602 OMPClausePrinter(raw_ostream &OS, const PrintingPolicy &Policy)
603 : OS(OS), Policy(Policy) { }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000604#define OPENMP_CLAUSE(Name, Class) \
605 void Visit##Class(Class *S);
606#include "clang/Basic/OpenMPKinds.def"
607};
608
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000609void OMPClausePrinter::VisitOMPIfClause(OMPIfClause *Node) {
610 OS << "if(";
611 Node->getCondition()->printPretty(OS, 0, Policy, 0);
612 OS << ")";
613}
614
Alexey Bataev568a8332014-03-06 06:15:19 +0000615void OMPClausePrinter::VisitOMPNumThreadsClause(OMPNumThreadsClause *Node) {
616 OS << "num_threads(";
617 Node->getNumThreads()->printPretty(OS, 0, Policy, 0);
618 OS << ")";
619}
620
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000621void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
622 OS << "default("
623 << getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind())
624 << ")";
625}
626
Alexey Bataev756c1962013-09-24 03:17:45 +0000627template<typename T>
628void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
629 for (typename T::varlist_iterator I = Node->varlist_begin(),
630 E = Node->varlist_end();
631 I != E; ++I)
632 OS << (I == Node->varlist_begin() ? StartSym : ',')
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000633 << *cast<NamedDecl>(cast<DeclRefExpr>(*I)->getDecl());
Alexey Bataev756c1962013-09-24 03:17:45 +0000634}
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000635
636void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) {
637 if (!Node->varlist_empty()) {
638 OS << "private";
Alexey Bataev756c1962013-09-24 03:17:45 +0000639 VisitOMPClauseList(Node, '(');
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000640 OS << ")";
641 }
642}
643
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000644void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) {
645 if (!Node->varlist_empty()) {
646 OS << "firstprivate";
647 VisitOMPClauseList(Node, '(');
648 OS << ")";
649 }
650}
651
Alexey Bataev758e55e2013-09-06 18:03:48 +0000652void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
653 if (!Node->varlist_empty()) {
654 OS << "shared";
Alexey Bataev756c1962013-09-24 03:17:45 +0000655 VisitOMPClauseList(Node, '(');
Alexey Bataev758e55e2013-09-06 18:03:48 +0000656 OS << ")";
657 }
658}
659
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000660}
661
662//===----------------------------------------------------------------------===//
663// OpenMP directives printing methods
664//===----------------------------------------------------------------------===//
665
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000666void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000667 OMPClausePrinter Printer(OS, Policy);
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000668 ArrayRef<OMPClause *> Clauses = S->clauses();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000669 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
670 I != E; ++I)
671 if (*I && !(*I)->isImplicit()) {
672 Printer.Visit(*I);
673 OS << ' ';
674 }
675 OS << "\n";
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000676 if (S->getAssociatedStmt()) {
677 assert(isa<CapturedStmt>(S->getAssociatedStmt()) &&
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000678 "Expected captured statement!");
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000679 Stmt *CS = cast<CapturedStmt>(S->getAssociatedStmt())->getCapturedStmt();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000680 PrintStmt(CS);
681 }
682}
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000683
684void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
685 Indent() << "#pragma omp parallel ";
686 PrintOMPExecutableDirective(Node);
687}
688
689void StmtPrinter::VisitOMPSimdDirective(OMPSimdDirective *Node) {
690 Indent() << "#pragma omp simd ";
691 PrintOMPExecutableDirective(Node);
692}
693
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000694//===----------------------------------------------------------------------===//
Chris Lattner71e23ce2006-11-04 20:18:38 +0000695// Expr printing methods.
696//===----------------------------------------------------------------------===//
Chris Lattnera3bcb7a2006-11-04 07:16:25 +0000697
Chris Lattner882f7882006-11-04 18:52:07 +0000698void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000699 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
700 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000701 if (Node->hasTemplateKeyword())
702 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000703 OS << Node->getNameInfo();
John McCallb3774b52010-08-19 23:49:38 +0000704 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +0000705 TemplateSpecializationType::PrintTemplateArgumentList(
706 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000707}
708
John McCall8cd78132009-11-19 22:55:06 +0000709void StmtPrinter::VisitDependentScopeDeclRefExpr(
710 DependentScopeDeclRefExpr *Node) {
Axel Naumann20b27862011-01-24 15:44:00 +0000711 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
712 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000713 if (Node->hasTemplateKeyword())
714 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000715 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000716 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +0000717 TemplateSpecializationType::PrintTemplateArgumentList(
718 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregor90a1a652009-03-19 17:26:29 +0000719}
720
John McCalld14a8642009-11-21 08:51:07 +0000721void StmtPrinter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *Node) {
Douglas Gregora727cb92009-06-30 22:34:41 +0000722 if (Node->getQualifier())
723 Node->getQualifier()->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000724 if (Node->hasTemplateKeyword())
725 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000726 OS << Node->getNameInfo();
John McCalle66edc12009-11-24 19:00:30 +0000727 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +0000728 TemplateSpecializationType::PrintTemplateArgumentList(
729 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregora727cb92009-06-30 22:34:41 +0000730}
731
Steve Naroffe46504b2007-11-12 14:29:37 +0000732void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000733 if (Node->getBase()) {
734 PrintExpr(Node->getBase());
735 OS << (Node->isArrow() ? "->" : ".");
736 }
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000737 OS << *Node->getDecl();
Steve Naroffe46504b2007-11-12 14:29:37 +0000738}
739
Steve Naroffec944032008-05-30 00:40:33 +0000740void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000741 if (Node->isSuperReceiver())
742 OS << "super.";
Richard Trieu6d92e502014-02-06 23:26:23 +0000743 else if (Node->isObjectReceiver() && Node->getBase()) {
Steve Naroffec944032008-05-30 00:40:33 +0000744 PrintExpr(Node->getBase());
745 OS << ".";
Richard Trieu6d92e502014-02-06 23:26:23 +0000746 } else if (Node->isClassReceiver() && Node->getClassReceiver()) {
747 OS << Node->getClassReceiver()->getName() << ".";
Steve Naroffec944032008-05-30 00:40:33 +0000748 }
Fariborz Jahanian681c0752010-10-14 16:04:05 +0000749
John McCallb7bd14f2010-12-02 01:19:52 +0000750 if (Node->isImplicitProperty())
Aaron Ballmanb190f972014-01-03 17:59:55 +0000751 Node->getImplicitPropertyGetter()->getSelector().print(OS);
John McCallb7bd14f2010-12-02 01:19:52 +0000752 else
753 OS << Node->getExplicitProperty()->getName();
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000754}
755
Ted Kremeneke65b0862012-03-06 20:05:56 +0000756void StmtPrinter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *Node) {
757
758 PrintExpr(Node->getBaseExpr());
759 OS << "[";
760 PrintExpr(Node->getKeyExpr());
761 OS << "]";
762}
763
Chris Lattner6307f192008-08-10 01:53:14 +0000764void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
Anders Carlsson625bfc82007-07-21 05:21:51 +0000765 switch (Node->getIdentType()) {
766 default:
David Blaikie83d382b2011-09-23 05:06:16 +0000767 llvm_unreachable("unknown case");
Chris Lattner6307f192008-08-10 01:53:14 +0000768 case PredefinedExpr::Func:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000769 OS << "__func__";
770 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000771 case PredefinedExpr::Function:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000772 OS << "__FUNCTION__";
773 break;
David Majnemerbed356a2013-11-06 23:31:56 +0000774 case PredefinedExpr::FuncDName:
775 OS << "__FUNCDNAME__";
776 break;
Nico Weber3a691a32012-06-23 02:07:59 +0000777 case PredefinedExpr::LFunction:
778 OS << "L__FUNCTION__";
779 break;
Chris Lattner6307f192008-08-10 01:53:14 +0000780 case PredefinedExpr::PrettyFunction:
Anders Carlsson625bfc82007-07-21 05:21:51 +0000781 OS << "__PRETTY_FUNCTION__";
782 break;
783 }
784}
785
Steve Naroffae4143e2007-04-26 20:39:23 +0000786void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000787 unsigned value = Node->getValue();
Douglas Gregorfb65e592011-07-27 05:40:30 +0000788
789 switch (Node->getKind()) {
790 case CharacterLiteral::Ascii: break; // no prefix.
791 case CharacterLiteral::Wide: OS << 'L'; break;
792 case CharacterLiteral::UTF16: OS << 'u'; break;
793 case CharacterLiteral::UTF32: OS << 'U'; break;
794 }
795
Chris Lattner666115c2007-07-13 23:58:20 +0000796 switch (value) {
797 case '\\':
798 OS << "'\\\\'";
799 break;
800 case '\'':
801 OS << "'\\''";
802 break;
803 case '\a':
804 // TODO: K&R: the meaning of '\\a' is different in traditional C
805 OS << "'\\a'";
806 break;
807 case '\b':
808 OS << "'\\b'";
809 break;
810 // Nonstandard escape sequence.
811 /*case '\e':
812 OS << "'\\e'";
813 break;*/
814 case '\f':
815 OS << "'\\f'";
816 break;
817 case '\n':
818 OS << "'\\n'";
819 break;
820 case '\r':
821 OS << "'\\r'";
822 break;
823 case '\t':
824 OS << "'\\t'";
825 break;
826 case '\v':
827 OS << "'\\v'";
828 break;
829 default:
Jordan Rose00d1b592013-02-08 22:30:27 +0000830 if (value < 256 && isPrintable((unsigned char)value))
Chris Lattner666115c2007-07-13 23:58:20 +0000831 OS << "'" << (char)value << "'";
Jordan Rose00d1b592013-02-08 22:30:27 +0000832 else if (value < 256)
833 OS << "'\\x" << llvm::format("%02x", value) << "'";
834 else if (value <= 0xFFFF)
835 OS << "'\\u" << llvm::format("%04x", value) << "'";
836 else
837 OS << "'\\U" << llvm::format("%08x", value) << "'";
Chris Lattner6e9d9b32007-07-13 05:18:11 +0000838 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000839}
840
Steve Naroffdf7855b2007-02-21 23:46:25 +0000841void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
Chris Lattner06430412007-05-21 05:45:03 +0000842 bool isSigned = Node->getType()->isSignedIntegerType();
843 OS << Node->getValue().toString(10, isSigned);
Mike Stump11289f42009-09-09 15:08:12 +0000844
Chris Lattner06430412007-05-21 05:45:03 +0000845 // Emit suffixes. Integer literals are always a builtin integer type.
John McCall9dd450b2009-09-21 23:43:11 +0000846 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
David Blaikie83d382b2011-09-23 05:06:16 +0000847 default: llvm_unreachable("Unexpected type for integer literal!");
Richard Trieu8b626ba2011-11-07 18:40:31 +0000848 // FIXME: The Short and UShort cases are to handle cases where a short
849 // integeral literal is formed during template instantiation. They should
850 // be removed when template instantiation no longer needs integer literals.
851 case BuiltinType::Short:
852 case BuiltinType::UShort:
Chris Lattner06430412007-05-21 05:45:03 +0000853 case BuiltinType::Int: break; // no suffix.
854 case BuiltinType::UInt: OS << 'U'; break;
855 case BuiltinType::Long: OS << 'L'; break;
856 case BuiltinType::ULong: OS << "UL"; break;
857 case BuiltinType::LongLong: OS << "LL"; break;
858 case BuiltinType::ULongLong: OS << "ULL"; break;
Richard Trieu8b626ba2011-11-07 18:40:31 +0000859 case BuiltinType::Int128: OS << "i128"; break;
860 case BuiltinType::UInt128: OS << "Ui128"; break;
Chris Lattner06430412007-05-21 05:45:03 +0000861 }
Chris Lattner882f7882006-11-04 18:52:07 +0000862}
Benjamin Kramer8a526762012-09-20 14:07:17 +0000863
864static void PrintFloatingLiteral(raw_ostream &OS, FloatingLiteral *Node,
865 bool PrintSuffix) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000866 SmallString<16> Str;
Eli Friedman53392062011-10-05 20:32:03 +0000867 Node->getValue().toString(Str);
868 OS << Str;
Benjamin Kramer8a526762012-09-20 14:07:17 +0000869 if (Str.find_first_not_of("-0123456789") == StringRef::npos)
870 OS << '.'; // Trailing dot in order to separate from ints.
871
872 if (!PrintSuffix)
873 return;
874
875 // Emit suffixes. Float literals are always a builtin float type.
876 switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
877 default: llvm_unreachable("Unexpected type for float literal!");
878 case BuiltinType::Half: break; // FIXME: suffix?
879 case BuiltinType::Double: break; // no suffix.
880 case BuiltinType::Float: OS << 'F'; break;
881 case BuiltinType::LongDouble: OS << 'L'; break;
882 }
883}
884
885void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
886 PrintFloatingLiteral(OS, Node, /*PrintSuffix=*/true);
Chris Lattner882f7882006-11-04 18:52:07 +0000887}
Chris Lattner1c20a172007-08-26 03:42:43 +0000888
889void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
890 PrintExpr(Node->getSubExpr());
891 OS << "i";
892}
893
Steve Naroffdf7855b2007-02-21 23:46:25 +0000894void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
Richard Trieudc355912012-06-13 20:25:24 +0000895 Str->outputString(OS);
Chris Lattner882f7882006-11-04 18:52:07 +0000896}
897void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
898 OS << "(";
899 PrintExpr(Node->getSubExpr());
Chris Lattnera076fde2007-05-31 18:21:33 +0000900 OS << ")";
Chris Lattner882f7882006-11-04 18:52:07 +0000901}
902void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
Chris Lattnere5b60442007-08-23 21:46:40 +0000903 if (!Node->isPostfix()) {
Chris Lattner15768702006-11-05 23:54:51 +0000904 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Mike Stump11289f42009-09-09 15:08:12 +0000905
Eli Friedman1cf25362009-06-14 22:39:26 +0000906 // Print a space if this is an "identifier operator" like __real, or if
907 // it might be concatenated incorrectly like '+'.
Chris Lattnere5b60442007-08-23 21:46:40 +0000908 switch (Node->getOpcode()) {
909 default: break;
John McCalle3027922010-08-25 11:45:40 +0000910 case UO_Real:
911 case UO_Imag:
912 case UO_Extension:
Chris Lattnere5b60442007-08-23 21:46:40 +0000913 OS << ' ';
914 break;
John McCalle3027922010-08-25 11:45:40 +0000915 case UO_Plus:
916 case UO_Minus:
Eli Friedman1cf25362009-06-14 22:39:26 +0000917 if (isa<UnaryOperator>(Node->getSubExpr()))
918 OS << ' ';
919 break;
Chris Lattnere5b60442007-08-23 21:46:40 +0000920 }
921 }
Chris Lattner882f7882006-11-04 18:52:07 +0000922 PrintExpr(Node->getSubExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000923
Chris Lattner15768702006-11-05 23:54:51 +0000924 if (Node->isPostfix())
925 OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
Chris Lattner882f7882006-11-04 18:52:07 +0000926}
Chris Lattner98dbf0a2007-08-30 17:59:59 +0000927
Douglas Gregor882211c2010-04-28 22:16:22 +0000928void StmtPrinter::VisitOffsetOfExpr(OffsetOfExpr *Node) {
929 OS << "__builtin_offsetof(";
Benjamin Kramerc5720e92013-02-22 14:19:01 +0000930 Node->getTypeSourceInfo()->getType().print(OS, Policy);
931 OS << ", ";
Douglas Gregor882211c2010-04-28 22:16:22 +0000932 bool PrintedSomething = false;
933 for (unsigned i = 0, n = Node->getNumComponents(); i < n; ++i) {
934 OffsetOfExpr::OffsetOfNode ON = Node->getComponent(i);
935 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Array) {
936 // Array node
937 OS << "[";
938 PrintExpr(Node->getIndexExpr(ON.getArrayExprIndex()));
939 OS << "]";
940 PrintedSomething = true;
941 continue;
942 }
Douglas Gregord1702062010-04-29 00:18:15 +0000943
944 // Skip implicit base indirections.
945 if (ON.getKind() == OffsetOfExpr::OffsetOfNode::Base)
946 continue;
947
Douglas Gregor882211c2010-04-28 22:16:22 +0000948 // Field or identifier node.
949 IdentifierInfo *Id = ON.getFieldName();
950 if (!Id)
951 continue;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000952
Douglas Gregor882211c2010-04-28 22:16:22 +0000953 if (PrintedSomething)
954 OS << ".";
955 else
956 PrintedSomething = true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000957 OS << Id->getName();
Douglas Gregor882211c2010-04-28 22:16:22 +0000958 }
959 OS << ")";
960}
961
Peter Collingbournee190dee2011-03-11 19:24:49 +0000962void StmtPrinter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *Node){
963 switch(Node->getKind()) {
964 case UETT_SizeOf:
965 OS << "sizeof";
966 break;
967 case UETT_AlignOf:
Jordan Rose58d54722012-06-30 21:33:57 +0000968 if (Policy.LangOpts.CPlusPlus)
969 OS << "alignof";
970 else if (Policy.LangOpts.C11)
971 OS << "_Alignof";
972 else
973 OS << "__alignof";
Peter Collingbournee190dee2011-03-11 19:24:49 +0000974 break;
975 case UETT_VecStep:
976 OS << "vec_step";
977 break;
978 }
Benjamin Kramerc5720e92013-02-22 14:19:01 +0000979 if (Node->isArgumentType()) {
980 OS << '(';
981 Node->getArgumentType().print(OS, Policy);
982 OS << ')';
983 } else {
Sebastian Redl6f282892008-11-11 17:56:53 +0000984 OS << " ";
985 PrintExpr(Node->getArgumentExpr());
986 }
Chris Lattner882f7882006-11-04 18:52:07 +0000987}
Peter Collingbourne91147592011-04-15 00:35:48 +0000988
989void StmtPrinter::VisitGenericSelectionExpr(GenericSelectionExpr *Node) {
990 OS << "_Generic(";
991 PrintExpr(Node->getControllingExpr());
992 for (unsigned i = 0; i != Node->getNumAssocs(); ++i) {
993 OS << ", ";
994 QualType T = Node->getAssocType(i);
995 if (T.isNull())
996 OS << "default";
997 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +0000998 T.print(OS, Policy);
Peter Collingbourne91147592011-04-15 00:35:48 +0000999 OS << ": ";
1000 PrintExpr(Node->getAssocExpr(i));
1001 }
1002 OS << ")";
1003}
1004
Chris Lattner882f7882006-11-04 18:52:07 +00001005void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
Ted Kremenekc81614d2007-08-20 16:18:38 +00001006 PrintExpr(Node->getLHS());
Chris Lattner882f7882006-11-04 18:52:07 +00001007 OS << "[";
Ted Kremenekc81614d2007-08-20 16:18:38 +00001008 PrintExpr(Node->getRHS());
Chris Lattner882f7882006-11-04 18:52:07 +00001009 OS << "]";
1010}
1011
Peter Collingbourne4b279a02011-02-08 21:17:54 +00001012void StmtPrinter::PrintCallArgs(CallExpr *Call) {
Chris Lattner882f7882006-11-04 18:52:07 +00001013 for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001014 if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
1015 // Don't print any defaulted arguments
1016 break;
1017 }
1018
Chris Lattner882f7882006-11-04 18:52:07 +00001019 if (i) OS << ", ";
1020 PrintExpr(Call->getArg(i));
1021 }
Peter Collingbourne4b279a02011-02-08 21:17:54 +00001022}
1023
1024void StmtPrinter::VisitCallExpr(CallExpr *Call) {
1025 PrintExpr(Call->getCallee());
1026 OS << "(";
1027 PrintCallArgs(Call);
Chris Lattner882f7882006-11-04 18:52:07 +00001028 OS << ")";
1029}
1030void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
Douglas Gregore4b414c2009-01-08 22:45:41 +00001031 // FIXME: Suppress printing implicit bases (like "this")
1032 PrintExpr(Node->getBase());
David Blaikie8fab8e52012-11-12 19:12:12 +00001033
1034 MemberExpr *ParentMember = dyn_cast<MemberExpr>(Node->getBase());
David Blaikie6bffd6f2012-11-12 19:32:32 +00001035 FieldDecl *ParentDecl = ParentMember
1036 ? dyn_cast<FieldDecl>(ParentMember->getMemberDecl()) : NULL;
David Blaikie8fab8e52012-11-12 19:12:12 +00001037
David Blaikie6bffd6f2012-11-12 19:32:32 +00001038 if (!ParentDecl || !ParentDecl->isAnonymousStructOrUnion())
David Blaikie8fab8e52012-11-12 19:12:12 +00001039 OS << (Node->isArrow() ? "->" : ".");
David Blaikie8fab8e52012-11-12 19:12:12 +00001040
Fariborz Jahanian2990c022010-01-11 21:17:32 +00001041 if (FieldDecl *FD = dyn_cast<FieldDecl>(Node->getMemberDecl()))
1042 if (FD->isAnonymousStructOrUnion())
1043 return;
David Blaikie8fab8e52012-11-12 19:12:12 +00001044
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001045 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1046 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001047 if (Node->hasTemplateKeyword())
1048 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001049 OS << Node->getMemberNameInfo();
John McCallb3774b52010-08-19 23:49:38 +00001050 if (Node->hasExplicitTemplateArgs())
Benjamin Kramer9170e912013-02-22 15:46:01 +00001051 TemplateSpecializationType::PrintTemplateArgumentList(
1052 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001053}
Steve Naroffe87026a2009-07-24 17:54:45 +00001054void StmtPrinter::VisitObjCIsaExpr(ObjCIsaExpr *Node) {
1055 PrintExpr(Node->getBase());
1056 OS << (Node->isArrow() ? "->isa" : ".isa");
1057}
1058
Nate Begemance4d7fc2008-04-18 23:10:10 +00001059void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
Steve Narofff7a5da12007-07-28 23:10:27 +00001060 PrintExpr(Node->getBase());
1061 OS << ".";
1062 OS << Node->getAccessor().getName();
1063}
Douglas Gregorf19b2312008-10-28 15:36:24 +00001064void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001065 OS << '(';
1066 Node->getTypeAsWritten().print(OS, Policy);
1067 OS << ')';
Chris Lattner882f7882006-11-04 18:52:07 +00001068 PrintExpr(Node->getSubExpr());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001069}
Steve Naroff57eb2c52007-07-19 21:32:11 +00001070void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001071 OS << '(';
1072 Node->getType().print(OS, Policy);
1073 OS << ')';
Steve Naroff57eb2c52007-07-19 21:32:11 +00001074 PrintExpr(Node->getInitializer());
1075}
Steve Naroff7a5af782007-07-13 16:58:59 +00001076void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
Alp Toker028ed912013-12-06 17:56:43 +00001077 // No need to print anything, simply forward to the subexpression.
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001078 PrintExpr(Node->getSubExpr());
Steve Naroff7a5af782007-07-13 16:58:59 +00001079}
Chris Lattner882f7882006-11-04 18:52:07 +00001080void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
1081 PrintExpr(Node->getLHS());
1082 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1083 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001084}
Chris Lattner86928112007-08-25 02:00:02 +00001085void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
1086 PrintExpr(Node->getLHS());
1087 OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
1088 PrintExpr(Node->getRHS());
1089}
Chris Lattner882f7882006-11-04 18:52:07 +00001090void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
1091 PrintExpr(Node->getCond());
John McCallc07a0c72011-02-17 10:25:35 +00001092 OS << " ? ";
1093 PrintExpr(Node->getLHS());
1094 OS << " : ";
Chris Lattner882f7882006-11-04 18:52:07 +00001095 PrintExpr(Node->getRHS());
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001096}
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001097
Chris Lattnereefa10e2007-05-28 06:56:27 +00001098// GNU extensions.
1099
John McCallc07a0c72011-02-17 10:25:35 +00001100void
1101StmtPrinter::VisitBinaryConditionalOperator(BinaryConditionalOperator *Node) {
1102 PrintExpr(Node->getCommon());
1103 OS << " ?: ";
1104 PrintExpr(Node->getFalseExpr());
1105}
Chris Lattnerd268a7a2007-08-03 17:31:20 +00001106void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
Chris Lattnereefa10e2007-05-28 06:56:27 +00001107 OS << "&&" << Node->getLabel()->getName();
Chris Lattnereefa10e2007-05-28 06:56:27 +00001108}
1109
Chris Lattner366727f2007-07-24 16:58:17 +00001110void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
1111 OS << "(";
1112 PrintRawCompoundStmt(E->getSubStmt());
1113 OS << ")";
1114}
1115
Steve Naroff9efdabc2007-08-03 21:21:27 +00001116void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
1117 OS << "__builtin_choose_expr(";
1118 PrintExpr(Node->getCond());
Chris Lattner81a96882007-08-04 00:20:15 +00001119 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +00001120 PrintExpr(Node->getLHS());
Chris Lattner81a96882007-08-04 00:20:15 +00001121 OS << ", ";
Steve Naroff9efdabc2007-08-03 21:21:27 +00001122 PrintExpr(Node->getRHS());
1123 OS << ")";
1124}
Chris Lattner366727f2007-07-24 16:58:17 +00001125
Douglas Gregor3be4b122008-11-29 04:51:27 +00001126void StmtPrinter::VisitGNUNullExpr(GNUNullExpr *) {
1127 OS << "__null";
1128}
1129
Eli Friedmana1b4ed82008-05-14 19:38:39 +00001130void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
1131 OS << "__builtin_shufflevector(";
1132 for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
1133 if (i) OS << ", ";
1134 PrintExpr(Node->getExpr(i));
1135 }
1136 OS << ")";
1137}
1138
Hal Finkelc4d7c822013-09-18 03:29:45 +00001139void StmtPrinter::VisitConvertVectorExpr(ConvertVectorExpr *Node) {
1140 OS << "__builtin_convertvector(";
1141 PrintExpr(Node->getSrcExpr());
1142 OS << ", ";
1143 Node->getType().print(OS, Policy);
1144 OS << ")";
1145}
1146
Anders Carlsson4692db02007-08-31 04:56:16 +00001147void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
Douglas Gregor36098ff2009-05-30 00:56:08 +00001148 if (Node->getSyntacticForm()) {
1149 Visit(Node->getSyntacticForm());
1150 return;
1151 }
1152
Anders Carlsson4692db02007-08-31 04:56:16 +00001153 OS << "{ ";
1154 for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
1155 if (i) OS << ", ";
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001156 if (Node->getInit(i))
1157 PrintExpr(Node->getInit(i));
1158 else
1159 OS << "0";
Anders Carlsson4692db02007-08-31 04:56:16 +00001160 }
1161 OS << " }";
1162}
1163
Nate Begeman5ec4b312009-08-10 23:49:36 +00001164void StmtPrinter::VisitParenListExpr(ParenListExpr* Node) {
1165 OS << "( ";
1166 for (unsigned i = 0, e = Node->getNumExprs(); i != e; ++i) {
1167 if (i) OS << ", ";
1168 PrintExpr(Node->getExpr(i));
1169 }
1170 OS << " )";
1171}
1172
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001173void StmtPrinter::VisitDesignatedInitExpr(DesignatedInitExpr *Node) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001174 for (DesignatedInitExpr::designators_iterator D = Node->designators_begin(),
1175 DEnd = Node->designators_end();
1176 D != DEnd; ++D) {
1177 if (D->isFieldDesignator()) {
1178 if (D->getDotLoc().isInvalid())
1179 OS << D->getFieldName()->getName() << ":";
1180 else
1181 OS << "." << D->getFieldName()->getName();
1182 } else {
1183 OS << "[";
1184 if (D->isArrayDesignator()) {
1185 PrintExpr(Node->getArrayIndex(*D));
1186 } else {
1187 PrintExpr(Node->getArrayRangeStart(*D));
1188 OS << " ... ";
Mike Stump11289f42009-09-09 15:08:12 +00001189 PrintExpr(Node->getArrayRangeEnd(*D));
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001190 }
1191 OS << "]";
1192 }
1193 }
1194
1195 OS << " = ";
1196 PrintExpr(Node->getInit());
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001197}
1198
Douglas Gregor0202cb42009-01-29 17:44:32 +00001199void StmtPrinter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001200 if (Policy.LangOpts.CPlusPlus) {
1201 OS << "/*implicit*/";
1202 Node->getType().print(OS, Policy);
1203 OS << "()";
1204 } else {
1205 OS << "/*implicit*/(";
1206 Node->getType().print(OS, Policy);
1207 OS << ')';
Douglas Gregor278f52e2009-05-30 00:08:05 +00001208 if (Node->getType()->isRecordType())
1209 OS << "{}";
1210 else
1211 OS << 0;
1212 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001213}
1214
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001215void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
Eli Friedman79635842009-05-30 04:20:30 +00001216 OS << "__builtin_va_arg(";
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001217 PrintExpr(Node->getSubExpr());
1218 OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001219 Node->getType().print(OS, Policy);
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001220 OS << ")";
1221}
1222
John McCallfe96e0b2011-11-06 09:01:30 +00001223void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
1224 PrintExpr(Node->getSyntacticForm());
1225}
1226
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001227void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
Eli Friedmanc2025562011-10-11 20:00:47 +00001228 const char *Name = 0;
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001229 switch (Node->getOp()) {
Richard Smithfeea8832012-04-12 05:08:17 +00001230#define BUILTIN(ID, TYPE, ATTRS)
1231#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1232 case AtomicExpr::AO ## ID: \
1233 Name = #ID "("; \
1234 break;
1235#include "clang/Basic/Builtins.def"
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001236 }
1237 OS << Name;
Richard Smithfeea8832012-04-12 05:08:17 +00001238
1239 // AtomicExpr stores its subexpressions in a permuted order.
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001240 PrintExpr(Node->getPtr());
Richard Smithfeea8832012-04-12 05:08:17 +00001241 if (Node->getOp() != AtomicExpr::AO__c11_atomic_load &&
1242 Node->getOp() != AtomicExpr::AO__atomic_load_n) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001243 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001244 PrintExpr(Node->getVal1());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001245 }
Richard Smithfeea8832012-04-12 05:08:17 +00001246 if (Node->getOp() == AtomicExpr::AO__atomic_exchange ||
1247 Node->isCmpXChg()) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001248 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001249 PrintExpr(Node->getVal2());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001250 }
Richard Smithfeea8832012-04-12 05:08:17 +00001251 if (Node->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
1252 Node->getOp() == AtomicExpr::AO__atomic_compare_exchange_n) {
Richard Smithfeea8832012-04-12 05:08:17 +00001253 OS << ", ";
Richard Smithdf6bee82013-05-01 19:02:43 +00001254 PrintExpr(Node->getWeak());
Richard Smithfeea8832012-04-12 05:08:17 +00001255 }
Richard Smithdf6bee82013-05-01 19:02:43 +00001256 if (Node->getOp() != AtomicExpr::AO__c11_atomic_init) {
1257 OS << ", ";
David Chisnallfa35df62012-01-16 17:27:18 +00001258 PrintExpr(Node->getOrder());
Richard Smithdf6bee82013-05-01 19:02:43 +00001259 }
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001260 if (Node->isCmpXChg()) {
1261 OS << ", ";
1262 PrintExpr(Node->getOrderFail());
1263 }
1264 OS << ")";
1265}
1266
Chris Lattnereefa10e2007-05-28 06:56:27 +00001267// C++
Douglas Gregor993603d2008-11-14 16:09:21 +00001268void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
1269 const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
1270 "",
1271#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1272 Spelling,
1273#include "clang/Basic/OperatorKinds.def"
1274 };
1275
1276 OverloadedOperatorKind Kind = Node->getOperator();
1277 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
1278 if (Node->getNumArgs() == 1) {
1279 OS << OpStrings[Kind] << ' ';
1280 PrintExpr(Node->getArg(0));
1281 } else {
1282 PrintExpr(Node->getArg(0));
1283 OS << ' ' << OpStrings[Kind];
1284 }
Eli Friedman8e236422012-10-12 22:45:14 +00001285 } else if (Kind == OO_Arrow) {
1286 PrintExpr(Node->getArg(0));
Douglas Gregor993603d2008-11-14 16:09:21 +00001287 } else if (Kind == OO_Call) {
1288 PrintExpr(Node->getArg(0));
1289 OS << '(';
1290 for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
1291 if (ArgIdx > 1)
1292 OS << ", ";
1293 if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
1294 PrintExpr(Node->getArg(ArgIdx));
1295 }
1296 OS << ')';
1297 } else if (Kind == OO_Subscript) {
1298 PrintExpr(Node->getArg(0));
1299 OS << '[';
1300 PrintExpr(Node->getArg(1));
1301 OS << ']';
1302 } else if (Node->getNumArgs() == 1) {
1303 OS << OpStrings[Kind] << ' ';
1304 PrintExpr(Node->getArg(0));
1305 } else if (Node->getNumArgs() == 2) {
1306 PrintExpr(Node->getArg(0));
1307 OS << ' ' << OpStrings[Kind] << ' ';
1308 PrintExpr(Node->getArg(1));
1309 } else {
David Blaikie83d382b2011-09-23 05:06:16 +00001310 llvm_unreachable("unknown overloaded operator");
Douglas Gregor993603d2008-11-14 16:09:21 +00001311 }
1312}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001313
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001314void StmtPrinter::VisitCXXMemberCallExpr(CXXMemberCallExpr *Node) {
Benjamin Kramer00e8a192014-02-25 18:03:55 +00001315 // If we have a conversion operator call only print the argument.
1316 CXXMethodDecl *MD = Node->getMethodDecl();
1317 if (MD && isa<CXXConversionDecl>(MD)) {
1318 PrintExpr(Node->getImplicitObjectArgument());
1319 return;
1320 }
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001321 VisitCallExpr(cast<CallExpr>(Node));
1322}
1323
Peter Collingbourne41f85462011-02-09 21:07:24 +00001324void StmtPrinter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *Node) {
1325 PrintExpr(Node->getCallee());
1326 OS << "<<<";
1327 PrintCallArgs(Node->getConfig());
1328 OS << ">>>(";
1329 PrintCallArgs(Node);
1330 OS << ")";
1331}
1332
Douglas Gregore200adc2008-10-27 19:41:14 +00001333void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
1334 OS << Node->getCastName() << '<';
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001335 Node->getTypeAsWritten().print(OS, Policy);
1336 OS << ">(";
Chris Lattnereefa10e2007-05-28 06:56:27 +00001337 PrintExpr(Node->getSubExpr());
1338 OS << ")";
1339}
1340
Douglas Gregore200adc2008-10-27 19:41:14 +00001341void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
1342 VisitCXXNamedCastExpr(Node);
1343}
1344
1345void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
1346 VisitCXXNamedCastExpr(Node);
1347}
1348
1349void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
1350 VisitCXXNamedCastExpr(Node);
1351}
1352
1353void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
1354 VisitCXXNamedCastExpr(Node);
1355}
1356
Sebastian Redlc4704762008-11-11 11:37:55 +00001357void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
1358 OS << "typeid(";
1359 if (Node->isTypeOperand()) {
David Majnemer143c55e2013-09-27 07:04:31 +00001360 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
Sebastian Redlc4704762008-11-11 11:37:55 +00001361 } else {
1362 PrintExpr(Node->getExprOperand());
1363 }
1364 OS << ")";
1365}
1366
Francois Pichet9f4f2072010-09-08 12:20:18 +00001367void StmtPrinter::VisitCXXUuidofExpr(CXXUuidofExpr *Node) {
1368 OS << "__uuidof(";
1369 if (Node->isTypeOperand()) {
David Majnemer143c55e2013-09-27 07:04:31 +00001370 Node->getTypeOperandSourceInfo()->getType().print(OS, Policy);
Francois Pichet9f4f2072010-09-08 12:20:18 +00001371 } else {
1372 PrintExpr(Node->getExprOperand());
1373 }
1374 OS << ")";
1375}
1376
John McCall5e77d762013-04-16 07:28:30 +00001377void StmtPrinter::VisitMSPropertyRefExpr(MSPropertyRefExpr *Node) {
1378 PrintExpr(Node->getBaseExpr());
1379 if (Node->isArrow())
1380 OS << "->";
1381 else
1382 OS << ".";
1383 if (NestedNameSpecifier *Qualifier =
1384 Node->getQualifierLoc().getNestedNameSpecifier())
1385 Qualifier->print(OS, Policy);
1386 OS << Node->getPropertyDecl()->getDeclName();
1387}
1388
Richard Smithc67fdd42012-03-07 08:35:16 +00001389void StmtPrinter::VisitUserDefinedLiteral(UserDefinedLiteral *Node) {
1390 switch (Node->getLiteralOperatorKind()) {
1391 case UserDefinedLiteral::LOK_Raw:
Richard Smith29e95952012-03-09 10:10:02 +00001392 OS << cast<StringLiteral>(Node->getArg(0)->IgnoreImpCasts())->getString();
Richard Smithc67fdd42012-03-07 08:35:16 +00001393 break;
1394 case UserDefinedLiteral::LOK_Template: {
Richard Smith29e95952012-03-09 10:10:02 +00001395 DeclRefExpr *DRE = cast<DeclRefExpr>(Node->getCallee()->IgnoreImpCasts());
1396 const TemplateArgumentList *Args =
1397 cast<FunctionDecl>(DRE->getDecl())->getTemplateSpecializationArgs();
1398 assert(Args);
1399 const TemplateArgument &Pack = Args->get(0);
1400 for (TemplateArgument::pack_iterator I = Pack.pack_begin(),
1401 E = Pack.pack_end(); I != E; ++I) {
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001402 char C = (char)I->getAsIntegral().getZExtValue();
Richard Smithc67fdd42012-03-07 08:35:16 +00001403 OS << C;
1404 }
1405 break;
1406 }
Richard Smith75025ba2012-03-08 09:02:38 +00001407 case UserDefinedLiteral::LOK_Integer: {
1408 // Print integer literal without suffix.
1409 IntegerLiteral *Int = cast<IntegerLiteral>(Node->getCookedLiteral());
1410 OS << Int->getValue().toString(10, /*isSigned*/false);
1411 break;
1412 }
Benjamin Kramer8a526762012-09-20 14:07:17 +00001413 case UserDefinedLiteral::LOK_Floating: {
1414 // Print floating literal without suffix.
1415 FloatingLiteral *Float = cast<FloatingLiteral>(Node->getCookedLiteral());
1416 PrintFloatingLiteral(OS, Float, /*PrintSuffix=*/false);
1417 break;
1418 }
Richard Smithc67fdd42012-03-07 08:35:16 +00001419 case UserDefinedLiteral::LOK_String:
1420 case UserDefinedLiteral::LOK_Character:
1421 PrintExpr(Node->getCookedLiteral());
1422 break;
1423 }
1424 OS << Node->getUDSuffix()->getName();
1425}
1426
Chris Lattnereefa10e2007-05-28 06:56:27 +00001427void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
1428 OS << (Node->getValue() ? "true" : "false");
1429}
1430
Sebastian Redl576fd422009-05-10 18:38:11 +00001431void StmtPrinter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *Node) {
1432 OS << "nullptr";
1433}
1434
Douglas Gregor97a9c812008-11-04 14:32:21 +00001435void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
1436 OS << "this";
1437}
1438
Chris Lattnerb7e656b2008-02-26 00:51:44 +00001439void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
1440 if (Node->getSubExpr() == 0)
1441 OS << "throw";
1442 else {
1443 OS << "throw ";
1444 PrintExpr(Node->getSubExpr());
1445 }
1446}
1447
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001448void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
Richard Smith852c9db2013-04-20 22:23:05 +00001449 // Nothing to print: we picked up the default argument.
1450}
1451
1452void StmtPrinter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *Node) {
1453 // Nothing to print: we picked up the default initializer.
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001454}
1455
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001456void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001457 Node->getType().print(OS, Policy);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001458 OS << "(";
1459 PrintExpr(Node->getSubExpr());
1460 OS << ")";
1461}
1462
Anders Carlsson993a4b32009-05-30 20:03:25 +00001463void StmtPrinter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *Node) {
1464 PrintExpr(Node->getSubExpr());
1465}
1466
Douglas Gregordd04d332009-01-16 18:33:17 +00001467void StmtPrinter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001468 Node->getType().print(OS, Policy);
Douglas Gregordd04d332009-01-16 18:33:17 +00001469 OS << "(";
1470 for (CXXTemporaryObjectExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001471 ArgEnd = Node->arg_end();
Douglas Gregordd04d332009-01-16 18:33:17 +00001472 Arg != ArgEnd; ++Arg) {
Rafael Espindola6f6f3c42013-05-24 16:11:44 +00001473 if (Arg->isDefaultArgument())
1474 break;
Douglas Gregordd04d332009-01-16 18:33:17 +00001475 if (Arg != Node->arg_begin())
1476 OS << ", ";
1477 PrintExpr(*Arg);
1478 }
1479 OS << ")";
1480}
1481
Douglas Gregore31e6062012-02-07 10:09:13 +00001482void StmtPrinter::VisitLambdaExpr(LambdaExpr *Node) {
1483 OS << '[';
1484 bool NeedComma = false;
1485 switch (Node->getCaptureDefault()) {
1486 case LCD_None:
1487 break;
1488
1489 case LCD_ByCopy:
1490 OS << '=';
1491 NeedComma = true;
1492 break;
1493
1494 case LCD_ByRef:
1495 OS << '&';
1496 NeedComma = true;
1497 break;
1498 }
1499 for (LambdaExpr::capture_iterator C = Node->explicit_capture_begin(),
1500 CEnd = Node->explicit_capture_end();
1501 C != CEnd;
1502 ++C) {
1503 if (NeedComma)
1504 OS << ", ";
1505 NeedComma = true;
1506
1507 switch (C->getCaptureKind()) {
1508 case LCK_This:
1509 OS << "this";
1510 break;
1511
1512 case LCK_ByRef:
Richard Smithbb13c9a2013-09-28 04:02:39 +00001513 if (Node->getCaptureDefault() != LCD_ByRef || C->isInitCapture())
Douglas Gregore31e6062012-02-07 10:09:13 +00001514 OS << '&';
1515 OS << C->getCapturedVar()->getName();
1516 break;
1517
1518 case LCK_ByCopy:
Douglas Gregore31e6062012-02-07 10:09:13 +00001519 OS << C->getCapturedVar()->getName();
1520 break;
1521 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00001522
1523 if (C->isInitCapture())
1524 PrintExpr(C->getCapturedVar()->getInit());
Douglas Gregore31e6062012-02-07 10:09:13 +00001525 }
1526 OS << ']';
1527
1528 if (Node->hasExplicitParameters()) {
1529 OS << " (";
1530 CXXMethodDecl *Method = Node->getCallOperator();
1531 NeedComma = false;
Aaron Ballman43b68be2014-03-07 17:50:17 +00001532 for (auto P : Method->params()) {
Douglas Gregore31e6062012-02-07 10:09:13 +00001533 if (NeedComma) {
1534 OS << ", ";
1535 } else {
1536 NeedComma = true;
1537 }
Aaron Ballman43b68be2014-03-07 17:50:17 +00001538 std::string ParamStr = P->getNameAsString();
1539 P->getOriginalType().print(OS, Policy, ParamStr);
Douglas Gregore31e6062012-02-07 10:09:13 +00001540 }
1541 if (Method->isVariadic()) {
1542 if (NeedComma)
1543 OS << ", ";
1544 OS << "...";
1545 }
1546 OS << ')';
1547
1548 if (Node->isMutable())
1549 OS << " mutable";
1550
1551 const FunctionProtoType *Proto
1552 = Method->getType()->getAs<FunctionProtoType>();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001553 Proto->printExceptionSpecification(OS, Policy);
Douglas Gregore31e6062012-02-07 10:09:13 +00001554
Bill Wendling44426052012-12-20 19:22:21 +00001555 // FIXME: Attributes
Douglas Gregore31e6062012-02-07 10:09:13 +00001556
Douglas Gregor0c46b2b2012-02-13 22:00:16 +00001557 // Print the trailing return type if it was specified in the source.
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001558 if (Node->hasExplicitResultType()) {
1559 OS << " -> ";
Alp Toker314cc812014-01-25 16:55:45 +00001560 Proto->getReturnType().print(OS, Policy);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001561 }
Douglas Gregore31e6062012-02-07 10:09:13 +00001562 }
1563
1564 // Print the body.
1565 CompoundStmt *Body = Node->getBody();
1566 OS << ' ';
1567 PrintStmt(Body);
1568}
1569
Douglas Gregor747eb782010-07-08 06:14:04 +00001570void StmtPrinter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *Node) {
Douglas Gregor2b88c112010-09-08 00:15:04 +00001571 if (TypeSourceInfo *TSInfo = Node->getTypeSourceInfo())
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001572 TSInfo->getType().print(OS, Policy);
Douglas Gregor2b88c112010-09-08 00:15:04 +00001573 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001574 Node->getType().print(OS, Policy);
1575 OS << "()";
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001576}
1577
Sebastian Redlbd150f42008-11-21 19:14:01 +00001578void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
1579 if (E->isGlobalNew())
1580 OS << "::";
1581 OS << "new ";
1582 unsigned NumPlace = E->getNumPlacementArgs();
Eli Friedmana6fdfaa2012-10-18 20:54:37 +00001583 if (NumPlace > 0 && !isa<CXXDefaultArgExpr>(E->getPlacementArg(0))) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00001584 OS << "(";
1585 PrintExpr(E->getPlacementArg(0));
1586 for (unsigned i = 1; i < NumPlace; ++i) {
Eli Friedmana6fdfaa2012-10-18 20:54:37 +00001587 if (isa<CXXDefaultArgExpr>(E->getPlacementArg(i)))
1588 break;
Sebastian Redlbd150f42008-11-21 19:14:01 +00001589 OS << ", ";
1590 PrintExpr(E->getPlacementArg(i));
1591 }
1592 OS << ") ";
1593 }
1594 if (E->isParenTypeId())
1595 OS << "(";
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001596 std::string TypeS;
1597 if (Expr *Size = E->getArraySize()) {
1598 llvm::raw_string_ostream s(TypeS);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001599 s << '[';
Richard Smith235341b2012-08-16 03:56:14 +00001600 Size->printPretty(s, Helper, Policy);
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001601 s << ']';
Sebastian Redld6d55ee2008-12-02 22:08:59 +00001602 }
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001603 E->getAllocatedType().print(OS, Policy, TypeS);
Sebastian Redlbd150f42008-11-21 19:14:01 +00001604 if (E->isParenTypeId())
1605 OS << ")";
1606
Sebastian Redl6047f072012-02-16 12:22:20 +00001607 CXXNewExpr::InitializationStyle InitStyle = E->getInitializationStyle();
1608 if (InitStyle) {
1609 if (InitStyle == CXXNewExpr::CallInit)
1610 OS << "(";
1611 PrintExpr(E->getInitializer());
1612 if (InitStyle == CXXNewExpr::CallInit)
1613 OS << ")";
Sebastian Redlbd150f42008-11-21 19:14:01 +00001614 }
1615}
1616
1617void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1618 if (E->isGlobalDelete())
1619 OS << "::";
1620 OS << "delete ";
1621 if (E->isArrayForm())
1622 OS << "[] ";
1623 PrintExpr(E->getArgument());
1624}
1625
Douglas Gregorad8a3362009-09-04 17:36:40 +00001626void StmtPrinter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1627 PrintExpr(E->getBase());
1628 if (E->isArrow())
1629 OS << "->";
1630 else
1631 OS << '.';
1632 if (E->getQualifier())
1633 E->getQualifier()->print(OS, Policy);
Eli Friedman9cc8ac52012-10-23 20:26:57 +00001634 OS << "~";
Mike Stump11289f42009-09-09 15:08:12 +00001635
Douglas Gregor678f90d2010-02-25 01:56:36 +00001636 if (IdentifierInfo *II = E->getDestroyedTypeIdentifier())
1637 OS << II->getName();
1638 else
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001639 E->getDestroyedType().print(OS, Policy);
Douglas Gregorad8a3362009-09-04 17:36:40 +00001640}
1641
Anders Carlsson0781ce72009-04-23 02:32:43 +00001642void StmtPrinter::VisitCXXConstructExpr(CXXConstructExpr *E) {
Richard Smithd59b8322012-12-19 01:39:02 +00001643 if (E->isListInitialization())
1644 OS << "{ ";
1645
Douglas Gregorbaba85d2011-01-24 17:25:03 +00001646 for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
1647 if (isa<CXXDefaultArgExpr>(E->getArg(i))) {
1648 // Don't print any defaulted arguments
1649 break;
1650 }
1651
1652 if (i) OS << ", ";
1653 PrintExpr(E->getArg(i));
Fariborz Jahaniand0bbf662010-01-13 21:41:11 +00001654 }
Richard Smithd59b8322012-12-19 01:39:02 +00001655
1656 if (E->isListInitialization())
1657 OS << " }";
Anders Carlsson0781ce72009-04-23 02:32:43 +00001658}
1659
Richard Smithcc1b96d2013-06-12 22:31:48 +00001660void StmtPrinter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
1661 PrintExpr(E->getSubExpr());
1662}
1663
John McCall5d413782010-12-06 08:20:24 +00001664void StmtPrinter::VisitExprWithCleanups(ExprWithCleanups *E) {
Alp Toker028ed912013-12-06 17:56:43 +00001665 // Just forward to the subexpression.
Anders Carlssondefc6442009-04-24 22:47:04 +00001666 PrintExpr(E->getSubExpr());
1667}
1668
Mike Stump11289f42009-09-09 15:08:12 +00001669void
Douglas Gregorce934142009-05-20 18:46:25 +00001670StmtPrinter::VisitCXXUnresolvedConstructExpr(
1671 CXXUnresolvedConstructExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001672 Node->getTypeAsWritten().print(OS, Policy);
Douglas Gregorce934142009-05-20 18:46:25 +00001673 OS << "(";
1674 for (CXXUnresolvedConstructExpr::arg_iterator Arg = Node->arg_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001675 ArgEnd = Node->arg_end();
Douglas Gregorce934142009-05-20 18:46:25 +00001676 Arg != ArgEnd; ++Arg) {
1677 if (Arg != Node->arg_begin())
1678 OS << ", ";
1679 PrintExpr(*Arg);
1680 }
1681 OS << ")";
1682}
1683
John McCall8cd78132009-11-19 22:55:06 +00001684void StmtPrinter::VisitCXXDependentScopeMemberExpr(
1685 CXXDependentScopeMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001686 if (!Node->isImplicitAccess()) {
1687 PrintExpr(Node->getBase());
1688 OS << (Node->isArrow() ? "->" : ".");
1689 }
Douglas Gregorc26e0f62009-09-03 16:14:30 +00001690 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1691 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001692 if (Node->hasTemplateKeyword())
Douglas Gregor308047d2009-09-09 00:23:06 +00001693 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001694 OS << Node->getMemberNameInfo();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001695 if (Node->hasExplicitTemplateArgs())
1696 TemplateSpecializationType::PrintTemplateArgumentList(
1697 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
Douglas Gregora8db9542009-05-22 21:13:27 +00001698}
1699
John McCall10eae182009-11-30 22:42:35 +00001700void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
John McCall2d74de92009-12-01 22:10:20 +00001701 if (!Node->isImplicitAccess()) {
1702 PrintExpr(Node->getBase());
1703 OS << (Node->isArrow() ? "->" : ".");
1704 }
John McCall10eae182009-11-30 22:42:35 +00001705 if (NestedNameSpecifier *Qualifier = Node->getQualifier())
1706 Qualifier->print(OS, Policy);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001707 if (Node->hasTemplateKeyword())
1708 OS << "template ";
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001709 OS << Node->getMemberNameInfo();
Benjamin Kramer9170e912013-02-22 15:46:01 +00001710 if (Node->hasExplicitTemplateArgs())
1711 TemplateSpecializationType::PrintTemplateArgumentList(
1712 OS, Node->getTemplateArgs(), Node->getNumTemplateArgs(), Policy);
John McCall10eae182009-11-30 22:42:35 +00001713}
1714
Douglas Gregor29c42f22012-02-24 07:38:34 +00001715static const char *getTypeTraitName(TypeTrait TT) {
1716 switch (TT) {
Alp Toker95e7ff22014-01-01 05:57:51 +00001717#define TYPE_TRAIT_1(Spelling, Name, Key) \
1718case clang::UTT_##Name: return #Spelling;
Alp Tokercbb90342013-12-13 20:49:58 +00001719#define TYPE_TRAIT_2(Spelling, Name, Key) \
1720case clang::BTT_##Name: return #Spelling;
Alp Toker40f9b1c2013-12-12 21:23:03 +00001721#define TYPE_TRAIT_N(Spelling, Name, Key) \
1722 case clang::TT_##Name: return #Spelling;
1723#include "clang/Basic/TokenKinds.def"
Douglas Gregor29c42f22012-02-24 07:38:34 +00001724 }
1725 llvm_unreachable("Type trait not covered by switch");
1726}
1727
John Wiegley6242b6a2011-04-28 00:16:57 +00001728static const char *getTypeTraitName(ArrayTypeTrait ATT) {
1729 switch (ATT) {
1730 case ATT_ArrayRank: return "__array_rank";
1731 case ATT_ArrayExtent: return "__array_extent";
1732 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001733 llvm_unreachable("Array type trait not covered by switch");
John Wiegley6242b6a2011-04-28 00:16:57 +00001734}
1735
John Wiegleyf9f65842011-04-25 06:54:41 +00001736static const char *getExpressionTraitName(ExpressionTrait ET) {
1737 switch (ET) {
John Wiegleyf9f65842011-04-25 06:54:41 +00001738 case ET_IsLValueExpr: return "__is_lvalue_expr";
1739 case ET_IsRValueExpr: return "__is_rvalue_expr";
1740 }
Chandler Carruthe46eaf32011-05-01 07:23:23 +00001741 llvm_unreachable("Expression type trait not covered by switch");
John Wiegleyf9f65842011-04-25 06:54:41 +00001742}
1743
Douglas Gregor29c42f22012-02-24 07:38:34 +00001744void StmtPrinter::VisitTypeTraitExpr(TypeTraitExpr *E) {
1745 OS << getTypeTraitName(E->getTrait()) << "(";
1746 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) {
1747 if (I > 0)
1748 OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001749 E->getArg(I)->getType().print(OS, Policy);
Douglas Gregor29c42f22012-02-24 07:38:34 +00001750 }
1751 OS << ")";
1752}
1753
John Wiegley6242b6a2011-04-28 00:16:57 +00001754void StmtPrinter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001755 OS << getTypeTraitName(E->getTrait()) << '(';
1756 E->getQueriedType().print(OS, Policy);
1757 OS << ')';
John Wiegley6242b6a2011-04-28 00:16:57 +00001758}
1759
John Wiegleyf9f65842011-04-25 06:54:41 +00001760void StmtPrinter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001761 OS << getExpressionTraitName(E->getTrait()) << '(';
1762 PrintExpr(E->getQueriedExpression());
1763 OS << ')';
John Wiegleyf9f65842011-04-25 06:54:41 +00001764}
1765
Sebastian Redl4202c0f2010-09-10 20:55:43 +00001766void StmtPrinter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1767 OS << "noexcept(";
1768 PrintExpr(E->getOperand());
1769 OS << ")";
1770}
1771
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001772void StmtPrinter::VisitPackExpansionExpr(PackExpansionExpr *E) {
Douglas Gregore8e9dd62011-01-03 17:17:50 +00001773 PrintExpr(E->getPattern());
1774 OS << "...";
1775}
1776
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001777void StmtPrinter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001778 OS << "sizeof...(" << *E->getPack() << ")";
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001779}
1780
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001781void StmtPrinter::VisitSubstNonTypeTemplateParmPackExpr(
1782 SubstNonTypeTemplateParmPackExpr *Node) {
Benjamin Kramerdb0fc512012-02-07 11:57:57 +00001783 OS << *Node->getParameterPack();
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001784}
1785
John McCall7c454bb2011-07-15 05:09:51 +00001786void StmtPrinter::VisitSubstNonTypeTemplateParmExpr(
1787 SubstNonTypeTemplateParmExpr *Node) {
1788 Visit(Node->getReplacement());
1789}
1790
Richard Smithb15fe3a2012-09-12 00:56:43 +00001791void StmtPrinter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
1792 OS << *E->getParameterPack();
1793}
1794
Douglas Gregorfe314812011-06-21 17:03:29 +00001795void StmtPrinter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *Node){
1796 PrintExpr(Node->GetTemporaryExpr());
1797}
1798
Mike Stump11289f42009-09-09 15:08:12 +00001799// Obj-C
Anders Carlsson76f4a902007-08-21 17:43:55 +00001800
1801void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
1802 OS << "@";
1803 VisitStringLiteral(Node->getString());
1804}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001805
Patrick Beard0caa3942012-04-19 00:25:12 +00001806void StmtPrinter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
Ted Kremeneke65b0862012-03-06 20:05:56 +00001807 OS << "@";
Patrick Beard0caa3942012-04-19 00:25:12 +00001808 Visit(E->getSubExpr());
Ted Kremeneke65b0862012-03-06 20:05:56 +00001809}
1810
1811void StmtPrinter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
1812 OS << "@[ ";
1813 StmtRange ch = E->children();
1814 if (ch.first != ch.second) {
1815 while (1) {
1816 Visit(*ch.first);
1817 ++ch.first;
1818 if (ch.first == ch.second) break;
1819 OS << ", ";
1820 }
1821 }
1822 OS << " ]";
1823}
1824
1825void StmtPrinter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
1826 OS << "@{ ";
1827 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
1828 if (I > 0)
1829 OS << ", ";
1830
1831 ObjCDictionaryElement Element = E->getKeyValueElement(I);
1832 Visit(Element.Key);
1833 OS << " : ";
1834 Visit(Element.Value);
1835 if (Element.isPackExpansion())
1836 OS << "...";
1837 }
1838 OS << " }";
1839}
1840
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001841void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001842 OS << "@encode(";
1843 Node->getEncodedType().print(OS, Policy);
1844 OS << ')';
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001845}
1846
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001847void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
Aaron Ballmanb190f972014-01-03 17:59:55 +00001848 OS << "@selector(";
1849 Node->getSelector().print(OS);
1850 OS << ')';
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001851}
1852
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001853void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
Benjamin Kramerb89514a2011-10-14 18:45:37 +00001854 OS << "@protocol(" << *Node->getProtocol() << ')';
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001855}
1856
Steve Naroffd54978b2007-09-18 23:55:05 +00001857void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
1858 OS << "[";
Douglas Gregor9a129192010-04-21 00:45:42 +00001859 switch (Mess->getReceiverKind()) {
1860 case ObjCMessageExpr::Instance:
1861 PrintExpr(Mess->getInstanceReceiver());
1862 break;
1863
1864 case ObjCMessageExpr::Class:
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001865 Mess->getClassReceiver().print(OS, Policy);
Douglas Gregor9a129192010-04-21 00:45:42 +00001866 break;
1867
1868 case ObjCMessageExpr::SuperInstance:
1869 case ObjCMessageExpr::SuperClass:
1870 OS << "Super";
1871 break;
1872 }
1873
Ted Kremeneka06e7122008-05-02 17:32:38 +00001874 OS << ' ';
Ted Kremenekb65a67d2008-04-16 04:30:16 +00001875 Selector selector = Mess->getSelector();
Steve Naroffc6814ea2007-10-02 20:01:56 +00001876 if (selector.isUnarySelector()) {
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00001877 OS << selector.getNameForSlot(0);
Steve Naroffc6814ea2007-10-02 20:01:56 +00001878 } else {
1879 for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
Ted Kremeneka06e7122008-05-02 17:32:38 +00001880 if (i < selector.getNumArgs()) {
1881 if (i > 0) OS << ' ';
1882 if (selector.getIdentifierInfoForSlot(i))
Chris Lattner1cbaacc2008-11-24 04:00:27 +00001883 OS << selector.getIdentifierInfoForSlot(i)->getName() << ':';
Ted Kremeneka06e7122008-05-02 17:32:38 +00001884 else
1885 OS << ":";
1886 }
1887 else OS << ", "; // Handle variadic methods.
Mike Stump11289f42009-09-09 15:08:12 +00001888
Steve Naroffc6814ea2007-10-02 20:01:56 +00001889 PrintExpr(Mess->getArg(i));
1890 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001891 }
1892 OS << "]";
1893}
1894
Ted Kremeneke65b0862012-03-06 20:05:56 +00001895void StmtPrinter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *Node) {
1896 OS << (Node->getValue() ? "__objc_yes" : "__objc_no");
1897}
1898
John McCall31168b02011-06-15 23:02:42 +00001899void
1900StmtPrinter::VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
1901 PrintExpr(E->getSubExpr());
1902}
1903
1904void
1905StmtPrinter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001906 OS << '(' << E->getBridgeKindName();
1907 E->getType().print(OS, Policy);
1908 OS << ')';
John McCall31168b02011-06-15 23:02:42 +00001909 PrintExpr(E->getSubExpr());
1910}
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001911
Steve Naroffc540d662008-09-03 18:15:37 +00001912void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001913 BlockDecl *BD = Node->getBlockDecl();
Steve Naroffc540d662008-09-03 18:15:37 +00001914 OS << "^";
Mike Stump11289f42009-09-09 15:08:12 +00001915
Steve Naroffc540d662008-09-03 18:15:37 +00001916 const FunctionType *AFT = Node->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00001917
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001918 if (isa<FunctionNoProtoType>(AFT)) {
Steve Naroffc540d662008-09-03 18:15:37 +00001919 OS << "()";
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001920 } else if (!BD->param_empty() || cast<FunctionProtoType>(AFT)->isVariadic()) {
Steve Naroffc540d662008-09-03 18:15:37 +00001921 OS << '(';
Steve Naroff415d3d52008-10-08 17:01:13 +00001922 for (BlockDecl::param_iterator AI = BD->param_begin(),
1923 E = BD->param_end(); AI != E; ++AI) {
1924 if (AI != BD->param_begin()) OS << ", ";
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001925 std::string ParamStr = (*AI)->getNameAsString();
1926 (*AI)->getType().print(OS, Policy, ParamStr);
Steve Naroffc540d662008-09-03 18:15:37 +00001927 }
Mike Stump11289f42009-09-09 15:08:12 +00001928
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001929 const FunctionProtoType *FT = cast<FunctionProtoType>(AFT);
Steve Naroffc540d662008-09-03 18:15:37 +00001930 if (FT->isVariadic()) {
Steve Naroff415d3d52008-10-08 17:01:13 +00001931 if (!BD->param_empty()) OS << ", ";
Steve Naroffc540d662008-09-03 18:15:37 +00001932 OS << "...";
1933 }
1934 OS << ')';
1935 }
Fariborz Jahanian1ace8cb2012-12-04 21:15:23 +00001936 OS << "{ }";
Steve Naroffc540d662008-09-03 18:15:37 +00001937}
1938
Ted Kremenekf551f322011-11-30 22:08:08 +00001939void StmtPrinter::VisitOpaqueValueExpr(OpaqueValueExpr *Node) {
1940 PrintExpr(Node->getSourceExpr());
1941}
John McCall8d69a212010-11-15 23:31:06 +00001942
Tanya Lattner55808c12011-06-04 00:47:47 +00001943void StmtPrinter::VisitAsTypeExpr(AsTypeExpr *Node) {
1944 OS << "__builtin_astype(";
1945 PrintExpr(Node->getSrcExpr());
Benjamin Kramerc5720e92013-02-22 14:19:01 +00001946 OS << ", ";
1947 Node->getType().print(OS, Policy);
Tanya Lattner55808c12011-06-04 00:47:47 +00001948 OS << ")";
1949}
1950
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001951//===----------------------------------------------------------------------===//
1952// Stmt method implementations
1953//===----------------------------------------------------------------------===//
1954
Craig Topperc571c812013-08-22 06:02:26 +00001955void Stmt::dumpPretty(const ASTContext &Context) const {
Richard Smith235341b2012-08-16 03:56:14 +00001956 printPretty(llvm::errs(), 0, PrintingPolicy(Context.getLangOpts()));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001957}
1958
Richard Smith235341b2012-08-16 03:56:14 +00001959void Stmt::printPretty(raw_ostream &OS,
1960 PrinterHelper *Helper,
Douglas Gregor7de59662009-05-29 20:38:28 +00001961 const PrintingPolicy &Policy,
1962 unsigned Indentation) const {
Chris Lattner882f7882006-11-04 18:52:07 +00001963 if (this == 0) {
1964 OS << "<NULL>";
1965 return;
1966 }
1967
Richard Smith235341b2012-08-16 03:56:14 +00001968 StmtPrinter P(OS, Helper, Policy, Indentation);
Chris Lattner62249a62007-08-21 04:04:25 +00001969 P.Visit(const_cast<Stmt*>(this));
Chris Lattnera3bcb7a2006-11-04 07:16:25 +00001970}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001971
1972//===----------------------------------------------------------------------===//
1973// PrinterHelper
1974//===----------------------------------------------------------------------===//
1975
1976// Implement virtual destructor.
Gabor Greif412af032007-09-11 15:32:40 +00001977PrinterHelper::~PrinterHelper() {}